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

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

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

Powered by Google App Engine
This is Rietveld 408576698