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

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

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

Powered by Google App Engine
This is Rietveld 408576698