1 /* 2 * Copyright 2012 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 #ifndef PC_TEST_INTEGRATION_TEST_HELPERS_H_ 12 #define PC_TEST_INTEGRATION_TEST_HELPERS_H_ 13 14 #include <limits.h> 15 #include <stdint.h> 16 #include <stdio.h> 17 18 #include <algorithm> 19 #include <functional> 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <set> 24 #include <string> 25 #include <utility> 26 #include <vector> 27 28 #include "absl/algorithm/container.h" 29 #include "absl/types/optional.h" 30 #include "api/audio_options.h" 31 #include "api/call/call_factory_interface.h" 32 #include "api/candidate.h" 33 #include "api/crypto/crypto_options.h" 34 #include "api/data_channel_interface.h" 35 #include "api/ice_transport_interface.h" 36 #include "api/jsep.h" 37 #include "api/media_stream_interface.h" 38 #include "api/media_types.h" 39 #include "api/peer_connection_interface.h" 40 #include "api/peer_connection_proxy.h" 41 #include "api/rtc_error.h" 42 #include "api/rtc_event_log/rtc_event_log_factory.h" 43 #include "api/rtc_event_log/rtc_event_log_factory_interface.h" 44 #include "api/rtc_event_log_output.h" 45 #include "api/rtp_receiver_interface.h" 46 #include "api/rtp_sender_interface.h" 47 #include "api/rtp_transceiver_interface.h" 48 #include "api/scoped_refptr.h" 49 #include "api/stats/rtc_stats.h" 50 #include "api/stats/rtc_stats_report.h" 51 #include "api/stats/rtcstats_objects.h" 52 #include "api/task_queue/default_task_queue_factory.h" 53 #include "api/task_queue/task_queue_factory.h" 54 #include "api/transport/field_trial_based_config.h" 55 #include "api/transport/webrtc_key_value_config.h" 56 #include "api/uma_metrics.h" 57 #include "api/video/video_rotation.h" 58 #include "api/video_codecs/sdp_video_format.h" 59 #include "api/video_codecs/video_decoder_factory.h" 60 #include "api/video_codecs/video_encoder_factory.h" 61 #include "call/call.h" 62 #include "logging/rtc_event_log/fake_rtc_event_log_factory.h" 63 #include "media/base/media_engine.h" 64 #include "media/base/stream_params.h" 65 #include "media/engine/fake_webrtc_video_engine.h" 66 #include "media/engine/webrtc_media_engine.h" 67 #include "media/engine/webrtc_media_engine_defaults.h" 68 #include "modules/audio_device/include/audio_device.h" 69 #include "modules/audio_processing/include/audio_processing.h" 70 #include "modules/audio_processing/test/audio_processing_builder_for_testing.h" 71 #include "p2p/base/fake_ice_transport.h" 72 #include "p2p/base/ice_transport_internal.h" 73 #include "p2p/base/mock_async_resolver.h" 74 #include "p2p/base/p2p_constants.h" 75 #include "p2p/base/port.h" 76 #include "p2p/base/port_allocator.h" 77 #include "p2p/base/port_interface.h" 78 #include "p2p/base/test_stun_server.h" 79 #include "p2p/base/test_turn_customizer.h" 80 #include "p2p/base/test_turn_server.h" 81 #include "p2p/client/basic_port_allocator.h" 82 #include "pc/dtmf_sender.h" 83 #include "pc/local_audio_source.h" 84 #include "pc/media_session.h" 85 #include "pc/peer_connection.h" 86 #include "pc/peer_connection_factory.h" 87 #include "pc/rtp_media_utils.h" 88 #include "pc/session_description.h" 89 #include "pc/test/fake_audio_capture_module.h" 90 #include "pc/test/fake_periodic_video_source.h" 91 #include "pc/test/fake_periodic_video_track_source.h" 92 #include "pc/test/fake_rtc_certificate_generator.h" 93 #include "pc/test/fake_video_track_renderer.h" 94 #include "pc/test/mock_peer_connection_observers.h" 95 #include "pc/video_track_source.h" 96 #include "rtc_base/checks.h" 97 #include "rtc_base/fake_clock.h" 98 #include "rtc_base/fake_mdns_responder.h" 99 #include "rtc_base/fake_network.h" 100 #include "rtc_base/firewall_socket_server.h" 101 #include "rtc_base/gunit.h" 102 #include "rtc_base/helpers.h" 103 #include "rtc_base/ip_address.h" 104 #include "rtc_base/location.h" 105 #include "rtc_base/logging.h" 106 #include "rtc_base/mdns_responder_interface.h" 107 #include "rtc_base/numerics/safe_conversions.h" 108 #include "rtc_base/ref_counted_object.h" 109 #include "rtc_base/rtc_certificate_generator.h" 110 #include "rtc_base/socket_address.h" 111 #include "rtc_base/ssl_stream_adapter.h" 112 #include "rtc_base/task_utils/pending_task_safety_flag.h" 113 #include "rtc_base/task_utils/to_queued_task.h" 114 #include "rtc_base/test_certificate_verifier.h" 115 #include "rtc_base/thread.h" 116 #include "rtc_base/time_utils.h" 117 #include "rtc_base/virtual_socket_server.h" 118 #include "system_wrappers/include/metrics.h" 119 #include "test/field_trial.h" 120 #include "test/gmock.h" 121 122 namespace webrtc { 123 124 using ::cricket::ContentInfo; 125 using ::cricket::StreamParams; 126 using ::rtc::SocketAddress; 127 using ::testing::_; 128 using ::testing::Combine; 129 using ::testing::Contains; 130 using ::testing::DoAll; 131 using ::testing::ElementsAre; 132 using ::testing::NiceMock; 133 using ::testing::Return; 134 using ::testing::SetArgPointee; 135 using ::testing::UnorderedElementsAreArray; 136 using ::testing::Values; 137 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; 138 139 static const int kDefaultTimeout = 10000; 140 static const int kMaxWaitForStatsMs = 3000; 141 static const int kMaxWaitForActivationMs = 5000; 142 static const int kMaxWaitForFramesMs = 10000; 143 // Default number of audio/video frames to wait for before considering a test 144 // successful. 145 static const int kDefaultExpectedAudioFrameCount = 3; 146 static const int kDefaultExpectedVideoFrameCount = 3; 147 148 static const char kDataChannelLabel[] = "data_channel"; 149 150 // SRTP cipher name negotiated by the tests. This must be updated if the 151 // default changes. 152 static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80; 153 static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM; 154 155 static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0); 156 157 // Helper function for constructing offer/answer options to initiate an ICE 158 // restart. 159 PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions(); 160 161 // Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic" 162 // attribute from received SDP, simulating a legacy endpoint. 163 void RemoveSsrcsAndMsids(cricket::SessionDescription* desc); 164 165 // Removes all stream information besides the stream ids, simulating an 166 // endpoint that only signals a=msid lines to convey stream_ids. 167 void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc); 168 169 int FindFirstMediaStatsIndexByKind( 170 const std::string& kind, 171 const std::vector<const webrtc::RTCMediaStreamTrackStats*>& 172 media_stats_vec); 173 174 class SignalingMessageReceiver { 175 public: 176 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0; 177 virtual void ReceiveIceMessage(const std::string& sdp_mid, 178 int sdp_mline_index, 179 const std::string& msg) = 0; 180 181 protected: SignalingMessageReceiver()182 SignalingMessageReceiver() {} ~SignalingMessageReceiver()183 virtual ~SignalingMessageReceiver() {} 184 }; 185 186 class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface { 187 public: MockRtpReceiverObserver(cricket::MediaType media_type)188 explicit MockRtpReceiverObserver(cricket::MediaType media_type) 189 : expected_media_type_(media_type) {} 190 OnFirstPacketReceived(cricket::MediaType media_type)191 void OnFirstPacketReceived(cricket::MediaType media_type) override { 192 ASSERT_EQ(expected_media_type_, media_type); 193 first_packet_received_ = true; 194 } 195 first_packet_received()196 bool first_packet_received() const { return first_packet_received_; } 197 ~MockRtpReceiverObserver()198 virtual ~MockRtpReceiverObserver() {} 199 200 private: 201 bool first_packet_received_ = false; 202 cricket::MediaType expected_media_type_; 203 }; 204 205 // Helper class that wraps a peer connection, observes it, and can accept 206 // signaling messages from another wrapper. 207 // 208 // Uses a fake network, fake A/V capture, and optionally fake 209 // encoders/decoders, though they aren't used by default since they don't 210 // advertise support of any codecs. 211 // TODO(steveanton): See how this could become a subclass of 212 // PeerConnectionWrapper defined in peerconnectionwrapper.h. 213 class PeerConnectionIntegrationWrapper : public webrtc::PeerConnectionObserver, 214 public SignalingMessageReceiver { 215 public: 216 // Different factory methods for convenience. 217 // TODO(deadbeef): Could use the pattern of: 218 // 219 // PeerConnectionIntegrationWrapper = 220 // WrapperBuilder.WithConfig(...).WithOptions(...).build(); 221 // 222 // To reduce some code duplication. CreateWithDtlsIdentityStore(const std::string & debug_name,std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,rtc::Thread * network_thread,rtc::Thread * worker_thread)223 static PeerConnectionIntegrationWrapper* CreateWithDtlsIdentityStore( 224 const std::string& debug_name, 225 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, 226 rtc::Thread* network_thread, 227 rtc::Thread* worker_thread) { 228 PeerConnectionIntegrationWrapper* client( 229 new PeerConnectionIntegrationWrapper(debug_name)); 230 webrtc::PeerConnectionDependencies dependencies(nullptr); 231 dependencies.cert_generator = std::move(cert_generator); 232 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread, 233 worker_thread, nullptr, 234 /*reset_encoder_factory=*/false, 235 /*reset_decoder_factory=*/false)) { 236 delete client; 237 return nullptr; 238 } 239 return client; 240 } 241 pc_factory()242 webrtc::PeerConnectionFactoryInterface* pc_factory() const { 243 return peer_connection_factory_.get(); 244 } 245 pc()246 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); } 247 248 // If a signaling message receiver is set (via ConnectFakeSignaling), this 249 // will set the whole offer/answer exchange in motion. Just need to wait for 250 // the signaling state to reach "stable". CreateAndSetAndSignalOffer()251 void CreateAndSetAndSignalOffer() { 252 auto offer = CreateOfferAndWait(); 253 ASSERT_NE(nullptr, offer); 254 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer))); 255 } 256 257 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or 258 // when a remote offer is received (via fake signaling) and an answer is 259 // generated. By default, uses default options. SetOfferAnswerOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & options)260 void SetOfferAnswerOptions( 261 const PeerConnectionInterface::RTCOfferAnswerOptions& options) { 262 offer_answer_options_ = options; 263 } 264 265 // Set a callback to be invoked when SDP is received via the fake signaling 266 // channel, which provides an opportunity to munge (modify) the SDP. This is 267 // used to test SDP being applied that a PeerConnection would normally not 268 // generate, but a non-JSEP endpoint might. SetReceivedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)269 void SetReceivedSdpMunger( 270 std::function<void(cricket::SessionDescription*)> munger) { 271 received_sdp_munger_ = std::move(munger); 272 } 273 274 // Similar to the above, but this is run on SDP immediately after it's 275 // generated. SetGeneratedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)276 void SetGeneratedSdpMunger( 277 std::function<void(cricket::SessionDescription*)> munger) { 278 generated_sdp_munger_ = std::move(munger); 279 } 280 281 // Set a callback to be invoked when a remote offer is received via the fake 282 // signaling channel. This provides an opportunity to change the 283 // PeerConnection state before an answer is created and sent to the caller. SetRemoteOfferHandler(std::function<void ()> handler)284 void SetRemoteOfferHandler(std::function<void()> handler) { 285 remote_offer_handler_ = std::move(handler); 286 } 287 SetRemoteAsyncResolver(rtc::MockAsyncResolver * resolver)288 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) { 289 remote_async_resolver_ = resolver; 290 } 291 292 // Every ICE connection state in order that has been seen by the observer. 293 std::vector<PeerConnectionInterface::IceConnectionState> ice_connection_state_history()294 ice_connection_state_history() const { 295 return ice_connection_state_history_; 296 } clear_ice_connection_state_history()297 void clear_ice_connection_state_history() { 298 ice_connection_state_history_.clear(); 299 } 300 301 // Every standardized ICE connection state in order that has been seen by the 302 // observer. 303 std::vector<PeerConnectionInterface::IceConnectionState> standardized_ice_connection_state_history()304 standardized_ice_connection_state_history() const { 305 return standardized_ice_connection_state_history_; 306 } 307 308 // Every PeerConnection state in order that has been seen by the observer. 309 std::vector<PeerConnectionInterface::PeerConnectionState> peer_connection_state_history()310 peer_connection_state_history() const { 311 return peer_connection_state_history_; 312 } 313 314 // Every ICE gathering state in order that has been seen by the observer. 315 std::vector<PeerConnectionInterface::IceGatheringState> ice_gathering_state_history()316 ice_gathering_state_history() const { 317 return ice_gathering_state_history_; 318 } 319 std::vector<cricket::CandidatePairChangeEvent> ice_candidate_pair_change_history()320 ice_candidate_pair_change_history() const { 321 return ice_candidate_pair_change_history_; 322 } 323 324 // Every PeerConnection signaling state in order that has been seen by the 325 // observer. 326 std::vector<PeerConnectionInterface::SignalingState> peer_connection_signaling_state_history()327 peer_connection_signaling_state_history() const { 328 return peer_connection_signaling_state_history_; 329 } 330 AddAudioVideoTracks()331 void AddAudioVideoTracks() { 332 AddAudioTrack(); 333 AddVideoTrack(); 334 } 335 AddAudioTrack()336 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() { 337 return AddTrack(CreateLocalAudioTrack()); 338 } 339 AddVideoTrack()340 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() { 341 return AddTrack(CreateLocalVideoTrack()); 342 } 343 CreateLocalAudioTrack()344 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() { 345 cricket::AudioOptions options; 346 // Disable highpass filter so that we can get all the test audio frames. 347 options.highpass_filter = false; 348 rtc::scoped_refptr<webrtc::AudioSourceInterface> source = 349 peer_connection_factory_->CreateAudioSource(options); 350 // TODO(perkj): Test audio source when it is implemented. Currently audio 351 // always use the default input. 352 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(), 353 source); 354 } 355 CreateLocalVideoTrack()356 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() { 357 webrtc::FakePeriodicVideoSource::Config config; 358 config.timestamp_offset_ms = rtc::TimeMillis(); 359 return CreateLocalVideoTrackInternal(config); 360 } 361 362 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackWithConfig(webrtc::FakePeriodicVideoSource::Config config)363 CreateLocalVideoTrackWithConfig( 364 webrtc::FakePeriodicVideoSource::Config config) { 365 return CreateLocalVideoTrackInternal(config); 366 } 367 368 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation)369 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) { 370 webrtc::FakePeriodicVideoSource::Config config; 371 config.rotation = rotation; 372 config.timestamp_offset_ms = rtc::TimeMillis(); 373 return CreateLocalVideoTrackInternal(config); 374 } 375 376 rtc::scoped_refptr<RtpSenderInterface> AddTrack( 377 rtc::scoped_refptr<MediaStreamTrackInterface> track, 378 const std::vector<std::string>& stream_ids = {}) { 379 auto result = pc()->AddTrack(track, stream_ids); 380 EXPECT_EQ(RTCErrorType::NONE, result.error().type()); 381 return result.MoveValue(); 382 } 383 GetReceiversOfType(cricket::MediaType media_type)384 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType( 385 cricket::MediaType media_type) { 386 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers; 387 for (const auto& receiver : pc()->GetReceivers()) { 388 if (receiver->media_type() == media_type) { 389 receivers.push_back(receiver); 390 } 391 } 392 return receivers; 393 } 394 GetFirstTransceiverOfType(cricket::MediaType media_type)395 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType( 396 cricket::MediaType media_type) { 397 for (auto transceiver : pc()->GetTransceivers()) { 398 if (transceiver->receiver()->media_type() == media_type) { 399 return transceiver; 400 } 401 } 402 return nullptr; 403 } 404 SignalingStateStable()405 bool SignalingStateStable() { 406 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable; 407 } 408 CreateDataChannel()409 void CreateDataChannel() { CreateDataChannel(nullptr); } 410 CreateDataChannel(const webrtc::DataChannelInit * init)411 void CreateDataChannel(const webrtc::DataChannelInit* init) { 412 CreateDataChannel(kDataChannelLabel, init); 413 } 414 CreateDataChannel(const std::string & label,const webrtc::DataChannelInit * init)415 void CreateDataChannel(const std::string& label, 416 const webrtc::DataChannelInit* init) { 417 data_channel_ = pc()->CreateDataChannel(label, init); 418 ASSERT_TRUE(data_channel_.get() != nullptr); 419 data_observer_.reset(new MockDataChannelObserver(data_channel_)); 420 } 421 data_channel()422 DataChannelInterface* data_channel() { return data_channel_; } data_observer()423 const MockDataChannelObserver* data_observer() const { 424 return data_observer_.get(); 425 } 426 audio_frames_received()427 int audio_frames_received() const { 428 return fake_audio_capture_module_->frames_received(); 429 } 430 431 // Takes minimum of video frames received for each track. 432 // 433 // Can be used like: 434 // EXPECT_GE(expected_frames, min_video_frames_received_per_track()); 435 // 436 // To ensure that all video tracks received at least a certain number of 437 // frames. min_video_frames_received_per_track()438 int min_video_frames_received_per_track() const { 439 int min_frames = INT_MAX; 440 if (fake_video_renderers_.empty()) { 441 return 0; 442 } 443 444 for (const auto& pair : fake_video_renderers_) { 445 min_frames = std::min(min_frames, pair.second->num_rendered_frames()); 446 } 447 return min_frames; 448 } 449 450 // Returns a MockStatsObserver in a state after stats gathering finished, 451 // which can be used to access the gathered stats. OldGetStatsForTrack(webrtc::MediaStreamTrackInterface * track)452 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack( 453 webrtc::MediaStreamTrackInterface* track) { 454 rtc::scoped_refptr<MockStatsObserver> observer( 455 new rtc::RefCountedObject<MockStatsObserver>()); 456 EXPECT_TRUE(peer_connection_->GetStats( 457 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard)); 458 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); 459 return observer; 460 } 461 462 // Version that doesn't take a track "filter", and gathers all stats. OldGetStats()463 rtc::scoped_refptr<MockStatsObserver> OldGetStats() { 464 return OldGetStatsForTrack(nullptr); 465 } 466 467 // Synchronously gets stats and returns them. If it times out, fails the test 468 // and returns null. NewGetStats()469 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() { 470 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( 471 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); 472 peer_connection_->GetStats(callback); 473 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); 474 return callback->report(); 475 } 476 rendered_width()477 int rendered_width() { 478 EXPECT_FALSE(fake_video_renderers_.empty()); 479 return fake_video_renderers_.empty() 480 ? 0 481 : fake_video_renderers_.begin()->second->width(); 482 } 483 rendered_height()484 int rendered_height() { 485 EXPECT_FALSE(fake_video_renderers_.empty()); 486 return fake_video_renderers_.empty() 487 ? 0 488 : fake_video_renderers_.begin()->second->height(); 489 } 490 rendered_aspect_ratio()491 double rendered_aspect_ratio() { 492 if (rendered_height() == 0) { 493 return 0.0; 494 } 495 return static_cast<double>(rendered_width()) / rendered_height(); 496 } 497 rendered_rotation()498 webrtc::VideoRotation rendered_rotation() { 499 EXPECT_FALSE(fake_video_renderers_.empty()); 500 return fake_video_renderers_.empty() 501 ? webrtc::kVideoRotation_0 502 : fake_video_renderers_.begin()->second->rotation(); 503 } 504 local_rendered_width()505 int local_rendered_width() { 506 return local_video_renderer_ ? local_video_renderer_->width() : 0; 507 } 508 local_rendered_height()509 int local_rendered_height() { 510 return local_video_renderer_ ? local_video_renderer_->height() : 0; 511 } 512 local_rendered_aspect_ratio()513 double local_rendered_aspect_ratio() { 514 if (local_rendered_height() == 0) { 515 return 0.0; 516 } 517 return static_cast<double>(local_rendered_width()) / 518 local_rendered_height(); 519 } 520 number_of_remote_streams()521 size_t number_of_remote_streams() { 522 if (!pc()) { 523 return 0; 524 } 525 return pc()->remote_streams()->count(); 526 } 527 remote_streams()528 StreamCollectionInterface* remote_streams() const { 529 if (!pc()) { 530 ADD_FAILURE(); 531 return nullptr; 532 } 533 return pc()->remote_streams(); 534 } 535 local_streams()536 StreamCollectionInterface* local_streams() { 537 if (!pc()) { 538 ADD_FAILURE(); 539 return nullptr; 540 } 541 return pc()->local_streams(); 542 } 543 signaling_state()544 webrtc::PeerConnectionInterface::SignalingState signaling_state() { 545 return pc()->signaling_state(); 546 } 547 ice_connection_state()548 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() { 549 return pc()->ice_connection_state(); 550 } 551 552 webrtc::PeerConnectionInterface::IceConnectionState standardized_ice_connection_state()553 standardized_ice_connection_state() { 554 return pc()->standardized_ice_connection_state(); 555 } 556 ice_gathering_state()557 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() { 558 return pc()->ice_gathering_state(); 559 } 560 561 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by 562 // GetReceivers. They're updated automatically when a remote offer/answer 563 // from the fake signaling channel is applied, or when 564 // ResetRtpReceiverObservers below is called. 565 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& rtp_receiver_observers()566 rtp_receiver_observers() { 567 return rtp_receiver_observers_; 568 } 569 ResetRtpReceiverObservers()570 void ResetRtpReceiverObservers() { 571 rtp_receiver_observers_.clear(); 572 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver : 573 pc()->GetReceivers()) { 574 std::unique_ptr<MockRtpReceiverObserver> observer( 575 new MockRtpReceiverObserver(receiver->media_type())); 576 receiver->SetObserver(observer.get()); 577 rtp_receiver_observers_.push_back(std::move(observer)); 578 } 579 } 580 network_manager()581 rtc::FakeNetworkManager* network_manager() const { 582 return fake_network_manager_.get(); 583 } port_allocator()584 cricket::PortAllocator* port_allocator() const { return port_allocator_; } 585 event_log_factory()586 webrtc::FakeRtcEventLogFactory* event_log_factory() const { 587 return event_log_factory_; 588 } 589 last_candidate_gathered()590 const cricket::Candidate& last_candidate_gathered() const { 591 return last_candidate_gathered_; 592 } error_event()593 const cricket::IceCandidateErrorEvent& error_event() const { 594 return error_event_; 595 } 596 597 // Sets the mDNS responder for the owned fake network manager and keeps a 598 // reference to the responder. SetMdnsResponder(std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder)599 void SetMdnsResponder( 600 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) { 601 RTC_DCHECK(mdns_responder != nullptr); 602 mdns_responder_ = mdns_responder.get(); 603 network_manager()->set_mdns_responder(std::move(mdns_responder)); 604 } 605 606 // Returns null on failure. CreateOfferAndWait()607 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() { 608 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( 609 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); 610 pc()->CreateOffer(observer, offer_answer_options_); 611 return WaitForDescriptionFromObserver(observer); 612 } Rollback()613 bool Rollback() { 614 return SetRemoteDescription( 615 webrtc::CreateSessionDescription(SdpType::kRollback, "")); 616 } 617 618 // Functions for querying stats. StartWatchingDelayStats()619 void StartWatchingDelayStats() { 620 // Get the baseline numbers for audio_packets and audio_delay. 621 auto received_stats = NewGetStats(); 622 auto track_stats = 623 received_stats->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>()[0]; 624 ASSERT_TRUE(track_stats->relative_packet_arrival_delay.is_defined()); 625 auto rtp_stats = 626 received_stats->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>()[0]; 627 ASSERT_TRUE(rtp_stats->packets_received.is_defined()); 628 ASSERT_TRUE(rtp_stats->track_id.is_defined()); 629 audio_track_stats_id_ = track_stats->id(); 630 ASSERT_TRUE(received_stats->Get(audio_track_stats_id_)); 631 rtp_stats_id_ = rtp_stats->id(); 632 ASSERT_EQ(audio_track_stats_id_, *rtp_stats->track_id); 633 audio_packets_stat_ = *rtp_stats->packets_received; 634 audio_delay_stat_ = *track_stats->relative_packet_arrival_delay; 635 audio_samples_stat_ = *track_stats->total_samples_received; 636 audio_concealed_stat_ = *track_stats->concealed_samples; 637 } 638 UpdateDelayStats(std::string tag,int desc_size)639 void UpdateDelayStats(std::string tag, int desc_size) { 640 auto report = NewGetStats(); 641 auto track_stats = 642 report->GetAs<webrtc::RTCMediaStreamTrackStats>(audio_track_stats_id_); 643 ASSERT_TRUE(track_stats); 644 auto rtp_stats = 645 report->GetAs<webrtc::RTCInboundRTPStreamStats>(rtp_stats_id_); 646 ASSERT_TRUE(rtp_stats); 647 auto delta_packets = *rtp_stats->packets_received - audio_packets_stat_; 648 auto delta_rpad = 649 *track_stats->relative_packet_arrival_delay - audio_delay_stat_; 650 auto recent_delay = delta_packets > 0 ? delta_rpad / delta_packets : -1; 651 // The purpose of these checks is to sound the alarm early if we introduce 652 // serious regressions. The numbers are not acceptable for production, but 653 // occur on slow bots. 654 // 655 // An average relative packet arrival delay over the renegotiation of 656 // > 100 ms indicates that something is dramatically wrong, and will impact 657 // quality for sure. 658 // Worst bots: 659 // linux_x86_dbg at 0.206 660 #if !defined(NDEBUG) 661 EXPECT_GT(0.25, recent_delay) << tag << " size " << desc_size; 662 #else 663 EXPECT_GT(0.1, recent_delay) << tag << " size " << desc_size; 664 #endif 665 auto delta_samples = 666 *track_stats->total_samples_received - audio_samples_stat_; 667 auto delta_concealed = 668 *track_stats->concealed_samples - audio_concealed_stat_; 669 // These limits should be adjusted down as we improve: 670 // 671 // Concealing more than 4000 samples during a renegotiation is unacceptable. 672 // But some bots are slow. 673 674 // Worst bots: 675 // linux_more_configs bot at conceal count 5184 676 // android_arm_rel at conceal count 9241 677 // linux_x86_dbg at 15174 678 #if !defined(NDEBUG) 679 EXPECT_GT(18000U, delta_concealed) << "Concealed " << delta_concealed 680 << " of " << delta_samples << " samples"; 681 #else 682 EXPECT_GT(15000U, delta_concealed) << "Concealed " << delta_concealed 683 << " of " << delta_samples << " samples"; 684 #endif 685 // Concealing more than 20% of samples during a renegotiation is 686 // unacceptable. 687 // Worst bots: 688 // linux_more_configs bot at conceal rate 0.516 689 // linux_x86_dbg bot at conceal rate 0.854 690 if (delta_samples > 0) { 691 #if !defined(NDEBUG) 692 EXPECT_GT(0.95, 1.0 * delta_concealed / delta_samples) 693 << "Concealed " << delta_concealed << " of " << delta_samples 694 << " samples"; 695 #else 696 EXPECT_GT(0.6, 1.0 * delta_concealed / delta_samples) 697 << "Concealed " << delta_concealed << " of " << delta_samples 698 << " samples"; 699 #endif 700 } 701 // Increment trailing counters 702 audio_packets_stat_ = *rtp_stats->packets_received; 703 audio_delay_stat_ = *track_stats->relative_packet_arrival_delay; 704 audio_samples_stat_ = *track_stats->total_samples_received; 705 audio_concealed_stat_ = *track_stats->concealed_samples; 706 } 707 708 private: PeerConnectionIntegrationWrapper(const std::string & debug_name)709 explicit PeerConnectionIntegrationWrapper(const std::string& debug_name) 710 : debug_name_(debug_name) {} 711 Init(const PeerConnectionFactory::Options * options,const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies,rtc::Thread * network_thread,rtc::Thread * worker_thread,std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,bool reset_encoder_factory,bool reset_decoder_factory)712 bool Init(const PeerConnectionFactory::Options* options, 713 const PeerConnectionInterface::RTCConfiguration* config, 714 webrtc::PeerConnectionDependencies dependencies, 715 rtc::Thread* network_thread, 716 rtc::Thread* worker_thread, 717 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory, 718 bool reset_encoder_factory, 719 bool reset_decoder_factory) { 720 // There's an error in this test code if Init ends up being called twice. 721 RTC_DCHECK(!peer_connection_); 722 RTC_DCHECK(!peer_connection_factory_); 723 724 fake_network_manager_.reset(new rtc::FakeNetworkManager()); 725 fake_network_manager_->AddInterface(kDefaultLocalAddress); 726 727 std::unique_ptr<cricket::PortAllocator> port_allocator( 728 new cricket::BasicPortAllocator(fake_network_manager_.get())); 729 port_allocator_ = port_allocator.get(); 730 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); 731 if (!fake_audio_capture_module_) { 732 return false; 733 } 734 rtc::Thread* const signaling_thread = rtc::Thread::Current(); 735 736 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies; 737 pc_factory_dependencies.network_thread = network_thread; 738 pc_factory_dependencies.worker_thread = worker_thread; 739 pc_factory_dependencies.signaling_thread = signaling_thread; 740 pc_factory_dependencies.task_queue_factory = 741 webrtc::CreateDefaultTaskQueueFactory(); 742 pc_factory_dependencies.trials = std::make_unique<FieldTrialBasedConfig>(); 743 cricket::MediaEngineDependencies media_deps; 744 media_deps.task_queue_factory = 745 pc_factory_dependencies.task_queue_factory.get(); 746 media_deps.adm = fake_audio_capture_module_; 747 webrtc::SetMediaEngineDefaults(&media_deps); 748 749 if (reset_encoder_factory) { 750 media_deps.video_encoder_factory.reset(); 751 } 752 if (reset_decoder_factory) { 753 media_deps.video_decoder_factory.reset(); 754 } 755 756 if (!media_deps.audio_processing) { 757 // If the standard Creation method for APM returns a null pointer, instead 758 // use the builder for testing to create an APM object. 759 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create(); 760 } 761 762 media_deps.trials = pc_factory_dependencies.trials.get(); 763 764 pc_factory_dependencies.media_engine = 765 cricket::CreateMediaEngine(std::move(media_deps)); 766 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory(); 767 if (event_log_factory) { 768 event_log_factory_ = event_log_factory.get(); 769 pc_factory_dependencies.event_log_factory = std::move(event_log_factory); 770 } else { 771 pc_factory_dependencies.event_log_factory = 772 std::make_unique<webrtc::RtcEventLogFactory>( 773 pc_factory_dependencies.task_queue_factory.get()); 774 } 775 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory( 776 std::move(pc_factory_dependencies)); 777 778 if (!peer_connection_factory_) { 779 return false; 780 } 781 if (options) { 782 peer_connection_factory_->SetOptions(*options); 783 } 784 if (config) { 785 sdp_semantics_ = config->sdp_semantics; 786 } 787 788 dependencies.allocator = std::move(port_allocator); 789 peer_connection_ = CreatePeerConnection(config, std::move(dependencies)); 790 return peer_connection_.get() != nullptr; 791 } 792 CreatePeerConnection(const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)793 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection( 794 const PeerConnectionInterface::RTCConfiguration* config, 795 webrtc::PeerConnectionDependencies dependencies) { 796 PeerConnectionInterface::RTCConfiguration modified_config; 797 // If |config| is null, this will result in a default configuration being 798 // used. 799 if (config) { 800 modified_config = *config; 801 } 802 // Disable resolution adaptation; we don't want it interfering with the 803 // test results. 804 // TODO(deadbeef): Do something more robust. Since we're testing for aspect 805 // ratios and not specific resolutions, is this even necessary? 806 modified_config.set_cpu_adaptation(false); 807 808 dependencies.observer = this; 809 return peer_connection_factory_->CreatePeerConnection( 810 modified_config, std::move(dependencies)); 811 } 812 set_signaling_message_receiver(SignalingMessageReceiver * signaling_message_receiver)813 void set_signaling_message_receiver( 814 SignalingMessageReceiver* signaling_message_receiver) { 815 signaling_message_receiver_ = signaling_message_receiver; 816 } 817 set_signaling_delay_ms(int delay_ms)818 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; } 819 set_signal_ice_candidates(bool signal)820 void set_signal_ice_candidates(bool signal) { 821 signal_ice_candidates_ = signal; 822 } 823 CreateLocalVideoTrackInternal(webrtc::FakePeriodicVideoSource::Config config)824 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal( 825 webrtc::FakePeriodicVideoSource::Config config) { 826 // Set max frame rate to 10fps to reduce the risk of test flakiness. 827 // TODO(deadbeef): Do something more robust. 828 config.frame_interval_ms = 100; 829 830 video_track_sources_.emplace_back( 831 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>( 832 config, false /* remote */)); 833 rtc::scoped_refptr<webrtc::VideoTrackInterface> track( 834 peer_connection_factory_->CreateVideoTrack( 835 rtc::CreateRandomUuid(), video_track_sources_.back())); 836 if (!local_video_renderer_) { 837 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track)); 838 } 839 return track; 840 } 841 HandleIncomingOffer(const std::string & msg)842 void HandleIncomingOffer(const std::string& msg) { 843 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer"; 844 std::unique_ptr<SessionDescriptionInterface> desc = 845 webrtc::CreateSessionDescription(SdpType::kOffer, msg); 846 if (received_sdp_munger_) { 847 received_sdp_munger_(desc->description()); 848 } 849 850 EXPECT_TRUE(SetRemoteDescription(std::move(desc))); 851 // Setting a remote description may have changed the number of receivers, 852 // so reset the receiver observers. 853 ResetRtpReceiverObservers(); 854 if (remote_offer_handler_) { 855 remote_offer_handler_(); 856 } 857 auto answer = CreateAnswer(); 858 ASSERT_NE(nullptr, answer); 859 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer))); 860 } 861 HandleIncomingAnswer(const std::string & msg)862 void HandleIncomingAnswer(const std::string& msg) { 863 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer"; 864 std::unique_ptr<SessionDescriptionInterface> desc = 865 webrtc::CreateSessionDescription(SdpType::kAnswer, msg); 866 if (received_sdp_munger_) { 867 received_sdp_munger_(desc->description()); 868 } 869 870 EXPECT_TRUE(SetRemoteDescription(std::move(desc))); 871 // Set the RtpReceiverObserver after receivers are created. 872 ResetRtpReceiverObservers(); 873 } 874 875 // Returns null on failure. CreateAnswer()876 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() { 877 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( 878 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); 879 pc()->CreateAnswer(observer, offer_answer_options_); 880 return WaitForDescriptionFromObserver(observer); 881 } 882 WaitForDescriptionFromObserver(MockCreateSessionDescriptionObserver * observer)883 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver( 884 MockCreateSessionDescriptionObserver* observer) { 885 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout); 886 if (!observer->result()) { 887 return nullptr; 888 } 889 auto description = observer->MoveDescription(); 890 if (generated_sdp_munger_) { 891 generated_sdp_munger_(description->description()); 892 } 893 return description; 894 } 895 896 // Setting the local description and sending the SDP message over the fake 897 // signaling channel are combined into the same method because the SDP 898 // message needs to be sent as soon as SetLocalDescription finishes, without 899 // waiting for the observer to be called. This ensures that ICE candidates 900 // don't outrace the description. SetLocalDescriptionAndSendSdpMessage(std::unique_ptr<SessionDescriptionInterface> desc)901 bool SetLocalDescriptionAndSendSdpMessage( 902 std::unique_ptr<SessionDescriptionInterface> desc) { 903 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( 904 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); 905 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage"; 906 SdpType type = desc->GetType(); 907 std::string sdp; 908 EXPECT_TRUE(desc->ToString(&sdp)); 909 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp; 910 pc()->SetLocalDescription(observer, desc.release()); 911 RemoveUnusedVideoRenderers(); 912 // As mentioned above, we need to send the message immediately after 913 // SetLocalDescription. 914 SendSdpMessage(type, sdp); 915 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); 916 return true; 917 } 918 SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)919 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) { 920 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( 921 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); 922 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription"; 923 pc()->SetRemoteDescription(observer, desc.release()); 924 RemoveUnusedVideoRenderers(); 925 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); 926 return observer->result(); 927 } 928 929 // This is a work around to remove unused fake_video_renderers from 930 // transceivers that have either stopped or are no longer receiving. RemoveUnusedVideoRenderers()931 void RemoveUnusedVideoRenderers() { 932 if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) { 933 return; 934 } 935 auto transceivers = pc()->GetTransceivers(); 936 std::set<std::string> active_renderers; 937 for (auto& transceiver : transceivers) { 938 // Note - we don't check for direction here. This function is called 939 // before direction is set, and in that case, we should not remove 940 // the renderer. 941 if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) { 942 active_renderers.insert(transceiver->receiver()->track()->id()); 943 } 944 } 945 for (auto it = fake_video_renderers_.begin(); 946 it != fake_video_renderers_.end();) { 947 // Remove fake video renderers belonging to any non-active transceivers. 948 if (!active_renderers.count(it->first)) { 949 it = fake_video_renderers_.erase(it); 950 } else { 951 it++; 952 } 953 } 954 } 955 956 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by 957 // default). SendSdpMessage(SdpType type,const std::string & msg)958 void SendSdpMessage(SdpType type, const std::string& msg) { 959 if (signaling_delay_ms_ == 0) { 960 RelaySdpMessageIfReceiverExists(type, msg); 961 } else { 962 rtc::Thread::Current()->PostDelayedTask( 963 ToQueuedTask(task_safety_.flag(), 964 [this, type, msg] { 965 RelaySdpMessageIfReceiverExists(type, msg); 966 }), 967 signaling_delay_ms_); 968 } 969 } 970 RelaySdpMessageIfReceiverExists(SdpType type,const std::string & msg)971 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) { 972 if (signaling_message_receiver_) { 973 signaling_message_receiver_->ReceiveSdpMessage(type, msg); 974 } 975 } 976 977 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by 978 // default). SendIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)979 void SendIceMessage(const std::string& sdp_mid, 980 int sdp_mline_index, 981 const std::string& msg) { 982 if (signaling_delay_ms_ == 0) { 983 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg); 984 } else { 985 rtc::Thread::Current()->PostDelayedTask( 986 ToQueuedTask(task_safety_.flag(), 987 [this, sdp_mid, sdp_mline_index, msg] { 988 RelayIceMessageIfReceiverExists(sdp_mid, 989 sdp_mline_index, msg); 990 }), 991 signaling_delay_ms_); 992 } 993 } 994 RelayIceMessageIfReceiverExists(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)995 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid, 996 int sdp_mline_index, 997 const std::string& msg) { 998 if (signaling_message_receiver_) { 999 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index, 1000 msg); 1001 } 1002 } 1003 1004 // SignalingMessageReceiver callbacks. ReceiveSdpMessage(SdpType type,const std::string & msg)1005 void ReceiveSdpMessage(SdpType type, const std::string& msg) override { 1006 if (type == SdpType::kOffer) { 1007 HandleIncomingOffer(msg); 1008 } else { 1009 HandleIncomingAnswer(msg); 1010 } 1011 } 1012 ReceiveIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)1013 void ReceiveIceMessage(const std::string& sdp_mid, 1014 int sdp_mline_index, 1015 const std::string& msg) override { 1016 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage"; 1017 std::unique_ptr<webrtc::IceCandidateInterface> candidate( 1018 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr)); 1019 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get())); 1020 } 1021 1022 // PeerConnectionObserver callbacks. OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)1023 void OnSignalingChange( 1024 webrtc::PeerConnectionInterface::SignalingState new_state) override { 1025 EXPECT_EQ(pc()->signaling_state(), new_state); 1026 peer_connection_signaling_state_history_.push_back(new_state); 1027 } OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)1028 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver, 1029 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& 1030 streams) override { 1031 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { 1032 rtc::scoped_refptr<VideoTrackInterface> video_track( 1033 static_cast<VideoTrackInterface*>(receiver->track().get())); 1034 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) == 1035 fake_video_renderers_.end()); 1036 fake_video_renderers_[video_track->id()] = 1037 std::make_unique<FakeVideoTrackRenderer>(video_track); 1038 } 1039 } OnRemoveTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver)1040 void OnRemoveTrack( 1041 rtc::scoped_refptr<RtpReceiverInterface> receiver) override { 1042 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { 1043 auto it = fake_video_renderers_.find(receiver->track()->id()); 1044 if (it != fake_video_renderers_.end()) { 1045 fake_video_renderers_.erase(it); 1046 } else { 1047 RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer"; 1048 } 1049 } 1050 } OnRenegotiationNeeded()1051 void OnRenegotiationNeeded() override {} OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)1052 void OnIceConnectionChange( 1053 webrtc::PeerConnectionInterface::IceConnectionState new_state) override { 1054 EXPECT_EQ(pc()->ice_connection_state(), new_state); 1055 ice_connection_state_history_.push_back(new_state); 1056 } OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)1057 void OnStandardizedIceConnectionChange( 1058 webrtc::PeerConnectionInterface::IceConnectionState new_state) override { 1059 standardized_ice_connection_state_history_.push_back(new_state); 1060 } OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state)1061 void OnConnectionChange( 1062 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override { 1063 peer_connection_state_history_.push_back(new_state); 1064 } 1065 OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)1066 void OnIceGatheringChange( 1067 webrtc::PeerConnectionInterface::IceGatheringState new_state) override { 1068 EXPECT_EQ(pc()->ice_gathering_state(), new_state); 1069 ice_gathering_state_history_.push_back(new_state); 1070 } 1071 OnIceSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent & event)1072 void OnIceSelectedCandidatePairChanged( 1073 const cricket::CandidatePairChangeEvent& event) { 1074 ice_candidate_pair_change_history_.push_back(event); 1075 } 1076 OnIceCandidate(const webrtc::IceCandidateInterface * candidate)1077 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override { 1078 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate"; 1079 1080 if (remote_async_resolver_) { 1081 const auto& local_candidate = candidate->candidate(); 1082 if (local_candidate.address().IsUnresolvedIP()) { 1083 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE); 1084 rtc::SocketAddress resolved_addr(local_candidate.address()); 1085 const auto resolved_ip = mdns_responder_->GetMappedAddressForName( 1086 local_candidate.address().hostname()); 1087 RTC_DCHECK(!resolved_ip.IsNil()); 1088 resolved_addr.SetResolvedIP(resolved_ip); 1089 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _)) 1090 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true))); 1091 EXPECT_CALL(*remote_async_resolver_, Destroy(_)); 1092 } 1093 } 1094 1095 std::string ice_sdp; 1096 EXPECT_TRUE(candidate->ToString(&ice_sdp)); 1097 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) { 1098 // Remote party may be deleted. 1099 return; 1100 } 1101 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp); 1102 last_candidate_gathered_ = candidate->candidate(); 1103 } OnIceCandidateError(const std::string & address,int port,const std::string & url,int error_code,const std::string & error_text)1104 void OnIceCandidateError(const std::string& address, 1105 int port, 1106 const std::string& url, 1107 int error_code, 1108 const std::string& error_text) override { 1109 error_event_ = cricket::IceCandidateErrorEvent(address, port, url, 1110 error_code, error_text); 1111 } OnDataChannel(rtc::scoped_refptr<DataChannelInterface> data_channel)1112 void OnDataChannel( 1113 rtc::scoped_refptr<DataChannelInterface> data_channel) override { 1114 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel"; 1115 data_channel_ = data_channel; 1116 data_observer_.reset(new MockDataChannelObserver(data_channel)); 1117 } 1118 1119 std::string debug_name_; 1120 1121 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_; 1122 // Reference to the mDNS responder owned by |fake_network_manager_| after set. 1123 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr; 1124 1125 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; 1126 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> 1127 peer_connection_factory_; 1128 1129 cricket::PortAllocator* port_allocator_; 1130 // Needed to keep track of number of frames sent. 1131 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; 1132 // Needed to keep track of number of frames received. 1133 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>> 1134 fake_video_renderers_; 1135 // Needed to ensure frames aren't received for removed tracks. 1136 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>> 1137 removed_fake_video_renderers_; 1138 1139 // For remote peer communication. 1140 SignalingMessageReceiver* signaling_message_receiver_ = nullptr; 1141 int signaling_delay_ms_ = 0; 1142 bool signal_ice_candidates_ = true; 1143 cricket::Candidate last_candidate_gathered_; 1144 cricket::IceCandidateErrorEvent error_event_; 1145 1146 // Store references to the video sources we've created, so that we can stop 1147 // them, if required. 1148 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>> 1149 video_track_sources_; 1150 // |local_video_renderer_| attached to the first created local video track. 1151 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_; 1152 1153 SdpSemantics sdp_semantics_; 1154 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_; 1155 std::function<void(cricket::SessionDescription*)> received_sdp_munger_; 1156 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_; 1157 std::function<void()> remote_offer_handler_; 1158 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr; 1159 rtc::scoped_refptr<DataChannelInterface> data_channel_; 1160 std::unique_ptr<MockDataChannelObserver> data_observer_; 1161 1162 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_; 1163 1164 std::vector<PeerConnectionInterface::IceConnectionState> 1165 ice_connection_state_history_; 1166 std::vector<PeerConnectionInterface::IceConnectionState> 1167 standardized_ice_connection_state_history_; 1168 std::vector<PeerConnectionInterface::PeerConnectionState> 1169 peer_connection_state_history_; 1170 std::vector<PeerConnectionInterface::IceGatheringState> 1171 ice_gathering_state_history_; 1172 std::vector<cricket::CandidatePairChangeEvent> 1173 ice_candidate_pair_change_history_; 1174 std::vector<PeerConnectionInterface::SignalingState> 1175 peer_connection_signaling_state_history_; 1176 webrtc::FakeRtcEventLogFactory* event_log_factory_; 1177 1178 // Variables for tracking delay stats on an audio track 1179 int audio_packets_stat_ = 0; 1180 double audio_delay_stat_ = 0.0; 1181 uint64_t audio_samples_stat_ = 0; 1182 uint64_t audio_concealed_stat_ = 0; 1183 std::string rtp_stats_id_; 1184 std::string audio_track_stats_id_; 1185 1186 ScopedTaskSafety task_safety_; 1187 1188 friend class PeerConnectionIntegrationBaseTest; 1189 }; 1190 1191 class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput { 1192 public: 1193 virtual ~MockRtcEventLogOutput() = default; 1194 MOCK_METHOD(bool, IsActive, (), (const, override)); 1195 MOCK_METHOD(bool, Write, (const std::string&), (override)); 1196 }; 1197 1198 // This helper object is used for both specifying how many audio/video frames 1199 // are expected to be received for a caller/callee. It provides helper functions 1200 // to specify these expectations. The object initially starts in a state of no 1201 // expectations. 1202 class MediaExpectations { 1203 public: 1204 enum ExpectFrames { 1205 kExpectSomeFrames, 1206 kExpectNoFrames, 1207 kNoExpectation, 1208 }; 1209 ExpectBidirectionalAudioAndVideo()1210 void ExpectBidirectionalAudioAndVideo() { 1211 ExpectBidirectionalAudio(); 1212 ExpectBidirectionalVideo(); 1213 } 1214 ExpectBidirectionalAudio()1215 void ExpectBidirectionalAudio() { 1216 CallerExpectsSomeAudio(); 1217 CalleeExpectsSomeAudio(); 1218 } 1219 ExpectNoAudio()1220 void ExpectNoAudio() { 1221 CallerExpectsNoAudio(); 1222 CalleeExpectsNoAudio(); 1223 } 1224 ExpectBidirectionalVideo()1225 void ExpectBidirectionalVideo() { 1226 CallerExpectsSomeVideo(); 1227 CalleeExpectsSomeVideo(); 1228 } 1229 ExpectNoVideo()1230 void ExpectNoVideo() { 1231 CallerExpectsNoVideo(); 1232 CalleeExpectsNoVideo(); 1233 } 1234 CallerExpectsSomeAudioAndVideo()1235 void CallerExpectsSomeAudioAndVideo() { 1236 CallerExpectsSomeAudio(); 1237 CallerExpectsSomeVideo(); 1238 } 1239 CalleeExpectsSomeAudioAndVideo()1240 void CalleeExpectsSomeAudioAndVideo() { 1241 CalleeExpectsSomeAudio(); 1242 CalleeExpectsSomeVideo(); 1243 } 1244 1245 // Caller's audio functions. 1246 void CallerExpectsSomeAudio( 1247 int expected_audio_frames = kDefaultExpectedAudioFrameCount) { 1248 caller_audio_expectation_ = kExpectSomeFrames; 1249 caller_audio_frames_expected_ = expected_audio_frames; 1250 } 1251 CallerExpectsNoAudio()1252 void CallerExpectsNoAudio() { 1253 caller_audio_expectation_ = kExpectNoFrames; 1254 caller_audio_frames_expected_ = 0; 1255 } 1256 1257 // Caller's video functions. 1258 void CallerExpectsSomeVideo( 1259 int expected_video_frames = kDefaultExpectedVideoFrameCount) { 1260 caller_video_expectation_ = kExpectSomeFrames; 1261 caller_video_frames_expected_ = expected_video_frames; 1262 } 1263 CallerExpectsNoVideo()1264 void CallerExpectsNoVideo() { 1265 caller_video_expectation_ = kExpectNoFrames; 1266 caller_video_frames_expected_ = 0; 1267 } 1268 1269 // Callee's audio functions. 1270 void CalleeExpectsSomeAudio( 1271 int expected_audio_frames = kDefaultExpectedAudioFrameCount) { 1272 callee_audio_expectation_ = kExpectSomeFrames; 1273 callee_audio_frames_expected_ = expected_audio_frames; 1274 } 1275 CalleeExpectsNoAudio()1276 void CalleeExpectsNoAudio() { 1277 callee_audio_expectation_ = kExpectNoFrames; 1278 callee_audio_frames_expected_ = 0; 1279 } 1280 1281 // Callee's video functions. 1282 void CalleeExpectsSomeVideo( 1283 int expected_video_frames = kDefaultExpectedVideoFrameCount) { 1284 callee_video_expectation_ = kExpectSomeFrames; 1285 callee_video_frames_expected_ = expected_video_frames; 1286 } 1287 CalleeExpectsNoVideo()1288 void CalleeExpectsNoVideo() { 1289 callee_video_expectation_ = kExpectNoFrames; 1290 callee_video_frames_expected_ = 0; 1291 } 1292 1293 ExpectFrames caller_audio_expectation_ = kNoExpectation; 1294 ExpectFrames caller_video_expectation_ = kNoExpectation; 1295 ExpectFrames callee_audio_expectation_ = kNoExpectation; 1296 ExpectFrames callee_video_expectation_ = kNoExpectation; 1297 int caller_audio_frames_expected_ = 0; 1298 int caller_video_frames_expected_ = 0; 1299 int callee_audio_frames_expected_ = 0; 1300 int callee_video_frames_expected_ = 0; 1301 }; 1302 1303 class MockIceTransport : public webrtc::IceTransportInterface { 1304 public: MockIceTransport(const std::string & name,int component)1305 MockIceTransport(const std::string& name, int component) 1306 : internal_(std::make_unique<cricket::FakeIceTransport>( 1307 name, 1308 component, 1309 nullptr /* network_thread */)) {} 1310 ~MockIceTransport() = default; internal()1311 cricket::IceTransportInternal* internal() { return internal_.get(); } 1312 1313 private: 1314 std::unique_ptr<cricket::FakeIceTransport> internal_; 1315 }; 1316 1317 class MockIceTransportFactory : public IceTransportFactory { 1318 public: 1319 ~MockIceTransportFactory() override = default; CreateIceTransport(const std::string & transport_name,int component,IceTransportInit init)1320 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport( 1321 const std::string& transport_name, 1322 int component, 1323 IceTransportInit init) { 1324 RecordIceTransportCreated(); 1325 return new rtc::RefCountedObject<MockIceTransport>(transport_name, 1326 component); 1327 } 1328 MOCK_METHOD(void, RecordIceTransportCreated, ()); 1329 }; 1330 1331 // Tests two PeerConnections connecting to each other end-to-end, using a 1332 // virtual network, fake A/V capture and fake encoder/decoders. The 1333 // PeerConnections share the threads/socket servers, but use separate versions 1334 // of everything else (including "PeerConnectionFactory"s). 1335 class PeerConnectionIntegrationBaseTest : public ::testing::Test { 1336 public: PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)1337 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics) 1338 : sdp_semantics_(sdp_semantics), 1339 ss_(new rtc::VirtualSocketServer()), 1340 fss_(new rtc::FirewallSocketServer(ss_.get())), 1341 network_thread_(new rtc::Thread(fss_.get())), 1342 worker_thread_(rtc::Thread::Create()) { 1343 network_thread_->SetName("PCNetworkThread", this); 1344 worker_thread_->SetName("PCWorkerThread", this); 1345 RTC_CHECK(network_thread_->Start()); 1346 RTC_CHECK(worker_thread_->Start()); 1347 webrtc::metrics::Reset(); 1348 } 1349 ~PeerConnectionIntegrationBaseTest()1350 ~PeerConnectionIntegrationBaseTest() { 1351 // The PeerConnections should be deleted before the TurnCustomizers. 1352 // A TurnPort is created with a raw pointer to a TurnCustomizer. The 1353 // TurnPort has the same lifetime as the PeerConnection, so it's expected 1354 // that the TurnCustomizer outlives the life of the PeerConnection or else 1355 // when Send() is called it will hit a seg fault. 1356 if (caller_) { 1357 caller_->set_signaling_message_receiver(nullptr); 1358 caller_->pc()->Close(); 1359 delete SetCallerPcWrapperAndReturnCurrent(nullptr); 1360 } 1361 if (callee_) { 1362 callee_->set_signaling_message_receiver(nullptr); 1363 callee_->pc()->Close(); 1364 delete SetCalleePcWrapperAndReturnCurrent(nullptr); 1365 } 1366 1367 // If turn servers were created for the test they need to be destroyed on 1368 // the network thread. 1369 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { 1370 turn_servers_.clear(); 1371 turn_customizers_.clear(); 1372 }); 1373 } 1374 SignalingStateStable()1375 bool SignalingStateStable() { 1376 return caller_->SignalingStateStable() && callee_->SignalingStateStable(); 1377 } 1378 DtlsConnected()1379 bool DtlsConnected() { 1380 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS 1381 // are connected. This is an important distinction. Once we have separate 1382 // ICE and DTLS state, this check needs to use the DTLS state. 1383 return (callee()->ice_connection_state() == 1384 webrtc::PeerConnectionInterface::kIceConnectionConnected || 1385 callee()->ice_connection_state() == 1386 webrtc::PeerConnectionInterface::kIceConnectionCompleted) && 1387 (caller()->ice_connection_state() == 1388 webrtc::PeerConnectionInterface::kIceConnectionConnected || 1389 caller()->ice_connection_state() == 1390 webrtc::PeerConnectionInterface::kIceConnectionCompleted); 1391 } 1392 1393 // When |event_log_factory| is null, the default implementation of the event 1394 // log factory will be used. CreatePeerConnectionWrapper(const std::string & debug_name,const PeerConnectionFactory::Options * options,const RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies,std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,bool reset_encoder_factory,bool reset_decoder_factory)1395 std::unique_ptr<PeerConnectionIntegrationWrapper> CreatePeerConnectionWrapper( 1396 const std::string& debug_name, 1397 const PeerConnectionFactory::Options* options, 1398 const RTCConfiguration* config, 1399 webrtc::PeerConnectionDependencies dependencies, 1400 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory, 1401 bool reset_encoder_factory, 1402 bool reset_decoder_factory) { 1403 RTCConfiguration modified_config; 1404 if (config) { 1405 modified_config = *config; 1406 } 1407 modified_config.sdp_semantics = sdp_semantics_; 1408 if (!dependencies.cert_generator) { 1409 dependencies.cert_generator = 1410 std::make_unique<FakeRTCCertificateGenerator>(); 1411 } 1412 std::unique_ptr<PeerConnectionIntegrationWrapper> client( 1413 new PeerConnectionIntegrationWrapper(debug_name)); 1414 1415 if (!client->Init(options, &modified_config, std::move(dependencies), 1416 network_thread_.get(), worker_thread_.get(), 1417 std::move(event_log_factory), reset_encoder_factory, 1418 reset_decoder_factory)) { 1419 return nullptr; 1420 } 1421 return client; 1422 } 1423 1424 std::unique_ptr<PeerConnectionIntegrationWrapper> CreatePeerConnectionWrapperWithFakeRtcEventLog(const std::string & debug_name,const PeerConnectionFactory::Options * options,const RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)1425 CreatePeerConnectionWrapperWithFakeRtcEventLog( 1426 const std::string& debug_name, 1427 const PeerConnectionFactory::Options* options, 1428 const RTCConfiguration* config, 1429 webrtc::PeerConnectionDependencies dependencies) { 1430 return CreatePeerConnectionWrapper( 1431 debug_name, options, config, std::move(dependencies), 1432 std::make_unique<webrtc::FakeRtcEventLogFactory>(), 1433 /*reset_encoder_factory=*/false, 1434 /*reset_decoder_factory=*/false); 1435 } 1436 CreatePeerConnectionWrappers()1437 bool CreatePeerConnectionWrappers() { 1438 return CreatePeerConnectionWrappersWithConfig( 1439 PeerConnectionInterface::RTCConfiguration(), 1440 PeerConnectionInterface::RTCConfiguration()); 1441 } 1442 CreatePeerConnectionWrappersWithSdpSemantics(SdpSemantics caller_semantics,SdpSemantics callee_semantics)1443 bool CreatePeerConnectionWrappersWithSdpSemantics( 1444 SdpSemantics caller_semantics, 1445 SdpSemantics callee_semantics) { 1446 // Can't specify the sdp_semantics in the passed-in configuration since it 1447 // will be overwritten by CreatePeerConnectionWrapper with whatever is 1448 // stored in sdp_semantics_. So get around this by modifying the instance 1449 // variable before calling CreatePeerConnectionWrapper for the caller and 1450 // callee PeerConnections. 1451 SdpSemantics original_semantics = sdp_semantics_; 1452 sdp_semantics_ = caller_semantics; 1453 caller_ = CreatePeerConnectionWrapper( 1454 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr), 1455 nullptr, 1456 /*reset_encoder_factory=*/false, 1457 /*reset_decoder_factory=*/false); 1458 sdp_semantics_ = callee_semantics; 1459 callee_ = CreatePeerConnectionWrapper( 1460 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr), 1461 nullptr, 1462 /*reset_encoder_factory=*/false, 1463 /*reset_decoder_factory=*/false); 1464 sdp_semantics_ = original_semantics; 1465 return caller_ && callee_; 1466 } 1467 CreatePeerConnectionWrappersWithConfig(const PeerConnectionInterface::RTCConfiguration & caller_config,const PeerConnectionInterface::RTCConfiguration & callee_config)1468 bool CreatePeerConnectionWrappersWithConfig( 1469 const PeerConnectionInterface::RTCConfiguration& caller_config, 1470 const PeerConnectionInterface::RTCConfiguration& callee_config) { 1471 caller_ = CreatePeerConnectionWrapper( 1472 "Caller", nullptr, &caller_config, 1473 webrtc::PeerConnectionDependencies(nullptr), nullptr, 1474 /*reset_encoder_factory=*/false, 1475 /*reset_decoder_factory=*/false); 1476 callee_ = CreatePeerConnectionWrapper( 1477 "Callee", nullptr, &callee_config, 1478 webrtc::PeerConnectionDependencies(nullptr), nullptr, 1479 /*reset_encoder_factory=*/false, 1480 /*reset_decoder_factory=*/false); 1481 return caller_ && callee_; 1482 } 1483 CreatePeerConnectionWrappersWithConfigAndDeps(const PeerConnectionInterface::RTCConfiguration & caller_config,webrtc::PeerConnectionDependencies caller_dependencies,const PeerConnectionInterface::RTCConfiguration & callee_config,webrtc::PeerConnectionDependencies callee_dependencies)1484 bool CreatePeerConnectionWrappersWithConfigAndDeps( 1485 const PeerConnectionInterface::RTCConfiguration& caller_config, 1486 webrtc::PeerConnectionDependencies caller_dependencies, 1487 const PeerConnectionInterface::RTCConfiguration& callee_config, 1488 webrtc::PeerConnectionDependencies callee_dependencies) { 1489 caller_ = 1490 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config, 1491 std::move(caller_dependencies), nullptr, 1492 /*reset_encoder_factory=*/false, 1493 /*reset_decoder_factory=*/false); 1494 callee_ = 1495 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config, 1496 std::move(callee_dependencies), nullptr, 1497 /*reset_encoder_factory=*/false, 1498 /*reset_decoder_factory=*/false); 1499 return caller_ && callee_; 1500 } 1501 CreatePeerConnectionWrappersWithOptions(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options)1502 bool CreatePeerConnectionWrappersWithOptions( 1503 const PeerConnectionFactory::Options& caller_options, 1504 const PeerConnectionFactory::Options& callee_options) { 1505 caller_ = CreatePeerConnectionWrapper( 1506 "Caller", &caller_options, nullptr, 1507 webrtc::PeerConnectionDependencies(nullptr), nullptr, 1508 /*reset_encoder_factory=*/false, 1509 /*reset_decoder_factory=*/false); 1510 callee_ = CreatePeerConnectionWrapper( 1511 "Callee", &callee_options, nullptr, 1512 webrtc::PeerConnectionDependencies(nullptr), nullptr, 1513 /*reset_encoder_factory=*/false, 1514 /*reset_decoder_factory=*/false); 1515 return caller_ && callee_; 1516 } 1517 CreatePeerConnectionWrappersWithFakeRtcEventLog()1518 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() { 1519 PeerConnectionInterface::RTCConfiguration default_config; 1520 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog( 1521 "Caller", nullptr, &default_config, 1522 webrtc::PeerConnectionDependencies(nullptr)); 1523 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog( 1524 "Callee", nullptr, &default_config, 1525 webrtc::PeerConnectionDependencies(nullptr)); 1526 return caller_ && callee_; 1527 } 1528 1529 std::unique_ptr<PeerConnectionIntegrationWrapper> CreatePeerConnectionWrapperWithAlternateKey()1530 CreatePeerConnectionWrapperWithAlternateKey() { 1531 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator( 1532 new FakeRTCCertificateGenerator()); 1533 cert_generator->use_alternate_key(); 1534 1535 webrtc::PeerConnectionDependencies dependencies(nullptr); 1536 dependencies.cert_generator = std::move(cert_generator); 1537 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, 1538 std::move(dependencies), nullptr, 1539 /*reset_encoder_factory=*/false, 1540 /*reset_decoder_factory=*/false); 1541 } 1542 CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee)1543 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) { 1544 caller_ = CreatePeerConnectionWrapper( 1545 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr), 1546 nullptr, 1547 /*reset_encoder_factory=*/!caller_to_callee, 1548 /*reset_decoder_factory=*/caller_to_callee); 1549 callee_ = CreatePeerConnectionWrapper( 1550 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr), 1551 nullptr, 1552 /*reset_encoder_factory=*/caller_to_callee, 1553 /*reset_decoder_factory=*/!caller_to_callee); 1554 return caller_ && callee_; 1555 } 1556 1557 cricket::TestTurnServer* CreateTurnServer( 1558 rtc::SocketAddress internal_address, 1559 rtc::SocketAddress external_address, 1560 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP, 1561 const std::string& common_name = "test turn server") { 1562 rtc::Thread* thread = network_thread(); 1563 std::unique_ptr<cricket::TestTurnServer> turn_server = 1564 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>( 1565 RTC_FROM_HERE, 1566 [thread, internal_address, external_address, type, common_name] { 1567 return std::make_unique<cricket::TestTurnServer>( 1568 thread, internal_address, external_address, type, 1569 /*ignore_bad_certs=*/true, common_name); 1570 }); 1571 turn_servers_.push_back(std::move(turn_server)); 1572 // Interactions with the turn server should be done on the network thread. 1573 return turn_servers_.back().get(); 1574 } 1575 CreateTurnCustomizer()1576 cricket::TestTurnCustomizer* CreateTurnCustomizer() { 1577 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer = 1578 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>( 1579 RTC_FROM_HERE, 1580 [] { return std::make_unique<cricket::TestTurnCustomizer>(); }); 1581 turn_customizers_.push_back(std::move(turn_customizer)); 1582 // Interactions with the turn customizer should be done on the network 1583 // thread. 1584 return turn_customizers_.back().get(); 1585 } 1586 1587 // Checks that the function counters for a TestTurnCustomizer are greater than 1588 // 0. ExpectTurnCustomizerCountersIncremented(cricket::TestTurnCustomizer * turn_customizer)1589 void ExpectTurnCustomizerCountersIncremented( 1590 cricket::TestTurnCustomizer* turn_customizer) { 1591 unsigned int allow_channel_data_counter = 1592 network_thread()->Invoke<unsigned int>( 1593 RTC_FROM_HERE, [turn_customizer] { 1594 return turn_customizer->allow_channel_data_cnt_; 1595 }); 1596 EXPECT_GT(allow_channel_data_counter, 0u); 1597 unsigned int modify_counter = network_thread()->Invoke<unsigned int>( 1598 RTC_FROM_HERE, 1599 [turn_customizer] { return turn_customizer->modify_cnt_; }); 1600 EXPECT_GT(modify_counter, 0u); 1601 } 1602 1603 // Once called, SDP blobs and ICE candidates will be automatically signaled 1604 // between PeerConnections. ConnectFakeSignaling()1605 void ConnectFakeSignaling() { 1606 caller_->set_signaling_message_receiver(callee_.get()); 1607 callee_->set_signaling_message_receiver(caller_.get()); 1608 } 1609 1610 // Once called, SDP blobs will be automatically signaled between 1611 // PeerConnections. Note that ICE candidates will not be signaled unless they 1612 // are in the exchanged SDP blobs. ConnectFakeSignalingForSdpOnly()1613 void ConnectFakeSignalingForSdpOnly() { 1614 ConnectFakeSignaling(); 1615 SetSignalIceCandidates(false); 1616 } 1617 SetSignalingDelayMs(int delay_ms)1618 void SetSignalingDelayMs(int delay_ms) { 1619 caller_->set_signaling_delay_ms(delay_ms); 1620 callee_->set_signaling_delay_ms(delay_ms); 1621 } 1622 SetSignalIceCandidates(bool signal)1623 void SetSignalIceCandidates(bool signal) { 1624 caller_->set_signal_ice_candidates(signal); 1625 callee_->set_signal_ice_candidates(signal); 1626 } 1627 1628 // Messages may get lost on the unreliable DataChannel, so we send multiple 1629 // times to avoid test flakiness. SendRtpDataWithRetries(webrtc::DataChannelInterface * dc,const std::string & data,int retries)1630 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc, 1631 const std::string& data, 1632 int retries) { 1633 for (int i = 0; i < retries; ++i) { 1634 dc->Send(DataBuffer(data)); 1635 } 1636 } 1637 network_thread()1638 rtc::Thread* network_thread() { return network_thread_.get(); } 1639 virtual_socket_server()1640 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); } 1641 caller()1642 PeerConnectionIntegrationWrapper* caller() { return caller_.get(); } 1643 1644 // Set the |caller_| to the |wrapper| passed in and return the 1645 // original |caller_|. SetCallerPcWrapperAndReturnCurrent(PeerConnectionIntegrationWrapper * wrapper)1646 PeerConnectionIntegrationWrapper* SetCallerPcWrapperAndReturnCurrent( 1647 PeerConnectionIntegrationWrapper* wrapper) { 1648 PeerConnectionIntegrationWrapper* old = caller_.release(); 1649 caller_.reset(wrapper); 1650 return old; 1651 } 1652 callee()1653 PeerConnectionIntegrationWrapper* callee() { return callee_.get(); } 1654 1655 // Set the |callee_| to the |wrapper| passed in and return the 1656 // original |callee_|. SetCalleePcWrapperAndReturnCurrent(PeerConnectionIntegrationWrapper * wrapper)1657 PeerConnectionIntegrationWrapper* SetCalleePcWrapperAndReturnCurrent( 1658 PeerConnectionIntegrationWrapper* wrapper) { 1659 PeerConnectionIntegrationWrapper* old = callee_.release(); 1660 callee_.reset(wrapper); 1661 return old; 1662 } 1663 SetPortAllocatorFlags(uint32_t caller_flags,uint32_t callee_flags)1664 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) { 1665 network_thread()->Invoke<void>(RTC_FROM_HERE, [this, caller_flags] { 1666 caller()->port_allocator()->set_flags(caller_flags); 1667 }); 1668 network_thread()->Invoke<void>(RTC_FROM_HERE, [this, callee_flags] { 1669 callee()->port_allocator()->set_flags(callee_flags); 1670 }); 1671 } 1672 firewall()1673 rtc::FirewallSocketServer* firewall() const { return fss_.get(); } 1674 1675 // Expects the provided number of new frames to be received within 1676 // kMaxWaitForFramesMs. The new expected frames are specified in 1677 // |media_expectations|. Returns false if any of the expectations were 1678 // not met. ExpectNewFrames(const MediaExpectations & media_expectations)1679 bool ExpectNewFrames(const MediaExpectations& media_expectations) { 1680 // Make sure there are no bogus tracks confusing the issue. 1681 caller()->RemoveUnusedVideoRenderers(); 1682 callee()->RemoveUnusedVideoRenderers(); 1683 // First initialize the expected frame counts based upon the current 1684 // frame count. 1685 int total_caller_audio_frames_expected = caller()->audio_frames_received(); 1686 if (media_expectations.caller_audio_expectation_ == 1687 MediaExpectations::kExpectSomeFrames) { 1688 total_caller_audio_frames_expected += 1689 media_expectations.caller_audio_frames_expected_; 1690 } 1691 int total_caller_video_frames_expected = 1692 caller()->min_video_frames_received_per_track(); 1693 if (media_expectations.caller_video_expectation_ == 1694 MediaExpectations::kExpectSomeFrames) { 1695 total_caller_video_frames_expected += 1696 media_expectations.caller_video_frames_expected_; 1697 } 1698 int total_callee_audio_frames_expected = callee()->audio_frames_received(); 1699 if (media_expectations.callee_audio_expectation_ == 1700 MediaExpectations::kExpectSomeFrames) { 1701 total_callee_audio_frames_expected += 1702 media_expectations.callee_audio_frames_expected_; 1703 } 1704 int total_callee_video_frames_expected = 1705 callee()->min_video_frames_received_per_track(); 1706 if (media_expectations.callee_video_expectation_ == 1707 MediaExpectations::kExpectSomeFrames) { 1708 total_callee_video_frames_expected += 1709 media_expectations.callee_video_frames_expected_; 1710 } 1711 1712 // Wait for the expected frames. 1713 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >= 1714 total_caller_audio_frames_expected && 1715 caller()->min_video_frames_received_per_track() >= 1716 total_caller_video_frames_expected && 1717 callee()->audio_frames_received() >= 1718 total_callee_audio_frames_expected && 1719 callee()->min_video_frames_received_per_track() >= 1720 total_callee_video_frames_expected, 1721 kMaxWaitForFramesMs); 1722 bool expectations_correct = 1723 caller()->audio_frames_received() >= 1724 total_caller_audio_frames_expected && 1725 caller()->min_video_frames_received_per_track() >= 1726 total_caller_video_frames_expected && 1727 callee()->audio_frames_received() >= 1728 total_callee_audio_frames_expected && 1729 callee()->min_video_frames_received_per_track() >= 1730 total_callee_video_frames_expected; 1731 1732 // After the combined wait, print out a more detailed message upon 1733 // failure. 1734 EXPECT_GE(caller()->audio_frames_received(), 1735 total_caller_audio_frames_expected); 1736 EXPECT_GE(caller()->min_video_frames_received_per_track(), 1737 total_caller_video_frames_expected); 1738 EXPECT_GE(callee()->audio_frames_received(), 1739 total_callee_audio_frames_expected); 1740 EXPECT_GE(callee()->min_video_frames_received_per_track(), 1741 total_callee_video_frames_expected); 1742 1743 // We want to make sure nothing unexpected was received. 1744 if (media_expectations.caller_audio_expectation_ == 1745 MediaExpectations::kExpectNoFrames) { 1746 EXPECT_EQ(caller()->audio_frames_received(), 1747 total_caller_audio_frames_expected); 1748 if (caller()->audio_frames_received() != 1749 total_caller_audio_frames_expected) { 1750 expectations_correct = false; 1751 } 1752 } 1753 if (media_expectations.caller_video_expectation_ == 1754 MediaExpectations::kExpectNoFrames) { 1755 EXPECT_EQ(caller()->min_video_frames_received_per_track(), 1756 total_caller_video_frames_expected); 1757 if (caller()->min_video_frames_received_per_track() != 1758 total_caller_video_frames_expected) { 1759 expectations_correct = false; 1760 } 1761 } 1762 if (media_expectations.callee_audio_expectation_ == 1763 MediaExpectations::kExpectNoFrames) { 1764 EXPECT_EQ(callee()->audio_frames_received(), 1765 total_callee_audio_frames_expected); 1766 if (callee()->audio_frames_received() != 1767 total_callee_audio_frames_expected) { 1768 expectations_correct = false; 1769 } 1770 } 1771 if (media_expectations.callee_video_expectation_ == 1772 MediaExpectations::kExpectNoFrames) { 1773 EXPECT_EQ(callee()->min_video_frames_received_per_track(), 1774 total_callee_video_frames_expected); 1775 if (callee()->min_video_frames_received_per_track() != 1776 total_callee_video_frames_expected) { 1777 expectations_correct = false; 1778 } 1779 } 1780 return expectations_correct; 1781 } 1782 ClosePeerConnections()1783 void ClosePeerConnections() { 1784 if (caller()) 1785 caller()->pc()->Close(); 1786 if (callee()) 1787 callee()->pc()->Close(); 1788 } 1789 TestNegotiatedCipherSuite(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options,int expected_cipher_suite)1790 void TestNegotiatedCipherSuite( 1791 const PeerConnectionFactory::Options& caller_options, 1792 const PeerConnectionFactory::Options& callee_options, 1793 int expected_cipher_suite) { 1794 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options, 1795 callee_options)); 1796 ConnectFakeSignaling(); 1797 caller()->AddAudioVideoTracks(); 1798 callee()->AddAudioVideoTracks(); 1799 caller()->CreateAndSetAndSignalOffer(); 1800 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout); 1801 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite), 1802 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout); 1803 // TODO(bugs.webrtc.org/9456): Fix it. 1804 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents( 1805 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", 1806 expected_cipher_suite)); 1807 } 1808 TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,bool remote_gcm_enabled,bool aes_ctr_enabled,int expected_cipher_suite)1809 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled, 1810 bool remote_gcm_enabled, 1811 bool aes_ctr_enabled, 1812 int expected_cipher_suite) { 1813 PeerConnectionFactory::Options caller_options; 1814 caller_options.crypto_options.srtp.enable_gcm_crypto_suites = 1815 local_gcm_enabled; 1816 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = 1817 aes_ctr_enabled; 1818 PeerConnectionFactory::Options callee_options; 1819 callee_options.crypto_options.srtp.enable_gcm_crypto_suites = 1820 remote_gcm_enabled; 1821 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = 1822 aes_ctr_enabled; 1823 TestNegotiatedCipherSuite(caller_options, callee_options, 1824 expected_cipher_suite); 1825 } 1826 1827 protected: 1828 SdpSemantics sdp_semantics_; 1829 1830 private: 1831 // |ss_| is used by |network_thread_| so it must be destroyed later. 1832 std::unique_ptr<rtc::VirtualSocketServer> ss_; 1833 std::unique_ptr<rtc::FirewallSocketServer> fss_; 1834 // |network_thread_| and |worker_thread_| are used by both 1835 // |caller_| and |callee_| so they must be destroyed 1836 // later. 1837 std::unique_ptr<rtc::Thread> network_thread_; 1838 std::unique_ptr<rtc::Thread> worker_thread_; 1839 // The turn servers and turn customizers should be accessed & deleted on the 1840 // network thread to avoid a race with the socket read/write that occurs 1841 // on the network thread. 1842 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_; 1843 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_; 1844 std::unique_ptr<PeerConnectionIntegrationWrapper> caller_; 1845 std::unique_ptr<PeerConnectionIntegrationWrapper> callee_; 1846 }; 1847 1848 } // namespace webrtc 1849 1850 #endif // PC_TEST_INTEGRATION_TEST_HELPERS_H_ 1851