blob: 28b7d16b914e2e5c3b0063eb63729ab896e4b02c [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/builtin_video_decoder_factory.h"
34#include "api/video_codecs/builtin_video_encoder_factory.h"
35#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "media/engine/fakewebrtcvideoengine.h"
37#include "p2p/base/p2pconstants.h"
38#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070039#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020040#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "p2p/base/testturnserver.h"
42#include "p2p/client/basicportallocator.h"
43#include "pc/dtmfsender.h"
44#include "pc/localaudiosource.h"
45#include "pc/mediasession.h"
46#include "pc/peerconnection.h"
47#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080048#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080049#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020051#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "pc/test/fakertccertificategenerator.h"
53#include "pc/test/fakevideotrackrenderer.h"
54#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070056#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/gunit.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070058#include "rtc_base/testcertificateverifier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020060#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070061
62using cricket::ContentInfo;
63using cricket::FakeWebRtcVideoDecoder;
64using cricket::FakeWebRtcVideoDecoderFactory;
65using cricket::FakeWebRtcVideoEncoder;
66using cricket::FakeWebRtcVideoEncoderFactory;
67using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070068using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070069using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080070using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070071using ::testing::ElementsAre;
72using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070073using webrtc::DataBuffer;
74using webrtc::DataChannelInterface;
75using webrtc::DtmfSender;
76using webrtc::DtmfSenderInterface;
77using webrtc::DtmfSenderObserverInterface;
78using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080079using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070080using webrtc::MediaConstraintsInterface;
81using webrtc::MediaStreamInterface;
82using webrtc::MediaStreamTrackInterface;
83using webrtc::MockCreateSessionDescriptionObserver;
84using webrtc::MockDataChannelObserver;
85using webrtc::MockSetSessionDescriptionObserver;
86using webrtc::MockStatsObserver;
87using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070088using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070089using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080090using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070091using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070092using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080093using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070094using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -080095using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010096using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080097using webrtc::RtpSenderInterface;
98using webrtc::RtpTransceiverDirection;
99using webrtc::RtpTransceiverInit;
100using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800101using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800102using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700103using webrtc::SessionDescriptionInterface;
104using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800105using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700106
107namespace {
108
109static const int kDefaultTimeout = 10000;
110static const int kMaxWaitForStatsMs = 3000;
111static const int kMaxWaitForActivationMs = 5000;
112static const int kMaxWaitForFramesMs = 10000;
113// Default number of audio/video frames to wait for before considering a test
114// successful.
115static const int kDefaultExpectedAudioFrameCount = 3;
116static const int kDefaultExpectedVideoFrameCount = 3;
117
deadbeef1dcb1642017-03-29 21:08:16 -0700118static const char kDataChannelLabel[] = "data_channel";
119
120// SRTP cipher name negotiated by the tests. This must be updated if the
121// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700122static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700123static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
124
Steve Antonede9ca52017-10-16 13:04:27 -0700125static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
126
deadbeef1dcb1642017-03-29 21:08:16 -0700127// Helper function for constructing offer/answer options to initiate an ICE
128// restart.
129PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
130 PeerConnectionInterface::RTCOfferAnswerOptions options;
131 options.ice_restart = true;
132 return options;
133}
134
deadbeefd8ad7882017-04-18 16:01:17 -0700135// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
136// attribute from received SDP, simulating a legacy endpoint.
137void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
138 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800139 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700140 }
141 desc->set_msid_supported(false);
142}
143
Seth Hampson5897a6e2018-04-03 11:16:33 -0700144// Removes all stream information besides the stream ids, simulating an
145// endpoint that only signals a=msid lines to convey stream_ids.
146void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
147 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700148 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700149 std::vector<std::string> stream_ids;
150 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700151 const StreamParams& first_stream =
152 content.media_description()->streams()[0];
153 track_id = first_stream.id;
154 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700155 }
156 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700157 StreamParams new_stream;
158 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700159 new_stream.set_stream_ids(stream_ids);
160 content.media_description()->AddStream(new_stream);
161 }
162}
163
zhihuangf8164932017-05-19 13:09:47 -0700164int FindFirstMediaStatsIndexByKind(
165 const std::string& kind,
166 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
167 media_stats_vec) {
168 for (size_t i = 0; i < media_stats_vec.size(); i++) {
169 if (media_stats_vec[i]->kind.ValueToString() == kind) {
170 return i;
171 }
172 }
173 return -1;
174}
175
deadbeef1dcb1642017-03-29 21:08:16 -0700176class SignalingMessageReceiver {
177 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800178 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700179 virtual void ReceiveIceMessage(const std::string& sdp_mid,
180 int sdp_mline_index,
181 const std::string& msg) = 0;
182
183 protected:
184 SignalingMessageReceiver() {}
185 virtual ~SignalingMessageReceiver() {}
186};
187
188class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
189 public:
190 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
191 : expected_media_type_(media_type) {}
192
193 void OnFirstPacketReceived(cricket::MediaType media_type) override {
194 ASSERT_EQ(expected_media_type_, media_type);
195 first_packet_received_ = true;
196 }
197
198 bool first_packet_received() const { return first_packet_received_; }
199
200 virtual ~MockRtpReceiverObserver() {}
201
202 private:
203 bool first_packet_received_ = false;
204 cricket::MediaType expected_media_type_;
205};
206
207// Helper class that wraps a peer connection, observes it, and can accept
208// signaling messages from another wrapper.
209//
210// Uses a fake network, fake A/V capture, and optionally fake
211// encoders/decoders, though they aren't used by default since they don't
212// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700213// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800214// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700215class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800216 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700217 public:
218 // Different factory methods for convenience.
219 // TODO(deadbeef): Could use the pattern of:
220 //
221 // PeerConnectionWrapper =
222 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
223 //
224 // To reduce some code duplication.
225 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
226 const std::string& debug_name,
227 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
228 rtc::Thread* network_thread,
229 rtc::Thread* worker_thread) {
230 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700231 webrtc::PeerConnectionDependencies dependencies(nullptr);
232 dependencies.cert_generator = std::move(cert_generator);
233 if (!client->Init(nullptr, nullptr, nullptr, std::move(dependencies),
deadbeef1dcb1642017-03-29 21:08:16 -0700234 network_thread, worker_thread)) {
235 delete client;
236 return nullptr;
237 }
238 return client;
239 }
240
deadbeef2f425aa2017-04-14 10:41:32 -0700241 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
242 return peer_connection_factory_.get();
243 }
244
deadbeef1dcb1642017-03-29 21:08:16 -0700245 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
246
247 // If a signaling message receiver is set (via ConnectFakeSignaling), this
248 // will set the whole offer/answer exchange in motion. Just need to wait for
249 // the signaling state to reach "stable".
250 void CreateAndSetAndSignalOffer() {
251 auto offer = CreateOffer();
252 ASSERT_NE(nullptr, offer);
253 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
254 }
255
256 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
257 // when a remote offer is received (via fake signaling) and an answer is
258 // generated. By default, uses default options.
259 void SetOfferAnswerOptions(
260 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
261 offer_answer_options_ = options;
262 }
263
264 // Set a callback to be invoked when SDP is received via the fake signaling
265 // channel, which provides an opportunity to munge (modify) the SDP. This is
266 // used to test SDP being applied that a PeerConnection would normally not
267 // generate, but a non-JSEP endpoint might.
268 void SetReceivedSdpMunger(
269 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100270 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700271 }
272
deadbeefc964d0b2017-04-03 10:03:35 -0700273 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700274 // generated.
275 void SetGeneratedSdpMunger(
276 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100277 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700278 }
279
Seth Hampson2f0d7022018-02-20 11:54:42 -0800280 // Set a callback to be invoked when a remote offer is received via the fake
281 // signaling channel. This provides an opportunity to change the
282 // PeerConnection state before an answer is created and sent to the caller.
283 void SetRemoteOfferHandler(std::function<void()> handler) {
284 remote_offer_handler_ = std::move(handler);
285 }
286
Steve Antonede9ca52017-10-16 13:04:27 -0700287 // Every ICE connection state in order that has been seen by the observer.
288 std::vector<PeerConnectionInterface::IceConnectionState>
289 ice_connection_state_history() const {
290 return ice_connection_state_history_;
291 }
Steve Anton6f25b092017-10-23 09:39:20 -0700292 void clear_ice_connection_state_history() {
293 ice_connection_state_history_.clear();
294 }
Steve Antonede9ca52017-10-16 13:04:27 -0700295
296 // Every ICE gathering state in order that has been seen by the observer.
297 std::vector<PeerConnectionInterface::IceGatheringState>
298 ice_gathering_state_history() const {
299 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700300 }
301
Steve Anton15324772018-01-16 10:26:49 -0800302 void AddAudioVideoTracks() {
303 AddAudioTrack();
304 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700305 }
306
Steve Anton74255ff2018-01-24 18:32:57 -0800307 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
308 return AddTrack(CreateLocalAudioTrack());
309 }
deadbeef1dcb1642017-03-29 21:08:16 -0700310
Steve Anton74255ff2018-01-24 18:32:57 -0800311 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
312 return AddTrack(CreateLocalVideoTrack());
313 }
deadbeef1dcb1642017-03-29 21:08:16 -0700314
315 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
316 FakeConstraints constraints;
317 // Disable highpass filter so that we can get all the test audio frames.
318 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
319 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
320 peer_connection_factory_->CreateAudioSource(&constraints);
321 // TODO(perkj): Test audio source when it is implemented. Currently audio
322 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700323 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700324 source);
325 }
326
327 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200328 return CreateLocalVideoTrackInternal(
329 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700330 }
331
332 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200333 CreateLocalVideoTrackWithConfig(
334 webrtc::FakePeriodicVideoSource::Config config) {
335 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700336 }
337
338 rtc::scoped_refptr<webrtc::VideoTrackInterface>
339 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200340 webrtc::FakePeriodicVideoSource::Config config;
341 config.rotation = rotation;
342 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700343 }
344
Steve Anton74255ff2018-01-24 18:32:57 -0800345 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
346 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800347 const std::vector<std::string>& stream_ids = {}) {
348 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800349 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800350 return result.MoveValue();
351 }
352
353 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
354 cricket::MediaType media_type) {
355 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
356 for (auto receiver : pc()->GetReceivers()) {
357 if (receiver->media_type() == media_type) {
358 receivers.push_back(receiver);
359 }
360 }
361 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700362 }
363
Seth Hampson2f0d7022018-02-20 11:54:42 -0800364 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
365 cricket::MediaType media_type) {
366 for (auto transceiver : pc()->GetTransceivers()) {
367 if (transceiver->receiver()->media_type() == media_type) {
368 return transceiver;
369 }
370 }
371 return nullptr;
372 }
373
deadbeef1dcb1642017-03-29 21:08:16 -0700374 bool SignalingStateStable() {
375 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
376 }
377
378 void CreateDataChannel() { CreateDataChannel(nullptr); }
379
380 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700381 CreateDataChannel(kDataChannelLabel, init);
382 }
383
384 void CreateDataChannel(const std::string& label,
385 const webrtc::DataChannelInit* init) {
386 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700387 ASSERT_TRUE(data_channel_.get() != nullptr);
388 data_observer_.reset(new MockDataChannelObserver(data_channel_));
389 }
390
391 DataChannelInterface* data_channel() { return data_channel_; }
392 const MockDataChannelObserver* data_observer() const {
393 return data_observer_.get();
394 }
395
396 int audio_frames_received() const {
397 return fake_audio_capture_module_->frames_received();
398 }
399
400 // Takes minimum of video frames received for each track.
401 //
402 // Can be used like:
403 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
404 //
405 // To ensure that all video tracks received at least a certain number of
406 // frames.
407 int min_video_frames_received_per_track() const {
408 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200409 if (fake_video_renderers_.empty()) {
410 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700411 }
deadbeef1dcb1642017-03-29 21:08:16 -0700412
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200413 for (const auto& pair : fake_video_renderers_) {
414 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700415 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200416 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700417 }
418
419 // Returns a MockStatsObserver in a state after stats gathering finished,
420 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700421 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700422 webrtc::MediaStreamTrackInterface* track) {
423 rtc::scoped_refptr<MockStatsObserver> observer(
424 new rtc::RefCountedObject<MockStatsObserver>());
425 EXPECT_TRUE(peer_connection_->GetStats(
426 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
427 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
428 return observer;
429 }
430
431 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700432 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
433 return OldGetStatsForTrack(nullptr);
434 }
435
436 // Synchronously gets stats and returns them. If it times out, fails the test
437 // and returns null.
438 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
439 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
440 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
441 peer_connection_->GetStats(callback);
442 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
443 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700444 }
445
446 int rendered_width() {
447 EXPECT_FALSE(fake_video_renderers_.empty());
448 return fake_video_renderers_.empty()
449 ? 0
450 : fake_video_renderers_.begin()->second->width();
451 }
452
453 int rendered_height() {
454 EXPECT_FALSE(fake_video_renderers_.empty());
455 return fake_video_renderers_.empty()
456 ? 0
457 : fake_video_renderers_.begin()->second->height();
458 }
459
460 double rendered_aspect_ratio() {
461 if (rendered_height() == 0) {
462 return 0.0;
463 }
464 return static_cast<double>(rendered_width()) / rendered_height();
465 }
466
467 webrtc::VideoRotation rendered_rotation() {
468 EXPECT_FALSE(fake_video_renderers_.empty());
469 return fake_video_renderers_.empty()
470 ? webrtc::kVideoRotation_0
471 : fake_video_renderers_.begin()->second->rotation();
472 }
473
474 int local_rendered_width() {
475 return local_video_renderer_ ? local_video_renderer_->width() : 0;
476 }
477
478 int local_rendered_height() {
479 return local_video_renderer_ ? local_video_renderer_->height() : 0;
480 }
481
482 double local_rendered_aspect_ratio() {
483 if (local_rendered_height() == 0) {
484 return 0.0;
485 }
486 return static_cast<double>(local_rendered_width()) /
487 local_rendered_height();
488 }
489
490 size_t number_of_remote_streams() {
491 if (!pc()) {
492 return 0;
493 }
494 return pc()->remote_streams()->count();
495 }
496
497 StreamCollectionInterface* remote_streams() const {
498 if (!pc()) {
499 ADD_FAILURE();
500 return nullptr;
501 }
502 return pc()->remote_streams();
503 }
504
505 StreamCollectionInterface* local_streams() {
506 if (!pc()) {
507 ADD_FAILURE();
508 return nullptr;
509 }
510 return pc()->local_streams();
511 }
512
513 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
514 return pc()->signaling_state();
515 }
516
517 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
518 return pc()->ice_connection_state();
519 }
520
521 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
522 return pc()->ice_gathering_state();
523 }
524
525 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
526 // GetReceivers. They're updated automatically when a remote offer/answer
527 // from the fake signaling channel is applied, or when
528 // ResetRtpReceiverObservers below is called.
529 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
530 rtp_receiver_observers() {
531 return rtp_receiver_observers_;
532 }
533
534 void ResetRtpReceiverObservers() {
535 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100536 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
537 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700538 std::unique_ptr<MockRtpReceiverObserver> observer(
539 new MockRtpReceiverObserver(receiver->media_type()));
540 receiver->SetObserver(observer.get());
541 rtp_receiver_observers_.push_back(std::move(observer));
542 }
543 }
544
Steve Antonede9ca52017-10-16 13:04:27 -0700545 rtc::FakeNetworkManager* network() const {
546 return fake_network_manager_.get();
547 }
548 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
549
deadbeef1dcb1642017-03-29 21:08:16 -0700550 private:
551 explicit PeerConnectionWrapper(const std::string& debug_name)
552 : debug_name_(debug_name) {}
553
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700554 bool Init(const MediaConstraintsInterface* constraints,
555 const PeerConnectionFactory::Options* options,
556 const PeerConnectionInterface::RTCConfiguration* config,
557 webrtc::PeerConnectionDependencies dependencies,
558 rtc::Thread* network_thread,
559 rtc::Thread* worker_thread) {
deadbeef1dcb1642017-03-29 21:08:16 -0700560 // There's an error in this test code if Init ends up being called twice.
561 RTC_DCHECK(!peer_connection_);
562 RTC_DCHECK(!peer_connection_factory_);
563
564 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700565 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700566
567 std::unique_ptr<cricket::PortAllocator> port_allocator(
568 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700569 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700570 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
571 if (!fake_audio_capture_module_) {
572 return false;
573 }
deadbeef1dcb1642017-03-29 21:08:16 -0700574 rtc::Thread* const signaling_thread = rtc::Thread::Current();
575 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
576 network_thread, worker_thread, signaling_thread,
Anders Carlsson67537952018-05-03 11:28:29 +0200577 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
578 fake_audio_capture_module_),
579 webrtc::CreateBuiltinAudioEncoderFactory(),
580 webrtc::CreateBuiltinAudioDecoderFactory(),
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200581 webrtc::CreateBuiltinVideoEncoderFactory(),
582 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
583 nullptr /* audio_processing */);
deadbeef1dcb1642017-03-29 21:08:16 -0700584 if (!peer_connection_factory_) {
585 return false;
586 }
587 if (options) {
588 peer_connection_factory_->SetOptions(*options);
589 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800590 if (config) {
591 sdp_semantics_ = config->sdp_semantics;
592 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700593
594 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700595 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700596 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700597 return peer_connection_.get() != nullptr;
598 }
599
600 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700601 const MediaConstraintsInterface* constraints,
602 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700603 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700604 PeerConnectionInterface::RTCConfiguration modified_config;
605 // If |config| is null, this will result in a default configuration being
606 // used.
607 if (config) {
608 modified_config = *config;
609 }
610 // Disable resolution adaptation; we don't want it interfering with the
611 // test results.
612 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
613 // ratios and not specific resolutions, is this even necessary?
614 modified_config.set_cpu_adaptation(false);
615
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700616 // Use the legacy interface.
617 if (constraints != nullptr) {
618 return peer_connection_factory_->CreatePeerConnection(
619 modified_config, constraints, std::move(dependencies.allocator),
620 std::move(dependencies.cert_generator), this);
621 }
622 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700623 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700624 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700625 }
626
627 void set_signaling_message_receiver(
628 SignalingMessageReceiver* signaling_message_receiver) {
629 signaling_message_receiver_ = signaling_message_receiver;
630 }
631
632 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
633
Steve Antonede9ca52017-10-16 13:04:27 -0700634 void set_signal_ice_candidates(bool signal) {
635 signal_ice_candidates_ = signal;
636 }
637
deadbeef1dcb1642017-03-29 21:08:16 -0700638 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200639 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700640 // Set max frame rate to 10fps to reduce the risk of test flakiness.
641 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200642 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700643
Niels Möller5c7efe72018-05-11 10:34:46 +0200644 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200645 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
646 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700647 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200648 peer_connection_factory_->CreateVideoTrack(
649 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700650 if (!local_video_renderer_) {
651 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
652 }
653 return track;
654 }
655
656 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100657 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800658 std::unique_ptr<SessionDescriptionInterface> desc =
659 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700660 if (received_sdp_munger_) {
661 received_sdp_munger_(desc->description());
662 }
663
664 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
665 // Setting a remote description may have changed the number of receivers,
666 // so reset the receiver observers.
667 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800668 if (remote_offer_handler_) {
669 remote_offer_handler_();
670 }
deadbeef1dcb1642017-03-29 21:08:16 -0700671 auto answer = CreateAnswer();
672 ASSERT_NE(nullptr, answer);
673 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
674 }
675
676 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100677 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800678 std::unique_ptr<SessionDescriptionInterface> desc =
679 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700680 if (received_sdp_munger_) {
681 received_sdp_munger_(desc->description());
682 }
683
684 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
685 // Set the RtpReceiverObserver after receivers are created.
686 ResetRtpReceiverObservers();
687 }
688
689 // Returns null on failure.
690 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
691 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
692 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
693 pc()->CreateOffer(observer, offer_answer_options_);
694 return WaitForDescriptionFromObserver(observer);
695 }
696
697 // Returns null on failure.
698 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
699 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
700 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
701 pc()->CreateAnswer(observer, offer_answer_options_);
702 return WaitForDescriptionFromObserver(observer);
703 }
704
705 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100706 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700707 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
708 if (!observer->result()) {
709 return nullptr;
710 }
711 auto description = observer->MoveDescription();
712 if (generated_sdp_munger_) {
713 generated_sdp_munger_(description->description());
714 }
715 return description;
716 }
717
718 // Setting the local description and sending the SDP message over the fake
719 // signaling channel are combined into the same method because the SDP
720 // message needs to be sent as soon as SetLocalDescription finishes, without
721 // waiting for the observer to be called. This ensures that ICE candidates
722 // don't outrace the description.
723 bool SetLocalDescriptionAndSendSdpMessage(
724 std::unique_ptr<SessionDescriptionInterface> desc) {
725 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
726 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100727 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800728 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700729 std::string sdp;
730 EXPECT_TRUE(desc->ToString(&sdp));
731 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800732 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
733 RemoveUnusedVideoRenderers();
734 }
deadbeef1dcb1642017-03-29 21:08:16 -0700735 // As mentioned above, we need to send the message immediately after
736 // SetLocalDescription.
737 SendSdpMessage(type, sdp);
738 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
739 return true;
740 }
741
742 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
743 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
744 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100745 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700746 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800747 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
748 RemoveUnusedVideoRenderers();
749 }
deadbeef1dcb1642017-03-29 21:08:16 -0700750 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
751 return observer->result();
752 }
753
Seth Hampson2f0d7022018-02-20 11:54:42 -0800754 // This is a work around to remove unused fake_video_renderers from
755 // transceivers that have either stopped or are no longer receiving.
756 void RemoveUnusedVideoRenderers() {
757 auto transceivers = pc()->GetTransceivers();
758 for (auto& transceiver : transceivers) {
759 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
760 continue;
761 }
762 // Remove fake video renderers from any stopped transceivers.
763 if (transceiver->stopped()) {
764 auto it =
765 fake_video_renderers_.find(transceiver->receiver()->track()->id());
766 if (it != fake_video_renderers_.end()) {
767 fake_video_renderers_.erase(it);
768 }
769 }
770 // Remove fake video renderers from any transceivers that are no longer
771 // receiving.
772 if ((transceiver->current_direction() &&
773 !webrtc::RtpTransceiverDirectionHasRecv(
774 *transceiver->current_direction()))) {
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 }
782 }
783
deadbeef1dcb1642017-03-29 21:08:16 -0700784 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
785 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800786 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700787 if (signaling_delay_ms_ == 0) {
788 RelaySdpMessageIfReceiverExists(type, msg);
789 } else {
790 invoker_.AsyncInvokeDelayed<void>(
791 RTC_FROM_HERE, rtc::Thread::Current(),
792 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
793 this, type, msg),
794 signaling_delay_ms_);
795 }
796 }
797
Steve Antona3a92c22017-12-07 10:27:41 -0800798 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700799 if (signaling_message_receiver_) {
800 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
801 }
802 }
803
804 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
805 // default).
806 void SendIceMessage(const std::string& sdp_mid,
807 int sdp_mline_index,
808 const std::string& msg) {
809 if (signaling_delay_ms_ == 0) {
810 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
811 } else {
812 invoker_.AsyncInvokeDelayed<void>(
813 RTC_FROM_HERE, rtc::Thread::Current(),
814 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
815 this, sdp_mid, sdp_mline_index, msg),
816 signaling_delay_ms_);
817 }
818 }
819
820 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
821 int sdp_mline_index,
822 const std::string& msg) {
823 if (signaling_message_receiver_) {
824 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
825 msg);
826 }
827 }
828
829 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800830 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
831 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700832 HandleIncomingOffer(msg);
833 } else {
834 HandleIncomingAnswer(msg);
835 }
836 }
837
838 void ReceiveIceMessage(const std::string& sdp_mid,
839 int sdp_mline_index,
840 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100841 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700842 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
843 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
844 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
845 }
846
847 // PeerConnectionObserver callbacks.
848 void OnSignalingChange(
849 webrtc::PeerConnectionInterface::SignalingState new_state) override {
850 EXPECT_EQ(pc()->signaling_state(), new_state);
851 }
Steve Anton15324772018-01-16 10:26:49 -0800852 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
853 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
854 streams) override {
855 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
856 rtc::scoped_refptr<VideoTrackInterface> video_track(
857 static_cast<VideoTrackInterface*>(receiver->track().get()));
858 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700859 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800860 fake_video_renderers_[video_track->id()] =
861 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700862 }
863 }
Steve Anton15324772018-01-16 10:26:49 -0800864 void OnRemoveTrack(
865 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
866 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
867 auto it = fake_video_renderers_.find(receiver->track()->id());
868 RTC_DCHECK(it != fake_video_renderers_.end());
869 fake_video_renderers_.erase(it);
870 }
871 }
deadbeef1dcb1642017-03-29 21:08:16 -0700872 void OnRenegotiationNeeded() override {}
873 void OnIceConnectionChange(
874 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
875 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700876 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700877 }
878 void OnIceGatheringChange(
879 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700880 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700881 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700882 }
883 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100884 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700885
886 std::string ice_sdp;
887 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700888 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700889 // Remote party may be deleted.
890 return;
891 }
892 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
893 }
894 void OnDataChannel(
895 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100896 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700897 data_channel_ = data_channel;
898 data_observer_.reset(new MockDataChannelObserver(data_channel));
899 }
900
deadbeef1dcb1642017-03-29 21:08:16 -0700901 std::string debug_name_;
902
903 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
904
905 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
906 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
907 peer_connection_factory_;
908
Steve Antonede9ca52017-10-16 13:04:27 -0700909 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700910 // Needed to keep track of number of frames sent.
911 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
912 // Needed to keep track of number of frames received.
913 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
914 fake_video_renderers_;
915 // Needed to ensure frames aren't received for removed tracks.
916 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
917 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700918
919 // For remote peer communication.
920 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
921 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700922 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700923
Niels Möller5c7efe72018-05-11 10:34:46 +0200924 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700925 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200926 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
927 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700928 // |local_video_renderer_| attached to the first created local video track.
929 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
930
Seth Hampson2f0d7022018-02-20 11:54:42 -0800931 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700932 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
933 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
934 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800935 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700936
937 rtc::scoped_refptr<DataChannelInterface> data_channel_;
938 std::unique_ptr<MockDataChannelObserver> data_observer_;
939
940 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
941
Steve Antonede9ca52017-10-16 13:04:27 -0700942 std::vector<PeerConnectionInterface::IceConnectionState>
943 ice_connection_state_history_;
944 std::vector<PeerConnectionInterface::IceGatheringState>
945 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700946
947 rtc::AsyncInvoker invoker_;
948
Seth Hampson2f0d7022018-02-20 11:54:42 -0800949 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700950};
951
Elad Alon99c3fe52017-10-13 16:29:40 +0200952class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
953 public:
954 virtual ~MockRtcEventLogOutput() = default;
955 MOCK_CONST_METHOD0(IsActive, bool());
956 MOCK_METHOD1(Write, bool(const std::string&));
957};
958
Seth Hampson2f0d7022018-02-20 11:54:42 -0800959// This helper object is used for both specifying how many audio/video frames
960// are expected to be received for a caller/callee. It provides helper functions
961// to specify these expectations. The object initially starts in a state of no
962// expectations.
963class MediaExpectations {
964 public:
965 enum ExpectFrames {
966 kExpectSomeFrames,
967 kExpectNoFrames,
968 kNoExpectation,
969 };
970
971 void ExpectBidirectionalAudioAndVideo() {
972 ExpectBidirectionalAudio();
973 ExpectBidirectionalVideo();
974 }
975
976 void ExpectBidirectionalAudio() {
977 CallerExpectsSomeAudio();
978 CalleeExpectsSomeAudio();
979 }
980
981 void ExpectNoAudio() {
982 CallerExpectsNoAudio();
983 CalleeExpectsNoAudio();
984 }
985
986 void ExpectBidirectionalVideo() {
987 CallerExpectsSomeVideo();
988 CalleeExpectsSomeVideo();
989 }
990
991 void ExpectNoVideo() {
992 CallerExpectsNoVideo();
993 CalleeExpectsNoVideo();
994 }
995
996 void CallerExpectsSomeAudioAndVideo() {
997 CallerExpectsSomeAudio();
998 CallerExpectsSomeVideo();
999 }
1000
1001 void CalleeExpectsSomeAudioAndVideo() {
1002 CalleeExpectsSomeAudio();
1003 CalleeExpectsSomeVideo();
1004 }
1005
1006 // Caller's audio functions.
1007 void CallerExpectsSomeAudio(
1008 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1009 caller_audio_expectation_ = kExpectSomeFrames;
1010 caller_audio_frames_expected_ = expected_audio_frames;
1011 }
1012
1013 void CallerExpectsNoAudio() {
1014 caller_audio_expectation_ = kExpectNoFrames;
1015 caller_audio_frames_expected_ = 0;
1016 }
1017
1018 // Caller's video functions.
1019 void CallerExpectsSomeVideo(
1020 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1021 caller_video_expectation_ = kExpectSomeFrames;
1022 caller_video_frames_expected_ = expected_video_frames;
1023 }
1024
1025 void CallerExpectsNoVideo() {
1026 caller_video_expectation_ = kExpectNoFrames;
1027 caller_video_frames_expected_ = 0;
1028 }
1029
1030 // Callee's audio functions.
1031 void CalleeExpectsSomeAudio(
1032 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1033 callee_audio_expectation_ = kExpectSomeFrames;
1034 callee_audio_frames_expected_ = expected_audio_frames;
1035 }
1036
1037 void CalleeExpectsNoAudio() {
1038 callee_audio_expectation_ = kExpectNoFrames;
1039 callee_audio_frames_expected_ = 0;
1040 }
1041
1042 // Callee's video functions.
1043 void CalleeExpectsSomeVideo(
1044 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1045 callee_video_expectation_ = kExpectSomeFrames;
1046 callee_video_frames_expected_ = expected_video_frames;
1047 }
1048
1049 void CalleeExpectsNoVideo() {
1050 callee_video_expectation_ = kExpectNoFrames;
1051 callee_video_frames_expected_ = 0;
1052 }
1053
1054 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1055 ExpectFrames caller_video_expectation_ = kNoExpectation;
1056 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1057 ExpectFrames callee_video_expectation_ = kNoExpectation;
1058 int caller_audio_frames_expected_ = 0;
1059 int caller_video_frames_expected_ = 0;
1060 int callee_audio_frames_expected_ = 0;
1061 int callee_video_frames_expected_ = 0;
1062};
1063
deadbeef1dcb1642017-03-29 21:08:16 -07001064// Tests two PeerConnections connecting to each other end-to-end, using a
1065// virtual network, fake A/V capture and fake encoder/decoders. The
1066// PeerConnections share the threads/socket servers, but use separate versions
1067// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001068class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001069 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001070 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1071 : sdp_semantics_(sdp_semantics),
1072 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001073 fss_(new rtc::FirewallSocketServer(ss_.get())),
1074 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001075 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001076 network_thread_->SetName("PCNetworkThread", this);
1077 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001078 RTC_CHECK(network_thread_->Start());
1079 RTC_CHECK(worker_thread_->Start());
1080 }
1081
Seth Hampson2f0d7022018-02-20 11:54:42 -08001082 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001083 if (caller_) {
1084 caller_->set_signaling_message_receiver(nullptr);
1085 }
1086 if (callee_) {
1087 callee_->set_signaling_message_receiver(nullptr);
1088 }
1089 }
1090
1091 bool SignalingStateStable() {
1092 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1093 }
1094
deadbeef71452802017-05-07 17:21:01 -07001095 bool DtlsConnected() {
1096 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1097 // are connected. This is an important distinction. Once we have separate
1098 // ICE and DTLS state, this check needs to use the DTLS state.
1099 return (callee()->ice_connection_state() ==
1100 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1101 callee()->ice_connection_state() ==
1102 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1103 (caller()->ice_connection_state() ==
1104 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1105 caller()->ice_connection_state() ==
1106 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1107 }
1108
Seth Hampson2f0d7022018-02-20 11:54:42 -08001109 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1110 const std::string& debug_name,
1111 const MediaConstraintsInterface* constraints,
1112 const PeerConnectionFactory::Options* options,
1113 const RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001114 webrtc::PeerConnectionDependencies dependencies) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001115 RTCConfiguration modified_config;
1116 if (config) {
1117 modified_config = *config;
1118 }
Steve Anton3acffc32018-04-12 17:21:03 -07001119 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001120 if (!dependencies.cert_generator) {
1121 dependencies.cert_generator =
1122 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001123 }
1124 std::unique_ptr<PeerConnectionWrapper> client(
1125 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001126
Seth Hampson2f0d7022018-02-20 11:54:42 -08001127 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001128 std::move(dependencies), network_thread_.get(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001129 worker_thread_.get())) {
1130 return nullptr;
1131 }
1132 return client;
1133 }
1134
deadbeef1dcb1642017-03-29 21:08:16 -07001135 bool CreatePeerConnectionWrappers() {
1136 return CreatePeerConnectionWrappersWithConfig(
1137 PeerConnectionInterface::RTCConfiguration(),
1138 PeerConnectionInterface::RTCConfiguration());
1139 }
1140
Steve Anton3acffc32018-04-12 17:21:03 -07001141 bool CreatePeerConnectionWrappersWithSdpSemantics(
1142 SdpSemantics caller_semantics,
1143 SdpSemantics callee_semantics) {
1144 // Can't specify the sdp_semantics in the passed-in configuration since it
1145 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1146 // stored in sdp_semantics_. So get around this by modifying the instance
1147 // variable before calling CreatePeerConnectionWrapper for the caller and
1148 // callee PeerConnections.
1149 SdpSemantics original_semantics = sdp_semantics_;
1150 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001151 caller_ = CreatePeerConnectionWrapper(
1152 "Caller", nullptr, nullptr, nullptr,
1153 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001154 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001155 callee_ = CreatePeerConnectionWrapper(
1156 "Callee", nullptr, nullptr, nullptr,
1157 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001158 sdp_semantics_ = original_semantics;
1159 return caller_ && callee_;
1160 }
1161
deadbeef1dcb1642017-03-29 21:08:16 -07001162 bool CreatePeerConnectionWrappersWithConstraints(
1163 MediaConstraintsInterface* caller_constraints,
1164 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001165 caller_ = CreatePeerConnectionWrapper(
1166 "Caller", caller_constraints, nullptr, nullptr,
1167 webrtc::PeerConnectionDependencies(nullptr));
1168 callee_ = CreatePeerConnectionWrapper(
1169 "Callee", callee_constraints, nullptr, nullptr,
1170 webrtc::PeerConnectionDependencies(nullptr));
1171
deadbeef1dcb1642017-03-29 21:08:16 -07001172 return caller_ && callee_;
1173 }
1174
1175 bool CreatePeerConnectionWrappersWithConfig(
1176 const PeerConnectionInterface::RTCConfiguration& caller_config,
1177 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001178 caller_ = CreatePeerConnectionWrapper(
1179 "Caller", nullptr, nullptr, &caller_config,
1180 webrtc::PeerConnectionDependencies(nullptr));
1181 callee_ = CreatePeerConnectionWrapper(
1182 "Callee", nullptr, nullptr, &callee_config,
1183 webrtc::PeerConnectionDependencies(nullptr));
1184 return caller_ && callee_;
1185 }
1186
1187 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1188 const PeerConnectionInterface::RTCConfiguration& caller_config,
1189 webrtc::PeerConnectionDependencies caller_dependencies,
1190 const PeerConnectionInterface::RTCConfiguration& callee_config,
1191 webrtc::PeerConnectionDependencies callee_dependencies) {
1192 caller_ =
1193 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
1194 std::move(caller_dependencies));
1195 callee_ =
1196 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
1197 std::move(callee_dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001198 return caller_ && callee_;
1199 }
1200
1201 bool CreatePeerConnectionWrappersWithOptions(
1202 const PeerConnectionFactory::Options& caller_options,
1203 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001204 caller_ = CreatePeerConnectionWrapper(
1205 "Caller", nullptr, &caller_options, nullptr,
1206 webrtc::PeerConnectionDependencies(nullptr));
1207 callee_ = CreatePeerConnectionWrapper(
1208 "Callee", nullptr, &callee_options, nullptr,
1209 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001210 return caller_ && callee_;
1211 }
1212
Seth Hampson2f0d7022018-02-20 11:54:42 -08001213 std::unique_ptr<PeerConnectionWrapper>
1214 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001215 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1216 new FakeRTCCertificateGenerator());
1217 cert_generator->use_alternate_key();
1218
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001219 webrtc::PeerConnectionDependencies dependencies(nullptr);
1220 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001221 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001222 std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001223 }
1224
1225 // Once called, SDP blobs and ICE candidates will be automatically signaled
1226 // between PeerConnections.
1227 void ConnectFakeSignaling() {
1228 caller_->set_signaling_message_receiver(callee_.get());
1229 callee_->set_signaling_message_receiver(caller_.get());
1230 }
1231
Steve Antonede9ca52017-10-16 13:04:27 -07001232 // Once called, SDP blobs will be automatically signaled between
1233 // PeerConnections. Note that ICE candidates will not be signaled unless they
1234 // are in the exchanged SDP blobs.
1235 void ConnectFakeSignalingForSdpOnly() {
1236 ConnectFakeSignaling();
1237 SetSignalIceCandidates(false);
1238 }
1239
deadbeef1dcb1642017-03-29 21:08:16 -07001240 void SetSignalingDelayMs(int delay_ms) {
1241 caller_->set_signaling_delay_ms(delay_ms);
1242 callee_->set_signaling_delay_ms(delay_ms);
1243 }
1244
Steve Antonede9ca52017-10-16 13:04:27 -07001245 void SetSignalIceCandidates(bool signal) {
1246 caller_->set_signal_ice_candidates(signal);
1247 callee_->set_signal_ice_candidates(signal);
1248 }
1249
deadbeef1dcb1642017-03-29 21:08:16 -07001250 // Messages may get lost on the unreliable DataChannel, so we send multiple
1251 // times to avoid test flakiness.
1252 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1253 const std::string& data,
1254 int retries) {
1255 for (int i = 0; i < retries; ++i) {
1256 dc->Send(DataBuffer(data));
1257 }
1258 }
1259
1260 rtc::Thread* network_thread() { return network_thread_.get(); }
1261
1262 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1263
1264 PeerConnectionWrapper* caller() { return caller_.get(); }
1265
1266 // Set the |caller_| to the |wrapper| passed in and return the
1267 // original |caller_|.
1268 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1269 PeerConnectionWrapper* wrapper) {
1270 PeerConnectionWrapper* old = caller_.release();
1271 caller_.reset(wrapper);
1272 return old;
1273 }
1274
1275 PeerConnectionWrapper* callee() { return callee_.get(); }
1276
1277 // Set the |callee_| to the |wrapper| passed in and return the
1278 // original |callee_|.
1279 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1280 PeerConnectionWrapper* wrapper) {
1281 PeerConnectionWrapper* old = callee_.release();
1282 callee_.reset(wrapper);
1283 return old;
1284 }
1285
Steve Antonede9ca52017-10-16 13:04:27 -07001286 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1287
Seth Hampson2f0d7022018-02-20 11:54:42 -08001288 // Expects the provided number of new frames to be received within
1289 // kMaxWaitForFramesMs. The new expected frames are specified in
1290 // |media_expectations|. Returns false if any of the expectations were
1291 // not met.
1292 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1293 // First initialize the expected frame counts based upon the current
1294 // frame count.
1295 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1296 if (media_expectations.caller_audio_expectation_ ==
1297 MediaExpectations::kExpectSomeFrames) {
1298 total_caller_audio_frames_expected +=
1299 media_expectations.caller_audio_frames_expected_;
1300 }
1301 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001302 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001303 if (media_expectations.caller_video_expectation_ ==
1304 MediaExpectations::kExpectSomeFrames) {
1305 total_caller_video_frames_expected +=
1306 media_expectations.caller_video_frames_expected_;
1307 }
1308 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1309 if (media_expectations.callee_audio_expectation_ ==
1310 MediaExpectations::kExpectSomeFrames) {
1311 total_callee_audio_frames_expected +=
1312 media_expectations.callee_audio_frames_expected_;
1313 }
1314 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001315 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001316 if (media_expectations.callee_video_expectation_ ==
1317 MediaExpectations::kExpectSomeFrames) {
1318 total_callee_video_frames_expected +=
1319 media_expectations.callee_video_frames_expected_;
1320 }
deadbeef1dcb1642017-03-29 21:08:16 -07001321
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001323 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001325 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001326 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001327 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001328 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001329 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001330 total_callee_video_frames_expected,
1331 kMaxWaitForFramesMs);
1332 bool expectations_correct =
1333 caller()->audio_frames_received() >=
1334 total_caller_audio_frames_expected &&
1335 caller()->min_video_frames_received_per_track() >=
1336 total_caller_video_frames_expected &&
1337 callee()->audio_frames_received() >=
1338 total_callee_audio_frames_expected &&
1339 callee()->min_video_frames_received_per_track() >=
1340 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001341
Seth Hampson2f0d7022018-02-20 11:54:42 -08001342 // After the combined wait, print out a more detailed message upon
1343 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001344 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001345 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001346 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001347 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001348 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001349 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001350 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001351 total_callee_video_frames_expected);
1352
1353 // We want to make sure nothing unexpected was received.
1354 if (media_expectations.caller_audio_expectation_ ==
1355 MediaExpectations::kExpectNoFrames) {
1356 EXPECT_EQ(caller()->audio_frames_received(),
1357 total_caller_audio_frames_expected);
1358 if (caller()->audio_frames_received() !=
1359 total_caller_audio_frames_expected) {
1360 expectations_correct = false;
1361 }
1362 }
1363 if (media_expectations.caller_video_expectation_ ==
1364 MediaExpectations::kExpectNoFrames) {
1365 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1366 total_caller_video_frames_expected);
1367 if (caller()->min_video_frames_received_per_track() !=
1368 total_caller_video_frames_expected) {
1369 expectations_correct = false;
1370 }
1371 }
1372 if (media_expectations.callee_audio_expectation_ ==
1373 MediaExpectations::kExpectNoFrames) {
1374 EXPECT_EQ(callee()->audio_frames_received(),
1375 total_callee_audio_frames_expected);
1376 if (callee()->audio_frames_received() !=
1377 total_callee_audio_frames_expected) {
1378 expectations_correct = false;
1379 }
1380 }
1381 if (media_expectations.callee_video_expectation_ ==
1382 MediaExpectations::kExpectNoFrames) {
1383 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1384 total_callee_video_frames_expected);
1385 if (callee()->min_video_frames_received_per_track() !=
1386 total_callee_video_frames_expected) {
1387 expectations_correct = false;
1388 }
1389 }
1390 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001391 }
1392
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001393 void TestNegotiatedCipherSuite(
1394 const PeerConnectionFactory::Options& caller_options,
1395 const PeerConnectionFactory::Options& callee_options,
1396 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001397 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1398 callee_options));
1399 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1400 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1401 caller()->pc()->RegisterUMAObserver(caller_observer);
1402 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001403 caller()->AddAudioVideoTracks();
1404 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001405 caller()->CreateAndSetAndSignalOffer();
1406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1407 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001408 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001409 EXPECT_EQ(
1410 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1411 expected_cipher_suite));
1412 caller()->pc()->RegisterUMAObserver(nullptr);
1413 }
1414
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001415 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1416 bool remote_gcm_enabled,
1417 int expected_cipher_suite) {
1418 PeerConnectionFactory::Options caller_options;
1419 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1420 PeerConnectionFactory::Options callee_options;
1421 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1422 TestNegotiatedCipherSuite(caller_options, callee_options,
1423 expected_cipher_suite);
1424 }
1425
Seth Hampson2f0d7022018-02-20 11:54:42 -08001426 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001427 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001428
deadbeef1dcb1642017-03-29 21:08:16 -07001429 private:
1430 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001431 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001432 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001433 // |network_thread_| and |worker_thread_| are used by both
1434 // |caller_| and |callee_| so they must be destroyed
1435 // later.
1436 std::unique_ptr<rtc::Thread> network_thread_;
1437 std::unique_ptr<rtc::Thread> worker_thread_;
1438 std::unique_ptr<PeerConnectionWrapper> caller_;
1439 std::unique_ptr<PeerConnectionWrapper> callee_;
1440};
1441
Seth Hampson2f0d7022018-02-20 11:54:42 -08001442class PeerConnectionIntegrationTest
1443 : public PeerConnectionIntegrationBaseTest,
1444 public ::testing::WithParamInterface<SdpSemantics> {
1445 protected:
1446 PeerConnectionIntegrationTest()
1447 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1448};
1449
1450class PeerConnectionIntegrationTestPlanB
1451 : public PeerConnectionIntegrationBaseTest {
1452 protected:
1453 PeerConnectionIntegrationTestPlanB()
1454 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1455};
1456
1457class PeerConnectionIntegrationTestUnifiedPlan
1458 : public PeerConnectionIntegrationBaseTest {
1459 protected:
1460 PeerConnectionIntegrationTestUnifiedPlan()
1461 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1462};
1463
deadbeef1dcb1642017-03-29 21:08:16 -07001464// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1465// includes testing that the callback is invoked if an observer is connected
1466// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001467TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001468 RtpReceiverObserverOnFirstPacketReceived) {
1469 ASSERT_TRUE(CreatePeerConnectionWrappers());
1470 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001471 caller()->AddAudioVideoTracks();
1472 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001473 // Start offer/answer exchange and wait for it to complete.
1474 caller()->CreateAndSetAndSignalOffer();
1475 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1476 // Should be one receiver each for audio/video.
1477 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1478 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1479 // Wait for all "first packet received" callbacks to be fired.
1480 EXPECT_TRUE_WAIT(
1481 std::all_of(caller()->rtp_receiver_observers().begin(),
1482 caller()->rtp_receiver_observers().end(),
1483 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1484 return o->first_packet_received();
1485 }),
1486 kMaxWaitForFramesMs);
1487 EXPECT_TRUE_WAIT(
1488 std::all_of(callee()->rtp_receiver_observers().begin(),
1489 callee()->rtp_receiver_observers().end(),
1490 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1491 return o->first_packet_received();
1492 }),
1493 kMaxWaitForFramesMs);
1494 // If new observers are set after the first packet was already received, the
1495 // callback should still be invoked.
1496 caller()->ResetRtpReceiverObservers();
1497 callee()->ResetRtpReceiverObservers();
1498 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1499 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1500 EXPECT_TRUE(
1501 std::all_of(caller()->rtp_receiver_observers().begin(),
1502 caller()->rtp_receiver_observers().end(),
1503 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1504 return o->first_packet_received();
1505 }));
1506 EXPECT_TRUE(
1507 std::all_of(callee()->rtp_receiver_observers().begin(),
1508 callee()->rtp_receiver_observers().end(),
1509 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1510 return o->first_packet_received();
1511 }));
1512}
1513
1514class DummyDtmfObserver : public DtmfSenderObserverInterface {
1515 public:
1516 DummyDtmfObserver() : completed_(false) {}
1517
1518 // Implements DtmfSenderObserverInterface.
1519 void OnToneChange(const std::string& tone) override {
1520 tones_.push_back(tone);
1521 if (tone.empty()) {
1522 completed_ = true;
1523 }
1524 }
1525
1526 const std::vector<std::string>& tones() const { return tones_; }
1527 bool completed() const { return completed_; }
1528
1529 private:
1530 bool completed_;
1531 std::vector<std::string> tones_;
1532};
1533
1534// Assumes |sender| already has an audio track added and the offer/answer
1535// exchange is done.
1536void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1537 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001538 // We should be able to get a DTMF sender from the local sender.
1539 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1540 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1541 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001542 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001543 dtmf_sender->RegisterObserver(&observer);
1544
1545 // Test the DtmfSender object just created.
1546 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1547 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1548
1549 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1550 std::vector<std::string> tones = {"1", "a", ""};
1551 EXPECT_EQ(tones, observer.tones());
1552 dtmf_sender->UnregisterObserver();
1553 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1554}
1555
1556// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1557// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001558TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001559 ASSERT_TRUE(CreatePeerConnectionWrappers());
1560 ConnectFakeSignaling();
1561 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001562 caller()->AddAudioTrack();
1563 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001564 caller()->CreateAndSetAndSignalOffer();
1565 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001566 // DTLS must finish before the DTMF sender can be used reliably.
1567 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001568 TestDtmfFromSenderToReceiver(caller(), callee());
1569 TestDtmfFromSenderToReceiver(callee(), caller());
1570}
1571
1572// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1573// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001574TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001575 ASSERT_TRUE(CreatePeerConnectionWrappers());
1576 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001577 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1578 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1579 caller()->pc()->RegisterUMAObserver(caller_observer);
1580
deadbeef1dcb1642017-03-29 21:08:16 -07001581 // Do normal offer/answer and wait for some frames to be received in each
1582 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001583 caller()->AddAudioVideoTracks();
1584 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001585 caller()->CreateAndSetAndSignalOffer();
1586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001587 MediaExpectations media_expectations;
1588 media_expectations.ExpectBidirectionalAudioAndVideo();
1589 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001590 EXPECT_LE(
1591 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1592 webrtc::kEnumCounterKeyProtocolDtls));
1593 EXPECT_EQ(
1594 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1595 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001596}
1597
1598// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001599TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001600 PeerConnectionInterface::RTCConfiguration sdes_config;
1601 sdes_config.enable_dtls_srtp.emplace(false);
1602 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1603 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001604 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1605 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1606 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001607
1608 // Do normal offer/answer and wait for some frames to be received in each
1609 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001610 caller()->AddAudioVideoTracks();
1611 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001612 caller()->CreateAndSetAndSignalOffer();
1613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001614 MediaExpectations media_expectations;
1615 media_expectations.ExpectBidirectionalAudioAndVideo();
1616 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001617 EXPECT_LE(
1618 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1619 webrtc::kEnumCounterKeyProtocolSdes));
1620 EXPECT_EQ(
1621 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1622 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001623}
1624
Steve Anton8c0f7a72017-10-03 10:03:10 -07001625// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1626// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001627TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001628 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1629 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1630 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1631 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1632 return pc->GetRemoteAudioSSLCertificate();
1633 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001634 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1635 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1636 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1637 return pc->GetRemoteAudioSSLCertChain();
1638 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001639
1640 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1641 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1642
1643 // Configure each side with a known certificate so they can be compared later.
1644 PeerConnectionInterface::RTCConfiguration caller_config;
1645 caller_config.enable_dtls_srtp.emplace(true);
1646 caller_config.certificates.push_back(caller_cert);
1647 PeerConnectionInterface::RTCConfiguration callee_config;
1648 callee_config.enable_dtls_srtp.emplace(true);
1649 callee_config.certificates.push_back(callee_cert);
1650 ASSERT_TRUE(
1651 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1652 ConnectFakeSignaling();
1653
1654 // When first initialized, there should not be a remote SSL certificate (and
1655 // calling this method should not crash).
1656 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1657 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001658 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1659 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001660
Steve Anton15324772018-01-16 10:26:49 -08001661 caller()->AddAudioTrack();
1662 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001663 caller()->CreateAndSetAndSignalOffer();
1664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1665 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1666
1667 // Once DTLS has been connected, each side should return the other's SSL
1668 // certificate when calling GetRemoteAudioSSLCertificate.
1669
1670 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1671 ASSERT_TRUE(caller_remote_cert);
1672 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1673 caller_remote_cert->ToPEMString());
1674
1675 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1676 ASSERT_TRUE(callee_remote_cert);
1677 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1678 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001679
1680 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1681 ASSERT_TRUE(caller_remote_cert_chain);
1682 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1683 auto remote_cert = &caller_remote_cert_chain->Get(0);
1684 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1685 remote_cert->ToPEMString());
1686
1687 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1688 ASSERT_TRUE(callee_remote_cert_chain);
1689 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1690 remote_cert = &callee_remote_cert_chain->Get(0);
1691 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1692 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001693}
1694
deadbeef1dcb1642017-03-29 21:08:16 -07001695// This test sets up a call between two parties with a source resolution of
1696// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001697TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001698 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1699 ASSERT_TRUE(CreatePeerConnectionWrappers());
1700 ConnectFakeSignaling();
1701
Niels Möller5c7efe72018-05-11 10:34:46 +02001702 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1703 webrtc::FakePeriodicVideoSource::Config config;
1704 config.width = 1280;
1705 config.height = 720;
1706 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1707 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001708
1709 // Do normal offer/answer and wait for at least one frame to be received in
1710 // each direction.
1711 caller()->CreateAndSetAndSignalOffer();
1712 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1713 callee()->min_video_frames_received_per_track() > 0,
1714 kMaxWaitForFramesMs);
1715
1716 // Check rendered aspect ratio.
1717 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1718 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1719 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1720 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1721}
1722
1723// This test sets up an one-way call, with media only from caller to
1724// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001725TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001726 ASSERT_TRUE(CreatePeerConnectionWrappers());
1727 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001728 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001729 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001730 MediaExpectations media_expectations;
1731 media_expectations.CalleeExpectsSomeAudioAndVideo();
1732 media_expectations.CallerExpectsNoAudio();
1733 media_expectations.CallerExpectsNoVideo();
1734 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001735}
1736
1737// This test sets up a audio call initially, with the callee rejecting video
1738// initially. Then later the callee decides to upgrade to audio/video, and
1739// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001740TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001741 ASSERT_TRUE(CreatePeerConnectionWrappers());
1742 ConnectFakeSignaling();
1743 // Initially, offer an audio/video stream from the caller, but refuse to
1744 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001745 caller()->AddAudioVideoTracks();
1746 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001747 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1748 PeerConnectionInterface::RTCOfferAnswerOptions options;
1749 options.offer_to_receive_video = 0;
1750 callee()->SetOfferAnswerOptions(options);
1751 } else {
1752 callee()->SetRemoteOfferHandler([this] {
1753 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1754 });
1755 }
deadbeef1dcb1642017-03-29 21:08:16 -07001756 // Do offer/answer and make sure audio is still received end-to-end.
1757 caller()->CreateAndSetAndSignalOffer();
1758 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001759 {
1760 MediaExpectations media_expectations;
1761 media_expectations.ExpectBidirectionalAudio();
1762 media_expectations.ExpectNoVideo();
1763 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1764 }
deadbeef1dcb1642017-03-29 21:08:16 -07001765 // Sanity check that the callee's description has a rejected video section.
1766 ASSERT_NE(nullptr, callee()->pc()->local_description());
1767 const ContentInfo* callee_video_content =
1768 GetFirstVideoContent(callee()->pc()->local_description()->description());
1769 ASSERT_NE(nullptr, callee_video_content);
1770 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001771
deadbeef1dcb1642017-03-29 21:08:16 -07001772 // Now negotiate with video and ensure negotiation succeeds, with video
1773 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001774 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001775 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1776 PeerConnectionInterface::RTCOfferAnswerOptions options;
1777 options.offer_to_receive_video = 1;
1778 callee()->SetOfferAnswerOptions(options);
1779 } else {
1780 callee()->SetRemoteOfferHandler(nullptr);
1781 caller()->SetRemoteOfferHandler([this] {
1782 // The caller creates a new transceiver to receive video on when receiving
1783 // the offer, but by default it is send only.
1784 auto transceivers = caller()->pc()->GetTransceivers();
1785 ASSERT_EQ(3, transceivers.size());
1786 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1787 transceivers[2]->receiver()->media_type());
1788 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1789 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1790 });
1791 }
deadbeef1dcb1642017-03-29 21:08:16 -07001792 callee()->CreateAndSetAndSignalOffer();
1793 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001794 {
1795 // Expect additional audio frames to be received after the upgrade.
1796 MediaExpectations media_expectations;
1797 media_expectations.ExpectBidirectionalAudioAndVideo();
1798 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1799 }
deadbeef1dcb1642017-03-29 21:08:16 -07001800}
1801
deadbeef4389b4d2017-09-07 09:07:36 -07001802// Simpler than the above test; just add an audio track to an established
1803// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001804TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001805 ASSERT_TRUE(CreatePeerConnectionWrappers());
1806 ConnectFakeSignaling();
1807 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001808 caller()->AddVideoTrack();
1809 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001810 caller()->CreateAndSetAndSignalOffer();
1811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1812 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001813 caller()->AddAudioTrack();
1814 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001815 caller()->CreateAndSetAndSignalOffer();
1816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1817 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001818 MediaExpectations media_expectations;
1819 media_expectations.ExpectBidirectionalAudioAndVideo();
1820 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001821}
1822
deadbeef1dcb1642017-03-29 21:08:16 -07001823// This test sets up a call that's transferred to a new caller with a different
1824// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001825TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001826 ASSERT_TRUE(CreatePeerConnectionWrappers());
1827 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001828 caller()->AddAudioVideoTracks();
1829 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001830 caller()->CreateAndSetAndSignalOffer();
1831 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1832
1833 // Keep the original peer around which will still send packets to the
1834 // receiving client. These SRTP packets will be dropped.
1835 std::unique_ptr<PeerConnectionWrapper> original_peer(
1836 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001837 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001838 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1839 // directly above.
1840 original_peer->pc()->Close();
1841
1842 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001843 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001844 caller()->CreateAndSetAndSignalOffer();
1845 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1846 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001847 MediaExpectations media_expectations;
1848 media_expectations.ExpectBidirectionalAudioAndVideo();
1849 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001850}
1851
1852// This test sets up a call that's transferred to a new callee with a different
1853// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001854TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001855 ASSERT_TRUE(CreatePeerConnectionWrappers());
1856 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001857 caller()->AddAudioVideoTracks();
1858 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001859 caller()->CreateAndSetAndSignalOffer();
1860 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1861
1862 // Keep the original peer around which will still send packets to the
1863 // receiving client. These SRTP packets will be dropped.
1864 std::unique_ptr<PeerConnectionWrapper> original_peer(
1865 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001866 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001867 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1868 // directly above.
1869 original_peer->pc()->Close();
1870
1871 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001872 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001873 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1874 caller()->CreateAndSetAndSignalOffer();
1875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1876 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001877 MediaExpectations media_expectations;
1878 media_expectations.ExpectBidirectionalAudioAndVideo();
1879 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001880}
1881
1882// This test sets up a non-bundled call and negotiates bundling at the same
1883// time as starting an ICE restart. When bundling is in effect in the restart,
1884// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001885TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001886 ASSERT_TRUE(CreatePeerConnectionWrappers());
1887 ConnectFakeSignaling();
1888
Steve Anton15324772018-01-16 10:26:49 -08001889 caller()->AddAudioVideoTracks();
1890 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001891 // Remove the bundle group from the SDP received by the callee.
1892 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1893 desc->RemoveGroupByName("BUNDLE");
1894 });
1895 caller()->CreateAndSetAndSignalOffer();
1896 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001897 {
1898 MediaExpectations media_expectations;
1899 media_expectations.ExpectBidirectionalAudioAndVideo();
1900 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1901 }
deadbeef1dcb1642017-03-29 21:08:16 -07001902 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1903 callee()->SetReceivedSdpMunger(nullptr);
1904 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1905 caller()->CreateAndSetAndSignalOffer();
1906 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1907
1908 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001909 {
1910 MediaExpectations media_expectations;
1911 media_expectations.ExpectBidirectionalAudioAndVideo();
1912 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1913 }
deadbeef1dcb1642017-03-29 21:08:16 -07001914}
1915
1916// Test CVO (Coordination of Video Orientation). If a video source is rotated
1917// and both peers support the CVO RTP header extension, the actual video frames
1918// don't need to be encoded in different resolutions, since the rotation is
1919// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001920TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001921 ASSERT_TRUE(CreatePeerConnectionWrappers());
1922 ConnectFakeSignaling();
1923 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001924 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001925 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001926 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001927 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1928
1929 // Wait for video frames to be received by both sides.
1930 caller()->CreateAndSetAndSignalOffer();
1931 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1932 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1933 callee()->min_video_frames_received_per_track() > 0,
1934 kMaxWaitForFramesMs);
1935
1936 // Ensure that the aspect ratio is unmodified.
1937 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1938 // not just assumed.
1939 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1940 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1941 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1942 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1943 // Ensure that the CVO bits were surfaced to the renderer.
1944 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1945 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1946}
1947
1948// Test that when the CVO extension isn't supported, video is rotated the
1949// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001950TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001951 ASSERT_TRUE(CreatePeerConnectionWrappers());
1952 ConnectFakeSignaling();
1953 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001954 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001955 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001956 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001957 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1958
1959 // Remove the CVO extension from the offered SDP.
1960 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1961 cricket::VideoContentDescription* video =
1962 GetFirstVideoContentDescription(desc);
1963 video->ClearRtpHeaderExtensions();
1964 });
1965 // Wait for video frames to be received by both sides.
1966 caller()->CreateAndSetAndSignalOffer();
1967 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1968 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1969 callee()->min_video_frames_received_per_track() > 0,
1970 kMaxWaitForFramesMs);
1971
1972 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1973 // rotation.
1974 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1975 // not just assumed.
1976 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1977 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1978 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1979 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1980 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1981 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1982 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1983}
1984
deadbeef1dcb1642017-03-29 21:08:16 -07001985// Test that if the answerer rejects the audio m= section, no audio is sent or
1986// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001987TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001988 ASSERT_TRUE(CreatePeerConnectionWrappers());
1989 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001990 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001991 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1992 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1993 // it will reject the audio m= section completely.
1994 PeerConnectionInterface::RTCOfferAnswerOptions options;
1995 options.offer_to_receive_audio = 0;
1996 callee()->SetOfferAnswerOptions(options);
1997 } else {
1998 // Stopping the audio RtpTransceiver will cause the media section to be
1999 // rejected in the answer.
2000 callee()->SetRemoteOfferHandler([this] {
2001 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2002 });
2003 }
Steve Anton15324772018-01-16 10:26:49 -08002004 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002005 // Do offer/answer and wait for successful end-to-end video frames.
2006 caller()->CreateAndSetAndSignalOffer();
2007 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002008 MediaExpectations media_expectations;
2009 media_expectations.ExpectBidirectionalVideo();
2010 media_expectations.ExpectNoAudio();
2011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2012
deadbeef1dcb1642017-03-29 21:08:16 -07002013 // Sanity check that the callee's description has a rejected audio section.
2014 ASSERT_NE(nullptr, callee()->pc()->local_description());
2015 const ContentInfo* callee_audio_content =
2016 GetFirstAudioContent(callee()->pc()->local_description()->description());
2017 ASSERT_NE(nullptr, callee_audio_content);
2018 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002019 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2020 // The caller's transceiver should have stopped after receiving the answer.
2021 EXPECT_TRUE(caller()
2022 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2023 ->stopped());
2024 }
deadbeef1dcb1642017-03-29 21:08:16 -07002025}
2026
2027// Test that if the answerer rejects the video m= section, no video is sent or
2028// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002029TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002030 ASSERT_TRUE(CreatePeerConnectionWrappers());
2031 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002032 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002033 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2034 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2035 // it will reject the video m= section completely.
2036 PeerConnectionInterface::RTCOfferAnswerOptions options;
2037 options.offer_to_receive_video = 0;
2038 callee()->SetOfferAnswerOptions(options);
2039 } else {
2040 // Stopping the video RtpTransceiver will cause the media section to be
2041 // rejected in the answer.
2042 callee()->SetRemoteOfferHandler([this] {
2043 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2044 });
2045 }
Steve Anton15324772018-01-16 10:26:49 -08002046 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002047 // Do offer/answer and wait for successful end-to-end audio frames.
2048 caller()->CreateAndSetAndSignalOffer();
2049 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002050 MediaExpectations media_expectations;
2051 media_expectations.ExpectBidirectionalAudio();
2052 media_expectations.ExpectNoVideo();
2053 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2054
deadbeef1dcb1642017-03-29 21:08:16 -07002055 // Sanity check that the callee's description has a rejected video section.
2056 ASSERT_NE(nullptr, callee()->pc()->local_description());
2057 const ContentInfo* callee_video_content =
2058 GetFirstVideoContent(callee()->pc()->local_description()->description());
2059 ASSERT_NE(nullptr, callee_video_content);
2060 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002061 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2062 // The caller's transceiver should have stopped after receiving the answer.
2063 EXPECT_TRUE(caller()
2064 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2065 ->stopped());
2066 }
deadbeef1dcb1642017-03-29 21:08:16 -07002067}
2068
2069// Test that if the answerer rejects both audio and video m= sections, nothing
2070// bad happens.
2071// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2072// test anything but the fact that negotiation succeeds, which doesn't mean
2073// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002074TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002075 ASSERT_TRUE(CreatePeerConnectionWrappers());
2076 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002077 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002078 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2079 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2080 // will reject both audio and video m= sections.
2081 PeerConnectionInterface::RTCOfferAnswerOptions options;
2082 options.offer_to_receive_audio = 0;
2083 options.offer_to_receive_video = 0;
2084 callee()->SetOfferAnswerOptions(options);
2085 } else {
2086 callee()->SetRemoteOfferHandler([this] {
2087 // Stopping all transceivers will cause all media sections to be rejected.
2088 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2089 transceiver->Stop();
2090 }
2091 });
2092 }
deadbeef1dcb1642017-03-29 21:08:16 -07002093 // Do offer/answer and wait for stable signaling state.
2094 caller()->CreateAndSetAndSignalOffer();
2095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002096
deadbeef1dcb1642017-03-29 21:08:16 -07002097 // Sanity check that the callee's description has rejected m= sections.
2098 ASSERT_NE(nullptr, callee()->pc()->local_description());
2099 const ContentInfo* callee_audio_content =
2100 GetFirstAudioContent(callee()->pc()->local_description()->description());
2101 ASSERT_NE(nullptr, callee_audio_content);
2102 EXPECT_TRUE(callee_audio_content->rejected);
2103 const ContentInfo* callee_video_content =
2104 GetFirstVideoContent(callee()->pc()->local_description()->description());
2105 ASSERT_NE(nullptr, callee_video_content);
2106 EXPECT_TRUE(callee_video_content->rejected);
2107}
2108
2109// This test sets up an audio and video call between two parties. After the
2110// call runs for a while, the caller sends an updated offer with video being
2111// rejected. Once the re-negotiation is done, the video flow should stop and
2112// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002113TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002116 caller()->AddAudioVideoTracks();
2117 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002118 caller()->CreateAndSetAndSignalOffer();
2119 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002120 {
2121 MediaExpectations media_expectations;
2122 media_expectations.ExpectBidirectionalAudioAndVideo();
2123 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2124 }
deadbeef1dcb1642017-03-29 21:08:16 -07002125 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002126 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2127 caller()->SetGeneratedSdpMunger(
2128 [](cricket::SessionDescription* description) {
2129 for (cricket::ContentInfo& content : description->contents()) {
2130 if (cricket::IsVideoContent(&content)) {
2131 content.rejected = true;
2132 }
2133 }
2134 });
2135 } else {
2136 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2137 }
deadbeef1dcb1642017-03-29 21:08:16 -07002138 caller()->CreateAndSetAndSignalOffer();
2139 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2140
2141 // Sanity check that the caller's description has a rejected video section.
2142 ASSERT_NE(nullptr, caller()->pc()->local_description());
2143 const ContentInfo* caller_video_content =
2144 GetFirstVideoContent(caller()->pc()->local_description()->description());
2145 ASSERT_NE(nullptr, caller_video_content);
2146 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002147 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002148 {
2149 MediaExpectations media_expectations;
2150 media_expectations.ExpectBidirectionalAudio();
2151 media_expectations.ExpectNoVideo();
2152 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2153 }
deadbeef1dcb1642017-03-29 21:08:16 -07002154}
2155
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002156// Do one offer/answer with audio, another that disables it (rejecting the m=
2157// section), and another that re-enables it. Regression test for:
2158// bugs.webrtc.org/6023
2159TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2160 ASSERT_TRUE(CreatePeerConnectionWrappers());
2161 ConnectFakeSignaling();
2162
2163 // Add audio track, do normal offer/answer.
2164 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2165 caller()->CreateLocalAudioTrack();
2166 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2167 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2168 caller()->CreateAndSetAndSignalOffer();
2169 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2170
2171 // Remove audio track, and set offer_to_receive_audio to false to cause the
2172 // m= section to be completely disabled, not just "recvonly".
2173 caller()->pc()->RemoveTrack(sender);
2174 PeerConnectionInterface::RTCOfferAnswerOptions options;
2175 options.offer_to_receive_audio = 0;
2176 caller()->SetOfferAnswerOptions(options);
2177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2179
2180 // Add the audio track again, expecting negotiation to succeed and frames to
2181 // flow.
2182 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2183 options.offer_to_receive_audio = 1;
2184 caller()->SetOfferAnswerOptions(options);
2185 caller()->CreateAndSetAndSignalOffer();
2186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2187
2188 MediaExpectations media_expectations;
2189 media_expectations.CalleeExpectsSomeAudio();
2190 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2191}
2192
deadbeef1dcb1642017-03-29 21:08:16 -07002193// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2194// is needed to support legacy endpoints.
2195// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2196// add a test for an end-to-end test without MID signaling either (basically,
2197// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002198TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002199 ASSERT_TRUE(CreatePeerConnectionWrappers());
2200 ConnectFakeSignaling();
2201 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002202 caller()->AddAudioVideoTracks();
2203 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002204 // Remove SSRCs and MSIDs from the received offer SDP.
2205 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002206 caller()->CreateAndSetAndSignalOffer();
2207 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002208 MediaExpectations media_expectations;
2209 media_expectations.ExpectBidirectionalAudioAndVideo();
2210 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002211}
2212
Seth Hampson5897a6e2018-04-03 11:16:33 -07002213// Basic end-to-end test, without SSRC signaling. This means that the track
2214// was created properly and frames are delivered when the MSIDs are communicated
2215// with a=msid lines and no a=ssrc lines.
2216TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2217 EndToEndCallWithoutSsrcSignaling) {
2218 const char kStreamId[] = "streamId";
2219 ASSERT_TRUE(CreatePeerConnectionWrappers());
2220 ConnectFakeSignaling();
2221 // Add just audio tracks.
2222 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2223 callee()->AddAudioTrack();
2224
2225 // Remove SSRCs from the received offer SDP.
2226 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2227 caller()->CreateAndSetAndSignalOffer();
2228 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2229 MediaExpectations media_expectations;
2230 media_expectations.ExpectBidirectionalAudio();
2231 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2232}
2233
Steve Antondf527fd2018-04-27 15:52:03 -07002234// Tests that video flows between multiple video tracks when SSRCs are not
2235// signaled. This exercises the MID RTP header extension which is needed to
2236// demux the incoming video tracks.
2237TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2238 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2239 ASSERT_TRUE(CreatePeerConnectionWrappers());
2240 ConnectFakeSignaling();
2241 caller()->AddVideoTrack();
2242 caller()->AddVideoTrack();
2243 callee()->AddVideoTrack();
2244 callee()->AddVideoTrack();
2245
2246 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2247 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2248 caller()->CreateAndSetAndSignalOffer();
2249 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2250 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2251 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2252
2253 // Expect video to be received in both directions on both tracks.
2254 MediaExpectations media_expectations;
2255 media_expectations.ExpectBidirectionalVideo();
2256 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2257}
2258
deadbeef1dcb1642017-03-29 21:08:16 -07002259// Test that if two video tracks are sent (from caller to callee, in this test),
2260// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002261TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002262 ASSERT_TRUE(CreatePeerConnectionWrappers());
2263 ConnectFakeSignaling();
2264 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002265 caller()->AddAudioVideoTracks();
2266 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002267 caller()->CreateAndSetAndSignalOffer();
2268 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002269 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002270
2271 MediaExpectations media_expectations;
2272 media_expectations.CalleeExpectsSomeAudioAndVideo();
2273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002274}
2275
2276static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2277 bool first = true;
2278 for (cricket::ContentInfo& content : desc->contents()) {
2279 if (first) {
2280 first = false;
2281 continue;
2282 }
2283 content.bundle_only = true;
2284 }
2285 first = true;
2286 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2287 if (first) {
2288 first = false;
2289 continue;
2290 }
2291 transport.description.ice_ufrag.clear();
2292 transport.description.ice_pwd.clear();
2293 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2294 transport.description.identity_fingerprint.reset(nullptr);
2295 }
2296}
2297
2298// Test that if applying a true "max bundle" offer, which uses ports of 0,
2299// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2300// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2301// successfully and media flows.
2302// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2303// TODO(deadbeef): Won't need this test once we start generating actual
2304// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002305TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002306 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2307 ASSERT_TRUE(CreatePeerConnectionWrappers());
2308 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002309 caller()->AddAudioVideoTracks();
2310 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002311 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2312 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2313 // but the first m= section.
2314 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2315 caller()->CreateAndSetAndSignalOffer();
2316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002317 MediaExpectations media_expectations;
2318 media_expectations.ExpectBidirectionalAudioAndVideo();
2319 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002320}
2321
2322// Test that we can receive the audio output level from a remote audio track.
2323// TODO(deadbeef): Use a fake audio source and verify that the output level is
2324// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002325TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002326 ASSERT_TRUE(CreatePeerConnectionWrappers());
2327 ConnectFakeSignaling();
2328 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002329 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002330 caller()->CreateAndSetAndSignalOffer();
2331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2332
2333 // Get the audio output level stats. Note that the level is not available
2334 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002335 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002336 kMaxWaitForFramesMs);
2337}
2338
2339// Test that an audio input level is reported.
2340// TODO(deadbeef): Use a fake audio source and verify that the input level is
2341// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002342TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002343 ASSERT_TRUE(CreatePeerConnectionWrappers());
2344 ConnectFakeSignaling();
2345 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002346 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002347 caller()->CreateAndSetAndSignalOffer();
2348 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2349
2350 // Get the audio input level stats. The level should be available very
2351 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002352 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002353 kMaxWaitForStatsMs);
2354}
2355
2356// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002357TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002358 ASSERT_TRUE(CreatePeerConnectionWrappers());
2359 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002360 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002361 // Do offer/answer, wait for the callee to receive some frames.
2362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002364
2365 MediaExpectations media_expectations;
2366 media_expectations.CalleeExpectsSomeAudioAndVideo();
2367 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002368
2369 // Get a handle to the remote tracks created, so they can be used as GetStats
2370 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002371 for (auto receiver : callee()->pc()->GetReceivers()) {
2372 // We received frames, so we definitely should have nonzero "received bytes"
2373 // stats at this point.
2374 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2375 0);
2376 }
deadbeef1dcb1642017-03-29 21:08:16 -07002377}
2378
2379// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002380TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002381 ASSERT_TRUE(CreatePeerConnectionWrappers());
2382 ConnectFakeSignaling();
2383 auto audio_track = caller()->CreateLocalAudioTrack();
2384 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002385 caller()->AddTrack(audio_track);
2386 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002387 // Do offer/answer, wait for the callee to receive some frames.
2388 caller()->CreateAndSetAndSignalOffer();
2389 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002390 MediaExpectations media_expectations;
2391 media_expectations.CalleeExpectsSomeAudioAndVideo();
2392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002393
2394 // The callee received frames, so we definitely should have nonzero "sent
2395 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002396 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2397 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2398}
2399
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002400// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002401TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002402 ASSERT_TRUE(CreatePeerConnectionWrappers());
2403 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002404 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002405
Steve Anton15324772018-01-16 10:26:49 -08002406 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002407
2408 // Do offer/answer, wait for the callee to receive some frames.
2409 caller()->CreateAndSetAndSignalOffer();
2410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2411
2412 // Get the remote audio track created on the receiver, so they can be used as
2413 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002414 auto receivers = callee()->pc()->GetReceivers();
2415 ASSERT_EQ(1u, receivers.size());
2416 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002417
2418 // Get the audio output level stats. Note that the level is not available
2419 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002420 EXPECT_TRUE_WAIT(
2421 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2422 0,
2423 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002424}
2425
deadbeefd8ad7882017-04-18 16:01:17 -07002426// Test that we can get stats (using the new stats implemnetation) for
2427// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2428// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002429TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002430 GetStatsForUnsignaledStreamWithNewStatsApi) {
2431 ASSERT_TRUE(CreatePeerConnectionWrappers());
2432 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002433 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002434 // Remove SSRCs and MSIDs from the received offer SDP.
2435 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2436 caller()->CreateAndSetAndSignalOffer();
2437 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002438 MediaExpectations media_expectations;
2439 media_expectations.CalleeExpectsSomeAudio(1);
2440 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002441
2442 // We received a frame, so we should have nonzero "bytes received" stats for
2443 // the unsignaled stream, if stats are working for it.
2444 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2445 callee()->NewGetStats();
2446 ASSERT_NE(nullptr, report);
2447 auto inbound_stream_stats =
2448 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2449 ASSERT_EQ(1U, inbound_stream_stats.size());
2450 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2451 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002452 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2453}
2454
2455// Test that we can successfully get the media related stats (audio level
2456// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002457TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002458 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2459 ASSERT_TRUE(CreatePeerConnectionWrappers());
2460 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002461 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002462 // Remove SSRCs and MSIDs from the received offer SDP.
2463 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2464 caller()->CreateAndSetAndSignalOffer();
2465 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002466 MediaExpectations media_expectations;
2467 media_expectations.CalleeExpectsSomeAudio(1);
2468 media_expectations.CalleeExpectsSomeVideo(1);
2469 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002470
2471 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2472 callee()->NewGetStats();
2473 ASSERT_NE(nullptr, report);
2474
2475 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2476 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2477 ASSERT_GE(audio_index, 0);
2478 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002479}
2480
deadbeef4e2deab2017-09-20 13:56:21 -07002481// Helper for test below.
2482void ModifySsrcs(cricket::SessionDescription* desc) {
2483 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002484 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002485 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002486 for (uint32_t& ssrc : stream.ssrcs) {
2487 ssrc = rtc::CreateRandomId();
2488 }
2489 }
2490 }
2491}
2492
2493// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2494// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2495// This should result in two "RTCInboundRTPStreamStats", but only one
2496// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2497// being reset to 0 once the SSRC change occurs.
2498//
2499// Regression test for this bug:
2500// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2501//
2502// The bug causes the track stats to only represent one of the two streams:
2503// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2504// that the track stat counters would reset to 0 when the new stream is
2505// received, and a 50% chance that they'll stop updating (while
2506// "concealed_samples" continues increasing, due to silence being generated for
2507// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002508TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002509 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002510 ASSERT_TRUE(CreatePeerConnectionWrappers());
2511 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002512 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002513 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2514 // that doesn't signal SSRCs (from the callee's perspective).
2515 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2516 caller()->CreateAndSetAndSignalOffer();
2517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2518 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519 {
2520 MediaExpectations media_expectations;
2521 media_expectations.CalleeExpectsSomeAudio(50);
2522 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2523 }
deadbeef4e2deab2017-09-20 13:56:21 -07002524 // Some audio frames were received, so we should have nonzero "samples
2525 // received" for the track.
2526 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2527 callee()->NewGetStats();
2528 ASSERT_NE(nullptr, report);
2529 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2530 ASSERT_EQ(1U, track_stats.size());
2531 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2532 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2533 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2534
2535 // Create a new offer and munge it to cause the caller to use a new SSRC.
2536 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2537 caller()->CreateAndSetAndSignalOffer();
2538 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2539 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2540 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002541 {
2542 MediaExpectations media_expectations;
2543 media_expectations.CalleeExpectsSomeAudio(25);
2544 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2545 }
deadbeef4e2deab2017-09-20 13:56:21 -07002546
2547 report = callee()->NewGetStats();
2548 ASSERT_NE(nullptr, report);
2549 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2550 ASSERT_EQ(1U, track_stats.size());
2551 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2552 // The "total samples received" stat should only be greater than it was
2553 // before.
2554 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2555 // Right now, the new SSRC will cause the counters to reset to 0.
2556 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2557
2558 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002559 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002560 // good sign that we're seeing stats from the old stream that's no longer
2561 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002562 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002563 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2564 EXPECT_LT(*track_stats[0]->concealed_samples,
2565 *track_stats[0]->total_samples_received *
2566 kAcceptableConcealedSamplesPercentage);
2567
2568 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2569 // sanity check that the SSRC really changed.
2570 // TODO(deadbeef): This isn't working right now, because we're not returning
2571 // *any* stats for the inactive stream. Uncomment when the bug is completely
2572 // fixed.
2573 // auto inbound_stream_stats =
2574 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2575 // ASSERT_EQ(2U, inbound_stream_stats.size());
2576}
2577
deadbeef1dcb1642017-03-29 21:08:16 -07002578// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002579TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002580 PeerConnectionFactory::Options dtls_10_options;
2581 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2582 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2583 dtls_10_options));
2584 ConnectFakeSignaling();
2585 // Do normal offer/answer and wait for some frames to be received in each
2586 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002587 caller()->AddAudioVideoTracks();
2588 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002589 caller()->CreateAndSetAndSignalOffer();
2590 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002591 MediaExpectations media_expectations;
2592 media_expectations.ExpectBidirectionalAudioAndVideo();
2593 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002594}
2595
2596// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002597TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002598 PeerConnectionFactory::Options dtls_10_options;
2599 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2600 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2601 dtls_10_options));
2602 ConnectFakeSignaling();
2603 // Register UMA observer before signaling begins.
2604 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2605 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2606 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002607 caller()->AddAudioVideoTracks();
2608 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002609 caller()->CreateAndSetAndSignalOffer();
2610 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2611 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002612 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002613 kDefaultTimeout);
2614 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002615 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002616 EXPECT_EQ(1,
2617 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2618 kDefaultSrtpCryptoSuite));
2619}
2620
2621// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002622TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002623 PeerConnectionFactory::Options dtls_12_options;
2624 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2625 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2626 dtls_12_options));
2627 ConnectFakeSignaling();
2628 // Register UMA observer before signaling begins.
2629 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2630 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2631 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002632 caller()->AddAudioVideoTracks();
2633 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002634 caller()->CreateAndSetAndSignalOffer();
2635 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2636 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002637 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002638 kDefaultTimeout);
2639 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002640 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002641 EXPECT_EQ(1,
2642 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2643 kDefaultSrtpCryptoSuite));
2644}
2645
2646// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2647// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002648TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002649 PeerConnectionFactory::Options caller_options;
2650 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2651 PeerConnectionFactory::Options callee_options;
2652 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2653 ASSERT_TRUE(
2654 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2655 ConnectFakeSignaling();
2656 // Do normal offer/answer and wait for some frames to be received in each
2657 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002658 caller()->AddAudioVideoTracks();
2659 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002660 caller()->CreateAndSetAndSignalOffer();
2661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002662 MediaExpectations media_expectations;
2663 media_expectations.ExpectBidirectionalAudioAndVideo();
2664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002665}
2666
2667// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2668// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002669TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002670 PeerConnectionFactory::Options caller_options;
2671 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2672 PeerConnectionFactory::Options callee_options;
2673 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2674 ASSERT_TRUE(
2675 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2676 ConnectFakeSignaling();
2677 // Do normal offer/answer and wait for some frames to be received in each
2678 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002679 caller()->AddAudioVideoTracks();
2680 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002681 caller()->CreateAndSetAndSignalOffer();
2682 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002683 MediaExpectations media_expectations;
2684 media_expectations.ExpectBidirectionalAudioAndVideo();
2685 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002686}
2687
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002688// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2689// works as expected; the cipher should only be used if enabled by both sides.
2690TEST_P(PeerConnectionIntegrationTest,
2691 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2692 PeerConnectionFactory::Options caller_options;
2693 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2694 PeerConnectionFactory::Options callee_options;
2695 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2696 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2697 TestNegotiatedCipherSuite(caller_options, callee_options,
2698 expected_cipher_suite);
2699}
2700
2701TEST_P(PeerConnectionIntegrationTest,
2702 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2703 PeerConnectionFactory::Options caller_options;
2704 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2705 PeerConnectionFactory::Options callee_options;
2706 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2707 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2708 TestNegotiatedCipherSuite(caller_options, callee_options,
2709 expected_cipher_suite);
2710}
2711
2712TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2713 PeerConnectionFactory::Options caller_options;
2714 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2715 PeerConnectionFactory::Options callee_options;
2716 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2717 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2718 TestNegotiatedCipherSuite(caller_options, callee_options,
2719 expected_cipher_suite);
2720}
2721
deadbeef1dcb1642017-03-29 21:08:16 -07002722// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002723TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002724 bool local_gcm_enabled = false;
2725 bool remote_gcm_enabled = false;
2726 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2727 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2728 expected_cipher_suite);
2729}
2730
2731// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002732TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002733 bool local_gcm_enabled = true;
2734 bool remote_gcm_enabled = true;
2735 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2736 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2737 expected_cipher_suite);
2738}
2739
2740// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002741TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002742 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2743 bool local_gcm_enabled = true;
2744 bool remote_gcm_enabled = false;
2745 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2746 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2747 expected_cipher_suite);
2748}
2749
2750// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002751TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002752 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2753 bool local_gcm_enabled = false;
2754 bool remote_gcm_enabled = true;
2755 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2756 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2757 expected_cipher_suite);
2758}
2759
deadbeef7914b8c2017-04-21 03:23:33 -07002760// Verify that media can be transmitted end-to-end when GCM crypto suites are
2761// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2762// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2763// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002764TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002765 PeerConnectionFactory::Options gcm_options;
2766 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2767 ASSERT_TRUE(
2768 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2769 ConnectFakeSignaling();
2770 // Do normal offer/answer and wait for some frames to be received in each
2771 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002772 caller()->AddAudioVideoTracks();
2773 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002774 caller()->CreateAndSetAndSignalOffer();
2775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002776 MediaExpectations media_expectations;
2777 media_expectations.ExpectBidirectionalAudioAndVideo();
2778 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002779}
2780
deadbeef1dcb1642017-03-29 21:08:16 -07002781// This test sets up a call between two parties with audio, video and an RTP
2782// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002783TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002784 FakeConstraints setup_constraints;
2785 setup_constraints.SetAllowRtpDataChannels();
2786 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2787 &setup_constraints));
2788 ConnectFakeSignaling();
2789 // Expect that data channel created on caller side will show up for callee as
2790 // well.
2791 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002792 caller()->AddAudioVideoTracks();
2793 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002794 caller()->CreateAndSetAndSignalOffer();
2795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2796 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002797 MediaExpectations media_expectations;
2798 media_expectations.ExpectBidirectionalAudioAndVideo();
2799 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002800 ASSERT_NE(nullptr, caller()->data_channel());
2801 ASSERT_NE(nullptr, callee()->data_channel());
2802 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2803 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2804
2805 // Ensure data can be sent in both directions.
2806 std::string data = "hello world";
2807 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2808 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2809 kDefaultTimeout);
2810 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2811 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2812 kDefaultTimeout);
2813}
2814
2815// Ensure that an RTP data channel is signaled as closed for the caller when
2816// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002817TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002818 RtpDataChannelSignaledClosedInCalleeOffer) {
2819 // Same procedure as above test.
2820 FakeConstraints setup_constraints;
2821 setup_constraints.SetAllowRtpDataChannels();
2822 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2823 &setup_constraints));
2824 ConnectFakeSignaling();
2825 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002826 caller()->AddAudioVideoTracks();
2827 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002828 caller()->CreateAndSetAndSignalOffer();
2829 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2830 ASSERT_NE(nullptr, caller()->data_channel());
2831 ASSERT_NE(nullptr, callee()->data_channel());
2832 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2833 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2834
2835 // Close the data channel on the callee, and do an updated offer/answer.
2836 callee()->data_channel()->Close();
2837 callee()->CreateAndSetAndSignalOffer();
2838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2839 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2840 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2841}
2842
2843// Tests that data is buffered in an RTP data channel until an observer is
2844// registered for it.
2845//
2846// NOTE: RTP data channels can receive data before the underlying
2847// transport has detected that a channel is writable and thus data can be
2848// received before the data channel state changes to open. That is hard to test
2849// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002850TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002851 DataBufferedUntilRtpDataChannelObserverRegistered) {
2852 // Use fake clock and simulated network delay so that we predictably can wait
2853 // until an SCTP message has been delivered without "sleep()"ing.
2854 rtc::ScopedFakeClock fake_clock;
2855 // Some things use a time of "0" as a special value, so we need to start out
2856 // the fake clock at a nonzero time.
2857 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02002858 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07002859 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2860 virtual_socket_server()->UpdateDelayDistribution();
2861
2862 FakeConstraints constraints;
2863 constraints.SetAllowRtpDataChannels();
2864 ASSERT_TRUE(
2865 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2866 ConnectFakeSignaling();
2867 caller()->CreateDataChannel();
2868 caller()->CreateAndSetAndSignalOffer();
2869 ASSERT_TRUE(caller()->data_channel() != nullptr);
2870 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2871 kDefaultTimeout, fake_clock);
2872 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2873 kDefaultTimeout, fake_clock);
2874 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2875 callee()->data_channel()->state(), kDefaultTimeout,
2876 fake_clock);
2877
2878 // Unregister the observer which is normally automatically registered.
2879 callee()->data_channel()->UnregisterObserver();
2880 // Send data and advance fake clock until it should have been received.
2881 std::string data = "hello world";
2882 caller()->data_channel()->Send(DataBuffer(data));
2883 SIMULATED_WAIT(false, 50, fake_clock);
2884
2885 // Attach data channel and expect data to be received immediately. Note that
2886 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2887 // further, but data can be received even if the callback is asynchronous.
2888 MockDataChannelObserver new_observer(callee()->data_channel());
2889 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2890 fake_clock);
2891}
2892
2893// This test sets up a call between two parties with audio, video and but only
2894// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002895TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002896 FakeConstraints setup_constraints_1;
2897 setup_constraints_1.SetAllowRtpDataChannels();
2898 // Must disable DTLS to make negotiation succeed.
2899 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2900 false);
2901 FakeConstraints setup_constraints_2;
2902 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2903 false);
2904 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2905 &setup_constraints_1, &setup_constraints_2));
2906 ConnectFakeSignaling();
2907 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002908 caller()->AddAudioVideoTracks();
2909 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002910 caller()->CreateAndSetAndSignalOffer();
2911 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2912 // The caller should still have a data channel, but it should be closed, and
2913 // one should ever have been created for the callee.
2914 EXPECT_TRUE(caller()->data_channel() != nullptr);
2915 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2916 EXPECT_EQ(nullptr, callee()->data_channel());
2917}
2918
2919// This test sets up a call between two parties with audio, and video. When
2920// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002921TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002922 FakeConstraints setup_constraints;
2923 setup_constraints.SetAllowRtpDataChannels();
2924 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2925 &setup_constraints));
2926 ConnectFakeSignaling();
2927 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002928 caller()->AddAudioVideoTracks();
2929 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002930 caller()->CreateAndSetAndSignalOffer();
2931 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2932 // Create data channel and do new offer and answer.
2933 caller()->CreateDataChannel();
2934 caller()->CreateAndSetAndSignalOffer();
2935 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2936 ASSERT_NE(nullptr, caller()->data_channel());
2937 ASSERT_NE(nullptr, callee()->data_channel());
2938 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2939 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2940 // Ensure data can be sent in both directions.
2941 std::string data = "hello world";
2942 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2943 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2944 kDefaultTimeout);
2945 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2946 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2947 kDefaultTimeout);
2948}
2949
2950#ifdef HAVE_SCTP
2951
2952// This test sets up a call between two parties with audio, video and an SCTP
2953// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002954TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002955 ASSERT_TRUE(CreatePeerConnectionWrappers());
2956 ConnectFakeSignaling();
2957 // Expect that data channel created on caller side will show up for callee as
2958 // well.
2959 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002960 caller()->AddAudioVideoTracks();
2961 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002962 caller()->CreateAndSetAndSignalOffer();
2963 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2964 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002965 MediaExpectations media_expectations;
2966 media_expectations.ExpectBidirectionalAudioAndVideo();
2967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002968 // Caller data channel should already exist (it created one). Callee data
2969 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2970 ASSERT_NE(nullptr, caller()->data_channel());
2971 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2972 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2973 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2974
2975 // Ensure data can be sent in both directions.
2976 std::string data = "hello world";
2977 caller()->data_channel()->Send(DataBuffer(data));
2978 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2979 kDefaultTimeout);
2980 callee()->data_channel()->Send(DataBuffer(data));
2981 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2982 kDefaultTimeout);
2983}
2984
2985// Ensure that when the callee closes an SCTP data channel, the closing
2986// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002987TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002988 // Same procedure as above test.
2989 ASSERT_TRUE(CreatePeerConnectionWrappers());
2990 ConnectFakeSignaling();
2991 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002992 caller()->AddAudioVideoTracks();
2993 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002994 caller()->CreateAndSetAndSignalOffer();
2995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2996 ASSERT_NE(nullptr, caller()->data_channel());
2997 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2998 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2999 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3000
3001 // Close the data channel on the callee side, and wait for it to reach the
3002 // "closed" state on both sides.
3003 callee()->data_channel()->Close();
3004 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3005 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3006}
3007
Seth Hampson2f0d7022018-02-20 11:54:42 -08003008TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003009 ASSERT_TRUE(CreatePeerConnectionWrappers());
3010 ConnectFakeSignaling();
3011 webrtc::DataChannelInit init;
3012 init.id = 53;
3013 init.maxRetransmits = 52;
3014 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003015 caller()->AddAudioVideoTracks();
3016 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003017 caller()->CreateAndSetAndSignalOffer();
3018 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003019 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3020 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003021 EXPECT_EQ(init.id, callee()->data_channel()->id());
3022 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3023 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3024 EXPECT_FALSE(callee()->data_channel()->negotiated());
3025}
3026
deadbeef1dcb1642017-03-29 21:08:16 -07003027// Test usrsctp's ability to process unordered data stream, where data actually
3028// arrives out of order using simulated delays. Previously there have been some
3029// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003030TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003031 // Introduce random network delays.
3032 // Otherwise it's not a true "unordered" test.
3033 virtual_socket_server()->set_delay_mean(20);
3034 virtual_socket_server()->set_delay_stddev(5);
3035 virtual_socket_server()->UpdateDelayDistribution();
3036 // Normal procedure, but with unordered data channel config.
3037 ASSERT_TRUE(CreatePeerConnectionWrappers());
3038 ConnectFakeSignaling();
3039 webrtc::DataChannelInit init;
3040 init.ordered = false;
3041 caller()->CreateDataChannel(&init);
3042 caller()->CreateAndSetAndSignalOffer();
3043 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3044 ASSERT_NE(nullptr, caller()->data_channel());
3045 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3046 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3047 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3048
3049 static constexpr int kNumMessages = 100;
3050 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3051 static constexpr size_t kMaxMessageSize = 4096;
3052 // Create and send random messages.
3053 std::vector<std::string> sent_messages;
3054 for (int i = 0; i < kNumMessages; ++i) {
3055 size_t length =
3056 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3057 std::string message;
3058 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3059 caller()->data_channel()->Send(DataBuffer(message));
3060 callee()->data_channel()->Send(DataBuffer(message));
3061 sent_messages.push_back(message);
3062 }
3063
3064 // Wait for all messages to be received.
3065 EXPECT_EQ_WAIT(kNumMessages,
3066 caller()->data_observer()->received_message_count(),
3067 kDefaultTimeout);
3068 EXPECT_EQ_WAIT(kNumMessages,
3069 callee()->data_observer()->received_message_count(),
3070 kDefaultTimeout);
3071
3072 // Sort and compare to make sure none of the messages were corrupted.
3073 std::vector<std::string> caller_received_messages =
3074 caller()->data_observer()->messages();
3075 std::vector<std::string> callee_received_messages =
3076 callee()->data_observer()->messages();
3077 std::sort(sent_messages.begin(), sent_messages.end());
3078 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3079 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3080 EXPECT_EQ(sent_messages, caller_received_messages);
3081 EXPECT_EQ(sent_messages, callee_received_messages);
3082}
3083
3084// This test sets up a call between two parties with audio, and video. When
3085// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003086TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003087 ASSERT_TRUE(CreatePeerConnectionWrappers());
3088 ConnectFakeSignaling();
3089 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003090 caller()->AddAudioVideoTracks();
3091 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003092 caller()->CreateAndSetAndSignalOffer();
3093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3094 // Create data channel and do new offer and answer.
3095 caller()->CreateDataChannel();
3096 caller()->CreateAndSetAndSignalOffer();
3097 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3098 // Caller data channel should already exist (it created one). Callee data
3099 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3100 ASSERT_NE(nullptr, caller()->data_channel());
3101 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3102 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3103 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3104 // Ensure data can be sent in both directions.
3105 std::string data = "hello world";
3106 caller()->data_channel()->Send(DataBuffer(data));
3107 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3108 kDefaultTimeout);
3109 callee()->data_channel()->Send(DataBuffer(data));
3110 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3111 kDefaultTimeout);
3112}
3113
deadbeef7914b8c2017-04-21 03:23:33 -07003114// Set up a connection initially just using SCTP data channels, later upgrading
3115// to audio/video, ensuring frames are received end-to-end. Effectively the
3116// inverse of the test above.
3117// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003118TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003119 ASSERT_TRUE(CreatePeerConnectionWrappers());
3120 ConnectFakeSignaling();
3121 // Do initial offer/answer with just data channel.
3122 caller()->CreateDataChannel();
3123 caller()->CreateAndSetAndSignalOffer();
3124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3125 // Wait until data can be sent over the data channel.
3126 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3127 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3128 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3129
3130 // Do subsequent offer/answer with two-way audio and video. Audio and video
3131 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003132 caller()->AddAudioVideoTracks();
3133 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003134 caller()->CreateAndSetAndSignalOffer();
3135 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003136 MediaExpectations media_expectations;
3137 media_expectations.ExpectBidirectionalAudioAndVideo();
3138 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003139}
3140
deadbeef8b7e9ad2017-05-25 09:38:55 -07003141static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003142 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003143 GetFirstDataContentDescription(desc);
3144 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003145 dcd_offer->set_use_sctpmap(false);
3146 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3147}
3148
3149// Test that the data channel works when a spec-compliant SCTP m= section is
3150// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3151// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003152TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003153 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3154 ASSERT_TRUE(CreatePeerConnectionWrappers());
3155 ConnectFakeSignaling();
3156 caller()->CreateDataChannel();
3157 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3158 caller()->CreateAndSetAndSignalOffer();
3159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3160 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3161 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3162 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3163
3164 // Ensure data can be sent in both directions.
3165 std::string data = "hello world";
3166 caller()->data_channel()->Send(DataBuffer(data));
3167 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3168 kDefaultTimeout);
3169 callee()->data_channel()->Send(DataBuffer(data));
3170 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3171 kDefaultTimeout);
3172}
3173
deadbeef1dcb1642017-03-29 21:08:16 -07003174#endif // HAVE_SCTP
3175
3176// Test that the ICE connection and gathering states eventually reach
3177// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003178TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003179 ASSERT_TRUE(CreatePeerConnectionWrappers());
3180 ConnectFakeSignaling();
3181 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003182 caller()->AddAudioVideoTracks();
3183 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003184 caller()->CreateAndSetAndSignalOffer();
3185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3186 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3187 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3188 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3189 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3190 // After the best candidate pair is selected and all candidates are signaled,
3191 // the ICE connection state should reach "complete".
3192 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3193 // answerer/"callee" by default) only reaches "connected". When this is
3194 // fixed, this test should be updated.
3195 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3196 caller()->ice_connection_state(), kDefaultTimeout);
3197 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3198 callee()->ice_connection_state(), kDefaultTimeout);
3199}
3200
Steve Antonede9ca52017-10-16 13:04:27 -07003201// Test that firewalling the ICE connection causes the clients to identify the
3202// disconnected state and then removing the firewall causes them to reconnect.
3203class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003204 : public PeerConnectionIntegrationBaseTest,
3205 public ::testing::WithParamInterface<
3206 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003207 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003208 PeerConnectionIntegrationIceStatesTest()
3209 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3210 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003211 }
3212
3213 void StartStunServer(const SocketAddress& server_address) {
3214 stun_server_.reset(
3215 cricket::TestStunServer::Create(network_thread(), server_address));
3216 }
3217
3218 bool TestIPv6() {
3219 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3220 }
3221
3222 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003223 network_thread()->Invoke<void>(
3224 RTC_FROM_HERE,
3225 rtc::Bind(&cricket::PortAllocator::set_flags,
3226 caller()->port_allocator(), port_allocator_flags_));
3227 network_thread()->Invoke<void>(
3228 RTC_FROM_HERE,
3229 rtc::Bind(&cricket::PortAllocator::set_flags,
3230 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003231 }
3232
3233 std::vector<SocketAddress> CallerAddresses() {
3234 std::vector<SocketAddress> addresses;
3235 addresses.push_back(SocketAddress("1.1.1.1", 0));
3236 if (TestIPv6()) {
3237 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3238 }
3239 return addresses;
3240 }
3241
3242 std::vector<SocketAddress> CalleeAddresses() {
3243 std::vector<SocketAddress> addresses;
3244 addresses.push_back(SocketAddress("2.2.2.2", 0));
3245 if (TestIPv6()) {
3246 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3247 }
3248 return addresses;
3249 }
3250
3251 void SetUpNetworkInterfaces() {
3252 // Remove the default interfaces added by the test infrastructure.
3253 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3254 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3255
3256 // Add network addresses for test.
3257 for (const auto& caller_address : CallerAddresses()) {
3258 caller()->network()->AddInterface(caller_address);
3259 }
3260 for (const auto& callee_address : CalleeAddresses()) {
3261 callee()->network()->AddInterface(callee_address);
3262 }
3263 }
3264
3265 private:
3266 uint32_t port_allocator_flags_;
3267 std::unique_ptr<cricket::TestStunServer> stun_server_;
3268};
3269
3270// Tests that the PeerConnection goes through all the ICE gathering/connection
3271// states over the duration of the call. This includes Disconnected and Failed
3272// states, induced by putting a firewall between the peers and waiting for them
3273// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003274TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3275 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3276 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3277 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003278
3279 const SocketAddress kStunServerAddress =
3280 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3281 StartStunServer(kStunServerAddress);
3282
3283 PeerConnectionInterface::RTCConfiguration config;
3284 PeerConnectionInterface::IceServer ice_stun_server;
3285 ice_stun_server.urls.push_back(
3286 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3287 kStunServerAddress.PortAsString());
3288 config.servers.push_back(ice_stun_server);
3289
3290 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3291 ConnectFakeSignaling();
3292 SetPortAllocatorFlags();
3293 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003294 caller()->AddAudioVideoTracks();
3295 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003296
3297 // Initial state before anything happens.
3298 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3299 caller()->ice_gathering_state());
3300 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3301 caller()->ice_connection_state());
3302
3303 // Start the call by creating the offer, setting it as the local description,
3304 // then sending it to the peer who will respond with an answer. This happens
3305 // asynchronously so that we can watch the states as it runs in the
3306 // background.
3307 caller()->CreateAndSetAndSignalOffer();
3308
Steve Anton83119dd2017-11-10 16:19:52 -08003309 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3310 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003311
3312 // Verify that the observer was notified of the intermediate transitions.
3313 EXPECT_THAT(caller()->ice_connection_state_history(),
3314 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3315 PeerConnectionInterface::kIceConnectionConnected,
3316 PeerConnectionInterface::kIceConnectionCompleted));
3317 EXPECT_THAT(caller()->ice_gathering_state_history(),
3318 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3319 PeerConnectionInterface::kIceGatheringComplete));
3320
3321 // Block connections to/from the caller and wait for ICE to become
3322 // disconnected.
3323 for (const auto& caller_address : CallerAddresses()) {
3324 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3325 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003326 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003327 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3328 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003329
3330 // Let ICE re-establish by removing the firewall rules.
3331 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003332 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003333 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3334 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003335
3336 // According to RFC7675, if there is no response within 30 seconds then the
3337 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003338 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003339 constexpr int kConsentTimeout = 30000;
3340 for (const auto& caller_address : CallerAddresses()) {
3341 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3342 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003343 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003344 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3345 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003346}
3347
3348// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3349// and that the statistics in the metric observers are updated correctly.
3350TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3351 ASSERT_TRUE(CreatePeerConnectionWrappers());
3352 ConnectFakeSignaling();
3353 SetPortAllocatorFlags();
3354 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003355 caller()->AddAudioVideoTracks();
3356 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003357
3358 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3359 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3360 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3361
3362 caller()->CreateAndSetAndSignalOffer();
3363
3364 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3365
3366 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3367 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3368 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3369 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3370 if (TestIPv6()) {
3371 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3372 // connection.
3373 EXPECT_EQ(0u, num_best_ipv4);
3374 EXPECT_EQ(1u, num_best_ipv6);
3375 } else {
3376 EXPECT_EQ(1u, num_best_ipv4);
3377 EXPECT_EQ(0u, num_best_ipv6);
3378 }
3379
3380 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3381 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3382 webrtc::kIceCandidatePairHostHost));
3383 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3384 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3385 webrtc::kIceCandidatePairHostPublicHostPublic));
3386}
3387
3388constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3389 cricket::PORTALLOCATOR_DISABLE_STUN |
3390 cricket::PORTALLOCATOR_DISABLE_RELAY;
3391constexpr uint32_t kFlagsIPv6NoStun =
3392 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3393 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3394constexpr uint32_t kFlagsIPv4Stun =
3395 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3396
Seth Hampson2f0d7022018-02-20 11:54:42 -08003397INSTANTIATE_TEST_CASE_P(
3398 PeerConnectionIntegrationTest,
3399 PeerConnectionIntegrationIceStatesTest,
3400 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3401 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3402 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3403 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003404
deadbeef1dcb1642017-03-29 21:08:16 -07003405// This test sets up a call between two parties with audio and video.
3406// During the call, the caller restarts ICE and the test verifies that
3407// new ICE candidates are generated and audio and video still can flow, and the
3408// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003409TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003410 ASSERT_TRUE(CreatePeerConnectionWrappers());
3411 ConnectFakeSignaling();
3412 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003413 caller()->AddAudioVideoTracks();
3414 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003415 caller()->CreateAndSetAndSignalOffer();
3416 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3417 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3418 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3419 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3420 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3421
3422 // To verify that the ICE restart actually occurs, get
3423 // ufrag/password/candidates before and after restart.
3424 // Create an SDP string of the first audio candidate for both clients.
3425 const webrtc::IceCandidateCollection* audio_candidates_caller =
3426 caller()->pc()->local_description()->candidates(0);
3427 const webrtc::IceCandidateCollection* audio_candidates_callee =
3428 callee()->pc()->local_description()->candidates(0);
3429 ASSERT_GT(audio_candidates_caller->count(), 0u);
3430 ASSERT_GT(audio_candidates_callee->count(), 0u);
3431 std::string caller_candidate_pre_restart;
3432 ASSERT_TRUE(
3433 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3434 std::string callee_candidate_pre_restart;
3435 ASSERT_TRUE(
3436 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3437 const cricket::SessionDescription* desc =
3438 caller()->pc()->local_description()->description();
3439 std::string caller_ufrag_pre_restart =
3440 desc->transport_infos()[0].description.ice_ufrag;
3441 desc = callee()->pc()->local_description()->description();
3442 std::string callee_ufrag_pre_restart =
3443 desc->transport_infos()[0].description.ice_ufrag;
3444
3445 // Have the caller initiate an ICE restart.
3446 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3447 caller()->CreateAndSetAndSignalOffer();
3448 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3449 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3450 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3451 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3452 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3453
3454 // Grab the ufrags/candidates again.
3455 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3456 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3457 ASSERT_GT(audio_candidates_caller->count(), 0u);
3458 ASSERT_GT(audio_candidates_callee->count(), 0u);
3459 std::string caller_candidate_post_restart;
3460 ASSERT_TRUE(
3461 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3462 std::string callee_candidate_post_restart;
3463 ASSERT_TRUE(
3464 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3465 desc = caller()->pc()->local_description()->description();
3466 std::string caller_ufrag_post_restart =
3467 desc->transport_infos()[0].description.ice_ufrag;
3468 desc = callee()->pc()->local_description()->description();
3469 std::string callee_ufrag_post_restart =
3470 desc->transport_infos()[0].description.ice_ufrag;
3471 // Sanity check that an ICE restart was actually negotiated in SDP.
3472 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3473 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3474 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3475 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3476
3477 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003478 MediaExpectations media_expectations;
3479 media_expectations.ExpectBidirectionalAudioAndVideo();
3480 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003481}
3482
3483// Verify that audio/video can be received end-to-end when ICE renomination is
3484// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003485TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003486 PeerConnectionInterface::RTCConfiguration config;
3487 config.enable_ice_renomination = true;
3488 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3489 ConnectFakeSignaling();
3490 // Do normal offer/answer and wait for some frames to be received in each
3491 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003492 caller()->AddAudioVideoTracks();
3493 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003494 caller()->CreateAndSetAndSignalOffer();
3495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3496 // Sanity check that ICE renomination was actually negotiated.
3497 const cricket::SessionDescription* desc =
3498 caller()->pc()->local_description()->description();
3499 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003500 ASSERT_NE(
3501 info.description.transport_options.end(),
3502 std::find(info.description.transport_options.begin(),
3503 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003504 }
3505 desc = callee()->pc()->local_description()->description();
3506 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003507 ASSERT_NE(
3508 info.description.transport_options.end(),
3509 std::find(info.description.transport_options.begin(),
3510 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003511 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003512 MediaExpectations media_expectations;
3513 media_expectations.ExpectBidirectionalAudioAndVideo();
3514 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003515}
3516
Steve Anton6f25b092017-10-23 09:39:20 -07003517// With a max bundle policy and RTCP muxing, adding a new media description to
3518// the connection should not affect ICE at all because the new media will use
3519// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003520TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003521 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003522 PeerConnectionInterface::RTCConfiguration config;
3523 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3524 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3525 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3526 config, PeerConnectionInterface::RTCConfiguration()));
3527 ConnectFakeSignaling();
3528
Steve Anton15324772018-01-16 10:26:49 -08003529 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003530 caller()->CreateAndSetAndSignalOffer();
3531 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003532 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3533 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003534
3535 caller()->clear_ice_connection_state_history();
3536
Steve Anton15324772018-01-16 10:26:49 -08003537 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003538 caller()->CreateAndSetAndSignalOffer();
3539 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3540
3541 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3542}
3543
deadbeef1dcb1642017-03-29 21:08:16 -07003544// This test sets up a call between two parties with audio and video. It then
3545// renegotiates setting the video m-line to "port 0", then later renegotiates
3546// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003547TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003548 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3549 ASSERT_TRUE(CreatePeerConnectionWrappers());
3550 ConnectFakeSignaling();
3551
3552 // Do initial negotiation, only sending media from the caller. Will result in
3553 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003554 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003555 caller()->CreateAndSetAndSignalOffer();
3556 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3557
3558 // Negotiate again, disabling the video "m=" section (the callee will set the
3559 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003560 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3561 PeerConnectionInterface::RTCOfferAnswerOptions options;
3562 options.offer_to_receive_video = 0;
3563 callee()->SetOfferAnswerOptions(options);
3564 } else {
3565 callee()->SetRemoteOfferHandler([this] {
3566 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3567 });
3568 }
deadbeef1dcb1642017-03-29 21:08:16 -07003569 caller()->CreateAndSetAndSignalOffer();
3570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3571 // Sanity check that video "m=" section was actually rejected.
3572 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3573 callee()->pc()->local_description()->description());
3574 ASSERT_NE(nullptr, answer_video_content);
3575 ASSERT_TRUE(answer_video_content->rejected);
3576
3577 // Enable video and do negotiation again, making sure video is received
3578 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003579 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3580 PeerConnectionInterface::RTCOfferAnswerOptions options;
3581 options.offer_to_receive_video = 1;
3582 callee()->SetOfferAnswerOptions(options);
3583 } else {
3584 // The caller's transceiver is stopped, so we need to add another track.
3585 auto caller_transceiver =
3586 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3587 EXPECT_TRUE(caller_transceiver->stopped());
3588 caller()->AddVideoTrack();
3589 }
3590 callee()->AddVideoTrack();
3591 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003592 caller()->CreateAndSetAndSignalOffer();
3593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003594
deadbeef1dcb1642017-03-29 21:08:16 -07003595 // Verify the caller receives frames from the newly added stream, and the
3596 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003597 MediaExpectations media_expectations;
3598 media_expectations.CalleeExpectsSomeAudio();
3599 media_expectations.ExpectBidirectionalVideo();
3600 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003601}
3602
deadbeef1dcb1642017-03-29 21:08:16 -07003603// This tests that if we negotiate after calling CreateSender but before we
3604// have a track, then set a track later, frames from the newly-set track are
3605// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003606TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003607 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3608 ASSERT_TRUE(CreatePeerConnectionWrappers());
3609 ConnectFakeSignaling();
3610 auto caller_audio_sender =
3611 caller()->pc()->CreateSender("audio", "caller_stream");
3612 auto caller_video_sender =
3613 caller()->pc()->CreateSender("video", "caller_stream");
3614 auto callee_audio_sender =
3615 callee()->pc()->CreateSender("audio", "callee_stream");
3616 auto callee_video_sender =
3617 callee()->pc()->CreateSender("video", "callee_stream");
3618 caller()->CreateAndSetAndSignalOffer();
3619 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3620 // Wait for ICE to complete, without any tracks being set.
3621 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3622 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3623 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3624 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3625 // Now set the tracks, and expect frames to immediately start flowing.
3626 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3627 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3628 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3629 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003630 MediaExpectations media_expectations;
3631 media_expectations.ExpectBidirectionalAudioAndVideo();
3632 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3633}
3634
3635// This tests that if we negotiate after calling AddTransceiver but before we
3636// have a track, then set a track later, frames from the newly-set tracks are
3637// received end-to-end.
3638TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3639 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3640 ASSERT_TRUE(CreatePeerConnectionWrappers());
3641 ConnectFakeSignaling();
3642 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3643 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3644 auto caller_audio_sender = audio_result.MoveValue()->sender();
3645 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3646 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3647 auto caller_video_sender = video_result.MoveValue()->sender();
3648 callee()->SetRemoteOfferHandler([this] {
3649 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3650 callee()->pc()->GetTransceivers()[0]->SetDirection(
3651 RtpTransceiverDirection::kSendRecv);
3652 callee()->pc()->GetTransceivers()[1]->SetDirection(
3653 RtpTransceiverDirection::kSendRecv);
3654 });
3655 caller()->CreateAndSetAndSignalOffer();
3656 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3657 // Wait for ICE to complete, without any tracks being set.
3658 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3659 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3660 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3661 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3662 // Now set the tracks, and expect frames to immediately start flowing.
3663 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3664 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3665 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3666 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3667 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3668 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3669 MediaExpectations media_expectations;
3670 media_expectations.ExpectBidirectionalAudioAndVideo();
3671 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003672}
3673
3674// This test verifies that a remote video track can be added via AddStream,
3675// and sent end-to-end. For this particular test, it's simply echoed back
3676// from the caller to the callee, rather than being forwarded to a third
3677// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003678TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003679 ASSERT_TRUE(CreatePeerConnectionWrappers());
3680 ConnectFakeSignaling();
3681 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003682 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003683 caller()->CreateAndSetAndSignalOffer();
3684 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3685 ASSERT_EQ(1, callee()->remote_streams()->count());
3686
3687 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3688 // time).
3689 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3690 callee()->CreateAndSetAndSignalOffer();
3691 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3692
Seth Hampson2f0d7022018-02-20 11:54:42 -08003693 MediaExpectations media_expectations;
3694 media_expectations.ExpectBidirectionalVideo();
3695 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003696}
3697
3698// Test that we achieve the expected end-to-end connection time, using a
3699// fake clock and simulated latency on the media and signaling paths.
3700// We use a TURN<->TURN connection because this is usually the quickest to
3701// set up initially, especially when we're confident the connection will work
3702// and can start sending media before we get a STUN response.
3703//
3704// With various optimizations enabled, here are the network delays we expect to
3705// be on the critical path:
3706// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3707// signaling answer (with DTLS fingerprint).
3708// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3709// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3710// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003711TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003712 rtc::ScopedFakeClock fake_clock;
3713 // Some things use a time of "0" as a special value, so we need to start out
3714 // the fake clock at a nonzero time.
3715 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003716 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003717
3718 static constexpr int media_hop_delay_ms = 50;
3719 static constexpr int signaling_trip_delay_ms = 500;
3720 // For explanation of these values, see comment above.
3721 static constexpr int required_media_hops = 9;
3722 static constexpr int required_signaling_trips = 2;
3723 // For internal delays (such as posting an event asychronously).
3724 static constexpr int allowed_internal_delay_ms = 20;
3725 static constexpr int total_connection_time_ms =
3726 media_hop_delay_ms * required_media_hops +
3727 signaling_trip_delay_ms * required_signaling_trips +
3728 allowed_internal_delay_ms;
3729
3730 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3731 3478};
3732 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3733 0};
3734 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3735 3478};
3736 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3737 0};
3738 cricket::TestTurnServer turn_server_1(network_thread(),
3739 turn_server_1_internal_address,
3740 turn_server_1_external_address);
3741 cricket::TestTurnServer turn_server_2(network_thread(),
3742 turn_server_2_internal_address,
3743 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003744
deadbeef1dcb1642017-03-29 21:08:16 -07003745 // Bypass permission check on received packets so media can be sent before
3746 // the candidate is signaled.
3747 turn_server_1.set_enable_permission_checks(false);
3748 turn_server_2.set_enable_permission_checks(false);
3749
3750 PeerConnectionInterface::RTCConfiguration client_1_config;
3751 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3752 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3753 ice_server_1.username = "test";
3754 ice_server_1.password = "test";
3755 client_1_config.servers.push_back(ice_server_1);
3756 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3757 client_1_config.presume_writable_when_fully_relayed = true;
3758
3759 PeerConnectionInterface::RTCConfiguration client_2_config;
3760 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3761 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3762 ice_server_2.username = "test";
3763 ice_server_2.password = "test";
3764 client_2_config.servers.push_back(ice_server_2);
3765 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3766 client_2_config.presume_writable_when_fully_relayed = true;
3767
3768 ASSERT_TRUE(
3769 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3770 // Set up the simulated delays.
3771 SetSignalingDelayMs(signaling_trip_delay_ms);
3772 ConnectFakeSignaling();
3773 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3774 virtual_socket_server()->UpdateDelayDistribution();
3775
3776 // Set "offer to receive audio/video" without adding any tracks, so we just
3777 // set up ICE/DTLS with no media.
3778 PeerConnectionInterface::RTCOfferAnswerOptions options;
3779 options.offer_to_receive_audio = 1;
3780 options.offer_to_receive_video = 1;
3781 caller()->SetOfferAnswerOptions(options);
3782 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003783 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3784 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003785 // Need to free the clients here since they're using things we created on
3786 // the stack.
3787 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3788 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3789}
3790
Jonas Orelandbdcee282017-10-10 14:01:40 +02003791// Verify that a TurnCustomizer passed in through RTCConfiguration
3792// is actually used by the underlying TURN candidate pair.
3793// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003794TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003795 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3796 3478};
3797 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3798 0};
3799 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3800 3478};
3801 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3802 0};
3803 cricket::TestTurnServer turn_server_1(network_thread(),
3804 turn_server_1_internal_address,
3805 turn_server_1_external_address);
3806 cricket::TestTurnServer turn_server_2(network_thread(),
3807 turn_server_2_internal_address,
3808 turn_server_2_external_address);
3809
3810 PeerConnectionInterface::RTCConfiguration client_1_config;
3811 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3812 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3813 ice_server_1.username = "test";
3814 ice_server_1.password = "test";
3815 client_1_config.servers.push_back(ice_server_1);
3816 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3817 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3818 client_1_config.turn_customizer = customizer1.get();
3819
3820 PeerConnectionInterface::RTCConfiguration client_2_config;
3821 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3822 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3823 ice_server_2.username = "test";
3824 ice_server_2.password = "test";
3825 client_2_config.servers.push_back(ice_server_2);
3826 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3827 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3828 client_2_config.turn_customizer = customizer2.get();
3829
3830 ASSERT_TRUE(
3831 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3832 ConnectFakeSignaling();
3833
3834 // Set "offer to receive audio/video" without adding any tracks, so we just
3835 // set up ICE/DTLS with no media.
3836 PeerConnectionInterface::RTCOfferAnswerOptions options;
3837 options.offer_to_receive_audio = 1;
3838 options.offer_to_receive_video = 1;
3839 caller()->SetOfferAnswerOptions(options);
3840 caller()->CreateAndSetAndSignalOffer();
3841 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3842
3843 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3844 EXPECT_GT(customizer1->modify_cnt_, 0u);
3845
3846 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3847 EXPECT_GT(customizer2->modify_cnt_, 0u);
3848
3849 // Need to free the clients here since they're using things we created on
3850 // the stack.
3851 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3852 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3853}
3854
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07003855// Verify that a SSLCertificateVerifier passed in through
3856// PeerConnectionDependencies is actually used by the underlying SSL
3857// implementation to determine whether a certificate presented by the TURN
3858// server is accepted by the client. Note that openssladapter_unittest.cc
3859// contains more detailed, lower-level tests.
3860TEST_P(PeerConnectionIntegrationTest,
3861 SSLCertificateVerifierUsedForTurnConnections) {
3862 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3863 3478};
3864 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3865
3866 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3867 // that host name verification passes on the fake certificate.
3868 cricket::TestTurnServer turn_server(
3869 network_thread(), turn_server_internal_address,
3870 turn_server_external_address, cricket::PROTO_TLS,
3871 /*ignore_bad_certs=*/true, "88.88.88.0");
3872
3873 webrtc::PeerConnectionInterface::IceServer ice_server;
3874 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3875 ice_server.username = "test";
3876 ice_server.password = "test";
3877
3878 PeerConnectionInterface::RTCConfiguration client_1_config;
3879 client_1_config.servers.push_back(ice_server);
3880 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3881
3882 PeerConnectionInterface::RTCConfiguration client_2_config;
3883 client_2_config.servers.push_back(ice_server);
3884 // Setting the type to kRelay forces the connection to go through a TURN
3885 // server.
3886 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3887
3888 // Get a copy to the pointer so we can verify calls later.
3889 rtc::TestCertificateVerifier* client_1_cert_verifier =
3890 new rtc::TestCertificateVerifier();
3891 client_1_cert_verifier->verify_certificate_ = true;
3892 rtc::TestCertificateVerifier* client_2_cert_verifier =
3893 new rtc::TestCertificateVerifier();
3894 client_2_cert_verifier->verify_certificate_ = true;
3895
3896 // Create the dependencies with the test certificate verifier.
3897 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3898 client_1_deps.tls_cert_verifier =
3899 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3900 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3901 client_2_deps.tls_cert_verifier =
3902 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3903
3904 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3905 client_1_config, std::move(client_1_deps), client_2_config,
3906 std::move(client_2_deps)));
3907 ConnectFakeSignaling();
3908
3909 // Set "offer to receive audio/video" without adding any tracks, so we just
3910 // set up ICE/DTLS with no media.
3911 PeerConnectionInterface::RTCOfferAnswerOptions options;
3912 options.offer_to_receive_audio = 1;
3913 options.offer_to_receive_video = 1;
3914 caller()->SetOfferAnswerOptions(options);
3915 caller()->CreateAndSetAndSignalOffer();
3916 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3917
3918 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
3919 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
3920
3921 // Need to free the clients here since they're using things we created on
3922 // the stack.
3923 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3924 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3925}
3926
3927TEST_P(PeerConnectionIntegrationTest,
3928 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
3929 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3930 3478};
3931 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3932
3933 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3934 // that host name verification passes on the fake certificate.
3935 cricket::TestTurnServer turn_server(
3936 network_thread(), turn_server_internal_address,
3937 turn_server_external_address, cricket::PROTO_TLS,
3938 /*ignore_bad_certs=*/true, "88.88.88.0");
3939
3940 webrtc::PeerConnectionInterface::IceServer ice_server;
3941 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3942 ice_server.username = "test";
3943 ice_server.password = "test";
3944
3945 PeerConnectionInterface::RTCConfiguration client_1_config;
3946 client_1_config.servers.push_back(ice_server);
3947 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3948
3949 PeerConnectionInterface::RTCConfiguration client_2_config;
3950 client_2_config.servers.push_back(ice_server);
3951 // Setting the type to kRelay forces the connection to go through a TURN
3952 // server.
3953 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3954
3955 // Get a copy to the pointer so we can verify calls later.
3956 rtc::TestCertificateVerifier* client_1_cert_verifier =
3957 new rtc::TestCertificateVerifier();
3958 client_1_cert_verifier->verify_certificate_ = false;
3959 rtc::TestCertificateVerifier* client_2_cert_verifier =
3960 new rtc::TestCertificateVerifier();
3961 client_2_cert_verifier->verify_certificate_ = false;
3962
3963 // Create the dependencies with the test certificate verifier.
3964 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3965 client_1_deps.tls_cert_verifier =
3966 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3967 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3968 client_2_deps.tls_cert_verifier =
3969 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3970
3971 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3972 client_1_config, std::move(client_1_deps), client_2_config,
3973 std::move(client_2_deps)));
3974 ConnectFakeSignaling();
3975
3976 // Set "offer to receive audio/video" without adding any tracks, so we just
3977 // set up ICE/DTLS with no media.
3978 PeerConnectionInterface::RTCOfferAnswerOptions options;
3979 options.offer_to_receive_audio = 1;
3980 options.offer_to_receive_video = 1;
3981 caller()->SetOfferAnswerOptions(options);
3982 caller()->CreateAndSetAndSignalOffer();
3983 bool wait_res = true;
3984 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
3985 // properly, should be able to just wait for a state of "failed" instead of
3986 // waiting a fixed 10 seconds.
3987 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
3988 ASSERT_FALSE(wait_res);
3989
3990 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
3991 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
3992
3993 // Need to free the clients here since they're using things we created on
3994 // the stack.
3995 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3996 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3997}
3998
deadbeefc964d0b2017-04-03 10:03:35 -07003999// Test that audio and video flow end-to-end when codec names don't use the
4000// expected casing, given that they're supposed to be case insensitive. To test
4001// this, all but one codec is removed from each media description, and its
4002// casing is changed.
4003//
4004// In the past, this has regressed and caused crashes/black video, due to the
4005// fact that code at some layers was doing case-insensitive comparisons and
4006// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004007TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004008 ASSERT_TRUE(CreatePeerConnectionWrappers());
4009 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004010 caller()->AddAudioVideoTracks();
4011 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004012
4013 // Remove all but one audio/video codec (opus and VP8), and change the
4014 // casing of the caller's generated offer.
4015 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4016 cricket::AudioContentDescription* audio =
4017 GetFirstAudioContentDescription(description);
4018 ASSERT_NE(nullptr, audio);
4019 auto audio_codecs = audio->codecs();
4020 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4021 [](const cricket::AudioCodec& codec) {
4022 return codec.name != "opus";
4023 }),
4024 audio_codecs.end());
4025 ASSERT_EQ(1u, audio_codecs.size());
4026 audio_codecs[0].name = "OpUs";
4027 audio->set_codecs(audio_codecs);
4028
4029 cricket::VideoContentDescription* video =
4030 GetFirstVideoContentDescription(description);
4031 ASSERT_NE(nullptr, video);
4032 auto video_codecs = video->codecs();
4033 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4034 [](const cricket::VideoCodec& codec) {
4035 return codec.name != "VP8";
4036 }),
4037 video_codecs.end());
4038 ASSERT_EQ(1u, video_codecs.size());
4039 video_codecs[0].name = "vP8";
4040 video->set_codecs(video_codecs);
4041 });
4042
4043 caller()->CreateAndSetAndSignalOffer();
4044 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4045
4046 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004047 MediaExpectations media_expectations;
4048 media_expectations.ExpectBidirectionalAudioAndVideo();
4049 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004050}
4051
Seth Hampson2f0d7022018-02-20 11:54:42 -08004052TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004053 ASSERT_TRUE(CreatePeerConnectionWrappers());
4054 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004055 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004056 caller()->CreateAndSetAndSignalOffer();
4057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004058 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004059 MediaExpectations media_expectations;
4060 media_expectations.CalleeExpectsSomeAudio(1);
4061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004062 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4063 auto receiver = callee()->pc()->GetReceivers()[0];
4064 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4065
4066 auto contributing_sources = receiver->GetSources();
4067 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4068 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4069 contributing_sources[0].source_id());
4070}
4071
deadbeef2f425aa2017-04-14 10:41:32 -07004072// Test that if a track is removed and added again with a different stream ID,
4073// the new stream ID is successfully communicated in SDP and media continues to
4074// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004075// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4076// it will not reuse a transceiver that has already been sending. After creating
4077// a new transceiver it tries to create an offer with two senders of the same
4078// track ids and it fails.
4079TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004080 ASSERT_TRUE(CreatePeerConnectionWrappers());
4081 ConnectFakeSignaling();
4082
4083 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4084 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4085 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4086 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4087
4088 // Add track using stream 1, do offer/answer.
4089 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4090 caller()->CreateLocalAudioTrack();
4091 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4092 caller()->pc()->AddTrack(track, {stream_1.get()});
4093 caller()->CreateAndSetAndSignalOffer();
4094 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004095 {
4096 MediaExpectations media_expectations;
4097 media_expectations.CalleeExpectsSomeAudio(1);
4098 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4099 }
deadbeef2f425aa2017-04-14 10:41:32 -07004100 // Remove the sender, and create a new one with the new stream.
4101 caller()->pc()->RemoveTrack(sender);
4102 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4103 caller()->CreateAndSetAndSignalOffer();
4104 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4105 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004106 {
4107 MediaExpectations media_expectations;
4108 media_expectations.CalleeExpectsSomeAudio();
4109 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4110 }
deadbeef2f425aa2017-04-14 10:41:32 -07004111}
4112
Seth Hampson2f0d7022018-02-20 11:54:42 -08004113TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004114 ASSERT_TRUE(CreatePeerConnectionWrappers());
4115 ConnectFakeSignaling();
4116
4117 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4118 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4119 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4120 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004121 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4122 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004123
Steve Anton15324772018-01-16 10:26:49 -08004124 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004125 caller()->CreateAndSetAndSignalOffer();
4126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4127}
4128
Steve Antonede9ca52017-10-16 13:04:27 -07004129// Test that if candidates are only signaled by applying full session
4130// descriptions (instead of using AddIceCandidate), the peers can connect to
4131// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004132TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004133 ASSERT_TRUE(CreatePeerConnectionWrappers());
4134 // Each side will signal the session descriptions but not candidates.
4135 ConnectFakeSignalingForSdpOnly();
4136
4137 // Add audio video track and exchange the initial offer/answer with media
4138 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004139 caller()->AddAudioVideoTracks();
4140 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004141 caller()->CreateAndSetAndSignalOffer();
4142
4143 // Wait for all candidates to be gathered on both the caller and callee.
4144 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4145 caller()->ice_gathering_state(), kDefaultTimeout);
4146 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4147 callee()->ice_gathering_state(), kDefaultTimeout);
4148
4149 // The candidates will now be included in the session description, so
4150 // signaling them will start the ICE connection.
4151 caller()->CreateAndSetAndSignalOffer();
4152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4153
4154 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004155 MediaExpectations media_expectations;
4156 media_expectations.ExpectBidirectionalAudioAndVideo();
4157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004158}
4159
henrika5f6bf242017-11-01 11:06:56 +01004160// Test that SetAudioPlayout can be used to disable audio playout from the
4161// start, then later enable it. This may be useful, for example, if the caller
4162// needs to play a local ringtone until some event occurs, after which it
4163// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004164TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004165 ASSERT_TRUE(CreatePeerConnectionWrappers());
4166 ConnectFakeSignaling();
4167
4168 // Set up audio-only call where audio playout is disabled on caller's side.
4169 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004170 caller()->AddAudioTrack();
4171 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004172 caller()->CreateAndSetAndSignalOffer();
4173 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4174
4175 // Pump messages for a second.
4176 WAIT(false, 1000);
4177 // Since audio playout is disabled, the caller shouldn't have received
4178 // anything (at the playout level, at least).
4179 EXPECT_EQ(0, caller()->audio_frames_received());
4180 // As a sanity check, make sure the callee (for which playout isn't disabled)
4181 // did still see frames on its audio level.
4182 ASSERT_GT(callee()->audio_frames_received(), 0);
4183
4184 // Enable playout again, and ensure audio starts flowing.
4185 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004186 MediaExpectations media_expectations;
4187 media_expectations.ExpectBidirectionalAudio();
4188 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004189}
4190
4191double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4192 auto report = pc->NewGetStats();
4193 auto track_stats_list =
4194 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4195 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4196 for (const auto* track_stats : track_stats_list) {
4197 if (track_stats->remote_source.is_defined() &&
4198 *track_stats->remote_source) {
4199 remote_track_stats = track_stats;
4200 break;
4201 }
4202 }
4203
4204 if (!remote_track_stats->total_audio_energy.is_defined()) {
4205 return 0.0;
4206 }
4207 return *remote_track_stats->total_audio_energy;
4208}
4209
4210// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4211// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004212TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004213 DisableAudioPlayoutStillGeneratesAudioStats) {
4214 ASSERT_TRUE(CreatePeerConnectionWrappers());
4215 ConnectFakeSignaling();
4216
4217 // Set up audio-only call where playout is disabled but audio-processing is
4218 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004219 caller()->AddAudioTrack();
4220 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004221 caller()->pc()->SetAudioPlayout(false);
4222
4223 caller()->CreateAndSetAndSignalOffer();
4224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4225
4226 // Wait for the callee to receive audio stats.
4227 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4228}
4229
henrika4f167df2017-11-01 14:45:55 +01004230// Test that SetAudioRecording can be used to disable audio recording from the
4231// start, then later enable it. This may be useful, for example, if the caller
4232// wants to ensure that no audio resources are active before a certain state
4233// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004234TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004235 ASSERT_TRUE(CreatePeerConnectionWrappers());
4236 ConnectFakeSignaling();
4237
4238 // Set up audio-only call where audio recording is disabled on caller's side.
4239 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004240 caller()->AddAudioTrack();
4241 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004242 caller()->CreateAndSetAndSignalOffer();
4243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4244
4245 // Pump messages for a second.
4246 WAIT(false, 1000);
4247 // Since caller has disabled audio recording, the callee shouldn't have
4248 // received anything.
4249 EXPECT_EQ(0, callee()->audio_frames_received());
4250 // As a sanity check, make sure the caller did still see frames on its
4251 // audio level since audio recording is enabled on the calle side.
4252 ASSERT_GT(caller()->audio_frames_received(), 0);
4253
4254 // Enable audio recording again, and ensure audio starts flowing.
4255 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004256 MediaExpectations media_expectations;
4257 media_expectations.ExpectBidirectionalAudio();
4258 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004259}
4260
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004261// Test that after closing PeerConnections, they stop sending any packets (ICE,
4262// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004263TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004264 // Set up audio/video/data, wait for some frames to be received.
4265 ASSERT_TRUE(CreatePeerConnectionWrappers());
4266 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004267 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004268#ifdef HAVE_SCTP
4269 caller()->CreateDataChannel();
4270#endif
4271 caller()->CreateAndSetAndSignalOffer();
4272 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004273 MediaExpectations media_expectations;
4274 media_expectations.CalleeExpectsSomeAudioAndVideo();
4275 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004276 // Close PeerConnections.
4277 caller()->pc()->Close();
4278 callee()->pc()->Close();
4279 // Pump messages for a second, and ensure no new packets end up sent.
4280 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4281 WAIT(false, 1000);
4282 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4283 EXPECT_EQ(sent_packets_a, sent_packets_b);
4284}
4285
Steve Anton7eca0932018-03-30 15:18:41 -07004286// Test that transport stats are generated by the RTCStatsCollector for a
4287// connection that only involves data channels. This is a regression test for
4288// crbug.com/826972.
4289#ifdef HAVE_SCTP
4290TEST_P(PeerConnectionIntegrationTest,
4291 TransportStatsReportedForDataChannelOnlyConnection) {
4292 ASSERT_TRUE(CreatePeerConnectionWrappers());
4293 ConnectFakeSignaling();
4294 caller()->CreateDataChannel();
4295
4296 caller()->CreateAndSetAndSignalOffer();
4297 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4298 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4299
4300 auto caller_report = caller()->NewGetStats();
4301 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4302 auto callee_report = callee()->NewGetStats();
4303 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4304}
4305#endif // HAVE_SCTP
4306
Seth Hampson2f0d7022018-02-20 11:54:42 -08004307INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4308 PeerConnectionIntegrationTest,
4309 Values(SdpSemantics::kPlanB,
4310 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004311
Steve Anton74255ff2018-01-24 18:32:57 -08004312// Tests that verify interoperability between Plan B and Unified Plan
4313// PeerConnections.
4314class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004315 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004316 public ::testing::WithParamInterface<
4317 std::tuple<SdpSemantics, SdpSemantics>> {
4318 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004319 // Setting the SdpSemantics for the base test to kDefault does not matter
4320 // because we specify not to use the test semantics when creating
4321 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004322 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004323 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004324 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004325 callee_semantics_(std::get<1>(GetParam())) {}
4326
4327 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004328 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4329 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004330 }
4331
4332 const SdpSemantics caller_semantics_;
4333 const SdpSemantics callee_semantics_;
4334};
4335
4336TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4337 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4338 ConnectFakeSignaling();
4339
4340 caller()->CreateAndSetAndSignalOffer();
4341 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4342}
4343
4344TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4345 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4346 ConnectFakeSignaling();
4347 auto audio_sender = caller()->AddAudioTrack();
4348
4349 caller()->CreateAndSetAndSignalOffer();
4350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4351
4352 // Verify that one audio receiver has been created on the remote and that it
4353 // has the same track ID as the sending track.
4354 auto receivers = callee()->pc()->GetReceivers();
4355 ASSERT_EQ(1u, receivers.size());
4356 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4357 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4358
Seth Hampson2f0d7022018-02-20 11:54:42 -08004359 MediaExpectations media_expectations;
4360 media_expectations.CalleeExpectsSomeAudio();
4361 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004362}
4363
4364TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4365 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4366 ConnectFakeSignaling();
4367 auto video_sender = caller()->AddVideoTrack();
4368 auto audio_sender = caller()->AddAudioTrack();
4369
4370 caller()->CreateAndSetAndSignalOffer();
4371 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4372
4373 // Verify that one audio and one video receiver have been created on the
4374 // remote and that they have the same track IDs as the sending tracks.
4375 auto audio_receivers =
4376 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4377 ASSERT_EQ(1u, audio_receivers.size());
4378 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4379 auto video_receivers =
4380 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4381 ASSERT_EQ(1u, video_receivers.size());
4382 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4383
Seth Hampson2f0d7022018-02-20 11:54:42 -08004384 MediaExpectations media_expectations;
4385 media_expectations.CalleeExpectsSomeAudioAndVideo();
4386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004387}
4388
4389TEST_P(PeerConnectionIntegrationInteropTest,
4390 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4391 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4392 ConnectFakeSignaling();
4393 caller()->AddAudioVideoTracks();
4394 callee()->AddAudioVideoTracks();
4395
4396 caller()->CreateAndSetAndSignalOffer();
4397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4398
Seth Hampson2f0d7022018-02-20 11:54:42 -08004399 MediaExpectations media_expectations;
4400 media_expectations.ExpectBidirectionalAudioAndVideo();
4401 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004402}
4403
4404TEST_P(PeerConnectionIntegrationInteropTest,
4405 ReverseRolesOneAudioLocalToOneVideoRemote) {
4406 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4407 ConnectFakeSignaling();
4408 caller()->AddAudioTrack();
4409 callee()->AddVideoTrack();
4410
4411 caller()->CreateAndSetAndSignalOffer();
4412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4413
4414 // Verify that only the audio track has been negotiated.
4415 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4416 // Might also check that the callee's NegotiationNeeded flag is set.
4417
4418 // Reverse roles.
4419 callee()->CreateAndSetAndSignalOffer();
4420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4421
Seth Hampson2f0d7022018-02-20 11:54:42 -08004422 MediaExpectations media_expectations;
4423 media_expectations.CallerExpectsSomeVideo();
4424 media_expectations.CalleeExpectsSomeAudio();
4425 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004426}
4427
Steve Antonba42e992018-04-09 14:10:01 -07004428INSTANTIATE_TEST_CASE_P(
4429 PeerConnectionIntegrationTest,
4430 PeerConnectionIntegrationInteropTest,
4431 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4432 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4433
4434// Test that if the Unified Plan side offers two video tracks then the Plan B
4435// side will only see the first one and ignore the second.
4436TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004437 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4438 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004439 ConnectFakeSignaling();
4440 auto first_sender = caller()->AddVideoTrack();
4441 caller()->AddVideoTrack();
4442
4443 caller()->CreateAndSetAndSignalOffer();
4444 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4445
4446 // Verify that there is only one receiver and it corresponds to the first
4447 // added track.
4448 auto receivers = callee()->pc()->GetReceivers();
4449 ASSERT_EQ(1u, receivers.size());
4450 EXPECT_TRUE(receivers[0]->track()->enabled());
4451 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4452
Seth Hampson2f0d7022018-02-20 11:54:42 -08004453 MediaExpectations media_expectations;
4454 media_expectations.CalleeExpectsSomeVideo();
4455 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004456}
4457
deadbeef1dcb1642017-03-29 21:08:16 -07004458} // namespace
4459
4460#endif // if !defined(THREAD_SANITIZER)