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