blob: 641b2fa7a557c8695afcd4993cd744a42fa2554c [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
deadbeef4e2deab2017-09-20 13:56:21 -07001991// Helper for test below.
1992void ModifySsrcs(cricket::SessionDescription* desc) {
1993 for (ContentInfo& content : desc->contents()) {
1994 MediaContentDescription* media_desc =
1995 static_cast<MediaContentDescription*>(content.description);
1996 for (cricket::StreamParams& stream : media_desc->mutable_streams()) {
1997 for (uint32_t& ssrc : stream.ssrcs) {
1998 ssrc = rtc::CreateRandomId();
1999 }
2000 }
2001 }
2002}
2003
2004// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2005// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2006// This should result in two "RTCInboundRTPStreamStats", but only one
2007// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2008// being reset to 0 once the SSRC change occurs.
2009//
2010// Regression test for this bug:
2011// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2012//
2013// The bug causes the track stats to only represent one of the two streams:
2014// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2015// that the track stat counters would reset to 0 when the new stream is
2016// received, and a 50% chance that they'll stop updating (while
2017// "concealed_samples" continues increasing, due to silence being generated for
2018// the inactive stream).
2019TEST_F(PeerConnectionIntegrationTest,
2020 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
2021 ASSERT_TRUE(CreatePeerConnectionWrappers());
2022 ConnectFakeSignaling();
2023 caller()->AddAudioOnlyMediaStream();
2024 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2025 // that doesn't signal SSRCs (from the callee's perspective).
2026 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2027 caller()->CreateAndSetAndSignalOffer();
2028 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2029 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
2030 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2031
2032 // Some audio frames were received, so we should have nonzero "samples
2033 // received" for the track.
2034 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2035 callee()->NewGetStats();
2036 ASSERT_NE(nullptr, report);
2037 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2038 ASSERT_EQ(1U, track_stats.size());
2039 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2040 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2041 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2042
2043 // Create a new offer and munge it to cause the caller to use a new SSRC.
2044 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2045 caller()->CreateAndSetAndSignalOffer();
2046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2047 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2048 // SSRC.
2049 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2050
2051 report = callee()->NewGetStats();
2052 ASSERT_NE(nullptr, report);
2053 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2054 ASSERT_EQ(1U, track_stats.size());
2055 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2056 // The "total samples received" stat should only be greater than it was
2057 // before.
2058 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2059 // Right now, the new SSRC will cause the counters to reset to 0.
2060 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2061
2062 // Additionally, the percentage of concealed samples (samples generated to
2063 // conceal packet loss) should be less than 25%%. If it's greater, that's a
2064 // good sign that we're seeing stats from the old stream that's no longer
2065 // receiving packets, and is generating concealed samples of silence.
2066 constexpr double kAcceptableConcealedSamplesPercentage = 0.25;
2067 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2068 EXPECT_LT(*track_stats[0]->concealed_samples,
2069 *track_stats[0]->total_samples_received *
2070 kAcceptableConcealedSamplesPercentage);
2071
2072 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2073 // sanity check that the SSRC really changed.
2074 // TODO(deadbeef): This isn't working right now, because we're not returning
2075 // *any* stats for the inactive stream. Uncomment when the bug is completely
2076 // fixed.
2077 // auto inbound_stream_stats =
2078 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2079 // ASSERT_EQ(2U, inbound_stream_stats.size());
2080}
2081
deadbeef1dcb1642017-03-29 21:08:16 -07002082// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
2083TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
2084 PeerConnectionFactory::Options dtls_10_options;
2085 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2086 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2087 dtls_10_options));
2088 ConnectFakeSignaling();
2089 // Do normal offer/answer and wait for some frames to be received in each
2090 // direction.
2091 caller()->AddAudioVideoMediaStream();
2092 callee()->AddAudioVideoMediaStream();
2093 caller()->CreateAndSetAndSignalOffer();
2094 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2095 ExpectNewFramesReceivedWithWait(
2096 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2097 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2098 kMaxWaitForFramesMs);
2099}
2100
2101// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
2102TEST_F(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
2103 PeerConnectionFactory::Options dtls_10_options;
2104 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2105 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2106 dtls_10_options));
2107 ConnectFakeSignaling();
2108 // Register UMA observer before signaling begins.
2109 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2110 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2111 caller()->pc()->RegisterUMAObserver(caller_observer);
2112 caller()->AddAudioVideoMediaStream();
2113 callee()->AddAudioVideoMediaStream();
2114 caller()->CreateAndSetAndSignalOffer();
2115 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2116 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002117 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002118 kDefaultTimeout);
2119 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002120 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002121 EXPECT_EQ(1,
2122 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2123 kDefaultSrtpCryptoSuite));
2124}
2125
2126// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
2127TEST_F(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
2128 PeerConnectionFactory::Options dtls_12_options;
2129 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2130 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2131 dtls_12_options));
2132 ConnectFakeSignaling();
2133 // Register UMA observer before signaling begins.
2134 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2135 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2136 caller()->pc()->RegisterUMAObserver(caller_observer);
2137 caller()->AddAudioVideoMediaStream();
2138 callee()->AddAudioVideoMediaStream();
2139 caller()->CreateAndSetAndSignalOffer();
2140 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2141 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002142 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002143 kDefaultTimeout);
2144 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002145 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002146 EXPECT_EQ(1,
2147 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2148 kDefaultSrtpCryptoSuite));
2149}
2150
2151// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2152// callee only supports 1.0.
2153TEST_F(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
2154 PeerConnectionFactory::Options caller_options;
2155 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2156 PeerConnectionFactory::Options callee_options;
2157 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2158 ASSERT_TRUE(
2159 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2160 ConnectFakeSignaling();
2161 // Do normal offer/answer and wait for some frames to be received in each
2162 // direction.
2163 caller()->AddAudioVideoMediaStream();
2164 callee()->AddAudioVideoMediaStream();
2165 caller()->CreateAndSetAndSignalOffer();
2166 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2167 ExpectNewFramesReceivedWithWait(
2168 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2169 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2170 kMaxWaitForFramesMs);
2171}
2172
2173// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2174// callee supports 1.2.
2175TEST_F(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
2176 PeerConnectionFactory::Options caller_options;
2177 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2178 PeerConnectionFactory::Options callee_options;
2179 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2180 ASSERT_TRUE(
2181 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2182 ConnectFakeSignaling();
2183 // Do normal offer/answer and wait for some frames to be received in each
2184 // direction.
2185 caller()->AddAudioVideoMediaStream();
2186 callee()->AddAudioVideoMediaStream();
2187 caller()->CreateAndSetAndSignalOffer();
2188 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2189 ExpectNewFramesReceivedWithWait(
2190 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2191 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2192 kMaxWaitForFramesMs);
2193}
2194
2195// Test that a non-GCM cipher is used if both sides only support non-GCM.
2196TEST_F(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
2197 bool local_gcm_enabled = false;
2198 bool remote_gcm_enabled = false;
2199 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2200 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2201 expected_cipher_suite);
2202}
2203
2204// Test that a GCM cipher is used if both ends support it.
2205TEST_F(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
2206 bool local_gcm_enabled = true;
2207 bool remote_gcm_enabled = true;
2208 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2209 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2210 expected_cipher_suite);
2211}
2212
2213// Test that GCM isn't used if only the offerer supports it.
2214TEST_F(PeerConnectionIntegrationTest,
2215 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2216 bool local_gcm_enabled = true;
2217 bool remote_gcm_enabled = false;
2218 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2219 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2220 expected_cipher_suite);
2221}
2222
2223// Test that GCM isn't used if only the answerer supports it.
2224TEST_F(PeerConnectionIntegrationTest,
2225 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2226 bool local_gcm_enabled = false;
2227 bool remote_gcm_enabled = true;
2228 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2229 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2230 expected_cipher_suite);
2231}
2232
deadbeef7914b8c2017-04-21 03:23:33 -07002233// Verify that media can be transmitted end-to-end when GCM crypto suites are
2234// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2235// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2236// works with it.
2237TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
2238 PeerConnectionFactory::Options gcm_options;
2239 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2240 ASSERT_TRUE(
2241 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2242 ConnectFakeSignaling();
2243 // Do normal offer/answer and wait for some frames to be received in each
2244 // direction.
2245 caller()->AddAudioVideoMediaStream();
2246 callee()->AddAudioVideoMediaStream();
2247 caller()->CreateAndSetAndSignalOffer();
2248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2249 ExpectNewFramesReceivedWithWait(
2250 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2251 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2252 kMaxWaitForFramesMs);
2253}
2254
deadbeef1dcb1642017-03-29 21:08:16 -07002255// This test sets up a call between two parties with audio, video and an RTP
2256// data channel.
2257TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
2258 FakeConstraints setup_constraints;
2259 setup_constraints.SetAllowRtpDataChannels();
2260 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2261 &setup_constraints));
2262 ConnectFakeSignaling();
2263 // Expect that data channel created on caller side will show up for callee as
2264 // well.
2265 caller()->CreateDataChannel();
2266 caller()->AddAudioVideoMediaStream();
2267 callee()->AddAudioVideoMediaStream();
2268 caller()->CreateAndSetAndSignalOffer();
2269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2270 // Ensure the existence of the RTP data channel didn't impede audio/video.
2271 ExpectNewFramesReceivedWithWait(
2272 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2273 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2274 kMaxWaitForFramesMs);
2275 ASSERT_NE(nullptr, caller()->data_channel());
2276 ASSERT_NE(nullptr, callee()->data_channel());
2277 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2278 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2279
2280 // Ensure data can be sent in both directions.
2281 std::string data = "hello world";
2282 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2283 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2284 kDefaultTimeout);
2285 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2286 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2287 kDefaultTimeout);
2288}
2289
2290// Ensure that an RTP data channel is signaled as closed for the caller when
2291// the callee rejects it in a subsequent offer.
2292TEST_F(PeerConnectionIntegrationTest,
2293 RtpDataChannelSignaledClosedInCalleeOffer) {
2294 // Same procedure as above test.
2295 FakeConstraints setup_constraints;
2296 setup_constraints.SetAllowRtpDataChannels();
2297 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2298 &setup_constraints));
2299 ConnectFakeSignaling();
2300 caller()->CreateDataChannel();
2301 caller()->AddAudioVideoMediaStream();
2302 callee()->AddAudioVideoMediaStream();
2303 caller()->CreateAndSetAndSignalOffer();
2304 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2305 ASSERT_NE(nullptr, caller()->data_channel());
2306 ASSERT_NE(nullptr, callee()->data_channel());
2307 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2308 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2309
2310 // Close the data channel on the callee, and do an updated offer/answer.
2311 callee()->data_channel()->Close();
2312 callee()->CreateAndSetAndSignalOffer();
2313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2314 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2315 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2316}
2317
2318// Tests that data is buffered in an RTP data channel until an observer is
2319// registered for it.
2320//
2321// NOTE: RTP data channels can receive data before the underlying
2322// transport has detected that a channel is writable and thus data can be
2323// received before the data channel state changes to open. That is hard to test
2324// but the same buffering is expected to be used in that case.
2325TEST_F(PeerConnectionIntegrationTest,
2326 DataBufferedUntilRtpDataChannelObserverRegistered) {
2327 // Use fake clock and simulated network delay so that we predictably can wait
2328 // until an SCTP message has been delivered without "sleep()"ing.
2329 rtc::ScopedFakeClock fake_clock;
2330 // Some things use a time of "0" as a special value, so we need to start out
2331 // the fake clock at a nonzero time.
2332 // TODO(deadbeef): Fix this.
2333 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2334 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2335 virtual_socket_server()->UpdateDelayDistribution();
2336
2337 FakeConstraints constraints;
2338 constraints.SetAllowRtpDataChannels();
2339 ASSERT_TRUE(
2340 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2341 ConnectFakeSignaling();
2342 caller()->CreateDataChannel();
2343 caller()->CreateAndSetAndSignalOffer();
2344 ASSERT_TRUE(caller()->data_channel() != nullptr);
2345 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2346 kDefaultTimeout, fake_clock);
2347 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2348 kDefaultTimeout, fake_clock);
2349 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2350 callee()->data_channel()->state(), kDefaultTimeout,
2351 fake_clock);
2352
2353 // Unregister the observer which is normally automatically registered.
2354 callee()->data_channel()->UnregisterObserver();
2355 // Send data and advance fake clock until it should have been received.
2356 std::string data = "hello world";
2357 caller()->data_channel()->Send(DataBuffer(data));
2358 SIMULATED_WAIT(false, 50, fake_clock);
2359
2360 // Attach data channel and expect data to be received immediately. Note that
2361 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2362 // further, but data can be received even if the callback is asynchronous.
2363 MockDataChannelObserver new_observer(callee()->data_channel());
2364 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2365 fake_clock);
2366}
2367
2368// This test sets up a call between two parties with audio, video and but only
2369// the caller client supports RTP data channels.
2370TEST_F(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
2371 FakeConstraints setup_constraints_1;
2372 setup_constraints_1.SetAllowRtpDataChannels();
2373 // Must disable DTLS to make negotiation succeed.
2374 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2375 false);
2376 FakeConstraints setup_constraints_2;
2377 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2378 false);
2379 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2380 &setup_constraints_1, &setup_constraints_2));
2381 ConnectFakeSignaling();
2382 caller()->CreateDataChannel();
2383 caller()->AddAudioVideoMediaStream();
2384 callee()->AddAudioVideoMediaStream();
2385 caller()->CreateAndSetAndSignalOffer();
2386 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2387 // The caller should still have a data channel, but it should be closed, and
2388 // one should ever have been created for the callee.
2389 EXPECT_TRUE(caller()->data_channel() != nullptr);
2390 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2391 EXPECT_EQ(nullptr, callee()->data_channel());
2392}
2393
2394// This test sets up a call between two parties with audio, and video. When
2395// audio and video is setup and flowing, an RTP data channel is negotiated.
2396TEST_F(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
2397 FakeConstraints setup_constraints;
2398 setup_constraints.SetAllowRtpDataChannels();
2399 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2400 &setup_constraints));
2401 ConnectFakeSignaling();
2402 // Do initial offer/answer with audio/video.
2403 caller()->AddAudioVideoMediaStream();
2404 callee()->AddAudioVideoMediaStream();
2405 caller()->CreateAndSetAndSignalOffer();
2406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2407 // Create data channel and do new offer and answer.
2408 caller()->CreateDataChannel();
2409 caller()->CreateAndSetAndSignalOffer();
2410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2411 ASSERT_NE(nullptr, caller()->data_channel());
2412 ASSERT_NE(nullptr, callee()->data_channel());
2413 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2414 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2415 // Ensure data can be sent in both directions.
2416 std::string data = "hello world";
2417 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2418 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2419 kDefaultTimeout);
2420 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2421 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2422 kDefaultTimeout);
2423}
2424
2425#ifdef HAVE_SCTP
2426
2427// This test sets up a call between two parties with audio, video and an SCTP
2428// data channel.
2429TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
2430 ASSERT_TRUE(CreatePeerConnectionWrappers());
2431 ConnectFakeSignaling();
2432 // Expect that data channel created on caller side will show up for callee as
2433 // well.
2434 caller()->CreateDataChannel();
2435 caller()->AddAudioVideoMediaStream();
2436 callee()->AddAudioVideoMediaStream();
2437 caller()->CreateAndSetAndSignalOffer();
2438 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2439 // Ensure the existence of the SCTP data channel didn't impede audio/video.
2440 ExpectNewFramesReceivedWithWait(
2441 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2442 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2443 kMaxWaitForFramesMs);
2444 // Caller data channel should already exist (it created one). Callee data
2445 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2446 ASSERT_NE(nullptr, caller()->data_channel());
2447 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2448 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2449 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2450
2451 // Ensure data can be sent in both directions.
2452 std::string data = "hello world";
2453 caller()->data_channel()->Send(DataBuffer(data));
2454 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2455 kDefaultTimeout);
2456 callee()->data_channel()->Send(DataBuffer(data));
2457 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2458 kDefaultTimeout);
2459}
2460
2461// Ensure that when the callee closes an SCTP data channel, the closing
2462// procedure results in the data channel being closed for the caller as well.
2463TEST_F(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
2464 // Same procedure as above test.
2465 ASSERT_TRUE(CreatePeerConnectionWrappers());
2466 ConnectFakeSignaling();
2467 caller()->CreateDataChannel();
2468 caller()->AddAudioVideoMediaStream();
2469 callee()->AddAudioVideoMediaStream();
2470 caller()->CreateAndSetAndSignalOffer();
2471 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2472 ASSERT_NE(nullptr, caller()->data_channel());
2473 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2474 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2475 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2476
2477 // Close the data channel on the callee side, and wait for it to reach the
2478 // "closed" state on both sides.
2479 callee()->data_channel()->Close();
2480 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2481 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2482}
2483
2484// Test usrsctp's ability to process unordered data stream, where data actually
2485// arrives out of order using simulated delays. Previously there have been some
2486// bugs in this area.
2487TEST_F(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
2488 // Introduce random network delays.
2489 // Otherwise it's not a true "unordered" test.
2490 virtual_socket_server()->set_delay_mean(20);
2491 virtual_socket_server()->set_delay_stddev(5);
2492 virtual_socket_server()->UpdateDelayDistribution();
2493 // Normal procedure, but with unordered data channel config.
2494 ASSERT_TRUE(CreatePeerConnectionWrappers());
2495 ConnectFakeSignaling();
2496 webrtc::DataChannelInit init;
2497 init.ordered = false;
2498 caller()->CreateDataChannel(&init);
2499 caller()->CreateAndSetAndSignalOffer();
2500 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2501 ASSERT_NE(nullptr, caller()->data_channel());
2502 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2503 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2504 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2505
2506 static constexpr int kNumMessages = 100;
2507 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2508 static constexpr size_t kMaxMessageSize = 4096;
2509 // Create and send random messages.
2510 std::vector<std::string> sent_messages;
2511 for (int i = 0; i < kNumMessages; ++i) {
2512 size_t length =
2513 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2514 std::string message;
2515 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2516 caller()->data_channel()->Send(DataBuffer(message));
2517 callee()->data_channel()->Send(DataBuffer(message));
2518 sent_messages.push_back(message);
2519 }
2520
2521 // Wait for all messages to be received.
2522 EXPECT_EQ_WAIT(kNumMessages,
2523 caller()->data_observer()->received_message_count(),
2524 kDefaultTimeout);
2525 EXPECT_EQ_WAIT(kNumMessages,
2526 callee()->data_observer()->received_message_count(),
2527 kDefaultTimeout);
2528
2529 // Sort and compare to make sure none of the messages were corrupted.
2530 std::vector<std::string> caller_received_messages =
2531 caller()->data_observer()->messages();
2532 std::vector<std::string> callee_received_messages =
2533 callee()->data_observer()->messages();
2534 std::sort(sent_messages.begin(), sent_messages.end());
2535 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2536 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2537 EXPECT_EQ(sent_messages, caller_received_messages);
2538 EXPECT_EQ(sent_messages, callee_received_messages);
2539}
2540
2541// This test sets up a call between two parties with audio, and video. When
2542// audio and video are setup and flowing, an SCTP data channel is negotiated.
2543TEST_F(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
2544 ASSERT_TRUE(CreatePeerConnectionWrappers());
2545 ConnectFakeSignaling();
2546 // Do initial offer/answer with audio/video.
2547 caller()->AddAudioVideoMediaStream();
2548 callee()->AddAudioVideoMediaStream();
2549 caller()->CreateAndSetAndSignalOffer();
2550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2551 // Create data channel and do new offer and answer.
2552 caller()->CreateDataChannel();
2553 caller()->CreateAndSetAndSignalOffer();
2554 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2555 // Caller data channel should already exist (it created one). Callee data
2556 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2557 ASSERT_NE(nullptr, caller()->data_channel());
2558 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2559 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2560 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2561 // Ensure data can be sent in both directions.
2562 std::string data = "hello world";
2563 caller()->data_channel()->Send(DataBuffer(data));
2564 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2565 kDefaultTimeout);
2566 callee()->data_channel()->Send(DataBuffer(data));
2567 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2568 kDefaultTimeout);
2569}
2570
deadbeef7914b8c2017-04-21 03:23:33 -07002571// Set up a connection initially just using SCTP data channels, later upgrading
2572// to audio/video, ensuring frames are received end-to-end. Effectively the
2573// inverse of the test above.
2574// This was broken in M57; see https://crbug.com/711243
2575TEST_F(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
2576 ASSERT_TRUE(CreatePeerConnectionWrappers());
2577 ConnectFakeSignaling();
2578 // Do initial offer/answer with just data channel.
2579 caller()->CreateDataChannel();
2580 caller()->CreateAndSetAndSignalOffer();
2581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2582 // Wait until data can be sent over the data channel.
2583 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2584 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2585 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2586
2587 // Do subsequent offer/answer with two-way audio and video. Audio and video
2588 // should end up bundled on the DTLS/ICE transport already used for data.
2589 caller()->AddAudioVideoMediaStream();
2590 callee()->AddAudioVideoMediaStream();
2591 caller()->CreateAndSetAndSignalOffer();
2592 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2593 ExpectNewFramesReceivedWithWait(
2594 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2595 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2596 kMaxWaitForFramesMs);
2597}
2598
deadbeef8b7e9ad2017-05-25 09:38:55 -07002599static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
2600 const ContentInfo* dc_offer = GetFirstDataContent(desc);
2601 ASSERT_NE(nullptr, dc_offer);
2602 cricket::DataContentDescription* dcd_offer =
2603 static_cast<cricket::DataContentDescription*>(dc_offer->description);
2604 dcd_offer->set_use_sctpmap(false);
2605 dcd_offer->set_protocol("UDP/DTLS/SCTP");
2606}
2607
2608// Test that the data channel works when a spec-compliant SCTP m= section is
2609// offered (using "a=sctp-port" instead of "a=sctpmap", and using
2610// "UDP/DTLS/SCTP" as the protocol).
2611TEST_F(PeerConnectionIntegrationTest,
2612 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
2613 ASSERT_TRUE(CreatePeerConnectionWrappers());
2614 ConnectFakeSignaling();
2615 caller()->CreateDataChannel();
2616 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
2617 caller()->CreateAndSetAndSignalOffer();
2618 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2619 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2620 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2621 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2622
2623 // Ensure data can be sent in both directions.
2624 std::string data = "hello world";
2625 caller()->data_channel()->Send(DataBuffer(data));
2626 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2627 kDefaultTimeout);
2628 callee()->data_channel()->Send(DataBuffer(data));
2629 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2630 kDefaultTimeout);
2631}
2632
deadbeef1dcb1642017-03-29 21:08:16 -07002633#endif // HAVE_SCTP
2634
2635// Test that the ICE connection and gathering states eventually reach
2636// "complete".
2637TEST_F(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
2638 ASSERT_TRUE(CreatePeerConnectionWrappers());
2639 ConnectFakeSignaling();
2640 // Do normal offer/answer.
2641 caller()->AddAudioVideoMediaStream();
2642 callee()->AddAudioVideoMediaStream();
2643 caller()->CreateAndSetAndSignalOffer();
2644 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2645 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2646 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
2647 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2648 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
2649 // After the best candidate pair is selected and all candidates are signaled,
2650 // the ICE connection state should reach "complete".
2651 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
2652 // answerer/"callee" by default) only reaches "connected". When this is
2653 // fixed, this test should be updated.
2654 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2655 caller()->ice_connection_state(), kDefaultTimeout);
2656 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2657 callee()->ice_connection_state(), kDefaultTimeout);
2658}
2659
2660// This test sets up a call between two parties with audio and video.
2661// During the call, the caller restarts ICE and the test verifies that
2662// new ICE candidates are generated and audio and video still can flow, and the
2663// ICE state reaches completed again.
2664TEST_F(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
2665 ASSERT_TRUE(CreatePeerConnectionWrappers());
2666 ConnectFakeSignaling();
2667 // Do normal offer/answer and wait for ICE to complete.
2668 caller()->AddAudioVideoMediaStream();
2669 callee()->AddAudioVideoMediaStream();
2670 caller()->CreateAndSetAndSignalOffer();
2671 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2672 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2673 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2674 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2675 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2676
2677 // To verify that the ICE restart actually occurs, get
2678 // ufrag/password/candidates before and after restart.
2679 // Create an SDP string of the first audio candidate for both clients.
2680 const webrtc::IceCandidateCollection* audio_candidates_caller =
2681 caller()->pc()->local_description()->candidates(0);
2682 const webrtc::IceCandidateCollection* audio_candidates_callee =
2683 callee()->pc()->local_description()->candidates(0);
2684 ASSERT_GT(audio_candidates_caller->count(), 0u);
2685 ASSERT_GT(audio_candidates_callee->count(), 0u);
2686 std::string caller_candidate_pre_restart;
2687 ASSERT_TRUE(
2688 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2689 std::string callee_candidate_pre_restart;
2690 ASSERT_TRUE(
2691 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2692 const cricket::SessionDescription* desc =
2693 caller()->pc()->local_description()->description();
2694 std::string caller_ufrag_pre_restart =
2695 desc->transport_infos()[0].description.ice_ufrag;
2696 desc = callee()->pc()->local_description()->description();
2697 std::string callee_ufrag_pre_restart =
2698 desc->transport_infos()[0].description.ice_ufrag;
2699
2700 // Have the caller initiate an ICE restart.
2701 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2702 caller()->CreateAndSetAndSignalOffer();
2703 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2704 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2705 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2706 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2707 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2708
2709 // Grab the ufrags/candidates again.
2710 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2711 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2712 ASSERT_GT(audio_candidates_caller->count(), 0u);
2713 ASSERT_GT(audio_candidates_callee->count(), 0u);
2714 std::string caller_candidate_post_restart;
2715 ASSERT_TRUE(
2716 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2717 std::string callee_candidate_post_restart;
2718 ASSERT_TRUE(
2719 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2720 desc = caller()->pc()->local_description()->description();
2721 std::string caller_ufrag_post_restart =
2722 desc->transport_infos()[0].description.ice_ufrag;
2723 desc = callee()->pc()->local_description()->description();
2724 std::string callee_ufrag_post_restart =
2725 desc->transport_infos()[0].description.ice_ufrag;
2726 // Sanity check that an ICE restart was actually negotiated in SDP.
2727 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
2728 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
2729 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
2730 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
2731
2732 // Ensure that additional frames are received after the ICE restart.
2733 ExpectNewFramesReceivedWithWait(
2734 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2735 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2736 kMaxWaitForFramesMs);
2737}
2738
2739// Verify that audio/video can be received end-to-end when ICE renomination is
2740// enabled.
2741TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
2742 PeerConnectionInterface::RTCConfiguration config;
2743 config.enable_ice_renomination = true;
2744 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2745 ConnectFakeSignaling();
2746 // Do normal offer/answer and wait for some frames to be received in each
2747 // direction.
2748 caller()->AddAudioVideoMediaStream();
2749 callee()->AddAudioVideoMediaStream();
2750 caller()->CreateAndSetAndSignalOffer();
2751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2752 // Sanity check that ICE renomination was actually negotiated.
2753 const cricket::SessionDescription* desc =
2754 caller()->pc()->local_description()->description();
2755 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07002756 ASSERT_NE(
2757 info.description.transport_options.end(),
2758 std::find(info.description.transport_options.begin(),
2759 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002760 }
2761 desc = callee()->pc()->local_description()->description();
2762 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07002763 ASSERT_NE(
2764 info.description.transport_options.end(),
2765 std::find(info.description.transport_options.begin(),
2766 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07002767 }
2768 ExpectNewFramesReceivedWithWait(
2769 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2770 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2771 kMaxWaitForFramesMs);
2772}
2773
2774// This test sets up a call between two parties with audio and video. It then
2775// renegotiates setting the video m-line to "port 0", then later renegotiates
2776// again, enabling video.
2777TEST_F(PeerConnectionIntegrationTest,
2778 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
2779 ASSERT_TRUE(CreatePeerConnectionWrappers());
2780 ConnectFakeSignaling();
2781
2782 // Do initial negotiation, only sending media from the caller. Will result in
2783 // video and audio recvonly "m=" sections.
2784 caller()->AddAudioVideoMediaStream();
2785 caller()->CreateAndSetAndSignalOffer();
2786 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2787
2788 // Negotiate again, disabling the video "m=" section (the callee will set the
2789 // port to 0 due to offer_to_receive_video = 0).
2790 PeerConnectionInterface::RTCOfferAnswerOptions options;
2791 options.offer_to_receive_video = 0;
2792 callee()->SetOfferAnswerOptions(options);
2793 caller()->CreateAndSetAndSignalOffer();
2794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2795 // Sanity check that video "m=" section was actually rejected.
2796 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
2797 callee()->pc()->local_description()->description());
2798 ASSERT_NE(nullptr, answer_video_content);
2799 ASSERT_TRUE(answer_video_content->rejected);
2800
2801 // Enable video and do negotiation again, making sure video is received
2802 // end-to-end, also adding media stream to callee.
2803 options.offer_to_receive_video = 1;
2804 callee()->SetOfferAnswerOptions(options);
2805 callee()->AddAudioVideoMediaStream();
2806 caller()->CreateAndSetAndSignalOffer();
2807 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2808 // Verify the caller receives frames from the newly added stream, and the
2809 // callee receives additional frames from the re-enabled video m= section.
2810 ExpectNewFramesReceivedWithWait(
2811 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2812 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2813 kMaxWaitForFramesMs);
2814}
2815
2816// This test sets up a Jsep call between two parties with external
2817// VideoDecoderFactory.
2818// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2819// See issue webrtc/2378.
2820TEST_F(PeerConnectionIntegrationTest,
2821 DISABLED_EndToEndCallWithVideoDecoderFactory) {
2822 ASSERT_TRUE(CreatePeerConnectionWrappers());
2823 EnableVideoDecoderFactory();
2824 ConnectFakeSignaling();
2825 caller()->AddAudioVideoMediaStream();
2826 callee()->AddAudioVideoMediaStream();
2827 caller()->CreateAndSetAndSignalOffer();
2828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2829 ExpectNewFramesReceivedWithWait(
2830 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2831 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2832 kMaxWaitForFramesMs);
2833}
2834
2835// This tests that if we negotiate after calling CreateSender but before we
2836// have a track, then set a track later, frames from the newly-set track are
2837// received end-to-end.
2838// TODO(deadbeef): Change this test to use AddTransceiver, once that's
2839// implemented.
2840TEST_F(PeerConnectionIntegrationTest,
2841 MediaFlowsAfterEarlyWarmupWithCreateSender) {
2842 ASSERT_TRUE(CreatePeerConnectionWrappers());
2843 ConnectFakeSignaling();
2844 auto caller_audio_sender =
2845 caller()->pc()->CreateSender("audio", "caller_stream");
2846 auto caller_video_sender =
2847 caller()->pc()->CreateSender("video", "caller_stream");
2848 auto callee_audio_sender =
2849 callee()->pc()->CreateSender("audio", "callee_stream");
2850 auto callee_video_sender =
2851 callee()->pc()->CreateSender("video", "callee_stream");
2852 caller()->CreateAndSetAndSignalOffer();
2853 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2854 // Wait for ICE to complete, without any tracks being set.
2855 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2856 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2857 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2858 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2859 // Now set the tracks, and expect frames to immediately start flowing.
2860 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
2861 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
2862 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
2863 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
2864 ExpectNewFramesReceivedWithWait(
2865 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2866 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2867 kMaxWaitForFramesMs);
2868}
2869
2870// This test verifies that a remote video track can be added via AddStream,
2871// and sent end-to-end. For this particular test, it's simply echoed back
2872// from the caller to the callee, rather than being forwarded to a third
2873// PeerConnection.
2874TEST_F(PeerConnectionIntegrationTest, CanSendRemoteVideoTrack) {
2875 ASSERT_TRUE(CreatePeerConnectionWrappers());
2876 ConnectFakeSignaling();
2877 // Just send a video track from the caller.
2878 caller()->AddMediaStreamFromTracks(nullptr,
2879 caller()->CreateLocalVideoTrack());
2880 caller()->CreateAndSetAndSignalOffer();
2881 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2882 ASSERT_EQ(1, callee()->remote_streams()->count());
2883
2884 // Echo the stream back, and do a new offer/anwer (initiated by callee this
2885 // time).
2886 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
2887 callee()->CreateAndSetAndSignalOffer();
2888 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2889
2890 int expected_caller_received_video_frames = kDefaultExpectedVideoFrameCount;
2891 ExpectNewFramesReceivedWithWait(0, expected_caller_received_video_frames, 0,
2892 0, kMaxWaitForFramesMs);
2893}
2894
2895// Test that we achieve the expected end-to-end connection time, using a
2896// fake clock and simulated latency on the media and signaling paths.
2897// We use a TURN<->TURN connection because this is usually the quickest to
2898// set up initially, especially when we're confident the connection will work
2899// and can start sending media before we get a STUN response.
2900//
2901// With various optimizations enabled, here are the network delays we expect to
2902// be on the critical path:
2903// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2904// signaling answer (with DTLS fingerprint).
2905// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2906// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2907// the first of which should have arrived before the answer.
2908TEST_F(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
2909 rtc::ScopedFakeClock fake_clock;
2910 // Some things use a time of "0" as a special value, so we need to start out
2911 // the fake clock at a nonzero time.
2912 // TODO(deadbeef): Fix this.
2913 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2914
2915 static constexpr int media_hop_delay_ms = 50;
2916 static constexpr int signaling_trip_delay_ms = 500;
2917 // For explanation of these values, see comment above.
2918 static constexpr int required_media_hops = 9;
2919 static constexpr int required_signaling_trips = 2;
2920 // For internal delays (such as posting an event asychronously).
2921 static constexpr int allowed_internal_delay_ms = 20;
2922 static constexpr int total_connection_time_ms =
2923 media_hop_delay_ms * required_media_hops +
2924 signaling_trip_delay_ms * required_signaling_trips +
2925 allowed_internal_delay_ms;
2926
2927 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2928 3478};
2929 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2930 0};
2931 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2932 3478};
2933 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2934 0};
2935 cricket::TestTurnServer turn_server_1(network_thread(),
2936 turn_server_1_internal_address,
2937 turn_server_1_external_address);
2938 cricket::TestTurnServer turn_server_2(network_thread(),
2939 turn_server_2_internal_address,
2940 turn_server_2_external_address);
2941 // Bypass permission check on received packets so media can be sent before
2942 // the candidate is signaled.
2943 turn_server_1.set_enable_permission_checks(false);
2944 turn_server_2.set_enable_permission_checks(false);
2945
2946 PeerConnectionInterface::RTCConfiguration client_1_config;
2947 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2948 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2949 ice_server_1.username = "test";
2950 ice_server_1.password = "test";
2951 client_1_config.servers.push_back(ice_server_1);
2952 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2953 client_1_config.presume_writable_when_fully_relayed = true;
2954
2955 PeerConnectionInterface::RTCConfiguration client_2_config;
2956 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2957 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2958 ice_server_2.username = "test";
2959 ice_server_2.password = "test";
2960 client_2_config.servers.push_back(ice_server_2);
2961 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2962 client_2_config.presume_writable_when_fully_relayed = true;
2963
2964 ASSERT_TRUE(
2965 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
2966 // Set up the simulated delays.
2967 SetSignalingDelayMs(signaling_trip_delay_ms);
2968 ConnectFakeSignaling();
2969 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2970 virtual_socket_server()->UpdateDelayDistribution();
2971
2972 // Set "offer to receive audio/video" without adding any tracks, so we just
2973 // set up ICE/DTLS with no media.
2974 PeerConnectionInterface::RTCOfferAnswerOptions options;
2975 options.offer_to_receive_audio = 1;
2976 options.offer_to_receive_video = 1;
2977 caller()->SetOfferAnswerOptions(options);
2978 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07002979 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
2980 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07002981 // Need to free the clients here since they're using things we created on
2982 // the stack.
2983 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
2984 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
2985}
2986
deadbeefc964d0b2017-04-03 10:03:35 -07002987// Test that audio and video flow end-to-end when codec names don't use the
2988// expected casing, given that they're supposed to be case insensitive. To test
2989// this, all but one codec is removed from each media description, and its
2990// casing is changed.
2991//
2992// In the past, this has regressed and caused crashes/black video, due to the
2993// fact that code at some layers was doing case-insensitive comparisons and
2994// code at other layers was not.
2995TEST_F(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
2996 ASSERT_TRUE(CreatePeerConnectionWrappers());
2997 ConnectFakeSignaling();
2998 caller()->AddAudioVideoMediaStream();
2999 callee()->AddAudioVideoMediaStream();
3000
3001 // Remove all but one audio/video codec (opus and VP8), and change the
3002 // casing of the caller's generated offer.
3003 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3004 cricket::AudioContentDescription* audio =
3005 GetFirstAudioContentDescription(description);
3006 ASSERT_NE(nullptr, audio);
3007 auto audio_codecs = audio->codecs();
3008 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3009 [](const cricket::AudioCodec& codec) {
3010 return codec.name != "opus";
3011 }),
3012 audio_codecs.end());
3013 ASSERT_EQ(1u, audio_codecs.size());
3014 audio_codecs[0].name = "OpUs";
3015 audio->set_codecs(audio_codecs);
3016
3017 cricket::VideoContentDescription* video =
3018 GetFirstVideoContentDescription(description);
3019 ASSERT_NE(nullptr, video);
3020 auto video_codecs = video->codecs();
3021 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3022 [](const cricket::VideoCodec& codec) {
3023 return codec.name != "VP8";
3024 }),
3025 video_codecs.end());
3026 ASSERT_EQ(1u, video_codecs.size());
3027 video_codecs[0].name = "vP8";
3028 video->set_codecs(video_codecs);
3029 });
3030
3031 caller()->CreateAndSetAndSignalOffer();
3032 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3033
3034 // Verify frames are still received end-to-end.
3035 ExpectNewFramesReceivedWithWait(
3036 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3037 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3038 kMaxWaitForFramesMs);
3039}
3040
hbos8d609f62017-04-10 07:39:05 -07003041TEST_F(PeerConnectionIntegrationTest, GetSources) {
3042 ASSERT_TRUE(CreatePeerConnectionWrappers());
3043 ConnectFakeSignaling();
3044 caller()->AddAudioOnlyMediaStream();
3045 caller()->CreateAndSetAndSignalOffer();
3046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003047 // Wait for one audio frame to be received by the callee.
hbos8d609f62017-04-10 07:39:05 -07003048 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3049 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3050 auto receiver = callee()->pc()->GetReceivers()[0];
3051 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3052
3053 auto contributing_sources = receiver->GetSources();
3054 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3055 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3056 contributing_sources[0].source_id());
3057}
3058
deadbeef2f425aa2017-04-14 10:41:32 -07003059// Test that if a track is removed and added again with a different stream ID,
3060// the new stream ID is successfully communicated in SDP and media continues to
3061// flow end-to-end.
3062TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) {
3063 ASSERT_TRUE(CreatePeerConnectionWrappers());
3064 ConnectFakeSignaling();
3065
3066 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3067 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3068 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3069 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3070
3071 // Add track using stream 1, do offer/answer.
3072 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3073 caller()->CreateLocalAudioTrack();
3074 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3075 caller()->pc()->AddTrack(track, {stream_1.get()});
3076 caller()->CreateAndSetAndSignalOffer();
3077 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3078 // Wait for one audio frame to be received by the callee.
3079 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3080
3081 // Remove the sender, and create a new one with the new stream.
3082 caller()->pc()->RemoveTrack(sender);
3083 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086 // Wait for additional audio frames to be received by the callee.
3087 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0,
3088 kMaxWaitForFramesMs);
3089}
3090
deadbeef1dcb1642017-03-29 21:08:16 -07003091} // namespace
3092
3093#endif // if !defined(THREAD_SANITIZER)