blob: a249d79dd83e5ea6784350c127e3c6dbf846172a [file] [log] [blame]
deadbeef1dcb1642017-03-30 04:08:161/*
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
Karl Wiberg1b0eae32017-10-17 12:48:5425#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 17:03:1030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 16:04:2031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 20:04:2736#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 12:01:4037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3138#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 19:54:4245#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 22:26:1146#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3147#include "pc/test/fakeaudiocapturemodule.h"
48#include "pc/test/fakeperiodicvideocapturer.h"
49#include "pc/test/fakertccertificategenerator.h"
50#include "pc/test/fakevideotrackrenderer.h"
51#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3152#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 20:04:2753#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3154#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3155#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 14:29:4056#include "test/gmock.h"
deadbeef1dcb1642017-03-30 04:08:1657
58using cricket::ContentInfo;
59using cricket::FakeWebRtcVideoDecoder;
60using cricket::FakeWebRtcVideoDecoderFactory;
61using cricket::FakeWebRtcVideoEncoder;
62using cricket::FakeWebRtcVideoEncoderFactory;
63using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 20:04:2764using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 19:54:4265using ::testing::Combine;
Steve Antonede9ca52017-10-16 20:04:2766using ::testing::ElementsAre;
67using ::testing::Values;
deadbeef1dcb1642017-03-30 04:08:1668using webrtc::DataBuffer;
69using webrtc::DataChannelInterface;
70using webrtc::DtmfSender;
71using webrtc::DtmfSenderInterface;
72using webrtc::DtmfSenderObserverInterface;
73using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 18:26:4974using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-30 04:08:1675using webrtc::MediaConstraintsInterface;
76using webrtc::MediaStreamInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 17:03:1083using webrtc::PeerConnection;
deadbeef1dcb1642017-03-30 04:08:1684using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-25 02:32:5785using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-30 04:08:1686using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 17:03:1087using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 18:26:4988using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 22:18:4189using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-25 02:32:5790using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 16:04:2091using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 19:54:4292using webrtc::RtpSenderInterface;
93using webrtc::RtpTransceiverDirection;
94using webrtc::RtpTransceiverInit;
95using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-18 01:41:0296using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 18:27:4197using webrtc::SdpType;
deadbeef1dcb1642017-03-30 04:08:1698using webrtc::SessionDescriptionInterface;
99using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 18:26:49100using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-30 04:08:16101
102namespace {
103
104static const int kDefaultTimeout = 10000;
105static const int kMaxWaitForStatsMs = 3000;
106static const int kMaxWaitForActivationMs = 5000;
107static const int kMaxWaitForFramesMs = 10000;
108// Default number of audio/video frames to wait for before considering a test
109// successful.
110static const int kDefaultExpectedAudioFrameCount = 3;
111static const int kDefaultExpectedVideoFrameCount = 3;
112
deadbeef1dcb1642017-03-30 04:08:16113static const char kDataChannelLabel[] = "data_channel";
114
115// SRTP cipher name negotiated by the tests. This must be updated if the
116// default changes.
Tommi8e545ee2018-02-08 16:25:20117static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
deadbeef1dcb1642017-03-30 04:08:16118static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
119
Steve Antonede9ca52017-10-16 20:04:27120static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
121
deadbeef1dcb1642017-03-30 04:08:16122// Helper function for constructing offer/answer options to initiate an ICE
123// restart.
124PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
125 PeerConnectionInterface::RTCOfferAnswerOptions options;
126 options.ice_restart = true;
127 return options;
128}
129
deadbeefd8ad7882017-04-18 23:01:17130// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
131// attribute from received SDP, simulating a legacy endpoint.
132void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
133 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 23:14:30134 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 23:01:17135 }
136 desc->set_msid_supported(false);
137}
138
zhihuangf8164932017-05-19 20:09:47139int FindFirstMediaStatsIndexByKind(
140 const std::string& kind,
141 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
142 media_stats_vec) {
143 for (size_t i = 0; i < media_stats_vec.size(); i++) {
144 if (media_stats_vec[i]->kind.ValueToString() == kind) {
145 return i;
146 }
147 }
148 return -1;
149}
150
deadbeef1dcb1642017-03-30 04:08:16151class SignalingMessageReceiver {
152 public:
Steve Antona3a92c22017-12-07 18:27:41153 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-30 04:08:16154 virtual void ReceiveIceMessage(const std::string& sdp_mid,
155 int sdp_mline_index,
156 const std::string& msg) = 0;
157
158 protected:
159 SignalingMessageReceiver() {}
160 virtual ~SignalingMessageReceiver() {}
161};
162
163class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
164 public:
165 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
166 : expected_media_type_(media_type) {}
167
168 void OnFirstPacketReceived(cricket::MediaType media_type) override {
169 ASSERT_EQ(expected_media_type_, media_type);
170 first_packet_received_ = true;
171 }
172
173 bool first_packet_received() const { return first_packet_received_; }
174
175 virtual ~MockRtpReceiverObserver() {}
176
177 private:
178 bool first_packet_received_ = false;
179 cricket::MediaType expected_media_type_;
180};
181
182// Helper class that wraps a peer connection, observes it, and can accept
183// signaling messages from another wrapper.
184//
185// Uses a fake network, fake A/V capture, and optionally fake
186// encoders/decoders, though they aren't used by default since they don't
187// advertise support of any codecs.
Steve Anton94286cb2017-09-26 23:20:19188// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 19:54:42189// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-30 04:08:16190class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 18:26:49191 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-30 04:08:16192 public:
193 // Different factory methods for convenience.
194 // TODO(deadbeef): Could use the pattern of:
195 //
196 // PeerConnectionWrapper =
197 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
198 //
199 // To reduce some code duplication.
200 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
201 const std::string& debug_name,
202 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
203 rtc::Thread* network_thread,
204 rtc::Thread* worker_thread) {
205 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
206 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
207 network_thread, worker_thread)) {
208 delete client;
209 return nullptr;
210 }
211 return client;
212 }
213
deadbeef2f425aa2017-04-14 17:41:32214 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
215 return peer_connection_factory_.get();
216 }
217
deadbeef1dcb1642017-03-30 04:08:16218 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
219
220 // If a signaling message receiver is set (via ConnectFakeSignaling), this
221 // will set the whole offer/answer exchange in motion. Just need to wait for
222 // the signaling state to reach "stable".
223 void CreateAndSetAndSignalOffer() {
224 auto offer = CreateOffer();
225 ASSERT_NE(nullptr, offer);
226 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
227 }
228
229 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
230 // when a remote offer is received (via fake signaling) and an answer is
231 // generated. By default, uses default options.
232 void SetOfferAnswerOptions(
233 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
234 offer_answer_options_ = options;
235 }
236
237 // Set a callback to be invoked when SDP is received via the fake signaling
238 // channel, which provides an opportunity to munge (modify) the SDP. This is
239 // used to test SDP being applied that a PeerConnection would normally not
240 // generate, but a non-JSEP endpoint might.
241 void SetReceivedSdpMunger(
242 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 16:04:20243 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-30 04:08:16244 }
245
deadbeefc964d0b2017-04-03 17:03:35246 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-30 04:08:16247 // generated.
248 void SetGeneratedSdpMunger(
249 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 16:04:20250 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-30 04:08:16251 }
252
Seth Hampson2f0d7022018-02-20 19:54:42253 // Set a callback to be invoked when a remote offer is received via the fake
254 // signaling channel. This provides an opportunity to change the
255 // PeerConnection state before an answer is created and sent to the caller.
256 void SetRemoteOfferHandler(std::function<void()> handler) {
257 remote_offer_handler_ = std::move(handler);
258 }
259
Steve Antonede9ca52017-10-16 20:04:27260 // Every ICE connection state in order that has been seen by the observer.
261 std::vector<PeerConnectionInterface::IceConnectionState>
262 ice_connection_state_history() const {
263 return ice_connection_state_history_;
264 }
Steve Anton6f25b092017-10-23 16:39:20265 void clear_ice_connection_state_history() {
266 ice_connection_state_history_.clear();
267 }
Steve Antonede9ca52017-10-16 20:04:27268
269 // Every ICE gathering state in order that has been seen by the observer.
270 std::vector<PeerConnectionInterface::IceGatheringState>
271 ice_gathering_state_history() const {
272 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-30 04:08:16273 }
274
Steve Anton15324772018-01-16 18:26:49275 void AddAudioVideoTracks() {
276 AddAudioTrack();
277 AddVideoTrack();
deadbeef1dcb1642017-03-30 04:08:16278 }
279
Steve Anton74255ff2018-01-25 02:32:57280 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
281 return AddTrack(CreateLocalAudioTrack());
282 }
deadbeef1dcb1642017-03-30 04:08:16283
Steve Anton74255ff2018-01-25 02:32:57284 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
285 return AddTrack(CreateLocalVideoTrack());
286 }
deadbeef1dcb1642017-03-30 04:08:16287
288 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
289 FakeConstraints constraints;
290 // Disable highpass filter so that we can get all the test audio frames.
291 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
292 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
293 peer_connection_factory_->CreateAudioSource(&constraints);
294 // TODO(perkj): Test audio source when it is implemented. Currently audio
295 // always use the default input.
deadbeefb1a15d72017-09-07 21:12:05296 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-30 04:08:16297 source);
298 }
299
300 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 21:12:05301 return CreateLocalVideoTrackInternal(FakeConstraints(),
302 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-30 04:08:16303 }
304
305 rtc::scoped_refptr<webrtc::VideoTrackInterface>
306 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 21:12:05307 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-30 04:08:16308 }
309
310 rtc::scoped_refptr<webrtc::VideoTrackInterface>
311 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 21:12:05312 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-30 04:08:16313 }
314
Steve Anton74255ff2018-01-25 02:32:57315 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
316 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:10317 const std::vector<std::string>& stream_ids = {}) {
318 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 18:26:49319 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-25 02:32:57320 return result.MoveValue();
321 }
322
323 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
324 cricket::MediaType media_type) {
325 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
326 for (auto receiver : pc()->GetReceivers()) {
327 if (receiver->media_type() == media_type) {
328 receivers.push_back(receiver);
329 }
330 }
331 return receivers;
deadbeef1dcb1642017-03-30 04:08:16332 }
333
Seth Hampson2f0d7022018-02-20 19:54:42334 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
335 cricket::MediaType media_type) {
336 for (auto transceiver : pc()->GetTransceivers()) {
337 if (transceiver->receiver()->media_type() == media_type) {
338 return transceiver;
339 }
340 }
341 return nullptr;
342 }
343
deadbeef1dcb1642017-03-30 04:08:16344 bool SignalingStateStable() {
345 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
346 }
347
348 void CreateDataChannel() { CreateDataChannel(nullptr); }
349
350 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 18:41:54351 CreateDataChannel(kDataChannelLabel, init);
352 }
353
354 void CreateDataChannel(const std::string& label,
355 const webrtc::DataChannelInit* init) {
356 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-30 04:08:16357 ASSERT_TRUE(data_channel_.get() != nullptr);
358 data_observer_.reset(new MockDataChannelObserver(data_channel_));
359 }
360
361 DataChannelInterface* data_channel() { return data_channel_; }
362 const MockDataChannelObserver* data_observer() const {
363 return data_observer_.get();
364 }
365
366 int audio_frames_received() const {
367 return fake_audio_capture_module_->frames_received();
368 }
369
370 // Takes minimum of video frames received for each track.
371 //
372 // Can be used like:
373 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
374 //
375 // To ensure that all video tracks received at least a certain number of
376 // frames.
377 int min_video_frames_received_per_track() const {
378 int min_frames = INT_MAX;
379 if (video_decoder_factory_enabled_) {
380 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
381 fake_video_decoder_factory_->decoders();
382 if (decoders.empty()) {
383 return 0;
384 }
385 for (FakeWebRtcVideoDecoder* decoder : decoders) {
386 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
387 }
388 return min_frames;
389 } else {
390 if (fake_video_renderers_.empty()) {
391 return 0;
392 }
393
394 for (const auto& pair : fake_video_renderers_) {
395 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
396 }
397 return min_frames;
398 }
399 }
400
401 // In contrast to the above, sums the video frames received for all tracks.
402 // Can be used to verify that no video frames were received, or that the
403 // counts didn't increase.
404 int total_video_frames_received() const {
405 int total = 0;
406 if (video_decoder_factory_enabled_) {
407 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
408 fake_video_decoder_factory_->decoders();
409 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
410 total += decoder->GetNumFramesReceived();
411 }
412 } else {
413 for (const auto& pair : fake_video_renderers_) {
414 total += pair.second->num_rendered_frames();
415 }
416 for (const auto& renderer : removed_fake_video_renderers_) {
417 total += renderer->num_rendered_frames();
418 }
419 }
420 return total;
421 }
422
423 // Returns a MockStatsObserver in a state after stats gathering finished,
424 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 23:01:17425 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-30 04:08:16426 webrtc::MediaStreamTrackInterface* track) {
427 rtc::scoped_refptr<MockStatsObserver> observer(
428 new rtc::RefCountedObject<MockStatsObserver>());
429 EXPECT_TRUE(peer_connection_->GetStats(
430 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
431 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
432 return observer;
433 }
434
435 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 23:01:17436 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
437 return OldGetStatsForTrack(nullptr);
438 }
439
440 // Synchronously gets stats and returns them. If it times out, fails the test
441 // and returns null.
442 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
443 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
444 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
445 peer_connection_->GetStats(callback);
446 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
447 return callback->report();
deadbeef1dcb1642017-03-30 04:08:16448 }
449
450 int rendered_width() {
451 EXPECT_FALSE(fake_video_renderers_.empty());
452 return fake_video_renderers_.empty()
453 ? 0
454 : fake_video_renderers_.begin()->second->width();
455 }
456
457 int rendered_height() {
458 EXPECT_FALSE(fake_video_renderers_.empty());
459 return fake_video_renderers_.empty()
460 ? 0
461 : fake_video_renderers_.begin()->second->height();
462 }
463
464 double rendered_aspect_ratio() {
465 if (rendered_height() == 0) {
466 return 0.0;
467 }
468 return static_cast<double>(rendered_width()) / rendered_height();
469 }
470
471 webrtc::VideoRotation rendered_rotation() {
472 EXPECT_FALSE(fake_video_renderers_.empty());
473 return fake_video_renderers_.empty()
474 ? webrtc::kVideoRotation_0
475 : fake_video_renderers_.begin()->second->rotation();
476 }
477
478 int local_rendered_width() {
479 return local_video_renderer_ ? local_video_renderer_->width() : 0;
480 }
481
482 int local_rendered_height() {
483 return local_video_renderer_ ? local_video_renderer_->height() : 0;
484 }
485
486 double local_rendered_aspect_ratio() {
487 if (local_rendered_height() == 0) {
488 return 0.0;
489 }
490 return static_cast<double>(local_rendered_width()) /
491 local_rendered_height();
492 }
493
494 size_t number_of_remote_streams() {
495 if (!pc()) {
496 return 0;
497 }
498 return pc()->remote_streams()->count();
499 }
500
501 StreamCollectionInterface* remote_streams() const {
502 if (!pc()) {
503 ADD_FAILURE();
504 return nullptr;
505 }
506 return pc()->remote_streams();
507 }
508
509 StreamCollectionInterface* local_streams() {
510 if (!pc()) {
511 ADD_FAILURE();
512 return nullptr;
513 }
514 return pc()->local_streams();
515 }
516
517 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
518 return pc()->signaling_state();
519 }
520
521 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
522 return pc()->ice_connection_state();
523 }
524
525 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
526 return pc()->ice_gathering_state();
527 }
528
529 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
530 // GetReceivers. They're updated automatically when a remote offer/answer
531 // from the fake signaling channel is applied, or when
532 // ResetRtpReceiverObservers below is called.
533 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
534 rtp_receiver_observers() {
535 return rtp_receiver_observers_;
536 }
537
538 void ResetRtpReceiverObservers() {
539 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 16:04:20540 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
541 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-30 04:08:16542 std::unique_ptr<MockRtpReceiverObserver> observer(
543 new MockRtpReceiverObserver(receiver->media_type()));
544 receiver->SetObserver(observer.get());
545 rtp_receiver_observers_.push_back(std::move(observer));
546 }
547 }
548
Steve Antonede9ca52017-10-16 20:04:27549 rtc::FakeNetworkManager* network() const {
550 return fake_network_manager_.get();
551 }
552 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
553
deadbeef1dcb1642017-03-30 04:08:16554 private:
555 explicit PeerConnectionWrapper(const std::string& debug_name)
556 : debug_name_(debug_name) {}
557
558 bool Init(
559 const MediaConstraintsInterface* constraints,
560 const PeerConnectionFactory::Options* options,
561 const PeerConnectionInterface::RTCConfiguration* config,
562 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
563 rtc::Thread* network_thread,
564 rtc::Thread* worker_thread) {
565 // There's an error in this test code if Init ends up being called twice.
566 RTC_DCHECK(!peer_connection_);
567 RTC_DCHECK(!peer_connection_factory_);
568
569 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 20:04:27570 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-30 04:08:16571
572 std::unique_ptr<cricket::PortAllocator> port_allocator(
573 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 20:04:27574 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-30 04:08:16575 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
576 if (!fake_audio_capture_module_) {
577 return false;
578 }
579 // Note that these factories don't end up getting used unless supported
580 // codecs are added to them.
581 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
582 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
583 rtc::Thread* const signaling_thread = rtc::Thread::Current();
584 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
585 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 12:48:54586 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
587 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-30 04:08:16588 fake_video_decoder_factory_);
589 if (!peer_connection_factory_) {
590 return false;
591 }
592 if (options) {
593 peer_connection_factory_->SetOptions(*options);
594 }
Seth Hampson2f0d7022018-02-20 19:54:42595 if (config) {
596 sdp_semantics_ = config->sdp_semantics;
597 }
deadbeef1dcb1642017-03-30 04:08:16598 peer_connection_ =
599 CreatePeerConnection(std::move(port_allocator), constraints, config,
600 std::move(cert_generator));
601 return peer_connection_.get() != nullptr;
602 }
603
604 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
605 std::unique_ptr<cricket::PortAllocator> port_allocator,
606 const MediaConstraintsInterface* constraints,
607 const PeerConnectionInterface::RTCConfiguration* config,
608 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
609 PeerConnectionInterface::RTCConfiguration modified_config;
610 // If |config| is null, this will result in a default configuration being
611 // used.
612 if (config) {
613 modified_config = *config;
614 }
615 // Disable resolution adaptation; we don't want it interfering with the
616 // test results.
617 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
618 // ratios and not specific resolutions, is this even necessary?
619 modified_config.set_cpu_adaptation(false);
620
621 return peer_connection_factory_->CreatePeerConnection(
622 modified_config, constraints, std::move(port_allocator),
623 std::move(cert_generator), this);
624 }
625
626 void set_signaling_message_receiver(
627 SignalingMessageReceiver* signaling_message_receiver) {
628 signaling_message_receiver_ = signaling_message_receiver;
629 }
630
631 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
632
Steve Antonede9ca52017-10-16 20:04:27633 void set_signal_ice_candidates(bool signal) {
634 signal_ice_candidates_ = signal;
635 }
636
deadbeef1dcb1642017-03-30 04:08:16637 void EnableVideoDecoderFactory() {
638 video_decoder_factory_enabled_ = true;
639 fake_video_decoder_factory_->AddSupportedVideoCodecType(
640 webrtc::kVideoCodecVP8);
641 }
642
643 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-30 04:08:16644 const FakeConstraints& constraints,
645 webrtc::VideoRotation rotation) {
646 // Set max frame rate to 10fps to reduce the risk of test flakiness.
647 // TODO(deadbeef): Do something more robust.
648 FakeConstraints source_constraints = constraints;
649 source_constraints.SetMandatoryMaxFrameRate(10);
650
651 cricket::FakeVideoCapturer* fake_capturer =
652 new webrtc::FakePeriodicVideoCapturer();
653 fake_capturer->SetRotation(rotation);
654 video_capturers_.push_back(fake_capturer);
655 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
656 peer_connection_factory_->CreateVideoSource(fake_capturer,
657 &source_constraints);
658 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 21:12:05659 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
660 source));
deadbeef1dcb1642017-03-30 04:08:16661 if (!local_video_renderer_) {
662 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
663 }
664 return track;
665 }
666
667 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 10:09:25668 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 18:27:41669 std::unique_ptr<SessionDescriptionInterface> desc =
670 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-30 04:08:16671 if (received_sdp_munger_) {
672 received_sdp_munger_(desc->description());
673 }
674
675 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
676 // Setting a remote description may have changed the number of receivers,
677 // so reset the receiver observers.
678 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 19:54:42679 if (remote_offer_handler_) {
680 remote_offer_handler_();
681 }
deadbeef1dcb1642017-03-30 04:08:16682 auto answer = CreateAnswer();
683 ASSERT_NE(nullptr, answer);
684 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
685 }
686
687 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 10:09:25688 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 18:27:41689 std::unique_ptr<SessionDescriptionInterface> desc =
690 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-30 04:08:16691 if (received_sdp_munger_) {
692 received_sdp_munger_(desc->description());
693 }
694
695 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
696 // Set the RtpReceiverObserver after receivers are created.
697 ResetRtpReceiverObservers();
698 }
699
700 // Returns null on failure.
701 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
702 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
703 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
704 pc()->CreateOffer(observer, offer_answer_options_);
705 return WaitForDescriptionFromObserver(observer);
706 }
707
708 // Returns null on failure.
709 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
710 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
711 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
712 pc()->CreateAnswer(observer, offer_answer_options_);
713 return WaitForDescriptionFromObserver(observer);
714 }
715
716 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 16:04:20717 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-30 04:08:16718 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
719 if (!observer->result()) {
720 return nullptr;
721 }
722 auto description = observer->MoveDescription();
723 if (generated_sdp_munger_) {
724 generated_sdp_munger_(description->description());
725 }
726 return description;
727 }
728
729 // Setting the local description and sending the SDP message over the fake
730 // signaling channel are combined into the same method because the SDP
731 // message needs to be sent as soon as SetLocalDescription finishes, without
732 // waiting for the observer to be called. This ensures that ICE candidates
733 // don't outrace the description.
734 bool SetLocalDescriptionAndSendSdpMessage(
735 std::unique_ptr<SessionDescriptionInterface> desc) {
736 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
737 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 10:09:25738 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 18:27:41739 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-30 04:08:16740 std::string sdp;
741 EXPECT_TRUE(desc->ToString(&sdp));
742 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 19:54:42743 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
744 RemoveUnusedVideoRenderers();
745 }
deadbeef1dcb1642017-03-30 04:08:16746 // As mentioned above, we need to send the message immediately after
747 // SetLocalDescription.
748 SendSdpMessage(type, sdp);
749 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
750 return true;
751 }
752
753 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
754 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
755 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 10:09:25756 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-30 04:08:16757 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 19:54:42758 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
759 RemoveUnusedVideoRenderers();
760 }
deadbeef1dcb1642017-03-30 04:08:16761 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
762 return observer->result();
763 }
764
Seth Hampson2f0d7022018-02-20 19:54:42765 // This is a work around to remove unused fake_video_renderers from
766 // transceivers that have either stopped or are no longer receiving.
767 void RemoveUnusedVideoRenderers() {
768 auto transceivers = pc()->GetTransceivers();
769 for (auto& transceiver : transceivers) {
770 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
771 continue;
772 }
773 // Remove fake video renderers from any stopped transceivers.
774 if (transceiver->stopped()) {
775 auto it =
776 fake_video_renderers_.find(transceiver->receiver()->track()->id());
777 if (it != fake_video_renderers_.end()) {
778 fake_video_renderers_.erase(it);
779 }
780 }
781 // Remove fake video renderers from any transceivers that are no longer
782 // receiving.
783 if ((transceiver->current_direction() &&
784 !webrtc::RtpTransceiverDirectionHasRecv(
785 *transceiver->current_direction()))) {
786 auto it =
787 fake_video_renderers_.find(transceiver->receiver()->track()->id());
788 if (it != fake_video_renderers_.end()) {
789 fake_video_renderers_.erase(it);
790 }
791 }
792 }
793 }
794
deadbeef1dcb1642017-03-30 04:08:16795 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
796 // default).
Steve Antona3a92c22017-12-07 18:27:41797 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-30 04:08:16798 if (signaling_delay_ms_ == 0) {
799 RelaySdpMessageIfReceiverExists(type, msg);
800 } else {
801 invoker_.AsyncInvokeDelayed<void>(
802 RTC_FROM_HERE, rtc::Thread::Current(),
803 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
804 this, type, msg),
805 signaling_delay_ms_);
806 }
807 }
808
Steve Antona3a92c22017-12-07 18:27:41809 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-30 04:08:16810 if (signaling_message_receiver_) {
811 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
812 }
813 }
814
815 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
816 // default).
817 void SendIceMessage(const std::string& sdp_mid,
818 int sdp_mline_index,
819 const std::string& msg) {
820 if (signaling_delay_ms_ == 0) {
821 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
822 } else {
823 invoker_.AsyncInvokeDelayed<void>(
824 RTC_FROM_HERE, rtc::Thread::Current(),
825 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
826 this, sdp_mid, sdp_mline_index, msg),
827 signaling_delay_ms_);
828 }
829 }
830
831 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
832 int sdp_mline_index,
833 const std::string& msg) {
834 if (signaling_message_receiver_) {
835 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
836 msg);
837 }
838 }
839
840 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 18:27:41841 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
842 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-30 04:08:16843 HandleIncomingOffer(msg);
844 } else {
845 HandleIncomingAnswer(msg);
846 }
847 }
848
849 void ReceiveIceMessage(const std::string& sdp_mid,
850 int sdp_mline_index,
851 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 10:09:25852 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-30 04:08:16853 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
854 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
855 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
856 }
857
858 // PeerConnectionObserver callbacks.
859 void OnSignalingChange(
860 webrtc::PeerConnectionInterface::SignalingState new_state) override {
861 EXPECT_EQ(pc()->signaling_state(), new_state);
862 }
Steve Anton15324772018-01-16 18:26:49863 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
864 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
865 streams) override {
866 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
867 rtc::scoped_refptr<VideoTrackInterface> video_track(
868 static_cast<VideoTrackInterface*>(receiver->track().get()));
869 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-30 04:08:16870 fake_video_renderers_.end());
Steve Anton15324772018-01-16 18:26:49871 fake_video_renderers_[video_track->id()] =
872 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-30 04:08:16873 }
874 }
Steve Anton15324772018-01-16 18:26:49875 void OnRemoveTrack(
876 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
877 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
878 auto it = fake_video_renderers_.find(receiver->track()->id());
879 RTC_DCHECK(it != fake_video_renderers_.end());
880 fake_video_renderers_.erase(it);
881 }
882 }
deadbeef1dcb1642017-03-30 04:08:16883 void OnRenegotiationNeeded() override {}
884 void OnIceConnectionChange(
885 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
886 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 20:04:27887 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-30 04:08:16888 }
889 void OnIceGatheringChange(
890 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-30 04:08:16891 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 20:04:27892 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-30 04:08:16893 }
894 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 10:09:25895 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-30 04:08:16896
897 std::string ice_sdp;
898 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 20:04:27899 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-30 04:08:16900 // Remote party may be deleted.
901 return;
902 }
903 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
904 }
905 void OnDataChannel(
906 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 10:09:25907 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-30 04:08:16908 data_channel_ = data_channel;
909 data_observer_.reset(new MockDataChannelObserver(data_channel));
910 }
911
deadbeef1dcb1642017-03-30 04:08:16912 std::string debug_name_;
913
914 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
915
916 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
917 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
918 peer_connection_factory_;
919
Steve Antonede9ca52017-10-16 20:04:27920 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-30 04:08:16921 // Needed to keep track of number of frames sent.
922 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
923 // Needed to keep track of number of frames received.
924 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
925 fake_video_renderers_;
926 // Needed to ensure frames aren't received for removed tracks.
927 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
928 removed_fake_video_renderers_;
929 // Needed to keep track of number of frames received when external decoder
930 // used.
931 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
932 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
933 bool video_decoder_factory_enabled_ = false;
934
935 // For remote peer communication.
936 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
937 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 20:04:27938 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-30 04:08:16939
940 // Store references to the video capturers we've created, so that we can stop
941 // them, if required.
942 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
943 // |local_video_renderer_| attached to the first created local video track.
944 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
945
Seth Hampson2f0d7022018-02-20 19:54:42946 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-30 04:08:16947 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
948 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
949 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 19:54:42950 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-30 04:08:16951
952 rtc::scoped_refptr<DataChannelInterface> data_channel_;
953 std::unique_ptr<MockDataChannelObserver> data_observer_;
954
955 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
956
Steve Antonede9ca52017-10-16 20:04:27957 std::vector<PeerConnectionInterface::IceConnectionState>
958 ice_connection_state_history_;
959 std::vector<PeerConnectionInterface::IceGatheringState>
960 ice_gathering_state_history_;
deadbeef1dcb1642017-03-30 04:08:16961
962 rtc::AsyncInvoker invoker_;
963
Seth Hampson2f0d7022018-02-20 19:54:42964 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-30 04:08:16965};
966
Elad Alon99c3fe52017-10-13 14:29:40967class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
968 public:
969 virtual ~MockRtcEventLogOutput() = default;
970 MOCK_CONST_METHOD0(IsActive, bool());
971 MOCK_METHOD1(Write, bool(const std::string&));
972};
973
Seth Hampson2f0d7022018-02-20 19:54:42974// This helper object is used for both specifying how many audio/video frames
975// are expected to be received for a caller/callee. It provides helper functions
976// to specify these expectations. The object initially starts in a state of no
977// expectations.
978class MediaExpectations {
979 public:
980 enum ExpectFrames {
981 kExpectSomeFrames,
982 kExpectNoFrames,
983 kNoExpectation,
984 };
985
986 void ExpectBidirectionalAudioAndVideo() {
987 ExpectBidirectionalAudio();
988 ExpectBidirectionalVideo();
989 }
990
991 void ExpectBidirectionalAudio() {
992 CallerExpectsSomeAudio();
993 CalleeExpectsSomeAudio();
994 }
995
996 void ExpectNoAudio() {
997 CallerExpectsNoAudio();
998 CalleeExpectsNoAudio();
999 }
1000
1001 void ExpectBidirectionalVideo() {
1002 CallerExpectsSomeVideo();
1003 CalleeExpectsSomeVideo();
1004 }
1005
1006 void ExpectNoVideo() {
1007 CallerExpectsNoVideo();
1008 CalleeExpectsNoVideo();
1009 }
1010
1011 void CallerExpectsSomeAudioAndVideo() {
1012 CallerExpectsSomeAudio();
1013 CallerExpectsSomeVideo();
1014 }
1015
1016 void CalleeExpectsSomeAudioAndVideo() {
1017 CalleeExpectsSomeAudio();
1018 CalleeExpectsSomeVideo();
1019 }
1020
1021 // Caller's audio functions.
1022 void CallerExpectsSomeAudio(
1023 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1024 caller_audio_expectation_ = kExpectSomeFrames;
1025 caller_audio_frames_expected_ = expected_audio_frames;
1026 }
1027
1028 void CallerExpectsNoAudio() {
1029 caller_audio_expectation_ = kExpectNoFrames;
1030 caller_audio_frames_expected_ = 0;
1031 }
1032
1033 // Caller's video functions.
1034 void CallerExpectsSomeVideo(
1035 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1036 caller_video_expectation_ = kExpectSomeFrames;
1037 caller_video_frames_expected_ = expected_video_frames;
1038 }
1039
1040 void CallerExpectsNoVideo() {
1041 caller_video_expectation_ = kExpectNoFrames;
1042 caller_video_frames_expected_ = 0;
1043 }
1044
1045 // Callee's audio functions.
1046 void CalleeExpectsSomeAudio(
1047 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1048 callee_audio_expectation_ = kExpectSomeFrames;
1049 callee_audio_frames_expected_ = expected_audio_frames;
1050 }
1051
1052 void CalleeExpectsNoAudio() {
1053 callee_audio_expectation_ = kExpectNoFrames;
1054 callee_audio_frames_expected_ = 0;
1055 }
1056
1057 // Callee's video functions.
1058 void CalleeExpectsSomeVideo(
1059 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1060 callee_video_expectation_ = kExpectSomeFrames;
1061 callee_video_frames_expected_ = expected_video_frames;
1062 }
1063
1064 void CalleeExpectsNoVideo() {
1065 callee_video_expectation_ = kExpectNoFrames;
1066 callee_video_frames_expected_ = 0;
1067 }
1068
1069 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1070 ExpectFrames caller_video_expectation_ = kNoExpectation;
1071 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1072 ExpectFrames callee_video_expectation_ = kNoExpectation;
1073 int caller_audio_frames_expected_ = 0;
1074 int caller_video_frames_expected_ = 0;
1075 int callee_audio_frames_expected_ = 0;
1076 int callee_video_frames_expected_ = 0;
1077};
1078
deadbeef1dcb1642017-03-30 04:08:161079// Tests two PeerConnections connecting to each other end-to-end, using a
1080// virtual network, fake A/V capture and fake encoder/decoders. The
1081// PeerConnections share the threads/socket servers, but use separate versions
1082// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 19:54:421083class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-30 04:08:161084 public:
Seth Hampson2f0d7022018-02-20 19:54:421085 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1086 : sdp_semantics_(sdp_semantics),
1087 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 20:04:271088 fss_(new rtc::FirewallSocketServer(ss_.get())),
1089 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-30 04:08:161090 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 14:21:481091 network_thread_->SetName("PCNetworkThread", this);
1092 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-30 04:08:161093 RTC_CHECK(network_thread_->Start());
1094 RTC_CHECK(worker_thread_->Start());
1095 }
1096
Seth Hampson2f0d7022018-02-20 19:54:421097 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-30 04:08:161098 if (caller_) {
1099 caller_->set_signaling_message_receiver(nullptr);
1100 }
1101 if (callee_) {
1102 callee_->set_signaling_message_receiver(nullptr);
1103 }
1104 }
1105
1106 bool SignalingStateStable() {
1107 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1108 }
1109
deadbeef71452802017-05-08 00:21:011110 bool DtlsConnected() {
1111 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1112 // are connected. This is an important distinction. Once we have separate
1113 // ICE and DTLS state, this check needs to use the DTLS state.
1114 return (callee()->ice_connection_state() ==
1115 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1116 callee()->ice_connection_state() ==
1117 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1118 (caller()->ice_connection_state() ==
1119 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1120 caller()->ice_connection_state() ==
1121 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1122 }
1123
Seth Hampson2f0d7022018-02-20 19:54:421124 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1125 const std::string& debug_name,
1126 const MediaConstraintsInterface* constraints,
1127 const PeerConnectionFactory::Options* options,
1128 const RTCConfiguration* config,
1129 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1130 RTCConfiguration modified_config;
1131 if (config) {
1132 modified_config = *config;
1133 }
1134 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1135 modified_config.sdp_semantics = sdp_semantics_;
1136 }
1137 if (!cert_generator) {
1138 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1139 }
1140 std::unique_ptr<PeerConnectionWrapper> client(
1141 new PeerConnectionWrapper(debug_name));
1142 if (!client->Init(constraints, options, &modified_config,
1143 std::move(cert_generator), network_thread_.get(),
1144 worker_thread_.get())) {
1145 return nullptr;
1146 }
1147 return client;
1148 }
1149
deadbeef1dcb1642017-03-30 04:08:161150 bool CreatePeerConnectionWrappers() {
1151 return CreatePeerConnectionWrappersWithConfig(
1152 PeerConnectionInterface::RTCConfiguration(),
1153 PeerConnectionInterface::RTCConfiguration());
1154 }
1155
1156 bool CreatePeerConnectionWrappersWithConstraints(
1157 MediaConstraintsInterface* caller_constraints,
1158 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 19:54:421159 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1160 nullptr, nullptr);
1161 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1162 nullptr, nullptr);
deadbeef1dcb1642017-03-30 04:08:161163 return caller_ && callee_;
1164 }
1165
1166 bool CreatePeerConnectionWrappersWithConfig(
1167 const PeerConnectionInterface::RTCConfiguration& caller_config,
1168 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 19:54:421169 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1170 &caller_config, nullptr);
1171 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1172 &callee_config, nullptr);
deadbeef1dcb1642017-03-30 04:08:161173 return caller_ && callee_;
1174 }
1175
1176 bool CreatePeerConnectionWrappersWithOptions(
1177 const PeerConnectionFactory::Options& caller_options,
1178 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 19:54:421179 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1180 nullptr, nullptr);
1181 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1182 nullptr, nullptr);
deadbeef1dcb1642017-03-30 04:08:161183 return caller_ && callee_;
1184 }
1185
Seth Hampson2f0d7022018-02-20 19:54:421186 std::unique_ptr<PeerConnectionWrapper>
1187 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-30 04:08:161188 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1189 new FakeRTCCertificateGenerator());
1190 cert_generator->use_alternate_key();
1191
Seth Hampson2f0d7022018-02-20 19:54:421192 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1193 std::move(cert_generator));
deadbeef1dcb1642017-03-30 04:08:161194 }
1195
1196 // Once called, SDP blobs and ICE candidates will be automatically signaled
1197 // between PeerConnections.
1198 void ConnectFakeSignaling() {
1199 caller_->set_signaling_message_receiver(callee_.get());
1200 callee_->set_signaling_message_receiver(caller_.get());
1201 }
1202
Steve Antonede9ca52017-10-16 20:04:271203 // Once called, SDP blobs will be automatically signaled between
1204 // PeerConnections. Note that ICE candidates will not be signaled unless they
1205 // are in the exchanged SDP blobs.
1206 void ConnectFakeSignalingForSdpOnly() {
1207 ConnectFakeSignaling();
1208 SetSignalIceCandidates(false);
1209 }
1210
deadbeef1dcb1642017-03-30 04:08:161211 void SetSignalingDelayMs(int delay_ms) {
1212 caller_->set_signaling_delay_ms(delay_ms);
1213 callee_->set_signaling_delay_ms(delay_ms);
1214 }
1215
Steve Antonede9ca52017-10-16 20:04:271216 void SetSignalIceCandidates(bool signal) {
1217 caller_->set_signal_ice_candidates(signal);
1218 callee_->set_signal_ice_candidates(signal);
1219 }
1220
deadbeef1dcb1642017-03-30 04:08:161221 void EnableVideoDecoderFactory() {
1222 caller_->EnableVideoDecoderFactory();
1223 callee_->EnableVideoDecoderFactory();
1224 }
1225
1226 // Messages may get lost on the unreliable DataChannel, so we send multiple
1227 // times to avoid test flakiness.
1228 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1229 const std::string& data,
1230 int retries) {
1231 for (int i = 0; i < retries; ++i) {
1232 dc->Send(DataBuffer(data));
1233 }
1234 }
1235
1236 rtc::Thread* network_thread() { return network_thread_.get(); }
1237
1238 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1239
1240 PeerConnectionWrapper* caller() { return caller_.get(); }
1241
1242 // Set the |caller_| to the |wrapper| passed in and return the
1243 // original |caller_|.
1244 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1245 PeerConnectionWrapper* wrapper) {
1246 PeerConnectionWrapper* old = caller_.release();
1247 caller_.reset(wrapper);
1248 return old;
1249 }
1250
1251 PeerConnectionWrapper* callee() { return callee_.get(); }
1252
1253 // Set the |callee_| to the |wrapper| passed in and return the
1254 // original |callee_|.
1255 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1256 PeerConnectionWrapper* wrapper) {
1257 PeerConnectionWrapper* old = callee_.release();
1258 callee_.reset(wrapper);
1259 return old;
1260 }
1261
Steve Antonede9ca52017-10-16 20:04:271262 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1263
Seth Hampson2f0d7022018-02-20 19:54:421264 // Expects the provided number of new frames to be received within
1265 // kMaxWaitForFramesMs. The new expected frames are specified in
1266 // |media_expectations|. Returns false if any of the expectations were
1267 // not met.
1268 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1269 // First initialize the expected frame counts based upon the current
1270 // frame count.
1271 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1272 if (media_expectations.caller_audio_expectation_ ==
1273 MediaExpectations::kExpectSomeFrames) {
1274 total_caller_audio_frames_expected +=
1275 media_expectations.caller_audio_frames_expected_;
1276 }
1277 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-30 04:08:161278 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 19:54:421279 if (media_expectations.caller_video_expectation_ ==
1280 MediaExpectations::kExpectSomeFrames) {
1281 total_caller_video_frames_expected +=
1282 media_expectations.caller_video_frames_expected_;
1283 }
1284 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1285 if (media_expectations.callee_audio_expectation_ ==
1286 MediaExpectations::kExpectSomeFrames) {
1287 total_callee_audio_frames_expected +=
1288 media_expectations.callee_audio_frames_expected_;
1289 }
1290 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-30 04:08:161291 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 19:54:421292 if (media_expectations.callee_video_expectation_ ==
1293 MediaExpectations::kExpectSomeFrames) {
1294 total_callee_video_frames_expected +=
1295 media_expectations.callee_video_frames_expected_;
1296 }
deadbeef1dcb1642017-03-30 04:08:161297
Seth Hampson2f0d7022018-02-20 19:54:421298 // Wait for the expected frames.
deadbeef1dcb1642017-03-30 04:08:161299 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 19:54:421300 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-30 04:08:161301 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 19:54:421302 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-30 04:08:161303 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 19:54:421304 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-30 04:08:161305 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 19:54:421306 total_callee_video_frames_expected,
1307 kMaxWaitForFramesMs);
1308 bool expectations_correct =
1309 caller()->audio_frames_received() >=
1310 total_caller_audio_frames_expected &&
1311 caller()->min_video_frames_received_per_track() >=
1312 total_caller_video_frames_expected &&
1313 callee()->audio_frames_received() >=
1314 total_callee_audio_frames_expected &&
1315 callee()->min_video_frames_received_per_track() >=
1316 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-30 04:08:161317
Seth Hampson2f0d7022018-02-20 19:54:421318 // After the combined wait, print out a more detailed message upon
1319 // failure.
deadbeef1dcb1642017-03-30 04:08:161320 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 19:54:421321 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-30 04:08:161322 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 19:54:421323 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-30 04:08:161324 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 19:54:421325 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-30 04:08:161326 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 19:54:421327 total_callee_video_frames_expected);
1328
1329 // We want to make sure nothing unexpected was received.
1330 if (media_expectations.caller_audio_expectation_ ==
1331 MediaExpectations::kExpectNoFrames) {
1332 EXPECT_EQ(caller()->audio_frames_received(),
1333 total_caller_audio_frames_expected);
1334 if (caller()->audio_frames_received() !=
1335 total_caller_audio_frames_expected) {
1336 expectations_correct = false;
1337 }
1338 }
1339 if (media_expectations.caller_video_expectation_ ==
1340 MediaExpectations::kExpectNoFrames) {
1341 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1342 total_caller_video_frames_expected);
1343 if (caller()->min_video_frames_received_per_track() !=
1344 total_caller_video_frames_expected) {
1345 expectations_correct = false;
1346 }
1347 }
1348 if (media_expectations.callee_audio_expectation_ ==
1349 MediaExpectations::kExpectNoFrames) {
1350 EXPECT_EQ(callee()->audio_frames_received(),
1351 total_callee_audio_frames_expected);
1352 if (callee()->audio_frames_received() !=
1353 total_callee_audio_frames_expected) {
1354 expectations_correct = false;
1355 }
1356 }
1357 if (media_expectations.callee_video_expectation_ ==
1358 MediaExpectations::kExpectNoFrames) {
1359 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1360 total_callee_video_frames_expected);
1361 if (callee()->min_video_frames_received_per_track() !=
1362 total_callee_video_frames_expected) {
1363 expectations_correct = false;
1364 }
1365 }
1366 return expectations_correct;
deadbeef1dcb1642017-03-30 04:08:161367 }
1368
Taylor Brandstetter5e55fe82018-03-23 18:50:161369 void TestNegotiatedCipherSuite(
1370 const PeerConnectionFactory::Options& caller_options,
1371 const PeerConnectionFactory::Options& callee_options,
1372 int expected_cipher_suite) {
deadbeef1dcb1642017-03-30 04:08:161373 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1374 callee_options));
1375 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1376 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1377 caller()->pc()->RegisterUMAObserver(caller_observer);
1378 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491379 caller()->AddAudioVideoTracks();
1380 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161381 caller()->CreateAndSetAndSignalOffer();
1382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1383 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 23:01:171384 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-30 04:08:161385 EXPECT_EQ(
1386 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1387 expected_cipher_suite));
1388 caller()->pc()->RegisterUMAObserver(nullptr);
1389 }
1390
Taylor Brandstetter5e55fe82018-03-23 18:50:161391 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1392 bool remote_gcm_enabled,
1393 int expected_cipher_suite) {
1394 PeerConnectionFactory::Options caller_options;
1395 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1396 PeerConnectionFactory::Options callee_options;
1397 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1398 TestNegotiatedCipherSuite(caller_options, callee_options,
1399 expected_cipher_suite);
1400 }
1401
Seth Hampson2f0d7022018-02-20 19:54:421402 protected:
1403 const SdpSemantics sdp_semantics_;
1404
deadbeef1dcb1642017-03-30 04:08:161405 private:
1406 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-30 04:08:161407 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 20:04:271408 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-30 04:08:161409 // |network_thread_| and |worker_thread_| are used by both
1410 // |caller_| and |callee_| so they must be destroyed
1411 // later.
1412 std::unique_ptr<rtc::Thread> network_thread_;
1413 std::unique_ptr<rtc::Thread> worker_thread_;
1414 std::unique_ptr<PeerConnectionWrapper> caller_;
1415 std::unique_ptr<PeerConnectionWrapper> callee_;
1416};
1417
Seth Hampson2f0d7022018-02-20 19:54:421418class PeerConnectionIntegrationTest
1419 : public PeerConnectionIntegrationBaseTest,
1420 public ::testing::WithParamInterface<SdpSemantics> {
1421 protected:
1422 PeerConnectionIntegrationTest()
1423 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1424};
1425
1426class PeerConnectionIntegrationTestPlanB
1427 : public PeerConnectionIntegrationBaseTest {
1428 protected:
1429 PeerConnectionIntegrationTestPlanB()
1430 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1431};
1432
1433class PeerConnectionIntegrationTestUnifiedPlan
1434 : public PeerConnectionIntegrationBaseTest {
1435 protected:
1436 PeerConnectionIntegrationTestUnifiedPlan()
1437 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1438};
1439
deadbeef1dcb1642017-03-30 04:08:161440// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1441// includes testing that the callback is invoked if an observer is connected
1442// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 19:54:421443TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:161444 RtpReceiverObserverOnFirstPacketReceived) {
1445 ASSERT_TRUE(CreatePeerConnectionWrappers());
1446 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491447 caller()->AddAudioVideoTracks();
1448 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161449 // Start offer/answer exchange and wait for it to complete.
1450 caller()->CreateAndSetAndSignalOffer();
1451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1452 // Should be one receiver each for audio/video.
1453 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1454 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1455 // Wait for all "first packet received" callbacks to be fired.
1456 EXPECT_TRUE_WAIT(
1457 std::all_of(caller()->rtp_receiver_observers().begin(),
1458 caller()->rtp_receiver_observers().end(),
1459 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1460 return o->first_packet_received();
1461 }),
1462 kMaxWaitForFramesMs);
1463 EXPECT_TRUE_WAIT(
1464 std::all_of(callee()->rtp_receiver_observers().begin(),
1465 callee()->rtp_receiver_observers().end(),
1466 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1467 return o->first_packet_received();
1468 }),
1469 kMaxWaitForFramesMs);
1470 // If new observers are set after the first packet was already received, the
1471 // callback should still be invoked.
1472 caller()->ResetRtpReceiverObservers();
1473 callee()->ResetRtpReceiverObservers();
1474 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1475 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1476 EXPECT_TRUE(
1477 std::all_of(caller()->rtp_receiver_observers().begin(),
1478 caller()->rtp_receiver_observers().end(),
1479 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1480 return o->first_packet_received();
1481 }));
1482 EXPECT_TRUE(
1483 std::all_of(callee()->rtp_receiver_observers().begin(),
1484 callee()->rtp_receiver_observers().end(),
1485 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1486 return o->first_packet_received();
1487 }));
1488}
1489
1490class DummyDtmfObserver : public DtmfSenderObserverInterface {
1491 public:
1492 DummyDtmfObserver() : completed_(false) {}
1493
1494 // Implements DtmfSenderObserverInterface.
1495 void OnToneChange(const std::string& tone) override {
1496 tones_.push_back(tone);
1497 if (tone.empty()) {
1498 completed_ = true;
1499 }
1500 }
1501
1502 const std::vector<std::string>& tones() const { return tones_; }
1503 bool completed() const { return completed_; }
1504
1505 private:
1506 bool completed_;
1507 std::vector<std::string> tones_;
1508};
1509
1510// Assumes |sender| already has an audio track added and the offer/answer
1511// exchange is done.
1512void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1513 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 18:26:491514 // We should be able to get a DTMF sender from the local sender.
1515 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1516 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1517 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-30 04:08:161518 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-30 04:08:161519 dtmf_sender->RegisterObserver(&observer);
1520
1521 // Test the DtmfSender object just created.
1522 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1523 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1524
1525 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1526 std::vector<std::string> tones = {"1", "a", ""};
1527 EXPECT_EQ(tones, observer.tones());
1528 dtmf_sender->UnregisterObserver();
1529 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1530}
1531
1532// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1533// direction).
Seth Hampson2f0d7022018-02-20 19:54:421534TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-30 04:08:161535 ASSERT_TRUE(CreatePeerConnectionWrappers());
1536 ConnectFakeSignaling();
1537 // Only need audio for DTMF.
Steve Anton15324772018-01-16 18:26:491538 caller()->AddAudioTrack();
1539 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-30 04:08:161540 caller()->CreateAndSetAndSignalOffer();
1541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-08 00:21:011542 // DTLS must finish before the DTMF sender can be used reliably.
1543 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-30 04:08:161544 TestDtmfFromSenderToReceiver(caller(), callee());
1545 TestDtmfFromSenderToReceiver(callee(), caller());
1546}
1547
1548// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1549// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 19:54:421550TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-30 04:08:161551 ASSERT_TRUE(CreatePeerConnectionWrappers());
1552 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 15:04:131553 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1554 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1555 caller()->pc()->RegisterUMAObserver(caller_observer);
1556
deadbeef1dcb1642017-03-30 04:08:161557 // Do normal offer/answer and wait for some frames to be received in each
1558 // direction.
Steve Anton15324772018-01-16 18:26:491559 caller()->AddAudioVideoTracks();
1560 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161561 caller()->CreateAndSetAndSignalOffer();
1562 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:421563 MediaExpectations media_expectations;
1564 media_expectations.ExpectBidirectionalAudioAndVideo();
1565 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 15:04:131566 EXPECT_LE(
1567 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1568 webrtc::kEnumCounterKeyProtocolDtls));
1569 EXPECT_EQ(
1570 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1571 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-30 04:08:161572}
1573
1574// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 19:54:421575TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-30 04:08:161576 PeerConnectionInterface::RTCConfiguration sdes_config;
1577 sdes_config.enable_dtls_srtp.emplace(false);
1578 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1579 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 15:04:131580 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1581 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1582 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-30 04:08:161583
1584 // Do normal offer/answer and wait for some frames to be received in each
1585 // direction.
Steve Anton15324772018-01-16 18:26:491586 caller()->AddAudioVideoTracks();
1587 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161588 caller()->CreateAndSetAndSignalOffer();
1589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:421590 MediaExpectations media_expectations;
1591 media_expectations.ExpectBidirectionalAudioAndVideo();
1592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 15:04:131593 EXPECT_LE(
1594 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1595 webrtc::kEnumCounterKeyProtocolSdes));
1596 EXPECT_EQ(
1597 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1598 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-30 04:08:161599}
1600
Steve Anton8c0f7a72017-10-03 17:03:101601// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1602// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 19:54:421603TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 17:03:101604 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1605 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1606 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1607 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1608 return pc->GetRemoteAudioSSLCertificate();
1609 };
Zhi Huang70b820f2018-01-27 22:16:151610 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1611 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1612 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1613 return pc->GetRemoteAudioSSLCertChain();
1614 };
Steve Anton8c0f7a72017-10-03 17:03:101615
1616 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1617 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1618
1619 // Configure each side with a known certificate so they can be compared later.
1620 PeerConnectionInterface::RTCConfiguration caller_config;
1621 caller_config.enable_dtls_srtp.emplace(true);
1622 caller_config.certificates.push_back(caller_cert);
1623 PeerConnectionInterface::RTCConfiguration callee_config;
1624 callee_config.enable_dtls_srtp.emplace(true);
1625 callee_config.certificates.push_back(callee_cert);
1626 ASSERT_TRUE(
1627 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1628 ConnectFakeSignaling();
1629
1630 // When first initialized, there should not be a remote SSL certificate (and
1631 // calling this method should not crash).
1632 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1633 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 22:16:151634 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1635 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 17:03:101636
Steve Anton15324772018-01-16 18:26:491637 caller()->AddAudioTrack();
1638 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 17:03:101639 caller()->CreateAndSetAndSignalOffer();
1640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1641 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1642
1643 // Once DTLS has been connected, each side should return the other's SSL
1644 // certificate when calling GetRemoteAudioSSLCertificate.
1645
1646 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1647 ASSERT_TRUE(caller_remote_cert);
1648 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1649 caller_remote_cert->ToPEMString());
1650
1651 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1652 ASSERT_TRUE(callee_remote_cert);
1653 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1654 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 22:16:151655
1656 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1657 ASSERT_TRUE(caller_remote_cert_chain);
1658 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1659 auto remote_cert = &caller_remote_cert_chain->Get(0);
1660 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1661 remote_cert->ToPEMString());
1662
1663 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1664 ASSERT_TRUE(callee_remote_cert_chain);
1665 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1666 remote_cert = &callee_remote_cert_chain->Get(0);
1667 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1668 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 17:03:101669}
1670
deadbeef1dcb1642017-03-30 04:08:161671// This test sets up a call between two parties (using DTLS) and tests that we
1672// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 19:54:421673TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-30 04:08:161674 ASSERT_TRUE(CreatePeerConnectionWrappers());
1675 ConnectFakeSignaling();
1676
1677 // Add video tracks with 16:9 constraint.
1678 FakeConstraints constraints;
1679 double requested_ratio = 16.0 / 9;
1680 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 18:26:491681 caller()->AddTrack(
1682 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1683 callee()->AddTrack(
1684 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-30 04:08:161685
1686 // Do normal offer/answer and wait for at least one frame to be received in
1687 // each direction.
1688 caller()->CreateAndSetAndSignalOffer();
1689 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1690 callee()->min_video_frames_received_per_track() > 0,
1691 kMaxWaitForFramesMs);
1692
1693 // Check rendered aspect ratio.
1694 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1695 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1696 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1697 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1698}
1699
1700// This test sets up a call between two parties with a source resolution of
1701// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 19:54:421702TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:161703 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1704 ASSERT_TRUE(CreatePeerConnectionWrappers());
1705 ConnectFakeSignaling();
1706
1707 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1708 // instead of aspect ratio constraint.
1709 FakeConstraints constraints;
1710 constraints.SetMandatoryMinWidth(1280);
1711 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 18:26:491712 caller()->AddTrack(
1713 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1714 callee()->AddTrack(
1715 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-30 04:08:161716
1717 // Do normal offer/answer and wait for at least one frame to be received in
1718 // each direction.
1719 caller()->CreateAndSetAndSignalOffer();
1720 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1721 callee()->min_video_frames_received_per_track() > 0,
1722 kMaxWaitForFramesMs);
1723
1724 // Check rendered aspect ratio.
1725 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1726 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1727 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1728 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1729}
1730
1731// This test sets up an one-way call, with media only from caller to
1732// callee.
Seth Hampson2f0d7022018-02-20 19:54:421733TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-30 04:08:161734 ASSERT_TRUE(CreatePeerConnectionWrappers());
1735 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491736 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161737 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 19:54:421738 MediaExpectations media_expectations;
1739 media_expectations.CalleeExpectsSomeAudioAndVideo();
1740 media_expectations.CallerExpectsNoAudio();
1741 media_expectations.CallerExpectsNoVideo();
1742 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:161743}
1744
1745// This test sets up a audio call initially, with the callee rejecting video
1746// initially. Then later the callee decides to upgrade to audio/video, and
1747// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 19:54:421748TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-30 04:08:161749 ASSERT_TRUE(CreatePeerConnectionWrappers());
1750 ConnectFakeSignaling();
1751 // Initially, offer an audio/video stream from the caller, but refuse to
1752 // send/receive video on the callee side.
Steve Anton15324772018-01-16 18:26:491753 caller()->AddAudioVideoTracks();
1754 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 19:54:421755 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1756 PeerConnectionInterface::RTCOfferAnswerOptions options;
1757 options.offer_to_receive_video = 0;
1758 callee()->SetOfferAnswerOptions(options);
1759 } else {
1760 callee()->SetRemoteOfferHandler([this] {
1761 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1762 });
1763 }
deadbeef1dcb1642017-03-30 04:08:161764 // Do offer/answer and make sure audio is still received end-to-end.
1765 caller()->CreateAndSetAndSignalOffer();
1766 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:421767 {
1768 MediaExpectations media_expectations;
1769 media_expectations.ExpectBidirectionalAudio();
1770 media_expectations.ExpectNoVideo();
1771 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1772 }
deadbeef1dcb1642017-03-30 04:08:161773 // Sanity check that the callee's description has a rejected video section.
1774 ASSERT_NE(nullptr, callee()->pc()->local_description());
1775 const ContentInfo* callee_video_content =
1776 GetFirstVideoContent(callee()->pc()->local_description()->description());
1777 ASSERT_NE(nullptr, callee_video_content);
1778 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 19:54:421779
deadbeef1dcb1642017-03-30 04:08:161780 // Now negotiate with video and ensure negotiation succeeds, with video
1781 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 18:26:491782 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 19:54:421783 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1784 PeerConnectionInterface::RTCOfferAnswerOptions options;
1785 options.offer_to_receive_video = 1;
1786 callee()->SetOfferAnswerOptions(options);
1787 } else {
1788 callee()->SetRemoteOfferHandler(nullptr);
1789 caller()->SetRemoteOfferHandler([this] {
1790 // The caller creates a new transceiver to receive video on when receiving
1791 // the offer, but by default it is send only.
1792 auto transceivers = caller()->pc()->GetTransceivers();
1793 ASSERT_EQ(3, transceivers.size());
1794 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1795 transceivers[2]->receiver()->media_type());
1796 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1797 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1798 });
1799 }
deadbeef1dcb1642017-03-30 04:08:161800 callee()->CreateAndSetAndSignalOffer();
1801 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:421802 {
1803 // Expect additional audio frames to be received after the upgrade.
1804 MediaExpectations media_expectations;
1805 media_expectations.ExpectBidirectionalAudioAndVideo();
1806 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1807 }
deadbeef1dcb1642017-03-30 04:08:161808}
1809
deadbeef4389b4d2017-09-07 16:07:361810// Simpler than the above test; just add an audio track to an established
1811// video-only connection.
Seth Hampson2f0d7022018-02-20 19:54:421812TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 16:07:361813 ASSERT_TRUE(CreatePeerConnectionWrappers());
1814 ConnectFakeSignaling();
1815 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 18:26:491816 caller()->AddVideoTrack();
1817 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 16:07:361818 caller()->CreateAndSetAndSignalOffer();
1819 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1820 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 18:26:491821 caller()->AddAudioTrack();
1822 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 16:07:361823 caller()->CreateAndSetAndSignalOffer();
1824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1825 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:421826 MediaExpectations media_expectations;
1827 media_expectations.ExpectBidirectionalAudioAndVideo();
1828 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 16:07:361829}
1830
deadbeef1dcb1642017-03-30 04:08:161831// This test sets up a call that's transferred to a new caller with a different
1832// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 19:54:421833TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-30 04:08:161834 ASSERT_TRUE(CreatePeerConnectionWrappers());
1835 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491836 caller()->AddAudioVideoTracks();
1837 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161838 caller()->CreateAndSetAndSignalOffer();
1839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1840
1841 // Keep the original peer around which will still send packets to the
1842 // receiving client. These SRTP packets will be dropped.
1843 std::unique_ptr<PeerConnectionWrapper> original_peer(
1844 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 19:54:421845 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-30 04:08:161846 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1847 // directly above.
1848 original_peer->pc()->Close();
1849
1850 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491851 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161852 caller()->CreateAndSetAndSignalOffer();
1853 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1854 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:421855 MediaExpectations media_expectations;
1856 media_expectations.ExpectBidirectionalAudioAndVideo();
1857 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:161858}
1859
1860// This test sets up a call that's transferred to a new callee with a different
1861// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 19:54:421862TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-30 04:08:161863 ASSERT_TRUE(CreatePeerConnectionWrappers());
1864 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491865 caller()->AddAudioVideoTracks();
1866 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161867 caller()->CreateAndSetAndSignalOffer();
1868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1869
1870 // Keep the original peer around which will still send packets to the
1871 // receiving client. These SRTP packets will be dropped.
1872 std::unique_ptr<PeerConnectionWrapper> original_peer(
1873 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 19:54:421874 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-30 04:08:161875 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1876 // directly above.
1877 original_peer->pc()->Close();
1878
1879 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491880 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161881 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1882 caller()->CreateAndSetAndSignalOffer();
1883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1884 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:421885 MediaExpectations media_expectations;
1886 media_expectations.ExpectBidirectionalAudioAndVideo();
1887 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:161888}
1889
1890// This test sets up a non-bundled call and negotiates bundling at the same
1891// time as starting an ICE restart. When bundling is in effect in the restart,
1892// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 19:54:421893TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-30 04:08:161894 ASSERT_TRUE(CreatePeerConnectionWrappers());
1895 ConnectFakeSignaling();
1896
Steve Anton15324772018-01-16 18:26:491897 caller()->AddAudioVideoTracks();
1898 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:161899 // Remove the bundle group from the SDP received by the callee.
1900 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1901 desc->RemoveGroupByName("BUNDLE");
1902 });
1903 caller()->CreateAndSetAndSignalOffer();
1904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:421905 {
1906 MediaExpectations media_expectations;
1907 media_expectations.ExpectBidirectionalAudioAndVideo();
1908 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1909 }
deadbeef1dcb1642017-03-30 04:08:161910 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1911 callee()->SetReceivedSdpMunger(nullptr);
1912 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1913 caller()->CreateAndSetAndSignalOffer();
1914 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1915
1916 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 19:54:421917 {
1918 MediaExpectations media_expectations;
1919 media_expectations.ExpectBidirectionalAudioAndVideo();
1920 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1921 }
deadbeef1dcb1642017-03-30 04:08:161922}
1923
1924// Test CVO (Coordination of Video Orientation). If a video source is rotated
1925// and both peers support the CVO RTP header extension, the actual video frames
1926// don't need to be encoded in different resolutions, since the rotation is
1927// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 19:54:421928TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-30 04:08:161929 ASSERT_TRUE(CreatePeerConnectionWrappers());
1930 ConnectFakeSignaling();
1931 // Add rotated video tracks.
Steve Anton15324772018-01-16 18:26:491932 caller()->AddTrack(
deadbeef1dcb1642017-03-30 04:08:161933 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 18:26:491934 callee()->AddTrack(
deadbeef1dcb1642017-03-30 04:08:161935 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1936
1937 // Wait for video frames to be received by both sides.
1938 caller()->CreateAndSetAndSignalOffer();
1939 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1940 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1941 callee()->min_video_frames_received_per_track() > 0,
1942 kMaxWaitForFramesMs);
1943
1944 // Ensure that the aspect ratio is unmodified.
1945 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1946 // not just assumed.
1947 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1948 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1949 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1950 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1951 // Ensure that the CVO bits were surfaced to the renderer.
1952 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1953 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1954}
1955
1956// Test that when the CVO extension isn't supported, video is rotated the
1957// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 19:54:421958TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-30 04:08:161959 ASSERT_TRUE(CreatePeerConnectionWrappers());
1960 ConnectFakeSignaling();
1961 // Add rotated video tracks.
Steve Anton15324772018-01-16 18:26:491962 caller()->AddTrack(
deadbeef1dcb1642017-03-30 04:08:161963 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 18:26:491964 callee()->AddTrack(
deadbeef1dcb1642017-03-30 04:08:161965 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1966
1967 // Remove the CVO extension from the offered SDP.
1968 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1969 cricket::VideoContentDescription* video =
1970 GetFirstVideoContentDescription(desc);
1971 video->ClearRtpHeaderExtensions();
1972 });
1973 // Wait for video frames to be received by both sides.
1974 caller()->CreateAndSetAndSignalOffer();
1975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1976 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1977 callee()->min_video_frames_received_per_track() > 0,
1978 kMaxWaitForFramesMs);
1979
1980 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1981 // rotation.
1982 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1983 // not just assumed.
1984 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1985 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1986 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1987 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1988 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1989 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1990 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1991}
1992
deadbeef1dcb1642017-03-30 04:08:161993// Test that if the answerer rejects the audio m= section, no audio is sent or
1994// received, but video still can be.
Seth Hampson2f0d7022018-02-20 19:54:421995TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-30 04:08:161996 ASSERT_TRUE(CreatePeerConnectionWrappers());
1997 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:491998 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 19:54:421999 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2000 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2001 // it will reject the audio m= section completely.
2002 PeerConnectionInterface::RTCOfferAnswerOptions options;
2003 options.offer_to_receive_audio = 0;
2004 callee()->SetOfferAnswerOptions(options);
2005 } else {
2006 // Stopping the audio RtpTransceiver will cause the media section to be
2007 // rejected in the answer.
2008 callee()->SetRemoteOfferHandler([this] {
2009 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2010 });
2011 }
Steve Anton15324772018-01-16 18:26:492012 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-30 04:08:162013 // Do offer/answer and wait for successful end-to-end video frames.
2014 caller()->CreateAndSetAndSignalOffer();
2015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422016 MediaExpectations media_expectations;
2017 media_expectations.ExpectBidirectionalVideo();
2018 media_expectations.ExpectNoAudio();
2019 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2020
deadbeef1dcb1642017-03-30 04:08:162021 // Sanity check that the callee's description has a rejected audio section.
2022 ASSERT_NE(nullptr, callee()->pc()->local_description());
2023 const ContentInfo* callee_audio_content =
2024 GetFirstAudioContent(callee()->pc()->local_description()->description());
2025 ASSERT_NE(nullptr, callee_audio_content);
2026 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 19:54:422027 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2028 // The caller's transceiver should have stopped after receiving the answer.
2029 EXPECT_TRUE(caller()
2030 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2031 ->stopped());
2032 }
deadbeef1dcb1642017-03-30 04:08:162033}
2034
2035// Test that if the answerer rejects the video m= section, no video is sent or
2036// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 19:54:422037TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-30 04:08:162038 ASSERT_TRUE(CreatePeerConnectionWrappers());
2039 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492040 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 19:54:422041 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2042 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2043 // it will reject the video m= section completely.
2044 PeerConnectionInterface::RTCOfferAnswerOptions options;
2045 options.offer_to_receive_video = 0;
2046 callee()->SetOfferAnswerOptions(options);
2047 } else {
2048 // Stopping the video RtpTransceiver will cause the media section to be
2049 // rejected in the answer.
2050 callee()->SetRemoteOfferHandler([this] {
2051 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2052 });
2053 }
Steve Anton15324772018-01-16 18:26:492054 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-30 04:08:162055 // Do offer/answer and wait for successful end-to-end audio frames.
2056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422058 MediaExpectations media_expectations;
2059 media_expectations.ExpectBidirectionalAudio();
2060 media_expectations.ExpectNoVideo();
2061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2062
deadbeef1dcb1642017-03-30 04:08:162063 // Sanity check that the callee's description has a rejected video section.
2064 ASSERT_NE(nullptr, callee()->pc()->local_description());
2065 const ContentInfo* callee_video_content =
2066 GetFirstVideoContent(callee()->pc()->local_description()->description());
2067 ASSERT_NE(nullptr, callee_video_content);
2068 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 19:54:422069 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2070 // The caller's transceiver should have stopped after receiving the answer.
2071 EXPECT_TRUE(caller()
2072 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2073 ->stopped());
2074 }
deadbeef1dcb1642017-03-30 04:08:162075}
2076
2077// Test that if the answerer rejects both audio and video m= sections, nothing
2078// bad happens.
2079// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2080// test anything but the fact that negotiation succeeds, which doesn't mean
2081// much.
Seth Hampson2f0d7022018-02-20 19:54:422082TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-30 04:08:162083 ASSERT_TRUE(CreatePeerConnectionWrappers());
2084 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492085 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 19:54:422086 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2087 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2088 // will reject both audio and video m= sections.
2089 PeerConnectionInterface::RTCOfferAnswerOptions options;
2090 options.offer_to_receive_audio = 0;
2091 options.offer_to_receive_video = 0;
2092 callee()->SetOfferAnswerOptions(options);
2093 } else {
2094 callee()->SetRemoteOfferHandler([this] {
2095 // Stopping all transceivers will cause all media sections to be rejected.
2096 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2097 transceiver->Stop();
2098 }
2099 });
2100 }
deadbeef1dcb1642017-03-30 04:08:162101 // Do offer/answer and wait for stable signaling state.
2102 caller()->CreateAndSetAndSignalOffer();
2103 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422104
deadbeef1dcb1642017-03-30 04:08:162105 // Sanity check that the callee's description has rejected m= sections.
2106 ASSERT_NE(nullptr, callee()->pc()->local_description());
2107 const ContentInfo* callee_audio_content =
2108 GetFirstAudioContent(callee()->pc()->local_description()->description());
2109 ASSERT_NE(nullptr, callee_audio_content);
2110 EXPECT_TRUE(callee_audio_content->rejected);
2111 const ContentInfo* callee_video_content =
2112 GetFirstVideoContent(callee()->pc()->local_description()->description());
2113 ASSERT_NE(nullptr, callee_video_content);
2114 EXPECT_TRUE(callee_video_content->rejected);
2115}
2116
2117// This test sets up an audio and video call between two parties. After the
2118// call runs for a while, the caller sends an updated offer with video being
2119// rejected. Once the re-negotiation is done, the video flow should stop and
2120// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 19:54:422121TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-30 04:08:162122 ASSERT_TRUE(CreatePeerConnectionWrappers());
2123 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492124 caller()->AddAudioVideoTracks();
2125 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162126 caller()->CreateAndSetAndSignalOffer();
2127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422128 {
2129 MediaExpectations media_expectations;
2130 media_expectations.ExpectBidirectionalAudioAndVideo();
2131 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2132 }
deadbeef1dcb1642017-03-30 04:08:162133 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 19:54:422134 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2135 caller()->SetGeneratedSdpMunger(
2136 [](cricket::SessionDescription* description) {
2137 for (cricket::ContentInfo& content : description->contents()) {
2138 if (cricket::IsVideoContent(&content)) {
2139 content.rejected = true;
2140 }
2141 }
2142 });
2143 } else {
2144 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2145 }
deadbeef1dcb1642017-03-30 04:08:162146 caller()->CreateAndSetAndSignalOffer();
2147 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2148
2149 // Sanity check that the caller's description has a rejected video section.
2150 ASSERT_NE(nullptr, caller()->pc()->local_description());
2151 const ContentInfo* caller_video_content =
2152 GetFirstVideoContent(caller()->pc()->local_description()->description());
2153 ASSERT_NE(nullptr, caller_video_content);
2154 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-30 04:08:162155 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 19:54:422156 {
2157 MediaExpectations media_expectations;
2158 media_expectations.ExpectBidirectionalAudio();
2159 media_expectations.ExpectNoVideo();
2160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2161 }
deadbeef1dcb1642017-03-30 04:08:162162}
2163
2164// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2165// is needed to support legacy endpoints.
2166// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2167// add a test for an end-to-end test without MID signaling either (basically,
2168// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 19:54:422169TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-30 04:08:162170 ASSERT_TRUE(CreatePeerConnectionWrappers());
2171 ConnectFakeSignaling();
2172 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 18:26:492173 caller()->AddAudioVideoTracks();
2174 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 23:01:172175 // Remove SSRCs and MSIDs from the received offer SDP.
2176 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-30 04:08:162177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422179 MediaExpectations media_expectations;
2180 media_expectations.ExpectBidirectionalAudioAndVideo();
2181 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162182}
2183
2184// Test that if two video tracks are sent (from caller to callee, in this test),
2185// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:422186TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-30 04:08:162187 ASSERT_TRUE(CreatePeerConnectionWrappers());
2188 ConnectFakeSignaling();
2189 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 18:26:492190 caller()->AddAudioVideoTracks();
2191 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-30 04:08:162192 caller()->CreateAndSetAndSignalOffer();
2193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 18:26:492194 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 19:54:422195
2196 MediaExpectations media_expectations;
2197 media_expectations.CalleeExpectsSomeAudioAndVideo();
2198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162199}
2200
2201static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2202 bool first = true;
2203 for (cricket::ContentInfo& content : desc->contents()) {
2204 if (first) {
2205 first = false;
2206 continue;
2207 }
2208 content.bundle_only = true;
2209 }
2210 first = true;
2211 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2212 if (first) {
2213 first = false;
2214 continue;
2215 }
2216 transport.description.ice_ufrag.clear();
2217 transport.description.ice_pwd.clear();
2218 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2219 transport.description.identity_fingerprint.reset(nullptr);
2220 }
2221}
2222
2223// Test that if applying a true "max bundle" offer, which uses ports of 0,
2224// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2225// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2226// successfully and media flows.
2227// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2228// TODO(deadbeef): Won't need this test once we start generating actual
2229// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 19:54:422230TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:162231 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2232 ASSERT_TRUE(CreatePeerConnectionWrappers());
2233 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492234 caller()->AddAudioVideoTracks();
2235 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162236 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2237 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2238 // but the first m= section.
2239 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2240 caller()->CreateAndSetAndSignalOffer();
2241 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422242 MediaExpectations media_expectations;
2243 media_expectations.ExpectBidirectionalAudioAndVideo();
2244 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162245}
2246
2247// Test that we can receive the audio output level from a remote audio track.
2248// TODO(deadbeef): Use a fake audio source and verify that the output level is
2249// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 19:54:422250TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-30 04:08:162251 ASSERT_TRUE(CreatePeerConnectionWrappers());
2252 ConnectFakeSignaling();
2253 // Just add an audio track.
Steve Anton15324772018-01-16 18:26:492254 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-30 04:08:162255 caller()->CreateAndSetAndSignalOffer();
2256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2257
2258 // Get the audio output level stats. Note that the level is not available
2259 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 23:01:172260 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-30 04:08:162261 kMaxWaitForFramesMs);
2262}
2263
2264// Test that an audio input level is reported.
2265// TODO(deadbeef): Use a fake audio source and verify that the input level is
2266// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 19:54:422267TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-30 04:08:162268 ASSERT_TRUE(CreatePeerConnectionWrappers());
2269 ConnectFakeSignaling();
2270 // Just add an audio track.
Steve Anton15324772018-01-16 18:26:492271 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-30 04:08:162272 caller()->CreateAndSetAndSignalOffer();
2273 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2274
2275 // Get the audio input level stats. The level should be available very
2276 // soon after the test starts.
deadbeefd8ad7882017-04-18 23:01:172277 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-30 04:08:162278 kMaxWaitForStatsMs);
2279}
2280
2281// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 19:54:422282TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-30 04:08:162283 ASSERT_TRUE(CreatePeerConnectionWrappers());
2284 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492285 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162286 // Do offer/answer, wait for the callee to receive some frames.
2287 caller()->CreateAndSetAndSignalOffer();
2288 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422289
2290 MediaExpectations media_expectations;
2291 media_expectations.CalleeExpectsSomeAudioAndVideo();
2292 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162293
2294 // Get a handle to the remote tracks created, so they can be used as GetStats
2295 // filters.
Steve Anton15324772018-01-16 18:26:492296 for (auto receiver : callee()->pc()->GetReceivers()) {
2297 // We received frames, so we definitely should have nonzero "received bytes"
2298 // stats at this point.
2299 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2300 0);
2301 }
deadbeef1dcb1642017-03-30 04:08:162302}
2303
2304// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 19:54:422305TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-30 04:08:162306 ASSERT_TRUE(CreatePeerConnectionWrappers());
2307 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492308 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162309 auto audio_track = caller()->CreateLocalAudioTrack();
2310 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 18:26:492311 caller()->AddTrack(audio_track);
2312 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-30 04:08:162313 // Do offer/answer, wait for the callee to receive some frames.
2314 caller()->CreateAndSetAndSignalOffer();
2315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422316 MediaExpectations media_expectations;
2317 media_expectations.CalleeExpectsSomeAudioAndVideo();
2318 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162319
2320 // The callee received frames, so we definitely should have nonzero "sent
2321 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 23:01:172322 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2323 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2324}
2325
Fredrik Solenberg73276ad2017-09-14 12:46:472326// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 19:54:422327TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 12:46:472328 ASSERT_TRUE(CreatePeerConnectionWrappers());
2329 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492330 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 12:46:472331
Steve Anton15324772018-01-16 18:26:492332 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 12:46:472333
2334 // Do offer/answer, wait for the callee to receive some frames.
2335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2337
2338 // Get the remote audio track created on the receiver, so they can be used as
2339 // GetStats filters.
Steve Antonfc853712018-03-01 21:48:582340 auto receivers = callee()->pc()->GetReceivers();
2341 ASSERT_EQ(1u, receivers.size());
2342 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 12:46:472343
2344 // Get the audio output level stats. Note that the level is not available
2345 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 17:48:352346 EXPECT_TRUE_WAIT(
2347 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2348 0,
2349 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 12:46:472350}
2351
deadbeefd8ad7882017-04-18 23:01:172352// Test that we can get stats (using the new stats implemnetation) for
2353// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2354// SDP.
Seth Hampson2f0d7022018-02-20 19:54:422355TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 23:01:172356 GetStatsForUnsignaledStreamWithNewStatsApi) {
2357 ASSERT_TRUE(CreatePeerConnectionWrappers());
2358 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492359 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 23:01:172360 // Remove SSRCs and MSIDs from the received offer SDP.
2361 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422364 MediaExpectations media_expectations;
2365 media_expectations.CalleeExpectsSomeAudio(1);
2366 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 23:01:172367
2368 // We received a frame, so we should have nonzero "bytes received" stats for
2369 // the unsignaled stream, if stats are working for it.
2370 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2371 callee()->NewGetStats();
2372 ASSERT_NE(nullptr, report);
2373 auto inbound_stream_stats =
2374 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2375 ASSERT_EQ(1U, inbound_stream_stats.size());
2376 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2377 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 20:09:472378 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2379}
2380
2381// Test that we can successfully get the media related stats (audio level
2382// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 19:54:422383TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 20:09:472384 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2385 ASSERT_TRUE(CreatePeerConnectionWrappers());
2386 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492387 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 20:09:472388 // Remove SSRCs and MSIDs from the received offer SDP.
2389 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2390 caller()->CreateAndSetAndSignalOffer();
2391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422392 MediaExpectations media_expectations;
2393 media_expectations.CalleeExpectsSomeAudio(1);
2394 media_expectations.CalleeExpectsSomeVideo(1);
2395 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 20:09:472396
2397 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2398 callee()->NewGetStats();
2399 ASSERT_NE(nullptr, report);
2400
2401 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2402 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2403 ASSERT_GE(audio_index, 0);
2404 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-30 04:08:162405}
2406
deadbeef4e2deab2017-09-20 20:56:212407// Helper for test below.
2408void ModifySsrcs(cricket::SessionDescription* desc) {
2409 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 23:14:302410 for (cricket::StreamParams& stream :
2411 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 20:56:212412 for (uint32_t& ssrc : stream.ssrcs) {
2413 ssrc = rtc::CreateRandomId();
2414 }
2415 }
2416 }
2417}
2418
2419// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2420// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2421// This should result in two "RTCInboundRTPStreamStats", but only one
2422// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2423// being reset to 0 once the SSRC change occurs.
2424//
2425// Regression test for this bug:
2426// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2427//
2428// The bug causes the track stats to only represent one of the two streams:
2429// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2430// that the track stat counters would reset to 0 when the new stream is
2431// received, and a 50% chance that they'll stop updating (while
2432// "concealed_samples" continues increasing, due to silence being generated for
2433// the inactive stream).
Seth Hampson2f0d7022018-02-20 19:54:422434TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-11 00:19:522435 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 20:56:212436 ASSERT_TRUE(CreatePeerConnectionWrappers());
2437 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:492438 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 20:56:212439 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2440 // that doesn't signal SSRCs (from the callee's perspective).
2441 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2442 caller()->CreateAndSetAndSignalOffer();
2443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2444 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 19:54:422445 {
2446 MediaExpectations media_expectations;
2447 media_expectations.CalleeExpectsSomeAudio(50);
2448 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2449 }
deadbeef4e2deab2017-09-20 20:56:212450 // Some audio frames were received, so we should have nonzero "samples
2451 // received" for the track.
2452 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2453 callee()->NewGetStats();
2454 ASSERT_NE(nullptr, report);
2455 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2456 ASSERT_EQ(1U, track_stats.size());
2457 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2458 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2459 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2460
2461 // Create a new offer and munge it to cause the caller to use a new SSRC.
2462 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2463 caller()->CreateAndSetAndSignalOffer();
2464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2465 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2466 // SSRC.
Seth Hampson2f0d7022018-02-20 19:54:422467 {
2468 MediaExpectations media_expectations;
2469 media_expectations.CalleeExpectsSomeAudio(25);
2470 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2471 }
deadbeef4e2deab2017-09-20 20:56:212472
2473 report = callee()->NewGetStats();
2474 ASSERT_NE(nullptr, report);
2475 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2476 ASSERT_EQ(1U, track_stats.size());
2477 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2478 // The "total samples received" stat should only be greater than it was
2479 // before.
2480 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2481 // Right now, the new SSRC will cause the counters to reset to 0.
2482 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2483
2484 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-11 00:19:522485 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 20:56:212486 // good sign that we're seeing stats from the old stream that's no longer
2487 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-11 00:19:522488 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 20:56:212489 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2490 EXPECT_LT(*track_stats[0]->concealed_samples,
2491 *track_stats[0]->total_samples_received *
2492 kAcceptableConcealedSamplesPercentage);
2493
2494 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2495 // sanity check that the SSRC really changed.
2496 // TODO(deadbeef): This isn't working right now, because we're not returning
2497 // *any* stats for the inactive stream. Uncomment when the bug is completely
2498 // fixed.
2499 // auto inbound_stream_stats =
2500 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2501 // ASSERT_EQ(2U, inbound_stream_stats.size());
2502}
2503
deadbeef1dcb1642017-03-30 04:08:162504// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 19:54:422505TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-30 04:08:162506 PeerConnectionFactory::Options dtls_10_options;
2507 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2508 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2509 dtls_10_options));
2510 ConnectFakeSignaling();
2511 // Do normal offer/answer and wait for some frames to be received in each
2512 // direction.
Steve Anton15324772018-01-16 18:26:492513 caller()->AddAudioVideoTracks();
2514 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162515 caller()->CreateAndSetAndSignalOffer();
2516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422517 MediaExpectations media_expectations;
2518 media_expectations.ExpectBidirectionalAudioAndVideo();
2519 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162520}
2521
2522// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 19:54:422523TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-30 04:08:162524 PeerConnectionFactory::Options dtls_10_options;
2525 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2526 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2527 dtls_10_options));
2528 ConnectFakeSignaling();
2529 // Register UMA observer before signaling begins.
2530 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2531 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2532 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 18:26:492533 caller()->AddAudioVideoTracks();
2534 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162535 caller()->CreateAndSetAndSignalOffer();
2536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2537 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 23:01:172538 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-30 04:08:162539 kDefaultTimeout);
2540 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 23:01:172541 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-30 04:08:162542 EXPECT_EQ(1,
2543 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2544 kDefaultSrtpCryptoSuite));
2545}
2546
2547// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 19:54:422548TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-30 04:08:162549 PeerConnectionFactory::Options dtls_12_options;
2550 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2551 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2552 dtls_12_options));
2553 ConnectFakeSignaling();
2554 // Register UMA observer before signaling begins.
2555 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2556 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2557 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 18:26:492558 caller()->AddAudioVideoTracks();
2559 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162560 caller()->CreateAndSetAndSignalOffer();
2561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2562 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 23:01:172563 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-30 04:08:162564 kDefaultTimeout);
2565 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 23:01:172566 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-30 04:08:162567 EXPECT_EQ(1,
2568 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2569 kDefaultSrtpCryptoSuite));
2570}
2571
2572// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2573// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 19:54:422574TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-30 04:08:162575 PeerConnectionFactory::Options caller_options;
2576 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2577 PeerConnectionFactory::Options callee_options;
2578 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2579 ASSERT_TRUE(
2580 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2581 ConnectFakeSignaling();
2582 // Do normal offer/answer and wait for some frames to be received in each
2583 // direction.
Steve Anton15324772018-01-16 18:26:492584 caller()->AddAudioVideoTracks();
2585 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162586 caller()->CreateAndSetAndSignalOffer();
2587 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422588 MediaExpectations media_expectations;
2589 media_expectations.ExpectBidirectionalAudioAndVideo();
2590 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162591}
2592
2593// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2594// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 19:54:422595TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-30 04:08:162596 PeerConnectionFactory::Options caller_options;
2597 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2598 PeerConnectionFactory::Options callee_options;
2599 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2600 ASSERT_TRUE(
2601 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2602 ConnectFakeSignaling();
2603 // Do normal offer/answer and wait for some frames to be received in each
2604 // direction.
Steve Anton15324772018-01-16 18:26:492605 caller()->AddAudioVideoTracks();
2606 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162607 caller()->CreateAndSetAndSignalOffer();
2608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422609 MediaExpectations media_expectations;
2610 media_expectations.ExpectBidirectionalAudioAndVideo();
2611 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162612}
2613
Taylor Brandstetter5e55fe82018-03-23 18:50:162614// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2615// works as expected; the cipher should only be used if enabled by both sides.
2616TEST_P(PeerConnectionIntegrationTest,
2617 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2618 PeerConnectionFactory::Options caller_options;
2619 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2620 PeerConnectionFactory::Options callee_options;
2621 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2622 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2623 TestNegotiatedCipherSuite(caller_options, callee_options,
2624 expected_cipher_suite);
2625}
2626
2627TEST_P(PeerConnectionIntegrationTest,
2628 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2629 PeerConnectionFactory::Options caller_options;
2630 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2631 PeerConnectionFactory::Options callee_options;
2632 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2633 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2634 TestNegotiatedCipherSuite(caller_options, callee_options,
2635 expected_cipher_suite);
2636}
2637
2638TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2639 PeerConnectionFactory::Options caller_options;
2640 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2641 PeerConnectionFactory::Options callee_options;
2642 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2643 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2644 TestNegotiatedCipherSuite(caller_options, callee_options,
2645 expected_cipher_suite);
2646}
2647
deadbeef1dcb1642017-03-30 04:08:162648// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 19:54:422649TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-30 04:08:162650 bool local_gcm_enabled = false;
2651 bool remote_gcm_enabled = false;
2652 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2653 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2654 expected_cipher_suite);
2655}
2656
2657// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 19:54:422658TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-30 04:08:162659 bool local_gcm_enabled = true;
2660 bool remote_gcm_enabled = true;
2661 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2662 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2663 expected_cipher_suite);
2664}
2665
2666// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 19:54:422667TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:162668 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2669 bool local_gcm_enabled = true;
2670 bool remote_gcm_enabled = false;
2671 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2672 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2673 expected_cipher_suite);
2674}
2675
2676// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 19:54:422677TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:162678 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2679 bool local_gcm_enabled = false;
2680 bool remote_gcm_enabled = true;
2681 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2682 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2683 expected_cipher_suite);
2684}
2685
deadbeef7914b8c2017-04-21 10:23:332686// Verify that media can be transmitted end-to-end when GCM crypto suites are
2687// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2688// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2689// works with it.
Seth Hampson2f0d7022018-02-20 19:54:422690TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 10:23:332691 PeerConnectionFactory::Options gcm_options;
2692 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2693 ASSERT_TRUE(
2694 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2695 ConnectFakeSignaling();
2696 // Do normal offer/answer and wait for some frames to be received in each
2697 // direction.
Steve Anton15324772018-01-16 18:26:492698 caller()->AddAudioVideoTracks();
2699 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 10:23:332700 caller()->CreateAndSetAndSignalOffer();
2701 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:422702 MediaExpectations media_expectations;
2703 media_expectations.ExpectBidirectionalAudioAndVideo();
2704 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 10:23:332705}
2706
deadbeef1dcb1642017-03-30 04:08:162707// This test sets up a call between two parties with audio, video and an RTP
2708// data channel.
Seth Hampson2f0d7022018-02-20 19:54:422709TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-30 04:08:162710 FakeConstraints setup_constraints;
2711 setup_constraints.SetAllowRtpDataChannels();
2712 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2713 &setup_constraints));
2714 ConnectFakeSignaling();
2715 // Expect that data channel created on caller side will show up for callee as
2716 // well.
2717 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 18:26:492718 caller()->AddAudioVideoTracks();
2719 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162720 caller()->CreateAndSetAndSignalOffer();
2721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2722 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 19:54:422723 MediaExpectations media_expectations;
2724 media_expectations.ExpectBidirectionalAudioAndVideo();
2725 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162726 ASSERT_NE(nullptr, caller()->data_channel());
2727 ASSERT_NE(nullptr, callee()->data_channel());
2728 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2729 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2730
2731 // Ensure data can be sent in both directions.
2732 std::string data = "hello world";
2733 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2734 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2735 kDefaultTimeout);
2736 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2737 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2738 kDefaultTimeout);
2739}
2740
2741// Ensure that an RTP data channel is signaled as closed for the caller when
2742// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 19:54:422743TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:162744 RtpDataChannelSignaledClosedInCalleeOffer) {
2745 // Same procedure as above test.
2746 FakeConstraints setup_constraints;
2747 setup_constraints.SetAllowRtpDataChannels();
2748 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2749 &setup_constraints));
2750 ConnectFakeSignaling();
2751 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 18:26:492752 caller()->AddAudioVideoTracks();
2753 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162754 caller()->CreateAndSetAndSignalOffer();
2755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2756 ASSERT_NE(nullptr, caller()->data_channel());
2757 ASSERT_NE(nullptr, callee()->data_channel());
2758 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2759 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2760
2761 // Close the data channel on the callee, and do an updated offer/answer.
2762 callee()->data_channel()->Close();
2763 callee()->CreateAndSetAndSignalOffer();
2764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2765 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2766 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2767}
2768
2769// Tests that data is buffered in an RTP data channel until an observer is
2770// registered for it.
2771//
2772// NOTE: RTP data channels can receive data before the underlying
2773// transport has detected that a channel is writable and thus data can be
2774// received before the data channel state changes to open. That is hard to test
2775// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 19:54:422776TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:162777 DataBufferedUntilRtpDataChannelObserverRegistered) {
2778 // Use fake clock and simulated network delay so that we predictably can wait
2779 // until an SCTP message has been delivered without "sleep()"ing.
2780 rtc::ScopedFakeClock fake_clock;
2781 // Some things use a time of "0" as a special value, so we need to start out
2782 // the fake clock at a nonzero time.
2783 // TODO(deadbeef): Fix this.
2784 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2785 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2786 virtual_socket_server()->UpdateDelayDistribution();
2787
2788 FakeConstraints constraints;
2789 constraints.SetAllowRtpDataChannels();
2790 ASSERT_TRUE(
2791 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2792 ConnectFakeSignaling();
2793 caller()->CreateDataChannel();
2794 caller()->CreateAndSetAndSignalOffer();
2795 ASSERT_TRUE(caller()->data_channel() != nullptr);
2796 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2797 kDefaultTimeout, fake_clock);
2798 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2799 kDefaultTimeout, fake_clock);
2800 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2801 callee()->data_channel()->state(), kDefaultTimeout,
2802 fake_clock);
2803
2804 // Unregister the observer which is normally automatically registered.
2805 callee()->data_channel()->UnregisterObserver();
2806 // Send data and advance fake clock until it should have been received.
2807 std::string data = "hello world";
2808 caller()->data_channel()->Send(DataBuffer(data));
2809 SIMULATED_WAIT(false, 50, fake_clock);
2810
2811 // Attach data channel and expect data to be received immediately. Note that
2812 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2813 // further, but data can be received even if the callback is asynchronous.
2814 MockDataChannelObserver new_observer(callee()->data_channel());
2815 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2816 fake_clock);
2817}
2818
2819// This test sets up a call between two parties with audio, video and but only
2820// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 19:54:422821TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-30 04:08:162822 FakeConstraints setup_constraints_1;
2823 setup_constraints_1.SetAllowRtpDataChannels();
2824 // Must disable DTLS to make negotiation succeed.
2825 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2826 false);
2827 FakeConstraints setup_constraints_2;
2828 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2829 false);
2830 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2831 &setup_constraints_1, &setup_constraints_2));
2832 ConnectFakeSignaling();
2833 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 18:26:492834 caller()->AddAudioVideoTracks();
2835 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162836 caller()->CreateAndSetAndSignalOffer();
2837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2838 // The caller should still have a data channel, but it should be closed, and
2839 // one should ever have been created for the callee.
2840 EXPECT_TRUE(caller()->data_channel() != nullptr);
2841 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2842 EXPECT_EQ(nullptr, callee()->data_channel());
2843}
2844
2845// This test sets up a call between two parties with audio, and video. When
2846// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 19:54:422847TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-30 04:08:162848 FakeConstraints setup_constraints;
2849 setup_constraints.SetAllowRtpDataChannels();
2850 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2851 &setup_constraints));
2852 ConnectFakeSignaling();
2853 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 18:26:492854 caller()->AddAudioVideoTracks();
2855 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162856 caller()->CreateAndSetAndSignalOffer();
2857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2858 // Create data channel and do new offer and answer.
2859 caller()->CreateDataChannel();
2860 caller()->CreateAndSetAndSignalOffer();
2861 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2862 ASSERT_NE(nullptr, caller()->data_channel());
2863 ASSERT_NE(nullptr, callee()->data_channel());
2864 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2865 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2866 // Ensure data can be sent in both directions.
2867 std::string data = "hello world";
2868 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2869 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2870 kDefaultTimeout);
2871 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2872 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2873 kDefaultTimeout);
2874}
2875
2876#ifdef HAVE_SCTP
2877
2878// This test sets up a call between two parties with audio, video and an SCTP
2879// data channel.
Seth Hampson2f0d7022018-02-20 19:54:422880TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-30 04:08:162881 ASSERT_TRUE(CreatePeerConnectionWrappers());
2882 ConnectFakeSignaling();
2883 // Expect that data channel created on caller side will show up for callee as
2884 // well.
2885 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 18:26:492886 caller()->AddAudioVideoTracks();
2887 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162888 caller()->CreateAndSetAndSignalOffer();
2889 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2890 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 19:54:422891 MediaExpectations media_expectations;
2892 media_expectations.ExpectBidirectionalAudioAndVideo();
2893 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:162894 // Caller data channel should already exist (it created one). Callee data
2895 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2896 ASSERT_NE(nullptr, caller()->data_channel());
2897 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2898 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2899 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2900
2901 // Ensure data can be sent in both directions.
2902 std::string data = "hello world";
2903 caller()->data_channel()->Send(DataBuffer(data));
2904 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2905 kDefaultTimeout);
2906 callee()->data_channel()->Send(DataBuffer(data));
2907 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2908 kDefaultTimeout);
2909}
2910
2911// Ensure that when the callee closes an SCTP data channel, the closing
2912// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 19:54:422913TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-30 04:08:162914 // Same procedure as above test.
2915 ASSERT_TRUE(CreatePeerConnectionWrappers());
2916 ConnectFakeSignaling();
2917 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 18:26:492918 caller()->AddAudioVideoTracks();
2919 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:162920 caller()->CreateAndSetAndSignalOffer();
2921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2922 ASSERT_NE(nullptr, caller()->data_channel());
2923 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2924 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2925 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2926
2927 // Close the data channel on the callee side, and wait for it to reach the
2928 // "closed" state on both sides.
2929 callee()->data_channel()->Close();
2930 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2931 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2932}
2933
Seth Hampson2f0d7022018-02-20 19:54:422934TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 18:41:542935 ASSERT_TRUE(CreatePeerConnectionWrappers());
2936 ConnectFakeSignaling();
2937 webrtc::DataChannelInit init;
2938 init.id = 53;
2939 init.maxRetransmits = 52;
2940 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 18:26:492941 caller()->AddAudioVideoTracks();
2942 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 18:41:542943 caller()->CreateAndSetAndSignalOffer();
2944 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 20:04:122945 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2946 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 18:41:542947 EXPECT_EQ(init.id, callee()->data_channel()->id());
2948 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2949 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2950 EXPECT_FALSE(callee()->data_channel()->negotiated());
2951}
2952
deadbeef1dcb1642017-03-30 04:08:162953// Test usrsctp's ability to process unordered data stream, where data actually
2954// arrives out of order using simulated delays. Previously there have been some
2955// bugs in this area.
Seth Hampson2f0d7022018-02-20 19:54:422956TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-30 04:08:162957 // Introduce random network delays.
2958 // Otherwise it's not a true "unordered" test.
2959 virtual_socket_server()->set_delay_mean(20);
2960 virtual_socket_server()->set_delay_stddev(5);
2961 virtual_socket_server()->UpdateDelayDistribution();
2962 // Normal procedure, but with unordered data channel config.
2963 ASSERT_TRUE(CreatePeerConnectionWrappers());
2964 ConnectFakeSignaling();
2965 webrtc::DataChannelInit init;
2966 init.ordered = false;
2967 caller()->CreateDataChannel(&init);
2968 caller()->CreateAndSetAndSignalOffer();
2969 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2970 ASSERT_NE(nullptr, caller()->data_channel());
2971 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2972 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2973 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2974
2975 static constexpr int kNumMessages = 100;
2976 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2977 static constexpr size_t kMaxMessageSize = 4096;
2978 // Create and send random messages.
2979 std::vector<std::string> sent_messages;
2980 for (int i = 0; i < kNumMessages; ++i) {
2981 size_t length =
2982 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2983 std::string message;
2984 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2985 caller()->data_channel()->Send(DataBuffer(message));
2986 callee()->data_channel()->Send(DataBuffer(message));
2987 sent_messages.push_back(message);
2988 }
2989
2990 // Wait for all messages to be received.
2991 EXPECT_EQ_WAIT(kNumMessages,
2992 caller()->data_observer()->received_message_count(),
2993 kDefaultTimeout);
2994 EXPECT_EQ_WAIT(kNumMessages,
2995 callee()->data_observer()->received_message_count(),
2996 kDefaultTimeout);
2997
2998 // Sort and compare to make sure none of the messages were corrupted.
2999 std::vector<std::string> caller_received_messages =
3000 caller()->data_observer()->messages();
3001 std::vector<std::string> callee_received_messages =
3002 callee()->data_observer()->messages();
3003 std::sort(sent_messages.begin(), sent_messages.end());
3004 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3005 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3006 EXPECT_EQ(sent_messages, caller_received_messages);
3007 EXPECT_EQ(sent_messages, callee_received_messages);
3008}
3009
3010// This test sets up a call between two parties with audio, and video. When
3011// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 19:54:423012TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-30 04:08:163013 ASSERT_TRUE(CreatePeerConnectionWrappers());
3014 ConnectFakeSignaling();
3015 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 18:26:493016 caller()->AddAudioVideoTracks();
3017 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163018 caller()->CreateAndSetAndSignalOffer();
3019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3020 // Create data channel and do new offer and answer.
3021 caller()->CreateDataChannel();
3022 caller()->CreateAndSetAndSignalOffer();
3023 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3024 // Caller data channel should already exist (it created one). Callee data
3025 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3026 ASSERT_NE(nullptr, caller()->data_channel());
3027 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3028 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3029 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3030 // Ensure data can be sent in both directions.
3031 std::string data = "hello world";
3032 caller()->data_channel()->Send(DataBuffer(data));
3033 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3034 kDefaultTimeout);
3035 callee()->data_channel()->Send(DataBuffer(data));
3036 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3037 kDefaultTimeout);
3038}
3039
deadbeef7914b8c2017-04-21 10:23:333040// Set up a connection initially just using SCTP data channels, later upgrading
3041// to audio/video, ensuring frames are received end-to-end. Effectively the
3042// inverse of the test above.
3043// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 19:54:423044TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 10:23:333045 ASSERT_TRUE(CreatePeerConnectionWrappers());
3046 ConnectFakeSignaling();
3047 // Do initial offer/answer with just data channel.
3048 caller()->CreateDataChannel();
3049 caller()->CreateAndSetAndSignalOffer();
3050 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3051 // Wait until data can be sent over the data channel.
3052 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3053 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3054 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3055
3056 // Do subsequent offer/answer with two-way audio and video. Audio and video
3057 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 18:26:493058 caller()->AddAudioVideoTracks();
3059 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 10:23:333060 caller()->CreateAndSetAndSignalOffer();
3061 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:423062 MediaExpectations media_expectations;
3063 media_expectations.ExpectBidirectionalAudioAndVideo();
3064 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 10:23:333065}
3066
deadbeef8b7e9ad2017-05-25 16:38:553067static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 16:38:553068 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 23:14:303069 GetFirstDataContentDescription(desc);
3070 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 16:38:553071 dcd_offer->set_use_sctpmap(false);
3072 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3073}
3074
3075// Test that the data channel works when a spec-compliant SCTP m= section is
3076// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3077// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 19:54:423078TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 16:38:553079 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3080 ASSERT_TRUE(CreatePeerConnectionWrappers());
3081 ConnectFakeSignaling();
3082 caller()->CreateDataChannel();
3083 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3087 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3088 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3089
3090 // Ensure data can be sent in both directions.
3091 std::string data = "hello world";
3092 caller()->data_channel()->Send(DataBuffer(data));
3093 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3094 kDefaultTimeout);
3095 callee()->data_channel()->Send(DataBuffer(data));
3096 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3097 kDefaultTimeout);
3098}
3099
deadbeef1dcb1642017-03-30 04:08:163100#endif // HAVE_SCTP
3101
3102// Test that the ICE connection and gathering states eventually reach
3103// "complete".
Seth Hampson2f0d7022018-02-20 19:54:423104TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-30 04:08:163105 ASSERT_TRUE(CreatePeerConnectionWrappers());
3106 ConnectFakeSignaling();
3107 // Do normal offer/answer.
Steve Anton15324772018-01-16 18:26:493108 caller()->AddAudioVideoTracks();
3109 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163110 caller()->CreateAndSetAndSignalOffer();
3111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3112 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3113 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3114 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3115 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3116 // After the best candidate pair is selected and all candidates are signaled,
3117 // the ICE connection state should reach "complete".
3118 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3119 // answerer/"callee" by default) only reaches "connected". When this is
3120 // fixed, this test should be updated.
3121 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3122 caller()->ice_connection_state(), kDefaultTimeout);
3123 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3124 callee()->ice_connection_state(), kDefaultTimeout);
3125}
3126
Steve Antonede9ca52017-10-16 20:04:273127// Test that firewalling the ICE connection causes the clients to identify the
3128// disconnected state and then removing the firewall causes them to reconnect.
3129class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 19:54:423130 : public PeerConnectionIntegrationBaseTest,
3131 public ::testing::WithParamInterface<
3132 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 20:04:273133 protected:
Seth Hampson2f0d7022018-02-20 19:54:423134 PeerConnectionIntegrationIceStatesTest()
3135 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3136 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 20:04:273137 }
3138
3139 void StartStunServer(const SocketAddress& server_address) {
3140 stun_server_.reset(
3141 cricket::TestStunServer::Create(network_thread(), server_address));
3142 }
3143
3144 bool TestIPv6() {
3145 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3146 }
3147
3148 void SetPortAllocatorFlags() {
3149 caller()->port_allocator()->set_flags(port_allocator_flags_);
3150 callee()->port_allocator()->set_flags(port_allocator_flags_);
3151 }
3152
3153 std::vector<SocketAddress> CallerAddresses() {
3154 std::vector<SocketAddress> addresses;
3155 addresses.push_back(SocketAddress("1.1.1.1", 0));
3156 if (TestIPv6()) {
3157 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3158 }
3159 return addresses;
3160 }
3161
3162 std::vector<SocketAddress> CalleeAddresses() {
3163 std::vector<SocketAddress> addresses;
3164 addresses.push_back(SocketAddress("2.2.2.2", 0));
3165 if (TestIPv6()) {
3166 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3167 }
3168 return addresses;
3169 }
3170
3171 void SetUpNetworkInterfaces() {
3172 // Remove the default interfaces added by the test infrastructure.
3173 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3174 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3175
3176 // Add network addresses for test.
3177 for (const auto& caller_address : CallerAddresses()) {
3178 caller()->network()->AddInterface(caller_address);
3179 }
3180 for (const auto& callee_address : CalleeAddresses()) {
3181 callee()->network()->AddInterface(callee_address);
3182 }
3183 }
3184
3185 private:
3186 uint32_t port_allocator_flags_;
3187 std::unique_ptr<cricket::TestStunServer> stun_server_;
3188};
3189
3190// Tests that the PeerConnection goes through all the ICE gathering/connection
3191// states over the duration of the call. This includes Disconnected and Failed
3192// states, induced by putting a firewall between the peers and waiting for them
3193// to time out.
Steve Anton83119dd2017-11-11 00:19:523194TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3195 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3196 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3197 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 20:04:273198
3199 const SocketAddress kStunServerAddress =
3200 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3201 StartStunServer(kStunServerAddress);
3202
3203 PeerConnectionInterface::RTCConfiguration config;
3204 PeerConnectionInterface::IceServer ice_stun_server;
3205 ice_stun_server.urls.push_back(
3206 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3207 kStunServerAddress.PortAsString());
3208 config.servers.push_back(ice_stun_server);
3209
3210 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3211 ConnectFakeSignaling();
3212 SetPortAllocatorFlags();
3213 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 18:26:493214 caller()->AddAudioVideoTracks();
3215 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 20:04:273216
3217 // Initial state before anything happens.
3218 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3219 caller()->ice_gathering_state());
3220 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3221 caller()->ice_connection_state());
3222
3223 // Start the call by creating the offer, setting it as the local description,
3224 // then sending it to the peer who will respond with an answer. This happens
3225 // asynchronously so that we can watch the states as it runs in the
3226 // background.
3227 caller()->CreateAndSetAndSignalOffer();
3228
Steve Anton83119dd2017-11-11 00:19:523229 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3230 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 20:04:273231
3232 // Verify that the observer was notified of the intermediate transitions.
3233 EXPECT_THAT(caller()->ice_connection_state_history(),
3234 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3235 PeerConnectionInterface::kIceConnectionConnected,
3236 PeerConnectionInterface::kIceConnectionCompleted));
3237 EXPECT_THAT(caller()->ice_gathering_state_history(),
3238 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3239 PeerConnectionInterface::kIceGatheringComplete));
3240
3241 // Block connections to/from the caller and wait for ICE to become
3242 // disconnected.
3243 for (const auto& caller_address : CallerAddresses()) {
3244 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3245 }
Mirko Bonadei675513b2017-11-09 10:09:253246 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-11 00:19:523247 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3248 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 20:04:273249
3250 // Let ICE re-establish by removing the firewall rules.
3251 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 10:09:253252 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-11 00:19:523253 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3254 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 20:04:273255
3256 // According to RFC7675, if there is no response within 30 seconds then the
3257 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-11 00:19:523258 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 20:04:273259 constexpr int kConsentTimeout = 30000;
3260 for (const auto& caller_address : CallerAddresses()) {
3261 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3262 }
Mirko Bonadei675513b2017-11-09 10:09:253263 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-11 00:19:523264 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3265 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 20:04:273266}
3267
3268// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3269// and that the statistics in the metric observers are updated correctly.
3270TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3271 ASSERT_TRUE(CreatePeerConnectionWrappers());
3272 ConnectFakeSignaling();
3273 SetPortAllocatorFlags();
3274 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 18:26:493275 caller()->AddAudioVideoTracks();
3276 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 20:04:273277
3278 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3279 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3280 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3281
3282 caller()->CreateAndSetAndSignalOffer();
3283
3284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3285
3286 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3287 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3288 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3289 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3290 if (TestIPv6()) {
3291 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3292 // connection.
3293 EXPECT_EQ(0u, num_best_ipv4);
3294 EXPECT_EQ(1u, num_best_ipv6);
3295 } else {
3296 EXPECT_EQ(1u, num_best_ipv4);
3297 EXPECT_EQ(0u, num_best_ipv6);
3298 }
3299
3300 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3301 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3302 webrtc::kIceCandidatePairHostHost));
3303 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3304 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3305 webrtc::kIceCandidatePairHostPublicHostPublic));
3306}
3307
3308constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3309 cricket::PORTALLOCATOR_DISABLE_STUN |
3310 cricket::PORTALLOCATOR_DISABLE_RELAY;
3311constexpr uint32_t kFlagsIPv6NoStun =
3312 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3313 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3314constexpr uint32_t kFlagsIPv4Stun =
3315 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3316
Seth Hampson2f0d7022018-02-20 19:54:423317INSTANTIATE_TEST_CASE_P(
3318 PeerConnectionIntegrationTest,
3319 PeerConnectionIntegrationIceStatesTest,
3320 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3321 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3322 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3323 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 20:04:273324
deadbeef1dcb1642017-03-30 04:08:163325// This test sets up a call between two parties with audio and video.
3326// During the call, the caller restarts ICE and the test verifies that
3327// new ICE candidates are generated and audio and video still can flow, and the
3328// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 19:54:423329TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-30 04:08:163330 ASSERT_TRUE(CreatePeerConnectionWrappers());
3331 ConnectFakeSignaling();
3332 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 18:26:493333 caller()->AddAudioVideoTracks();
3334 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163335 caller()->CreateAndSetAndSignalOffer();
3336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3337 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3338 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3339 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3340 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3341
3342 // To verify that the ICE restart actually occurs, get
3343 // ufrag/password/candidates before and after restart.
3344 // Create an SDP string of the first audio candidate for both clients.
3345 const webrtc::IceCandidateCollection* audio_candidates_caller =
3346 caller()->pc()->local_description()->candidates(0);
3347 const webrtc::IceCandidateCollection* audio_candidates_callee =
3348 callee()->pc()->local_description()->candidates(0);
3349 ASSERT_GT(audio_candidates_caller->count(), 0u);
3350 ASSERT_GT(audio_candidates_callee->count(), 0u);
3351 std::string caller_candidate_pre_restart;
3352 ASSERT_TRUE(
3353 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3354 std::string callee_candidate_pre_restart;
3355 ASSERT_TRUE(
3356 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3357 const cricket::SessionDescription* desc =
3358 caller()->pc()->local_description()->description();
3359 std::string caller_ufrag_pre_restart =
3360 desc->transport_infos()[0].description.ice_ufrag;
3361 desc = callee()->pc()->local_description()->description();
3362 std::string callee_ufrag_pre_restart =
3363 desc->transport_infos()[0].description.ice_ufrag;
3364
3365 // Have the caller initiate an ICE restart.
3366 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3367 caller()->CreateAndSetAndSignalOffer();
3368 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3369 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3370 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3371 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3372 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3373
3374 // Grab the ufrags/candidates again.
3375 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3376 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3377 ASSERT_GT(audio_candidates_caller->count(), 0u);
3378 ASSERT_GT(audio_candidates_callee->count(), 0u);
3379 std::string caller_candidate_post_restart;
3380 ASSERT_TRUE(
3381 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3382 std::string callee_candidate_post_restart;
3383 ASSERT_TRUE(
3384 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3385 desc = caller()->pc()->local_description()->description();
3386 std::string caller_ufrag_post_restart =
3387 desc->transport_infos()[0].description.ice_ufrag;
3388 desc = callee()->pc()->local_description()->description();
3389 std::string callee_ufrag_post_restart =
3390 desc->transport_infos()[0].description.ice_ufrag;
3391 // Sanity check that an ICE restart was actually negotiated in SDP.
3392 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3393 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3394 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3395 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3396
3397 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 19:54:423398 MediaExpectations media_expectations;
3399 media_expectations.ExpectBidirectionalAudioAndVideo();
3400 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163401}
3402
3403// Verify that audio/video can be received end-to-end when ICE renomination is
3404// enabled.
Seth Hampson2f0d7022018-02-20 19:54:423405TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-30 04:08:163406 PeerConnectionInterface::RTCConfiguration config;
3407 config.enable_ice_renomination = true;
3408 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3409 ConnectFakeSignaling();
3410 // Do normal offer/answer and wait for some frames to be received in each
3411 // direction.
Steve Anton15324772018-01-16 18:26:493412 caller()->AddAudioVideoTracks();
3413 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163414 caller()->CreateAndSetAndSignalOffer();
3415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3416 // Sanity check that ICE renomination was actually negotiated.
3417 const cricket::SessionDescription* desc =
3418 caller()->pc()->local_description()->description();
3419 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 09:41:293420 ASSERT_NE(
3421 info.description.transport_options.end(),
3422 std::find(info.description.transport_options.begin(),
3423 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-30 04:08:163424 }
3425 desc = callee()->pc()->local_description()->description();
3426 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 09:41:293427 ASSERT_NE(
3428 info.description.transport_options.end(),
3429 std::find(info.description.transport_options.begin(),
3430 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-30 04:08:163431 }
Seth Hampson2f0d7022018-02-20 19:54:423432 MediaExpectations media_expectations;
3433 media_expectations.ExpectBidirectionalAudioAndVideo();
3434 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163435}
3436
Steve Anton6f25b092017-10-23 16:39:203437// With a max bundle policy and RTCP muxing, adding a new media description to
3438// the connection should not affect ICE at all because the new media will use
3439// the existing connection.
Seth Hampson2f0d7022018-02-20 19:54:423440TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-11 00:19:523441 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 16:39:203442 PeerConnectionInterface::RTCConfiguration config;
3443 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3444 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3445 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3446 config, PeerConnectionInterface::RTCConfiguration()));
3447 ConnectFakeSignaling();
3448
Steve Anton15324772018-01-16 18:26:493449 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 16:39:203450 caller()->CreateAndSetAndSignalOffer();
3451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 19:24:503452 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3453 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 16:39:203454
3455 caller()->clear_ice_connection_state_history();
3456
Steve Anton15324772018-01-16 18:26:493457 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 16:39:203458 caller()->CreateAndSetAndSignalOffer();
3459 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3460
3461 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3462}
3463
deadbeef1dcb1642017-03-30 04:08:163464// This test sets up a call between two parties with audio and video. It then
3465// renegotiates setting the video m-line to "port 0", then later renegotiates
3466// again, enabling video.
Seth Hampson2f0d7022018-02-20 19:54:423467TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:163468 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3469 ASSERT_TRUE(CreatePeerConnectionWrappers());
3470 ConnectFakeSignaling();
3471
3472 // Do initial negotiation, only sending media from the caller. Will result in
3473 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 18:26:493474 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163475 caller()->CreateAndSetAndSignalOffer();
3476 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3477
3478 // Negotiate again, disabling the video "m=" section (the callee will set the
3479 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 19:54:423480 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3481 PeerConnectionInterface::RTCOfferAnswerOptions options;
3482 options.offer_to_receive_video = 0;
3483 callee()->SetOfferAnswerOptions(options);
3484 } else {
3485 callee()->SetRemoteOfferHandler([this] {
3486 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3487 });
3488 }
deadbeef1dcb1642017-03-30 04:08:163489 caller()->CreateAndSetAndSignalOffer();
3490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3491 // Sanity check that video "m=" section was actually rejected.
3492 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3493 callee()->pc()->local_description()->description());
3494 ASSERT_NE(nullptr, answer_video_content);
3495 ASSERT_TRUE(answer_video_content->rejected);
3496
3497 // Enable video and do negotiation again, making sure video is received
3498 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 19:54:423499 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3500 PeerConnectionInterface::RTCOfferAnswerOptions options;
3501 options.offer_to_receive_video = 1;
3502 callee()->SetOfferAnswerOptions(options);
3503 } else {
3504 // The caller's transceiver is stopped, so we need to add another track.
3505 auto caller_transceiver =
3506 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3507 EXPECT_TRUE(caller_transceiver->stopped());
3508 caller()->AddVideoTrack();
3509 }
3510 callee()->AddVideoTrack();
3511 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-30 04:08:163512 caller()->CreateAndSetAndSignalOffer();
3513 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:423514
deadbeef1dcb1642017-03-30 04:08:163515 // Verify the caller receives frames from the newly added stream, and the
3516 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 19:54:423517 MediaExpectations media_expectations;
3518 media_expectations.CalleeExpectsSomeAudio();
3519 media_expectations.ExpectBidirectionalVideo();
3520 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163521}
3522
3523// This test sets up a Jsep call between two parties with external
3524// VideoDecoderFactory.
3525// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3526// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 19:54:423527TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-30 04:08:163528 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3529 ASSERT_TRUE(CreatePeerConnectionWrappers());
3530 EnableVideoDecoderFactory();
3531 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:493532 caller()->AddAudioVideoTracks();
3533 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-30 04:08:163534 caller()->CreateAndSetAndSignalOffer();
3535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:423536 MediaExpectations media_expectations;
3537 media_expectations.ExpectBidirectionalAudioAndVideo();
3538 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163539}
3540
3541// This tests that if we negotiate after calling CreateSender but before we
3542// have a track, then set a track later, frames from the newly-set track are
3543// received end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:423544TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-30 04:08:163545 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3546 ASSERT_TRUE(CreatePeerConnectionWrappers());
3547 ConnectFakeSignaling();
3548 auto caller_audio_sender =
3549 caller()->pc()->CreateSender("audio", "caller_stream");
3550 auto caller_video_sender =
3551 caller()->pc()->CreateSender("video", "caller_stream");
3552 auto callee_audio_sender =
3553 callee()->pc()->CreateSender("audio", "callee_stream");
3554 auto callee_video_sender =
3555 callee()->pc()->CreateSender("video", "callee_stream");
3556 caller()->CreateAndSetAndSignalOffer();
3557 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3558 // Wait for ICE to complete, without any tracks being set.
3559 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3560 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3561 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3562 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3563 // Now set the tracks, and expect frames to immediately start flowing.
3564 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3565 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3566 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3567 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 19:54:423568 MediaExpectations media_expectations;
3569 media_expectations.ExpectBidirectionalAudioAndVideo();
3570 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3571}
3572
3573// This tests that if we negotiate after calling AddTransceiver but before we
3574// have a track, then set a track later, frames from the newly-set tracks are
3575// received end-to-end.
3576TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3577 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3578 ASSERT_TRUE(CreatePeerConnectionWrappers());
3579 ConnectFakeSignaling();
3580 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3581 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3582 auto caller_audio_sender = audio_result.MoveValue()->sender();
3583 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3584 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3585 auto caller_video_sender = video_result.MoveValue()->sender();
3586 callee()->SetRemoteOfferHandler([this] {
3587 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3588 callee()->pc()->GetTransceivers()[0]->SetDirection(
3589 RtpTransceiverDirection::kSendRecv);
3590 callee()->pc()->GetTransceivers()[1]->SetDirection(
3591 RtpTransceiverDirection::kSendRecv);
3592 });
3593 caller()->CreateAndSetAndSignalOffer();
3594 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3595 // Wait for ICE to complete, without any tracks being set.
3596 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3597 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3598 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3599 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3600 // Now set the tracks, and expect frames to immediately start flowing.
3601 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3602 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3603 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3604 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3605 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3606 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3607 MediaExpectations media_expectations;
3608 media_expectations.ExpectBidirectionalAudioAndVideo();
3609 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163610}
3611
3612// This test verifies that a remote video track can be added via AddStream,
3613// and sent end-to-end. For this particular test, it's simply echoed back
3614// from the caller to the callee, rather than being forwarded to a third
3615// PeerConnection.
Seth Hampson2f0d7022018-02-20 19:54:423616TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-30 04:08:163617 ASSERT_TRUE(CreatePeerConnectionWrappers());
3618 ConnectFakeSignaling();
3619 // Just send a video track from the caller.
Steve Anton15324772018-01-16 18:26:493620 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-30 04:08:163621 caller()->CreateAndSetAndSignalOffer();
3622 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3623 ASSERT_EQ(1, callee()->remote_streams()->count());
3624
3625 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3626 // time).
3627 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3628 callee()->CreateAndSetAndSignalOffer();
3629 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3630
Seth Hampson2f0d7022018-02-20 19:54:423631 MediaExpectations media_expectations;
3632 media_expectations.ExpectBidirectionalVideo();
3633 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-30 04:08:163634}
3635
3636// Test that we achieve the expected end-to-end connection time, using a
3637// fake clock and simulated latency on the media and signaling paths.
3638// We use a TURN<->TURN connection because this is usually the quickest to
3639// set up initially, especially when we're confident the connection will work
3640// and can start sending media before we get a STUN response.
3641//
3642// With various optimizations enabled, here are the network delays we expect to
3643// be on the critical path:
3644// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3645// signaling answer (with DTLS fingerprint).
3646// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3647// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3648// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 19:54:423649TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-30 04:08:163650 rtc::ScopedFakeClock fake_clock;
3651 // Some things use a time of "0" as a special value, so we need to start out
3652 // the fake clock at a nonzero time.
3653 // TODO(deadbeef): Fix this.
3654 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3655
3656 static constexpr int media_hop_delay_ms = 50;
3657 static constexpr int signaling_trip_delay_ms = 500;
3658 // For explanation of these values, see comment above.
3659 static constexpr int required_media_hops = 9;
3660 static constexpr int required_signaling_trips = 2;
3661 // For internal delays (such as posting an event asychronously).
3662 static constexpr int allowed_internal_delay_ms = 20;
3663 static constexpr int total_connection_time_ms =
3664 media_hop_delay_ms * required_media_hops +
3665 signaling_trip_delay_ms * required_signaling_trips +
3666 allowed_internal_delay_ms;
3667
3668 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3669 3478};
3670 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3671 0};
3672 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3673 3478};
3674 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3675 0};
3676 cricket::TestTurnServer turn_server_1(network_thread(),
3677 turn_server_1_internal_address,
3678 turn_server_1_external_address);
3679 cricket::TestTurnServer turn_server_2(network_thread(),
3680 turn_server_2_internal_address,
3681 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 12:01:403682
deadbeef1dcb1642017-03-30 04:08:163683 // Bypass permission check on received packets so media can be sent before
3684 // the candidate is signaled.
3685 turn_server_1.set_enable_permission_checks(false);
3686 turn_server_2.set_enable_permission_checks(false);
3687
3688 PeerConnectionInterface::RTCConfiguration client_1_config;
3689 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3690 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3691 ice_server_1.username = "test";
3692 ice_server_1.password = "test";
3693 client_1_config.servers.push_back(ice_server_1);
3694 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3695 client_1_config.presume_writable_when_fully_relayed = true;
3696
3697 PeerConnectionInterface::RTCConfiguration client_2_config;
3698 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3699 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3700 ice_server_2.username = "test";
3701 ice_server_2.password = "test";
3702 client_2_config.servers.push_back(ice_server_2);
3703 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3704 client_2_config.presume_writable_when_fully_relayed = true;
3705
3706 ASSERT_TRUE(
3707 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3708 // Set up the simulated delays.
3709 SetSignalingDelayMs(signaling_trip_delay_ms);
3710 ConnectFakeSignaling();
3711 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3712 virtual_socket_server()->UpdateDelayDistribution();
3713
3714 // Set "offer to receive audio/video" without adding any tracks, so we just
3715 // set up ICE/DTLS with no media.
3716 PeerConnectionInterface::RTCOfferAnswerOptions options;
3717 options.offer_to_receive_audio = 1;
3718 options.offer_to_receive_video = 1;
3719 caller()->SetOfferAnswerOptions(options);
3720 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-08 00:21:013721 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3722 fake_clock);
deadbeef1dcb1642017-03-30 04:08:163723 // Need to free the clients here since they're using things we created on
3724 // the stack.
3725 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3726 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3727}
3728
Jonas Orelandbdcee282017-10-10 12:01:403729// Verify that a TurnCustomizer passed in through RTCConfiguration
3730// is actually used by the underlying TURN candidate pair.
3731// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 19:54:423732TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 12:01:403733 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3734 3478};
3735 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3736 0};
3737 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3738 3478};
3739 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3740 0};
3741 cricket::TestTurnServer turn_server_1(network_thread(),
3742 turn_server_1_internal_address,
3743 turn_server_1_external_address);
3744 cricket::TestTurnServer turn_server_2(network_thread(),
3745 turn_server_2_internal_address,
3746 turn_server_2_external_address);
3747
3748 PeerConnectionInterface::RTCConfiguration client_1_config;
3749 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3750 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3751 ice_server_1.username = "test";
3752 ice_server_1.password = "test";
3753 client_1_config.servers.push_back(ice_server_1);
3754 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3755 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3756 client_1_config.turn_customizer = customizer1.get();
3757
3758 PeerConnectionInterface::RTCConfiguration client_2_config;
3759 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3760 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3761 ice_server_2.username = "test";
3762 ice_server_2.password = "test";
3763 client_2_config.servers.push_back(ice_server_2);
3764 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3765 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3766 client_2_config.turn_customizer = customizer2.get();
3767
3768 ASSERT_TRUE(
3769 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3770 ConnectFakeSignaling();
3771
3772 // Set "offer to receive audio/video" without adding any tracks, so we just
3773 // set up ICE/DTLS with no media.
3774 PeerConnectionInterface::RTCOfferAnswerOptions options;
3775 options.offer_to_receive_audio = 1;
3776 options.offer_to_receive_video = 1;
3777 caller()->SetOfferAnswerOptions(options);
3778 caller()->CreateAndSetAndSignalOffer();
3779 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3780
3781 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3782 EXPECT_GT(customizer1->modify_cnt_, 0u);
3783
3784 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3785 EXPECT_GT(customizer2->modify_cnt_, 0u);
3786
3787 // Need to free the clients here since they're using things we created on
3788 // the stack.
3789 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3790 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3791}
3792
deadbeefc964d0b2017-04-03 17:03:353793// Test that audio and video flow end-to-end when codec names don't use the
3794// expected casing, given that they're supposed to be case insensitive. To test
3795// this, all but one codec is removed from each media description, and its
3796// casing is changed.
3797//
3798// In the past, this has regressed and caused crashes/black video, due to the
3799// fact that code at some layers was doing case-insensitive comparisons and
3800// code at other layers was not.
Seth Hampson2f0d7022018-02-20 19:54:423801TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 17:03:353802 ASSERT_TRUE(CreatePeerConnectionWrappers());
3803 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:493804 caller()->AddAudioVideoTracks();
3805 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 17:03:353806
3807 // Remove all but one audio/video codec (opus and VP8), and change the
3808 // casing of the caller's generated offer.
3809 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3810 cricket::AudioContentDescription* audio =
3811 GetFirstAudioContentDescription(description);
3812 ASSERT_NE(nullptr, audio);
3813 auto audio_codecs = audio->codecs();
3814 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3815 [](const cricket::AudioCodec& codec) {
3816 return codec.name != "opus";
3817 }),
3818 audio_codecs.end());
3819 ASSERT_EQ(1u, audio_codecs.size());
3820 audio_codecs[0].name = "OpUs";
3821 audio->set_codecs(audio_codecs);
3822
3823 cricket::VideoContentDescription* video =
3824 GetFirstVideoContentDescription(description);
3825 ASSERT_NE(nullptr, video);
3826 auto video_codecs = video->codecs();
3827 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3828 [](const cricket::VideoCodec& codec) {
3829 return codec.name != "VP8";
3830 }),
3831 video_codecs.end());
3832 ASSERT_EQ(1u, video_codecs.size());
3833 video_codecs[0].name = "vP8";
3834 video->set_codecs(video_codecs);
3835 });
3836
3837 caller()->CreateAndSetAndSignalOffer();
3838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3839
3840 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:423841 MediaExpectations media_expectations;
3842 media_expectations.ExpectBidirectionalAudioAndVideo();
3843 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 17:03:353844}
3845
Seth Hampson2f0d7022018-02-20 19:54:423846TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 14:39:053847 ASSERT_TRUE(CreatePeerConnectionWrappers());
3848 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:493849 caller()->AddAudioTrack();
hbos8d609f62017-04-10 14:39:053850 caller()->CreateAndSetAndSignalOffer();
3851 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 23:01:173852 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 19:54:423853 MediaExpectations media_expectations;
3854 media_expectations.CalleeExpectsSomeAudio(1);
3855 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 14:39:053856 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3857 auto receiver = callee()->pc()->GetReceivers()[0];
3858 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3859
3860 auto contributing_sources = receiver->GetSources();
3861 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3862 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3863 contributing_sources[0].source_id());
3864}
3865
deadbeef2f425aa2017-04-14 17:41:323866// Test that if a track is removed and added again with a different stream ID,
3867// the new stream ID is successfully communicated in SDP and media continues to
3868// flow end-to-end.
Seth Hampson2f0d7022018-02-20 19:54:423869// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3870// it will not reuse a transceiver that has already been sending. After creating
3871// a new transceiver it tries to create an offer with two senders of the same
3872// track ids and it fails.
3873TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 17:41:323874 ASSERT_TRUE(CreatePeerConnectionWrappers());
3875 ConnectFakeSignaling();
3876
3877 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3878 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3879 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3880 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3881
3882 // Add track using stream 1, do offer/answer.
3883 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3884 caller()->CreateLocalAudioTrack();
3885 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3886 caller()->pc()->AddTrack(track, {stream_1.get()});
3887 caller()->CreateAndSetAndSignalOffer();
3888 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:423889 {
3890 MediaExpectations media_expectations;
3891 media_expectations.CalleeExpectsSomeAudio(1);
3892 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3893 }
deadbeef2f425aa2017-04-14 17:41:323894 // Remove the sender, and create a new one with the new stream.
3895 caller()->pc()->RemoveTrack(sender);
3896 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3897 caller()->CreateAndSetAndSignalOffer();
3898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3899 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 19:54:423900 {
3901 MediaExpectations media_expectations;
3902 media_expectations.CalleeExpectsSomeAudio();
3903 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3904 }
deadbeef2f425aa2017-04-14 17:41:323905}
3906
Seth Hampson2f0d7022018-02-20 19:54:423907TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 14:29:403908 ASSERT_TRUE(CreatePeerConnectionWrappers());
3909 ConnectFakeSignaling();
3910
3911 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3912 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3913 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3914 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 16:38:143915 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3916 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 14:29:403917
Steve Anton15324772018-01-16 18:26:493918 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 14:29:403919 caller()->CreateAndSetAndSignalOffer();
3920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3921}
3922
Steve Antonede9ca52017-10-16 20:04:273923// Test that if candidates are only signaled by applying full session
3924// descriptions (instead of using AddIceCandidate), the peers can connect to
3925// each other and exchange media.
Seth Hampson2f0d7022018-02-20 19:54:423926TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 20:04:273927 ASSERT_TRUE(CreatePeerConnectionWrappers());
3928 // Each side will signal the session descriptions but not candidates.
3929 ConnectFakeSignalingForSdpOnly();
3930
3931 // Add audio video track and exchange the initial offer/answer with media
3932 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 18:26:493933 caller()->AddAudioVideoTracks();
3934 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 20:04:273935 caller()->CreateAndSetAndSignalOffer();
3936
3937 // Wait for all candidates to be gathered on both the caller and callee.
3938 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3939 caller()->ice_gathering_state(), kDefaultTimeout);
3940 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3941 callee()->ice_gathering_state(), kDefaultTimeout);
3942
3943 // The candidates will now be included in the session description, so
3944 // signaling them will start the ICE connection.
3945 caller()->CreateAndSetAndSignalOffer();
3946 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3947
3948 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 19:54:423949 MediaExpectations media_expectations;
3950 media_expectations.ExpectBidirectionalAudioAndVideo();
3951 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 20:04:273952}
3953
henrika5f6bf242017-11-01 10:06:563954// Test that SetAudioPlayout can be used to disable audio playout from the
3955// start, then later enable it. This may be useful, for example, if the caller
3956// needs to play a local ringtone until some event occurs, after which it
3957// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 19:54:423958TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 10:06:563959 ASSERT_TRUE(CreatePeerConnectionWrappers());
3960 ConnectFakeSignaling();
3961
3962 // Set up audio-only call where audio playout is disabled on caller's side.
3963 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 18:26:493964 caller()->AddAudioTrack();
3965 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 10:06:563966 caller()->CreateAndSetAndSignalOffer();
3967 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3968
3969 // Pump messages for a second.
3970 WAIT(false, 1000);
3971 // Since audio playout is disabled, the caller shouldn't have received
3972 // anything (at the playout level, at least).
3973 EXPECT_EQ(0, caller()->audio_frames_received());
3974 // As a sanity check, make sure the callee (for which playout isn't disabled)
3975 // did still see frames on its audio level.
3976 ASSERT_GT(callee()->audio_frames_received(), 0);
3977
3978 // Enable playout again, and ensure audio starts flowing.
3979 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 19:54:423980 MediaExpectations media_expectations;
3981 media_expectations.ExpectBidirectionalAudio();
3982 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 10:06:563983}
3984
3985double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3986 auto report = pc->NewGetStats();
3987 auto track_stats_list =
3988 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3989 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3990 for (const auto* track_stats : track_stats_list) {
3991 if (track_stats->remote_source.is_defined() &&
3992 *track_stats->remote_source) {
3993 remote_track_stats = track_stats;
3994 break;
3995 }
3996 }
3997
3998 if (!remote_track_stats->total_audio_energy.is_defined()) {
3999 return 0.0;
4000 }
4001 return *remote_track_stats->total_audio_energy;
4002}
4003
4004// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4005// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 19:54:424006TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 10:06:564007 DisableAudioPlayoutStillGeneratesAudioStats) {
4008 ASSERT_TRUE(CreatePeerConnectionWrappers());
4009 ConnectFakeSignaling();
4010
4011 // Set up audio-only call where playout is disabled but audio-processing is
4012 // still active.
Steve Anton15324772018-01-16 18:26:494013 caller()->AddAudioTrack();
4014 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 10:06:564015 caller()->pc()->SetAudioPlayout(false);
4016
4017 caller()->CreateAndSetAndSignalOffer();
4018 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4019
4020 // Wait for the callee to receive audio stats.
4021 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4022}
4023
henrika4f167df2017-11-01 13:45:554024// Test that SetAudioRecording can be used to disable audio recording from the
4025// start, then later enable it. This may be useful, for example, if the caller
4026// wants to ensure that no audio resources are active before a certain state
4027// is reached.
Seth Hampson2f0d7022018-02-20 19:54:424028TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 13:45:554029 ASSERT_TRUE(CreatePeerConnectionWrappers());
4030 ConnectFakeSignaling();
4031
4032 // Set up audio-only call where audio recording is disabled on caller's side.
4033 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 18:26:494034 caller()->AddAudioTrack();
4035 callee()->AddAudioTrack();
henrika4f167df2017-11-01 13:45:554036 caller()->CreateAndSetAndSignalOffer();
4037 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4038
4039 // Pump messages for a second.
4040 WAIT(false, 1000);
4041 // Since caller has disabled audio recording, the callee shouldn't have
4042 // received anything.
4043 EXPECT_EQ(0, callee()->audio_frames_received());
4044 // As a sanity check, make sure the caller did still see frames on its
4045 // audio level since audio recording is enabled on the calle side.
4046 ASSERT_GT(caller()->audio_frames_received(), 0);
4047
4048 // Enable audio recording again, and ensure audio starts flowing.
4049 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 19:54:424050 MediaExpectations media_expectations;
4051 media_expectations.ExpectBidirectionalAudio();
4052 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 13:45:554053}
4054
Taylor Brandstetter389a97c2018-01-04 00:26:064055// Test that after closing PeerConnections, they stop sending any packets (ICE,
4056// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 19:54:424057TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-04 00:26:064058 // Set up audio/video/data, wait for some frames to be received.
4059 ASSERT_TRUE(CreatePeerConnectionWrappers());
4060 ConnectFakeSignaling();
Steve Anton15324772018-01-16 18:26:494061 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-04 00:26:064062#ifdef HAVE_SCTP
4063 caller()->CreateDataChannel();
4064#endif
4065 caller()->CreateAndSetAndSignalOffer();
4066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 19:54:424067 MediaExpectations media_expectations;
4068 media_expectations.CalleeExpectsSomeAudioAndVideo();
4069 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-04 00:26:064070 // Close PeerConnections.
4071 caller()->pc()->Close();
4072 callee()->pc()->Close();
4073 // Pump messages for a second, and ensure no new packets end up sent.
4074 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4075 WAIT(false, 1000);
4076 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4077 EXPECT_EQ(sent_packets_a, sent_packets_b);
4078}
4079
Steve Anton7eca0932018-03-30 22:18:414080// Test that transport stats are generated by the RTCStatsCollector for a
4081// connection that only involves data channels. This is a regression test for
4082// crbug.com/826972.
4083#ifdef HAVE_SCTP
4084TEST_P(PeerConnectionIntegrationTest,
4085 TransportStatsReportedForDataChannelOnlyConnection) {
4086 ASSERT_TRUE(CreatePeerConnectionWrappers());
4087 ConnectFakeSignaling();
4088 caller()->CreateDataChannel();
4089
4090 caller()->CreateAndSetAndSignalOffer();
4091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4092 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4093
4094 auto caller_report = caller()->NewGetStats();
4095 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4096 auto callee_report = callee()->NewGetStats();
4097 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4098}
4099#endif // HAVE_SCTP
4100
Seth Hampson2f0d7022018-02-20 19:54:424101INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4102 PeerConnectionIntegrationTest,
4103 Values(SdpSemantics::kPlanB,
4104 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-18 01:41:024105
Steve Anton74255ff2018-01-25 02:32:574106// Tests that verify interoperability between Plan B and Unified Plan
4107// PeerConnections.
4108class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 19:54:424109 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-25 02:32:574110 public ::testing::WithParamInterface<
4111 std::tuple<SdpSemantics, SdpSemantics>> {
4112 protected:
Seth Hampson2f0d7022018-02-20 19:54:424113 // Setting the SdpSemantics for the base test to kDefault does not matter
4114 // because we specify not to use the test semantics when creating
4115 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-25 02:32:574116 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 19:54:424117 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4118 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-25 02:32:574119 callee_semantics_(std::get<1>(GetParam())) {}
4120
4121 bool CreatePeerConnectionWrappersWithSemantics() {
4122 RTCConfiguration caller_config;
4123 caller_config.sdp_semantics = caller_semantics_;
4124 RTCConfiguration callee_config;
4125 callee_config.sdp_semantics = callee_semantics_;
4126 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4127 }
4128
4129 const SdpSemantics caller_semantics_;
4130 const SdpSemantics callee_semantics_;
4131};
4132
4133TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4134 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4135 ConnectFakeSignaling();
4136
4137 caller()->CreateAndSetAndSignalOffer();
4138 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4139}
4140
4141TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4142 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4143 ConnectFakeSignaling();
4144 auto audio_sender = caller()->AddAudioTrack();
4145
4146 caller()->CreateAndSetAndSignalOffer();
4147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4148
4149 // Verify that one audio receiver has been created on the remote and that it
4150 // has the same track ID as the sending track.
4151 auto receivers = callee()->pc()->GetReceivers();
4152 ASSERT_EQ(1u, receivers.size());
4153 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4154 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4155
Seth Hampson2f0d7022018-02-20 19:54:424156 MediaExpectations media_expectations;
4157 media_expectations.CalleeExpectsSomeAudio();
4158 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574159}
4160
4161TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4162 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4163 ConnectFakeSignaling();
4164 auto video_sender = caller()->AddVideoTrack();
4165 auto audio_sender = caller()->AddAudioTrack();
4166
4167 caller()->CreateAndSetAndSignalOffer();
4168 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4169
4170 // Verify that one audio and one video receiver have been created on the
4171 // remote and that they have the same track IDs as the sending tracks.
4172 auto audio_receivers =
4173 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4174 ASSERT_EQ(1u, audio_receivers.size());
4175 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4176 auto video_receivers =
4177 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4178 ASSERT_EQ(1u, video_receivers.size());
4179 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4180
Seth Hampson2f0d7022018-02-20 19:54:424181 MediaExpectations media_expectations;
4182 media_expectations.CalleeExpectsSomeAudioAndVideo();
4183 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574184}
4185
4186TEST_P(PeerConnectionIntegrationInteropTest,
4187 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4188 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4189 ConnectFakeSignaling();
4190 caller()->AddAudioVideoTracks();
4191 callee()->AddAudioVideoTracks();
4192
4193 caller()->CreateAndSetAndSignalOffer();
4194 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4195
Seth Hampson2f0d7022018-02-20 19:54:424196 MediaExpectations media_expectations;
4197 media_expectations.ExpectBidirectionalAudioAndVideo();
4198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574199}
4200
4201TEST_P(PeerConnectionIntegrationInteropTest,
4202 ReverseRolesOneAudioLocalToOneVideoRemote) {
4203 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4204 ConnectFakeSignaling();
4205 caller()->AddAudioTrack();
4206 callee()->AddVideoTrack();
4207
4208 caller()->CreateAndSetAndSignalOffer();
4209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4210
4211 // Verify that only the audio track has been negotiated.
4212 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4213 // Might also check that the callee's NegotiationNeeded flag is set.
4214
4215 // Reverse roles.
4216 callee()->CreateAndSetAndSignalOffer();
4217 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4218
Seth Hampson2f0d7022018-02-20 19:54:424219 MediaExpectations media_expectations;
4220 media_expectations.CallerExpectsSomeVideo();
4221 media_expectations.CalleeExpectsSomeAudio();
4222 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574223}
4224
4225// Test that if one side offers two video tracks then the other side will only
4226// see the first one and ignore the second.
4227TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToNoMediaRemote) {
4228 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4229 ConnectFakeSignaling();
4230 auto first_sender = caller()->AddVideoTrack();
4231 caller()->AddVideoTrack();
4232
4233 caller()->CreateAndSetAndSignalOffer();
4234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4235
4236 // Verify that there is only one receiver and it corresponds to the first
4237 // added track.
4238 auto receivers = callee()->pc()->GetReceivers();
4239 ASSERT_EQ(1u, receivers.size());
4240 EXPECT_TRUE(receivers[0]->track()->enabled());
4241 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4242
Seth Hampson2f0d7022018-02-20 19:54:424243 MediaExpectations media_expectations;
4244 media_expectations.CalleeExpectsSomeVideo();
4245 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574246}
4247
4248// Test that in the multi-track case each endpoint only looks at the first track
4249// and ignores the second one.
4250TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToTwoVideoRemote) {
4251 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4252 ConnectFakeSignaling();
4253 caller()->AddVideoTrack();
4254 caller()->AddVideoTrack();
4255 callee()->AddVideoTrack();
4256 callee()->AddVideoTrack();
4257
4258 caller()->CreateAndSetAndSignalOffer();
4259 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4260
4261 PeerConnectionWrapper* plan_b =
4262 (caller_semantics_ == SdpSemantics::kPlanB ? caller() : callee());
4263 PeerConnectionWrapper* unified_plan =
4264 (caller_semantics_ == SdpSemantics::kUnifiedPlan ? caller() : callee());
4265
4266 // Should have two senders each, one for each track.
4267 EXPECT_EQ(2u, plan_b->pc()->GetSenders().size());
4268 EXPECT_EQ(2u, unified_plan->pc()->GetSenders().size());
4269
4270 // Plan B will have one receiver since it only looks at the first video
4271 // section. The receiver should have the same track ID as the sender's first
4272 // track.
4273 ASSERT_EQ(1u, plan_b->pc()->GetReceivers().size());
4274 EXPECT_EQ(unified_plan->pc()->GetSenders()[0]->track()->id(),
4275 plan_b->pc()->GetReceivers()[0]->track()->id());
4276
4277 // Unified Plan will have two receivers since they were created with the
4278 // transceivers when the tracks were added.
4279 ASSERT_EQ(2u, unified_plan->pc()->GetReceivers().size());
4280
4281 if (unified_plan == caller()) {
4282 // If the Unified Plan endpoint was the caller, then the Plan B endpoint
4283 // would have rejected the second video media section so we would expect the
4284 // transceiver to be stopped.
4285 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[0]->stopped());
4286 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[1]->stopped());
4287 } else {
4288 // If the Unified Plan endpoint was the callee, then the Plan B endpoint
4289 // would have offered only one video section so we would expect the first
4290 // transceiver to map to the first track and the second transceiver to be
4291 // missing a mid.
4292 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[0]->mid());
4293 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[1]->mid());
4294 }
4295
4296 // Should be exchanging video frames for the first tracks on each endpoint.
Seth Hampson2f0d7022018-02-20 19:54:424297 MediaExpectations media_expectations;
4298 media_expectations.ExpectBidirectionalVideo();
4299 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-25 02:32:574300}
4301
4302INSTANTIATE_TEST_CASE_P(
4303 PeerConnectionIntegrationTest,
4304 PeerConnectionIntegrationInteropTest,
4305 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4306 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4307
deadbeef1dcb1642017-03-30 04:08:164308} // namespace
4309
4310#endif // if !defined(THREAD_SANITIZER)