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