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