deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 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 | // Disable for TSan v2, see |
| 12 | // https://code.google.com/p/webrtc/issues/detail?id=1205 for details. |
| 13 | #if !defined(THREAD_SANITIZER) |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <functional> |
| 19 | #include <list> |
| 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "webrtc/api/fakemetricsobserver.h" |
| 26 | #include "webrtc/api/mediastreaminterface.h" |
| 27 | #include "webrtc/api/peerconnectioninterface.h" |
| 28 | #include "webrtc/api/test/fakeconstraints.h" |
| 29 | #include "webrtc/base/asyncinvoker.h" |
| 30 | #include "webrtc/base/fakenetwork.h" |
| 31 | #include "webrtc/base/gunit.h" |
| 32 | #include "webrtc/base/helpers.h" |
| 33 | #include "webrtc/base/physicalsocketserver.h" |
| 34 | #include "webrtc/base/ssladapter.h" |
| 35 | #include "webrtc/base/sslstreamadapter.h" |
| 36 | #include "webrtc/base/thread.h" |
| 37 | #include "webrtc/base/virtualsocketserver.h" |
| 38 | #include "webrtc/media/engine/fakewebrtcvideoengine.h" |
| 39 | #include "webrtc/p2p/base/p2pconstants.h" |
| 40 | #include "webrtc/p2p/base/portinterface.h" |
| 41 | #include "webrtc/p2p/base/sessiondescription.h" |
| 42 | #include "webrtc/p2p/base/testturnserver.h" |
| 43 | #include "webrtc/p2p/client/basicportallocator.h" |
| 44 | #include "webrtc/pc/dtmfsender.h" |
| 45 | #include "webrtc/pc/localaudiosource.h" |
| 46 | #include "webrtc/pc/mediasession.h" |
| 47 | #include "webrtc/pc/peerconnection.h" |
| 48 | #include "webrtc/pc/peerconnectionfactory.h" |
| 49 | #include "webrtc/pc/test/fakeaudiocapturemodule.h" |
| 50 | #include "webrtc/pc/test/fakeperiodicvideocapturer.h" |
| 51 | #include "webrtc/pc/test/fakertccertificategenerator.h" |
| 52 | #include "webrtc/pc/test/fakevideotrackrenderer.h" |
| 53 | #include "webrtc/pc/test/mockpeerconnectionobservers.h" |
| 54 | |
| 55 | using cricket::ContentInfo; |
| 56 | using cricket::FakeWebRtcVideoDecoder; |
| 57 | using cricket::FakeWebRtcVideoDecoderFactory; |
| 58 | using cricket::FakeWebRtcVideoEncoder; |
| 59 | using cricket::FakeWebRtcVideoEncoderFactory; |
| 60 | using cricket::MediaContentDescription; |
| 61 | using webrtc::DataBuffer; |
| 62 | using webrtc::DataChannelInterface; |
| 63 | using webrtc::DtmfSender; |
| 64 | using webrtc::DtmfSenderInterface; |
| 65 | using webrtc::DtmfSenderObserverInterface; |
| 66 | using webrtc::FakeConstraints; |
| 67 | using webrtc::MediaConstraintsInterface; |
| 68 | using webrtc::MediaStreamInterface; |
| 69 | using webrtc::MediaStreamTrackInterface; |
| 70 | using webrtc::MockCreateSessionDescriptionObserver; |
| 71 | using webrtc::MockDataChannelObserver; |
| 72 | using webrtc::MockSetSessionDescriptionObserver; |
| 73 | using webrtc::MockStatsObserver; |
| 74 | using webrtc::ObserverInterface; |
| 75 | using webrtc::PeerConnectionInterface; |
| 76 | using webrtc::PeerConnectionFactory; |
| 77 | using webrtc::SessionDescriptionInterface; |
| 78 | using webrtc::StreamCollectionInterface; |
| 79 | |
| 80 | namespace { |
| 81 | |
| 82 | static const int kDefaultTimeout = 10000; |
| 83 | static const int kMaxWaitForStatsMs = 3000; |
| 84 | static const int kMaxWaitForActivationMs = 5000; |
| 85 | static const int kMaxWaitForFramesMs = 10000; |
| 86 | // Default number of audio/video frames to wait for before considering a test |
| 87 | // successful. |
| 88 | static const int kDefaultExpectedAudioFrameCount = 3; |
| 89 | static const int kDefaultExpectedVideoFrameCount = 3; |
| 90 | |
| 91 | static const char kDefaultStreamLabel[] = "stream_label"; |
| 92 | static const char kDefaultVideoTrackId[] = "video_track"; |
| 93 | static const char kDefaultAudioTrackId[] = "audio_track"; |
| 94 | static const char kDataChannelLabel[] = "data_channel"; |
| 95 | |
| 96 | // SRTP cipher name negotiated by the tests. This must be updated if the |
| 97 | // default changes. |
| 98 | static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32; |
| 99 | static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM; |
| 100 | |
| 101 | // Helper function for constructing offer/answer options to initiate an ICE |
| 102 | // restart. |
| 103 | PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() { |
| 104 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 105 | options.ice_restart = true; |
| 106 | return options; |
| 107 | } |
| 108 | |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 109 | // Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic" |
| 110 | // attribute from received SDP, simulating a legacy endpoint. |
| 111 | void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) { |
| 112 | for (ContentInfo& content : desc->contents()) { |
| 113 | MediaContentDescription* media_desc = |
| 114 | static_cast<MediaContentDescription*>(content.description); |
| 115 | media_desc->mutable_streams().clear(); |
| 116 | } |
| 117 | desc->set_msid_supported(false); |
| 118 | } |
| 119 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 120 | class SignalingMessageReceiver { |
| 121 | public: |
| 122 | virtual void ReceiveSdpMessage(const std::string& type, |
| 123 | const std::string& msg) = 0; |
| 124 | virtual void ReceiveIceMessage(const std::string& sdp_mid, |
| 125 | int sdp_mline_index, |
| 126 | const std::string& msg) = 0; |
| 127 | |
| 128 | protected: |
| 129 | SignalingMessageReceiver() {} |
| 130 | virtual ~SignalingMessageReceiver() {} |
| 131 | }; |
| 132 | |
| 133 | class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface { |
| 134 | public: |
| 135 | explicit MockRtpReceiverObserver(cricket::MediaType media_type) |
| 136 | : expected_media_type_(media_type) {} |
| 137 | |
| 138 | void OnFirstPacketReceived(cricket::MediaType media_type) override { |
| 139 | ASSERT_EQ(expected_media_type_, media_type); |
| 140 | first_packet_received_ = true; |
| 141 | } |
| 142 | |
| 143 | bool first_packet_received() const { return first_packet_received_; } |
| 144 | |
| 145 | virtual ~MockRtpReceiverObserver() {} |
| 146 | |
| 147 | private: |
| 148 | bool first_packet_received_ = false; |
| 149 | cricket::MediaType expected_media_type_; |
| 150 | }; |
| 151 | |
| 152 | // Helper class that wraps a peer connection, observes it, and can accept |
| 153 | // signaling messages from another wrapper. |
| 154 | // |
| 155 | // Uses a fake network, fake A/V capture, and optionally fake |
| 156 | // encoders/decoders, though they aren't used by default since they don't |
| 157 | // advertise support of any codecs. |
| 158 | class PeerConnectionWrapper : public webrtc::PeerConnectionObserver, |
| 159 | public SignalingMessageReceiver, |
| 160 | public ObserverInterface { |
| 161 | public: |
| 162 | // Different factory methods for convenience. |
| 163 | // TODO(deadbeef): Could use the pattern of: |
| 164 | // |
| 165 | // PeerConnectionWrapper = |
| 166 | // WrapperBuilder.WithConfig(...).WithOptions(...).build(); |
| 167 | // |
| 168 | // To reduce some code duplication. |
| 169 | static PeerConnectionWrapper* CreateWithDtlsIdentityStore( |
| 170 | const std::string& debug_name, |
| 171 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
| 172 | rtc::Thread* network_thread, |
| 173 | rtc::Thread* worker_thread) { |
| 174 | PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name)); |
| 175 | if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator), |
| 176 | network_thread, worker_thread)) { |
| 177 | delete client; |
| 178 | return nullptr; |
| 179 | } |
| 180 | return client; |
| 181 | } |
| 182 | |
| 183 | static PeerConnectionWrapper* CreateWithConfig( |
| 184 | const std::string& debug_name, |
| 185 | const PeerConnectionInterface::RTCConfiguration& config, |
| 186 | rtc::Thread* network_thread, |
| 187 | rtc::Thread* worker_thread) { |
| 188 | std::unique_ptr<FakeRTCCertificateGenerator> cert_generator( |
| 189 | new FakeRTCCertificateGenerator()); |
| 190 | PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name)); |
| 191 | if (!client->Init(nullptr, nullptr, &config, std::move(cert_generator), |
| 192 | network_thread, worker_thread)) { |
| 193 | delete client; |
| 194 | return nullptr; |
| 195 | } |
| 196 | return client; |
| 197 | } |
| 198 | |
| 199 | static PeerConnectionWrapper* CreateWithOptions( |
| 200 | const std::string& debug_name, |
| 201 | const PeerConnectionFactory::Options& options, |
| 202 | rtc::Thread* network_thread, |
| 203 | rtc::Thread* worker_thread) { |
| 204 | std::unique_ptr<FakeRTCCertificateGenerator> cert_generator( |
| 205 | new FakeRTCCertificateGenerator()); |
| 206 | PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name)); |
| 207 | if (!client->Init(nullptr, &options, nullptr, std::move(cert_generator), |
| 208 | network_thread, worker_thread)) { |
| 209 | delete client; |
| 210 | return nullptr; |
| 211 | } |
| 212 | return client; |
| 213 | } |
| 214 | |
| 215 | static PeerConnectionWrapper* CreateWithConstraints( |
| 216 | const std::string& debug_name, |
| 217 | const MediaConstraintsInterface* constraints, |
| 218 | rtc::Thread* network_thread, |
| 219 | rtc::Thread* worker_thread) { |
| 220 | std::unique_ptr<FakeRTCCertificateGenerator> cert_generator( |
| 221 | new FakeRTCCertificateGenerator()); |
| 222 | PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name)); |
| 223 | if (!client->Init(constraints, nullptr, nullptr, std::move(cert_generator), |
| 224 | network_thread, worker_thread)) { |
| 225 | delete client; |
| 226 | return nullptr; |
| 227 | } |
| 228 | return client; |
| 229 | } |
| 230 | |
deadbeef | 2f425aa | 2017-04-14 10:41:32 -0700 | [diff] [blame] | 231 | webrtc::PeerConnectionFactoryInterface* pc_factory() const { |
| 232 | return peer_connection_factory_.get(); |
| 233 | } |
| 234 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 235 | webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); } |
| 236 | |
| 237 | // If a signaling message receiver is set (via ConnectFakeSignaling), this |
| 238 | // will set the whole offer/answer exchange in motion. Just need to wait for |
| 239 | // the signaling state to reach "stable". |
| 240 | void CreateAndSetAndSignalOffer() { |
| 241 | auto offer = CreateOffer(); |
| 242 | ASSERT_NE(nullptr, offer); |
| 243 | EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer))); |
| 244 | } |
| 245 | |
| 246 | // Sets the options to be used when CreateAndSetAndSignalOffer is called, or |
| 247 | // when a remote offer is received (via fake signaling) and an answer is |
| 248 | // generated. By default, uses default options. |
| 249 | void SetOfferAnswerOptions( |
| 250 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 251 | offer_answer_options_ = options; |
| 252 | } |
| 253 | |
| 254 | // Set a callback to be invoked when SDP is received via the fake signaling |
| 255 | // channel, which provides an opportunity to munge (modify) the SDP. This is |
| 256 | // used to test SDP being applied that a PeerConnection would normally not |
| 257 | // generate, but a non-JSEP endpoint might. |
| 258 | void SetReceivedSdpMunger( |
| 259 | std::function<void(cricket::SessionDescription*)> munger) { |
| 260 | received_sdp_munger_ = munger; |
| 261 | } |
| 262 | |
deadbeef | c964d0b | 2017-04-03 10:03:35 -0700 | [diff] [blame] | 263 | // Similar to the above, but this is run on SDP immediately after it's |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 264 | // generated. |
| 265 | void SetGeneratedSdpMunger( |
| 266 | std::function<void(cricket::SessionDescription*)> munger) { |
| 267 | generated_sdp_munger_ = munger; |
| 268 | } |
| 269 | |
| 270 | // Number of times the gathering state has transitioned to "gathering". |
| 271 | // Useful for telling if an ICE restart occurred as expected. |
| 272 | int transitions_to_gathering_state() const { |
| 273 | return transitions_to_gathering_state_; |
| 274 | } |
| 275 | |
| 276 | // TODO(deadbeef): Switch the majority of these tests to use AddTrack instead |
| 277 | // of AddStream since AddStream is deprecated. |
| 278 | void AddAudioVideoMediaStream() { |
| 279 | AddMediaStreamFromTracks(CreateLocalAudioTrack(), CreateLocalVideoTrack()); |
| 280 | } |
| 281 | |
| 282 | void AddAudioOnlyMediaStream() { |
| 283 | AddMediaStreamFromTracks(CreateLocalAudioTrack(), nullptr); |
| 284 | } |
| 285 | |
| 286 | void AddVideoOnlyMediaStream() { |
| 287 | AddMediaStreamFromTracks(nullptr, CreateLocalVideoTrack()); |
| 288 | } |
| 289 | |
| 290 | rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() { |
| 291 | FakeConstraints constraints; |
| 292 | // Disable highpass filter so that we can get all the test audio frames. |
| 293 | constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false); |
| 294 | rtc::scoped_refptr<webrtc::AudioSourceInterface> source = |
| 295 | peer_connection_factory_->CreateAudioSource(&constraints); |
| 296 | // TODO(perkj): Test audio source when it is implemented. Currently audio |
| 297 | // always use the default input. |
| 298 | return peer_connection_factory_->CreateAudioTrack(kDefaultAudioTrackId, |
| 299 | source); |
| 300 | } |
| 301 | |
| 302 | rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() { |
| 303 | return CreateLocalVideoTrackInternal( |
| 304 | kDefaultVideoTrackId, FakeConstraints(), webrtc::kVideoRotation_0); |
| 305 | } |
| 306 | |
| 307 | rtc::scoped_refptr<webrtc::VideoTrackInterface> |
| 308 | CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) { |
| 309 | return CreateLocalVideoTrackInternal(kDefaultVideoTrackId, constraints, |
| 310 | webrtc::kVideoRotation_0); |
| 311 | } |
| 312 | |
| 313 | rtc::scoped_refptr<webrtc::VideoTrackInterface> |
| 314 | CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) { |
| 315 | return CreateLocalVideoTrackInternal(kDefaultVideoTrackId, |
| 316 | FakeConstraints(), rotation); |
| 317 | } |
| 318 | |
| 319 | rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackWithId( |
| 320 | const std::string& id) { |
| 321 | return CreateLocalVideoTrackInternal(id, FakeConstraints(), |
| 322 | webrtc::kVideoRotation_0); |
| 323 | } |
| 324 | |
| 325 | void AddMediaStreamFromTracks( |
| 326 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio, |
| 327 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video) { |
| 328 | AddMediaStreamFromTracksWithLabel(audio, video, kDefaultStreamLabel); |
| 329 | } |
| 330 | |
| 331 | void AddMediaStreamFromTracksWithLabel( |
| 332 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio, |
| 333 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video, |
| 334 | const std::string& stream_label) { |
| 335 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 336 | peer_connection_factory_->CreateLocalMediaStream(stream_label); |
| 337 | if (audio) { |
| 338 | stream->AddTrack(audio); |
| 339 | } |
| 340 | if (video) { |
| 341 | stream->AddTrack(video); |
| 342 | } |
| 343 | EXPECT_TRUE(pc()->AddStream(stream)); |
| 344 | } |
| 345 | |
| 346 | bool SignalingStateStable() { |
| 347 | return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable; |
| 348 | } |
| 349 | |
| 350 | void CreateDataChannel() { CreateDataChannel(nullptr); } |
| 351 | |
| 352 | void CreateDataChannel(const webrtc::DataChannelInit* init) { |
| 353 | data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init); |
| 354 | ASSERT_TRUE(data_channel_.get() != nullptr); |
| 355 | data_observer_.reset(new MockDataChannelObserver(data_channel_)); |
| 356 | } |
| 357 | |
| 358 | DataChannelInterface* data_channel() { return data_channel_; } |
| 359 | const MockDataChannelObserver* data_observer() const { |
| 360 | return data_observer_.get(); |
| 361 | } |
| 362 | |
| 363 | int audio_frames_received() const { |
| 364 | return fake_audio_capture_module_->frames_received(); |
| 365 | } |
| 366 | |
| 367 | // Takes minimum of video frames received for each track. |
| 368 | // |
| 369 | // Can be used like: |
| 370 | // EXPECT_GE(expected_frames, min_video_frames_received_per_track()); |
| 371 | // |
| 372 | // To ensure that all video tracks received at least a certain number of |
| 373 | // frames. |
| 374 | int min_video_frames_received_per_track() const { |
| 375 | int min_frames = INT_MAX; |
| 376 | if (video_decoder_factory_enabled_) { |
| 377 | const std::vector<FakeWebRtcVideoDecoder*>& decoders = |
| 378 | fake_video_decoder_factory_->decoders(); |
| 379 | if (decoders.empty()) { |
| 380 | return 0; |
| 381 | } |
| 382 | for (FakeWebRtcVideoDecoder* decoder : decoders) { |
| 383 | min_frames = std::min(min_frames, decoder->GetNumFramesReceived()); |
| 384 | } |
| 385 | return min_frames; |
| 386 | } else { |
| 387 | if (fake_video_renderers_.empty()) { |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | for (const auto& pair : fake_video_renderers_) { |
| 392 | min_frames = std::min(min_frames, pair.second->num_rendered_frames()); |
| 393 | } |
| 394 | return min_frames; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // In contrast to the above, sums the video frames received for all tracks. |
| 399 | // Can be used to verify that no video frames were received, or that the |
| 400 | // counts didn't increase. |
| 401 | int total_video_frames_received() const { |
| 402 | int total = 0; |
| 403 | if (video_decoder_factory_enabled_) { |
| 404 | const std::vector<FakeWebRtcVideoDecoder*>& decoders = |
| 405 | fake_video_decoder_factory_->decoders(); |
| 406 | for (const FakeWebRtcVideoDecoder* decoder : decoders) { |
| 407 | total += decoder->GetNumFramesReceived(); |
| 408 | } |
| 409 | } else { |
| 410 | for (const auto& pair : fake_video_renderers_) { |
| 411 | total += pair.second->num_rendered_frames(); |
| 412 | } |
| 413 | for (const auto& renderer : removed_fake_video_renderers_) { |
| 414 | total += renderer->num_rendered_frames(); |
| 415 | } |
| 416 | } |
| 417 | return total; |
| 418 | } |
| 419 | |
| 420 | // Returns a MockStatsObserver in a state after stats gathering finished, |
| 421 | // which can be used to access the gathered stats. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 422 | rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack( |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 423 | webrtc::MediaStreamTrackInterface* track) { |
| 424 | rtc::scoped_refptr<MockStatsObserver> observer( |
| 425 | new rtc::RefCountedObject<MockStatsObserver>()); |
| 426 | EXPECT_TRUE(peer_connection_->GetStats( |
| 427 | observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard)); |
| 428 | EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); |
| 429 | return observer; |
| 430 | } |
| 431 | |
| 432 | // Version that doesn't take a track "filter", and gathers all stats. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 433 | rtc::scoped_refptr<MockStatsObserver> OldGetStats() { |
| 434 | return OldGetStatsForTrack(nullptr); |
| 435 | } |
| 436 | |
| 437 | // Synchronously gets stats and returns them. If it times out, fails the test |
| 438 | // and returns null. |
| 439 | rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() { |
| 440 | rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback( |
| 441 | new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>()); |
| 442 | peer_connection_->GetStats(callback); |
| 443 | EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout); |
| 444 | return callback->report(); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | int rendered_width() { |
| 448 | EXPECT_FALSE(fake_video_renderers_.empty()); |
| 449 | return fake_video_renderers_.empty() |
| 450 | ? 0 |
| 451 | : fake_video_renderers_.begin()->second->width(); |
| 452 | } |
| 453 | |
| 454 | int rendered_height() { |
| 455 | EXPECT_FALSE(fake_video_renderers_.empty()); |
| 456 | return fake_video_renderers_.empty() |
| 457 | ? 0 |
| 458 | : fake_video_renderers_.begin()->second->height(); |
| 459 | } |
| 460 | |
| 461 | double rendered_aspect_ratio() { |
| 462 | if (rendered_height() == 0) { |
| 463 | return 0.0; |
| 464 | } |
| 465 | return static_cast<double>(rendered_width()) / rendered_height(); |
| 466 | } |
| 467 | |
| 468 | webrtc::VideoRotation rendered_rotation() { |
| 469 | EXPECT_FALSE(fake_video_renderers_.empty()); |
| 470 | return fake_video_renderers_.empty() |
| 471 | ? webrtc::kVideoRotation_0 |
| 472 | : fake_video_renderers_.begin()->second->rotation(); |
| 473 | } |
| 474 | |
| 475 | int local_rendered_width() { |
| 476 | return local_video_renderer_ ? local_video_renderer_->width() : 0; |
| 477 | } |
| 478 | |
| 479 | int local_rendered_height() { |
| 480 | return local_video_renderer_ ? local_video_renderer_->height() : 0; |
| 481 | } |
| 482 | |
| 483 | double local_rendered_aspect_ratio() { |
| 484 | if (local_rendered_height() == 0) { |
| 485 | return 0.0; |
| 486 | } |
| 487 | return static_cast<double>(local_rendered_width()) / |
| 488 | local_rendered_height(); |
| 489 | } |
| 490 | |
| 491 | size_t number_of_remote_streams() { |
| 492 | if (!pc()) { |
| 493 | return 0; |
| 494 | } |
| 495 | return pc()->remote_streams()->count(); |
| 496 | } |
| 497 | |
| 498 | StreamCollectionInterface* remote_streams() const { |
| 499 | if (!pc()) { |
| 500 | ADD_FAILURE(); |
| 501 | return nullptr; |
| 502 | } |
| 503 | return pc()->remote_streams(); |
| 504 | } |
| 505 | |
| 506 | StreamCollectionInterface* local_streams() { |
| 507 | if (!pc()) { |
| 508 | ADD_FAILURE(); |
| 509 | return nullptr; |
| 510 | } |
| 511 | return pc()->local_streams(); |
| 512 | } |
| 513 | |
| 514 | webrtc::PeerConnectionInterface::SignalingState signaling_state() { |
| 515 | return pc()->signaling_state(); |
| 516 | } |
| 517 | |
| 518 | webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() { |
| 519 | return pc()->ice_connection_state(); |
| 520 | } |
| 521 | |
| 522 | webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() { |
| 523 | return pc()->ice_gathering_state(); |
| 524 | } |
| 525 | |
| 526 | // Returns a MockRtpReceiverObserver for each RtpReceiver returned by |
| 527 | // GetReceivers. They're updated automatically when a remote offer/answer |
| 528 | // from the fake signaling channel is applied, or when |
| 529 | // ResetRtpReceiverObservers below is called. |
| 530 | const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& |
| 531 | rtp_receiver_observers() { |
| 532 | return rtp_receiver_observers_; |
| 533 | } |
| 534 | |
| 535 | void ResetRtpReceiverObservers() { |
| 536 | rtp_receiver_observers_.clear(); |
| 537 | for (auto receiver : pc()->GetReceivers()) { |
| 538 | std::unique_ptr<MockRtpReceiverObserver> observer( |
| 539 | new MockRtpReceiverObserver(receiver->media_type())); |
| 540 | receiver->SetObserver(observer.get()); |
| 541 | rtp_receiver_observers_.push_back(std::move(observer)); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | private: |
| 546 | explicit PeerConnectionWrapper(const std::string& debug_name) |
| 547 | : debug_name_(debug_name) {} |
| 548 | |
| 549 | bool Init( |
| 550 | const MediaConstraintsInterface* constraints, |
| 551 | const PeerConnectionFactory::Options* options, |
| 552 | const PeerConnectionInterface::RTCConfiguration* config, |
| 553 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
| 554 | rtc::Thread* network_thread, |
| 555 | rtc::Thread* worker_thread) { |
| 556 | // There's an error in this test code if Init ends up being called twice. |
| 557 | RTC_DCHECK(!peer_connection_); |
| 558 | RTC_DCHECK(!peer_connection_factory_); |
| 559 | |
| 560 | fake_network_manager_.reset(new rtc::FakeNetworkManager()); |
| 561 | fake_network_manager_->AddInterface(rtc::SocketAddress("192.168.1.1", 0)); |
| 562 | |
| 563 | std::unique_ptr<cricket::PortAllocator> port_allocator( |
| 564 | new cricket::BasicPortAllocator(fake_network_manager_.get())); |
| 565 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
| 566 | if (!fake_audio_capture_module_) { |
| 567 | return false; |
| 568 | } |
| 569 | // Note that these factories don't end up getting used unless supported |
| 570 | // codecs are added to them. |
| 571 | fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory(); |
| 572 | fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory(); |
| 573 | rtc::Thread* const signaling_thread = rtc::Thread::Current(); |
| 574 | peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( |
| 575 | network_thread, worker_thread, signaling_thread, |
| 576 | fake_audio_capture_module_, fake_video_encoder_factory_, |
| 577 | fake_video_decoder_factory_); |
| 578 | if (!peer_connection_factory_) { |
| 579 | return false; |
| 580 | } |
| 581 | if (options) { |
| 582 | peer_connection_factory_->SetOptions(*options); |
| 583 | } |
| 584 | peer_connection_ = |
| 585 | CreatePeerConnection(std::move(port_allocator), constraints, config, |
| 586 | std::move(cert_generator)); |
| 587 | return peer_connection_.get() != nullptr; |
| 588 | } |
| 589 | |
| 590 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection( |
| 591 | std::unique_ptr<cricket::PortAllocator> port_allocator, |
| 592 | const MediaConstraintsInterface* constraints, |
| 593 | const PeerConnectionInterface::RTCConfiguration* config, |
| 594 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) { |
| 595 | PeerConnectionInterface::RTCConfiguration modified_config; |
| 596 | // If |config| is null, this will result in a default configuration being |
| 597 | // used. |
| 598 | if (config) { |
| 599 | modified_config = *config; |
| 600 | } |
| 601 | // Disable resolution adaptation; we don't want it interfering with the |
| 602 | // test results. |
| 603 | // TODO(deadbeef): Do something more robust. Since we're testing for aspect |
| 604 | // ratios and not specific resolutions, is this even necessary? |
| 605 | modified_config.set_cpu_adaptation(false); |
| 606 | |
| 607 | return peer_connection_factory_->CreatePeerConnection( |
| 608 | modified_config, constraints, std::move(port_allocator), |
| 609 | std::move(cert_generator), this); |
| 610 | } |
| 611 | |
| 612 | void set_signaling_message_receiver( |
| 613 | SignalingMessageReceiver* signaling_message_receiver) { |
| 614 | signaling_message_receiver_ = signaling_message_receiver; |
| 615 | } |
| 616 | |
| 617 | void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; } |
| 618 | |
| 619 | void EnableVideoDecoderFactory() { |
| 620 | video_decoder_factory_enabled_ = true; |
| 621 | fake_video_decoder_factory_->AddSupportedVideoCodecType( |
| 622 | webrtc::kVideoCodecVP8); |
| 623 | } |
| 624 | |
| 625 | rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal( |
| 626 | const std::string& track_id, |
| 627 | const FakeConstraints& constraints, |
| 628 | webrtc::VideoRotation rotation) { |
| 629 | // Set max frame rate to 10fps to reduce the risk of test flakiness. |
| 630 | // TODO(deadbeef): Do something more robust. |
| 631 | FakeConstraints source_constraints = constraints; |
| 632 | source_constraints.SetMandatoryMaxFrameRate(10); |
| 633 | |
| 634 | cricket::FakeVideoCapturer* fake_capturer = |
| 635 | new webrtc::FakePeriodicVideoCapturer(); |
| 636 | fake_capturer->SetRotation(rotation); |
| 637 | video_capturers_.push_back(fake_capturer); |
| 638 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source = |
| 639 | peer_connection_factory_->CreateVideoSource(fake_capturer, |
| 640 | &source_constraints); |
| 641 | rtc::scoped_refptr<webrtc::VideoTrackInterface> track( |
| 642 | peer_connection_factory_->CreateVideoTrack(track_id, source)); |
| 643 | if (!local_video_renderer_) { |
| 644 | local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track)); |
| 645 | } |
| 646 | return track; |
| 647 | } |
| 648 | |
| 649 | void HandleIncomingOffer(const std::string& msg) { |
| 650 | LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer"; |
| 651 | std::unique_ptr<SessionDescriptionInterface> desc( |
| 652 | webrtc::CreateSessionDescription("offer", msg, nullptr)); |
| 653 | if (received_sdp_munger_) { |
| 654 | received_sdp_munger_(desc->description()); |
| 655 | } |
| 656 | |
| 657 | EXPECT_TRUE(SetRemoteDescription(std::move(desc))); |
| 658 | // Setting a remote description may have changed the number of receivers, |
| 659 | // so reset the receiver observers. |
| 660 | ResetRtpReceiverObservers(); |
| 661 | auto answer = CreateAnswer(); |
| 662 | ASSERT_NE(nullptr, answer); |
| 663 | EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer))); |
| 664 | } |
| 665 | |
| 666 | void HandleIncomingAnswer(const std::string& msg) { |
| 667 | LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer"; |
| 668 | std::unique_ptr<SessionDescriptionInterface> desc( |
| 669 | webrtc::CreateSessionDescription("answer", msg, nullptr)); |
| 670 | if (received_sdp_munger_) { |
| 671 | received_sdp_munger_(desc->description()); |
| 672 | } |
| 673 | |
| 674 | EXPECT_TRUE(SetRemoteDescription(std::move(desc))); |
| 675 | // Set the RtpReceiverObserver after receivers are created. |
| 676 | ResetRtpReceiverObservers(); |
| 677 | } |
| 678 | |
| 679 | // Returns null on failure. |
| 680 | std::unique_ptr<SessionDescriptionInterface> CreateOffer() { |
| 681 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( |
| 682 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 683 | pc()->CreateOffer(observer, offer_answer_options_); |
| 684 | return WaitForDescriptionFromObserver(observer); |
| 685 | } |
| 686 | |
| 687 | // Returns null on failure. |
| 688 | std::unique_ptr<SessionDescriptionInterface> CreateAnswer() { |
| 689 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer( |
| 690 | new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>()); |
| 691 | pc()->CreateAnswer(observer, offer_answer_options_); |
| 692 | return WaitForDescriptionFromObserver(observer); |
| 693 | } |
| 694 | |
| 695 | std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver( |
| 696 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer) { |
| 697 | EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout); |
| 698 | if (!observer->result()) { |
| 699 | return nullptr; |
| 700 | } |
| 701 | auto description = observer->MoveDescription(); |
| 702 | if (generated_sdp_munger_) { |
| 703 | generated_sdp_munger_(description->description()); |
| 704 | } |
| 705 | return description; |
| 706 | } |
| 707 | |
| 708 | // Setting the local description and sending the SDP message over the fake |
| 709 | // signaling channel are combined into the same method because the SDP |
| 710 | // message needs to be sent as soon as SetLocalDescription finishes, without |
| 711 | // waiting for the observer to be called. This ensures that ICE candidates |
| 712 | // don't outrace the description. |
| 713 | bool SetLocalDescriptionAndSendSdpMessage( |
| 714 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 715 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( |
| 716 | new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); |
| 717 | LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage"; |
| 718 | std::string type = desc->type(); |
| 719 | std::string sdp; |
| 720 | EXPECT_TRUE(desc->ToString(&sdp)); |
| 721 | pc()->SetLocalDescription(observer, desc.release()); |
| 722 | // As mentioned above, we need to send the message immediately after |
| 723 | // SetLocalDescription. |
| 724 | SendSdpMessage(type, sdp); |
| 725 | EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); |
| 726 | return true; |
| 727 | } |
| 728 | |
| 729 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) { |
| 730 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer( |
| 731 | new rtc::RefCountedObject<MockSetSessionDescriptionObserver>()); |
| 732 | LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription"; |
| 733 | pc()->SetRemoteDescription(observer, desc.release()); |
| 734 | EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout); |
| 735 | return observer->result(); |
| 736 | } |
| 737 | |
| 738 | // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by |
| 739 | // default). |
| 740 | void SendSdpMessage(const std::string& type, const std::string& msg) { |
| 741 | if (signaling_delay_ms_ == 0) { |
| 742 | RelaySdpMessageIfReceiverExists(type, msg); |
| 743 | } else { |
| 744 | invoker_.AsyncInvokeDelayed<void>( |
| 745 | RTC_FROM_HERE, rtc::Thread::Current(), |
| 746 | rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists, |
| 747 | this, type, msg), |
| 748 | signaling_delay_ms_); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | void RelaySdpMessageIfReceiverExists(const std::string& type, |
| 753 | const std::string& msg) { |
| 754 | if (signaling_message_receiver_) { |
| 755 | signaling_message_receiver_->ReceiveSdpMessage(type, msg); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by |
| 760 | // default). |
| 761 | void SendIceMessage(const std::string& sdp_mid, |
| 762 | int sdp_mline_index, |
| 763 | const std::string& msg) { |
| 764 | if (signaling_delay_ms_ == 0) { |
| 765 | RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg); |
| 766 | } else { |
| 767 | invoker_.AsyncInvokeDelayed<void>( |
| 768 | RTC_FROM_HERE, rtc::Thread::Current(), |
| 769 | rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists, |
| 770 | this, sdp_mid, sdp_mline_index, msg), |
| 771 | signaling_delay_ms_); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | void RelayIceMessageIfReceiverExists(const std::string& sdp_mid, |
| 776 | int sdp_mline_index, |
| 777 | const std::string& msg) { |
| 778 | if (signaling_message_receiver_) { |
| 779 | signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index, |
| 780 | msg); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // SignalingMessageReceiver callbacks. |
| 785 | void ReceiveSdpMessage(const std::string& type, |
| 786 | const std::string& msg) override { |
| 787 | if (type == webrtc::SessionDescriptionInterface::kOffer) { |
| 788 | HandleIncomingOffer(msg); |
| 789 | } else { |
| 790 | HandleIncomingAnswer(msg); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | void ReceiveIceMessage(const std::string& sdp_mid, |
| 795 | int sdp_mline_index, |
| 796 | const std::string& msg) override { |
| 797 | LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage"; |
| 798 | std::unique_ptr<webrtc::IceCandidateInterface> candidate( |
| 799 | webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr)); |
| 800 | EXPECT_TRUE(pc()->AddIceCandidate(candidate.get())); |
| 801 | } |
| 802 | |
| 803 | // PeerConnectionObserver callbacks. |
| 804 | void OnSignalingChange( |
| 805 | webrtc::PeerConnectionInterface::SignalingState new_state) override { |
| 806 | EXPECT_EQ(pc()->signaling_state(), new_state); |
| 807 | } |
| 808 | void OnAddStream( |
| 809 | rtc::scoped_refptr<MediaStreamInterface> media_stream) override { |
| 810 | media_stream->RegisterObserver(this); |
| 811 | for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) { |
| 812 | const std::string id = media_stream->GetVideoTracks()[i]->id(); |
| 813 | ASSERT_TRUE(fake_video_renderers_.find(id) == |
| 814 | fake_video_renderers_.end()); |
| 815 | fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer( |
| 816 | media_stream->GetVideoTracks()[i])); |
| 817 | } |
| 818 | } |
| 819 | void OnRemoveStream( |
| 820 | rtc::scoped_refptr<MediaStreamInterface> media_stream) override {} |
| 821 | void OnRenegotiationNeeded() override {} |
| 822 | void OnIceConnectionChange( |
| 823 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override { |
| 824 | EXPECT_EQ(pc()->ice_connection_state(), new_state); |
| 825 | } |
| 826 | void OnIceGatheringChange( |
| 827 | webrtc::PeerConnectionInterface::IceGatheringState new_state) override { |
| 828 | if (new_state == PeerConnectionInterface::kIceGatheringGathering) { |
| 829 | ++transitions_to_gathering_state_; |
| 830 | } |
| 831 | EXPECT_EQ(pc()->ice_gathering_state(), new_state); |
| 832 | } |
| 833 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override { |
| 834 | LOG(LS_INFO) << debug_name_ << ": OnIceCandidate"; |
| 835 | |
| 836 | std::string ice_sdp; |
| 837 | EXPECT_TRUE(candidate->ToString(&ice_sdp)); |
| 838 | if (signaling_message_receiver_ == nullptr) { |
| 839 | // Remote party may be deleted. |
| 840 | return; |
| 841 | } |
| 842 | SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp); |
| 843 | } |
| 844 | void OnDataChannel( |
| 845 | rtc::scoped_refptr<DataChannelInterface> data_channel) override { |
| 846 | LOG(LS_INFO) << debug_name_ << ": OnDataChannel"; |
| 847 | data_channel_ = data_channel; |
| 848 | data_observer_.reset(new MockDataChannelObserver(data_channel)); |
| 849 | } |
| 850 | |
| 851 | // MediaStreamInterface callback |
| 852 | void OnChanged() override { |
| 853 | // Track added or removed from MediaStream, so update our renderers. |
| 854 | rtc::scoped_refptr<StreamCollectionInterface> remote_streams = |
| 855 | pc()->remote_streams(); |
| 856 | // Remove renderers for tracks that were removed. |
| 857 | for (auto it = fake_video_renderers_.begin(); |
| 858 | it != fake_video_renderers_.end();) { |
| 859 | if (remote_streams->FindVideoTrack(it->first) == nullptr) { |
| 860 | auto to_remove = it++; |
| 861 | removed_fake_video_renderers_.push_back(std::move(to_remove->second)); |
| 862 | fake_video_renderers_.erase(to_remove); |
| 863 | } else { |
| 864 | ++it; |
| 865 | } |
| 866 | } |
| 867 | // Create renderers for new video tracks. |
| 868 | for (size_t stream_index = 0; stream_index < remote_streams->count(); |
| 869 | ++stream_index) { |
| 870 | MediaStreamInterface* remote_stream = remote_streams->at(stream_index); |
| 871 | for (size_t track_index = 0; |
| 872 | track_index < remote_stream->GetVideoTracks().size(); |
| 873 | ++track_index) { |
| 874 | const std::string id = |
| 875 | remote_stream->GetVideoTracks()[track_index]->id(); |
| 876 | if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) { |
| 877 | continue; |
| 878 | } |
| 879 | fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer( |
| 880 | remote_stream->GetVideoTracks()[track_index])); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | std::string debug_name_; |
| 886 | |
| 887 | std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_; |
| 888 | |
| 889 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
| 890 | rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> |
| 891 | peer_connection_factory_; |
| 892 | |
| 893 | // Needed to keep track of number of frames sent. |
| 894 | rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; |
| 895 | // Needed to keep track of number of frames received. |
| 896 | std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>> |
| 897 | fake_video_renderers_; |
| 898 | // Needed to ensure frames aren't received for removed tracks. |
| 899 | std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>> |
| 900 | removed_fake_video_renderers_; |
| 901 | // Needed to keep track of number of frames received when external decoder |
| 902 | // used. |
| 903 | FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr; |
| 904 | FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr; |
| 905 | bool video_decoder_factory_enabled_ = false; |
| 906 | |
| 907 | // For remote peer communication. |
| 908 | SignalingMessageReceiver* signaling_message_receiver_ = nullptr; |
| 909 | int signaling_delay_ms_ = 0; |
| 910 | |
| 911 | // Store references to the video capturers we've created, so that we can stop |
| 912 | // them, if required. |
| 913 | std::vector<cricket::FakeVideoCapturer*> video_capturers_; |
| 914 | // |local_video_renderer_| attached to the first created local video track. |
| 915 | std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_; |
| 916 | |
| 917 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_; |
| 918 | std::function<void(cricket::SessionDescription*)> received_sdp_munger_; |
| 919 | std::function<void(cricket::SessionDescription*)> generated_sdp_munger_; |
| 920 | |
| 921 | rtc::scoped_refptr<DataChannelInterface> data_channel_; |
| 922 | std::unique_ptr<MockDataChannelObserver> data_observer_; |
| 923 | |
| 924 | std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_; |
| 925 | |
| 926 | int transitions_to_gathering_state_ = 0; |
| 927 | |
| 928 | rtc::AsyncInvoker invoker_; |
| 929 | |
| 930 | friend class PeerConnectionIntegrationTest; |
| 931 | }; |
| 932 | |
| 933 | // Tests two PeerConnections connecting to each other end-to-end, using a |
| 934 | // virtual network, fake A/V capture and fake encoder/decoders. The |
| 935 | // PeerConnections share the threads/socket servers, but use separate versions |
| 936 | // of everything else (including "PeerConnectionFactory"s). |
| 937 | class PeerConnectionIntegrationTest : public testing::Test { |
| 938 | public: |
| 939 | PeerConnectionIntegrationTest() |
| 940 | : pss_(new rtc::PhysicalSocketServer), |
| 941 | ss_(new rtc::VirtualSocketServer(pss_.get())), |
| 942 | network_thread_(new rtc::Thread(ss_.get())), |
| 943 | worker_thread_(rtc::Thread::Create()) { |
| 944 | RTC_CHECK(network_thread_->Start()); |
| 945 | RTC_CHECK(worker_thread_->Start()); |
| 946 | } |
| 947 | |
| 948 | ~PeerConnectionIntegrationTest() { |
| 949 | if (caller_) { |
| 950 | caller_->set_signaling_message_receiver(nullptr); |
| 951 | } |
| 952 | if (callee_) { |
| 953 | callee_->set_signaling_message_receiver(nullptr); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | bool SignalingStateStable() { |
| 958 | return caller_->SignalingStateStable() && callee_->SignalingStateStable(); |
| 959 | } |
| 960 | |
deadbeef | 7145280 | 2017-05-07 17:21:01 -0700 | [diff] [blame^] | 961 | bool DtlsConnected() { |
| 962 | // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS |
| 963 | // are connected. This is an important distinction. Once we have separate |
| 964 | // ICE and DTLS state, this check needs to use the DTLS state. |
| 965 | return (callee()->ice_connection_state() == |
| 966 | webrtc::PeerConnectionInterface::kIceConnectionConnected || |
| 967 | callee()->ice_connection_state() == |
| 968 | webrtc::PeerConnectionInterface::kIceConnectionCompleted) && |
| 969 | (caller()->ice_connection_state() == |
| 970 | webrtc::PeerConnectionInterface::kIceConnectionConnected || |
| 971 | caller()->ice_connection_state() == |
| 972 | webrtc::PeerConnectionInterface::kIceConnectionCompleted); |
| 973 | } |
| 974 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 975 | bool CreatePeerConnectionWrappers() { |
| 976 | return CreatePeerConnectionWrappersWithConfig( |
| 977 | PeerConnectionInterface::RTCConfiguration(), |
| 978 | PeerConnectionInterface::RTCConfiguration()); |
| 979 | } |
| 980 | |
| 981 | bool CreatePeerConnectionWrappersWithConstraints( |
| 982 | MediaConstraintsInterface* caller_constraints, |
| 983 | MediaConstraintsInterface* callee_constraints) { |
| 984 | caller_.reset(PeerConnectionWrapper::CreateWithConstraints( |
| 985 | "Caller", caller_constraints, network_thread_.get(), |
| 986 | worker_thread_.get())); |
| 987 | callee_.reset(PeerConnectionWrapper::CreateWithConstraints( |
| 988 | "Callee", callee_constraints, network_thread_.get(), |
| 989 | worker_thread_.get())); |
| 990 | return caller_ && callee_; |
| 991 | } |
| 992 | |
| 993 | bool CreatePeerConnectionWrappersWithConfig( |
| 994 | const PeerConnectionInterface::RTCConfiguration& caller_config, |
| 995 | const PeerConnectionInterface::RTCConfiguration& callee_config) { |
| 996 | caller_.reset(PeerConnectionWrapper::CreateWithConfig( |
| 997 | "Caller", caller_config, network_thread_.get(), worker_thread_.get())); |
| 998 | callee_.reset(PeerConnectionWrapper::CreateWithConfig( |
| 999 | "Callee", callee_config, network_thread_.get(), worker_thread_.get())); |
| 1000 | return caller_ && callee_; |
| 1001 | } |
| 1002 | |
| 1003 | bool CreatePeerConnectionWrappersWithOptions( |
| 1004 | const PeerConnectionFactory::Options& caller_options, |
| 1005 | const PeerConnectionFactory::Options& callee_options) { |
| 1006 | caller_.reset(PeerConnectionWrapper::CreateWithOptions( |
| 1007 | "Caller", caller_options, network_thread_.get(), worker_thread_.get())); |
| 1008 | callee_.reset(PeerConnectionWrapper::CreateWithOptions( |
| 1009 | "Callee", callee_options, network_thread_.get(), worker_thread_.get())); |
| 1010 | return caller_ && callee_; |
| 1011 | } |
| 1012 | |
| 1013 | PeerConnectionWrapper* CreatePeerConnectionWrapperWithAlternateKey() { |
| 1014 | std::unique_ptr<FakeRTCCertificateGenerator> cert_generator( |
| 1015 | new FakeRTCCertificateGenerator()); |
| 1016 | cert_generator->use_alternate_key(); |
| 1017 | |
| 1018 | // Make sure the new client is using a different certificate. |
| 1019 | return PeerConnectionWrapper::CreateWithDtlsIdentityStore( |
| 1020 | "New Peer", std::move(cert_generator), network_thread_.get(), |
| 1021 | worker_thread_.get()); |
| 1022 | } |
| 1023 | |
| 1024 | // Once called, SDP blobs and ICE candidates will be automatically signaled |
| 1025 | // between PeerConnections. |
| 1026 | void ConnectFakeSignaling() { |
| 1027 | caller_->set_signaling_message_receiver(callee_.get()); |
| 1028 | callee_->set_signaling_message_receiver(caller_.get()); |
| 1029 | } |
| 1030 | |
| 1031 | void SetSignalingDelayMs(int delay_ms) { |
| 1032 | caller_->set_signaling_delay_ms(delay_ms); |
| 1033 | callee_->set_signaling_delay_ms(delay_ms); |
| 1034 | } |
| 1035 | |
| 1036 | void EnableVideoDecoderFactory() { |
| 1037 | caller_->EnableVideoDecoderFactory(); |
| 1038 | callee_->EnableVideoDecoderFactory(); |
| 1039 | } |
| 1040 | |
| 1041 | // Messages may get lost on the unreliable DataChannel, so we send multiple |
| 1042 | // times to avoid test flakiness. |
| 1043 | void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc, |
| 1044 | const std::string& data, |
| 1045 | int retries) { |
| 1046 | for (int i = 0; i < retries; ++i) { |
| 1047 | dc->Send(DataBuffer(data)); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | rtc::Thread* network_thread() { return network_thread_.get(); } |
| 1052 | |
| 1053 | rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); } |
| 1054 | |
| 1055 | PeerConnectionWrapper* caller() { return caller_.get(); } |
| 1056 | |
| 1057 | // Set the |caller_| to the |wrapper| passed in and return the |
| 1058 | // original |caller_|. |
| 1059 | PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent( |
| 1060 | PeerConnectionWrapper* wrapper) { |
| 1061 | PeerConnectionWrapper* old = caller_.release(); |
| 1062 | caller_.reset(wrapper); |
| 1063 | return old; |
| 1064 | } |
| 1065 | |
| 1066 | PeerConnectionWrapper* callee() { return callee_.get(); } |
| 1067 | |
| 1068 | // Set the |callee_| to the |wrapper| passed in and return the |
| 1069 | // original |callee_|. |
| 1070 | PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent( |
| 1071 | PeerConnectionWrapper* wrapper) { |
| 1072 | PeerConnectionWrapper* old = callee_.release(); |
| 1073 | callee_.reset(wrapper); |
| 1074 | return old; |
| 1075 | } |
| 1076 | |
| 1077 | // Expects the provided number of new frames to be received within |wait_ms|. |
| 1078 | // "New frames" meaning that it waits for the current frame counts to |
| 1079 | // *increase* by the provided values. For video, uses |
| 1080 | // RecievedVideoFramesForEachTrack for the case of multiple video tracks |
| 1081 | // being received. |
| 1082 | void ExpectNewFramesReceivedWithWait( |
| 1083 | int expected_caller_received_audio_frames, |
| 1084 | int expected_caller_received_video_frames, |
| 1085 | int expected_callee_received_audio_frames, |
| 1086 | int expected_callee_received_video_frames, |
| 1087 | int wait_ms) { |
| 1088 | // Add current frame counts to the provided values, in order to wait for |
| 1089 | // the frame count to increase. |
| 1090 | expected_caller_received_audio_frames += caller()->audio_frames_received(); |
| 1091 | expected_caller_received_video_frames += |
| 1092 | caller()->min_video_frames_received_per_track(); |
| 1093 | expected_callee_received_audio_frames += callee()->audio_frames_received(); |
| 1094 | expected_callee_received_video_frames += |
| 1095 | callee()->min_video_frames_received_per_track(); |
| 1096 | |
| 1097 | EXPECT_TRUE_WAIT(caller()->audio_frames_received() >= |
| 1098 | expected_caller_received_audio_frames && |
| 1099 | caller()->min_video_frames_received_per_track() >= |
| 1100 | expected_caller_received_video_frames && |
| 1101 | callee()->audio_frames_received() >= |
| 1102 | expected_callee_received_audio_frames && |
| 1103 | callee()->min_video_frames_received_per_track() >= |
| 1104 | expected_callee_received_video_frames, |
| 1105 | wait_ms); |
| 1106 | |
| 1107 | // After the combined wait, do an "expect" for each individual count, to |
| 1108 | // print out a more detailed message upon failure. |
| 1109 | EXPECT_GE(caller()->audio_frames_received(), |
| 1110 | expected_caller_received_audio_frames); |
| 1111 | EXPECT_GE(caller()->min_video_frames_received_per_track(), |
| 1112 | expected_caller_received_video_frames); |
| 1113 | EXPECT_GE(callee()->audio_frames_received(), |
| 1114 | expected_callee_received_audio_frames); |
| 1115 | EXPECT_GE(callee()->min_video_frames_received_per_track(), |
| 1116 | expected_callee_received_video_frames); |
| 1117 | } |
| 1118 | |
| 1119 | void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled, |
| 1120 | bool remote_gcm_enabled, |
| 1121 | int expected_cipher_suite) { |
| 1122 | PeerConnectionFactory::Options caller_options; |
| 1123 | caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled; |
| 1124 | PeerConnectionFactory::Options callee_options; |
| 1125 | callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled; |
| 1126 | ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options, |
| 1127 | callee_options)); |
| 1128 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer = |
| 1129 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 1130 | caller()->pc()->RegisterUMAObserver(caller_observer); |
| 1131 | ConnectFakeSignaling(); |
| 1132 | caller()->AddAudioVideoMediaStream(); |
| 1133 | callee()->AddAudioVideoMediaStream(); |
| 1134 | caller()->CreateAndSetAndSignalOffer(); |
| 1135 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1136 | EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite), |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1137 | caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1138 | EXPECT_EQ( |
| 1139 | 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher, |
| 1140 | expected_cipher_suite)); |
| 1141 | caller()->pc()->RegisterUMAObserver(nullptr); |
| 1142 | } |
| 1143 | |
| 1144 | private: |
| 1145 | // |ss_| is used by |network_thread_| so it must be destroyed later. |
| 1146 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 1147 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
| 1148 | // |network_thread_| and |worker_thread_| are used by both |
| 1149 | // |caller_| and |callee_| so they must be destroyed |
| 1150 | // later. |
| 1151 | std::unique_ptr<rtc::Thread> network_thread_; |
| 1152 | std::unique_ptr<rtc::Thread> worker_thread_; |
| 1153 | std::unique_ptr<PeerConnectionWrapper> caller_; |
| 1154 | std::unique_ptr<PeerConnectionWrapper> callee_; |
| 1155 | }; |
| 1156 | |
| 1157 | // Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This |
| 1158 | // includes testing that the callback is invoked if an observer is connected |
| 1159 | // after the first packet has already been received. |
| 1160 | TEST_F(PeerConnectionIntegrationTest, |
| 1161 | RtpReceiverObserverOnFirstPacketReceived) { |
| 1162 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1163 | ConnectFakeSignaling(); |
| 1164 | caller()->AddAudioVideoMediaStream(); |
| 1165 | callee()->AddAudioVideoMediaStream(); |
| 1166 | // Start offer/answer exchange and wait for it to complete. |
| 1167 | caller()->CreateAndSetAndSignalOffer(); |
| 1168 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1169 | // Should be one receiver each for audio/video. |
| 1170 | EXPECT_EQ(2, caller()->rtp_receiver_observers().size()); |
| 1171 | EXPECT_EQ(2, callee()->rtp_receiver_observers().size()); |
| 1172 | // Wait for all "first packet received" callbacks to be fired. |
| 1173 | EXPECT_TRUE_WAIT( |
| 1174 | std::all_of(caller()->rtp_receiver_observers().begin(), |
| 1175 | caller()->rtp_receiver_observers().end(), |
| 1176 | [](const std::unique_ptr<MockRtpReceiverObserver>& o) { |
| 1177 | return o->first_packet_received(); |
| 1178 | }), |
| 1179 | kMaxWaitForFramesMs); |
| 1180 | EXPECT_TRUE_WAIT( |
| 1181 | std::all_of(callee()->rtp_receiver_observers().begin(), |
| 1182 | callee()->rtp_receiver_observers().end(), |
| 1183 | [](const std::unique_ptr<MockRtpReceiverObserver>& o) { |
| 1184 | return o->first_packet_received(); |
| 1185 | }), |
| 1186 | kMaxWaitForFramesMs); |
| 1187 | // If new observers are set after the first packet was already received, the |
| 1188 | // callback should still be invoked. |
| 1189 | caller()->ResetRtpReceiverObservers(); |
| 1190 | callee()->ResetRtpReceiverObservers(); |
| 1191 | EXPECT_EQ(2, caller()->rtp_receiver_observers().size()); |
| 1192 | EXPECT_EQ(2, callee()->rtp_receiver_observers().size()); |
| 1193 | EXPECT_TRUE( |
| 1194 | std::all_of(caller()->rtp_receiver_observers().begin(), |
| 1195 | caller()->rtp_receiver_observers().end(), |
| 1196 | [](const std::unique_ptr<MockRtpReceiverObserver>& o) { |
| 1197 | return o->first_packet_received(); |
| 1198 | })); |
| 1199 | EXPECT_TRUE( |
| 1200 | std::all_of(callee()->rtp_receiver_observers().begin(), |
| 1201 | callee()->rtp_receiver_observers().end(), |
| 1202 | [](const std::unique_ptr<MockRtpReceiverObserver>& o) { |
| 1203 | return o->first_packet_received(); |
| 1204 | })); |
| 1205 | } |
| 1206 | |
| 1207 | class DummyDtmfObserver : public DtmfSenderObserverInterface { |
| 1208 | public: |
| 1209 | DummyDtmfObserver() : completed_(false) {} |
| 1210 | |
| 1211 | // Implements DtmfSenderObserverInterface. |
| 1212 | void OnToneChange(const std::string& tone) override { |
| 1213 | tones_.push_back(tone); |
| 1214 | if (tone.empty()) { |
| 1215 | completed_ = true; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | const std::vector<std::string>& tones() const { return tones_; } |
| 1220 | bool completed() const { return completed_; } |
| 1221 | |
| 1222 | private: |
| 1223 | bool completed_; |
| 1224 | std::vector<std::string> tones_; |
| 1225 | }; |
| 1226 | |
| 1227 | // Assumes |sender| already has an audio track added and the offer/answer |
| 1228 | // exchange is done. |
| 1229 | void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender, |
| 1230 | PeerConnectionWrapper* receiver) { |
| 1231 | DummyDtmfObserver observer; |
| 1232 | rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender; |
| 1233 | |
| 1234 | // We should be able to create a DTMF sender from a local track. |
| 1235 | webrtc::AudioTrackInterface* localtrack = |
| 1236 | sender->local_streams()->at(0)->GetAudioTracks()[0]; |
| 1237 | dtmf_sender = sender->pc()->CreateDtmfSender(localtrack); |
| 1238 | ASSERT_NE(nullptr, dtmf_sender.get()); |
| 1239 | dtmf_sender->RegisterObserver(&observer); |
| 1240 | |
| 1241 | // Test the DtmfSender object just created. |
| 1242 | EXPECT_TRUE(dtmf_sender->CanInsertDtmf()); |
| 1243 | EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50)); |
| 1244 | |
| 1245 | EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout); |
| 1246 | std::vector<std::string> tones = {"1", "a", ""}; |
| 1247 | EXPECT_EQ(tones, observer.tones()); |
| 1248 | dtmf_sender->UnregisterObserver(); |
| 1249 | // TODO(deadbeef): Verify the tones were actually received end-to-end. |
| 1250 | } |
| 1251 | |
| 1252 | // Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each |
| 1253 | // direction). |
| 1254 | TEST_F(PeerConnectionIntegrationTest, DtmfSenderObserver) { |
| 1255 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1256 | ConnectFakeSignaling(); |
| 1257 | // Only need audio for DTMF. |
| 1258 | caller()->AddAudioOnlyMediaStream(); |
| 1259 | callee()->AddAudioOnlyMediaStream(); |
| 1260 | caller()->CreateAndSetAndSignalOffer(); |
| 1261 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
deadbeef | 7145280 | 2017-05-07 17:21:01 -0700 | [diff] [blame^] | 1262 | // DTLS must finish before the DTMF sender can be used reliably. |
| 1263 | ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1264 | TestDtmfFromSenderToReceiver(caller(), callee()); |
| 1265 | TestDtmfFromSenderToReceiver(callee(), caller()); |
| 1266 | } |
| 1267 | |
| 1268 | // Basic end-to-end test, verifying media can be encoded/transmitted/decoded |
| 1269 | // between two connections, using DTLS-SRTP. |
| 1270 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls) { |
| 1271 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1272 | ConnectFakeSignaling(); |
| 1273 | // Do normal offer/answer and wait for some frames to be received in each |
| 1274 | // direction. |
| 1275 | caller()->AddAudioVideoMediaStream(); |
| 1276 | callee()->AddAudioVideoMediaStream(); |
| 1277 | caller()->CreateAndSetAndSignalOffer(); |
| 1278 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1279 | ExpectNewFramesReceivedWithWait( |
| 1280 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1281 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1282 | kMaxWaitForFramesMs); |
| 1283 | } |
| 1284 | |
| 1285 | // Uses SDES instead of DTLS for key agreement. |
| 1286 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSdes) { |
| 1287 | PeerConnectionInterface::RTCConfiguration sdes_config; |
| 1288 | sdes_config.enable_dtls_srtp.emplace(false); |
| 1289 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config)); |
| 1290 | ConnectFakeSignaling(); |
| 1291 | |
| 1292 | // Do normal offer/answer and wait for some frames to be received in each |
| 1293 | // direction. |
| 1294 | caller()->AddAudioVideoMediaStream(); |
| 1295 | callee()->AddAudioVideoMediaStream(); |
| 1296 | caller()->CreateAndSetAndSignalOffer(); |
| 1297 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1298 | ExpectNewFramesReceivedWithWait( |
| 1299 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1300 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1301 | kMaxWaitForFramesMs); |
| 1302 | } |
| 1303 | |
| 1304 | // This test sets up a call between two parties (using DTLS) and tests that we |
| 1305 | // can get a video aspect ratio of 16:9. |
| 1306 | TEST_F(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) { |
| 1307 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1308 | ConnectFakeSignaling(); |
| 1309 | |
| 1310 | // Add video tracks with 16:9 constraint. |
| 1311 | FakeConstraints constraints; |
| 1312 | double requested_ratio = 16.0 / 9; |
| 1313 | constraints.SetMandatoryMinAspectRatio(requested_ratio); |
| 1314 | caller()->AddMediaStreamFromTracks( |
| 1315 | nullptr, caller()->CreateLocalVideoTrackWithConstraints(constraints)); |
| 1316 | callee()->AddMediaStreamFromTracks( |
| 1317 | nullptr, callee()->CreateLocalVideoTrackWithConstraints(constraints)); |
| 1318 | |
| 1319 | // Do normal offer/answer and wait for at least one frame to be received in |
| 1320 | // each direction. |
| 1321 | caller()->CreateAndSetAndSignalOffer(); |
| 1322 | ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 && |
| 1323 | callee()->min_video_frames_received_per_track() > 0, |
| 1324 | kMaxWaitForFramesMs); |
| 1325 | |
| 1326 | // Check rendered aspect ratio. |
| 1327 | EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio()); |
| 1328 | EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio()); |
| 1329 | EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio()); |
| 1330 | EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio()); |
| 1331 | } |
| 1332 | |
| 1333 | // This test sets up a call between two parties with a source resolution of |
| 1334 | // 1280x720 and verifies that a 16:9 aspect ratio is received. |
| 1335 | TEST_F(PeerConnectionIntegrationTest, |
| 1336 | Send1280By720ResolutionAndReceive16To9AspectRatio) { |
| 1337 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1338 | ConnectFakeSignaling(); |
| 1339 | |
| 1340 | // Similar to above test, but uses MandatoryMin[Width/Height] constraint |
| 1341 | // instead of aspect ratio constraint. |
| 1342 | FakeConstraints constraints; |
| 1343 | constraints.SetMandatoryMinWidth(1280); |
| 1344 | constraints.SetMandatoryMinHeight(720); |
| 1345 | caller()->AddMediaStreamFromTracks( |
| 1346 | nullptr, caller()->CreateLocalVideoTrackWithConstraints(constraints)); |
| 1347 | callee()->AddMediaStreamFromTracks( |
| 1348 | nullptr, callee()->CreateLocalVideoTrackWithConstraints(constraints)); |
| 1349 | |
| 1350 | // Do normal offer/answer and wait for at least one frame to be received in |
| 1351 | // each direction. |
| 1352 | caller()->CreateAndSetAndSignalOffer(); |
| 1353 | ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 && |
| 1354 | callee()->min_video_frames_received_per_track() > 0, |
| 1355 | kMaxWaitForFramesMs); |
| 1356 | |
| 1357 | // Check rendered aspect ratio. |
| 1358 | EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio()); |
| 1359 | EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio()); |
| 1360 | EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio()); |
| 1361 | EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio()); |
| 1362 | } |
| 1363 | |
| 1364 | // This test sets up an one-way call, with media only from caller to |
| 1365 | // callee. |
| 1366 | TEST_F(PeerConnectionIntegrationTest, OneWayMediaCall) { |
| 1367 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1368 | ConnectFakeSignaling(); |
| 1369 | caller()->AddAudioVideoMediaStream(); |
| 1370 | caller()->CreateAndSetAndSignalOffer(); |
| 1371 | int caller_received_frames = 0; |
| 1372 | ExpectNewFramesReceivedWithWait( |
| 1373 | caller_received_frames, caller_received_frames, |
| 1374 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1375 | kMaxWaitForFramesMs); |
| 1376 | } |
| 1377 | |
| 1378 | // This test sets up a audio call initially, with the callee rejecting video |
| 1379 | // initially. Then later the callee decides to upgrade to audio/video, and |
| 1380 | // initiates a new offer/answer exchange. |
| 1381 | TEST_F(PeerConnectionIntegrationTest, AudioToVideoUpgrade) { |
| 1382 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1383 | ConnectFakeSignaling(); |
| 1384 | // Initially, offer an audio/video stream from the caller, but refuse to |
| 1385 | // send/receive video on the callee side. |
| 1386 | caller()->AddAudioVideoMediaStream(); |
| 1387 | callee()->AddMediaStreamFromTracks(callee()->CreateLocalAudioTrack(), |
| 1388 | nullptr); |
| 1389 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 1390 | options.offer_to_receive_video = 0; |
| 1391 | callee()->SetOfferAnswerOptions(options); |
| 1392 | // Do offer/answer and make sure audio is still received end-to-end. |
| 1393 | caller()->CreateAndSetAndSignalOffer(); |
| 1394 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1395 | ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0, |
| 1396 | kDefaultExpectedAudioFrameCount, 0, |
| 1397 | kMaxWaitForFramesMs); |
| 1398 | // Sanity check that the callee's description has a rejected video section. |
| 1399 | ASSERT_NE(nullptr, callee()->pc()->local_description()); |
| 1400 | const ContentInfo* callee_video_content = |
| 1401 | GetFirstVideoContent(callee()->pc()->local_description()->description()); |
| 1402 | ASSERT_NE(nullptr, callee_video_content); |
| 1403 | EXPECT_TRUE(callee_video_content->rejected); |
| 1404 | // Now negotiate with video and ensure negotiation succeeds, with video |
| 1405 | // frames and additional audio frames being received. |
| 1406 | callee()->AddMediaStreamFromTracksWithLabel( |
| 1407 | nullptr, callee()->CreateLocalVideoTrack(), "video_only_stream"); |
| 1408 | options.offer_to_receive_video = 1; |
| 1409 | callee()->SetOfferAnswerOptions(options); |
| 1410 | callee()->CreateAndSetAndSignalOffer(); |
| 1411 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1412 | // Expect additional audio frames to be received after the upgrade. |
| 1413 | ExpectNewFramesReceivedWithWait( |
| 1414 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1415 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1416 | kMaxWaitForFramesMs); |
| 1417 | } |
| 1418 | |
| 1419 | // This test sets up a call that's transferred to a new caller with a different |
| 1420 | // DTLS fingerprint. |
| 1421 | TEST_F(PeerConnectionIntegrationTest, CallTransferredForCallee) { |
| 1422 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1423 | ConnectFakeSignaling(); |
| 1424 | caller()->AddAudioVideoMediaStream(); |
| 1425 | callee()->AddAudioVideoMediaStream(); |
| 1426 | caller()->CreateAndSetAndSignalOffer(); |
| 1427 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1428 | |
| 1429 | // Keep the original peer around which will still send packets to the |
| 1430 | // receiving client. These SRTP packets will be dropped. |
| 1431 | std::unique_ptr<PeerConnectionWrapper> original_peer( |
| 1432 | SetCallerPcWrapperAndReturnCurrent( |
| 1433 | CreatePeerConnectionWrapperWithAlternateKey())); |
| 1434 | // TODO(deadbeef): Why do we call Close here? That goes against the comment |
| 1435 | // directly above. |
| 1436 | original_peer->pc()->Close(); |
| 1437 | |
| 1438 | ConnectFakeSignaling(); |
| 1439 | caller()->AddAudioVideoMediaStream(); |
| 1440 | caller()->CreateAndSetAndSignalOffer(); |
| 1441 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1442 | // Wait for some additional frames to be transmitted end-to-end. |
| 1443 | ExpectNewFramesReceivedWithWait( |
| 1444 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1445 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1446 | kMaxWaitForFramesMs); |
| 1447 | } |
| 1448 | |
| 1449 | // This test sets up a call that's transferred to a new callee with a different |
| 1450 | // DTLS fingerprint. |
| 1451 | TEST_F(PeerConnectionIntegrationTest, CallTransferredForCaller) { |
| 1452 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1453 | ConnectFakeSignaling(); |
| 1454 | caller()->AddAudioVideoMediaStream(); |
| 1455 | callee()->AddAudioVideoMediaStream(); |
| 1456 | caller()->CreateAndSetAndSignalOffer(); |
| 1457 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1458 | |
| 1459 | // Keep the original peer around which will still send packets to the |
| 1460 | // receiving client. These SRTP packets will be dropped. |
| 1461 | std::unique_ptr<PeerConnectionWrapper> original_peer( |
| 1462 | SetCalleePcWrapperAndReturnCurrent( |
| 1463 | CreatePeerConnectionWrapperWithAlternateKey())); |
| 1464 | // TODO(deadbeef): Why do we call Close here? That goes against the comment |
| 1465 | // directly above. |
| 1466 | original_peer->pc()->Close(); |
| 1467 | |
| 1468 | ConnectFakeSignaling(); |
| 1469 | callee()->AddAudioVideoMediaStream(); |
| 1470 | caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions()); |
| 1471 | caller()->CreateAndSetAndSignalOffer(); |
| 1472 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1473 | // Wait for some additional frames to be transmitted end-to-end. |
| 1474 | ExpectNewFramesReceivedWithWait( |
| 1475 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1476 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1477 | kMaxWaitForFramesMs); |
| 1478 | } |
| 1479 | |
| 1480 | // This test sets up a non-bundled call and negotiates bundling at the same |
| 1481 | // time as starting an ICE restart. When bundling is in effect in the restart, |
| 1482 | // the DTLS-SRTP context should be successfully reset. |
| 1483 | TEST_F(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) { |
| 1484 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1485 | ConnectFakeSignaling(); |
| 1486 | |
| 1487 | caller()->AddAudioVideoMediaStream(); |
| 1488 | callee()->AddAudioVideoMediaStream(); |
| 1489 | // Remove the bundle group from the SDP received by the callee. |
| 1490 | callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) { |
| 1491 | desc->RemoveGroupByName("BUNDLE"); |
| 1492 | }); |
| 1493 | caller()->CreateAndSetAndSignalOffer(); |
| 1494 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1495 | ExpectNewFramesReceivedWithWait( |
| 1496 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1497 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1498 | kMaxWaitForFramesMs); |
| 1499 | |
| 1500 | // Now stop removing the BUNDLE group, and trigger an ICE restart. |
| 1501 | callee()->SetReceivedSdpMunger(nullptr); |
| 1502 | caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions()); |
| 1503 | caller()->CreateAndSetAndSignalOffer(); |
| 1504 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1505 | |
| 1506 | // Expect additional frames to be received after the ICE restart. |
| 1507 | ExpectNewFramesReceivedWithWait( |
| 1508 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1509 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1510 | kMaxWaitForFramesMs); |
| 1511 | } |
| 1512 | |
| 1513 | // Test CVO (Coordination of Video Orientation). If a video source is rotated |
| 1514 | // and both peers support the CVO RTP header extension, the actual video frames |
| 1515 | // don't need to be encoded in different resolutions, since the rotation is |
| 1516 | // communicated through the RTP header extension. |
| 1517 | TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) { |
| 1518 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1519 | ConnectFakeSignaling(); |
| 1520 | // Add rotated video tracks. |
| 1521 | caller()->AddMediaStreamFromTracks( |
| 1522 | nullptr, |
| 1523 | caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90)); |
| 1524 | callee()->AddMediaStreamFromTracks( |
| 1525 | nullptr, |
| 1526 | callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270)); |
| 1527 | |
| 1528 | // Wait for video frames to be received by both sides. |
| 1529 | caller()->CreateAndSetAndSignalOffer(); |
| 1530 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1531 | ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 && |
| 1532 | callee()->min_video_frames_received_per_track() > 0, |
| 1533 | kMaxWaitForFramesMs); |
| 1534 | |
| 1535 | // Ensure that the aspect ratio is unmodified. |
| 1536 | // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test, |
| 1537 | // not just assumed. |
| 1538 | EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio()); |
| 1539 | EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio()); |
| 1540 | EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio()); |
| 1541 | EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio()); |
| 1542 | // Ensure that the CVO bits were surfaced to the renderer. |
| 1543 | EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation()); |
| 1544 | EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation()); |
| 1545 | } |
| 1546 | |
| 1547 | // Test that when the CVO extension isn't supported, video is rotated the |
| 1548 | // old-fashioned way, by encoding rotated frames. |
| 1549 | TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) { |
| 1550 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1551 | ConnectFakeSignaling(); |
| 1552 | // Add rotated video tracks. |
| 1553 | caller()->AddMediaStreamFromTracks( |
| 1554 | nullptr, |
| 1555 | caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90)); |
| 1556 | callee()->AddMediaStreamFromTracks( |
| 1557 | nullptr, |
| 1558 | callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270)); |
| 1559 | |
| 1560 | // Remove the CVO extension from the offered SDP. |
| 1561 | callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) { |
| 1562 | cricket::VideoContentDescription* video = |
| 1563 | GetFirstVideoContentDescription(desc); |
| 1564 | video->ClearRtpHeaderExtensions(); |
| 1565 | }); |
| 1566 | // Wait for video frames to be received by both sides. |
| 1567 | caller()->CreateAndSetAndSignalOffer(); |
| 1568 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1569 | ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 && |
| 1570 | callee()->min_video_frames_received_per_track() > 0, |
| 1571 | kMaxWaitForFramesMs); |
| 1572 | |
| 1573 | // Expect that the aspect ratio is inversed to account for the 90/270 degree |
| 1574 | // rotation. |
| 1575 | // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test, |
| 1576 | // not just assumed. |
| 1577 | EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio()); |
| 1578 | EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio()); |
| 1579 | EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio()); |
| 1580 | EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio()); |
| 1581 | // Expect that each endpoint is unaware of the rotation of the other endpoint. |
| 1582 | EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation()); |
| 1583 | EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation()); |
| 1584 | } |
| 1585 | |
| 1586 | // TODO(deadbeef): The tests below rely on RTCOfferAnswerOptions to reject an |
| 1587 | // m= section. When we implement Unified Plan SDP, the right way to do this |
| 1588 | // would be by stopping an RtpTransceiver. |
| 1589 | |
| 1590 | // Test that if the answerer rejects the audio m= section, no audio is sent or |
| 1591 | // received, but video still can be. |
| 1592 | TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) { |
| 1593 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1594 | ConnectFakeSignaling(); |
| 1595 | caller()->AddAudioVideoMediaStream(); |
| 1596 | // Only add video track for callee, and set offer_to_receive_audio to 0, so |
| 1597 | // it will reject the audio m= section completely. |
| 1598 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 1599 | options.offer_to_receive_audio = 0; |
| 1600 | callee()->SetOfferAnswerOptions(options); |
| 1601 | callee()->AddMediaStreamFromTracks(nullptr, |
| 1602 | callee()->CreateLocalVideoTrack()); |
| 1603 | // Do offer/answer and wait for successful end-to-end video frames. |
| 1604 | caller()->CreateAndSetAndSignalOffer(); |
| 1605 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1606 | ExpectNewFramesReceivedWithWait(0, kDefaultExpectedVideoFrameCount, 0, |
| 1607 | kDefaultExpectedVideoFrameCount, |
| 1608 | kMaxWaitForFramesMs); |
| 1609 | // Shouldn't have received audio frames at any point. |
| 1610 | EXPECT_EQ(0, caller()->audio_frames_received()); |
| 1611 | EXPECT_EQ(0, callee()->audio_frames_received()); |
| 1612 | // Sanity check that the callee's description has a rejected audio section. |
| 1613 | ASSERT_NE(nullptr, callee()->pc()->local_description()); |
| 1614 | const ContentInfo* callee_audio_content = |
| 1615 | GetFirstAudioContent(callee()->pc()->local_description()->description()); |
| 1616 | ASSERT_NE(nullptr, callee_audio_content); |
| 1617 | EXPECT_TRUE(callee_audio_content->rejected); |
| 1618 | } |
| 1619 | |
| 1620 | // Test that if the answerer rejects the video m= section, no video is sent or |
| 1621 | // received, but audio still can be. |
| 1622 | TEST_F(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) { |
| 1623 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1624 | ConnectFakeSignaling(); |
| 1625 | caller()->AddAudioVideoMediaStream(); |
| 1626 | // Only add audio track for callee, and set offer_to_receive_video to 0, so |
| 1627 | // it will reject the video m= section completely. |
| 1628 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 1629 | options.offer_to_receive_video = 0; |
| 1630 | callee()->SetOfferAnswerOptions(options); |
| 1631 | callee()->AddMediaStreamFromTracks(callee()->CreateLocalAudioTrack(), |
| 1632 | nullptr); |
| 1633 | // Do offer/answer and wait for successful end-to-end audio frames. |
| 1634 | caller()->CreateAndSetAndSignalOffer(); |
| 1635 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1636 | ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0, |
| 1637 | kDefaultExpectedAudioFrameCount, 0, |
| 1638 | kMaxWaitForFramesMs); |
| 1639 | // Shouldn't have received video frames at any point. |
| 1640 | EXPECT_EQ(0, caller()->total_video_frames_received()); |
| 1641 | EXPECT_EQ(0, callee()->total_video_frames_received()); |
| 1642 | // Sanity check that the callee's description has a rejected video section. |
| 1643 | ASSERT_NE(nullptr, callee()->pc()->local_description()); |
| 1644 | const ContentInfo* callee_video_content = |
| 1645 | GetFirstVideoContent(callee()->pc()->local_description()->description()); |
| 1646 | ASSERT_NE(nullptr, callee_video_content); |
| 1647 | EXPECT_TRUE(callee_video_content->rejected); |
| 1648 | } |
| 1649 | |
| 1650 | // Test that if the answerer rejects both audio and video m= sections, nothing |
| 1651 | // bad happens. |
| 1652 | // TODO(deadbeef): Test that a data channel still works. Currently this doesn't |
| 1653 | // test anything but the fact that negotiation succeeds, which doesn't mean |
| 1654 | // much. |
| 1655 | TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) { |
| 1656 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1657 | ConnectFakeSignaling(); |
| 1658 | caller()->AddAudioVideoMediaStream(); |
| 1659 | // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it |
| 1660 | // will reject both audio and video m= sections. |
| 1661 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 1662 | options.offer_to_receive_audio = 0; |
| 1663 | options.offer_to_receive_video = 0; |
| 1664 | callee()->SetOfferAnswerOptions(options); |
| 1665 | // Do offer/answer and wait for stable signaling state. |
| 1666 | caller()->CreateAndSetAndSignalOffer(); |
| 1667 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1668 | // Sanity check that the callee's description has rejected m= sections. |
| 1669 | ASSERT_NE(nullptr, callee()->pc()->local_description()); |
| 1670 | const ContentInfo* callee_audio_content = |
| 1671 | GetFirstAudioContent(callee()->pc()->local_description()->description()); |
| 1672 | ASSERT_NE(nullptr, callee_audio_content); |
| 1673 | EXPECT_TRUE(callee_audio_content->rejected); |
| 1674 | const ContentInfo* callee_video_content = |
| 1675 | GetFirstVideoContent(callee()->pc()->local_description()->description()); |
| 1676 | ASSERT_NE(nullptr, callee_video_content); |
| 1677 | EXPECT_TRUE(callee_video_content->rejected); |
| 1678 | } |
| 1679 | |
| 1680 | // This test sets up an audio and video call between two parties. After the |
| 1681 | // call runs for a while, the caller sends an updated offer with video being |
| 1682 | // rejected. Once the re-negotiation is done, the video flow should stop and |
| 1683 | // the audio flow should continue. |
| 1684 | TEST_F(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) { |
| 1685 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1686 | ConnectFakeSignaling(); |
| 1687 | caller()->AddAudioVideoMediaStream(); |
| 1688 | callee()->AddAudioVideoMediaStream(); |
| 1689 | caller()->CreateAndSetAndSignalOffer(); |
| 1690 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1691 | ExpectNewFramesReceivedWithWait( |
| 1692 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1693 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1694 | kMaxWaitForFramesMs); |
| 1695 | |
| 1696 | // Renegotiate, rejecting the video m= section. |
| 1697 | // TODO(deadbeef): When an RtpTransceiver API is available, use that to |
| 1698 | // reject the video m= section. |
| 1699 | caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) { |
| 1700 | for (cricket::ContentInfo& content : description->contents()) { |
| 1701 | if (cricket::IsVideoContent(&content)) { |
| 1702 | content.rejected = true; |
| 1703 | } |
| 1704 | } |
| 1705 | }); |
| 1706 | caller()->CreateAndSetAndSignalOffer(); |
| 1707 | ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs); |
| 1708 | |
| 1709 | // Sanity check that the caller's description has a rejected video section. |
| 1710 | ASSERT_NE(nullptr, caller()->pc()->local_description()); |
| 1711 | const ContentInfo* caller_video_content = |
| 1712 | GetFirstVideoContent(caller()->pc()->local_description()->description()); |
| 1713 | ASSERT_NE(nullptr, caller_video_content); |
| 1714 | EXPECT_TRUE(caller_video_content->rejected); |
| 1715 | |
| 1716 | int caller_video_received = caller()->total_video_frames_received(); |
| 1717 | int callee_video_received = callee()->total_video_frames_received(); |
| 1718 | |
| 1719 | // Wait for some additional audio frames to be received. |
| 1720 | ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0, |
| 1721 | kDefaultExpectedAudioFrameCount, 0, |
| 1722 | kMaxWaitForFramesMs); |
| 1723 | |
| 1724 | // During this time, we shouldn't have received any additional video frames |
| 1725 | // for the rejected video tracks. |
| 1726 | EXPECT_EQ(caller_video_received, caller()->total_video_frames_received()); |
| 1727 | EXPECT_EQ(callee_video_received, callee()->total_video_frames_received()); |
| 1728 | } |
| 1729 | |
| 1730 | // Basic end-to-end test, but without SSRC/MSID signaling. This functionality |
| 1731 | // is needed to support legacy endpoints. |
| 1732 | // TODO(deadbeef): When we support the MID extension and demuxing on MID, also |
| 1733 | // add a test for an end-to-end test without MID signaling either (basically, |
| 1734 | // the minimum acceptable SDP). |
| 1735 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) { |
| 1736 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1737 | ConnectFakeSignaling(); |
| 1738 | // Add audio and video, testing that packets can be demuxed on payload type. |
| 1739 | caller()->AddAudioVideoMediaStream(); |
| 1740 | callee()->AddAudioVideoMediaStream(); |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1741 | // Remove SSRCs and MSIDs from the received offer SDP. |
| 1742 | callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1743 | caller()->CreateAndSetAndSignalOffer(); |
| 1744 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1745 | ExpectNewFramesReceivedWithWait( |
| 1746 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1747 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1748 | kMaxWaitForFramesMs); |
| 1749 | } |
| 1750 | |
| 1751 | // Test that if two video tracks are sent (from caller to callee, in this test), |
| 1752 | // they're transmitted correctly end-to-end. |
| 1753 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) { |
| 1754 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1755 | ConnectFakeSignaling(); |
| 1756 | // Add one audio/video stream, and one video-only stream. |
| 1757 | caller()->AddAudioVideoMediaStream(); |
| 1758 | caller()->AddMediaStreamFromTracksWithLabel( |
| 1759 | nullptr, caller()->CreateLocalVideoTrackWithId("extra_track"), |
| 1760 | "extra_stream"); |
| 1761 | caller()->CreateAndSetAndSignalOffer(); |
| 1762 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1763 | ASSERT_EQ(2u, callee()->number_of_remote_streams()); |
| 1764 | int expected_callee_received_frames = kDefaultExpectedVideoFrameCount; |
| 1765 | ExpectNewFramesReceivedWithWait(0, 0, 0, expected_callee_received_frames, |
| 1766 | kMaxWaitForFramesMs); |
| 1767 | } |
| 1768 | |
| 1769 | static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) { |
| 1770 | bool first = true; |
| 1771 | for (cricket::ContentInfo& content : desc->contents()) { |
| 1772 | if (first) { |
| 1773 | first = false; |
| 1774 | continue; |
| 1775 | } |
| 1776 | content.bundle_only = true; |
| 1777 | } |
| 1778 | first = true; |
| 1779 | for (cricket::TransportInfo& transport : desc->transport_infos()) { |
| 1780 | if (first) { |
| 1781 | first = false; |
| 1782 | continue; |
| 1783 | } |
| 1784 | transport.description.ice_ufrag.clear(); |
| 1785 | transport.description.ice_pwd.clear(); |
| 1786 | transport.description.connection_role = cricket::CONNECTIONROLE_NONE; |
| 1787 | transport.description.identity_fingerprint.reset(nullptr); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | // Test that if applying a true "max bundle" offer, which uses ports of 0, |
| 1792 | // "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and |
| 1793 | // "a=ice-pwd" for all but the audio "m=" section, negotiation still completes |
| 1794 | // successfully and media flows. |
| 1795 | // TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works. |
| 1796 | // TODO(deadbeef): Won't need this test once we start generating actual |
| 1797 | // standards-compliant SDP. |
| 1798 | TEST_F(PeerConnectionIntegrationTest, |
| 1799 | EndToEndCallWithSpecCompliantMaxBundleOffer) { |
| 1800 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1801 | ConnectFakeSignaling(); |
| 1802 | caller()->AddAudioVideoMediaStream(); |
| 1803 | callee()->AddAudioVideoMediaStream(); |
| 1804 | // Do the equivalent of setting the port to 0, adding a=bundle-only, and |
| 1805 | // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all |
| 1806 | // but the first m= section. |
| 1807 | callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer); |
| 1808 | caller()->CreateAndSetAndSignalOffer(); |
| 1809 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1810 | ExpectNewFramesReceivedWithWait( |
| 1811 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1812 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1813 | kMaxWaitForFramesMs); |
| 1814 | } |
| 1815 | |
| 1816 | // Test that we can receive the audio output level from a remote audio track. |
| 1817 | // TODO(deadbeef): Use a fake audio source and verify that the output level is |
| 1818 | // exactly what the source on the other side was configured with. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1819 | TEST_F(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) { |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1820 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1821 | ConnectFakeSignaling(); |
| 1822 | // Just add an audio track. |
| 1823 | caller()->AddMediaStreamFromTracks(caller()->CreateLocalAudioTrack(), |
| 1824 | nullptr); |
| 1825 | caller()->CreateAndSetAndSignalOffer(); |
| 1826 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1827 | |
| 1828 | // Get the audio output level stats. Note that the level is not available |
| 1829 | // until an RTCP packet has been received. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1830 | EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0, |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1831 | kMaxWaitForFramesMs); |
| 1832 | } |
| 1833 | |
| 1834 | // Test that an audio input level is reported. |
| 1835 | // TODO(deadbeef): Use a fake audio source and verify that the input level is |
| 1836 | // exactly what the source was configured with. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1837 | TEST_F(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) { |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1838 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1839 | ConnectFakeSignaling(); |
| 1840 | // Just add an audio track. |
| 1841 | caller()->AddMediaStreamFromTracks(caller()->CreateLocalAudioTrack(), |
| 1842 | nullptr); |
| 1843 | caller()->CreateAndSetAndSignalOffer(); |
| 1844 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1845 | |
| 1846 | // Get the audio input level stats. The level should be available very |
| 1847 | // soon after the test starts. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1848 | EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0, |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1849 | kMaxWaitForStatsMs); |
| 1850 | } |
| 1851 | |
| 1852 | // Test that we can get incoming byte counts from both audio and video tracks. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1853 | TEST_F(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) { |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1854 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1855 | ConnectFakeSignaling(); |
| 1856 | caller()->AddAudioVideoMediaStream(); |
| 1857 | // Do offer/answer, wait for the callee to receive some frames. |
| 1858 | caller()->CreateAndSetAndSignalOffer(); |
| 1859 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1860 | int expected_caller_received_frames = 0; |
| 1861 | ExpectNewFramesReceivedWithWait( |
| 1862 | expected_caller_received_frames, expected_caller_received_frames, |
| 1863 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1864 | kMaxWaitForFramesMs); |
| 1865 | |
| 1866 | // Get a handle to the remote tracks created, so they can be used as GetStats |
| 1867 | // filters. |
| 1868 | StreamCollectionInterface* remote_streams = callee()->remote_streams(); |
| 1869 | ASSERT_EQ(1u, remote_streams->count()); |
| 1870 | ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size()); |
| 1871 | ASSERT_EQ(1u, remote_streams->at(0)->GetVideoTracks().size()); |
| 1872 | MediaStreamTrackInterface* remote_audio_track = |
| 1873 | remote_streams->at(0)->GetAudioTracks()[0]; |
| 1874 | MediaStreamTrackInterface* remote_video_track = |
| 1875 | remote_streams->at(0)->GetVideoTracks()[0]; |
| 1876 | |
| 1877 | // We received frames, so we definitely should have nonzero "received bytes" |
| 1878 | // stats at this point. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1879 | EXPECT_GT(callee()->OldGetStatsForTrack(remote_audio_track)->BytesReceived(), |
| 1880 | 0); |
| 1881 | EXPECT_GT(callee()->OldGetStatsForTrack(remote_video_track)->BytesReceived(), |
| 1882 | 0); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | // Test that we can get outgoing byte counts from both audio and video tracks. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1886 | TEST_F(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) { |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1887 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1888 | ConnectFakeSignaling(); |
| 1889 | auto audio_track = caller()->CreateLocalAudioTrack(); |
| 1890 | auto video_track = caller()->CreateLocalVideoTrack(); |
| 1891 | caller()->AddMediaStreamFromTracks(audio_track, video_track); |
| 1892 | // Do offer/answer, wait for the callee to receive some frames. |
| 1893 | caller()->CreateAndSetAndSignalOffer(); |
| 1894 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1895 | int expected_caller_received_frames = 0; |
| 1896 | ExpectNewFramesReceivedWithWait( |
| 1897 | expected_caller_received_frames, expected_caller_received_frames, |
| 1898 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1899 | kMaxWaitForFramesMs); |
| 1900 | |
| 1901 | // The callee received frames, so we definitely should have nonzero "sent |
| 1902 | // bytes" stats at this point. |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1903 | EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0); |
| 1904 | EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0); |
| 1905 | } |
| 1906 | |
| 1907 | // Test that we can get stats (using the new stats implemnetation) for |
| 1908 | // unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in |
| 1909 | // SDP. |
| 1910 | TEST_F(PeerConnectionIntegrationTest, |
| 1911 | GetStatsForUnsignaledStreamWithNewStatsApi) { |
| 1912 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 1913 | ConnectFakeSignaling(); |
| 1914 | caller()->AddAudioOnlyMediaStream(); |
| 1915 | // Remove SSRCs and MSIDs from the received offer SDP. |
| 1916 | callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids); |
| 1917 | caller()->CreateAndSetAndSignalOffer(); |
| 1918 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1919 | // Wait for one audio frame to be received by the callee. |
| 1920 | ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs); |
| 1921 | |
| 1922 | // We received a frame, so we should have nonzero "bytes received" stats for |
| 1923 | // the unsignaled stream, if stats are working for it. |
| 1924 | rtc::scoped_refptr<const webrtc::RTCStatsReport> report = |
| 1925 | callee()->NewGetStats(); |
| 1926 | ASSERT_NE(nullptr, report); |
| 1927 | auto inbound_stream_stats = |
| 1928 | report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>(); |
| 1929 | ASSERT_EQ(1U, inbound_stream_stats.size()); |
| 1930 | ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined()); |
| 1931 | ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U); |
| 1932 | // TODO(deadbeef): Test that track_id is defined. This is not currently |
| 1933 | // working since SSRCs are used to match RtpReceivers (and their tracks) with |
| 1934 | // received stream stats in TrackMediaInfoMap. |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | // Test that DTLS 1.0 is used if both sides only support DTLS 1.0. |
| 1938 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) { |
| 1939 | PeerConnectionFactory::Options dtls_10_options; |
| 1940 | dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10; |
| 1941 | ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options, |
| 1942 | dtls_10_options)); |
| 1943 | ConnectFakeSignaling(); |
| 1944 | // Do normal offer/answer and wait for some frames to be received in each |
| 1945 | // direction. |
| 1946 | caller()->AddAudioVideoMediaStream(); |
| 1947 | callee()->AddAudioVideoMediaStream(); |
| 1948 | caller()->CreateAndSetAndSignalOffer(); |
| 1949 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1950 | ExpectNewFramesReceivedWithWait( |
| 1951 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1952 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 1953 | kMaxWaitForFramesMs); |
| 1954 | } |
| 1955 | |
| 1956 | // Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated. |
| 1957 | TEST_F(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) { |
| 1958 | PeerConnectionFactory::Options dtls_10_options; |
| 1959 | dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10; |
| 1960 | ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options, |
| 1961 | dtls_10_options)); |
| 1962 | ConnectFakeSignaling(); |
| 1963 | // Register UMA observer before signaling begins. |
| 1964 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer = |
| 1965 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 1966 | caller()->pc()->RegisterUMAObserver(caller_observer); |
| 1967 | caller()->AddAudioVideoMediaStream(); |
| 1968 | callee()->AddAudioVideoMediaStream(); |
| 1969 | caller()->CreateAndSetAndSignalOffer(); |
| 1970 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1971 | EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher( |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1972 | caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT), |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1973 | kDefaultTimeout); |
| 1974 | EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite), |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1975 | caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1976 | EXPECT_EQ(1, |
| 1977 | caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher, |
| 1978 | kDefaultSrtpCryptoSuite)); |
| 1979 | } |
| 1980 | |
| 1981 | // Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated. |
| 1982 | TEST_F(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) { |
| 1983 | PeerConnectionFactory::Options dtls_12_options; |
| 1984 | dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12; |
| 1985 | ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options, |
| 1986 | dtls_12_options)); |
| 1987 | ConnectFakeSignaling(); |
| 1988 | // Register UMA observer before signaling begins. |
| 1989 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer = |
| 1990 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 1991 | caller()->pc()->RegisterUMAObserver(caller_observer); |
| 1992 | caller()->AddAudioVideoMediaStream(); |
| 1993 | callee()->AddAudioVideoMediaStream(); |
| 1994 | caller()->CreateAndSetAndSignalOffer(); |
| 1995 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 1996 | EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher( |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 1997 | caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT), |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 1998 | kDefaultTimeout); |
| 1999 | EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite), |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 2000 | caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2001 | EXPECT_EQ(1, |
| 2002 | caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher, |
| 2003 | kDefaultSrtpCryptoSuite)); |
| 2004 | } |
| 2005 | |
| 2006 | // Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the |
| 2007 | // callee only supports 1.0. |
| 2008 | TEST_F(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) { |
| 2009 | PeerConnectionFactory::Options caller_options; |
| 2010 | caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12; |
| 2011 | PeerConnectionFactory::Options callee_options; |
| 2012 | callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10; |
| 2013 | ASSERT_TRUE( |
| 2014 | CreatePeerConnectionWrappersWithOptions(caller_options, callee_options)); |
| 2015 | ConnectFakeSignaling(); |
| 2016 | // Do normal offer/answer and wait for some frames to be received in each |
| 2017 | // direction. |
| 2018 | caller()->AddAudioVideoMediaStream(); |
| 2019 | callee()->AddAudioVideoMediaStream(); |
| 2020 | caller()->CreateAndSetAndSignalOffer(); |
| 2021 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2022 | ExpectNewFramesReceivedWithWait( |
| 2023 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2024 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2025 | kMaxWaitForFramesMs); |
| 2026 | } |
| 2027 | |
| 2028 | // Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the |
| 2029 | // callee supports 1.2. |
| 2030 | TEST_F(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) { |
| 2031 | PeerConnectionFactory::Options caller_options; |
| 2032 | caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10; |
| 2033 | PeerConnectionFactory::Options callee_options; |
| 2034 | callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12; |
| 2035 | ASSERT_TRUE( |
| 2036 | CreatePeerConnectionWrappersWithOptions(caller_options, callee_options)); |
| 2037 | ConnectFakeSignaling(); |
| 2038 | // Do normal offer/answer and wait for some frames to be received in each |
| 2039 | // direction. |
| 2040 | caller()->AddAudioVideoMediaStream(); |
| 2041 | callee()->AddAudioVideoMediaStream(); |
| 2042 | caller()->CreateAndSetAndSignalOffer(); |
| 2043 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2044 | ExpectNewFramesReceivedWithWait( |
| 2045 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2046 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2047 | kMaxWaitForFramesMs); |
| 2048 | } |
| 2049 | |
| 2050 | // Test that a non-GCM cipher is used if both sides only support non-GCM. |
| 2051 | TEST_F(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) { |
| 2052 | bool local_gcm_enabled = false; |
| 2053 | bool remote_gcm_enabled = false; |
| 2054 | int expected_cipher_suite = kDefaultSrtpCryptoSuite; |
| 2055 | TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled, |
| 2056 | expected_cipher_suite); |
| 2057 | } |
| 2058 | |
| 2059 | // Test that a GCM cipher is used if both ends support it. |
| 2060 | TEST_F(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) { |
| 2061 | bool local_gcm_enabled = true; |
| 2062 | bool remote_gcm_enabled = true; |
| 2063 | int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm; |
| 2064 | TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled, |
| 2065 | expected_cipher_suite); |
| 2066 | } |
| 2067 | |
| 2068 | // Test that GCM isn't used if only the offerer supports it. |
| 2069 | TEST_F(PeerConnectionIntegrationTest, |
| 2070 | NonGcmCipherUsedWhenOnlyCallerSupportsGcm) { |
| 2071 | bool local_gcm_enabled = true; |
| 2072 | bool remote_gcm_enabled = false; |
| 2073 | int expected_cipher_suite = kDefaultSrtpCryptoSuite; |
| 2074 | TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled, |
| 2075 | expected_cipher_suite); |
| 2076 | } |
| 2077 | |
| 2078 | // Test that GCM isn't used if only the answerer supports it. |
| 2079 | TEST_F(PeerConnectionIntegrationTest, |
| 2080 | NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) { |
| 2081 | bool local_gcm_enabled = false; |
| 2082 | bool remote_gcm_enabled = true; |
| 2083 | int expected_cipher_suite = kDefaultSrtpCryptoSuite; |
| 2084 | TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled, |
| 2085 | expected_cipher_suite); |
| 2086 | } |
| 2087 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 2088 | // Verify that media can be transmitted end-to-end when GCM crypto suites are |
| 2089 | // enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported, |
| 2090 | // only verify that a GCM cipher is negotiated, and not necessarily that SRTP |
| 2091 | // works with it. |
| 2092 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) { |
| 2093 | PeerConnectionFactory::Options gcm_options; |
| 2094 | gcm_options.crypto_options.enable_gcm_crypto_suites = true; |
| 2095 | ASSERT_TRUE( |
| 2096 | CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options)); |
| 2097 | ConnectFakeSignaling(); |
| 2098 | // Do normal offer/answer and wait for some frames to be received in each |
| 2099 | // direction. |
| 2100 | caller()->AddAudioVideoMediaStream(); |
| 2101 | callee()->AddAudioVideoMediaStream(); |
| 2102 | caller()->CreateAndSetAndSignalOffer(); |
| 2103 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2104 | ExpectNewFramesReceivedWithWait( |
| 2105 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2106 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2107 | kMaxWaitForFramesMs); |
| 2108 | } |
| 2109 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2110 | // This test sets up a call between two parties with audio, video and an RTP |
| 2111 | // data channel. |
| 2112 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) { |
| 2113 | FakeConstraints setup_constraints; |
| 2114 | setup_constraints.SetAllowRtpDataChannels(); |
| 2115 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints, |
| 2116 | &setup_constraints)); |
| 2117 | ConnectFakeSignaling(); |
| 2118 | // Expect that data channel created on caller side will show up for callee as |
| 2119 | // well. |
| 2120 | caller()->CreateDataChannel(); |
| 2121 | caller()->AddAudioVideoMediaStream(); |
| 2122 | callee()->AddAudioVideoMediaStream(); |
| 2123 | caller()->CreateAndSetAndSignalOffer(); |
| 2124 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2125 | // Ensure the existence of the RTP data channel didn't impede audio/video. |
| 2126 | ExpectNewFramesReceivedWithWait( |
| 2127 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2128 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2129 | kMaxWaitForFramesMs); |
| 2130 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2131 | ASSERT_NE(nullptr, callee()->data_channel()); |
| 2132 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2133 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2134 | |
| 2135 | // Ensure data can be sent in both directions. |
| 2136 | std::string data = "hello world"; |
| 2137 | SendRtpDataWithRetries(caller()->data_channel(), data, 5); |
| 2138 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 2139 | kDefaultTimeout); |
| 2140 | SendRtpDataWithRetries(callee()->data_channel(), data, 5); |
| 2141 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 2142 | kDefaultTimeout); |
| 2143 | } |
| 2144 | |
| 2145 | // Ensure that an RTP data channel is signaled as closed for the caller when |
| 2146 | // the callee rejects it in a subsequent offer. |
| 2147 | TEST_F(PeerConnectionIntegrationTest, |
| 2148 | RtpDataChannelSignaledClosedInCalleeOffer) { |
| 2149 | // Same procedure as above test. |
| 2150 | FakeConstraints setup_constraints; |
| 2151 | setup_constraints.SetAllowRtpDataChannels(); |
| 2152 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints, |
| 2153 | &setup_constraints)); |
| 2154 | ConnectFakeSignaling(); |
| 2155 | caller()->CreateDataChannel(); |
| 2156 | caller()->AddAudioVideoMediaStream(); |
| 2157 | callee()->AddAudioVideoMediaStream(); |
| 2158 | caller()->CreateAndSetAndSignalOffer(); |
| 2159 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2160 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2161 | ASSERT_NE(nullptr, callee()->data_channel()); |
| 2162 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2163 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2164 | |
| 2165 | // Close the data channel on the callee, and do an updated offer/answer. |
| 2166 | callee()->data_channel()->Close(); |
| 2167 | callee()->CreateAndSetAndSignalOffer(); |
| 2168 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2169 | EXPECT_FALSE(caller()->data_observer()->IsOpen()); |
| 2170 | EXPECT_FALSE(callee()->data_observer()->IsOpen()); |
| 2171 | } |
| 2172 | |
| 2173 | // Tests that data is buffered in an RTP data channel until an observer is |
| 2174 | // registered for it. |
| 2175 | // |
| 2176 | // NOTE: RTP data channels can receive data before the underlying |
| 2177 | // transport has detected that a channel is writable and thus data can be |
| 2178 | // received before the data channel state changes to open. That is hard to test |
| 2179 | // but the same buffering is expected to be used in that case. |
| 2180 | TEST_F(PeerConnectionIntegrationTest, |
| 2181 | DataBufferedUntilRtpDataChannelObserverRegistered) { |
| 2182 | // Use fake clock and simulated network delay so that we predictably can wait |
| 2183 | // until an SCTP message has been delivered without "sleep()"ing. |
| 2184 | rtc::ScopedFakeClock fake_clock; |
| 2185 | // Some things use a time of "0" as a special value, so we need to start out |
| 2186 | // the fake clock at a nonzero time. |
| 2187 | // TODO(deadbeef): Fix this. |
| 2188 | fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 2189 | virtual_socket_server()->set_delay_mean(5); // 5 ms per hop. |
| 2190 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2191 | |
| 2192 | FakeConstraints constraints; |
| 2193 | constraints.SetAllowRtpDataChannels(); |
| 2194 | ASSERT_TRUE( |
| 2195 | CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints)); |
| 2196 | ConnectFakeSignaling(); |
| 2197 | caller()->CreateDataChannel(); |
| 2198 | caller()->CreateAndSetAndSignalOffer(); |
| 2199 | ASSERT_TRUE(caller()->data_channel() != nullptr); |
| 2200 | ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr, |
| 2201 | kDefaultTimeout, fake_clock); |
| 2202 | ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(), |
| 2203 | kDefaultTimeout, fake_clock); |
| 2204 | ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen, |
| 2205 | callee()->data_channel()->state(), kDefaultTimeout, |
| 2206 | fake_clock); |
| 2207 | |
| 2208 | // Unregister the observer which is normally automatically registered. |
| 2209 | callee()->data_channel()->UnregisterObserver(); |
| 2210 | // Send data and advance fake clock until it should have been received. |
| 2211 | std::string data = "hello world"; |
| 2212 | caller()->data_channel()->Send(DataBuffer(data)); |
| 2213 | SIMULATED_WAIT(false, 50, fake_clock); |
| 2214 | |
| 2215 | // Attach data channel and expect data to be received immediately. Note that |
| 2216 | // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any |
| 2217 | // further, but data can be received even if the callback is asynchronous. |
| 2218 | MockDataChannelObserver new_observer(callee()->data_channel()); |
| 2219 | EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout, |
| 2220 | fake_clock); |
| 2221 | } |
| 2222 | |
| 2223 | // This test sets up a call between two parties with audio, video and but only |
| 2224 | // the caller client supports RTP data channels. |
| 2225 | TEST_F(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) { |
| 2226 | FakeConstraints setup_constraints_1; |
| 2227 | setup_constraints_1.SetAllowRtpDataChannels(); |
| 2228 | // Must disable DTLS to make negotiation succeed. |
| 2229 | setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, |
| 2230 | false); |
| 2231 | FakeConstraints setup_constraints_2; |
| 2232 | setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, |
| 2233 | false); |
| 2234 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints( |
| 2235 | &setup_constraints_1, &setup_constraints_2)); |
| 2236 | ConnectFakeSignaling(); |
| 2237 | caller()->CreateDataChannel(); |
| 2238 | caller()->AddAudioVideoMediaStream(); |
| 2239 | callee()->AddAudioVideoMediaStream(); |
| 2240 | caller()->CreateAndSetAndSignalOffer(); |
| 2241 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2242 | // The caller should still have a data channel, but it should be closed, and |
| 2243 | // one should ever have been created for the callee. |
| 2244 | EXPECT_TRUE(caller()->data_channel() != nullptr); |
| 2245 | EXPECT_FALSE(caller()->data_observer()->IsOpen()); |
| 2246 | EXPECT_EQ(nullptr, callee()->data_channel()); |
| 2247 | } |
| 2248 | |
| 2249 | // This test sets up a call between two parties with audio, and video. When |
| 2250 | // audio and video is setup and flowing, an RTP data channel is negotiated. |
| 2251 | TEST_F(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) { |
| 2252 | FakeConstraints setup_constraints; |
| 2253 | setup_constraints.SetAllowRtpDataChannels(); |
| 2254 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints, |
| 2255 | &setup_constraints)); |
| 2256 | ConnectFakeSignaling(); |
| 2257 | // Do initial offer/answer with audio/video. |
| 2258 | caller()->AddAudioVideoMediaStream(); |
| 2259 | callee()->AddAudioVideoMediaStream(); |
| 2260 | caller()->CreateAndSetAndSignalOffer(); |
| 2261 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2262 | // Create data channel and do new offer and answer. |
| 2263 | caller()->CreateDataChannel(); |
| 2264 | caller()->CreateAndSetAndSignalOffer(); |
| 2265 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2266 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2267 | ASSERT_NE(nullptr, callee()->data_channel()); |
| 2268 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2269 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2270 | // Ensure data can be sent in both directions. |
| 2271 | std::string data = "hello world"; |
| 2272 | SendRtpDataWithRetries(caller()->data_channel(), data, 5); |
| 2273 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 2274 | kDefaultTimeout); |
| 2275 | SendRtpDataWithRetries(callee()->data_channel(), data, 5); |
| 2276 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 2277 | kDefaultTimeout); |
| 2278 | } |
| 2279 | |
| 2280 | #ifdef HAVE_SCTP |
| 2281 | |
| 2282 | // This test sets up a call between two parties with audio, video and an SCTP |
| 2283 | // data channel. |
| 2284 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) { |
| 2285 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2286 | ConnectFakeSignaling(); |
| 2287 | // Expect that data channel created on caller side will show up for callee as |
| 2288 | // well. |
| 2289 | caller()->CreateDataChannel(); |
| 2290 | caller()->AddAudioVideoMediaStream(); |
| 2291 | callee()->AddAudioVideoMediaStream(); |
| 2292 | caller()->CreateAndSetAndSignalOffer(); |
| 2293 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2294 | // Ensure the existence of the SCTP data channel didn't impede audio/video. |
| 2295 | ExpectNewFramesReceivedWithWait( |
| 2296 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2297 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2298 | kMaxWaitForFramesMs); |
| 2299 | // Caller data channel should already exist (it created one). Callee data |
| 2300 | // channel may not exist yet, since negotiation happens in-band, not in SDP. |
| 2301 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2302 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 2303 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2304 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2305 | |
| 2306 | // Ensure data can be sent in both directions. |
| 2307 | std::string data = "hello world"; |
| 2308 | caller()->data_channel()->Send(DataBuffer(data)); |
| 2309 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 2310 | kDefaultTimeout); |
| 2311 | callee()->data_channel()->Send(DataBuffer(data)); |
| 2312 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 2313 | kDefaultTimeout); |
| 2314 | } |
| 2315 | |
| 2316 | // Ensure that when the callee closes an SCTP data channel, the closing |
| 2317 | // procedure results in the data channel being closed for the caller as well. |
| 2318 | TEST_F(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) { |
| 2319 | // Same procedure as above test. |
| 2320 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2321 | ConnectFakeSignaling(); |
| 2322 | caller()->CreateDataChannel(); |
| 2323 | caller()->AddAudioVideoMediaStream(); |
| 2324 | callee()->AddAudioVideoMediaStream(); |
| 2325 | caller()->CreateAndSetAndSignalOffer(); |
| 2326 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2327 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2328 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 2329 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2330 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2331 | |
| 2332 | // Close the data channel on the callee side, and wait for it to reach the |
| 2333 | // "closed" state on both sides. |
| 2334 | callee()->data_channel()->Close(); |
| 2335 | EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2336 | EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2337 | } |
| 2338 | |
| 2339 | // Test usrsctp's ability to process unordered data stream, where data actually |
| 2340 | // arrives out of order using simulated delays. Previously there have been some |
| 2341 | // bugs in this area. |
| 2342 | TEST_F(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) { |
| 2343 | // Introduce random network delays. |
| 2344 | // Otherwise it's not a true "unordered" test. |
| 2345 | virtual_socket_server()->set_delay_mean(20); |
| 2346 | virtual_socket_server()->set_delay_stddev(5); |
| 2347 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2348 | // Normal procedure, but with unordered data channel config. |
| 2349 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2350 | ConnectFakeSignaling(); |
| 2351 | webrtc::DataChannelInit init; |
| 2352 | init.ordered = false; |
| 2353 | caller()->CreateDataChannel(&init); |
| 2354 | caller()->CreateAndSetAndSignalOffer(); |
| 2355 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2356 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2357 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 2358 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2359 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2360 | |
| 2361 | static constexpr int kNumMessages = 100; |
| 2362 | // Deliberately chosen to be larger than the MTU so messages get fragmented. |
| 2363 | static constexpr size_t kMaxMessageSize = 4096; |
| 2364 | // Create and send random messages. |
| 2365 | std::vector<std::string> sent_messages; |
| 2366 | for (int i = 0; i < kNumMessages; ++i) { |
| 2367 | size_t length = |
| 2368 | (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand) |
| 2369 | std::string message; |
| 2370 | ASSERT_TRUE(rtc::CreateRandomString(length, &message)); |
| 2371 | caller()->data_channel()->Send(DataBuffer(message)); |
| 2372 | callee()->data_channel()->Send(DataBuffer(message)); |
| 2373 | sent_messages.push_back(message); |
| 2374 | } |
| 2375 | |
| 2376 | // Wait for all messages to be received. |
| 2377 | EXPECT_EQ_WAIT(kNumMessages, |
| 2378 | caller()->data_observer()->received_message_count(), |
| 2379 | kDefaultTimeout); |
| 2380 | EXPECT_EQ_WAIT(kNumMessages, |
| 2381 | callee()->data_observer()->received_message_count(), |
| 2382 | kDefaultTimeout); |
| 2383 | |
| 2384 | // Sort and compare to make sure none of the messages were corrupted. |
| 2385 | std::vector<std::string> caller_received_messages = |
| 2386 | caller()->data_observer()->messages(); |
| 2387 | std::vector<std::string> callee_received_messages = |
| 2388 | callee()->data_observer()->messages(); |
| 2389 | std::sort(sent_messages.begin(), sent_messages.end()); |
| 2390 | std::sort(caller_received_messages.begin(), caller_received_messages.end()); |
| 2391 | std::sort(callee_received_messages.begin(), callee_received_messages.end()); |
| 2392 | EXPECT_EQ(sent_messages, caller_received_messages); |
| 2393 | EXPECT_EQ(sent_messages, callee_received_messages); |
| 2394 | } |
| 2395 | |
| 2396 | // This test sets up a call between two parties with audio, and video. When |
| 2397 | // audio and video are setup and flowing, an SCTP data channel is negotiated. |
| 2398 | TEST_F(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) { |
| 2399 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2400 | ConnectFakeSignaling(); |
| 2401 | // Do initial offer/answer with audio/video. |
| 2402 | caller()->AddAudioVideoMediaStream(); |
| 2403 | callee()->AddAudioVideoMediaStream(); |
| 2404 | caller()->CreateAndSetAndSignalOffer(); |
| 2405 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2406 | // Create data channel and do new offer and answer. |
| 2407 | caller()->CreateDataChannel(); |
| 2408 | caller()->CreateAndSetAndSignalOffer(); |
| 2409 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2410 | // Caller data channel should already exist (it created one). Callee data |
| 2411 | // channel may not exist yet, since negotiation happens in-band, not in SDP. |
| 2412 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 2413 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 2414 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2415 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2416 | // Ensure data can be sent in both directions. |
| 2417 | std::string data = "hello world"; |
| 2418 | caller()->data_channel()->Send(DataBuffer(data)); |
| 2419 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 2420 | kDefaultTimeout); |
| 2421 | callee()->data_channel()->Send(DataBuffer(data)); |
| 2422 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 2423 | kDefaultTimeout); |
| 2424 | } |
| 2425 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 2426 | // Set up a connection initially just using SCTP data channels, later upgrading |
| 2427 | // to audio/video, ensuring frames are received end-to-end. Effectively the |
| 2428 | // inverse of the test above. |
| 2429 | // This was broken in M57; see https://crbug.com/711243 |
| 2430 | TEST_F(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) { |
| 2431 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2432 | ConnectFakeSignaling(); |
| 2433 | // Do initial offer/answer with just data channel. |
| 2434 | caller()->CreateDataChannel(); |
| 2435 | caller()->CreateAndSetAndSignalOffer(); |
| 2436 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2437 | // Wait until data can be sent over the data channel. |
| 2438 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 2439 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2440 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 2441 | |
| 2442 | // Do subsequent offer/answer with two-way audio and video. Audio and video |
| 2443 | // should end up bundled on the DTLS/ICE transport already used for data. |
| 2444 | caller()->AddAudioVideoMediaStream(); |
| 2445 | callee()->AddAudioVideoMediaStream(); |
| 2446 | caller()->CreateAndSetAndSignalOffer(); |
| 2447 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2448 | ExpectNewFramesReceivedWithWait( |
| 2449 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2450 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2451 | kMaxWaitForFramesMs); |
| 2452 | } |
| 2453 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2454 | #endif // HAVE_SCTP |
| 2455 | |
| 2456 | // Test that the ICE connection and gathering states eventually reach |
| 2457 | // "complete". |
| 2458 | TEST_F(PeerConnectionIntegrationTest, IceStatesReachCompletion) { |
| 2459 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2460 | ConnectFakeSignaling(); |
| 2461 | // Do normal offer/answer. |
| 2462 | caller()->AddAudioVideoMediaStream(); |
| 2463 | callee()->AddAudioVideoMediaStream(); |
| 2464 | caller()->CreateAndSetAndSignalOffer(); |
| 2465 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2466 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete, |
| 2467 | caller()->ice_gathering_state(), kMaxWaitForFramesMs); |
| 2468 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete, |
| 2469 | callee()->ice_gathering_state(), kMaxWaitForFramesMs); |
| 2470 | // After the best candidate pair is selected and all candidates are signaled, |
| 2471 | // the ICE connection state should reach "complete". |
| 2472 | // TODO(deadbeef): Currently, the ICE "controlled" agent (the |
| 2473 | // answerer/"callee" by default) only reaches "connected". When this is |
| 2474 | // fixed, this test should be updated. |
| 2475 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted, |
| 2476 | caller()->ice_connection_state(), kDefaultTimeout); |
| 2477 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected, |
| 2478 | callee()->ice_connection_state(), kDefaultTimeout); |
| 2479 | } |
| 2480 | |
| 2481 | // This test sets up a call between two parties with audio and video. |
| 2482 | // During the call, the caller restarts ICE and the test verifies that |
| 2483 | // new ICE candidates are generated and audio and video still can flow, and the |
| 2484 | // ICE state reaches completed again. |
| 2485 | TEST_F(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) { |
| 2486 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2487 | ConnectFakeSignaling(); |
| 2488 | // Do normal offer/answer and wait for ICE to complete. |
| 2489 | caller()->AddAudioVideoMediaStream(); |
| 2490 | callee()->AddAudioVideoMediaStream(); |
| 2491 | caller()->CreateAndSetAndSignalOffer(); |
| 2492 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2493 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted, |
| 2494 | caller()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2495 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected, |
| 2496 | callee()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2497 | |
| 2498 | // To verify that the ICE restart actually occurs, get |
| 2499 | // ufrag/password/candidates before and after restart. |
| 2500 | // Create an SDP string of the first audio candidate for both clients. |
| 2501 | const webrtc::IceCandidateCollection* audio_candidates_caller = |
| 2502 | caller()->pc()->local_description()->candidates(0); |
| 2503 | const webrtc::IceCandidateCollection* audio_candidates_callee = |
| 2504 | callee()->pc()->local_description()->candidates(0); |
| 2505 | ASSERT_GT(audio_candidates_caller->count(), 0u); |
| 2506 | ASSERT_GT(audio_candidates_callee->count(), 0u); |
| 2507 | std::string caller_candidate_pre_restart; |
| 2508 | ASSERT_TRUE( |
| 2509 | audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart)); |
| 2510 | std::string callee_candidate_pre_restart; |
| 2511 | ASSERT_TRUE( |
| 2512 | audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart)); |
| 2513 | const cricket::SessionDescription* desc = |
| 2514 | caller()->pc()->local_description()->description(); |
| 2515 | std::string caller_ufrag_pre_restart = |
| 2516 | desc->transport_infos()[0].description.ice_ufrag; |
| 2517 | desc = callee()->pc()->local_description()->description(); |
| 2518 | std::string callee_ufrag_pre_restart = |
| 2519 | desc->transport_infos()[0].description.ice_ufrag; |
| 2520 | |
| 2521 | // Have the caller initiate an ICE restart. |
| 2522 | caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions()); |
| 2523 | caller()->CreateAndSetAndSignalOffer(); |
| 2524 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2525 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted, |
| 2526 | caller()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2527 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected, |
| 2528 | callee()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2529 | |
| 2530 | // Grab the ufrags/candidates again. |
| 2531 | audio_candidates_caller = caller()->pc()->local_description()->candidates(0); |
| 2532 | audio_candidates_callee = callee()->pc()->local_description()->candidates(0); |
| 2533 | ASSERT_GT(audio_candidates_caller->count(), 0u); |
| 2534 | ASSERT_GT(audio_candidates_callee->count(), 0u); |
| 2535 | std::string caller_candidate_post_restart; |
| 2536 | ASSERT_TRUE( |
| 2537 | audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart)); |
| 2538 | std::string callee_candidate_post_restart; |
| 2539 | ASSERT_TRUE( |
| 2540 | audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart)); |
| 2541 | desc = caller()->pc()->local_description()->description(); |
| 2542 | std::string caller_ufrag_post_restart = |
| 2543 | desc->transport_infos()[0].description.ice_ufrag; |
| 2544 | desc = callee()->pc()->local_description()->description(); |
| 2545 | std::string callee_ufrag_post_restart = |
| 2546 | desc->transport_infos()[0].description.ice_ufrag; |
| 2547 | // Sanity check that an ICE restart was actually negotiated in SDP. |
| 2548 | ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart); |
| 2549 | ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart); |
| 2550 | ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart); |
| 2551 | ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart); |
| 2552 | |
| 2553 | // Ensure that additional frames are received after the ICE restart. |
| 2554 | ExpectNewFramesReceivedWithWait( |
| 2555 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2556 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2557 | kMaxWaitForFramesMs); |
| 2558 | } |
| 2559 | |
| 2560 | // Verify that audio/video can be received end-to-end when ICE renomination is |
| 2561 | // enabled. |
| 2562 | TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) { |
| 2563 | PeerConnectionInterface::RTCConfiguration config; |
| 2564 | config.enable_ice_renomination = true; |
| 2565 | ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config)); |
| 2566 | ConnectFakeSignaling(); |
| 2567 | // Do normal offer/answer and wait for some frames to be received in each |
| 2568 | // direction. |
| 2569 | caller()->AddAudioVideoMediaStream(); |
| 2570 | callee()->AddAudioVideoMediaStream(); |
| 2571 | caller()->CreateAndSetAndSignalOffer(); |
| 2572 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2573 | // Sanity check that ICE renomination was actually negotiated. |
| 2574 | const cricket::SessionDescription* desc = |
| 2575 | caller()->pc()->local_description()->description(); |
| 2576 | for (const cricket::TransportInfo& info : desc->transport_infos()) { |
deadbeef | 30952b4 | 2017-04-21 02:41:29 -0700 | [diff] [blame] | 2577 | ASSERT_NE( |
| 2578 | info.description.transport_options.end(), |
| 2579 | std::find(info.description.transport_options.begin(), |
| 2580 | info.description.transport_options.end(), "renomination")); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2581 | } |
| 2582 | desc = callee()->pc()->local_description()->description(); |
| 2583 | for (const cricket::TransportInfo& info : desc->transport_infos()) { |
deadbeef | 30952b4 | 2017-04-21 02:41:29 -0700 | [diff] [blame] | 2584 | ASSERT_NE( |
| 2585 | info.description.transport_options.end(), |
| 2586 | std::find(info.description.transport_options.begin(), |
| 2587 | info.description.transport_options.end(), "renomination")); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2588 | } |
| 2589 | ExpectNewFramesReceivedWithWait( |
| 2590 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2591 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2592 | kMaxWaitForFramesMs); |
| 2593 | } |
| 2594 | |
| 2595 | // This test sets up a call between two parties with audio and video. It then |
| 2596 | // renegotiates setting the video m-line to "port 0", then later renegotiates |
| 2597 | // again, enabling video. |
| 2598 | TEST_F(PeerConnectionIntegrationTest, |
| 2599 | VideoFlowsAfterMediaSectionIsRejectedAndRecycled) { |
| 2600 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2601 | ConnectFakeSignaling(); |
| 2602 | |
| 2603 | // Do initial negotiation, only sending media from the caller. Will result in |
| 2604 | // video and audio recvonly "m=" sections. |
| 2605 | caller()->AddAudioVideoMediaStream(); |
| 2606 | caller()->CreateAndSetAndSignalOffer(); |
| 2607 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2608 | |
| 2609 | // Negotiate again, disabling the video "m=" section (the callee will set the |
| 2610 | // port to 0 due to offer_to_receive_video = 0). |
| 2611 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 2612 | options.offer_to_receive_video = 0; |
| 2613 | callee()->SetOfferAnswerOptions(options); |
| 2614 | caller()->CreateAndSetAndSignalOffer(); |
| 2615 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2616 | // Sanity check that video "m=" section was actually rejected. |
| 2617 | const ContentInfo* answer_video_content = cricket::GetFirstVideoContent( |
| 2618 | callee()->pc()->local_description()->description()); |
| 2619 | ASSERT_NE(nullptr, answer_video_content); |
| 2620 | ASSERT_TRUE(answer_video_content->rejected); |
| 2621 | |
| 2622 | // Enable video and do negotiation again, making sure video is received |
| 2623 | // end-to-end, also adding media stream to callee. |
| 2624 | options.offer_to_receive_video = 1; |
| 2625 | callee()->SetOfferAnswerOptions(options); |
| 2626 | callee()->AddAudioVideoMediaStream(); |
| 2627 | caller()->CreateAndSetAndSignalOffer(); |
| 2628 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2629 | // Verify the caller receives frames from the newly added stream, and the |
| 2630 | // callee receives additional frames from the re-enabled video m= section. |
| 2631 | ExpectNewFramesReceivedWithWait( |
| 2632 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2633 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2634 | kMaxWaitForFramesMs); |
| 2635 | } |
| 2636 | |
| 2637 | // This test sets up a Jsep call between two parties with external |
| 2638 | // VideoDecoderFactory. |
| 2639 | // TODO(holmer): Disabled due to sometimes crashing on buildbots. |
| 2640 | // See issue webrtc/2378. |
| 2641 | TEST_F(PeerConnectionIntegrationTest, |
| 2642 | DISABLED_EndToEndCallWithVideoDecoderFactory) { |
| 2643 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2644 | EnableVideoDecoderFactory(); |
| 2645 | ConnectFakeSignaling(); |
| 2646 | caller()->AddAudioVideoMediaStream(); |
| 2647 | callee()->AddAudioVideoMediaStream(); |
| 2648 | caller()->CreateAndSetAndSignalOffer(); |
| 2649 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2650 | ExpectNewFramesReceivedWithWait( |
| 2651 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2652 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2653 | kMaxWaitForFramesMs); |
| 2654 | } |
| 2655 | |
| 2656 | // This tests that if we negotiate after calling CreateSender but before we |
| 2657 | // have a track, then set a track later, frames from the newly-set track are |
| 2658 | // received end-to-end. |
| 2659 | // TODO(deadbeef): Change this test to use AddTransceiver, once that's |
| 2660 | // implemented. |
| 2661 | TEST_F(PeerConnectionIntegrationTest, |
| 2662 | MediaFlowsAfterEarlyWarmupWithCreateSender) { |
| 2663 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2664 | ConnectFakeSignaling(); |
| 2665 | auto caller_audio_sender = |
| 2666 | caller()->pc()->CreateSender("audio", "caller_stream"); |
| 2667 | auto caller_video_sender = |
| 2668 | caller()->pc()->CreateSender("video", "caller_stream"); |
| 2669 | auto callee_audio_sender = |
| 2670 | callee()->pc()->CreateSender("audio", "callee_stream"); |
| 2671 | auto callee_video_sender = |
| 2672 | callee()->pc()->CreateSender("video", "callee_stream"); |
| 2673 | caller()->CreateAndSetAndSignalOffer(); |
| 2674 | ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs); |
| 2675 | // Wait for ICE to complete, without any tracks being set. |
| 2676 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted, |
| 2677 | caller()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2678 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected, |
| 2679 | callee()->ice_connection_state(), kMaxWaitForFramesMs); |
| 2680 | // Now set the tracks, and expect frames to immediately start flowing. |
| 2681 | EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack())); |
| 2682 | EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack())); |
| 2683 | EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack())); |
| 2684 | EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack())); |
| 2685 | ExpectNewFramesReceivedWithWait( |
| 2686 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2687 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2688 | kMaxWaitForFramesMs); |
| 2689 | } |
| 2690 | |
| 2691 | // This test verifies that a remote video track can be added via AddStream, |
| 2692 | // and sent end-to-end. For this particular test, it's simply echoed back |
| 2693 | // from the caller to the callee, rather than being forwarded to a third |
| 2694 | // PeerConnection. |
| 2695 | TEST_F(PeerConnectionIntegrationTest, CanSendRemoteVideoTrack) { |
| 2696 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2697 | ConnectFakeSignaling(); |
| 2698 | // Just send a video track from the caller. |
| 2699 | caller()->AddMediaStreamFromTracks(nullptr, |
| 2700 | caller()->CreateLocalVideoTrack()); |
| 2701 | caller()->CreateAndSetAndSignalOffer(); |
| 2702 | ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs); |
| 2703 | ASSERT_EQ(1, callee()->remote_streams()->count()); |
| 2704 | |
| 2705 | // Echo the stream back, and do a new offer/anwer (initiated by callee this |
| 2706 | // time). |
| 2707 | callee()->pc()->AddStream(callee()->remote_streams()->at(0)); |
| 2708 | callee()->CreateAndSetAndSignalOffer(); |
| 2709 | ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs); |
| 2710 | |
| 2711 | int expected_caller_received_video_frames = kDefaultExpectedVideoFrameCount; |
| 2712 | ExpectNewFramesReceivedWithWait(0, expected_caller_received_video_frames, 0, |
| 2713 | 0, kMaxWaitForFramesMs); |
| 2714 | } |
| 2715 | |
| 2716 | // Test that we achieve the expected end-to-end connection time, using a |
| 2717 | // fake clock and simulated latency on the media and signaling paths. |
| 2718 | // We use a TURN<->TURN connection because this is usually the quickest to |
| 2719 | // set up initially, especially when we're confident the connection will work |
| 2720 | // and can start sending media before we get a STUN response. |
| 2721 | // |
| 2722 | // With various optimizations enabled, here are the network delays we expect to |
| 2723 | // be on the critical path: |
| 2724 | // 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then |
| 2725 | // signaling answer (with DTLS fingerprint). |
| 2726 | // 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when |
| 2727 | // using TURN<->TURN pair, and DTLS exchange is 4 packets, |
| 2728 | // the first of which should have arrived before the answer. |
| 2729 | TEST_F(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) { |
| 2730 | rtc::ScopedFakeClock fake_clock; |
| 2731 | // Some things use a time of "0" as a special value, so we need to start out |
| 2732 | // the fake clock at a nonzero time. |
| 2733 | // TODO(deadbeef): Fix this. |
| 2734 | fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 2735 | |
| 2736 | static constexpr int media_hop_delay_ms = 50; |
| 2737 | static constexpr int signaling_trip_delay_ms = 500; |
| 2738 | // For explanation of these values, see comment above. |
| 2739 | static constexpr int required_media_hops = 9; |
| 2740 | static constexpr int required_signaling_trips = 2; |
| 2741 | // For internal delays (such as posting an event asychronously). |
| 2742 | static constexpr int allowed_internal_delay_ms = 20; |
| 2743 | static constexpr int total_connection_time_ms = |
| 2744 | media_hop_delay_ms * required_media_hops + |
| 2745 | signaling_trip_delay_ms * required_signaling_trips + |
| 2746 | allowed_internal_delay_ms; |
| 2747 | |
| 2748 | static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0", |
| 2749 | 3478}; |
| 2750 | static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1", |
| 2751 | 0}; |
| 2752 | static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0", |
| 2753 | 3478}; |
| 2754 | static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1", |
| 2755 | 0}; |
| 2756 | cricket::TestTurnServer turn_server_1(network_thread(), |
| 2757 | turn_server_1_internal_address, |
| 2758 | turn_server_1_external_address); |
| 2759 | cricket::TestTurnServer turn_server_2(network_thread(), |
| 2760 | turn_server_2_internal_address, |
| 2761 | turn_server_2_external_address); |
| 2762 | // Bypass permission check on received packets so media can be sent before |
| 2763 | // the candidate is signaled. |
| 2764 | turn_server_1.set_enable_permission_checks(false); |
| 2765 | turn_server_2.set_enable_permission_checks(false); |
| 2766 | |
| 2767 | PeerConnectionInterface::RTCConfiguration client_1_config; |
| 2768 | webrtc::PeerConnectionInterface::IceServer ice_server_1; |
| 2769 | ice_server_1.urls.push_back("turn:88.88.88.0:3478"); |
| 2770 | ice_server_1.username = "test"; |
| 2771 | ice_server_1.password = "test"; |
| 2772 | client_1_config.servers.push_back(ice_server_1); |
| 2773 | client_1_config.type = webrtc::PeerConnectionInterface::kRelay; |
| 2774 | client_1_config.presume_writable_when_fully_relayed = true; |
| 2775 | |
| 2776 | PeerConnectionInterface::RTCConfiguration client_2_config; |
| 2777 | webrtc::PeerConnectionInterface::IceServer ice_server_2; |
| 2778 | ice_server_2.urls.push_back("turn:99.99.99.0:3478"); |
| 2779 | ice_server_2.username = "test"; |
| 2780 | ice_server_2.password = "test"; |
| 2781 | client_2_config.servers.push_back(ice_server_2); |
| 2782 | client_2_config.type = webrtc::PeerConnectionInterface::kRelay; |
| 2783 | client_2_config.presume_writable_when_fully_relayed = true; |
| 2784 | |
| 2785 | ASSERT_TRUE( |
| 2786 | CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config)); |
| 2787 | // Set up the simulated delays. |
| 2788 | SetSignalingDelayMs(signaling_trip_delay_ms); |
| 2789 | ConnectFakeSignaling(); |
| 2790 | virtual_socket_server()->set_delay_mean(media_hop_delay_ms); |
| 2791 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2792 | |
| 2793 | // Set "offer to receive audio/video" without adding any tracks, so we just |
| 2794 | // set up ICE/DTLS with no media. |
| 2795 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 2796 | options.offer_to_receive_audio = 1; |
| 2797 | options.offer_to_receive_video = 1; |
| 2798 | caller()->SetOfferAnswerOptions(options); |
| 2799 | caller()->CreateAndSetAndSignalOffer(); |
deadbeef | 7145280 | 2017-05-07 17:21:01 -0700 | [diff] [blame^] | 2800 | EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms, |
| 2801 | fake_clock); |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2802 | // Need to free the clients here since they're using things we created on |
| 2803 | // the stack. |
| 2804 | delete SetCallerPcWrapperAndReturnCurrent(nullptr); |
| 2805 | delete SetCalleePcWrapperAndReturnCurrent(nullptr); |
| 2806 | } |
| 2807 | |
deadbeef | c964d0b | 2017-04-03 10:03:35 -0700 | [diff] [blame] | 2808 | // Test that audio and video flow end-to-end when codec names don't use the |
| 2809 | // expected casing, given that they're supposed to be case insensitive. To test |
| 2810 | // this, all but one codec is removed from each media description, and its |
| 2811 | // casing is changed. |
| 2812 | // |
| 2813 | // In the past, this has regressed and caused crashes/black video, due to the |
| 2814 | // fact that code at some layers was doing case-insensitive comparisons and |
| 2815 | // code at other layers was not. |
| 2816 | TEST_F(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) { |
| 2817 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2818 | ConnectFakeSignaling(); |
| 2819 | caller()->AddAudioVideoMediaStream(); |
| 2820 | callee()->AddAudioVideoMediaStream(); |
| 2821 | |
| 2822 | // Remove all but one audio/video codec (opus and VP8), and change the |
| 2823 | // casing of the caller's generated offer. |
| 2824 | caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) { |
| 2825 | cricket::AudioContentDescription* audio = |
| 2826 | GetFirstAudioContentDescription(description); |
| 2827 | ASSERT_NE(nullptr, audio); |
| 2828 | auto audio_codecs = audio->codecs(); |
| 2829 | audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(), |
| 2830 | [](const cricket::AudioCodec& codec) { |
| 2831 | return codec.name != "opus"; |
| 2832 | }), |
| 2833 | audio_codecs.end()); |
| 2834 | ASSERT_EQ(1u, audio_codecs.size()); |
| 2835 | audio_codecs[0].name = "OpUs"; |
| 2836 | audio->set_codecs(audio_codecs); |
| 2837 | |
| 2838 | cricket::VideoContentDescription* video = |
| 2839 | GetFirstVideoContentDescription(description); |
| 2840 | ASSERT_NE(nullptr, video); |
| 2841 | auto video_codecs = video->codecs(); |
| 2842 | video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(), |
| 2843 | [](const cricket::VideoCodec& codec) { |
| 2844 | return codec.name != "VP8"; |
| 2845 | }), |
| 2846 | video_codecs.end()); |
| 2847 | ASSERT_EQ(1u, video_codecs.size()); |
| 2848 | video_codecs[0].name = "vP8"; |
| 2849 | video->set_codecs(video_codecs); |
| 2850 | }); |
| 2851 | |
| 2852 | caller()->CreateAndSetAndSignalOffer(); |
| 2853 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2854 | |
| 2855 | // Verify frames are still received end-to-end. |
| 2856 | ExpectNewFramesReceivedWithWait( |
| 2857 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2858 | kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount, |
| 2859 | kMaxWaitForFramesMs); |
| 2860 | } |
| 2861 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 2862 | TEST_F(PeerConnectionIntegrationTest, GetSources) { |
| 2863 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2864 | ConnectFakeSignaling(); |
| 2865 | caller()->AddAudioOnlyMediaStream(); |
| 2866 | caller()->CreateAndSetAndSignalOffer(); |
| 2867 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
deadbeef | d8ad788 | 2017-04-18 16:01:17 -0700 | [diff] [blame] | 2868 | // Wait for one audio frame to be received by the callee. |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 2869 | ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs); |
| 2870 | ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u); |
| 2871 | auto receiver = callee()->pc()->GetReceivers()[0]; |
| 2872 | ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO); |
| 2873 | |
| 2874 | auto contributing_sources = receiver->GetSources(); |
| 2875 | ASSERT_GT(receiver->GetParameters().encodings.size(), 0u); |
| 2876 | EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc, |
| 2877 | contributing_sources[0].source_id()); |
| 2878 | } |
| 2879 | |
deadbeef | 2f425aa | 2017-04-14 10:41:32 -0700 | [diff] [blame] | 2880 | // Test that if a track is removed and added again with a different stream ID, |
| 2881 | // the new stream ID is successfully communicated in SDP and media continues to |
| 2882 | // flow end-to-end. |
| 2883 | TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) { |
| 2884 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 2885 | ConnectFakeSignaling(); |
| 2886 | |
| 2887 | rtc::scoped_refptr<MediaStreamInterface> stream_1 = |
| 2888 | caller()->pc_factory()->CreateLocalMediaStream("stream_1"); |
| 2889 | rtc::scoped_refptr<MediaStreamInterface> stream_2 = |
| 2890 | caller()->pc_factory()->CreateLocalMediaStream("stream_2"); |
| 2891 | |
| 2892 | // Add track using stream 1, do offer/answer. |
| 2893 | rtc::scoped_refptr<webrtc::AudioTrackInterface> track = |
| 2894 | caller()->CreateLocalAudioTrack(); |
| 2895 | rtc::scoped_refptr<webrtc::RtpSenderInterface> sender = |
| 2896 | caller()->pc()->AddTrack(track, {stream_1.get()}); |
| 2897 | caller()->CreateAndSetAndSignalOffer(); |
| 2898 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2899 | // Wait for one audio frame to be received by the callee. |
| 2900 | ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs); |
| 2901 | |
| 2902 | // Remove the sender, and create a new one with the new stream. |
| 2903 | caller()->pc()->RemoveTrack(sender); |
| 2904 | sender = caller()->pc()->AddTrack(track, {stream_2.get()}); |
| 2905 | caller()->CreateAndSetAndSignalOffer(); |
| 2906 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 2907 | // Wait for additional audio frames to be received by the callee. |
| 2908 | ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0, |
| 2909 | kMaxWaitForFramesMs); |
| 2910 | } |
| 2911 | |
deadbeef | 1dcb164 | 2017-03-29 21:08:16 -0700 | [diff] [blame] | 2912 | } // namespace |
| 2913 | |
| 2914 | #endif // if !defined(THREAD_SANITIZER) |