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