Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 <tuple> |
| 12 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 13 | #include "absl/memory/memory.h" |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 14 | #include "api/jsep.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 15 | #include "api/peerconnectionproxy.h" |
| 16 | #include "media/base/fakemediaengine.h" |
| 17 | #include "pc/mediasession.h" |
| 18 | #include "pc/peerconnection.h" |
| 19 | #include "pc/peerconnectionfactory.h" |
| 20 | #include "pc/peerconnectionwrapper.h" |
| 21 | #include "pc/sdputils.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 22 | #include "pc/test/fakesctptransport.h" |
| 23 | #include "rtc_base/gunit.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 24 | #include "rtc_base/virtualsocketserver.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 25 | #include "system_wrappers/include/metrics_default.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 30 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 31 | using ::testing::Values; |
| 32 | |
| 33 | static constexpr int kDefaultTimeout = 10000; |
| 34 | |
| 35 | int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) { |
| 36 | int signature = 0; |
| 37 | for (const auto it : events) { |
| 38 | signature |= static_cast<int>(it); |
| 39 | } |
| 40 | return signature; |
| 41 | } |
| 42 | |
| 43 | class PeerConnectionFactoryForUsageHistogramTest |
| 44 | : public rtc::RefCountedObject<PeerConnectionFactory> { |
| 45 | public: |
| 46 | PeerConnectionFactoryForUsageHistogramTest() |
| 47 | : rtc::RefCountedObject<PeerConnectionFactory>( |
| 48 | rtc::Thread::Current(), |
| 49 | rtc::Thread::Current(), |
| 50 | rtc::Thread::Current(), |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 51 | absl::make_unique<cricket::FakeMediaEngine>(), |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 52 | CreateCallFactory(), |
| 53 | nullptr) {} |
| 54 | |
| 55 | void ActionsBeforeInitializeForTesting(PeerConnectionInterface* pc) override { |
| 56 | PeerConnection* internal_pc = static_cast<PeerConnection*>(pc); |
| 57 | if (return_histogram_very_quickly_) { |
| 58 | internal_pc->ReturnHistogramVeryQuicklyForTesting(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void ReturnHistogramVeryQuickly() { return_histogram_very_quickly_ = true; } |
| 63 | |
| 64 | private: |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 65 | bool return_histogram_very_quickly_ = false; |
| 66 | }; |
| 67 | |
| 68 | class PeerConnectionWrapperForUsageHistogramTest; |
| 69 | typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr; |
| 70 | |
| 71 | class ObserverForUsageHistogramTest : public MockPeerConnectionObserver { |
| 72 | public: |
| 73 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
| 74 | void PrepareToExchangeCandidates(RawWrapperPtr other) { |
| 75 | candidate_target_ = other; |
| 76 | } |
| 77 | |
| 78 | bool HaveDataChannel() { return last_datachannel_; } |
| 79 | |
| 80 | private: |
| 81 | RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions. |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | class PeerConnectionWrapperForUsageHistogramTest |
| 85 | : public PeerConnectionWrapper { |
| 86 | public: |
| 87 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 88 | |
| 89 | PeerConnection* GetInternalPeerConnection() { |
| 90 | auto* pci = |
| 91 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 92 | pc()); |
| 93 | return static_cast<PeerConnection*>(pci->internal()); |
| 94 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 95 | |
| 96 | void PrepareToExchangeCandidates( |
| 97 | PeerConnectionWrapperForUsageHistogramTest* other) { |
| 98 | static_cast<ObserverForUsageHistogramTest*>(observer()) |
| 99 | ->PrepareToExchangeCandidates(other); |
| 100 | static_cast<ObserverForUsageHistogramTest*>(other->observer()) |
| 101 | ->PrepareToExchangeCandidates(this); |
| 102 | } |
| 103 | |
| 104 | bool IsConnected() { |
| 105 | return pc()->ice_connection_state() == |
| 106 | PeerConnectionInterface::kIceConnectionConnected || |
| 107 | pc()->ice_connection_state() == |
| 108 | PeerConnectionInterface::kIceConnectionCompleted; |
| 109 | } |
| 110 | |
| 111 | bool HaveDataChannel() { |
| 112 | return static_cast<ObserverForUsageHistogramTest*>(observer()) |
| 113 | ->HaveDataChannel(); |
| 114 | } |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 115 | void AddOrBufferIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 116 | if (!pc()->AddIceCandidate(candidate)) { |
| 117 | std::string sdp; |
| 118 | EXPECT_TRUE(candidate->ToString(&sdp)); |
| 119 | std::unique_ptr<webrtc::IceCandidateInterface> candidate_copy( |
| 120 | CreateIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(), |
| 121 | sdp, nullptr)); |
| 122 | buffered_candidates_.push_back(std::move(candidate_copy)); |
| 123 | } |
| 124 | } |
| 125 | void AddBufferedIceCandidates() { |
| 126 | for (const auto& candidate : buffered_candidates_) { |
| 127 | EXPECT_TRUE(pc()->AddIceCandidate(candidate.get())); |
| 128 | } |
| 129 | buffered_candidates_.clear(); |
| 130 | } |
| 131 | bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) { |
| 132 | PrepareToExchangeCandidates(callee); |
| 133 | EXPECT_TRUE(ExchangeOfferAnswerWith(callee)); |
| 134 | AddBufferedIceCandidates(); |
| 135 | callee->AddBufferedIceCandidates(); |
| 136 | EXPECT_TRUE_WAIT(IsConnected(), kDefaultTimeout); |
| 137 | EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout); |
| 138 | return IsConnected() && callee->IsConnected(); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | // Candidates that have been sent but not yet configured |
| 143 | std::vector<std::unique_ptr<webrtc::IceCandidateInterface>> |
| 144 | buffered_candidates_; |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 147 | void ObserverForUsageHistogramTest::OnIceCandidate( |
| 148 | const webrtc::IceCandidateInterface* candidate) { |
| 149 | if (candidate_target_) { |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 150 | this->candidate_target_->AddOrBufferIceCandidate(candidate); |
| 151 | } else { |
| 152 | FAIL() << "Early candidate detected"; |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 156 | class PeerConnectionUsageHistogramTest : public ::testing::Test { |
| 157 | protected: |
| 158 | typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest> |
| 159 | WrapperPtr; |
| 160 | |
| 161 | PeerConnectionUsageHistogramTest() |
| 162 | : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 163 | webrtc::metrics::Reset(); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | WrapperPtr CreatePeerConnection() { |
| 167 | return CreatePeerConnection( |
| 168 | RTCConfiguration(), PeerConnectionFactoryInterface::Options(), false); |
| 169 | } |
| 170 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 171 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 172 | return CreatePeerConnection( |
| 173 | config, PeerConnectionFactoryInterface::Options(), false); |
| 174 | } |
| 175 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 176 | WrapperPtr CreatePeerConnectionWithImmediateReport() { |
| 177 | return CreatePeerConnection( |
| 178 | RTCConfiguration(), PeerConnectionFactoryInterface::Options(), true); |
| 179 | } |
| 180 | |
| 181 | WrapperPtr CreatePeerConnection( |
| 182 | const RTCConfiguration& config, |
| 183 | const PeerConnectionFactoryInterface::Options factory_options, |
| 184 | bool immediate_report) { |
| 185 | rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory( |
| 186 | new PeerConnectionFactoryForUsageHistogramTest()); |
| 187 | pc_factory->SetOptions(factory_options); |
| 188 | RTC_CHECK(pc_factory->Initialize()); |
| 189 | if (immediate_report) { |
| 190 | pc_factory->ReturnHistogramVeryQuickly(); |
| 191 | } |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 192 | auto observer = absl::make_unique<ObserverForUsageHistogramTest>(); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 193 | auto pc = pc_factory->CreatePeerConnection(config, nullptr, nullptr, |
| 194 | observer.get()); |
| 195 | if (!pc) { |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 199 | auto wrapper = |
| 200 | absl::make_unique<PeerConnectionWrapperForUsageHistogramTest>( |
| 201 | pc_factory, pc, std::move(observer)); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 202 | return wrapper; |
| 203 | } |
| 204 | |
| 205 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 206 | rtc::AutoSocketServerThread main_; |
| 207 | }; |
| 208 | |
| 209 | TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) { |
| 210 | auto pc = CreatePeerConnectionWithImmediateReport(); |
| 211 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 212 | int expected_fingerprint = MakeUsageFingerprint({}); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 213 | ASSERT_TRUE_WAIT( |
| 214 | 1u == webrtc::metrics::NumSamples("WebRTC.PeerConnection.UsagePattern"), |
| 215 | kDefaultTimeout); |
| 216 | EXPECT_EQ(1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.UsagePattern", |
| 217 | expected_fingerprint)); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 220 | #ifndef WEBRTC_ANDROID |
| 221 | // These tests do not work on Android. Why is unclear. |
| 222 | // https://bugs.webrtc.org/9461 |
| 223 | |
| 224 | // Test getting the usage fingerprint for an audio/video connection. |
| 225 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) { |
| 226 | auto caller = CreatePeerConnection(); |
| 227 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 228 | caller->AddAudioTrack("audio"); |
| 229 | caller->AddVideoTrack("video"); |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 230 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 231 | caller->pc()->Close(); |
| 232 | callee->pc()->Close(); |
| 233 | int expected_fingerprint = MakeUsageFingerprint( |
| 234 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 235 | PeerConnection::UsageEvent::VIDEO_ADDED, |
| 236 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 237 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED, |
| 238 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 239 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 240 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 241 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 242 | EXPECT_EQ(2, |
| 243 | webrtc::metrics::NumSamples("WebRTC.PeerConnection.UsagePattern")); |
| 244 | EXPECT_EQ(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.UsagePattern", |
| 245 | expected_fingerprint)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | #ifdef HAVE_SCTP |
| 249 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) { |
| 250 | auto caller = CreatePeerConnection(); |
| 251 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 252 | caller->CreateDataChannel("foodata"); |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 253 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 254 | ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout); |
| 255 | caller->pc()->Close(); |
| 256 | callee->pc()->Close(); |
| 257 | int expected_fingerprint = MakeUsageFingerprint( |
| 258 | {PeerConnection::UsageEvent::DATA_ADDED, |
| 259 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 260 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED, |
| 261 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 262 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 263 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 264 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 265 | EXPECT_EQ(2, |
| 266 | webrtc::metrics::NumSamples("WebRTC.PeerConnection.UsagePattern")); |
| 267 | EXPECT_EQ(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.UsagePattern", |
| 268 | expected_fingerprint)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 269 | } |
| 270 | #endif // HAVE_SCTP |
| 271 | #endif // WEBRTC_ANDROID |
| 272 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 273 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) { |
| 274 | RTCConfiguration configuration; |
| 275 | PeerConnection::IceServer server; |
| 276 | server.urls = {"stun:dummy.stun.server/"}; |
| 277 | configuration.servers.push_back(server); |
| 278 | server.urls = {"turn:dummy.turn.server/"}; |
| 279 | server.username = "username"; |
| 280 | server.password = "password"; |
| 281 | configuration.servers.push_back(server); |
| 282 | auto caller = CreatePeerConnection(configuration); |
| 283 | ASSERT_TRUE(caller); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 284 | caller->pc()->Close(); |
| 285 | int expected_fingerprint = |
| 286 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 287 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 288 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 289 | EXPECT_EQ(1, |
| 290 | webrtc::metrics::NumSamples("WebRTC.PeerConnection.UsagePattern")); |
| 291 | EXPECT_EQ(1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.UsagePattern", |
| 292 | expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) { |
| 296 | RTCConfiguration configuration; |
| 297 | PeerConnection::IceServer server; |
| 298 | server.urls = {"stun:dummy.stun.server/"}; |
| 299 | configuration.servers.push_back(server); |
| 300 | server.urls = {"turn:dummy.turn.server/"}; |
| 301 | server.username = "username"; |
| 302 | server.password = "password"; |
| 303 | configuration.servers.push_back(server); |
| 304 | auto caller = CreatePeerConnection(); |
| 305 | ASSERT_TRUE(caller); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 306 | RTCError error; |
| 307 | caller->pc()->SetConfiguration(configuration, &error); |
| 308 | ASSERT_TRUE(error.ok()); |
| 309 | caller->pc()->Close(); |
| 310 | int expected_fingerprint = |
| 311 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 312 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 313 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 314 | EXPECT_EQ(1, |
| 315 | webrtc::metrics::NumSamples("WebRTC.PeerConnection.UsagePattern")); |
| 316 | EXPECT_EQ(1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.UsagePattern", |
| 317 | expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 320 | } // namespace webrtc |