Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: webrtc/pc/peerconnection_integrationtest.cc

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

Powered by Google App Engine
This is Rietveld 408576698