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