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 <functional>
18 #include <list>
19 #include <map>
20 #include <memory>
21 #include <utility>
22 #include <vector>
23
24 #include "absl/algorithm/container.h"
25 #include "api/media_stream_interface.h"
26 #include "api/peer_connection_interface.h"
27 #include "api/peer_connection_proxy.h"
28 #include "api/rtc_event_log/rtc_event_log_factory.h"
29 #include "api/rtp_receiver_interface.h"
30 #include "api/task_queue/default_task_queue_factory.h"
31 #include "api/uma_metrics.h"
32 #include "api/video_codecs/sdp_video_format.h"
33 #include "call/call.h"
34 #include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
35 #include "media/engine/fake_webrtc_video_engine.h"
36 #include "media/engine/webrtc_media_engine.h"
37 #include "media/engine/webrtc_media_engine_defaults.h"
38 #include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
39 #include "p2p/base/fake_ice_transport.h"
40 #include "p2p/base/mock_async_resolver.h"
41 #include "p2p/base/p2p_constants.h"
42 #include "p2p/base/port_interface.h"
43 #include "p2p/base/test_stun_server.h"
44 #include "p2p/base/test_turn_customizer.h"
45 #include "p2p/base/test_turn_server.h"
46 #include "p2p/client/basic_port_allocator.h"
47 #include "pc/dtmf_sender.h"
48 #include "pc/local_audio_source.h"
49 #include "pc/media_session.h"
50 #include "pc/peer_connection.h"
51 #include "pc/peer_connection_factory.h"
52 #include "pc/rtp_media_utils.h"
53 #include "pc/session_description.h"
54 #include "pc/test/fake_audio_capture_module.h"
55 #include "pc/test/fake_periodic_video_track_source.h"
56 #include "pc/test/fake_rtc_certificate_generator.h"
57 #include "pc/test/fake_video_track_renderer.h"
58 #include "pc/test/mock_peer_connection_observers.h"
59 #include "rtc_base/fake_clock.h"
60 #include "rtc_base/fake_mdns_responder.h"
61 #include "rtc_base/fake_network.h"
62 #include "rtc_base/firewall_socket_server.h"
63 #include "rtc_base/gunit.h"
64 #include "rtc_base/numerics/safe_conversions.h"
65 #include "rtc_base/test_certificate_verifier.h"
66 #include "rtc_base/time_utils.h"
67 #include "rtc_base/virtual_socket_server.h"
68 #include "system_wrappers/include/metrics.h"
69 #include "test/field_trial.h"
70 #include "test/gmock.h"
71
72 namespace webrtc {
73 namespace {
74
75 using ::cricket::ContentInfo;
76 using ::cricket::StreamParams;
77 using ::rtc::SocketAddress;
78 using ::testing::_;
79 using ::testing::Combine;
80 using ::testing::Contains;
81 using ::testing::DoAll;
82 using ::testing::ElementsAre;
83 using ::testing::NiceMock;
84 using ::testing::Return;
85 using ::testing::SetArgPointee;
86 using ::testing::UnorderedElementsAreArray;
87 using ::testing::Values;
88 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
89
90 static const int kDefaultTimeout = 10000;
91 static const int kMaxWaitForStatsMs = 3000;
92 static const int kMaxWaitForActivationMs = 5000;
93 static const int kMaxWaitForFramesMs = 10000;
94 // Default number of audio/video frames to wait for before considering a test
95 // successful.
96 static const int kDefaultExpectedAudioFrameCount = 3;
97 static const int kDefaultExpectedVideoFrameCount = 3;
98
99 static const char kDataChannelLabel[] = "data_channel";
100
101 // SRTP cipher name negotiated by the tests. This must be updated if the
102 // default changes.
103 static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
104 static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
105
106 static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
107
108 // Helper function for constructing offer/answer options to initiate an ICE
109 // restart.
IceRestartOfferAnswerOptions()110 PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
111 PeerConnectionInterface::RTCOfferAnswerOptions options;
112 options.ice_restart = true;
113 return options;
114 }
115
116 // Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
117 // attribute from received SDP, simulating a legacy endpoint.
RemoveSsrcsAndMsids(cricket::SessionDescription * desc)118 void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
119 for (ContentInfo& content : desc->contents()) {
120 content.media_description()->mutable_streams().clear();
121 }
122 desc->set_msid_supported(false);
123 desc->set_msid_signaling(0);
124 }
125
126 // Removes all stream information besides the stream ids, simulating an
127 // endpoint that only signals a=msid lines to convey stream_ids.
RemoveSsrcsAndKeepMsids(cricket::SessionDescription * desc)128 void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
129 for (ContentInfo& content : desc->contents()) {
130 std::string track_id;
131 std::vector<std::string> stream_ids;
132 if (!content.media_description()->streams().empty()) {
133 const StreamParams& first_stream =
134 content.media_description()->streams()[0];
135 track_id = first_stream.id;
136 stream_ids = first_stream.stream_ids();
137 }
138 content.media_description()->mutable_streams().clear();
139 StreamParams new_stream;
140 new_stream.id = track_id;
141 new_stream.set_stream_ids(stream_ids);
142 content.media_description()->AddStream(new_stream);
143 }
144 }
145
FindFirstMediaStatsIndexByKind(const std::string & kind,const std::vector<const webrtc::RTCMediaStreamTrackStats * > & media_stats_vec)146 int FindFirstMediaStatsIndexByKind(
147 const std::string& kind,
148 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
149 media_stats_vec) {
150 for (size_t i = 0; i < media_stats_vec.size(); i++) {
151 if (media_stats_vec[i]->kind.ValueToString() == kind) {
152 return i;
153 }
154 }
155 return -1;
156 }
157
158 class SignalingMessageReceiver {
159 public:
160 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
161 virtual void ReceiveIceMessage(const std::string& sdp_mid,
162 int sdp_mline_index,
163 const std::string& msg) = 0;
164
165 protected:
SignalingMessageReceiver()166 SignalingMessageReceiver() {}
~SignalingMessageReceiver()167 virtual ~SignalingMessageReceiver() {}
168 };
169
170 class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
171 public:
MockRtpReceiverObserver(cricket::MediaType media_type)172 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
173 : expected_media_type_(media_type) {}
174
OnFirstPacketReceived(cricket::MediaType media_type)175 void OnFirstPacketReceived(cricket::MediaType media_type) override {
176 ASSERT_EQ(expected_media_type_, media_type);
177 first_packet_received_ = true;
178 }
179
first_packet_received() const180 bool first_packet_received() const { return first_packet_received_; }
181
~MockRtpReceiverObserver()182 virtual ~MockRtpReceiverObserver() {}
183
184 private:
185 bool first_packet_received_ = false;
186 cricket::MediaType expected_media_type_;
187 };
188
189 // Helper class that wraps a peer connection, observes it, and can accept
190 // signaling messages from another wrapper.
191 //
192 // Uses a fake network, fake A/V capture, and optionally fake
193 // encoders/decoders, though they aren't used by default since they don't
194 // advertise support of any codecs.
195 // TODO(steveanton): See how this could become a subclass of
196 // PeerConnectionWrapper defined in peerconnectionwrapper.h.
197 class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
198 public SignalingMessageReceiver {
199 public:
200 // Different factory methods for convenience.
201 // TODO(deadbeef): Could use the pattern of:
202 //
203 // PeerConnectionWrapper =
204 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
205 //
206 // To reduce some code duplication.
CreateWithDtlsIdentityStore(const std::string & debug_name,std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,rtc::Thread * network_thread,rtc::Thread * worker_thread)207 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
208 const std::string& debug_name,
209 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
210 rtc::Thread* network_thread,
211 rtc::Thread* worker_thread) {
212 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
213 webrtc::PeerConnectionDependencies dependencies(nullptr);
214 dependencies.cert_generator = std::move(cert_generator);
215 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
216 worker_thread, nullptr,
217 /*reset_encoder_factory=*/false,
218 /*reset_decoder_factory=*/false)) {
219 delete client;
220 return nullptr;
221 }
222 return client;
223 }
224
pc_factory() const225 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
226 return peer_connection_factory_.get();
227 }
228
pc() const229 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
230
231 // If a signaling message receiver is set (via ConnectFakeSignaling), this
232 // will set the whole offer/answer exchange in motion. Just need to wait for
233 // the signaling state to reach "stable".
CreateAndSetAndSignalOffer()234 void CreateAndSetAndSignalOffer() {
235 auto offer = CreateOfferAndWait();
236 ASSERT_NE(nullptr, offer);
237 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
238 }
239
240 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
241 // when a remote offer is received (via fake signaling) and an answer is
242 // generated. By default, uses default options.
SetOfferAnswerOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & options)243 void SetOfferAnswerOptions(
244 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
245 offer_answer_options_ = options;
246 }
247
248 // Set a callback to be invoked when SDP is received via the fake signaling
249 // channel, which provides an opportunity to munge (modify) the SDP. This is
250 // used to test SDP being applied that a PeerConnection would normally not
251 // generate, but a non-JSEP endpoint might.
SetReceivedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)252 void SetReceivedSdpMunger(
253 std::function<void(cricket::SessionDescription*)> munger) {
254 received_sdp_munger_ = std::move(munger);
255 }
256
257 // Similar to the above, but this is run on SDP immediately after it's
258 // generated.
SetGeneratedSdpMunger(std::function<void (cricket::SessionDescription *)> munger)259 void SetGeneratedSdpMunger(
260 std::function<void(cricket::SessionDescription*)> munger) {
261 generated_sdp_munger_ = std::move(munger);
262 }
263
264 // Set a callback to be invoked when a remote offer is received via the fake
265 // signaling channel. This provides an opportunity to change the
266 // PeerConnection state before an answer is created and sent to the caller.
SetRemoteOfferHandler(std::function<void ()> handler)267 void SetRemoteOfferHandler(std::function<void()> handler) {
268 remote_offer_handler_ = std::move(handler);
269 }
270
SetRemoteAsyncResolver(rtc::MockAsyncResolver * resolver)271 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
272 remote_async_resolver_ = resolver;
273 }
274
275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
ice_connection_state_history() const277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
clear_ice_connection_state_history()280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
283
284 // Every standardized ICE connection state in order that has been seen by the
285 // observer.
286 std::vector<PeerConnectionInterface::IceConnectionState>
standardized_ice_connection_state_history() const287 standardized_ice_connection_state_history() const {
288 return standardized_ice_connection_state_history_;
289 }
290
291 // Every PeerConnection state in order that has been seen by the observer.
292 std::vector<PeerConnectionInterface::PeerConnectionState>
peer_connection_state_history() const293 peer_connection_state_history() const {
294 return peer_connection_state_history_;
295 }
296
297 // Every ICE gathering state in order that has been seen by the observer.
298 std::vector<PeerConnectionInterface::IceGatheringState>
ice_gathering_state_history() const299 ice_gathering_state_history() const {
300 return ice_gathering_state_history_;
301 }
302 std::vector<cricket::CandidatePairChangeEvent>
ice_candidate_pair_change_history() const303 ice_candidate_pair_change_history() const {
304 return ice_candidate_pair_change_history_;
305 }
306
307 // Every PeerConnection signaling state in order that has been seen by the
308 // observer.
309 std::vector<PeerConnectionInterface::SignalingState>
peer_connection_signaling_state_history() const310 peer_connection_signaling_state_history() const {
311 return peer_connection_signaling_state_history_;
312 }
313
AddAudioVideoTracks()314 void AddAudioVideoTracks() {
315 AddAudioTrack();
316 AddVideoTrack();
317 }
318
AddAudioTrack()319 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
320 return AddTrack(CreateLocalAudioTrack());
321 }
322
AddVideoTrack()323 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
324 return AddTrack(CreateLocalVideoTrack());
325 }
326
CreateLocalAudioTrack()327 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
328 cricket::AudioOptions options;
329 // Disable highpass filter so that we can get all the test audio frames.
330 options.highpass_filter = false;
331 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
332 peer_connection_factory_->CreateAudioSource(options);
333 // TODO(perkj): Test audio source when it is implemented. Currently audio
334 // always use the default input.
335 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
336 source);
337 }
338
CreateLocalVideoTrack()339 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
340 webrtc::FakePeriodicVideoSource::Config config;
341 config.timestamp_offset_ms = rtc::TimeMillis();
342 return CreateLocalVideoTrackInternal(config);
343 }
344
345 rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithConfig(webrtc::FakePeriodicVideoSource::Config config)346 CreateLocalVideoTrackWithConfig(
347 webrtc::FakePeriodicVideoSource::Config config) {
348 return CreateLocalVideoTrackInternal(config);
349 }
350
351 rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation)352 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
353 webrtc::FakePeriodicVideoSource::Config config;
354 config.rotation = rotation;
355 config.timestamp_offset_ms = rtc::TimeMillis();
356 return CreateLocalVideoTrackInternal(config);
357 }
358
AddTrack(rtc::scoped_refptr<MediaStreamTrackInterface> track,const std::vector<std::string> & stream_ids={})359 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
360 rtc::scoped_refptr<MediaStreamTrackInterface> track,
361 const std::vector<std::string>& stream_ids = {}) {
362 auto result = pc()->AddTrack(track, stream_ids);
363 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
364 return result.MoveValue();
365 }
366
GetReceiversOfType(cricket::MediaType media_type)367 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
368 cricket::MediaType media_type) {
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
370 for (const auto& receiver : pc()->GetReceivers()) {
371 if (receiver->media_type() == media_type) {
372 receivers.push_back(receiver);
373 }
374 }
375 return receivers;
376 }
377
GetFirstTransceiverOfType(cricket::MediaType media_type)378 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
379 cricket::MediaType media_type) {
380 for (auto transceiver : pc()->GetTransceivers()) {
381 if (transceiver->receiver()->media_type() == media_type) {
382 return transceiver;
383 }
384 }
385 return nullptr;
386 }
387
SignalingStateStable()388 bool SignalingStateStable() {
389 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
390 }
391
CreateDataChannel()392 void CreateDataChannel() { CreateDataChannel(nullptr); }
393
CreateDataChannel(const webrtc::DataChannelInit * init)394 void CreateDataChannel(const webrtc::DataChannelInit* init) {
395 CreateDataChannel(kDataChannelLabel, init);
396 }
397
CreateDataChannel(const std::string & label,const webrtc::DataChannelInit * init)398 void CreateDataChannel(const std::string& label,
399 const webrtc::DataChannelInit* init) {
400 data_channel_ = pc()->CreateDataChannel(label, init);
401 ASSERT_TRUE(data_channel_.get() != nullptr);
402 data_observer_.reset(new MockDataChannelObserver(data_channel_));
403 }
404
data_channel()405 DataChannelInterface* data_channel() { return data_channel_; }
data_observer() const406 const MockDataChannelObserver* data_observer() const {
407 return data_observer_.get();
408 }
409
audio_frames_received() const410 int audio_frames_received() const {
411 return fake_audio_capture_module_->frames_received();
412 }
413
414 // Takes minimum of video frames received for each track.
415 //
416 // Can be used like:
417 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
418 //
419 // To ensure that all video tracks received at least a certain number of
420 // frames.
min_video_frames_received_per_track() const421 int min_video_frames_received_per_track() const {
422 int min_frames = INT_MAX;
423 if (fake_video_renderers_.empty()) {
424 return 0;
425 }
426
427 for (const auto& pair : fake_video_renderers_) {
428 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
429 }
430 return min_frames;
431 }
432
433 // Returns a MockStatsObserver in a state after stats gathering finished,
434 // which can be used to access the gathered stats.
OldGetStatsForTrack(webrtc::MediaStreamTrackInterface * track)435 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
436 webrtc::MediaStreamTrackInterface* track) {
437 rtc::scoped_refptr<MockStatsObserver> observer(
438 new rtc::RefCountedObject<MockStatsObserver>());
439 EXPECT_TRUE(peer_connection_->GetStats(
440 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
441 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
442 return observer;
443 }
444
445 // Version that doesn't take a track "filter", and gathers all stats.
OldGetStats()446 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
447 return OldGetStatsForTrack(nullptr);
448 }
449
450 // Synchronously gets stats and returns them. If it times out, fails the test
451 // and returns null.
NewGetStats()452 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
453 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
454 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
455 peer_connection_->GetStats(callback);
456 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
457 return callback->report();
458 }
459
rendered_width()460 int rendered_width() {
461 EXPECT_FALSE(fake_video_renderers_.empty());
462 return fake_video_renderers_.empty()
463 ? 0
464 : fake_video_renderers_.begin()->second->width();
465 }
466
rendered_height()467 int rendered_height() {
468 EXPECT_FALSE(fake_video_renderers_.empty());
469 return fake_video_renderers_.empty()
470 ? 0
471 : fake_video_renderers_.begin()->second->height();
472 }
473
rendered_aspect_ratio()474 double rendered_aspect_ratio() {
475 if (rendered_height() == 0) {
476 return 0.0;
477 }
478 return static_cast<double>(rendered_width()) / rendered_height();
479 }
480
rendered_rotation()481 webrtc::VideoRotation rendered_rotation() {
482 EXPECT_FALSE(fake_video_renderers_.empty());
483 return fake_video_renderers_.empty()
484 ? webrtc::kVideoRotation_0
485 : fake_video_renderers_.begin()->second->rotation();
486 }
487
local_rendered_width()488 int local_rendered_width() {
489 return local_video_renderer_ ? local_video_renderer_->width() : 0;
490 }
491
local_rendered_height()492 int local_rendered_height() {
493 return local_video_renderer_ ? local_video_renderer_->height() : 0;
494 }
495
local_rendered_aspect_ratio()496 double local_rendered_aspect_ratio() {
497 if (local_rendered_height() == 0) {
498 return 0.0;
499 }
500 return static_cast<double>(local_rendered_width()) /
501 local_rendered_height();
502 }
503
number_of_remote_streams()504 size_t number_of_remote_streams() {
505 if (!pc()) {
506 return 0;
507 }
508 return pc()->remote_streams()->count();
509 }
510
remote_streams() const511 StreamCollectionInterface* remote_streams() const {
512 if (!pc()) {
513 ADD_FAILURE();
514 return nullptr;
515 }
516 return pc()->remote_streams();
517 }
518
local_streams()519 StreamCollectionInterface* local_streams() {
520 if (!pc()) {
521 ADD_FAILURE();
522 return nullptr;
523 }
524 return pc()->local_streams();
525 }
526
signaling_state()527 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
528 return pc()->signaling_state();
529 }
530
ice_connection_state()531 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
532 return pc()->ice_connection_state();
533 }
534
535 webrtc::PeerConnectionInterface::IceConnectionState
standardized_ice_connection_state()536 standardized_ice_connection_state() {
537 return pc()->standardized_ice_connection_state();
538 }
539
ice_gathering_state()540 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
541 return pc()->ice_gathering_state();
542 }
543
544 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
545 // GetReceivers. They're updated automatically when a remote offer/answer
546 // from the fake signaling channel is applied, or when
547 // ResetRtpReceiverObservers below is called.
548 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
rtp_receiver_observers()549 rtp_receiver_observers() {
550 return rtp_receiver_observers_;
551 }
552
ResetRtpReceiverObservers()553 void ResetRtpReceiverObservers() {
554 rtp_receiver_observers_.clear();
555 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
556 pc()->GetReceivers()) {
557 std::unique_ptr<MockRtpReceiverObserver> observer(
558 new MockRtpReceiverObserver(receiver->media_type()));
559 receiver->SetObserver(observer.get());
560 rtp_receiver_observers_.push_back(std::move(observer));
561 }
562 }
563
network_manager() const564 rtc::FakeNetworkManager* network_manager() const {
565 return fake_network_manager_.get();
566 }
port_allocator() const567 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
568
event_log_factory() const569 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
570 return event_log_factory_;
571 }
572
last_candidate_gathered() const573 const cricket::Candidate& last_candidate_gathered() const {
574 return last_candidate_gathered_;
575 }
error_event() const576 const cricket::IceCandidateErrorEvent& error_event() const {
577 return error_event_;
578 }
579
580 // Sets the mDNS responder for the owned fake network manager and keeps a
581 // reference to the responder.
SetMdnsResponder(std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder)582 void SetMdnsResponder(
583 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
584 RTC_DCHECK(mdns_responder != nullptr);
585 mdns_responder_ = mdns_responder.get();
586 network_manager()->set_mdns_responder(std::move(mdns_responder));
587 }
588
589 // Returns null on failure.
CreateOfferAndWait()590 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
591 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
592 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
593 pc()->CreateOffer(observer, offer_answer_options_);
594 return WaitForDescriptionFromObserver(observer);
595 }
Rollback()596 bool Rollback() {
597 return SetRemoteDescription(
598 webrtc::CreateSessionDescription(SdpType::kRollback, ""));
599 }
600
601 private:
PeerConnectionWrapper(const std::string & debug_name)602 explicit PeerConnectionWrapper(const std::string& debug_name)
603 : debug_name_(debug_name) {}
604
Init(const PeerConnectionFactory::Options * options,const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies,rtc::Thread * network_thread,rtc::Thread * worker_thread,std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,bool reset_encoder_factory,bool reset_decoder_factory)605 bool Init(
606 const PeerConnectionFactory::Options* options,
607 const PeerConnectionInterface::RTCConfiguration* config,
608 webrtc::PeerConnectionDependencies dependencies,
609 rtc::Thread* network_thread,
610 rtc::Thread* worker_thread,
611 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
612 bool reset_encoder_factory,
613 bool reset_decoder_factory) {
614 // There's an error in this test code if Init ends up being called twice.
615 RTC_DCHECK(!peer_connection_);
616 RTC_DCHECK(!peer_connection_factory_);
617
618 fake_network_manager_.reset(new rtc::FakeNetworkManager());
619 fake_network_manager_->AddInterface(kDefaultLocalAddress);
620
621 std::unique_ptr<cricket::PortAllocator> port_allocator(
622 new cricket::BasicPortAllocator(fake_network_manager_.get()));
623 port_allocator_ = port_allocator.get();
624 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
625 if (!fake_audio_capture_module_) {
626 return false;
627 }
628 rtc::Thread* const signaling_thread = rtc::Thread::Current();
629
630 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
631 pc_factory_dependencies.network_thread = network_thread;
632 pc_factory_dependencies.worker_thread = worker_thread;
633 pc_factory_dependencies.signaling_thread = signaling_thread;
634 pc_factory_dependencies.task_queue_factory =
635 webrtc::CreateDefaultTaskQueueFactory();
636 cricket::MediaEngineDependencies media_deps;
637 media_deps.task_queue_factory =
638 pc_factory_dependencies.task_queue_factory.get();
639 media_deps.adm = fake_audio_capture_module_;
640 webrtc::SetMediaEngineDefaults(&media_deps);
641
642 if (reset_encoder_factory) {
643 media_deps.video_encoder_factory.reset();
644 }
645 if (reset_decoder_factory) {
646 media_deps.video_decoder_factory.reset();
647 }
648
649 if (!media_deps.audio_processing) {
650 // If the standard Creation method for APM returns a null pointer, instead
651 // use the builder for testing to create an APM object.
652 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
653 }
654
655 pc_factory_dependencies.media_engine =
656 cricket::CreateMediaEngine(std::move(media_deps));
657 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
658 if (event_log_factory) {
659 event_log_factory_ = event_log_factory.get();
660 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
661 } else {
662 pc_factory_dependencies.event_log_factory =
663 std::make_unique<webrtc::RtcEventLogFactory>(
664 pc_factory_dependencies.task_queue_factory.get());
665 }
666 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
667 std::move(pc_factory_dependencies));
668
669 if (!peer_connection_factory_) {
670 return false;
671 }
672 if (options) {
673 peer_connection_factory_->SetOptions(*options);
674 }
675 if (config) {
676 sdp_semantics_ = config->sdp_semantics;
677 }
678
679 dependencies.allocator = std::move(port_allocator);
680 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
681 return peer_connection_.get() != nullptr;
682 }
683
CreatePeerConnection(const PeerConnectionInterface::RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)684 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
685 const PeerConnectionInterface::RTCConfiguration* config,
686 webrtc::PeerConnectionDependencies dependencies) {
687 PeerConnectionInterface::RTCConfiguration modified_config;
688 // If |config| is null, this will result in a default configuration being
689 // used.
690 if (config) {
691 modified_config = *config;
692 }
693 // Disable resolution adaptation; we don't want it interfering with the
694 // test results.
695 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
696 // ratios and not specific resolutions, is this even necessary?
697 modified_config.set_cpu_adaptation(false);
698
699 dependencies.observer = this;
700 return peer_connection_factory_->CreatePeerConnection(
701 modified_config, std::move(dependencies));
702 }
703
set_signaling_message_receiver(SignalingMessageReceiver * signaling_message_receiver)704 void set_signaling_message_receiver(
705 SignalingMessageReceiver* signaling_message_receiver) {
706 signaling_message_receiver_ = signaling_message_receiver;
707 }
708
set_signaling_delay_ms(int delay_ms)709 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
710
set_signal_ice_candidates(bool signal)711 void set_signal_ice_candidates(bool signal) {
712 signal_ice_candidates_ = signal;
713 }
714
CreateLocalVideoTrackInternal(webrtc::FakePeriodicVideoSource::Config config)715 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
716 webrtc::FakePeriodicVideoSource::Config config) {
717 // Set max frame rate to 10fps to reduce the risk of test flakiness.
718 // TODO(deadbeef): Do something more robust.
719 config.frame_interval_ms = 100;
720
721 video_track_sources_.emplace_back(
722 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
723 config, false /* remote */));
724 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
725 peer_connection_factory_->CreateVideoTrack(
726 rtc::CreateRandomUuid(), video_track_sources_.back()));
727 if (!local_video_renderer_) {
728 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
729 }
730 return track;
731 }
732
HandleIncomingOffer(const std::string & msg)733 void HandleIncomingOffer(const std::string& msg) {
734 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
735 std::unique_ptr<SessionDescriptionInterface> desc =
736 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
737 if (received_sdp_munger_) {
738 received_sdp_munger_(desc->description());
739 }
740
741 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
742 // Setting a remote description may have changed the number of receivers,
743 // so reset the receiver observers.
744 ResetRtpReceiverObservers();
745 if (remote_offer_handler_) {
746 remote_offer_handler_();
747 }
748 auto answer = CreateAnswer();
749 ASSERT_NE(nullptr, answer);
750 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
751 }
752
HandleIncomingAnswer(const std::string & msg)753 void HandleIncomingAnswer(const std::string& msg) {
754 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
755 std::unique_ptr<SessionDescriptionInterface> desc =
756 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
757 if (received_sdp_munger_) {
758 received_sdp_munger_(desc->description());
759 }
760
761 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
762 // Set the RtpReceiverObserver after receivers are created.
763 ResetRtpReceiverObservers();
764 }
765
766 // Returns null on failure.
CreateAnswer()767 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
768 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
769 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
770 pc()->CreateAnswer(observer, offer_answer_options_);
771 return WaitForDescriptionFromObserver(observer);
772 }
773
WaitForDescriptionFromObserver(MockCreateSessionDescriptionObserver * observer)774 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
775 MockCreateSessionDescriptionObserver* observer) {
776 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
777 if (!observer->result()) {
778 return nullptr;
779 }
780 auto description = observer->MoveDescription();
781 if (generated_sdp_munger_) {
782 generated_sdp_munger_(description->description());
783 }
784 return description;
785 }
786
787 // Setting the local description and sending the SDP message over the fake
788 // signaling channel are combined into the same method because the SDP
789 // message needs to be sent as soon as SetLocalDescription finishes, without
790 // waiting for the observer to be called. This ensures that ICE candidates
791 // don't outrace the description.
SetLocalDescriptionAndSendSdpMessage(std::unique_ptr<SessionDescriptionInterface> desc)792 bool SetLocalDescriptionAndSendSdpMessage(
793 std::unique_ptr<SessionDescriptionInterface> desc) {
794 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
795 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
796 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
797 SdpType type = desc->GetType();
798 std::string sdp;
799 EXPECT_TRUE(desc->ToString(&sdp));
800 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
801 pc()->SetLocalDescription(observer, desc.release());
802 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
803 RemoveUnusedVideoRenderers();
804 }
805 // As mentioned above, we need to send the message immediately after
806 // SetLocalDescription.
807 SendSdpMessage(type, sdp);
808 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
809 return true;
810 }
811
SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)812 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
813 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
814 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
815 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
816 pc()->SetRemoteDescription(observer, desc.release());
817 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
818 RemoveUnusedVideoRenderers();
819 }
820 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
821 return observer->result();
822 }
823
824 // This is a work around to remove unused fake_video_renderers from
825 // transceivers that have either stopped or are no longer receiving.
RemoveUnusedVideoRenderers()826 void RemoveUnusedVideoRenderers() {
827 auto transceivers = pc()->GetTransceivers();
828 for (auto& transceiver : transceivers) {
829 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
830 continue;
831 }
832 // Remove fake video renderers from any stopped transceivers.
833 if (transceiver->stopped()) {
834 auto it =
835 fake_video_renderers_.find(transceiver->receiver()->track()->id());
836 if (it != fake_video_renderers_.end()) {
837 fake_video_renderers_.erase(it);
838 }
839 }
840 // Remove fake video renderers from any transceivers that are no longer
841 // receiving.
842 if ((transceiver->current_direction() &&
843 !webrtc::RtpTransceiverDirectionHasRecv(
844 *transceiver->current_direction()))) {
845 auto it =
846 fake_video_renderers_.find(transceiver->receiver()->track()->id());
847 if (it != fake_video_renderers_.end()) {
848 fake_video_renderers_.erase(it);
849 }
850 }
851 }
852 }
853
854 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
855 // default).
SendSdpMessage(SdpType type,const std::string & msg)856 void SendSdpMessage(SdpType type, const std::string& msg) {
857 if (signaling_delay_ms_ == 0) {
858 RelaySdpMessageIfReceiverExists(type, msg);
859 } else {
860 invoker_.AsyncInvokeDelayed<void>(
861 RTC_FROM_HERE, rtc::Thread::Current(),
862 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
863 this, type, msg),
864 signaling_delay_ms_);
865 }
866 }
867
RelaySdpMessageIfReceiverExists(SdpType type,const std::string & msg)868 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
869 if (signaling_message_receiver_) {
870 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
871 }
872 }
873
874 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
875 // default).
SendIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)876 void SendIceMessage(const std::string& sdp_mid,
877 int sdp_mline_index,
878 const std::string& msg) {
879 if (signaling_delay_ms_ == 0) {
880 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
881 } else {
882 invoker_.AsyncInvokeDelayed<void>(
883 RTC_FROM_HERE, rtc::Thread::Current(),
884 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
885 this, sdp_mid, sdp_mline_index, msg),
886 signaling_delay_ms_);
887 }
888 }
889
RelayIceMessageIfReceiverExists(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)890 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
891 int sdp_mline_index,
892 const std::string& msg) {
893 if (signaling_message_receiver_) {
894 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
895 msg);
896 }
897 }
898
899 // SignalingMessageReceiver callbacks.
ReceiveSdpMessage(SdpType type,const std::string & msg)900 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
901 if (type == SdpType::kOffer) {
902 HandleIncomingOffer(msg);
903 } else {
904 HandleIncomingAnswer(msg);
905 }
906 }
907
ReceiveIceMessage(const std::string & sdp_mid,int sdp_mline_index,const std::string & msg)908 void ReceiveIceMessage(const std::string& sdp_mid,
909 int sdp_mline_index,
910 const std::string& msg) override {
911 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
912 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
913 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
914 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
915 }
916
917 // PeerConnectionObserver callbacks.
OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)918 void OnSignalingChange(
919 webrtc::PeerConnectionInterface::SignalingState new_state) override {
920 EXPECT_EQ(pc()->signaling_state(), new_state);
921 peer_connection_signaling_state_history_.push_back(new_state);
922 }
OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)923 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
924 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
925 streams) override {
926 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
927 rtc::scoped_refptr<VideoTrackInterface> video_track(
928 static_cast<VideoTrackInterface*>(receiver->track().get()));
929 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
930 fake_video_renderers_.end());
931 fake_video_renderers_[video_track->id()] =
932 std::make_unique<FakeVideoTrackRenderer>(video_track);
933 }
934 }
OnRemoveTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver)935 void OnRemoveTrack(
936 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
937 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
938 auto it = fake_video_renderers_.find(receiver->track()->id());
939 RTC_DCHECK(it != fake_video_renderers_.end());
940 fake_video_renderers_.erase(it);
941 }
942 }
OnRenegotiationNeeded()943 void OnRenegotiationNeeded() override {}
OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)944 void OnIceConnectionChange(
945 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
946 EXPECT_EQ(pc()->ice_connection_state(), new_state);
947 ice_connection_state_history_.push_back(new_state);
948 }
OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)949 void OnStandardizedIceConnectionChange(
950 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
951 standardized_ice_connection_state_history_.push_back(new_state);
952 }
OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state)953 void OnConnectionChange(
954 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
955 peer_connection_state_history_.push_back(new_state);
956 }
957
OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)958 void OnIceGatheringChange(
959 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
960 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
961 ice_gathering_state_history_.push_back(new_state);
962 }
963
OnIceSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent & event)964 void OnIceSelectedCandidatePairChanged(
965 const cricket::CandidatePairChangeEvent& event) {
966 ice_candidate_pair_change_history_.push_back(event);
967 }
968
OnIceCandidate(const webrtc::IceCandidateInterface * candidate)969 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
970 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
971
972 if (remote_async_resolver_) {
973 const auto& local_candidate = candidate->candidate();
974 if (local_candidate.address().IsUnresolvedIP()) {
975 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
976 rtc::SocketAddress resolved_addr(local_candidate.address());
977 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
978 local_candidate.address().hostname());
979 RTC_DCHECK(!resolved_ip.IsNil());
980 resolved_addr.SetResolvedIP(resolved_ip);
981 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
982 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
983 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
984 }
985 }
986
987 std::string ice_sdp;
988 EXPECT_TRUE(candidate->ToString(&ice_sdp));
989 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
990 // Remote party may be deleted.
991 return;
992 }
993 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
994 last_candidate_gathered_ = candidate->candidate();
995 }
OnIceCandidateError(const std::string & address,int port,const std::string & url,int error_code,const std::string & error_text)996 void OnIceCandidateError(const std::string& address,
997 int port,
998 const std::string& url,
999 int error_code,
1000 const std::string& error_text) override {
1001 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
1002 error_code, error_text);
1003 }
OnDataChannel(rtc::scoped_refptr<DataChannelInterface> data_channel)1004 void OnDataChannel(
1005 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
1006 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
1007 data_channel_ = data_channel;
1008 data_observer_.reset(new MockDataChannelObserver(data_channel));
1009 }
1010
1011 std::string debug_name_;
1012
1013 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1014 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1015 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
1016
1017 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1018 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1019 peer_connection_factory_;
1020
1021 cricket::PortAllocator* port_allocator_;
1022 // Needed to keep track of number of frames sent.
1023 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1024 // Needed to keep track of number of frames received.
1025 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1026 fake_video_renderers_;
1027 // Needed to ensure frames aren't received for removed tracks.
1028 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1029 removed_fake_video_renderers_;
1030
1031 // For remote peer communication.
1032 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1033 int signaling_delay_ms_ = 0;
1034 bool signal_ice_candidates_ = true;
1035 cricket::Candidate last_candidate_gathered_;
1036 cricket::IceCandidateErrorEvent error_event_;
1037
1038 // Store references to the video sources we've created, so that we can stop
1039 // them, if required.
1040 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1041 video_track_sources_;
1042 // |local_video_renderer_| attached to the first created local video track.
1043 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1044
1045 SdpSemantics sdp_semantics_;
1046 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1047 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1048 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
1049 std::function<void()> remote_offer_handler_;
1050 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
1051 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1052 std::unique_ptr<MockDataChannelObserver> data_observer_;
1053
1054 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1055
1056 std::vector<PeerConnectionInterface::IceConnectionState>
1057 ice_connection_state_history_;
1058 std::vector<PeerConnectionInterface::IceConnectionState>
1059 standardized_ice_connection_state_history_;
1060 std::vector<PeerConnectionInterface::PeerConnectionState>
1061 peer_connection_state_history_;
1062 std::vector<PeerConnectionInterface::IceGatheringState>
1063 ice_gathering_state_history_;
1064 std::vector<cricket::CandidatePairChangeEvent>
1065 ice_candidate_pair_change_history_;
1066 std::vector<PeerConnectionInterface::SignalingState>
1067 peer_connection_signaling_state_history_;
1068 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1069
1070 rtc::AsyncInvoker invoker_;
1071
1072 friend class PeerConnectionIntegrationBaseTest;
1073 };
1074
1075 class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1076 public:
1077 virtual ~MockRtcEventLogOutput() = default;
1078 MOCK_METHOD(bool, IsActive, (), (const, override));
1079 MOCK_METHOD(bool, Write, (const std::string&), (override));
1080 };
1081
1082 // This helper object is used for both specifying how many audio/video frames
1083 // are expected to be received for a caller/callee. It provides helper functions
1084 // to specify these expectations. The object initially starts in a state of no
1085 // expectations.
1086 class MediaExpectations {
1087 public:
1088 enum ExpectFrames {
1089 kExpectSomeFrames,
1090 kExpectNoFrames,
1091 kNoExpectation,
1092 };
1093
ExpectBidirectionalAudioAndVideo()1094 void ExpectBidirectionalAudioAndVideo() {
1095 ExpectBidirectionalAudio();
1096 ExpectBidirectionalVideo();
1097 }
1098
ExpectBidirectionalAudio()1099 void ExpectBidirectionalAudio() {
1100 CallerExpectsSomeAudio();
1101 CalleeExpectsSomeAudio();
1102 }
1103
ExpectNoAudio()1104 void ExpectNoAudio() {
1105 CallerExpectsNoAudio();
1106 CalleeExpectsNoAudio();
1107 }
1108
ExpectBidirectionalVideo()1109 void ExpectBidirectionalVideo() {
1110 CallerExpectsSomeVideo();
1111 CalleeExpectsSomeVideo();
1112 }
1113
ExpectNoVideo()1114 void ExpectNoVideo() {
1115 CallerExpectsNoVideo();
1116 CalleeExpectsNoVideo();
1117 }
1118
CallerExpectsSomeAudioAndVideo()1119 void CallerExpectsSomeAudioAndVideo() {
1120 CallerExpectsSomeAudio();
1121 CallerExpectsSomeVideo();
1122 }
1123
CalleeExpectsSomeAudioAndVideo()1124 void CalleeExpectsSomeAudioAndVideo() {
1125 CalleeExpectsSomeAudio();
1126 CalleeExpectsSomeVideo();
1127 }
1128
1129 // Caller's audio functions.
CallerExpectsSomeAudio(int expected_audio_frames=kDefaultExpectedAudioFrameCount)1130 void CallerExpectsSomeAudio(
1131 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1132 caller_audio_expectation_ = kExpectSomeFrames;
1133 caller_audio_frames_expected_ = expected_audio_frames;
1134 }
1135
CallerExpectsNoAudio()1136 void CallerExpectsNoAudio() {
1137 caller_audio_expectation_ = kExpectNoFrames;
1138 caller_audio_frames_expected_ = 0;
1139 }
1140
1141 // Caller's video functions.
CallerExpectsSomeVideo(int expected_video_frames=kDefaultExpectedVideoFrameCount)1142 void CallerExpectsSomeVideo(
1143 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1144 caller_video_expectation_ = kExpectSomeFrames;
1145 caller_video_frames_expected_ = expected_video_frames;
1146 }
1147
CallerExpectsNoVideo()1148 void CallerExpectsNoVideo() {
1149 caller_video_expectation_ = kExpectNoFrames;
1150 caller_video_frames_expected_ = 0;
1151 }
1152
1153 // Callee's audio functions.
CalleeExpectsSomeAudio(int expected_audio_frames=kDefaultExpectedAudioFrameCount)1154 void CalleeExpectsSomeAudio(
1155 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1156 callee_audio_expectation_ = kExpectSomeFrames;
1157 callee_audio_frames_expected_ = expected_audio_frames;
1158 }
1159
CalleeExpectsNoAudio()1160 void CalleeExpectsNoAudio() {
1161 callee_audio_expectation_ = kExpectNoFrames;
1162 callee_audio_frames_expected_ = 0;
1163 }
1164
1165 // Callee's video functions.
CalleeExpectsSomeVideo(int expected_video_frames=kDefaultExpectedVideoFrameCount)1166 void CalleeExpectsSomeVideo(
1167 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1168 callee_video_expectation_ = kExpectSomeFrames;
1169 callee_video_frames_expected_ = expected_video_frames;
1170 }
1171
CalleeExpectsNoVideo()1172 void CalleeExpectsNoVideo() {
1173 callee_video_expectation_ = kExpectNoFrames;
1174 callee_video_frames_expected_ = 0;
1175 }
1176
1177 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1178 ExpectFrames caller_video_expectation_ = kNoExpectation;
1179 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1180 ExpectFrames callee_video_expectation_ = kNoExpectation;
1181 int caller_audio_frames_expected_ = 0;
1182 int caller_video_frames_expected_ = 0;
1183 int callee_audio_frames_expected_ = 0;
1184 int callee_video_frames_expected_ = 0;
1185 };
1186
1187 class MockIceTransport : public webrtc::IceTransportInterface {
1188 public:
MockIceTransport(const std::string & name,int component)1189 MockIceTransport(const std::string& name, int component)
1190 : internal_(std::make_unique<cricket::FakeIceTransport>(
1191 name,
1192 component,
1193 nullptr /* network_thread */)) {}
1194 ~MockIceTransport() = default;
internal()1195 cricket::IceTransportInternal* internal() { return internal_.get(); }
1196
1197 private:
1198 std::unique_ptr<cricket::FakeIceTransport> internal_;
1199 };
1200
1201 class MockIceTransportFactory : public IceTransportFactory {
1202 public:
1203 ~MockIceTransportFactory() override = default;
CreateIceTransport(const std::string & transport_name,int component,IceTransportInit init)1204 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1205 const std::string& transport_name,
1206 int component,
1207 IceTransportInit init) {
1208 RecordIceTransportCreated();
1209 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1210 component);
1211 }
1212 MOCK_METHOD(void, RecordIceTransportCreated, ());
1213 };
1214
1215 // Tests two PeerConnections connecting to each other end-to-end, using a
1216 // virtual network, fake A/V capture and fake encoder/decoders. The
1217 // PeerConnections share the threads/socket servers, but use separate versions
1218 // of everything else (including "PeerConnectionFactory"s).
1219 class PeerConnectionIntegrationBaseTest : public ::testing::Test {
1220 public:
PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)1221 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1222 : sdp_semantics_(sdp_semantics),
1223 ss_(new rtc::VirtualSocketServer()),
1224 fss_(new rtc::FirewallSocketServer(ss_.get())),
1225 network_thread_(new rtc::Thread(fss_.get())),
1226 worker_thread_(rtc::Thread::Create()) {
1227 network_thread_->SetName("PCNetworkThread", this);
1228 worker_thread_->SetName("PCWorkerThread", this);
1229 RTC_CHECK(network_thread_->Start());
1230 RTC_CHECK(worker_thread_->Start());
1231 webrtc::metrics::Reset();
1232 }
1233
~PeerConnectionIntegrationBaseTest()1234 ~PeerConnectionIntegrationBaseTest() {
1235 // The PeerConnections should deleted before the TurnCustomizers.
1236 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1237 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1238 // that the TurnCustomizer outlives the life of the PeerConnection or else
1239 // when Send() is called it will hit a seg fault.
1240 if (caller_) {
1241 caller_->set_signaling_message_receiver(nullptr);
1242 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
1243 }
1244 if (callee_) {
1245 callee_->set_signaling_message_receiver(nullptr);
1246 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
1247 }
1248
1249 // If turn servers were created for the test they need to be destroyed on
1250 // the network thread.
1251 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1252 turn_servers_.clear();
1253 turn_customizers_.clear();
1254 });
1255 }
1256
SignalingStateStable()1257 bool SignalingStateStable() {
1258 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1259 }
1260
DtlsConnected()1261 bool DtlsConnected() {
1262 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1263 // are connected. This is an important distinction. Once we have separate
1264 // ICE and DTLS state, this check needs to use the DTLS state.
1265 return (callee()->ice_connection_state() ==
1266 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1267 callee()->ice_connection_state() ==
1268 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1269 (caller()->ice_connection_state() ==
1270 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1271 caller()->ice_connection_state() ==
1272 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1273 }
1274
1275 // When |event_log_factory| is null, the default implementation of the event
1276 // log factory will be used.
CreatePeerConnectionWrapper(const std::string & debug_name,const PeerConnectionFactory::Options * options,const RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies,std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,bool reset_encoder_factory,bool reset_decoder_factory)1277 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1278 const std::string& debug_name,
1279 const PeerConnectionFactory::Options* options,
1280 const RTCConfiguration* config,
1281 webrtc::PeerConnectionDependencies dependencies,
1282 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1283 bool reset_encoder_factory,
1284 bool reset_decoder_factory) {
1285 RTCConfiguration modified_config;
1286 if (config) {
1287 modified_config = *config;
1288 }
1289 modified_config.sdp_semantics = sdp_semantics_;
1290 if (!dependencies.cert_generator) {
1291 dependencies.cert_generator =
1292 std::make_unique<FakeRTCCertificateGenerator>();
1293 }
1294 std::unique_ptr<PeerConnectionWrapper> client(
1295 new PeerConnectionWrapper(debug_name));
1296
1297 if (!client->Init(options, &modified_config, std::move(dependencies),
1298 network_thread_.get(), worker_thread_.get(),
1299 std::move(event_log_factory), reset_encoder_factory,
1300 reset_decoder_factory)) {
1301 return nullptr;
1302 }
1303 return client;
1304 }
1305
1306 std::unique_ptr<PeerConnectionWrapper>
CreatePeerConnectionWrapperWithFakeRtcEventLog(const std::string & debug_name,const PeerConnectionFactory::Options * options,const RTCConfiguration * config,webrtc::PeerConnectionDependencies dependencies)1307 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1308 const std::string& debug_name,
1309 const PeerConnectionFactory::Options* options,
1310 const RTCConfiguration* config,
1311 webrtc::PeerConnectionDependencies dependencies) {
1312 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1313 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
1314 return CreatePeerConnectionWrapper(debug_name, options, config,
1315 std::move(dependencies),
1316 std::move(event_log_factory),
1317 /*reset_encoder_factory=*/false,
1318 /*reset_decoder_factory=*/false);
1319 }
1320
CreatePeerConnectionWrappers()1321 bool CreatePeerConnectionWrappers() {
1322 return CreatePeerConnectionWrappersWithConfig(
1323 PeerConnectionInterface::RTCConfiguration(),
1324 PeerConnectionInterface::RTCConfiguration());
1325 }
1326
CreatePeerConnectionWrappersWithSdpSemantics(SdpSemantics caller_semantics,SdpSemantics callee_semantics)1327 bool CreatePeerConnectionWrappersWithSdpSemantics(
1328 SdpSemantics caller_semantics,
1329 SdpSemantics callee_semantics) {
1330 // Can't specify the sdp_semantics in the passed-in configuration since it
1331 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1332 // stored in sdp_semantics_. So get around this by modifying the instance
1333 // variable before calling CreatePeerConnectionWrapper for the caller and
1334 // callee PeerConnections.
1335 SdpSemantics original_semantics = sdp_semantics_;
1336 sdp_semantics_ = caller_semantics;
1337 caller_ = CreatePeerConnectionWrapper(
1338 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1339 nullptr,
1340 /*reset_encoder_factory=*/false,
1341 /*reset_decoder_factory=*/false);
1342 sdp_semantics_ = callee_semantics;
1343 callee_ = CreatePeerConnectionWrapper(
1344 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1345 nullptr,
1346 /*reset_encoder_factory=*/false,
1347 /*reset_decoder_factory=*/false);
1348 sdp_semantics_ = original_semantics;
1349 return caller_ && callee_;
1350 }
1351
CreatePeerConnectionWrappersWithConfig(const PeerConnectionInterface::RTCConfiguration & caller_config,const PeerConnectionInterface::RTCConfiguration & callee_config)1352 bool CreatePeerConnectionWrappersWithConfig(
1353 const PeerConnectionInterface::RTCConfiguration& caller_config,
1354 const PeerConnectionInterface::RTCConfiguration& callee_config) {
1355 caller_ = CreatePeerConnectionWrapper(
1356 "Caller", nullptr, &caller_config,
1357 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1358 /*reset_encoder_factory=*/false,
1359 /*reset_decoder_factory=*/false);
1360 callee_ = CreatePeerConnectionWrapper(
1361 "Callee", nullptr, &callee_config,
1362 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1363 /*reset_encoder_factory=*/false,
1364 /*reset_decoder_factory=*/false);
1365 return caller_ && callee_;
1366 }
1367
CreatePeerConnectionWrappersWithConfigAndDeps(const PeerConnectionInterface::RTCConfiguration & caller_config,webrtc::PeerConnectionDependencies caller_dependencies,const PeerConnectionInterface::RTCConfiguration & callee_config,webrtc::PeerConnectionDependencies callee_dependencies)1368 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1369 const PeerConnectionInterface::RTCConfiguration& caller_config,
1370 webrtc::PeerConnectionDependencies caller_dependencies,
1371 const PeerConnectionInterface::RTCConfiguration& callee_config,
1372 webrtc::PeerConnectionDependencies callee_dependencies) {
1373 caller_ =
1374 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1375 std::move(caller_dependencies), nullptr,
1376 /*reset_encoder_factory=*/false,
1377 /*reset_decoder_factory=*/false);
1378 callee_ =
1379 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1380 std::move(callee_dependencies), nullptr,
1381 /*reset_encoder_factory=*/false,
1382 /*reset_decoder_factory=*/false);
1383 return caller_ && callee_;
1384 }
1385
CreatePeerConnectionWrappersWithOptions(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options)1386 bool CreatePeerConnectionWrappersWithOptions(
1387 const PeerConnectionFactory::Options& caller_options,
1388 const PeerConnectionFactory::Options& callee_options) {
1389 caller_ = CreatePeerConnectionWrapper(
1390 "Caller", &caller_options, nullptr,
1391 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1392 /*reset_encoder_factory=*/false,
1393 /*reset_decoder_factory=*/false);
1394 callee_ = CreatePeerConnectionWrapper(
1395 "Callee", &callee_options, nullptr,
1396 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1397 /*reset_encoder_factory=*/false,
1398 /*reset_decoder_factory=*/false);
1399 return caller_ && callee_;
1400 }
1401
CreatePeerConnectionWrappersWithFakeRtcEventLog()1402 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1403 PeerConnectionInterface::RTCConfiguration default_config;
1404 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1405 "Caller", nullptr, &default_config,
1406 webrtc::PeerConnectionDependencies(nullptr));
1407 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1408 "Callee", nullptr, &default_config,
1409 webrtc::PeerConnectionDependencies(nullptr));
1410 return caller_ && callee_;
1411 }
1412
1413 std::unique_ptr<PeerConnectionWrapper>
CreatePeerConnectionWrapperWithAlternateKey()1414 CreatePeerConnectionWrapperWithAlternateKey() {
1415 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1416 new FakeRTCCertificateGenerator());
1417 cert_generator->use_alternate_key();
1418
1419 webrtc::PeerConnectionDependencies dependencies(nullptr);
1420 dependencies.cert_generator = std::move(cert_generator);
1421 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1422 std::move(dependencies), nullptr,
1423 /*reset_encoder_factory=*/false,
1424 /*reset_decoder_factory=*/false);
1425 }
1426
CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee)1427 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1428 caller_ = CreatePeerConnectionWrapper(
1429 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1430 nullptr,
1431 /*reset_encoder_factory=*/!caller_to_callee,
1432 /*reset_decoder_factory=*/caller_to_callee);
1433 callee_ = CreatePeerConnectionWrapper(
1434 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
1435 nullptr,
1436 /*reset_encoder_factory=*/caller_to_callee,
1437 /*reset_decoder_factory=*/!caller_to_callee);
1438 return caller_ && callee_;
1439 }
1440
CreateTurnServer(rtc::SocketAddress internal_address,rtc::SocketAddress external_address,cricket::ProtocolType type=cricket::ProtocolType::PROTO_UDP,const std::string & common_name="test turn server")1441 cricket::TestTurnServer* CreateTurnServer(
1442 rtc::SocketAddress internal_address,
1443 rtc::SocketAddress external_address,
1444 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1445 const std::string& common_name = "test turn server") {
1446 rtc::Thread* thread = network_thread();
1447 std::unique_ptr<cricket::TestTurnServer> turn_server =
1448 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1449 RTC_FROM_HERE,
1450 [thread, internal_address, external_address, type, common_name] {
1451 return std::make_unique<cricket::TestTurnServer>(
1452 thread, internal_address, external_address, type,
1453 /*ignore_bad_certs=*/true, common_name);
1454 });
1455 turn_servers_.push_back(std::move(turn_server));
1456 // Interactions with the turn server should be done on the network thread.
1457 return turn_servers_.back().get();
1458 }
1459
CreateTurnCustomizer()1460 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1461 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1462 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1463 RTC_FROM_HERE,
1464 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
1465 turn_customizers_.push_back(std::move(turn_customizer));
1466 // Interactions with the turn customizer should be done on the network
1467 // thread.
1468 return turn_customizers_.back().get();
1469 }
1470
1471 // Checks that the function counters for a TestTurnCustomizer are greater than
1472 // 0.
ExpectTurnCustomizerCountersIncremented(cricket::TestTurnCustomizer * turn_customizer)1473 void ExpectTurnCustomizerCountersIncremented(
1474 cricket::TestTurnCustomizer* turn_customizer) {
1475 unsigned int allow_channel_data_counter =
1476 network_thread()->Invoke<unsigned int>(
1477 RTC_FROM_HERE, [turn_customizer] {
1478 return turn_customizer->allow_channel_data_cnt_;
1479 });
1480 EXPECT_GT(allow_channel_data_counter, 0u);
1481 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1482 RTC_FROM_HERE,
1483 [turn_customizer] { return turn_customizer->modify_cnt_; });
1484 EXPECT_GT(modify_counter, 0u);
1485 }
1486
1487 // Once called, SDP blobs and ICE candidates will be automatically signaled
1488 // between PeerConnections.
ConnectFakeSignaling()1489 void ConnectFakeSignaling() {
1490 caller_->set_signaling_message_receiver(callee_.get());
1491 callee_->set_signaling_message_receiver(caller_.get());
1492 }
1493
1494 // Once called, SDP blobs will be automatically signaled between
1495 // PeerConnections. Note that ICE candidates will not be signaled unless they
1496 // are in the exchanged SDP blobs.
ConnectFakeSignalingForSdpOnly()1497 void ConnectFakeSignalingForSdpOnly() {
1498 ConnectFakeSignaling();
1499 SetSignalIceCandidates(false);
1500 }
1501
SetSignalingDelayMs(int delay_ms)1502 void SetSignalingDelayMs(int delay_ms) {
1503 caller_->set_signaling_delay_ms(delay_ms);
1504 callee_->set_signaling_delay_ms(delay_ms);
1505 }
1506
SetSignalIceCandidates(bool signal)1507 void SetSignalIceCandidates(bool signal) {
1508 caller_->set_signal_ice_candidates(signal);
1509 callee_->set_signal_ice_candidates(signal);
1510 }
1511
1512 // Messages may get lost on the unreliable DataChannel, so we send multiple
1513 // times to avoid test flakiness.
SendRtpDataWithRetries(webrtc::DataChannelInterface * dc,const std::string & data,int retries)1514 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1515 const std::string& data,
1516 int retries) {
1517 for (int i = 0; i < retries; ++i) {
1518 dc->Send(DataBuffer(data));
1519 }
1520 }
1521
network_thread()1522 rtc::Thread* network_thread() { return network_thread_.get(); }
1523
virtual_socket_server()1524 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1525
caller()1526 PeerConnectionWrapper* caller() { return caller_.get(); }
1527
1528 // Set the |caller_| to the |wrapper| passed in and return the
1529 // original |caller_|.
SetCallerPcWrapperAndReturnCurrent(PeerConnectionWrapper * wrapper)1530 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1531 PeerConnectionWrapper* wrapper) {
1532 PeerConnectionWrapper* old = caller_.release();
1533 caller_.reset(wrapper);
1534 return old;
1535 }
1536
callee()1537 PeerConnectionWrapper* callee() { return callee_.get(); }
1538
1539 // Set the |callee_| to the |wrapper| passed in and return the
1540 // original |callee_|.
SetCalleePcWrapperAndReturnCurrent(PeerConnectionWrapper * wrapper)1541 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1542 PeerConnectionWrapper* wrapper) {
1543 PeerConnectionWrapper* old = callee_.release();
1544 callee_.reset(wrapper);
1545 return old;
1546 }
1547
SetPortAllocatorFlags(uint32_t caller_flags,uint32_t callee_flags)1548 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1549 network_thread()->Invoke<void>(
1550 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1551 caller()->port_allocator(), caller_flags));
1552 network_thread()->Invoke<void>(
1553 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1554 callee()->port_allocator(), callee_flags));
1555 }
1556
firewall() const1557 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1558
1559 // Expects the provided number of new frames to be received within
1560 // kMaxWaitForFramesMs. The new expected frames are specified in
1561 // |media_expectations|. Returns false if any of the expectations were
1562 // not met.
ExpectNewFrames(const MediaExpectations & media_expectations)1563 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1564 // First initialize the expected frame counts based upon the current
1565 // frame count.
1566 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1567 if (media_expectations.caller_audio_expectation_ ==
1568 MediaExpectations::kExpectSomeFrames) {
1569 total_caller_audio_frames_expected +=
1570 media_expectations.caller_audio_frames_expected_;
1571 }
1572 int total_caller_video_frames_expected =
1573 caller()->min_video_frames_received_per_track();
1574 if (media_expectations.caller_video_expectation_ ==
1575 MediaExpectations::kExpectSomeFrames) {
1576 total_caller_video_frames_expected +=
1577 media_expectations.caller_video_frames_expected_;
1578 }
1579 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1580 if (media_expectations.callee_audio_expectation_ ==
1581 MediaExpectations::kExpectSomeFrames) {
1582 total_callee_audio_frames_expected +=
1583 media_expectations.callee_audio_frames_expected_;
1584 }
1585 int total_callee_video_frames_expected =
1586 callee()->min_video_frames_received_per_track();
1587 if (media_expectations.callee_video_expectation_ ==
1588 MediaExpectations::kExpectSomeFrames) {
1589 total_callee_video_frames_expected +=
1590 media_expectations.callee_video_frames_expected_;
1591 }
1592
1593 // Wait for the expected frames.
1594 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
1595 total_caller_audio_frames_expected &&
1596 caller()->min_video_frames_received_per_track() >=
1597 total_caller_video_frames_expected &&
1598 callee()->audio_frames_received() >=
1599 total_callee_audio_frames_expected &&
1600 callee()->min_video_frames_received_per_track() >=
1601 total_callee_video_frames_expected,
1602 kMaxWaitForFramesMs);
1603 bool expectations_correct =
1604 caller()->audio_frames_received() >=
1605 total_caller_audio_frames_expected &&
1606 caller()->min_video_frames_received_per_track() >=
1607 total_caller_video_frames_expected &&
1608 callee()->audio_frames_received() >=
1609 total_callee_audio_frames_expected &&
1610 callee()->min_video_frames_received_per_track() >=
1611 total_callee_video_frames_expected;
1612
1613 // After the combined wait, print out a more detailed message upon
1614 // failure.
1615 EXPECT_GE(caller()->audio_frames_received(),
1616 total_caller_audio_frames_expected);
1617 EXPECT_GE(caller()->min_video_frames_received_per_track(),
1618 total_caller_video_frames_expected);
1619 EXPECT_GE(callee()->audio_frames_received(),
1620 total_callee_audio_frames_expected);
1621 EXPECT_GE(callee()->min_video_frames_received_per_track(),
1622 total_callee_video_frames_expected);
1623
1624 // We want to make sure nothing unexpected was received.
1625 if (media_expectations.caller_audio_expectation_ ==
1626 MediaExpectations::kExpectNoFrames) {
1627 EXPECT_EQ(caller()->audio_frames_received(),
1628 total_caller_audio_frames_expected);
1629 if (caller()->audio_frames_received() !=
1630 total_caller_audio_frames_expected) {
1631 expectations_correct = false;
1632 }
1633 }
1634 if (media_expectations.caller_video_expectation_ ==
1635 MediaExpectations::kExpectNoFrames) {
1636 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1637 total_caller_video_frames_expected);
1638 if (caller()->min_video_frames_received_per_track() !=
1639 total_caller_video_frames_expected) {
1640 expectations_correct = false;
1641 }
1642 }
1643 if (media_expectations.callee_audio_expectation_ ==
1644 MediaExpectations::kExpectNoFrames) {
1645 EXPECT_EQ(callee()->audio_frames_received(),
1646 total_callee_audio_frames_expected);
1647 if (callee()->audio_frames_received() !=
1648 total_callee_audio_frames_expected) {
1649 expectations_correct = false;
1650 }
1651 }
1652 if (media_expectations.callee_video_expectation_ ==
1653 MediaExpectations::kExpectNoFrames) {
1654 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1655 total_callee_video_frames_expected);
1656 if (callee()->min_video_frames_received_per_track() !=
1657 total_callee_video_frames_expected) {
1658 expectations_correct = false;
1659 }
1660 }
1661 return expectations_correct;
1662 }
1663
ClosePeerConnections()1664 void ClosePeerConnections() {
1665 caller()->pc()->Close();
1666 callee()->pc()->Close();
1667 }
1668
TestNegotiatedCipherSuite(const PeerConnectionFactory::Options & caller_options,const PeerConnectionFactory::Options & callee_options,int expected_cipher_suite)1669 void TestNegotiatedCipherSuite(
1670 const PeerConnectionFactory::Options& caller_options,
1671 const PeerConnectionFactory::Options& callee_options,
1672 int expected_cipher_suite) {
1673 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1674 callee_options));
1675 ConnectFakeSignaling();
1676 caller()->AddAudioVideoTracks();
1677 callee()->AddAudioVideoTracks();
1678 caller()->CreateAndSetAndSignalOffer();
1679 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1680 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1681 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
1682 // TODO(bugs.webrtc.org/9456): Fix it.
1683 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1684 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1685 expected_cipher_suite));
1686 }
1687
TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,bool remote_gcm_enabled,bool aes_ctr_enabled,int expected_cipher_suite)1688 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1689 bool remote_gcm_enabled,
1690 bool aes_ctr_enabled,
1691 int expected_cipher_suite) {
1692 PeerConnectionFactory::Options caller_options;
1693 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1694 local_gcm_enabled;
1695 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1696 aes_ctr_enabled;
1697 PeerConnectionFactory::Options callee_options;
1698 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1699 remote_gcm_enabled;
1700 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1701 aes_ctr_enabled;
1702 TestNegotiatedCipherSuite(caller_options, callee_options,
1703 expected_cipher_suite);
1704 }
1705
1706 protected:
1707 SdpSemantics sdp_semantics_;
1708
1709 private:
1710 // |ss_| is used by |network_thread_| so it must be destroyed later.
1711 std::unique_ptr<rtc::VirtualSocketServer> ss_;
1712 std::unique_ptr<rtc::FirewallSocketServer> fss_;
1713 // |network_thread_| and |worker_thread_| are used by both
1714 // |caller_| and |callee_| so they must be destroyed
1715 // later.
1716 std::unique_ptr<rtc::Thread> network_thread_;
1717 std::unique_ptr<rtc::Thread> worker_thread_;
1718 // The turn servers and turn customizers should be accessed & deleted on the
1719 // network thread to avoid a race with the socket read/write that occurs
1720 // on the network thread.
1721 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1722 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
1723 std::unique_ptr<PeerConnectionWrapper> caller_;
1724 std::unique_ptr<PeerConnectionWrapper> callee_;
1725 };
1726
1727 class PeerConnectionIntegrationTest
1728 : public PeerConnectionIntegrationBaseTest,
1729 public ::testing::WithParamInterface<SdpSemantics> {
1730 protected:
PeerConnectionIntegrationTest()1731 PeerConnectionIntegrationTest()
1732 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1733 };
1734
1735 // Fake clock must be set before threads are started to prevent race on
1736 // Set/GetClockForTesting().
1737 // To achieve that, multiple inheritance is used as a mixin pattern
1738 // where order of construction is finely controlled.
1739 // This also ensures peerconnection is closed before switching back to non-fake
1740 // clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1741 class FakeClockForTest : public rtc::ScopedFakeClock {
1742 protected:
FakeClockForTest()1743 FakeClockForTest() {
1744 // Some things use a time of "0" as a special value, so we need to start out
1745 // the fake clock at a nonzero time.
1746 // TODO(deadbeef): Fix this.
1747 AdvanceTime(webrtc::TimeDelta::Seconds(1));
1748 }
1749
1750 // Explicit handle.
FakeClock()1751 ScopedFakeClock& FakeClock() { return *this; }
1752 };
1753
1754 // Ensure FakeClockForTest is constructed first (see class for rationale).
1755 class PeerConnectionIntegrationTestWithFakeClock
1756 : public FakeClockForTest,
1757 public PeerConnectionIntegrationTest {};
1758
1759 class PeerConnectionIntegrationTestPlanB
1760 : public PeerConnectionIntegrationBaseTest {
1761 protected:
PeerConnectionIntegrationTestPlanB()1762 PeerConnectionIntegrationTestPlanB()
1763 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1764 };
1765
1766 class PeerConnectionIntegrationTestUnifiedPlan
1767 : public PeerConnectionIntegrationBaseTest {
1768 protected:
PeerConnectionIntegrationTestUnifiedPlan()1769 PeerConnectionIntegrationTestUnifiedPlan()
1770 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1771 };
1772
1773 // Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1774 // includes testing that the callback is invoked if an observer is connected
1775 // after the first packet has already been received.
TEST_P(PeerConnectionIntegrationTest,RtpReceiverObserverOnFirstPacketReceived)1776 TEST_P(PeerConnectionIntegrationTest,
1777 RtpReceiverObserverOnFirstPacketReceived) {
1778 ASSERT_TRUE(CreatePeerConnectionWrappers());
1779 ConnectFakeSignaling();
1780 caller()->AddAudioVideoTracks();
1781 callee()->AddAudioVideoTracks();
1782 // Start offer/answer exchange and wait for it to complete.
1783 caller()->CreateAndSetAndSignalOffer();
1784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1785 // Should be one receiver each for audio/video.
1786 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1787 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
1788 // Wait for all "first packet received" callbacks to be fired.
1789 EXPECT_TRUE_WAIT(
1790 absl::c_all_of(caller()->rtp_receiver_observers(),
1791 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1792 return o->first_packet_received();
1793 }),
1794 kMaxWaitForFramesMs);
1795 EXPECT_TRUE_WAIT(
1796 absl::c_all_of(callee()->rtp_receiver_observers(),
1797 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1798 return o->first_packet_received();
1799 }),
1800 kMaxWaitForFramesMs);
1801 // If new observers are set after the first packet was already received, the
1802 // callback should still be invoked.
1803 caller()->ResetRtpReceiverObservers();
1804 callee()->ResetRtpReceiverObservers();
1805 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1806 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
1807 EXPECT_TRUE(
1808 absl::c_all_of(caller()->rtp_receiver_observers(),
1809 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1810 return o->first_packet_received();
1811 }));
1812 EXPECT_TRUE(
1813 absl::c_all_of(callee()->rtp_receiver_observers(),
1814 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1815 return o->first_packet_received();
1816 }));
1817 }
1818
1819 class DummyDtmfObserver : public DtmfSenderObserverInterface {
1820 public:
DummyDtmfObserver()1821 DummyDtmfObserver() : completed_(false) {}
1822
1823 // Implements DtmfSenderObserverInterface.
OnToneChange(const std::string & tone)1824 void OnToneChange(const std::string& tone) override {
1825 tones_.push_back(tone);
1826 if (tone.empty()) {
1827 completed_ = true;
1828 }
1829 }
1830
tones() const1831 const std::vector<std::string>& tones() const { return tones_; }
completed() const1832 bool completed() const { return completed_; }
1833
1834 private:
1835 bool completed_;
1836 std::vector<std::string> tones_;
1837 };
1838
1839 // Assumes |sender| already has an audio track added and the offer/answer
1840 // exchange is done.
TestDtmfFromSenderToReceiver(PeerConnectionWrapper * sender,PeerConnectionWrapper * receiver)1841 void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1842 PeerConnectionWrapper* receiver) {
1843 // We should be able to get a DTMF sender from the local sender.
1844 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1845 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1846 ASSERT_TRUE(dtmf_sender);
1847 DummyDtmfObserver observer;
1848 dtmf_sender->RegisterObserver(&observer);
1849
1850 // Test the DtmfSender object just created.
1851 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1852 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1853
1854 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1855 std::vector<std::string> tones = {"1", "a", ""};
1856 EXPECT_EQ(tones, observer.tones());
1857 dtmf_sender->UnregisterObserver();
1858 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1859 }
1860
1861 // Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1862 // direction).
TEST_P(PeerConnectionIntegrationTest,DtmfSenderObserver)1863 TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
1864 ASSERT_TRUE(CreatePeerConnectionWrappers());
1865 ConnectFakeSignaling();
1866 // Only need audio for DTMF.
1867 caller()->AddAudioTrack();
1868 callee()->AddAudioTrack();
1869 caller()->CreateAndSetAndSignalOffer();
1870 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1871 // DTLS must finish before the DTMF sender can be used reliably.
1872 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1873 TestDtmfFromSenderToReceiver(caller(), callee());
1874 TestDtmfFromSenderToReceiver(callee(), caller());
1875 }
1876
1877 // Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1878 // between two connections, using DTLS-SRTP.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithDtls)1879 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
1880 ASSERT_TRUE(CreatePeerConnectionWrappers());
1881 ConnectFakeSignaling();
1882
1883 // Do normal offer/answer and wait for some frames to be received in each
1884 // direction.
1885 caller()->AddAudioVideoTracks();
1886 callee()->AddAudioVideoTracks();
1887 caller()->CreateAndSetAndSignalOffer();
1888 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1889 MediaExpectations media_expectations;
1890 media_expectations.ExpectBidirectionalAudioAndVideo();
1891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1892 EXPECT_METRIC_LE(
1893 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1894 webrtc::kEnumCounterKeyProtocolDtls));
1895 EXPECT_METRIC_EQ(
1896 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1897 webrtc::kEnumCounterKeyProtocolSdes));
1898 }
1899
1900 // Uses SDES instead of DTLS for key agreement.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSdes)1901 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
1902 PeerConnectionInterface::RTCConfiguration sdes_config;
1903 sdes_config.enable_dtls_srtp.emplace(false);
1904 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1905 ConnectFakeSignaling();
1906
1907 // Do normal offer/answer and wait for some frames to be received in each
1908 // direction.
1909 caller()->AddAudioVideoTracks();
1910 callee()->AddAudioVideoTracks();
1911 caller()->CreateAndSetAndSignalOffer();
1912 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1913 MediaExpectations media_expectations;
1914 media_expectations.ExpectBidirectionalAudioAndVideo();
1915 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1916 EXPECT_METRIC_LE(
1917 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1918 webrtc::kEnumCounterKeyProtocolSdes));
1919 EXPECT_METRIC_EQ(
1920 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1921 webrtc::kEnumCounterKeyProtocolDtls));
1922 }
1923
1924 // Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1925 // option to offer encrypted versions of all header extensions alongside the
1926 // unencrypted versions.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithEncryptedRtpHeaderExtensions)1927 TEST_P(PeerConnectionIntegrationTest,
1928 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1929 CryptoOptions crypto_options;
1930 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1931 PeerConnectionInterface::RTCConfiguration config;
1932 config.crypto_options = crypto_options;
1933 // Note: This allows offering >14 RTP header extensions.
1934 config.offer_extmap_allow_mixed = true;
1935 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1936 ConnectFakeSignaling();
1937
1938 // Do normal offer/answer and wait for some frames to be received in each
1939 // direction.
1940 caller()->AddAudioVideoTracks();
1941 callee()->AddAudioVideoTracks();
1942 caller()->CreateAndSetAndSignalOffer();
1943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1944 MediaExpectations media_expectations;
1945 media_expectations.ExpectBidirectionalAudioAndVideo();
1946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1947 }
1948
1949 // Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1950 // certificate once the DTLS handshake has finished.
TEST_P(PeerConnectionIntegrationTest,GetRemoteAudioSSLCertificateReturnsExchangedCertificate)1951 TEST_P(PeerConnectionIntegrationTest,
1952 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1953 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1954 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1955 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1956 return pc->GetRemoteAudioSSLCertificate();
1957 };
1958 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1959 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1960 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1961 return pc->GetRemoteAudioSSLCertChain();
1962 };
1963
1964 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1965 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1966
1967 // Configure each side with a known certificate so they can be compared later.
1968 PeerConnectionInterface::RTCConfiguration caller_config;
1969 caller_config.enable_dtls_srtp.emplace(true);
1970 caller_config.certificates.push_back(caller_cert);
1971 PeerConnectionInterface::RTCConfiguration callee_config;
1972 callee_config.enable_dtls_srtp.emplace(true);
1973 callee_config.certificates.push_back(callee_cert);
1974 ASSERT_TRUE(
1975 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1976 ConnectFakeSignaling();
1977
1978 // When first initialized, there should not be a remote SSL certificate (and
1979 // calling this method should not crash).
1980 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1981 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
1982 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1983 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
1984
1985 caller()->AddAudioTrack();
1986 callee()->AddAudioTrack();
1987 caller()->CreateAndSetAndSignalOffer();
1988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1989 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1990
1991 // Once DTLS has been connected, each side should return the other's SSL
1992 // certificate when calling GetRemoteAudioSSLCertificate.
1993
1994 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1995 ASSERT_TRUE(caller_remote_cert);
1996 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
1997 caller_remote_cert->ToPEMString());
1998
1999 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
2000 ASSERT_TRUE(callee_remote_cert);
2001 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
2002 callee_remote_cert->ToPEMString());
2003
2004 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2005 ASSERT_TRUE(caller_remote_cert_chain);
2006 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2007 auto remote_cert = &caller_remote_cert_chain->Get(0);
2008 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
2009 remote_cert->ToPEMString());
2010
2011 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2012 ASSERT_TRUE(callee_remote_cert_chain);
2013 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2014 remote_cert = &callee_remote_cert_chain->Get(0);
2015 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
2016 remote_cert->ToPEMString());
2017 }
2018
2019 // This test sets up a call between two parties with a source resolution of
2020 // 1280x720 and verifies that a 16:9 aspect ratio is received.
TEST_P(PeerConnectionIntegrationTest,Send1280By720ResolutionAndReceive16To9AspectRatio)2021 TEST_P(PeerConnectionIntegrationTest,
2022 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2023 ASSERT_TRUE(CreatePeerConnectionWrappers());
2024 ConnectFakeSignaling();
2025
2026 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2027 webrtc::FakePeriodicVideoSource::Config config;
2028 config.width = 1280;
2029 config.height = 720;
2030 config.timestamp_offset_ms = rtc::TimeMillis();
2031 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2032 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
2033
2034 // Do normal offer/answer and wait for at least one frame to be received in
2035 // each direction.
2036 caller()->CreateAndSetAndSignalOffer();
2037 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2038 callee()->min_video_frames_received_per_track() > 0,
2039 kMaxWaitForFramesMs);
2040
2041 // Check rendered aspect ratio.
2042 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2043 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2044 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2045 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2046 }
2047
2048 // This test sets up an one-way call, with media only from caller to
2049 // callee.
TEST_P(PeerConnectionIntegrationTest,OneWayMediaCall)2050 TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
2051 ASSERT_TRUE(CreatePeerConnectionWrappers());
2052 ConnectFakeSignaling();
2053 caller()->AddAudioVideoTracks();
2054 caller()->CreateAndSetAndSignalOffer();
2055 MediaExpectations media_expectations;
2056 media_expectations.CalleeExpectsSomeAudioAndVideo();
2057 media_expectations.CallerExpectsNoAudio();
2058 media_expectations.CallerExpectsNoVideo();
2059 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2060 }
2061
2062 // Tests that send only works without the caller having a decoder factory and
2063 // the callee having an encoder factory.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSendOnlyVideo)2064 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2065 ASSERT_TRUE(
2066 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2067 ConnectFakeSignaling();
2068 // Add one-directional video, from caller to callee.
2069 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2070 caller()->CreateLocalVideoTrack();
2071 caller()->AddTrack(caller_track);
2072 PeerConnectionInterface::RTCOfferAnswerOptions options;
2073 options.offer_to_receive_video = 0;
2074 caller()->SetOfferAnswerOptions(options);
2075 caller()->CreateAndSetAndSignalOffer();
2076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2077 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2078
2079 // Expect video to be received in one direction.
2080 MediaExpectations media_expectations;
2081 media_expectations.CallerExpectsNoVideo();
2082 media_expectations.CalleeExpectsSomeVideo();
2083
2084 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2085 }
2086
2087 // Tests that receive only works without the caller having an encoder factory
2088 // and the callee having a decoder factory.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithReceiveOnlyVideo)2089 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2090 ASSERT_TRUE(
2091 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2092 ConnectFakeSignaling();
2093 // Add one-directional video, from callee to caller.
2094 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2095 callee()->CreateLocalVideoTrack();
2096 callee()->AddTrack(callee_track);
2097 PeerConnectionInterface::RTCOfferAnswerOptions options;
2098 options.offer_to_receive_video = 1;
2099 caller()->SetOfferAnswerOptions(options);
2100 caller()->CreateAndSetAndSignalOffer();
2101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2102 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2103
2104 // Expect video to be received in one direction.
2105 MediaExpectations media_expectations;
2106 media_expectations.CallerExpectsSomeVideo();
2107 media_expectations.CalleeExpectsNoVideo();
2108
2109 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2110 }
2111
TEST_P(PeerConnectionIntegrationTest,EndToEndCallAddReceiveVideoToSendOnlyCall)2112 TEST_P(PeerConnectionIntegrationTest,
2113 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
2116 // Add one-directional video, from caller to callee.
2117 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2118 caller()->CreateLocalVideoTrack();
2119 caller()->AddTrack(caller_track);
2120 caller()->CreateAndSetAndSignalOffer();
2121 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2122
2123 // Add receive video.
2124 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2125 callee()->CreateLocalVideoTrack();
2126 callee()->AddTrack(callee_track);
2127 caller()->CreateAndSetAndSignalOffer();
2128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2129
2130 // Ensure that video frames are received end-to-end.
2131 MediaExpectations media_expectations;
2132 media_expectations.ExpectBidirectionalVideo();
2133 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2134 }
2135
TEST_P(PeerConnectionIntegrationTest,EndToEndCallAddSendVideoToReceiveOnlyCall)2136 TEST_P(PeerConnectionIntegrationTest,
2137 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
2140 // Add one-directional video, from callee to caller.
2141 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2142 callee()->CreateLocalVideoTrack();
2143 callee()->AddTrack(callee_track);
2144 caller()->CreateAndSetAndSignalOffer();
2145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2146
2147 // Add send video.
2148 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2149 caller()->CreateLocalVideoTrack();
2150 caller()->AddTrack(caller_track);
2151 caller()->CreateAndSetAndSignalOffer();
2152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2153
2154 // Expect video to be received in one direction.
2155 MediaExpectations media_expectations;
2156 media_expectations.ExpectBidirectionalVideo();
2157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2158 }
2159
TEST_P(PeerConnectionIntegrationTest,EndToEndCallRemoveReceiveVideoFromSendReceiveCall)2160 TEST_P(PeerConnectionIntegrationTest,
2161 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2162 ASSERT_TRUE(CreatePeerConnectionWrappers());
2163 ConnectFakeSignaling();
2164 // Add send video, from caller to callee.
2165 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2166 caller()->CreateLocalVideoTrack();
2167 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2168 caller()->AddTrack(caller_track);
2169 // Add receive video, from callee to caller.
2170 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2171 callee()->CreateLocalVideoTrack();
2172
2173 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2174 callee()->AddTrack(callee_track);
2175 caller()->CreateAndSetAndSignalOffer();
2176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2177
2178 // Remove receive video (i.e., callee sender track).
2179 callee()->pc()->RemoveTrack(callee_sender);
2180
2181 caller()->CreateAndSetAndSignalOffer();
2182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2183
2184 // Expect one-directional video.
2185 MediaExpectations media_expectations;
2186 media_expectations.CallerExpectsNoVideo();
2187 media_expectations.CalleeExpectsSomeVideo();
2188
2189 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2190 }
2191
TEST_P(PeerConnectionIntegrationTest,EndToEndCallRemoveSendVideoFromSendReceiveCall)2192 TEST_P(PeerConnectionIntegrationTest,
2193 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2194 ASSERT_TRUE(CreatePeerConnectionWrappers());
2195 ConnectFakeSignaling();
2196 // Add send video, from caller to callee.
2197 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2198 caller()->CreateLocalVideoTrack();
2199 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2200 caller()->AddTrack(caller_track);
2201 // Add receive video, from callee to caller.
2202 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2203 callee()->CreateLocalVideoTrack();
2204
2205 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2206 callee()->AddTrack(callee_track);
2207 caller()->CreateAndSetAndSignalOffer();
2208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2209
2210 // Remove send video (i.e., caller sender track).
2211 caller()->pc()->RemoveTrack(caller_sender);
2212
2213 caller()->CreateAndSetAndSignalOffer();
2214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2215
2216 // Expect one-directional video.
2217 MediaExpectations media_expectations;
2218 media_expectations.CalleeExpectsNoVideo();
2219 media_expectations.CallerExpectsSomeVideo();
2220
2221 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2222 }
2223
2224 // This test sets up a audio call initially, with the callee rejecting video
2225 // initially. Then later the callee decides to upgrade to audio/video, and
2226 // initiates a new offer/answer exchange.
TEST_P(PeerConnectionIntegrationTest,AudioToVideoUpgrade)2227 TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
2228 ASSERT_TRUE(CreatePeerConnectionWrappers());
2229 ConnectFakeSignaling();
2230 // Initially, offer an audio/video stream from the caller, but refuse to
2231 // send/receive video on the callee side.
2232 caller()->AddAudioVideoTracks();
2233 callee()->AddAudioTrack();
2234 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2235 PeerConnectionInterface::RTCOfferAnswerOptions options;
2236 options.offer_to_receive_video = 0;
2237 callee()->SetOfferAnswerOptions(options);
2238 } else {
2239 callee()->SetRemoteOfferHandler([this] {
2240 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2241 });
2242 }
2243 // Do offer/answer and make sure audio is still received end-to-end.
2244 caller()->CreateAndSetAndSignalOffer();
2245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2246 {
2247 MediaExpectations media_expectations;
2248 media_expectations.ExpectBidirectionalAudio();
2249 media_expectations.ExpectNoVideo();
2250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2251 }
2252 // Sanity check that the callee's description has a rejected video section.
2253 ASSERT_NE(nullptr, callee()->pc()->local_description());
2254 const ContentInfo* callee_video_content =
2255 GetFirstVideoContent(callee()->pc()->local_description()->description());
2256 ASSERT_NE(nullptr, callee_video_content);
2257 EXPECT_TRUE(callee_video_content->rejected);
2258
2259 // Now negotiate with video and ensure negotiation succeeds, with video
2260 // frames and additional audio frames being received.
2261 callee()->AddVideoTrack();
2262 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2263 PeerConnectionInterface::RTCOfferAnswerOptions options;
2264 options.offer_to_receive_video = 1;
2265 callee()->SetOfferAnswerOptions(options);
2266 } else {
2267 callee()->SetRemoteOfferHandler(nullptr);
2268 caller()->SetRemoteOfferHandler([this] {
2269 // The caller creates a new transceiver to receive video on when receiving
2270 // the offer, but by default it is send only.
2271 auto transceivers = caller()->pc()->GetTransceivers();
2272 ASSERT_EQ(3U, transceivers.size());
2273 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2274 transceivers[2]->receiver()->media_type());
2275 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2276 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2277 });
2278 }
2279 callee()->CreateAndSetAndSignalOffer();
2280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2281 {
2282 // Expect additional audio frames to be received after the upgrade.
2283 MediaExpectations media_expectations;
2284 media_expectations.ExpectBidirectionalAudioAndVideo();
2285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2286 }
2287 }
2288
2289 // Simpler than the above test; just add an audio track to an established
2290 // video-only connection.
TEST_P(PeerConnectionIntegrationTest,AddAudioToVideoOnlyCall)2291 TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
2292 ASSERT_TRUE(CreatePeerConnectionWrappers());
2293 ConnectFakeSignaling();
2294 // Do initial offer/answer with just a video track.
2295 caller()->AddVideoTrack();
2296 callee()->AddVideoTrack();
2297 caller()->CreateAndSetAndSignalOffer();
2298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2299 // Now add an audio track and do another offer/answer.
2300 caller()->AddAudioTrack();
2301 callee()->AddAudioTrack();
2302 caller()->CreateAndSetAndSignalOffer();
2303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2304 // Ensure both audio and video frames are received end-to-end.
2305 MediaExpectations media_expectations;
2306 media_expectations.ExpectBidirectionalAudioAndVideo();
2307 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2308 }
2309
2310 // This test sets up a call that's transferred to a new caller with a different
2311 // DTLS fingerprint.
TEST_P(PeerConnectionIntegrationTest,CallTransferredForCallee)2312 TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
2313 ASSERT_TRUE(CreatePeerConnectionWrappers());
2314 ConnectFakeSignaling();
2315 caller()->AddAudioVideoTracks();
2316 callee()->AddAudioVideoTracks();
2317 caller()->CreateAndSetAndSignalOffer();
2318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2319
2320 // Keep the original peer around which will still send packets to the
2321 // receiving client. These SRTP packets will be dropped.
2322 std::unique_ptr<PeerConnectionWrapper> original_peer(
2323 SetCallerPcWrapperAndReturnCurrent(
2324 CreatePeerConnectionWrapperWithAlternateKey().release()));
2325 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2326 // directly above.
2327 original_peer->pc()->Close();
2328
2329 ConnectFakeSignaling();
2330 caller()->AddAudioVideoTracks();
2331 caller()->CreateAndSetAndSignalOffer();
2332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2333 // Wait for some additional frames to be transmitted end-to-end.
2334 MediaExpectations media_expectations;
2335 media_expectations.ExpectBidirectionalAudioAndVideo();
2336 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2337 }
2338
2339 // This test sets up a call that's transferred to a new callee with a different
2340 // DTLS fingerprint.
TEST_P(PeerConnectionIntegrationTest,CallTransferredForCaller)2341 TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
2342 ASSERT_TRUE(CreatePeerConnectionWrappers());
2343 ConnectFakeSignaling();
2344 caller()->AddAudioVideoTracks();
2345 callee()->AddAudioVideoTracks();
2346 caller()->CreateAndSetAndSignalOffer();
2347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2348
2349 // Keep the original peer around which will still send packets to the
2350 // receiving client. These SRTP packets will be dropped.
2351 std::unique_ptr<PeerConnectionWrapper> original_peer(
2352 SetCalleePcWrapperAndReturnCurrent(
2353 CreatePeerConnectionWrapperWithAlternateKey().release()));
2354 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2355 // directly above.
2356 original_peer->pc()->Close();
2357
2358 ConnectFakeSignaling();
2359 callee()->AddAudioVideoTracks();
2360 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2361 caller()->CreateAndSetAndSignalOffer();
2362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2363 // Wait for some additional frames to be transmitted end-to-end.
2364 MediaExpectations media_expectations;
2365 media_expectations.ExpectBidirectionalAudioAndVideo();
2366 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2367 }
2368
2369 // This test sets up a non-bundled call and negotiates bundling at the same
2370 // time as starting an ICE restart. When bundling is in effect in the restart,
2371 // the DTLS-SRTP context should be successfully reset.
TEST_P(PeerConnectionIntegrationTest,BundlingEnabledWhileIceRestartOccurs)2372 TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
2373 ASSERT_TRUE(CreatePeerConnectionWrappers());
2374 ConnectFakeSignaling();
2375
2376 caller()->AddAudioVideoTracks();
2377 callee()->AddAudioVideoTracks();
2378 // Remove the bundle group from the SDP received by the callee.
2379 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2380 desc->RemoveGroupByName("BUNDLE");
2381 });
2382 caller()->CreateAndSetAndSignalOffer();
2383 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2384 {
2385 MediaExpectations media_expectations;
2386 media_expectations.ExpectBidirectionalAudioAndVideo();
2387 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2388 }
2389 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2390 callee()->SetReceivedSdpMunger(nullptr);
2391 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2392 caller()->CreateAndSetAndSignalOffer();
2393 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2394
2395 // Expect additional frames to be received after the ICE restart.
2396 {
2397 MediaExpectations media_expectations;
2398 media_expectations.ExpectBidirectionalAudioAndVideo();
2399 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2400 }
2401 }
2402
2403 // Test CVO (Coordination of Video Orientation). If a video source is rotated
2404 // and both peers support the CVO RTP header extension, the actual video frames
2405 // don't need to be encoded in different resolutions, since the rotation is
2406 // communicated through the RTP header extension.
TEST_P(PeerConnectionIntegrationTest,RotatedVideoWithCVOExtension)2407 TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
2408 ASSERT_TRUE(CreatePeerConnectionWrappers());
2409 ConnectFakeSignaling();
2410 // Add rotated video tracks.
2411 caller()->AddTrack(
2412 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
2413 callee()->AddTrack(
2414 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2415
2416 // Wait for video frames to be received by both sides.
2417 caller()->CreateAndSetAndSignalOffer();
2418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2419 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2420 callee()->min_video_frames_received_per_track() > 0,
2421 kMaxWaitForFramesMs);
2422
2423 // Ensure that the aspect ratio is unmodified.
2424 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2425 // not just assumed.
2426 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2427 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2428 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2429 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2430 // Ensure that the CVO bits were surfaced to the renderer.
2431 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2432 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2433 }
2434
2435 // Test that when the CVO extension isn't supported, video is rotated the
2436 // old-fashioned way, by encoding rotated frames.
TEST_P(PeerConnectionIntegrationTest,RotatedVideoWithoutCVOExtension)2437 TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
2438 ASSERT_TRUE(CreatePeerConnectionWrappers());
2439 ConnectFakeSignaling();
2440 // Add rotated video tracks.
2441 caller()->AddTrack(
2442 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
2443 callee()->AddTrack(
2444 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2445
2446 // Remove the CVO extension from the offered SDP.
2447 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2448 cricket::VideoContentDescription* video =
2449 GetFirstVideoContentDescription(desc);
2450 video->ClearRtpHeaderExtensions();
2451 });
2452 // Wait for video frames to be received by both sides.
2453 caller()->CreateAndSetAndSignalOffer();
2454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2455 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2456 callee()->min_video_frames_received_per_track() > 0,
2457 kMaxWaitForFramesMs);
2458
2459 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2460 // rotation.
2461 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2462 // not just assumed.
2463 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2464 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2465 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2466 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2467 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2468 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2469 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2470 }
2471
2472 // Test that if the answerer rejects the audio m= section, no audio is sent or
2473 // received, but video still can be.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsAudioSection)2474 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
2475 ASSERT_TRUE(CreatePeerConnectionWrappers());
2476 ConnectFakeSignaling();
2477 caller()->AddAudioVideoTracks();
2478 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2479 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2480 // it will reject the audio m= section completely.
2481 PeerConnectionInterface::RTCOfferAnswerOptions options;
2482 options.offer_to_receive_audio = 0;
2483 callee()->SetOfferAnswerOptions(options);
2484 } else {
2485 // Stopping the audio RtpTransceiver will cause the media section to be
2486 // rejected in the answer.
2487 callee()->SetRemoteOfferHandler([this] {
2488 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2489 });
2490 }
2491 callee()->AddTrack(callee()->CreateLocalVideoTrack());
2492 // Do offer/answer and wait for successful end-to-end video frames.
2493 caller()->CreateAndSetAndSignalOffer();
2494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2495 MediaExpectations media_expectations;
2496 media_expectations.ExpectBidirectionalVideo();
2497 media_expectations.ExpectNoAudio();
2498 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2499
2500 // Sanity check that the callee's description has a rejected audio section.
2501 ASSERT_NE(nullptr, callee()->pc()->local_description());
2502 const ContentInfo* callee_audio_content =
2503 GetFirstAudioContent(callee()->pc()->local_description()->description());
2504 ASSERT_NE(nullptr, callee_audio_content);
2505 EXPECT_TRUE(callee_audio_content->rejected);
2506 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2507 // The caller's transceiver should have stopped after receiving the answer.
2508 EXPECT_TRUE(caller()
2509 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2510 ->stopped());
2511 }
2512 }
2513
2514 // Test that if the answerer rejects the video m= section, no video is sent or
2515 // received, but audio still can be.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsVideoSection)2516 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
2517 ASSERT_TRUE(CreatePeerConnectionWrappers());
2518 ConnectFakeSignaling();
2519 caller()->AddAudioVideoTracks();
2520 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2521 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2522 // it will reject the video m= section completely.
2523 PeerConnectionInterface::RTCOfferAnswerOptions options;
2524 options.offer_to_receive_video = 0;
2525 callee()->SetOfferAnswerOptions(options);
2526 } else {
2527 // Stopping the video RtpTransceiver will cause the media section to be
2528 // rejected in the answer.
2529 callee()->SetRemoteOfferHandler([this] {
2530 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2531 });
2532 }
2533 callee()->AddTrack(callee()->CreateLocalAudioTrack());
2534 // Do offer/answer and wait for successful end-to-end audio frames.
2535 caller()->CreateAndSetAndSignalOffer();
2536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2537 MediaExpectations media_expectations;
2538 media_expectations.ExpectBidirectionalAudio();
2539 media_expectations.ExpectNoVideo();
2540 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2541
2542 // Sanity check that the callee's description has a rejected video section.
2543 ASSERT_NE(nullptr, callee()->pc()->local_description());
2544 const ContentInfo* callee_video_content =
2545 GetFirstVideoContent(callee()->pc()->local_description()->description());
2546 ASSERT_NE(nullptr, callee_video_content);
2547 EXPECT_TRUE(callee_video_content->rejected);
2548 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2549 // The caller's transceiver should have stopped after receiving the answer.
2550 EXPECT_TRUE(caller()
2551 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2552 ->stopped());
2553 }
2554 }
2555
2556 // Test that if the answerer rejects both audio and video m= sections, nothing
2557 // bad happens.
2558 // TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2559 // test anything but the fact that negotiation succeeds, which doesn't mean
2560 // much.
TEST_P(PeerConnectionIntegrationTest,AnswererRejectsAudioAndVideoSections)2561 TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
2562 ASSERT_TRUE(CreatePeerConnectionWrappers());
2563 ConnectFakeSignaling();
2564 caller()->AddAudioVideoTracks();
2565 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2566 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2567 // will reject both audio and video m= sections.
2568 PeerConnectionInterface::RTCOfferAnswerOptions options;
2569 options.offer_to_receive_audio = 0;
2570 options.offer_to_receive_video = 0;
2571 callee()->SetOfferAnswerOptions(options);
2572 } else {
2573 callee()->SetRemoteOfferHandler([this] {
2574 // Stopping all transceivers will cause all media sections to be rejected.
2575 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
2576 transceiver->Stop();
2577 }
2578 });
2579 }
2580 // Do offer/answer and wait for stable signaling state.
2581 caller()->CreateAndSetAndSignalOffer();
2582 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2583
2584 // Sanity check that the callee's description has rejected m= sections.
2585 ASSERT_NE(nullptr, callee()->pc()->local_description());
2586 const ContentInfo* callee_audio_content =
2587 GetFirstAudioContent(callee()->pc()->local_description()->description());
2588 ASSERT_NE(nullptr, callee_audio_content);
2589 EXPECT_TRUE(callee_audio_content->rejected);
2590 const ContentInfo* callee_video_content =
2591 GetFirstVideoContent(callee()->pc()->local_description()->description());
2592 ASSERT_NE(nullptr, callee_video_content);
2593 EXPECT_TRUE(callee_video_content->rejected);
2594 }
2595
2596 // This test sets up an audio and video call between two parties. After the
2597 // call runs for a while, the caller sends an updated offer with video being
2598 // rejected. Once the re-negotiation is done, the video flow should stop and
2599 // the audio flow should continue.
TEST_P(PeerConnectionIntegrationTest,VideoRejectedInSubsequentOffer)2600 TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
2601 ASSERT_TRUE(CreatePeerConnectionWrappers());
2602 ConnectFakeSignaling();
2603 caller()->AddAudioVideoTracks();
2604 callee()->AddAudioVideoTracks();
2605 caller()->CreateAndSetAndSignalOffer();
2606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2607 {
2608 MediaExpectations media_expectations;
2609 media_expectations.ExpectBidirectionalAudioAndVideo();
2610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2611 }
2612 // Renegotiate, rejecting the video m= section.
2613 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2614 caller()->SetGeneratedSdpMunger(
2615 [](cricket::SessionDescription* description) {
2616 for (cricket::ContentInfo& content : description->contents()) {
2617 if (cricket::IsVideoContent(&content)) {
2618 content.rejected = true;
2619 }
2620 }
2621 });
2622 } else {
2623 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2624 }
2625 caller()->CreateAndSetAndSignalOffer();
2626 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2627
2628 // Sanity check that the caller's description has a rejected video section.
2629 ASSERT_NE(nullptr, caller()->pc()->local_description());
2630 const ContentInfo* caller_video_content =
2631 GetFirstVideoContent(caller()->pc()->local_description()->description());
2632 ASSERT_NE(nullptr, caller_video_content);
2633 EXPECT_TRUE(caller_video_content->rejected);
2634 // Wait for some additional audio frames to be received.
2635 {
2636 MediaExpectations media_expectations;
2637 media_expectations.ExpectBidirectionalAudio();
2638 media_expectations.ExpectNoVideo();
2639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2640 }
2641 }
2642
2643 // Do one offer/answer with audio, another that disables it (rejecting the m=
2644 // section), and another that re-enables it. Regression test for:
2645 // bugs.webrtc.org/6023
TEST_F(PeerConnectionIntegrationTestPlanB,EnableAudioAfterRejecting)2646 TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2647 ASSERT_TRUE(CreatePeerConnectionWrappers());
2648 ConnectFakeSignaling();
2649
2650 // Add audio track, do normal offer/answer.
2651 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2652 caller()->CreateLocalAudioTrack();
2653 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2654 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2655 caller()->CreateAndSetAndSignalOffer();
2656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2657
2658 // Remove audio track, and set offer_to_receive_audio to false to cause the
2659 // m= section to be completely disabled, not just "recvonly".
2660 caller()->pc()->RemoveTrack(sender);
2661 PeerConnectionInterface::RTCOfferAnswerOptions options;
2662 options.offer_to_receive_audio = 0;
2663 caller()->SetOfferAnswerOptions(options);
2664 caller()->CreateAndSetAndSignalOffer();
2665 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2666
2667 // Add the audio track again, expecting negotiation to succeed and frames to
2668 // flow.
2669 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2670 options.offer_to_receive_audio = 1;
2671 caller()->SetOfferAnswerOptions(options);
2672 caller()->CreateAndSetAndSignalOffer();
2673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2674
2675 MediaExpectations media_expectations;
2676 media_expectations.CalleeExpectsSomeAudio();
2677 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2678 }
2679
2680 // Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2681 // is needed to support legacy endpoints.
2682 // TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2683 // add a test for an end-to-end test without MID signaling either (basically,
2684 // the minimum acceptable SDP).
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithoutSsrcOrMsidSignaling)2685 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
2686 ASSERT_TRUE(CreatePeerConnectionWrappers());
2687 ConnectFakeSignaling();
2688 // Add audio and video, testing that packets can be demuxed on payload type.
2689 caller()->AddAudioVideoTracks();
2690 callee()->AddAudioVideoTracks();
2691 // Remove SSRCs and MSIDs from the received offer SDP.
2692 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2693 caller()->CreateAndSetAndSignalOffer();
2694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2695 MediaExpectations media_expectations;
2696 media_expectations.ExpectBidirectionalAudioAndVideo();
2697 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2698 }
2699
2700 // Basic end-to-end test, without SSRC signaling. This means that the track
2701 // was created properly and frames are delivered when the MSIDs are communicated
2702 // with a=msid lines and no a=ssrc lines.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithoutSsrcSignaling)2703 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2704 EndToEndCallWithoutSsrcSignaling) {
2705 const char kStreamId[] = "streamId";
2706 ASSERT_TRUE(CreatePeerConnectionWrappers());
2707 ConnectFakeSignaling();
2708 // Add just audio tracks.
2709 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2710 callee()->AddAudioTrack();
2711
2712 // Remove SSRCs from the received offer SDP.
2713 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2714 caller()->CreateAndSetAndSignalOffer();
2715 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2716 MediaExpectations media_expectations;
2717 media_expectations.ExpectBidirectionalAudio();
2718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2719 }
2720
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallAddReceiveVideoToSendOnlyCall)2721 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2722 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2723 ASSERT_TRUE(CreatePeerConnectionWrappers());
2724 ConnectFakeSignaling();
2725 // Add one-directional video, from caller to callee.
2726 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2727 caller()->CreateLocalVideoTrack();
2728
2729 RtpTransceiverInit video_transceiver_init;
2730 video_transceiver_init.stream_ids = {"video1"};
2731 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2732 auto video_sender =
2733 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2734 caller()->CreateAndSetAndSignalOffer();
2735 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2736
2737 // Add receive direction.
2738 video_sender->SetDirection(RtpTransceiverDirection::kSendRecv);
2739
2740 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2741 callee()->CreateLocalVideoTrack();
2742
2743 callee()->AddTrack(callee_track);
2744 caller()->CreateAndSetAndSignalOffer();
2745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2746 // Ensure that video frames are received end-to-end.
2747 MediaExpectations media_expectations;
2748 media_expectations.ExpectBidirectionalVideo();
2749 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2750 }
2751
2752 // Tests that video flows between multiple video tracks when SSRCs are not
2753 // signaled. This exercises the MID RTP header extension which is needed to
2754 // demux the incoming video tracks.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc)2755 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2756 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2757 ASSERT_TRUE(CreatePeerConnectionWrappers());
2758 ConnectFakeSignaling();
2759 caller()->AddVideoTrack();
2760 caller()->AddVideoTrack();
2761 callee()->AddVideoTrack();
2762 callee()->AddVideoTrack();
2763
2764 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2765 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2766 caller()->CreateAndSetAndSignalOffer();
2767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2768 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2769 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2770
2771 // Expect video to be received in both directions on both tracks.
2772 MediaExpectations media_expectations;
2773 media_expectations.ExpectBidirectionalVideo();
2774 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2775 }
2776
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,NoStreamsMsidLinePresent)2777 TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2778 ASSERT_TRUE(CreatePeerConnectionWrappers());
2779 ConnectFakeSignaling();
2780 caller()->AddAudioTrack();
2781 caller()->AddVideoTrack();
2782 caller()->CreateAndSetAndSignalOffer();
2783 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2784 auto callee_receivers = callee()->pc()->GetReceivers();
2785 ASSERT_EQ(2u, callee_receivers.size());
2786 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2787 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2788 }
2789
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,NoStreamsMsidLineMissing)2790 TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2791 ASSERT_TRUE(CreatePeerConnectionWrappers());
2792 ConnectFakeSignaling();
2793 caller()->AddAudioTrack();
2794 caller()->AddVideoTrack();
2795 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2796 caller()->CreateAndSetAndSignalOffer();
2797 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2798 auto callee_receivers = callee()->pc()->GetReceivers();
2799 ASSERT_EQ(2u, callee_receivers.size());
2800 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2801 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2802 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2803 callee_receivers[1]->stream_ids()[0]);
2804 EXPECT_EQ(callee_receivers[0]->streams()[0],
2805 callee_receivers[1]->streams()[0]);
2806 }
2807
2808 // Test that if two video tracks are sent (from caller to callee, in this test),
2809 // they're transmitted correctly end-to-end.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithTwoVideoTracks)2810 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
2811 ASSERT_TRUE(CreatePeerConnectionWrappers());
2812 ConnectFakeSignaling();
2813 // Add one audio/video stream, and one video-only stream.
2814 caller()->AddAudioVideoTracks();
2815 caller()->AddVideoTrack();
2816 caller()->CreateAndSetAndSignalOffer();
2817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2818 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
2819
2820 MediaExpectations media_expectations;
2821 media_expectations.CalleeExpectsSomeAudioAndVideo();
2822 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2823 }
2824
MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription * desc)2825 static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2826 bool first = true;
2827 for (cricket::ContentInfo& content : desc->contents()) {
2828 if (first) {
2829 first = false;
2830 continue;
2831 }
2832 content.bundle_only = true;
2833 }
2834 first = true;
2835 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2836 if (first) {
2837 first = false;
2838 continue;
2839 }
2840 transport.description.ice_ufrag.clear();
2841 transport.description.ice_pwd.clear();
2842 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2843 transport.description.identity_fingerprint.reset(nullptr);
2844 }
2845 }
2846
2847 // Test that if applying a true "max bundle" offer, which uses ports of 0,
2848 // "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2849 // "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2850 // successfully and media flows.
2851 // TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2852 // TODO(deadbeef): Won't need this test once we start generating actual
2853 // standards-compliant SDP.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSpecCompliantMaxBundleOffer)2854 TEST_P(PeerConnectionIntegrationTest,
2855 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2856 ASSERT_TRUE(CreatePeerConnectionWrappers());
2857 ConnectFakeSignaling();
2858 caller()->AddAudioVideoTracks();
2859 callee()->AddAudioVideoTracks();
2860 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2861 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2862 // but the first m= section.
2863 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2864 caller()->CreateAndSetAndSignalOffer();
2865 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2866 MediaExpectations media_expectations;
2867 media_expectations.ExpectBidirectionalAudioAndVideo();
2868 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2869 }
2870
2871 // Test that we can receive the audio output level from a remote audio track.
2872 // TODO(deadbeef): Use a fake audio source and verify that the output level is
2873 // exactly what the source on the other side was configured with.
TEST_P(PeerConnectionIntegrationTest,GetAudioOutputLevelStatsWithOldStatsApi)2874 TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
2875 ASSERT_TRUE(CreatePeerConnectionWrappers());
2876 ConnectFakeSignaling();
2877 // Just add an audio track.
2878 caller()->AddAudioTrack();
2879 caller()->CreateAndSetAndSignalOffer();
2880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2881
2882 // Get the audio output level stats. Note that the level is not available
2883 // until an RTCP packet has been received.
2884 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
2885 kMaxWaitForFramesMs);
2886 }
2887
2888 // Test that an audio input level is reported.
2889 // TODO(deadbeef): Use a fake audio source and verify that the input level is
2890 // exactly what the source was configured with.
TEST_P(PeerConnectionIntegrationTest,GetAudioInputLevelStatsWithOldStatsApi)2891 TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
2892 ASSERT_TRUE(CreatePeerConnectionWrappers());
2893 ConnectFakeSignaling();
2894 // Just add an audio track.
2895 caller()->AddAudioTrack();
2896 caller()->CreateAndSetAndSignalOffer();
2897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2898
2899 // Get the audio input level stats. The level should be available very
2900 // soon after the test starts.
2901 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
2902 kMaxWaitForStatsMs);
2903 }
2904
2905 // Test that we can get incoming byte counts from both audio and video tracks.
TEST_P(PeerConnectionIntegrationTest,GetBytesReceivedStatsWithOldStatsApi)2906 TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
2907 ASSERT_TRUE(CreatePeerConnectionWrappers());
2908 ConnectFakeSignaling();
2909 caller()->AddAudioVideoTracks();
2910 // Do offer/answer, wait for the callee to receive some frames.
2911 caller()->CreateAndSetAndSignalOffer();
2912 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2913
2914 MediaExpectations media_expectations;
2915 media_expectations.CalleeExpectsSomeAudioAndVideo();
2916 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2917
2918 // Get a handle to the remote tracks created, so they can be used as GetStats
2919 // filters.
2920 for (const auto& receiver : callee()->pc()->GetReceivers()) {
2921 // We received frames, so we definitely should have nonzero "received bytes"
2922 // stats at this point.
2923 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2924 0);
2925 }
2926 }
2927
2928 // Test that we can get outgoing byte counts from both audio and video tracks.
TEST_P(PeerConnectionIntegrationTest,GetBytesSentStatsWithOldStatsApi)2929 TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
2930 ASSERT_TRUE(CreatePeerConnectionWrappers());
2931 ConnectFakeSignaling();
2932 auto audio_track = caller()->CreateLocalAudioTrack();
2933 auto video_track = caller()->CreateLocalVideoTrack();
2934 caller()->AddTrack(audio_track);
2935 caller()->AddTrack(video_track);
2936 // Do offer/answer, wait for the callee to receive some frames.
2937 caller()->CreateAndSetAndSignalOffer();
2938 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2939 MediaExpectations media_expectations;
2940 media_expectations.CalleeExpectsSomeAudioAndVideo();
2941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2942
2943 // The callee received frames, so we definitely should have nonzero "sent
2944 // bytes" stats at this point.
2945 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2946 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2947 }
2948
2949 // Test that we can get capture start ntp time.
TEST_P(PeerConnectionIntegrationTest,GetCaptureStartNtpTimeWithOldStatsApi)2950 TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
2951 ASSERT_TRUE(CreatePeerConnectionWrappers());
2952 ConnectFakeSignaling();
2953 caller()->AddAudioTrack();
2954
2955 callee()->AddAudioTrack();
2956
2957 // Do offer/answer, wait for the callee to receive some frames.
2958 caller()->CreateAndSetAndSignalOffer();
2959 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2960
2961 // Get the remote audio track created on the receiver, so they can be used as
2962 // GetStats filters.
2963 auto receivers = callee()->pc()->GetReceivers();
2964 ASSERT_EQ(1u, receivers.size());
2965 auto remote_audio_track = receivers[0]->track();
2966
2967 // Get the audio output level stats. Note that the level is not available
2968 // until an RTCP packet has been received.
2969 EXPECT_TRUE_WAIT(
2970 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2971 0,
2972 2 * kMaxWaitForFramesMs);
2973 }
2974
2975 // Test that the track ID is associated with all local and remote SSRC stats
2976 // using the old GetStats() and more than 1 audio and more than 1 video track.
2977 // This is a regression test for crbug.com/906988
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,OldGetStatsAssociatesTrackIdForManyMediaSections)2978 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2979 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2980 ASSERT_TRUE(CreatePeerConnectionWrappers());
2981 ConnectFakeSignaling();
2982 auto audio_sender_1 = caller()->AddAudioTrack();
2983 auto video_sender_1 = caller()->AddVideoTrack();
2984 auto audio_sender_2 = caller()->AddAudioTrack();
2985 auto video_sender_2 = caller()->AddVideoTrack();
2986 caller()->CreateAndSetAndSignalOffer();
2987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2988
2989 MediaExpectations media_expectations;
2990 media_expectations.CalleeExpectsSomeAudioAndVideo();
2991 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2992
2993 std::vector<std::string> track_ids = {
2994 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2995 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2996
2997 auto caller_stats = caller()->OldGetStats();
2998 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2999 auto callee_stats = callee()->OldGetStats();
3000 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3001 }
3002
3003 // Test that the new GetStats() returns stats for all outgoing/incoming streams
3004 // with the correct track IDs if there are more than one audio and more than one
3005 // video senders/receivers.
TEST_P(PeerConnectionIntegrationTest,NewGetStatsManyAudioAndManyVideoStreams)3006 TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3007 ASSERT_TRUE(CreatePeerConnectionWrappers());
3008 ConnectFakeSignaling();
3009 auto audio_sender_1 = caller()->AddAudioTrack();
3010 auto video_sender_1 = caller()->AddVideoTrack();
3011 auto audio_sender_2 = caller()->AddAudioTrack();
3012 auto video_sender_2 = caller()->AddVideoTrack();
3013 caller()->CreateAndSetAndSignalOffer();
3014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3015
3016 MediaExpectations media_expectations;
3017 media_expectations.CalleeExpectsSomeAudioAndVideo();
3018 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3019
3020 std::vector<std::string> track_ids = {
3021 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3022 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3023
3024 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3025 caller()->NewGetStats();
3026 ASSERT_TRUE(caller_report);
3027 auto outbound_stream_stats =
3028 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
3029 ASSERT_EQ(outbound_stream_stats.size(), 4u);
3030 std::vector<std::string> outbound_track_ids;
3031 for (const auto& stat : outbound_stream_stats) {
3032 ASSERT_TRUE(stat->bytes_sent.is_defined());
3033 EXPECT_LT(0u, *stat->bytes_sent);
3034 if (*stat->kind == "video") {
3035 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3036 EXPECT_GT(*stat->key_frames_encoded, 0u);
3037 ASSERT_TRUE(stat->frames_encoded.is_defined());
3038 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3039 }
3040 ASSERT_TRUE(stat->track_id.is_defined());
3041 const auto* track_stat =
3042 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3043 ASSERT_TRUE(track_stat);
3044 outbound_track_ids.push_back(*track_stat->track_identifier);
3045 }
3046 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3047
3048 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3049 callee()->NewGetStats();
3050 ASSERT_TRUE(callee_report);
3051 auto inbound_stream_stats =
3052 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3053 ASSERT_EQ(4u, inbound_stream_stats.size());
3054 std::vector<std::string> inbound_track_ids;
3055 for (const auto& stat : inbound_stream_stats) {
3056 ASSERT_TRUE(stat->bytes_received.is_defined());
3057 EXPECT_LT(0u, *stat->bytes_received);
3058 if (*stat->kind == "video") {
3059 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3060 EXPECT_GT(*stat->key_frames_decoded, 0u);
3061 ASSERT_TRUE(stat->frames_decoded.is_defined());
3062 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3063 }
3064 ASSERT_TRUE(stat->track_id.is_defined());
3065 const auto* track_stat =
3066 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3067 ASSERT_TRUE(track_stat);
3068 inbound_track_ids.push_back(*track_stat->track_identifier);
3069 }
3070 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3071 }
3072
3073 // Test that we can get stats (using the new stats implementation) for
3074 // unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3075 // SDP.
TEST_P(PeerConnectionIntegrationTest,GetStatsForUnsignaledStreamWithNewStatsApi)3076 TEST_P(PeerConnectionIntegrationTest,
3077 GetStatsForUnsignaledStreamWithNewStatsApi) {
3078 ASSERT_TRUE(CreatePeerConnectionWrappers());
3079 ConnectFakeSignaling();
3080 caller()->AddAudioTrack();
3081 // Remove SSRCs and MSIDs from the received offer SDP.
3082 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3083 caller()->CreateAndSetAndSignalOffer();
3084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3085 MediaExpectations media_expectations;
3086 media_expectations.CalleeExpectsSomeAudio(1);
3087 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3088
3089 // We received a frame, so we should have nonzero "bytes received" stats for
3090 // the unsignaled stream, if stats are working for it.
3091 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3092 callee()->NewGetStats();
3093 ASSERT_NE(nullptr, report);
3094 auto inbound_stream_stats =
3095 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3096 ASSERT_EQ(1U, inbound_stream_stats.size());
3097 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3098 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
3099 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3100 }
3101
3102 // Same as above but for the legacy stats implementation.
TEST_P(PeerConnectionIntegrationTest,GetStatsForUnsignaledStreamWithOldStatsApi)3103 TEST_P(PeerConnectionIntegrationTest,
3104 GetStatsForUnsignaledStreamWithOldStatsApi) {
3105 ASSERT_TRUE(CreatePeerConnectionWrappers());
3106 ConnectFakeSignaling();
3107 caller()->AddAudioTrack();
3108 // Remove SSRCs and MSIDs from the received offer SDP.
3109 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3110 caller()->CreateAndSetAndSignalOffer();
3111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3112
3113 // Note that, since the old stats implementation associates SSRCs with tracks
3114 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3115 // associated track ID. So we can't use the track "selector" argument.
3116 //
3117 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3118 // return cached stats if not enough time has passed since the last update.
3119 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
3120 kDefaultTimeout);
3121 }
3122
3123 // Test that we can successfully get the media related stats (audio level
3124 // etc.) for the unsignaled stream.
TEST_P(PeerConnectionIntegrationTest,GetMediaStatsForUnsignaledStreamWithNewStatsApi)3125 TEST_P(PeerConnectionIntegrationTest,
3126 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3127 ASSERT_TRUE(CreatePeerConnectionWrappers());
3128 ConnectFakeSignaling();
3129 caller()->AddAudioVideoTracks();
3130 // Remove SSRCs and MSIDs from the received offer SDP.
3131 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3132 caller()->CreateAndSetAndSignalOffer();
3133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3134 MediaExpectations media_expectations;
3135 media_expectations.CalleeExpectsSomeAudio(1);
3136 media_expectations.CalleeExpectsSomeVideo(1);
3137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3138
3139 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3140 callee()->NewGetStats();
3141 ASSERT_NE(nullptr, report);
3142
3143 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3144 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3145 ASSERT_GE(audio_index, 0);
3146 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
3147 }
3148
3149 // Helper for test below.
ModifySsrcs(cricket::SessionDescription * desc)3150 void ModifySsrcs(cricket::SessionDescription* desc) {
3151 for (ContentInfo& content : desc->contents()) {
3152 for (StreamParams& stream :
3153 content.media_description()->mutable_streams()) {
3154 for (uint32_t& ssrc : stream.ssrcs) {
3155 ssrc = rtc::CreateRandomId();
3156 }
3157 }
3158 }
3159 }
3160
3161 // Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3162 // SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3163 // This should result in two "RTCInboundRTPStreamStats", but only one
3164 // "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3165 // being reset to 0 once the SSRC change occurs.
3166 //
3167 // Regression test for this bug:
3168 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3169 //
3170 // The bug causes the track stats to only represent one of the two streams:
3171 // whichever one has the higher SSRC. So with this bug, there was a 50% chance
3172 // that the track stat counters would reset to 0 when the new stream is
3173 // received, and a 50% chance that they'll stop updating (while
3174 // "concealed_samples" continues increasing, due to silence being generated for
3175 // the inactive stream).
TEST_P(PeerConnectionIntegrationTest,TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges)3176 TEST_P(PeerConnectionIntegrationTest,
3177 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
3178 ASSERT_TRUE(CreatePeerConnectionWrappers());
3179 ConnectFakeSignaling();
3180 caller()->AddAudioTrack();
3181 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3182 // that doesn't signal SSRCs (from the callee's perspective).
3183 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3184 caller()->CreateAndSetAndSignalOffer();
3185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3186 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
3187 {
3188 MediaExpectations media_expectations;
3189 media_expectations.CalleeExpectsSomeAudio(50);
3190 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3191 }
3192 // Some audio frames were received, so we should have nonzero "samples
3193 // received" for the track.
3194 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3195 callee()->NewGetStats();
3196 ASSERT_NE(nullptr, report);
3197 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3198 ASSERT_EQ(1U, track_stats.size());
3199 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3200 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3201 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3202
3203 // Create a new offer and munge it to cause the caller to use a new SSRC.
3204 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3205 caller()->CreateAndSetAndSignalOffer();
3206 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3207 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3208 // SSRC.
3209 {
3210 MediaExpectations media_expectations;
3211 media_expectations.CalleeExpectsSomeAudio(25);
3212 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3213 }
3214
3215 report = callee()->NewGetStats();
3216 ASSERT_NE(nullptr, report);
3217 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3218 ASSERT_EQ(1U, track_stats.size());
3219 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3220 // The "total samples received" stat should only be greater than it was
3221 // before.
3222 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3223 // Right now, the new SSRC will cause the counters to reset to 0.
3224 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3225
3226 // Additionally, the percentage of concealed samples (samples generated to
3227 // conceal packet loss) should be less than 50%. If it's greater, that's a
3228 // good sign that we're seeing stats from the old stream that's no longer
3229 // receiving packets, and is generating concealed samples of silence.
3230 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
3231 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3232 EXPECT_LT(*track_stats[0]->concealed_samples,
3233 *track_stats[0]->total_samples_received *
3234 kAcceptableConcealedSamplesPercentage);
3235
3236 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3237 // sanity check that the SSRC really changed.
3238 // TODO(deadbeef): This isn't working right now, because we're not returning
3239 // *any* stats for the inactive stream. Uncomment when the bug is completely
3240 // fixed.
3241 // auto inbound_stream_stats =
3242 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3243 // ASSERT_EQ(2U, inbound_stream_stats.size());
3244 }
3245
3246 // Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithDtls10)3247 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
3248 PeerConnectionFactory::Options dtls_10_options;
3249 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3250 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3251 dtls_10_options));
3252 ConnectFakeSignaling();
3253 // Do normal offer/answer and wait for some frames to be received in each
3254 // direction.
3255 caller()->AddAudioVideoTracks();
3256 callee()->AddAudioVideoTracks();
3257 caller()->CreateAndSetAndSignalOffer();
3258 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3259 MediaExpectations media_expectations;
3260 media_expectations.ExpectBidirectionalAudioAndVideo();
3261 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3262 }
3263
3264 // Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
TEST_P(PeerConnectionIntegrationTest,Dtls10CipherStatsAndUmaMetrics)3265 TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
3266 PeerConnectionFactory::Options dtls_10_options;
3267 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3268 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3269 dtls_10_options));
3270 ConnectFakeSignaling();
3271 caller()->AddAudioVideoTracks();
3272 callee()->AddAudioVideoTracks();
3273 caller()->CreateAndSetAndSignalOffer();
3274 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3275 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
3276 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
3277 kDefaultTimeout);
3278 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
3279 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
3280 // TODO(bugs.webrtc.org/9456): Fix it.
3281 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3282 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3283 kDefaultSrtpCryptoSuite));
3284 }
3285
3286 // Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
TEST_P(PeerConnectionIntegrationTest,Dtls12CipherStatsAndUmaMetrics)3287 TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
3288 PeerConnectionFactory::Options dtls_12_options;
3289 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3290 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3291 dtls_12_options));
3292 ConnectFakeSignaling();
3293 caller()->AddAudioVideoTracks();
3294 callee()->AddAudioVideoTracks();
3295 caller()->CreateAndSetAndSignalOffer();
3296 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3297 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
3298 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
3299 kDefaultTimeout);
3300 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
3301 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
3302 // TODO(bugs.webrtc.org/9456): Fix it.
3303 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3304 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3305 kDefaultSrtpCryptoSuite));
3306 }
3307
3308 // Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3309 // callee only supports 1.0.
TEST_P(PeerConnectionIntegrationTest,CallerDtls12ToCalleeDtls10)3310 TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
3311 PeerConnectionFactory::Options caller_options;
3312 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3313 PeerConnectionFactory::Options callee_options;
3314 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3315 ASSERT_TRUE(
3316 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3317 ConnectFakeSignaling();
3318 // Do normal offer/answer and wait for some frames to be received in each
3319 // direction.
3320 caller()->AddAudioVideoTracks();
3321 callee()->AddAudioVideoTracks();
3322 caller()->CreateAndSetAndSignalOffer();
3323 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3324 MediaExpectations media_expectations;
3325 media_expectations.ExpectBidirectionalAudioAndVideo();
3326 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3327 }
3328
3329 // Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3330 // callee supports 1.2.
TEST_P(PeerConnectionIntegrationTest,CallerDtls10ToCalleeDtls12)3331 TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
3332 PeerConnectionFactory::Options caller_options;
3333 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3334 PeerConnectionFactory::Options callee_options;
3335 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3336 ASSERT_TRUE(
3337 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3338 ConnectFakeSignaling();
3339 // Do normal offer/answer and wait for some frames to be received in each
3340 // direction.
3341 caller()->AddAudioVideoTracks();
3342 callee()->AddAudioVideoTracks();
3343 caller()->CreateAndSetAndSignalOffer();
3344 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3345 MediaExpectations media_expectations;
3346 media_expectations.ExpectBidirectionalAudioAndVideo();
3347 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3348 }
3349
3350 // The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3351 // works as expected; the cipher should only be used if enabled by both sides.
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported)3352 TEST_P(PeerConnectionIntegrationTest,
3353 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3354 PeerConnectionFactory::Options caller_options;
3355 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
3356 PeerConnectionFactory::Options callee_options;
3357 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3358 false;
3359 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3360 TestNegotiatedCipherSuite(caller_options, callee_options,
3361 expected_cipher_suite);
3362 }
3363
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported)3364 TEST_P(PeerConnectionIntegrationTest,
3365 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3366 PeerConnectionFactory::Options caller_options;
3367 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3368 false;
3369 PeerConnectionFactory::Options callee_options;
3370 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
3371 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3372 TestNegotiatedCipherSuite(caller_options, callee_options,
3373 expected_cipher_suite);
3374 }
3375
TEST_P(PeerConnectionIntegrationTest,Aes128Sha1_32_CipherUsedWhenSupported)3376 TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3377 PeerConnectionFactory::Options caller_options;
3378 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
3379 PeerConnectionFactory::Options callee_options;
3380 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
3381 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3382 TestNegotiatedCipherSuite(caller_options, callee_options,
3383 expected_cipher_suite);
3384 }
3385
3386 // Test that a non-GCM cipher is used if both sides only support non-GCM.
TEST_P(PeerConnectionIntegrationTest,NonGcmCipherUsedWhenGcmNotSupported)3387 TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
3388 bool local_gcm_enabled = false;
3389 bool remote_gcm_enabled = false;
3390 bool aes_ctr_enabled = true;
3391 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3392 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3393 aes_ctr_enabled, expected_cipher_suite);
3394 }
3395
3396 // Test that a GCM cipher is used if both ends support it and non-GCM is
3397 // disabled.
TEST_P(PeerConnectionIntegrationTest,GcmCipherUsedWhenOnlyGcmSupported)3398 TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
3399 bool local_gcm_enabled = true;
3400 bool remote_gcm_enabled = true;
3401 bool aes_ctr_enabled = false;
3402 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3403 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3404 aes_ctr_enabled, expected_cipher_suite);
3405 }
3406
3407 // Verify that media can be transmitted end-to-end when GCM crypto suites are
3408 // enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3409 // only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3410 // works with it.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithGcmCipher)3411 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
3412 PeerConnectionFactory::Options gcm_options;
3413 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
3414 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
3415 ASSERT_TRUE(
3416 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3417 ConnectFakeSignaling();
3418 // Do normal offer/answer and wait for some frames to be received in each
3419 // direction.
3420 caller()->AddAudioVideoTracks();
3421 callee()->AddAudioVideoTracks();
3422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3424 MediaExpectations media_expectations;
3425 media_expectations.ExpectBidirectionalAudioAndVideo();
3426 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3427 }
3428
3429 // This test sets up a call between two parties with audio, video and an RTP
3430 // data channel.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithRtpDataChannel)3431 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
3432 PeerConnectionInterface::RTCConfiguration rtc_config;
3433 rtc_config.enable_rtp_data_channel = true;
3434 rtc_config.enable_dtls_srtp = false;
3435 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3436 ConnectFakeSignaling();
3437 // Expect that data channel created on caller side will show up for callee as
3438 // well.
3439 caller()->CreateDataChannel();
3440 caller()->AddAudioVideoTracks();
3441 callee()->AddAudioVideoTracks();
3442 caller()->CreateAndSetAndSignalOffer();
3443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3444 // Ensure the existence of the RTP data channel didn't impede audio/video.
3445 MediaExpectations media_expectations;
3446 media_expectations.ExpectBidirectionalAudioAndVideo();
3447 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3448 ASSERT_NE(nullptr, caller()->data_channel());
3449 ASSERT_NE(nullptr, callee()->data_channel());
3450 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3451 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3452
3453 // Ensure data can be sent in both directions.
3454 std::string data = "hello world";
3455 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3456 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3457 kDefaultTimeout);
3458 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3459 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3460 kDefaultTimeout);
3461 }
3462
TEST_P(PeerConnectionIntegrationTest,RtpDataChannelWorksAfterRollback)3463 TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3464 PeerConnectionInterface::RTCConfiguration rtc_config;
3465 rtc_config.enable_rtp_data_channel = true;
3466 rtc_config.enable_dtls_srtp = false;
3467 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3468 ConnectFakeSignaling();
3469 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3470 ASSERT_TRUE(data_channel.get() != nullptr);
3471 caller()->CreateAndSetAndSignalOffer();
3472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3473
3474 caller()->CreateDataChannel("label_2", nullptr);
3475 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3476 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3477 caller()->pc()->SetLocalDescription(observer,
3478 caller()->CreateOfferAndWait().release());
3479 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3480 caller()->Rollback();
3481
3482 std::string data = "hello world";
3483 SendRtpDataWithRetries(data_channel, data, 5);
3484 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3485 kDefaultTimeout);
3486 }
3487
3488 // Ensure that an RTP data channel is signaled as closed for the caller when
3489 // the callee rejects it in a subsequent offer.
TEST_P(PeerConnectionIntegrationTest,RtpDataChannelSignaledClosedInCalleeOffer)3490 TEST_P(PeerConnectionIntegrationTest,
3491 RtpDataChannelSignaledClosedInCalleeOffer) {
3492 // Same procedure as above test.
3493 PeerConnectionInterface::RTCConfiguration rtc_config;
3494 rtc_config.enable_rtp_data_channel = true;
3495 rtc_config.enable_dtls_srtp = false;
3496 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3497 ConnectFakeSignaling();
3498 caller()->CreateDataChannel();
3499 caller()->AddAudioVideoTracks();
3500 callee()->AddAudioVideoTracks();
3501 caller()->CreateAndSetAndSignalOffer();
3502 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3503 ASSERT_NE(nullptr, caller()->data_channel());
3504 ASSERT_NE(nullptr, callee()->data_channel());
3505 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3506 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3507
3508 // Close the data channel on the callee, and do an updated offer/answer.
3509 callee()->data_channel()->Close();
3510 callee()->CreateAndSetAndSignalOffer();
3511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3512 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3513 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3514 }
3515
3516 // Tests that data is buffered in an RTP data channel until an observer is
3517 // registered for it.
3518 //
3519 // NOTE: RTP data channels can receive data before the underlying
3520 // transport has detected that a channel is writable and thus data can be
3521 // received before the data channel state changes to open. That is hard to test
3522 // but the same buffering is expected to be used in that case.
3523 //
3524 // Use fake clock and simulated network delay so that we predictably can wait
3525 // until an SCTP message has been delivered without "sleep()"ing.
TEST_P(PeerConnectionIntegrationTestWithFakeClock,DataBufferedUntilRtpDataChannelObserverRegistered)3526 TEST_P(PeerConnectionIntegrationTestWithFakeClock,
3527 DataBufferedUntilRtpDataChannelObserverRegistered) {
3528 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3529 virtual_socket_server()->UpdateDelayDistribution();
3530
3531 PeerConnectionInterface::RTCConfiguration rtc_config;
3532 rtc_config.enable_rtp_data_channel = true;
3533 rtc_config.enable_dtls_srtp = false;
3534 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3535 ConnectFakeSignaling();
3536 caller()->CreateDataChannel();
3537 caller()->CreateAndSetAndSignalOffer();
3538 ASSERT_TRUE(caller()->data_channel() != nullptr);
3539 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3540 kDefaultTimeout, FakeClock());
3541 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3542 kDefaultTimeout, FakeClock());
3543 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3544 callee()->data_channel()->state(), kDefaultTimeout,
3545 FakeClock());
3546
3547 // Unregister the observer which is normally automatically registered.
3548 callee()->data_channel()->UnregisterObserver();
3549 // Send data and advance fake clock until it should have been received.
3550 std::string data = "hello world";
3551 caller()->data_channel()->Send(DataBuffer(data));
3552 SIMULATED_WAIT(false, 50, FakeClock());
3553
3554 // Attach data channel and expect data to be received immediately. Note that
3555 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3556 // further, but data can be received even if the callback is asynchronous.
3557 MockDataChannelObserver new_observer(callee()->data_channel());
3558 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3559 FakeClock());
3560 }
3561
3562 // This test sets up a call between two parties with audio, video and but only
3563 // the caller client supports RTP data channels.
TEST_P(PeerConnectionIntegrationTest,RtpDataChannelsRejectedByCallee)3564 TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
3565 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3566 rtc_config_1.enable_rtp_data_channel = true;
3567 // Must disable DTLS to make negotiation succeed.
3568 rtc_config_1.enable_dtls_srtp = false;
3569 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3570 rtc_config_2.enable_dtls_srtp = false;
3571 rtc_config_2.enable_dtls_srtp = false;
3572 ASSERT_TRUE(
3573 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
3574 ConnectFakeSignaling();
3575 caller()->CreateDataChannel();
3576 ASSERT_TRUE(caller()->data_channel() != nullptr);
3577 caller()->AddAudioVideoTracks();
3578 callee()->AddAudioVideoTracks();
3579 caller()->CreateAndSetAndSignalOffer();
3580 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3581 // The caller should still have a data channel, but it should be closed, and
3582 // one should ever have been created for the callee.
3583 EXPECT_TRUE(caller()->data_channel() != nullptr);
3584 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3585 EXPECT_EQ(nullptr, callee()->data_channel());
3586 }
3587
3588 // This test sets up a call between two parties with audio, and video. When
3589 // audio and video is setup and flowing, an RTP data channel is negotiated.
TEST_P(PeerConnectionIntegrationTest,AddRtpDataChannelInSubsequentOffer)3590 TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
3591 PeerConnectionInterface::RTCConfiguration rtc_config;
3592 rtc_config.enable_rtp_data_channel = true;
3593 rtc_config.enable_dtls_srtp = false;
3594 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3595 ConnectFakeSignaling();
3596 // Do initial offer/answer with audio/video.
3597 caller()->AddAudioVideoTracks();
3598 callee()->AddAudioVideoTracks();
3599 caller()->CreateAndSetAndSignalOffer();
3600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3601 // Create data channel and do new offer and answer.
3602 caller()->CreateDataChannel();
3603 caller()->CreateAndSetAndSignalOffer();
3604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3605 ASSERT_NE(nullptr, caller()->data_channel());
3606 ASSERT_NE(nullptr, callee()->data_channel());
3607 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3608 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3609 // Ensure data can be sent in both directions.
3610 std::string data = "hello world";
3611 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3612 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3613 kDefaultTimeout);
3614 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3615 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3616 kDefaultTimeout);
3617 }
3618
3619 #ifdef HAVE_SCTP
3620
3621 // This test sets up a call between two parties with audio, video and an SCTP
3622 // data channel.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithSctpDataChannel)3623 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
3624 ASSERT_TRUE(CreatePeerConnectionWrappers());
3625 ConnectFakeSignaling();
3626 // Expect that data channel created on caller side will show up for callee as
3627 // well.
3628 caller()->CreateDataChannel();
3629 caller()->AddAudioVideoTracks();
3630 callee()->AddAudioVideoTracks();
3631 caller()->CreateAndSetAndSignalOffer();
3632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3633 // Ensure the existence of the SCTP data channel didn't impede audio/video.
3634 MediaExpectations media_expectations;
3635 media_expectations.ExpectBidirectionalAudioAndVideo();
3636 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3637 // Caller data channel should already exist (it created one). Callee data
3638 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3639 ASSERT_NE(nullptr, caller()->data_channel());
3640 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3641 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3642 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3643
3644 // Ensure data can be sent in both directions.
3645 std::string data = "hello world";
3646 caller()->data_channel()->Send(DataBuffer(data));
3647 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3648 kDefaultTimeout);
3649 callee()->data_channel()->Send(DataBuffer(data));
3650 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3651 kDefaultTimeout);
3652 }
3653
3654 // Ensure that when the callee closes an SCTP data channel, the closing
3655 // procedure results in the data channel being closed for the caller as well.
TEST_P(PeerConnectionIntegrationTest,CalleeClosesSctpDataChannel)3656 TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
3657 // Same procedure as above test.
3658 ASSERT_TRUE(CreatePeerConnectionWrappers());
3659 ConnectFakeSignaling();
3660 caller()->CreateDataChannel();
3661 caller()->AddAudioVideoTracks();
3662 callee()->AddAudioVideoTracks();
3663 caller()->CreateAndSetAndSignalOffer();
3664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3665 ASSERT_NE(nullptr, caller()->data_channel());
3666 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3667 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3668 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3669
3670 // Close the data channel on the callee side, and wait for it to reach the
3671 // "closed" state on both sides.
3672 callee()->data_channel()->Close();
3673 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3674 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3675 }
3676
TEST_P(PeerConnectionIntegrationTest,SctpDataChannelConfigSentToOtherSide)3677 TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
3678 ASSERT_TRUE(CreatePeerConnectionWrappers());
3679 ConnectFakeSignaling();
3680 webrtc::DataChannelInit init;
3681 init.id = 53;
3682 init.maxRetransmits = 52;
3683 caller()->CreateDataChannel("data-channel", &init);
3684 caller()->AddAudioVideoTracks();
3685 callee()->AddAudioVideoTracks();
3686 caller()->CreateAndSetAndSignalOffer();
3687 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3688 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3689 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3690 // Since "negotiated" is false, the "id" parameter should be ignored.
3691 EXPECT_NE(init.id, callee()->data_channel()->id());
3692 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3693 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3694 EXPECT_FALSE(callee()->data_channel()->negotiated());
3695 }
3696
3697 // Test usrsctp's ability to process unordered data stream, where data actually
3698 // arrives out of order using simulated delays. Previously there have been some
3699 // bugs in this area.
TEST_P(PeerConnectionIntegrationTest,StressTestUnorderedSctpDataChannel)3700 TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
3701 // Introduce random network delays.
3702 // Otherwise it's not a true "unordered" test.
3703 virtual_socket_server()->set_delay_mean(20);
3704 virtual_socket_server()->set_delay_stddev(5);
3705 virtual_socket_server()->UpdateDelayDistribution();
3706 // Normal procedure, but with unordered data channel config.
3707 ASSERT_TRUE(CreatePeerConnectionWrappers());
3708 ConnectFakeSignaling();
3709 webrtc::DataChannelInit init;
3710 init.ordered = false;
3711 caller()->CreateDataChannel(&init);
3712 caller()->CreateAndSetAndSignalOffer();
3713 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3714 ASSERT_NE(nullptr, caller()->data_channel());
3715 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3716 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3717 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3718
3719 static constexpr int kNumMessages = 100;
3720 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3721 static constexpr size_t kMaxMessageSize = 4096;
3722 // Create and send random messages.
3723 std::vector<std::string> sent_messages;
3724 for (int i = 0; i < kNumMessages; ++i) {
3725 size_t length =
3726 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3727 std::string message;
3728 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3729 caller()->data_channel()->Send(DataBuffer(message));
3730 callee()->data_channel()->Send(DataBuffer(message));
3731 sent_messages.push_back(message);
3732 }
3733
3734 // Wait for all messages to be received.
3735 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
3736 caller()->data_observer()->received_message_count(),
3737 kDefaultTimeout);
3738 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
3739 callee()->data_observer()->received_message_count(),
3740 kDefaultTimeout);
3741
3742 // Sort and compare to make sure none of the messages were corrupted.
3743 std::vector<std::string> caller_received_messages =
3744 caller()->data_observer()->messages();
3745 std::vector<std::string> callee_received_messages =
3746 callee()->data_observer()->messages();
3747 absl::c_sort(sent_messages);
3748 absl::c_sort(caller_received_messages);
3749 absl::c_sort(callee_received_messages);
3750 EXPECT_EQ(sent_messages, caller_received_messages);
3751 EXPECT_EQ(sent_messages, callee_received_messages);
3752 }
3753
3754 // This test sets up a call between two parties with audio, and video. When
3755 // audio and video are setup and flowing, an SCTP data channel is negotiated.
TEST_P(PeerConnectionIntegrationTest,AddSctpDataChannelInSubsequentOffer)3756 TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
3757 ASSERT_TRUE(CreatePeerConnectionWrappers());
3758 ConnectFakeSignaling();
3759 // Do initial offer/answer with audio/video.
3760 caller()->AddAudioVideoTracks();
3761 callee()->AddAudioVideoTracks();
3762 caller()->CreateAndSetAndSignalOffer();
3763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3764 // Create data channel and do new offer and answer.
3765 caller()->CreateDataChannel();
3766 caller()->CreateAndSetAndSignalOffer();
3767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3768 // Caller data channel should already exist (it created one). Callee data
3769 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3770 ASSERT_NE(nullptr, caller()->data_channel());
3771 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3772 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3773 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3774 // Ensure data can be sent in both directions.
3775 std::string data = "hello world";
3776 caller()->data_channel()->Send(DataBuffer(data));
3777 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3778 kDefaultTimeout);
3779 callee()->data_channel()->Send(DataBuffer(data));
3780 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3781 kDefaultTimeout);
3782 }
3783
3784 // Set up a connection initially just using SCTP data channels, later upgrading
3785 // to audio/video, ensuring frames are received end-to-end. Effectively the
3786 // inverse of the test above.
3787 // This was broken in M57; see https://crbug.com/711243
TEST_P(PeerConnectionIntegrationTest,SctpDataChannelToAudioVideoUpgrade)3788 TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
3789 ASSERT_TRUE(CreatePeerConnectionWrappers());
3790 ConnectFakeSignaling();
3791 // Do initial offer/answer with just data channel.
3792 caller()->CreateDataChannel();
3793 caller()->CreateAndSetAndSignalOffer();
3794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3795 // Wait until data can be sent over the data channel.
3796 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3797 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3798 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3799
3800 // Do subsequent offer/answer with two-way audio and video. Audio and video
3801 // should end up bundled on the DTLS/ICE transport already used for data.
3802 caller()->AddAudioVideoTracks();
3803 callee()->AddAudioVideoTracks();
3804 caller()->CreateAndSetAndSignalOffer();
3805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3806 MediaExpectations media_expectations;
3807 media_expectations.ExpectBidirectionalAudioAndVideo();
3808 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3809 }
3810
MakeSpecCompliantSctpOffer(cricket::SessionDescription * desc)3811 static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
3812 cricket::SctpDataContentDescription* dcd_offer =
3813 GetFirstSctpDataContentDescription(desc);
3814 // See https://crbug.com/webrtc/11211 - this function is a no-op
3815 ASSERT_TRUE(dcd_offer);
3816 dcd_offer->set_use_sctpmap(false);
3817 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3818 }
3819
3820 // Test that the data channel works when a spec-compliant SCTP m= section is
3821 // offered (using "a=sctp-port" instead of "a=sctpmap", and using
3822 // "UDP/DTLS/SCTP" as the protocol).
TEST_P(PeerConnectionIntegrationTest,DataChannelWorksWhenSpecCompliantSctpOfferReceived)3823 TEST_P(PeerConnectionIntegrationTest,
3824 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3825 ASSERT_TRUE(CreatePeerConnectionWrappers());
3826 ConnectFakeSignaling();
3827 caller()->CreateDataChannel();
3828 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3829 caller()->CreateAndSetAndSignalOffer();
3830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3831 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3832 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3833 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3834
3835 // Ensure data can be sent in both directions.
3836 std::string data = "hello world";
3837 caller()->data_channel()->Send(DataBuffer(data));
3838 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3839 kDefaultTimeout);
3840 callee()->data_channel()->Send(DataBuffer(data));
3841 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3842 kDefaultTimeout);
3843 }
3844
3845 #endif // HAVE_SCTP
3846
3847 // Test that the ICE connection and gathering states eventually reach
3848 // "complete".
TEST_P(PeerConnectionIntegrationTest,IceStatesReachCompletion)3849 TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
3850 ASSERT_TRUE(CreatePeerConnectionWrappers());
3851 ConnectFakeSignaling();
3852 // Do normal offer/answer.
3853 caller()->AddAudioVideoTracks();
3854 callee()->AddAudioVideoTracks();
3855 caller()->CreateAndSetAndSignalOffer();
3856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3857 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3858 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3859 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3860 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3861 // After the best candidate pair is selected and all candidates are signaled,
3862 // the ICE connection state should reach "complete".
3863 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3864 // answerer/"callee" by default) only reaches "connected". When this is
3865 // fixed, this test should be updated.
3866 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3867 caller()->ice_connection_state(), kDefaultTimeout);
3868 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3869 callee()->ice_connection_state(), kDefaultTimeout);
3870 }
3871
3872 constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3873 cricket::PORTALLOCATOR_DISABLE_RELAY |
3874 cricket::PORTALLOCATOR_DISABLE_TCP;
3875
3876 // Use a mock resolver to resolve the hostname back to the original IP on both
3877 // sides and check that the ICE connection connects.
TEST_P(PeerConnectionIntegrationTest,IceStatesReachCompletionWithRemoteHostname)3878 TEST_P(PeerConnectionIntegrationTest,
3879 IceStatesReachCompletionWithRemoteHostname) {
3880 auto caller_resolver_factory =
3881 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3882 auto callee_resolver_factory =
3883 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3884 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3885 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
3886
3887 // This also verifies that the injected AsyncResolverFactory is used by
3888 // P2PTransportChannel.
3889 EXPECT_CALL(*caller_resolver_factory, Create())
3890 .WillOnce(Return(&caller_async_resolver));
3891 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3892 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3893
3894 EXPECT_CALL(*callee_resolver_factory, Create())
3895 .WillOnce(Return(&callee_async_resolver));
3896 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3897 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3898
3899 PeerConnectionInterface::RTCConfiguration config;
3900 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3901 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3902
3903 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3904 config, std::move(caller_deps), config, std::move(callee_deps)));
3905
3906 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3907 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3908
3909 // Enable hostname candidates with mDNS names.
3910 caller()->SetMdnsResponder(
3911 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
3912 callee()->SetMdnsResponder(
3913 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
3914
3915 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
3916
3917 ConnectFakeSignaling();
3918 caller()->AddAudioVideoTracks();
3919 callee()->AddAudioVideoTracks();
3920 caller()->CreateAndSetAndSignalOffer();
3921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3922 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3923 caller()->ice_connection_state(), kDefaultTimeout);
3924 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3925 callee()->ice_connection_state(), kDefaultTimeout);
3926
3927 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3928 "WebRTC.PeerConnection.CandidatePairType_UDP",
3929 webrtc::kIceCandidatePairHostNameHostName));
3930 }
3931
3932 // Test that firewalling the ICE connection causes the clients to identify the
3933 // disconnected state and then removing the firewall causes them to reconnect.
3934 class PeerConnectionIntegrationIceStatesTest
3935 : public PeerConnectionIntegrationBaseTest,
3936 public ::testing::WithParamInterface<
3937 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
3938 protected:
PeerConnectionIntegrationIceStatesTest()3939 PeerConnectionIntegrationIceStatesTest()
3940 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3941 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
3942 }
3943
StartStunServer(const SocketAddress & server_address)3944 void StartStunServer(const SocketAddress& server_address) {
3945 stun_server_.reset(
3946 cricket::TestStunServer::Create(network_thread(), server_address));
3947 }
3948
TestIPv6()3949 bool TestIPv6() {
3950 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3951 }
3952
SetPortAllocatorFlags()3953 void SetPortAllocatorFlags() {
3954 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3955 port_allocator_flags_, port_allocator_flags_);
3956 }
3957
CallerAddresses()3958 std::vector<SocketAddress> CallerAddresses() {
3959 std::vector<SocketAddress> addresses;
3960 addresses.push_back(SocketAddress("1.1.1.1", 0));
3961 if (TestIPv6()) {
3962 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3963 }
3964 return addresses;
3965 }
3966
CalleeAddresses()3967 std::vector<SocketAddress> CalleeAddresses() {
3968 std::vector<SocketAddress> addresses;
3969 addresses.push_back(SocketAddress("2.2.2.2", 0));
3970 if (TestIPv6()) {
3971 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3972 }
3973 return addresses;
3974 }
3975
SetUpNetworkInterfaces()3976 void SetUpNetworkInterfaces() {
3977 // Remove the default interfaces added by the test infrastructure.
3978 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3979 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3980
3981 // Add network addresses for test.
3982 for (const auto& caller_address : CallerAddresses()) {
3983 caller()->network_manager()->AddInterface(caller_address);
3984 }
3985 for (const auto& callee_address : CalleeAddresses()) {
3986 callee()->network_manager()->AddInterface(callee_address);
3987 }
3988 }
3989
3990 private:
3991 uint32_t port_allocator_flags_;
3992 std::unique_ptr<cricket::TestStunServer> stun_server_;
3993 };
3994
3995 // Ensure FakeClockForTest is constructed first (see class for rationale).
3996 class PeerConnectionIntegrationIceStatesTestWithFakeClock
3997 : public FakeClockForTest,
3998 public PeerConnectionIntegrationIceStatesTest {};
3999
4000 // Tests that the PeerConnection goes through all the ICE gathering/connection
4001 // states over the duration of the call. This includes Disconnected and Failed
4002 // states, induced by putting a firewall between the peers and waiting for them
4003 // to time out.
TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,VerifyIceStates)4004 TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
4005 const SocketAddress kStunServerAddress =
4006 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4007 StartStunServer(kStunServerAddress);
4008
4009 PeerConnectionInterface::RTCConfiguration config;
4010 PeerConnectionInterface::IceServer ice_stun_server;
4011 ice_stun_server.urls.push_back(
4012 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4013 kStunServerAddress.PortAsString());
4014 config.servers.push_back(ice_stun_server);
4015
4016 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4017 ConnectFakeSignaling();
4018 SetPortAllocatorFlags();
4019 SetUpNetworkInterfaces();
4020 caller()->AddAudioVideoTracks();
4021 callee()->AddAudioVideoTracks();
4022
4023 // Initial state before anything happens.
4024 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4025 caller()->ice_gathering_state());
4026 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4027 caller()->ice_connection_state());
4028 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4029 caller()->standardized_ice_connection_state());
4030
4031 // Start the call by creating the offer, setting it as the local description,
4032 // then sending it to the peer who will respond with an answer. This happens
4033 // asynchronously so that we can watch the states as it runs in the
4034 // background.
4035 caller()->CreateAndSetAndSignalOffer();
4036
4037 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4038 caller()->ice_connection_state(), kDefaultTimeout,
4039 FakeClock());
4040 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4041 caller()->standardized_ice_connection_state(),
4042 kDefaultTimeout, FakeClock());
4043
4044 // Verify that the observer was notified of the intermediate transitions.
4045 EXPECT_THAT(caller()->ice_connection_state_history(),
4046 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4047 PeerConnectionInterface::kIceConnectionConnected,
4048 PeerConnectionInterface::kIceConnectionCompleted));
4049 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4050 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4051 PeerConnectionInterface::kIceConnectionConnected,
4052 PeerConnectionInterface::kIceConnectionCompleted));
4053 EXPECT_THAT(
4054 caller()->peer_connection_state_history(),
4055 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
4056 PeerConnectionInterface::PeerConnectionState::kConnected));
4057 EXPECT_THAT(caller()->ice_gathering_state_history(),
4058 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4059 PeerConnectionInterface::kIceGatheringComplete));
4060
4061 // Block connections to/from the caller and wait for ICE to become
4062 // disconnected.
4063 for (const auto& caller_address : CallerAddresses()) {
4064 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4065 }
4066 RTC_LOG(LS_INFO) << "Firewall rules applied";
4067 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4068 caller()->ice_connection_state(), kDefaultTimeout,
4069 FakeClock());
4070 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4071 caller()->standardized_ice_connection_state(),
4072 kDefaultTimeout, FakeClock());
4073
4074 // Let ICE re-establish by removing the firewall rules.
4075 firewall()->ClearRules();
4076 RTC_LOG(LS_INFO) << "Firewall rules cleared";
4077 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4078 caller()->ice_connection_state(), kDefaultTimeout,
4079 FakeClock());
4080 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4081 caller()->standardized_ice_connection_state(),
4082 kDefaultTimeout, FakeClock());
4083
4084 // According to RFC7675, if there is no response within 30 seconds then the
4085 // peer should consider the other side to have rejected the connection. This
4086 // is signaled by the state transitioning to "failed".
4087 constexpr int kConsentTimeout = 30000;
4088 for (const auto& caller_address : CallerAddresses()) {
4089 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4090 }
4091 RTC_LOG(LS_INFO) << "Firewall rules applied again";
4092 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4093 caller()->ice_connection_state(), kConsentTimeout,
4094 FakeClock());
4095 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4096 caller()->standardized_ice_connection_state(),
4097 kConsentTimeout, FakeClock());
4098 }
4099
4100 // Tests that if the connection doesn't get set up properly we eventually reach
4101 // the "failed" iceConnectionState.
TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,IceStateSetupFailure)4102 TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4103 IceStateSetupFailure) {
4104 // Block connections to/from the caller and wait for ICE to become
4105 // disconnected.
4106 for (const auto& caller_address : CallerAddresses()) {
4107 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4108 }
4109
4110 ASSERT_TRUE(CreatePeerConnectionWrappers());
4111 ConnectFakeSignaling();
4112 SetPortAllocatorFlags();
4113 SetUpNetworkInterfaces();
4114 caller()->AddAudioVideoTracks();
4115 caller()->CreateAndSetAndSignalOffer();
4116
4117 // According to RFC7675, if there is no response within 30 seconds then the
4118 // peer should consider the other side to have rejected the connection. This
4119 // is signaled by the state transitioning to "failed".
4120 constexpr int kConsentTimeout = 30000;
4121 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4122 caller()->standardized_ice_connection_state(),
4123 kConsentTimeout, FakeClock());
4124 }
4125
4126 // Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4127 // and that the statistics in the metric observers are updated correctly.
TEST_P(PeerConnectionIntegrationIceStatesTest,VerifyBestConnection)4128 TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4129 ASSERT_TRUE(CreatePeerConnectionWrappers());
4130 ConnectFakeSignaling();
4131 SetPortAllocatorFlags();
4132 SetUpNetworkInterfaces();
4133 caller()->AddAudioVideoTracks();
4134 callee()->AddAudioVideoTracks();
4135 caller()->CreateAndSetAndSignalOffer();
4136
4137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4139 caller()->ice_connection_state(), kDefaultTimeout);
4140 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4141 callee()->ice_connection_state(), kDefaultTimeout);
4142
4143 // TODO(bugs.webrtc.org/9456): Fix it.
4144 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4145 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4146 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4147 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
4148 if (TestIPv6()) {
4149 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4150 // connection.
4151 EXPECT_METRIC_EQ(0, num_best_ipv4);
4152 EXPECT_METRIC_EQ(1, num_best_ipv6);
4153 } else {
4154 EXPECT_METRIC_EQ(1, num_best_ipv4);
4155 EXPECT_METRIC_EQ(0, num_best_ipv6);
4156 }
4157
4158 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4159 "WebRTC.PeerConnection.CandidatePairType_UDP",
4160 webrtc::kIceCandidatePairHostHost));
4161 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4162 "WebRTC.PeerConnection.CandidatePairType_UDP",
4163 webrtc::kIceCandidatePairHostPublicHostPublic));
4164 }
4165
4166 constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4167 cricket::PORTALLOCATOR_DISABLE_STUN |
4168 cricket::PORTALLOCATOR_DISABLE_RELAY;
4169 constexpr uint32_t kFlagsIPv6NoStun =
4170 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4171 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4172 constexpr uint32_t kFlagsIPv4Stun =
4173 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4174
4175 INSTANTIATE_TEST_SUITE_P(
4176 PeerConnectionIntegrationTest,
4177 PeerConnectionIntegrationIceStatesTest,
4178 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4179 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4180 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4181 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4182
4183 INSTANTIATE_TEST_SUITE_P(
4184 PeerConnectionIntegrationTest,
4185 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4186 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4187 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4188 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4189 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4190
4191 // This test sets up a call between two parties with audio and video.
4192 // During the call, the caller restarts ICE and the test verifies that
4193 // new ICE candidates are generated and audio and video still can flow, and the
4194 // ICE state reaches completed again.
TEST_P(PeerConnectionIntegrationTest,MediaContinuesFlowingAfterIceRestart)4195 TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
4196 ASSERT_TRUE(CreatePeerConnectionWrappers());
4197 ConnectFakeSignaling();
4198 // Do normal offer/answer and wait for ICE to complete.
4199 caller()->AddAudioVideoTracks();
4200 callee()->AddAudioVideoTracks();
4201 caller()->CreateAndSetAndSignalOffer();
4202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4203 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4204 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4205 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4206 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4207
4208 // To verify that the ICE restart actually occurs, get
4209 // ufrag/password/candidates before and after restart.
4210 // Create an SDP string of the first audio candidate for both clients.
4211 const webrtc::IceCandidateCollection* audio_candidates_caller =
4212 caller()->pc()->local_description()->candidates(0);
4213 const webrtc::IceCandidateCollection* audio_candidates_callee =
4214 callee()->pc()->local_description()->candidates(0);
4215 ASSERT_GT(audio_candidates_caller->count(), 0u);
4216 ASSERT_GT(audio_candidates_callee->count(), 0u);
4217 std::string caller_candidate_pre_restart;
4218 ASSERT_TRUE(
4219 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4220 std::string callee_candidate_pre_restart;
4221 ASSERT_TRUE(
4222 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4223 const cricket::SessionDescription* desc =
4224 caller()->pc()->local_description()->description();
4225 std::string caller_ufrag_pre_restart =
4226 desc->transport_infos()[0].description.ice_ufrag;
4227 desc = callee()->pc()->local_description()->description();
4228 std::string callee_ufrag_pre_restart =
4229 desc->transport_infos()[0].description.ice_ufrag;
4230
4231 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
4232 // Have the caller initiate an ICE restart.
4233 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4234 caller()->CreateAndSetAndSignalOffer();
4235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4236 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4237 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4238 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4239 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4240
4241 // Grab the ufrags/candidates again.
4242 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4243 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4244 ASSERT_GT(audio_candidates_caller->count(), 0u);
4245 ASSERT_GT(audio_candidates_callee->count(), 0u);
4246 std::string caller_candidate_post_restart;
4247 ASSERT_TRUE(
4248 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4249 std::string callee_candidate_post_restart;
4250 ASSERT_TRUE(
4251 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4252 desc = caller()->pc()->local_description()->description();
4253 std::string caller_ufrag_post_restart =
4254 desc->transport_infos()[0].description.ice_ufrag;
4255 desc = callee()->pc()->local_description()->description();
4256 std::string callee_ufrag_post_restart =
4257 desc->transport_infos()[0].description.ice_ufrag;
4258 // Sanity check that an ICE restart was actually negotiated in SDP.
4259 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4260 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4261 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4262 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4263 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
4264
4265 // Ensure that additional frames are received after the ICE restart.
4266 MediaExpectations media_expectations;
4267 media_expectations.ExpectBidirectionalAudioAndVideo();
4268 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4269 }
4270
4271 // Verify that audio/video can be received end-to-end when ICE renomination is
4272 // enabled.
TEST_P(PeerConnectionIntegrationTest,EndToEndCallWithIceRenomination)4273 TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
4274 PeerConnectionInterface::RTCConfiguration config;
4275 config.enable_ice_renomination = true;
4276 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4277 ConnectFakeSignaling();
4278 // Do normal offer/answer and wait for some frames to be received in each
4279 // direction.
4280 caller()->AddAudioVideoTracks();
4281 callee()->AddAudioVideoTracks();
4282 caller()->CreateAndSetAndSignalOffer();
4283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4284 // Sanity check that ICE renomination was actually negotiated.
4285 const cricket::SessionDescription* desc =
4286 caller()->pc()->local_description()->description();
4287 for (const cricket::TransportInfo& info : desc->transport_infos()) {
4288 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
4289 }
4290 desc = callee()->pc()->local_description()->description();
4291 for (const cricket::TransportInfo& info : desc->transport_infos()) {
4292 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
4293 }
4294 MediaExpectations media_expectations;
4295 media_expectations.ExpectBidirectionalAudioAndVideo();
4296 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4297 }
4298
4299 // With a max bundle policy and RTCP muxing, adding a new media description to
4300 // the connection should not affect ICE at all because the new media will use
4301 // the existing connection.
TEST_P(PeerConnectionIntegrationTest,AddMediaToConnectedBundleDoesNotRestartIce)4302 TEST_P(PeerConnectionIntegrationTest,
4303 AddMediaToConnectedBundleDoesNotRestartIce) {
4304 PeerConnectionInterface::RTCConfiguration config;
4305 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4306 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4307 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4308 config, PeerConnectionInterface::RTCConfiguration()));
4309 ConnectFakeSignaling();
4310
4311 caller()->AddAudioTrack();
4312 caller()->CreateAndSetAndSignalOffer();
4313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4314 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4315 caller()->ice_connection_state(), kDefaultTimeout);
4316
4317 caller()->clear_ice_connection_state_history();
4318
4319 caller()->AddVideoTrack();
4320 caller()->CreateAndSetAndSignalOffer();
4321 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4322
4323 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4324 }
4325
4326 // This test sets up a call between two parties with audio and video. It then
4327 // renegotiates setting the video m-line to "port 0", then later renegotiates
4328 // again, enabling video.
TEST_P(PeerConnectionIntegrationTest,VideoFlowsAfterMediaSectionIsRejectedAndRecycled)4329 TEST_P(PeerConnectionIntegrationTest,
4330 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4331 ASSERT_TRUE(CreatePeerConnectionWrappers());
4332 ConnectFakeSignaling();
4333
4334 // Do initial negotiation, only sending media from the caller. Will result in
4335 // video and audio recvonly "m=" sections.
4336 caller()->AddAudioVideoTracks();
4337 caller()->CreateAndSetAndSignalOffer();
4338 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4339
4340 // Negotiate again, disabling the video "m=" section (the callee will set the
4341 // port to 0 due to offer_to_receive_video = 0).
4342 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4343 PeerConnectionInterface::RTCOfferAnswerOptions options;
4344 options.offer_to_receive_video = 0;
4345 callee()->SetOfferAnswerOptions(options);
4346 } else {
4347 callee()->SetRemoteOfferHandler([this] {
4348 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4349 });
4350 }
4351 caller()->CreateAndSetAndSignalOffer();
4352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4353 // Sanity check that video "m=" section was actually rejected.
4354 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4355 callee()->pc()->local_description()->description());
4356 ASSERT_NE(nullptr, answer_video_content);
4357 ASSERT_TRUE(answer_video_content->rejected);
4358
4359 // Enable video and do negotiation again, making sure video is received
4360 // end-to-end, also adding media stream to callee.
4361 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4362 PeerConnectionInterface::RTCOfferAnswerOptions options;
4363 options.offer_to_receive_video = 1;
4364 callee()->SetOfferAnswerOptions(options);
4365 } else {
4366 // The caller's transceiver is stopped, so we need to add another track.
4367 auto caller_transceiver =
4368 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4369 EXPECT_TRUE(caller_transceiver->stopped());
4370 caller()->AddVideoTrack();
4371 }
4372 callee()->AddVideoTrack();
4373 callee()->SetRemoteOfferHandler(nullptr);
4374 caller()->CreateAndSetAndSignalOffer();
4375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4376
4377 // Verify the caller receives frames from the newly added stream, and the
4378 // callee receives additional frames from the re-enabled video m= section.
4379 MediaExpectations media_expectations;
4380 media_expectations.CalleeExpectsSomeAudio();
4381 media_expectations.ExpectBidirectionalVideo();
4382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4383 }
4384
4385 // This tests that if we negotiate after calling CreateSender but before we
4386 // have a track, then set a track later, frames from the newly-set track are
4387 // received end-to-end.
TEST_F(PeerConnectionIntegrationTestPlanB,MediaFlowsAfterEarlyWarmupWithCreateSender)4388 TEST_F(PeerConnectionIntegrationTestPlanB,
4389 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4390 ASSERT_TRUE(CreatePeerConnectionWrappers());
4391 ConnectFakeSignaling();
4392 auto caller_audio_sender =
4393 caller()->pc()->CreateSender("audio", "caller_stream");
4394 auto caller_video_sender =
4395 caller()->pc()->CreateSender("video", "caller_stream");
4396 auto callee_audio_sender =
4397 callee()->pc()->CreateSender("audio", "callee_stream");
4398 auto callee_video_sender =
4399 callee()->pc()->CreateSender("video", "callee_stream");
4400 caller()->CreateAndSetAndSignalOffer();
4401 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4402 // Wait for ICE to complete, without any tracks being set.
4403 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4404 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4405 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4406 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4407 // Now set the tracks, and expect frames to immediately start flowing.
4408 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4409 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4410 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4411 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4412 MediaExpectations media_expectations;
4413 media_expectations.ExpectBidirectionalAudioAndVideo();
4414 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4415 }
4416
4417 // This tests that if we negotiate after calling AddTransceiver but before we
4418 // have a track, then set a track later, frames from the newly-set tracks are
4419 // received end-to-end.
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,MediaFlowsAfterEarlyWarmupWithAddTransceiver)4420 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4421 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4422 ASSERT_TRUE(CreatePeerConnectionWrappers());
4423 ConnectFakeSignaling();
4424 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4425 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4426 auto caller_audio_sender = audio_result.MoveValue()->sender();
4427 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4428 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4429 auto caller_video_sender = video_result.MoveValue()->sender();
4430 callee()->SetRemoteOfferHandler([this] {
4431 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4432 callee()->pc()->GetTransceivers()[0]->SetDirection(
4433 RtpTransceiverDirection::kSendRecv);
4434 callee()->pc()->GetTransceivers()[1]->SetDirection(
4435 RtpTransceiverDirection::kSendRecv);
4436 });
4437 caller()->CreateAndSetAndSignalOffer();
4438 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4439 // Wait for ICE to complete, without any tracks being set.
4440 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4441 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4442 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4443 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4444 // Now set the tracks, and expect frames to immediately start flowing.
4445 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4446 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4447 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4448 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4449 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4450 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4451 MediaExpectations media_expectations;
4452 media_expectations.ExpectBidirectionalAudioAndVideo();
4453 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4454 }
4455
4456 // This test verifies that a remote video track can be added via AddStream,
4457 // and sent end-to-end. For this particular test, it's simply echoed back
4458 // from the caller to the callee, rather than being forwarded to a third
4459 // PeerConnection.
TEST_F(PeerConnectionIntegrationTestPlanB,CanSendRemoteVideoTrack)4460 TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
4461 ASSERT_TRUE(CreatePeerConnectionWrappers());
4462 ConnectFakeSignaling();
4463 // Just send a video track from the caller.
4464 caller()->AddVideoTrack();
4465 caller()->CreateAndSetAndSignalOffer();
4466 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4467 ASSERT_EQ(1U, callee()->remote_streams()->count());
4468
4469 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4470 // time).
4471 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4472 callee()->CreateAndSetAndSignalOffer();
4473 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4474
4475 MediaExpectations media_expectations;
4476 media_expectations.ExpectBidirectionalVideo();
4477 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4478 }
4479
4480 // Test that we achieve the expected end-to-end connection time, using a
4481 // fake clock and simulated latency on the media and signaling paths.
4482 // We use a TURN<->TURN connection because this is usually the quickest to
4483 // set up initially, especially when we're confident the connection will work
4484 // and can start sending media before we get a STUN response.
4485 //
4486 // With various optimizations enabled, here are the network delays we expect to
4487 // be on the critical path:
4488 // 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4489 // signaling answer (with DTLS fingerprint).
4490 // 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4491 // using TURN<->TURN pair, and DTLS exchange is 4 packets,
4492 // the first of which should have arrived before the answer.
TEST_P(PeerConnectionIntegrationTestWithFakeClock,EndToEndConnectionTimeWithTurnTurnPair)4493 TEST_P(PeerConnectionIntegrationTestWithFakeClock,
4494 EndToEndConnectionTimeWithTurnTurnPair) {
4495 static constexpr int media_hop_delay_ms = 50;
4496 static constexpr int signaling_trip_delay_ms = 500;
4497 // For explanation of these values, see comment above.
4498 static constexpr int required_media_hops = 9;
4499 static constexpr int required_signaling_trips = 2;
4500 // For internal delays (such as posting an event asychronously).
4501 static constexpr int allowed_internal_delay_ms = 20;
4502 static constexpr int total_connection_time_ms =
4503 media_hop_delay_ms * required_media_hops +
4504 signaling_trip_delay_ms * required_signaling_trips +
4505 allowed_internal_delay_ms;
4506
4507 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4508 3478};
4509 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4510 0};
4511 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4512 3478};
4513 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4514 0};
4515 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4516 turn_server_1_internal_address, turn_server_1_external_address);
4517
4518 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4519 turn_server_2_internal_address, turn_server_2_external_address);
4520 // Bypass permission check on received packets so media can be sent before
4521 // the candidate is signaled.
4522 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4523 turn_server_1->set_enable_permission_checks(false);
4524 });
4525 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4526 turn_server_2->set_enable_permission_checks(false);
4527 });
4528
4529 PeerConnectionInterface::RTCConfiguration client_1_config;
4530 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4531 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4532 ice_server_1.username = "test";
4533 ice_server_1.password = "test";
4534 client_1_config.servers.push_back(ice_server_1);
4535 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4536 client_1_config.presume_writable_when_fully_relayed = true;
4537
4538 PeerConnectionInterface::RTCConfiguration client_2_config;
4539 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4540 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4541 ice_server_2.username = "test";
4542 ice_server_2.password = "test";
4543 client_2_config.servers.push_back(ice_server_2);
4544 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4545 client_2_config.presume_writable_when_fully_relayed = true;
4546
4547 ASSERT_TRUE(
4548 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4549 // Set up the simulated delays.
4550 SetSignalingDelayMs(signaling_trip_delay_ms);
4551 ConnectFakeSignaling();
4552 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4553 virtual_socket_server()->UpdateDelayDistribution();
4554
4555 // Set "offer to receive audio/video" without adding any tracks, so we just
4556 // set up ICE/DTLS with no media.
4557 PeerConnectionInterface::RTCOfferAnswerOptions options;
4558 options.offer_to_receive_audio = 1;
4559 options.offer_to_receive_video = 1;
4560 caller()->SetOfferAnswerOptions(options);
4561 caller()->CreateAndSetAndSignalOffer();
4562 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4563 FakeClock());
4564 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4565 // If this is not done a DCHECK can be hit in ports.cc, because a large
4566 // negative number is calculated for the rtt due to the global clock changing.
4567 ClosePeerConnections();
4568 }
4569
4570 // Verify that a TurnCustomizer passed in through RTCConfiguration
4571 // is actually used by the underlying TURN candidate pair.
4572 // Note that turnport_unittest.cc contains more detailed, lower-level tests.
TEST_P(PeerConnectionIntegrationTest,TurnCustomizerUsedForTurnConnections)4573 TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
4574 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4575 3478};
4576 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4577 0};
4578 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4579 3478};
4580 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4581 0};
4582 CreateTurnServer(turn_server_1_internal_address,
4583 turn_server_1_external_address);
4584 CreateTurnServer(turn_server_2_internal_address,
4585 turn_server_2_external_address);
4586
4587 PeerConnectionInterface::RTCConfiguration client_1_config;
4588 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4589 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4590 ice_server_1.username = "test";
4591 ice_server_1.password = "test";
4592 client_1_config.servers.push_back(ice_server_1);
4593 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4594 auto* customizer1 = CreateTurnCustomizer();
4595 client_1_config.turn_customizer = customizer1;
4596
4597 PeerConnectionInterface::RTCConfiguration client_2_config;
4598 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4599 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4600 ice_server_2.username = "test";
4601 ice_server_2.password = "test";
4602 client_2_config.servers.push_back(ice_server_2);
4603 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4604 auto* customizer2 = CreateTurnCustomizer();
4605 client_2_config.turn_customizer = customizer2;
4606
4607 ASSERT_TRUE(
4608 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4609 ConnectFakeSignaling();
4610
4611 // Set "offer to receive audio/video" without adding any tracks, so we just
4612 // set up ICE/DTLS with no media.
4613 PeerConnectionInterface::RTCOfferAnswerOptions options;
4614 options.offer_to_receive_audio = 1;
4615 options.offer_to_receive_video = 1;
4616 caller()->SetOfferAnswerOptions(options);
4617 caller()->CreateAndSetAndSignalOffer();
4618 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4619
4620 ExpectTurnCustomizerCountersIncremented(customizer1);
4621 ExpectTurnCustomizerCountersIncremented(customizer2);
4622 }
4623
4624 // Verifies that you can use TCP instead of UDP to connect to a TURN server and
4625 // send media between the caller and the callee.
TEST_P(PeerConnectionIntegrationTest,TCPUsedForTurnConnections)4626 TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4627 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4628 3478};
4629 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4630
4631 // Enable TCP for the fake turn server.
4632 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4633 cricket::PROTO_TCP);
4634
4635 webrtc::PeerConnectionInterface::IceServer ice_server;
4636 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4637 ice_server.username = "test";
4638 ice_server.password = "test";
4639
4640 PeerConnectionInterface::RTCConfiguration client_1_config;
4641 client_1_config.servers.push_back(ice_server);
4642 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4643
4644 PeerConnectionInterface::RTCConfiguration client_2_config;
4645 client_2_config.servers.push_back(ice_server);
4646 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4647
4648 ASSERT_TRUE(
4649 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4650
4651 // Do normal offer/answer and wait for ICE to complete.
4652 ConnectFakeSignaling();
4653 caller()->AddAudioVideoTracks();
4654 callee()->AddAudioVideoTracks();
4655 caller()->CreateAndSetAndSignalOffer();
4656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4657 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4658 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4659
4660 MediaExpectations media_expectations;
4661 media_expectations.ExpectBidirectionalAudioAndVideo();
4662 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4663 }
4664
4665 // Verify that a SSLCertificateVerifier passed in through
4666 // PeerConnectionDependencies is actually used by the underlying SSL
4667 // implementation to determine whether a certificate presented by the TURN
4668 // server is accepted by the client. Note that openssladapter_unittest.cc
4669 // contains more detailed, lower-level tests.
TEST_P(PeerConnectionIntegrationTest,SSLCertificateVerifierUsedForTurnConnections)4670 TEST_P(PeerConnectionIntegrationTest,
4671 SSLCertificateVerifierUsedForTurnConnections) {
4672 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4673 3478};
4674 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4675
4676 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4677 // that host name verification passes on the fake certificate.
4678 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4679 cricket::PROTO_TLS, "88.88.88.0");
4680
4681 webrtc::PeerConnectionInterface::IceServer ice_server;
4682 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4683 ice_server.username = "test";
4684 ice_server.password = "test";
4685
4686 PeerConnectionInterface::RTCConfiguration client_1_config;
4687 client_1_config.servers.push_back(ice_server);
4688 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4689
4690 PeerConnectionInterface::RTCConfiguration client_2_config;
4691 client_2_config.servers.push_back(ice_server);
4692 // Setting the type to kRelay forces the connection to go through a TURN
4693 // server.
4694 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4695
4696 // Get a copy to the pointer so we can verify calls later.
4697 rtc::TestCertificateVerifier* client_1_cert_verifier =
4698 new rtc::TestCertificateVerifier();
4699 client_1_cert_verifier->verify_certificate_ = true;
4700 rtc::TestCertificateVerifier* client_2_cert_verifier =
4701 new rtc::TestCertificateVerifier();
4702 client_2_cert_verifier->verify_certificate_ = true;
4703
4704 // Create the dependencies with the test certificate verifier.
4705 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4706 client_1_deps.tls_cert_verifier =
4707 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4708 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4709 client_2_deps.tls_cert_verifier =
4710 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4711
4712 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4713 client_1_config, std::move(client_1_deps), client_2_config,
4714 std::move(client_2_deps)));
4715 ConnectFakeSignaling();
4716
4717 // Set "offer to receive audio/video" without adding any tracks, so we just
4718 // set up ICE/DTLS with no media.
4719 PeerConnectionInterface::RTCOfferAnswerOptions options;
4720 options.offer_to_receive_audio = 1;
4721 options.offer_to_receive_video = 1;
4722 caller()->SetOfferAnswerOptions(options);
4723 caller()->CreateAndSetAndSignalOffer();
4724 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4725
4726 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4727 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4728 }
4729
TEST_P(PeerConnectionIntegrationTest,SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection)4730 TEST_P(PeerConnectionIntegrationTest,
4731 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4732 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4733 3478};
4734 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4735
4736 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4737 // that host name verification passes on the fake certificate.
4738 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4739 cricket::PROTO_TLS, "88.88.88.0");
4740
4741 webrtc::PeerConnectionInterface::IceServer ice_server;
4742 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4743 ice_server.username = "test";
4744 ice_server.password = "test";
4745
4746 PeerConnectionInterface::RTCConfiguration client_1_config;
4747 client_1_config.servers.push_back(ice_server);
4748 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4749
4750 PeerConnectionInterface::RTCConfiguration client_2_config;
4751 client_2_config.servers.push_back(ice_server);
4752 // Setting the type to kRelay forces the connection to go through a TURN
4753 // server.
4754 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4755
4756 // Get a copy to the pointer so we can verify calls later.
4757 rtc::TestCertificateVerifier* client_1_cert_verifier =
4758 new rtc::TestCertificateVerifier();
4759 client_1_cert_verifier->verify_certificate_ = false;
4760 rtc::TestCertificateVerifier* client_2_cert_verifier =
4761 new rtc::TestCertificateVerifier();
4762 client_2_cert_verifier->verify_certificate_ = false;
4763
4764 // Create the dependencies with the test certificate verifier.
4765 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4766 client_1_deps.tls_cert_verifier =
4767 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4768 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4769 client_2_deps.tls_cert_verifier =
4770 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4771
4772 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4773 client_1_config, std::move(client_1_deps), client_2_config,
4774 std::move(client_2_deps)));
4775 ConnectFakeSignaling();
4776
4777 // Set "offer to receive audio/video" without adding any tracks, so we just
4778 // set up ICE/DTLS with no media.
4779 PeerConnectionInterface::RTCOfferAnswerOptions options;
4780 options.offer_to_receive_audio = 1;
4781 options.offer_to_receive_video = 1;
4782 caller()->SetOfferAnswerOptions(options);
4783 caller()->CreateAndSetAndSignalOffer();
4784 bool wait_res = true;
4785 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4786 // properly, should be able to just wait for a state of "failed" instead of
4787 // waiting a fixed 10 seconds.
4788 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4789 ASSERT_FALSE(wait_res);
4790
4791 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4792 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4793 }
4794
4795 // Test that the injected ICE transport factory is used to create ICE transports
4796 // for WebRTC connections.
TEST_P(PeerConnectionIntegrationTest,IceTransportFactoryUsedForConnections)4797 TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
4798 PeerConnectionInterface::RTCConfiguration default_config;
4799 PeerConnectionDependencies dependencies(nullptr);
4800 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
4801 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
4802 dependencies.ice_transport_factory = std::move(ice_transport_factory);
4803 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
4804 std::move(dependencies), nullptr,
4805 /*reset_encoder_factory=*/false,
4806 /*reset_decoder_factory=*/false);
4807 ASSERT_TRUE(wrapper);
4808 wrapper->CreateDataChannel();
4809 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
4810 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
4811 wrapper->pc()->SetLocalDescription(observer,
4812 wrapper->CreateOfferAndWait().release());
4813 }
4814
4815 // Test that audio and video flow end-to-end when codec names don't use the
4816 // expected casing, given that they're supposed to be case insensitive. To test
4817 // this, all but one codec is removed from each media description, and its
4818 // casing is changed.
4819 //
4820 // In the past, this has regressed and caused crashes/black video, due to the
4821 // fact that code at some layers was doing case-insensitive comparisons and
4822 // code at other layers was not.
TEST_P(PeerConnectionIntegrationTest,CodecNamesAreCaseInsensitive)4823 TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
4824 ASSERT_TRUE(CreatePeerConnectionWrappers());
4825 ConnectFakeSignaling();
4826 caller()->AddAudioVideoTracks();
4827 callee()->AddAudioVideoTracks();
4828
4829 // Remove all but one audio/video codec (opus and VP8), and change the
4830 // casing of the caller's generated offer.
4831 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4832 cricket::AudioContentDescription* audio =
4833 GetFirstAudioContentDescription(description);
4834 ASSERT_NE(nullptr, audio);
4835 auto audio_codecs = audio->codecs();
4836 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4837 [](const cricket::AudioCodec& codec) {
4838 return codec.name != "opus";
4839 }),
4840 audio_codecs.end());
4841 ASSERT_EQ(1u, audio_codecs.size());
4842 audio_codecs[0].name = "OpUs";
4843 audio->set_codecs(audio_codecs);
4844
4845 cricket::VideoContentDescription* video =
4846 GetFirstVideoContentDescription(description);
4847 ASSERT_NE(nullptr, video);
4848 auto video_codecs = video->codecs();
4849 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4850 [](const cricket::VideoCodec& codec) {
4851 return codec.name != "VP8";
4852 }),
4853 video_codecs.end());
4854 ASSERT_EQ(1u, video_codecs.size());
4855 video_codecs[0].name = "vP8";
4856 video->set_codecs(video_codecs);
4857 });
4858
4859 caller()->CreateAndSetAndSignalOffer();
4860 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4861
4862 // Verify frames are still received end-to-end.
4863 MediaExpectations media_expectations;
4864 media_expectations.ExpectBidirectionalAudioAndVideo();
4865 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4866 }
4867
TEST_P(PeerConnectionIntegrationTest,GetSourcesAudio)4868 TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
4869 ASSERT_TRUE(CreatePeerConnectionWrappers());
4870 ConnectFakeSignaling();
4871 caller()->AddAudioTrack();
4872 caller()->CreateAndSetAndSignalOffer();
4873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4874 // Wait for one audio frame to be received by the callee.
4875 MediaExpectations media_expectations;
4876 media_expectations.CalleeExpectsSomeAudio(1);
4877 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4878 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4879 auto receiver = callee()->pc()->GetReceivers()[0];
4880 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4881 auto sources = receiver->GetSources();
4882 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4883 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4884 sources[0].source_id());
4885 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4886 }
4887
TEST_P(PeerConnectionIntegrationTest,GetSourcesVideo)4888 TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4889 ASSERT_TRUE(CreatePeerConnectionWrappers());
4890 ConnectFakeSignaling();
4891 caller()->AddVideoTrack();
4892 caller()->CreateAndSetAndSignalOffer();
4893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4894 // Wait for one video frame to be received by the callee.
4895 MediaExpectations media_expectations;
4896 media_expectations.CalleeExpectsSomeVideo(1);
4897 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4898 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4899 auto receiver = callee()->pc()->GetReceivers()[0];
4900 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4901 auto sources = receiver->GetSources();
4902 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4903 ASSERT_GT(sources.size(), 0u);
4904 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4905 sources[0].source_id());
4906 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4907 }
4908
4909 // Test that if a track is removed and added again with a different stream ID,
4910 // the new stream ID is successfully communicated in SDP and media continues to
4911 // flow end-to-end.
4912 // TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4913 // it will not reuse a transceiver that has already been sending. After creating
4914 // a new transceiver it tries to create an offer with two senders of the same
4915 // track ids and it fails.
TEST_F(PeerConnectionIntegrationTestPlanB,RemoveAndAddTrackWithNewStreamId)4916 TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
4917 ASSERT_TRUE(CreatePeerConnectionWrappers());
4918 ConnectFakeSignaling();
4919
4920 // Add track using stream 1, do offer/answer.
4921 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4922 caller()->CreateLocalAudioTrack();
4923 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4924 caller()->AddTrack(track, {"stream_1"});
4925 caller()->CreateAndSetAndSignalOffer();
4926 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4927 {
4928 MediaExpectations media_expectations;
4929 media_expectations.CalleeExpectsSomeAudio(1);
4930 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4931 }
4932 // Remove the sender, and create a new one with the new stream.
4933 caller()->pc()->RemoveTrack(sender);
4934 sender = caller()->AddTrack(track, {"stream_2"});
4935 caller()->CreateAndSetAndSignalOffer();
4936 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4937 // Wait for additional audio frames to be received by the callee.
4938 {
4939 MediaExpectations media_expectations;
4940 media_expectations.CalleeExpectsSomeAudio();
4941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4942 }
4943 }
4944
TEST_P(PeerConnectionIntegrationTest,RtcEventLogOutputWriteCalled)4945 TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
4946 ASSERT_TRUE(CreatePeerConnectionWrappers());
4947 ConnectFakeSignaling();
4948
4949 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
4950 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4951 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
4952 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
4953 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4954 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
4955
4956 caller()->AddAudioVideoTracks();
4957 caller()->CreateAndSetAndSignalOffer();
4958 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4959 }
4960
4961 // Test that if candidates are only signaled by applying full session
4962 // descriptions (instead of using AddIceCandidate), the peers can connect to
4963 // each other and exchange media.
TEST_P(PeerConnectionIntegrationTest,MediaFlowsWhenCandidatesSetOnlyInSdp)4964 TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
4965 ASSERT_TRUE(CreatePeerConnectionWrappers());
4966 // Each side will signal the session descriptions but not candidates.
4967 ConnectFakeSignalingForSdpOnly();
4968
4969 // Add audio video track and exchange the initial offer/answer with media
4970 // information only. This will start ICE gathering on each side.
4971 caller()->AddAudioVideoTracks();
4972 callee()->AddAudioVideoTracks();
4973 caller()->CreateAndSetAndSignalOffer();
4974
4975 // Wait for all candidates to be gathered on both the caller and callee.
4976 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4977 caller()->ice_gathering_state(), kDefaultTimeout);
4978 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4979 callee()->ice_gathering_state(), kDefaultTimeout);
4980
4981 // The candidates will now be included in the session description, so
4982 // signaling them will start the ICE connection.
4983 caller()->CreateAndSetAndSignalOffer();
4984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4985
4986 // Ensure that media flows in both directions.
4987 MediaExpectations media_expectations;
4988 media_expectations.ExpectBidirectionalAudioAndVideo();
4989 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4990 }
4991
4992 // Test that SetAudioPlayout can be used to disable audio playout from the
4993 // start, then later enable it. This may be useful, for example, if the caller
4994 // needs to play a local ringtone until some event occurs, after which it
4995 // switches to playing the received audio.
TEST_P(PeerConnectionIntegrationTest,DisableAndEnableAudioPlayout)4996 TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
4997 ASSERT_TRUE(CreatePeerConnectionWrappers());
4998 ConnectFakeSignaling();
4999
5000 // Set up audio-only call where audio playout is disabled on caller's side.
5001 caller()->pc()->SetAudioPlayout(false);
5002 caller()->AddAudioTrack();
5003 callee()->AddAudioTrack();
5004 caller()->CreateAndSetAndSignalOffer();
5005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5006
5007 // Pump messages for a second.
5008 WAIT(false, 1000);
5009 // Since audio playout is disabled, the caller shouldn't have received
5010 // anything (at the playout level, at least).
5011 EXPECT_EQ(0, caller()->audio_frames_received());
5012 // As a sanity check, make sure the callee (for which playout isn't disabled)
5013 // did still see frames on its audio level.
5014 ASSERT_GT(callee()->audio_frames_received(), 0);
5015
5016 // Enable playout again, and ensure audio starts flowing.
5017 caller()->pc()->SetAudioPlayout(true);
5018 MediaExpectations media_expectations;
5019 media_expectations.ExpectBidirectionalAudio();
5020 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5021 }
5022
GetAudioEnergyStat(PeerConnectionWrapper * pc)5023 double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5024 auto report = pc->NewGetStats();
5025 auto track_stats_list =
5026 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5027 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5028 for (const auto* track_stats : track_stats_list) {
5029 if (track_stats->remote_source.is_defined() &&
5030 *track_stats->remote_source) {
5031 remote_track_stats = track_stats;
5032 break;
5033 }
5034 }
5035
5036 if (!remote_track_stats->total_audio_energy.is_defined()) {
5037 return 0.0;
5038 }
5039 return *remote_track_stats->total_audio_energy;
5040 }
5041
5042 // Test that if audio playout is disabled via the SetAudioPlayout() method, then
5043 // incoming audio is still processed and statistics are generated.
TEST_P(PeerConnectionIntegrationTest,DisableAudioPlayoutStillGeneratesAudioStats)5044 TEST_P(PeerConnectionIntegrationTest,
5045 DisableAudioPlayoutStillGeneratesAudioStats) {
5046 ASSERT_TRUE(CreatePeerConnectionWrappers());
5047 ConnectFakeSignaling();
5048
5049 // Set up audio-only call where playout is disabled but audio-processing is
5050 // still active.
5051 caller()->AddAudioTrack();
5052 callee()->AddAudioTrack();
5053 caller()->pc()->SetAudioPlayout(false);
5054
5055 caller()->CreateAndSetAndSignalOffer();
5056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5057
5058 // Wait for the callee to receive audio stats.
5059 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5060 }
5061
5062 // Test that SetAudioRecording can be used to disable audio recording from the
5063 // start, then later enable it. This may be useful, for example, if the caller
5064 // wants to ensure that no audio resources are active before a certain state
5065 // is reached.
TEST_P(PeerConnectionIntegrationTest,DisableAndEnableAudioRecording)5066 TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
5067 ASSERT_TRUE(CreatePeerConnectionWrappers());
5068 ConnectFakeSignaling();
5069
5070 // Set up audio-only call where audio recording is disabled on caller's side.
5071 caller()->pc()->SetAudioRecording(false);
5072 caller()->AddAudioTrack();
5073 callee()->AddAudioTrack();
5074 caller()->CreateAndSetAndSignalOffer();
5075 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5076
5077 // Pump messages for a second.
5078 WAIT(false, 1000);
5079 // Since caller has disabled audio recording, the callee shouldn't have
5080 // received anything.
5081 EXPECT_EQ(0, callee()->audio_frames_received());
5082 // As a sanity check, make sure the caller did still see frames on its
5083 // audio level since audio recording is enabled on the calle side.
5084 ASSERT_GT(caller()->audio_frames_received(), 0);
5085
5086 // Enable audio recording again, and ensure audio starts flowing.
5087 caller()->pc()->SetAudioRecording(true);
5088 MediaExpectations media_expectations;
5089 media_expectations.ExpectBidirectionalAudio();
5090 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5091 }
5092
5093 // Test that after closing PeerConnections, they stop sending any packets (ICE,
5094 // DTLS, RTP...).
TEST_P(PeerConnectionIntegrationTest,ClosingConnectionStopsPacketFlow)5095 TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
5096 // Set up audio/video/data, wait for some frames to be received.
5097 ASSERT_TRUE(CreatePeerConnectionWrappers());
5098 ConnectFakeSignaling();
5099 caller()->AddAudioVideoTracks();
5100 #ifdef HAVE_SCTP
5101 caller()->CreateDataChannel();
5102 #endif
5103 caller()->CreateAndSetAndSignalOffer();
5104 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5105 MediaExpectations media_expectations;
5106 media_expectations.CalleeExpectsSomeAudioAndVideo();
5107 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5108 // Close PeerConnections.
5109 ClosePeerConnections();
5110 // Pump messages for a second, and ensure no new packets end up sent.
5111 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5112 WAIT(false, 1000);
5113 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5114 EXPECT_EQ(sent_packets_a, sent_packets_b);
5115 }
5116
5117 // Test that transport stats are generated by the RTCStatsCollector for a
5118 // connection that only involves data channels. This is a regression test for
5119 // crbug.com/826972.
5120 #ifdef HAVE_SCTP
TEST_P(PeerConnectionIntegrationTest,TransportStatsReportedForDataChannelOnlyConnection)5121 TEST_P(PeerConnectionIntegrationTest,
5122 TransportStatsReportedForDataChannelOnlyConnection) {
5123 ASSERT_TRUE(CreatePeerConnectionWrappers());
5124 ConnectFakeSignaling();
5125 caller()->CreateDataChannel();
5126
5127 caller()->CreateAndSetAndSignalOffer();
5128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5129 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5130
5131 auto caller_report = caller()->NewGetStats();
5132 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5133 auto callee_report = callee()->NewGetStats();
5134 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5135 }
5136 #endif // HAVE_SCTP
5137
TEST_P(PeerConnectionIntegrationTest,IceEventsGeneratedAndLoggedInRtcEventLog)5138 TEST_P(PeerConnectionIntegrationTest,
5139 IceEventsGeneratedAndLoggedInRtcEventLog) {
5140 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5141 ConnectFakeSignaling();
5142 PeerConnectionInterface::RTCOfferAnswerOptions options;
5143 options.offer_to_receive_audio = 1;
5144 caller()->SetOfferAnswerOptions(options);
5145 caller()->CreateAndSetAndSignalOffer();
5146 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5147 ASSERT_NE(nullptr, caller()->event_log_factory());
5148 ASSERT_NE(nullptr, callee()->event_log_factory());
5149 webrtc::FakeRtcEventLog* caller_event_log =
5150 static_cast<webrtc::FakeRtcEventLog*>(
5151 caller()->event_log_factory()->last_log_created());
5152 webrtc::FakeRtcEventLog* callee_event_log =
5153 static_cast<webrtc::FakeRtcEventLog*>(
5154 callee()->event_log_factory()->last_log_created());
5155 ASSERT_NE(nullptr, caller_event_log);
5156 ASSERT_NE(nullptr, callee_event_log);
5157 int caller_ice_config_count = caller_event_log->GetEventCount(
5158 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5159 int caller_ice_event_count = caller_event_log->GetEventCount(
5160 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5161 int callee_ice_config_count = callee_event_log->GetEventCount(
5162 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5163 int callee_ice_event_count = callee_event_log->GetEventCount(
5164 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5165 EXPECT_LT(0, caller_ice_config_count);
5166 EXPECT_LT(0, caller_ice_event_count);
5167 EXPECT_LT(0, callee_ice_config_count);
5168 EXPECT_LT(0, callee_ice_event_count);
5169 }
5170
TEST_P(PeerConnectionIntegrationTest,RegatherAfterChangingIceTransportType)5171 TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
5172 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5173 3478};
5174 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5175
5176 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5177
5178 webrtc::PeerConnectionInterface::IceServer ice_server;
5179 ice_server.urls.push_back("turn:88.88.88.0:3478");
5180 ice_server.username = "test";
5181 ice_server.password = "test";
5182
5183 PeerConnectionInterface::RTCConfiguration caller_config;
5184 caller_config.servers.push_back(ice_server);
5185 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5186 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5187 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
5188
5189 PeerConnectionInterface::RTCConfiguration callee_config;
5190 callee_config.servers.push_back(ice_server);
5191 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5192 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5193 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
5194
5195 ASSERT_TRUE(
5196 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5197
5198 // Do normal offer/answer and wait for ICE to complete.
5199 ConnectFakeSignaling();
5200 caller()->AddAudioVideoTracks();
5201 callee()->AddAudioVideoTracks();
5202 caller()->CreateAndSetAndSignalOffer();
5203 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5204 // Since we are doing continual gathering, the ICE transport does not reach
5205 // kIceGatheringComplete (see
5206 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5207 // kIceConnectionComplete.
5208 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5209 caller()->ice_connection_state(), kDefaultTimeout);
5210 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5211 callee()->ice_connection_state(), kDefaultTimeout);
5212 // Note that we cannot use the metric
5213 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5214 // metric is only populated when we reach kIceConnectionComplete in the
5215 // current implementation.
5216 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5217 caller()->last_candidate_gathered().type());
5218 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5219 callee()->last_candidate_gathered().type());
5220
5221 // Loosen the caller's candidate filter.
5222 caller_config = caller()->pc()->GetConfiguration();
5223 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5224 caller()->pc()->SetConfiguration(caller_config);
5225 // We should have gathered a new host candidate.
5226 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5227 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5228
5229 // Loosen the callee's candidate filter.
5230 callee_config = callee()->pc()->GetConfiguration();
5231 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5232 callee()->pc()->SetConfiguration(callee_config);
5233 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5234 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5235
5236 // Create an offer and verify that it does not contain an ICE restart (i.e new
5237 // ice credentials).
5238 std::string caller_ufrag_pre_offer = caller()
5239 ->pc()
5240 ->local_description()
5241 ->description()
5242 ->transport_infos()[0]
5243 .description.ice_ufrag;
5244 caller()->CreateAndSetAndSignalOffer();
5245 std::string caller_ufrag_post_offer = caller()
5246 ->pc()
5247 ->local_description()
5248 ->description()
5249 ->transport_infos()[0]
5250 .description.ice_ufrag;
5251 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
5252 }
5253
TEST_P(PeerConnectionIntegrationTest,OnIceCandidateError)5254 TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
5255 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5256 3478};
5257 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5258
5259 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5260
5261 webrtc::PeerConnectionInterface::IceServer ice_server;
5262 ice_server.urls.push_back("turn:88.88.88.0:3478");
5263 ice_server.username = "test";
5264 ice_server.password = "123";
5265
5266 PeerConnectionInterface::RTCConfiguration caller_config;
5267 caller_config.servers.push_back(ice_server);
5268 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5269 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5270
5271 PeerConnectionInterface::RTCConfiguration callee_config;
5272 callee_config.servers.push_back(ice_server);
5273 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5274 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5275
5276 ASSERT_TRUE(
5277 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5278
5279 // Do normal offer/answer and wait for ICE to complete.
5280 ConnectFakeSignaling();
5281 caller()->AddAudioVideoTracks();
5282 callee()->AddAudioVideoTracks();
5283 caller()->CreateAndSetAndSignalOffer();
5284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5285 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5286 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5287 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
5288 EXPECT_NE(caller()->error_event().address, "");
5289 }
5290
TEST_P(PeerConnectionIntegrationTest,OnIceCandidateErrorWithEmptyAddress)5291 TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
5292 webrtc::PeerConnectionInterface::IceServer ice_server;
5293 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
5294 ice_server.username = "test";
5295 ice_server.password = "test";
5296
5297 PeerConnectionInterface::RTCConfiguration caller_config;
5298 caller_config.servers.push_back(ice_server);
5299 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5300 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5301
5302 PeerConnectionInterface::RTCConfiguration callee_config;
5303 callee_config.servers.push_back(ice_server);
5304 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5305 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5306
5307 ASSERT_TRUE(
5308 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5309
5310 // Do normal offer/answer and wait for ICE to complete.
5311 ConnectFakeSignaling();
5312 caller()->AddAudioVideoTracks();
5313 callee()->AddAudioVideoTracks();
5314 caller()->CreateAndSetAndSignalOffer();
5315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5316 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
5317 EXPECT_EQ(caller()->error_event().address, "");
5318 }
5319
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,AudioKeepsFlowingAfterImplicitRollback)5320 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5321 AudioKeepsFlowingAfterImplicitRollback) {
5322 PeerConnectionInterface::RTCConfiguration config;
5323 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5324 config.enable_implicit_rollback = true;
5325 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5326 ConnectFakeSignaling();
5327 caller()->AddAudioTrack();
5328 callee()->AddAudioTrack();
5329 caller()->CreateAndSetAndSignalOffer();
5330 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5331 MediaExpectations media_expectations;
5332 media_expectations.ExpectBidirectionalAudio();
5333 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5334 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5335 caller()->AddVideoTrack();
5336 callee()->AddVideoTrack();
5337 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5338 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5339 callee()->pc()->SetLocalDescription(observer,
5340 callee()->CreateOfferAndWait().release());
5341 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5342 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5344 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5345 }
5346
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,ImplicitRollbackVisitsStableState)5347 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5348 ImplicitRollbackVisitsStableState) {
5349 RTCConfiguration config;
5350 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5351 config.enable_implicit_rollback = true;
5352
5353 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5354
5355 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5356 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5357 callee()->pc()->SetLocalDescription(sld_observer,
5358 callee()->CreateOfferAndWait().release());
5359 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5360 EXPECT_EQ(sld_observer->error(), "");
5361
5362 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5363 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5364 callee()->pc()->SetRemoteDescription(
5365 srd_observer, caller()->CreateOfferAndWait().release());
5366 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5367 EXPECT_EQ(srd_observer->error(), "");
5368
5369 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5370 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5371 PeerConnectionInterface::kStable,
5372 PeerConnectionInterface::kHaveRemoteOffer));
5373 }
5374
5375 INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5376 PeerConnectionIntegrationTest,
5377 Values(SdpSemantics::kPlanB,
5378 SdpSemantics::kUnifiedPlan));
5379
5380 INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5381 PeerConnectionIntegrationTestWithFakeClock,
5382 Values(SdpSemantics::kPlanB,
5383 SdpSemantics::kUnifiedPlan));
5384
5385 // Tests that verify interoperability between Plan B and Unified Plan
5386 // PeerConnections.
5387 class PeerConnectionIntegrationInteropTest
5388 : public PeerConnectionIntegrationBaseTest,
5389 public ::testing::WithParamInterface<
5390 std::tuple<SdpSemantics, SdpSemantics>> {
5391 protected:
5392 // Setting the SdpSemantics for the base test to kDefault does not matter
5393 // because we specify not to use the test semantics when creating
5394 // PeerConnectionWrappers.
PeerConnectionIntegrationInteropTest()5395 PeerConnectionIntegrationInteropTest()
5396 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
5397 caller_semantics_(std::get<0>(GetParam())),
5398 callee_semantics_(std::get<1>(GetParam())) {}
5399
CreatePeerConnectionWrappersWithSemantics()5400 bool CreatePeerConnectionWrappersWithSemantics() {
5401 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5402 callee_semantics_);
5403 }
5404
5405 const SdpSemantics caller_semantics_;
5406 const SdpSemantics callee_semantics_;
5407 };
5408
TEST_P(PeerConnectionIntegrationInteropTest,NoMediaLocalToNoMediaRemote)5409 TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5410 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5411 ConnectFakeSignaling();
5412
5413 caller()->CreateAndSetAndSignalOffer();
5414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5415 }
5416
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioLocalToNoMediaRemote)5417 TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5418 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5419 ConnectFakeSignaling();
5420 auto audio_sender = caller()->AddAudioTrack();
5421
5422 caller()->CreateAndSetAndSignalOffer();
5423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5424
5425 // Verify that one audio receiver has been created on the remote and that it
5426 // has the same track ID as the sending track.
5427 auto receivers = callee()->pc()->GetReceivers();
5428 ASSERT_EQ(1u, receivers.size());
5429 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5430 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5431
5432 MediaExpectations media_expectations;
5433 media_expectations.CalleeExpectsSomeAudio();
5434 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5435 }
5436
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioOneVideoToNoMediaRemote)5437 TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5438 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5439 ConnectFakeSignaling();
5440 auto video_sender = caller()->AddVideoTrack();
5441 auto audio_sender = caller()->AddAudioTrack();
5442
5443 caller()->CreateAndSetAndSignalOffer();
5444 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5445
5446 // Verify that one audio and one video receiver have been created on the
5447 // remote and that they have the same track IDs as the sending tracks.
5448 auto audio_receivers =
5449 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5450 ASSERT_EQ(1u, audio_receivers.size());
5451 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5452 auto video_receivers =
5453 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5454 ASSERT_EQ(1u, video_receivers.size());
5455 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5456
5457 MediaExpectations media_expectations;
5458 media_expectations.CalleeExpectsSomeAudioAndVideo();
5459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5460 }
5461
TEST_P(PeerConnectionIntegrationInteropTest,OneAudioOneVideoLocalToOneAudioOneVideoRemote)5462 TEST_P(PeerConnectionIntegrationInteropTest,
5463 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5464 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5465 ConnectFakeSignaling();
5466 caller()->AddAudioVideoTracks();
5467 callee()->AddAudioVideoTracks();
5468
5469 caller()->CreateAndSetAndSignalOffer();
5470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5471
5472 MediaExpectations media_expectations;
5473 media_expectations.ExpectBidirectionalAudioAndVideo();
5474 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5475 }
5476
TEST_P(PeerConnectionIntegrationInteropTest,ReverseRolesOneAudioLocalToOneVideoRemote)5477 TEST_P(PeerConnectionIntegrationInteropTest,
5478 ReverseRolesOneAudioLocalToOneVideoRemote) {
5479 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5480 ConnectFakeSignaling();
5481 caller()->AddAudioTrack();
5482 callee()->AddVideoTrack();
5483
5484 caller()->CreateAndSetAndSignalOffer();
5485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5486
5487 // Verify that only the audio track has been negotiated.
5488 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5489 // Might also check that the callee's NegotiationNeeded flag is set.
5490
5491 // Reverse roles.
5492 callee()->CreateAndSetAndSignalOffer();
5493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5494
5495 MediaExpectations media_expectations;
5496 media_expectations.CallerExpectsSomeVideo();
5497 media_expectations.CalleeExpectsSomeAudio();
5498 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5499 }
5500
5501 INSTANTIATE_TEST_SUITE_P(
5502 PeerConnectionIntegrationTest,
5503 PeerConnectionIntegrationInteropTest,
5504 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5505 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5506
5507 // Test that if the Unified Plan side offers two video tracks then the Plan B
5508 // side will only see the first one and ignore the second.
TEST_F(PeerConnectionIntegrationTestPlanB,TwoVideoUnifiedPlanToNoMediaPlanB)5509 TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
5510 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5511 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
5512 ConnectFakeSignaling();
5513 auto first_sender = caller()->AddVideoTrack();
5514 caller()->AddVideoTrack();
5515
5516 caller()->CreateAndSetAndSignalOffer();
5517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5518
5519 // Verify that there is only one receiver and it corresponds to the first
5520 // added track.
5521 auto receivers = callee()->pc()->GetReceivers();
5522 ASSERT_EQ(1u, receivers.size());
5523 EXPECT_TRUE(receivers[0]->track()->enabled());
5524 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5525
5526 MediaExpectations media_expectations;
5527 media_expectations.CalleeExpectsSomeVideo();
5528 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5529 }
5530
5531 // Test that if the initial offer tagged BUNDLE section is rejected due to its
5532 // associated RtpTransceiver being stopped and another transceiver is added,
5533 // then renegotiation causes the callee to receive the new video track without
5534 // error.
5535 // This is a regression test for bugs.webrtc.org/9954
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,ReOfferWithStoppedBundleTaggedTransceiver)5536 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5537 ReOfferWithStoppedBundleTaggedTransceiver) {
5538 RTCConfiguration config;
5539 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5540 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5541 ConnectFakeSignaling();
5542 auto audio_transceiver_or_error =
5543 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5544 ASSERT_TRUE(audio_transceiver_or_error.ok());
5545 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5546
5547 caller()->CreateAndSetAndSignalOffer();
5548 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5549 {
5550 MediaExpectations media_expectations;
5551 media_expectations.CalleeExpectsSomeAudio();
5552 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5553 }
5554
5555 audio_transceiver->Stop();
5556 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5557
5558 caller()->CreateAndSetAndSignalOffer();
5559 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5560 {
5561 MediaExpectations media_expectations;
5562 media_expectations.CalleeExpectsSomeVideo();
5563 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5564 }
5565 }
5566
5567 #ifdef HAVE_SCTP
5568
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithBundledSctpDataChannel)5569 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5570 EndToEndCallWithBundledSctpDataChannel) {
5571 ASSERT_TRUE(CreatePeerConnectionWrappers());
5572 ConnectFakeSignaling();
5573 caller()->CreateDataChannel();
5574 caller()->AddAudioVideoTracks();
5575 callee()->AddAudioVideoTracks();
5576 caller()->CreateAndSetAndSignalOffer();
5577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5578 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
5579 caller()->pc()->GetSctpTransport()->Information().state(),
5580 kDefaultTimeout);
5581 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5582 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5583 }
5584
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,EndToEndCallWithDataChannelOnlyConnects)5585 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5586 EndToEndCallWithDataChannelOnlyConnects) {
5587 ASSERT_TRUE(CreatePeerConnectionWrappers());
5588 ConnectFakeSignaling();
5589 caller()->CreateDataChannel();
5590 caller()->CreateAndSetAndSignalOffer();
5591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5592 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5593 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5594 ASSERT_TRUE(caller()->data_observer()->IsOpen());
5595 }
5596
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,DataChannelClosesWhenClosed)5597 TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
5598 ASSERT_TRUE(CreatePeerConnectionWrappers());
5599 ConnectFakeSignaling();
5600 caller()->CreateDataChannel();
5601 caller()->CreateAndSetAndSignalOffer();
5602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5603 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5604 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5605 caller()->data_channel()->Close();
5606 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5607 }
5608
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,DataChannelClosesWhenClosedReverse)5609 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5610 DataChannelClosesWhenClosedReverse) {
5611 ASSERT_TRUE(CreatePeerConnectionWrappers());
5612 ConnectFakeSignaling();
5613 caller()->CreateDataChannel();
5614 caller()->CreateAndSetAndSignalOffer();
5615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5616 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5617 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5618 callee()->data_channel()->Close();
5619 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
5620 }
5621
TEST_F(PeerConnectionIntegrationTestUnifiedPlan,DataChannelClosesWhenPeerConnectionClosed)5622 TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5623 DataChannelClosesWhenPeerConnectionClosed) {
5624 ASSERT_TRUE(CreatePeerConnectionWrappers());
5625 ConnectFakeSignaling();
5626 caller()->CreateDataChannel();
5627 caller()->CreateAndSetAndSignalOffer();
5628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5629 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5630 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5631 caller()->pc()->Close();
5632 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5633 }
5634
5635 #endif // HAVE_SCTP
5636
5637 } // namespace
5638 } // namespace webrtc
5639
5640 #endif // if !defined(THREAD_SANITIZER)
5641