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