blob: e697e3aef936bda186b721ea79f867458228f0cd [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öller5c7efe72018-05-11 10:34:46 +020051#include "pc/test/fakeperiodicvideosource.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
Niels Möller5c7efe72018-05-11 10:34:46 +0200241 ~PeerConnectionWrapper() {
242 // Tear down video sources in the proper order.
243 for (const auto& video_source : fake_video_sources_) {
244 // No more calls to downstream OnFrame
245 video_source->Stop();
246 }
247 for (const auto& track_source : video_track_sources_) {
248 // No more calls to upstream AddOrUpdateSink
249 track_source->OnSourceDestroyed();
250 }
251 fake_video_sources_.clear();
252 video_track_sources_.clear();
253 }
254
deadbeef2f425aa2017-04-14 10:41:32 -0700255 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
256 return peer_connection_factory_.get();
257 }
258
deadbeef1dcb1642017-03-29 21:08:16 -0700259 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
260
261 // If a signaling message receiver is set (via ConnectFakeSignaling), this
262 // will set the whole offer/answer exchange in motion. Just need to wait for
263 // the signaling state to reach "stable".
264 void CreateAndSetAndSignalOffer() {
265 auto offer = CreateOffer();
266 ASSERT_NE(nullptr, offer);
267 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
268 }
269
270 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
271 // when a remote offer is received (via fake signaling) and an answer is
272 // generated. By default, uses default options.
273 void SetOfferAnswerOptions(
274 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
275 offer_answer_options_ = options;
276 }
277
278 // Set a callback to be invoked when SDP is received via the fake signaling
279 // channel, which provides an opportunity to munge (modify) the SDP. This is
280 // used to test SDP being applied that a PeerConnection would normally not
281 // generate, but a non-JSEP endpoint might.
282 void SetReceivedSdpMunger(
283 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100284 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700285 }
286
deadbeefc964d0b2017-04-03 10:03:35 -0700287 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700288 // generated.
289 void SetGeneratedSdpMunger(
290 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100291 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700292 }
293
Seth Hampson2f0d7022018-02-20 11:54:42 -0800294 // Set a callback to be invoked when a remote offer is received via the fake
295 // signaling channel. This provides an opportunity to change the
296 // PeerConnection state before an answer is created and sent to the caller.
297 void SetRemoteOfferHandler(std::function<void()> handler) {
298 remote_offer_handler_ = std::move(handler);
299 }
300
Steve Antonede9ca52017-10-16 13:04:27 -0700301 // Every ICE connection state in order that has been seen by the observer.
302 std::vector<PeerConnectionInterface::IceConnectionState>
303 ice_connection_state_history() const {
304 return ice_connection_state_history_;
305 }
Steve Anton6f25b092017-10-23 09:39:20 -0700306 void clear_ice_connection_state_history() {
307 ice_connection_state_history_.clear();
308 }
Steve Antonede9ca52017-10-16 13:04:27 -0700309
310 // Every ICE gathering state in order that has been seen by the observer.
311 std::vector<PeerConnectionInterface::IceGatheringState>
312 ice_gathering_state_history() const {
313 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700314 }
315
Steve Anton15324772018-01-16 10:26:49 -0800316 void AddAudioVideoTracks() {
317 AddAudioTrack();
318 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700319 }
320
Steve Anton74255ff2018-01-24 18:32:57 -0800321 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
322 return AddTrack(CreateLocalAudioTrack());
323 }
deadbeef1dcb1642017-03-29 21:08:16 -0700324
Steve Anton74255ff2018-01-24 18:32:57 -0800325 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
326 return AddTrack(CreateLocalVideoTrack());
327 }
deadbeef1dcb1642017-03-29 21:08:16 -0700328
329 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
330 FakeConstraints constraints;
331 // Disable highpass filter so that we can get all the test audio frames.
332 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
333 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
334 peer_connection_factory_->CreateAudioSource(&constraints);
335 // TODO(perkj): Test audio source when it is implemented. Currently audio
336 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700337 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700338 source);
339 }
340
341 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200342 return CreateLocalVideoTrackInternal(
343 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700344 }
345
346 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200347 CreateLocalVideoTrackWithConfig(
348 webrtc::FakePeriodicVideoSource::Config config) {
349 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700350 }
351
352 rtc::scoped_refptr<webrtc::VideoTrackInterface>
353 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200354 webrtc::FakePeriodicVideoSource::Config config;
355 config.rotation = rotation;
356 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700357 }
358
Steve Anton74255ff2018-01-24 18:32:57 -0800359 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
360 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800361 const std::vector<std::string>& stream_ids = {}) {
362 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800363 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800364 return result.MoveValue();
365 }
366
367 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
368 cricket::MediaType media_type) {
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
370 for (auto receiver : pc()->GetReceivers()) {
371 if (receiver->media_type() == media_type) {
372 receivers.push_back(receiver);
373 }
374 }
375 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700376 }
377
Seth Hampson2f0d7022018-02-20 11:54:42 -0800378 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
379 cricket::MediaType media_type) {
380 for (auto transceiver : pc()->GetTransceivers()) {
381 if (transceiver->receiver()->media_type() == media_type) {
382 return transceiver;
383 }
384 }
385 return nullptr;
386 }
387
deadbeef1dcb1642017-03-29 21:08:16 -0700388 bool SignalingStateStable() {
389 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
390 }
391
392 void CreateDataChannel() { CreateDataChannel(nullptr); }
393
394 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700395 CreateDataChannel(kDataChannelLabel, init);
396 }
397
398 void CreateDataChannel(const std::string& label,
399 const webrtc::DataChannelInit* init) {
400 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700401 ASSERT_TRUE(data_channel_.get() != nullptr);
402 data_observer_.reset(new MockDataChannelObserver(data_channel_));
403 }
404
405 DataChannelInterface* data_channel() { return data_channel_; }
406 const MockDataChannelObserver* data_observer() const {
407 return data_observer_.get();
408 }
409
410 int audio_frames_received() const {
411 return fake_audio_capture_module_->frames_received();
412 }
413
414 // Takes minimum of video frames received for each track.
415 //
416 // Can be used like:
417 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
418 //
419 // To ensure that all video tracks received at least a certain number of
420 // frames.
421 int min_video_frames_received_per_track() const {
422 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200423 if (fake_video_renderers_.empty()) {
424 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700425 }
deadbeef1dcb1642017-03-29 21:08:16 -0700426
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200427 for (const auto& pair : fake_video_renderers_) {
428 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700429 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200430 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700431 }
432
433 // Returns a MockStatsObserver in a state after stats gathering finished,
434 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700436 webrtc::MediaStreamTrackInterface* track) {
437 rtc::scoped_refptr<MockStatsObserver> observer(
438 new rtc::RefCountedObject<MockStatsObserver>());
439 EXPECT_TRUE(peer_connection_->GetStats(
440 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
441 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
442 return observer;
443 }
444
445 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700446 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
447 return OldGetStatsForTrack(nullptr);
448 }
449
450 // Synchronously gets stats and returns them. If it times out, fails the test
451 // and returns null.
452 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
453 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
454 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
455 peer_connection_->GetStats(callback);
456 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
457 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700458 }
459
460 int rendered_width() {
461 EXPECT_FALSE(fake_video_renderers_.empty());
462 return fake_video_renderers_.empty()
463 ? 0
464 : fake_video_renderers_.begin()->second->width();
465 }
466
467 int rendered_height() {
468 EXPECT_FALSE(fake_video_renderers_.empty());
469 return fake_video_renderers_.empty()
470 ? 0
471 : fake_video_renderers_.begin()->second->height();
472 }
473
474 double rendered_aspect_ratio() {
475 if (rendered_height() == 0) {
476 return 0.0;
477 }
478 return static_cast<double>(rendered_width()) / rendered_height();
479 }
480
481 webrtc::VideoRotation rendered_rotation() {
482 EXPECT_FALSE(fake_video_renderers_.empty());
483 return fake_video_renderers_.empty()
484 ? webrtc::kVideoRotation_0
485 : fake_video_renderers_.begin()->second->rotation();
486 }
487
488 int local_rendered_width() {
489 return local_video_renderer_ ? local_video_renderer_->width() : 0;
490 }
491
492 int local_rendered_height() {
493 return local_video_renderer_ ? local_video_renderer_->height() : 0;
494 }
495
496 double local_rendered_aspect_ratio() {
497 if (local_rendered_height() == 0) {
498 return 0.0;
499 }
500 return static_cast<double>(local_rendered_width()) /
501 local_rendered_height();
502 }
503
504 size_t number_of_remote_streams() {
505 if (!pc()) {
506 return 0;
507 }
508 return pc()->remote_streams()->count();
509 }
510
511 StreamCollectionInterface* remote_streams() const {
512 if (!pc()) {
513 ADD_FAILURE();
514 return nullptr;
515 }
516 return pc()->remote_streams();
517 }
518
519 StreamCollectionInterface* local_streams() {
520 if (!pc()) {
521 ADD_FAILURE();
522 return nullptr;
523 }
524 return pc()->local_streams();
525 }
526
527 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
528 return pc()->signaling_state();
529 }
530
531 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
532 return pc()->ice_connection_state();
533 }
534
535 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
536 return pc()->ice_gathering_state();
537 }
538
539 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
540 // GetReceivers. They're updated automatically when a remote offer/answer
541 // from the fake signaling channel is applied, or when
542 // ResetRtpReceiverObservers below is called.
543 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
544 rtp_receiver_observers() {
545 return rtp_receiver_observers_;
546 }
547
548 void ResetRtpReceiverObservers() {
549 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100550 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
551 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700552 std::unique_ptr<MockRtpReceiverObserver> observer(
553 new MockRtpReceiverObserver(receiver->media_type()));
554 receiver->SetObserver(observer.get());
555 rtp_receiver_observers_.push_back(std::move(observer));
556 }
557 }
558
Steve Antonede9ca52017-10-16 13:04:27 -0700559 rtc::FakeNetworkManager* network() const {
560 return fake_network_manager_.get();
561 }
562 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
563
deadbeef1dcb1642017-03-29 21:08:16 -0700564 private:
565 explicit PeerConnectionWrapper(const std::string& debug_name)
566 : debug_name_(debug_name) {}
567
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700568 bool Init(const MediaConstraintsInterface* constraints,
569 const PeerConnectionFactory::Options* options,
570 const PeerConnectionInterface::RTCConfiguration* config,
571 webrtc::PeerConnectionDependencies dependencies,
572 rtc::Thread* network_thread,
573 rtc::Thread* worker_thread) {
deadbeef1dcb1642017-03-29 21:08:16 -0700574 // There's an error in this test code if Init ends up being called twice.
575 RTC_DCHECK(!peer_connection_);
576 RTC_DCHECK(!peer_connection_factory_);
577
578 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700579 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700580
581 std::unique_ptr<cricket::PortAllocator> port_allocator(
582 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700583 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700584 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
585 if (!fake_audio_capture_module_) {
586 return false;
587 }
deadbeef1dcb1642017-03-29 21:08:16 -0700588 rtc::Thread* const signaling_thread = rtc::Thread::Current();
589 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
590 network_thread, worker_thread, signaling_thread,
Anders Carlsson67537952018-05-03 11:28:29 +0200591 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
592 fake_audio_capture_module_),
593 webrtc::CreateBuiltinAudioEncoderFactory(),
594 webrtc::CreateBuiltinAudioDecoderFactory(),
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200595 webrtc::CreateBuiltinVideoEncoderFactory(),
596 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
597 nullptr /* audio_processing */);
deadbeef1dcb1642017-03-29 21:08:16 -0700598 if (!peer_connection_factory_) {
599 return false;
600 }
601 if (options) {
602 peer_connection_factory_->SetOptions(*options);
603 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800604 if (config) {
605 sdp_semantics_ = config->sdp_semantics;
606 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700607
608 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700609 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700610 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700611 return peer_connection_.get() != nullptr;
612 }
613
614 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700615 const MediaConstraintsInterface* constraints,
616 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700617 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700618 PeerConnectionInterface::RTCConfiguration modified_config;
619 // If |config| is null, this will result in a default configuration being
620 // used.
621 if (config) {
622 modified_config = *config;
623 }
624 // Disable resolution adaptation; we don't want it interfering with the
625 // test results.
626 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
627 // ratios and not specific resolutions, is this even necessary?
628 modified_config.set_cpu_adaptation(false);
629
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700630 // Use the legacy interface.
631 if (constraints != nullptr) {
632 return peer_connection_factory_->CreatePeerConnection(
633 modified_config, constraints, std::move(dependencies.allocator),
634 std::move(dependencies.cert_generator), this);
635 }
636 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700637 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700638 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700639 }
640
641 void set_signaling_message_receiver(
642 SignalingMessageReceiver* signaling_message_receiver) {
643 signaling_message_receiver_ = signaling_message_receiver;
644 }
645
646 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
647
Steve Antonede9ca52017-10-16 13:04:27 -0700648 void set_signal_ice_candidates(bool signal) {
649 signal_ice_candidates_ = signal;
650 }
651
deadbeef1dcb1642017-03-29 21:08:16 -0700652 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200653 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700654 // Set max frame rate to 10fps to reduce the risk of test flakiness.
655 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200656 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700657
Niels Möller5c7efe72018-05-11 10:34:46 +0200658 fake_video_sources_.emplace_back(
659 rtc::MakeUnique<webrtc::FakePeriodicVideoSource>(config));
660
661 video_track_sources_.emplace_back(
662 new rtc::RefCountedObject<webrtc::VideoTrackSource>(
663 fake_video_sources_.back().get(), false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700664 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200665 peer_connection_factory_->CreateVideoTrack(
666 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700667 if (!local_video_renderer_) {
668 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
669 }
670 return track;
671 }
672
673 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100674 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800675 std::unique_ptr<SessionDescriptionInterface> desc =
676 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700677 if (received_sdp_munger_) {
678 received_sdp_munger_(desc->description());
679 }
680
681 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
682 // Setting a remote description may have changed the number of receivers,
683 // so reset the receiver observers.
684 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800685 if (remote_offer_handler_) {
686 remote_offer_handler_();
687 }
deadbeef1dcb1642017-03-29 21:08:16 -0700688 auto answer = CreateAnswer();
689 ASSERT_NE(nullptr, answer);
690 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
691 }
692
693 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100694 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800695 std::unique_ptr<SessionDescriptionInterface> desc =
696 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700697 if (received_sdp_munger_) {
698 received_sdp_munger_(desc->description());
699 }
700
701 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
702 // Set the RtpReceiverObserver after receivers are created.
703 ResetRtpReceiverObservers();
704 }
705
706 // Returns null on failure.
707 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
708 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
709 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
710 pc()->CreateOffer(observer, offer_answer_options_);
711 return WaitForDescriptionFromObserver(observer);
712 }
713
714 // Returns null on failure.
715 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
716 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
717 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
718 pc()->CreateAnswer(observer, offer_answer_options_);
719 return WaitForDescriptionFromObserver(observer);
720 }
721
722 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100723 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700724 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
725 if (!observer->result()) {
726 return nullptr;
727 }
728 auto description = observer->MoveDescription();
729 if (generated_sdp_munger_) {
730 generated_sdp_munger_(description->description());
731 }
732 return description;
733 }
734
735 // Setting the local description and sending the SDP message over the fake
736 // signaling channel are combined into the same method because the SDP
737 // message needs to be sent as soon as SetLocalDescription finishes, without
738 // waiting for the observer to be called. This ensures that ICE candidates
739 // don't outrace the description.
740 bool SetLocalDescriptionAndSendSdpMessage(
741 std::unique_ptr<SessionDescriptionInterface> desc) {
742 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
743 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100744 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800745 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700746 std::string sdp;
747 EXPECT_TRUE(desc->ToString(&sdp));
748 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800749 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
750 RemoveUnusedVideoRenderers();
751 }
deadbeef1dcb1642017-03-29 21:08:16 -0700752 // As mentioned above, we need to send the message immediately after
753 // SetLocalDescription.
754 SendSdpMessage(type, sdp);
755 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
756 return true;
757 }
758
759 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
760 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
761 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100762 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700763 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
765 RemoveUnusedVideoRenderers();
766 }
deadbeef1dcb1642017-03-29 21:08:16 -0700767 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
768 return observer->result();
769 }
770
Seth Hampson2f0d7022018-02-20 11:54:42 -0800771 // This is a work around to remove unused fake_video_renderers from
772 // transceivers that have either stopped or are no longer receiving.
773 void RemoveUnusedVideoRenderers() {
774 auto transceivers = pc()->GetTransceivers();
775 for (auto& transceiver : transceivers) {
776 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
777 continue;
778 }
779 // Remove fake video renderers from any stopped transceivers.
780 if (transceiver->stopped()) {
781 auto it =
782 fake_video_renderers_.find(transceiver->receiver()->track()->id());
783 if (it != fake_video_renderers_.end()) {
784 fake_video_renderers_.erase(it);
785 }
786 }
787 // Remove fake video renderers from any transceivers that are no longer
788 // receiving.
789 if ((transceiver->current_direction() &&
790 !webrtc::RtpTransceiverDirectionHasRecv(
791 *transceiver->current_direction()))) {
792 auto it =
793 fake_video_renderers_.find(transceiver->receiver()->track()->id());
794 if (it != fake_video_renderers_.end()) {
795 fake_video_renderers_.erase(it);
796 }
797 }
798 }
799 }
800
deadbeef1dcb1642017-03-29 21:08:16 -0700801 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
802 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800803 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700804 if (signaling_delay_ms_ == 0) {
805 RelaySdpMessageIfReceiverExists(type, msg);
806 } else {
807 invoker_.AsyncInvokeDelayed<void>(
808 RTC_FROM_HERE, rtc::Thread::Current(),
809 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
810 this, type, msg),
811 signaling_delay_ms_);
812 }
813 }
814
Steve Antona3a92c22017-12-07 10:27:41 -0800815 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700816 if (signaling_message_receiver_) {
817 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
818 }
819 }
820
821 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
822 // default).
823 void SendIceMessage(const std::string& sdp_mid,
824 int sdp_mline_index,
825 const std::string& msg) {
826 if (signaling_delay_ms_ == 0) {
827 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
828 } else {
829 invoker_.AsyncInvokeDelayed<void>(
830 RTC_FROM_HERE, rtc::Thread::Current(),
831 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
832 this, sdp_mid, sdp_mline_index, msg),
833 signaling_delay_ms_);
834 }
835 }
836
837 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
838 int sdp_mline_index,
839 const std::string& msg) {
840 if (signaling_message_receiver_) {
841 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
842 msg);
843 }
844 }
845
846 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800847 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
848 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700849 HandleIncomingOffer(msg);
850 } else {
851 HandleIncomingAnswer(msg);
852 }
853 }
854
855 void ReceiveIceMessage(const std::string& sdp_mid,
856 int sdp_mline_index,
857 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100858 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700859 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
860 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
861 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
862 }
863
864 // PeerConnectionObserver callbacks.
865 void OnSignalingChange(
866 webrtc::PeerConnectionInterface::SignalingState new_state) override {
867 EXPECT_EQ(pc()->signaling_state(), new_state);
868 }
Steve Anton15324772018-01-16 10:26:49 -0800869 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
870 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
871 streams) override {
872 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
873 rtc::scoped_refptr<VideoTrackInterface> video_track(
874 static_cast<VideoTrackInterface*>(receiver->track().get()));
875 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700876 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800877 fake_video_renderers_[video_track->id()] =
878 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700879 }
880 }
Steve Anton15324772018-01-16 10:26:49 -0800881 void OnRemoveTrack(
882 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
883 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
884 auto it = fake_video_renderers_.find(receiver->track()->id());
885 RTC_DCHECK(it != fake_video_renderers_.end());
886 fake_video_renderers_.erase(it);
887 }
888 }
deadbeef1dcb1642017-03-29 21:08:16 -0700889 void OnRenegotiationNeeded() override {}
890 void OnIceConnectionChange(
891 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
892 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700893 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700894 }
895 void OnIceGatheringChange(
896 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700897 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700898 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700899 }
900 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100901 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700902
903 std::string ice_sdp;
904 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700905 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700906 // Remote party may be deleted.
907 return;
908 }
909 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
910 }
911 void OnDataChannel(
912 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100913 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700914 data_channel_ = data_channel;
915 data_observer_.reset(new MockDataChannelObserver(data_channel));
916 }
917
deadbeef1dcb1642017-03-29 21:08:16 -0700918 std::string debug_name_;
919
920 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
921
922 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
923 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
924 peer_connection_factory_;
925
Steve Antonede9ca52017-10-16 13:04:27 -0700926 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700927 // Needed to keep track of number of frames sent.
928 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
929 // Needed to keep track of number of frames received.
930 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
931 fake_video_renderers_;
932 // Needed to ensure frames aren't received for removed tracks.
933 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
934 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700935
936 // For remote peer communication.
937 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
938 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700939 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700940
Niels Möller5c7efe72018-05-11 10:34:46 +0200941 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700942 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200943 std::vector<std::unique_ptr<webrtc::FakePeriodicVideoSource>>
944 fake_video_sources_;
945 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
946 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700947 // |local_video_renderer_| attached to the first created local video track.
948 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
949
Seth Hampson2f0d7022018-02-20 11:54:42 -0800950 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700951 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
952 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
953 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800954 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700955
956 rtc::scoped_refptr<DataChannelInterface> data_channel_;
957 std::unique_ptr<MockDataChannelObserver> data_observer_;
958
959 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
960
Steve Antonede9ca52017-10-16 13:04:27 -0700961 std::vector<PeerConnectionInterface::IceConnectionState>
962 ice_connection_state_history_;
963 std::vector<PeerConnectionInterface::IceGatheringState>
964 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700965
966 rtc::AsyncInvoker invoker_;
967
Seth Hampson2f0d7022018-02-20 11:54:42 -0800968 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700969};
970
Elad Alon99c3fe52017-10-13 16:29:40 +0200971class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
972 public:
973 virtual ~MockRtcEventLogOutput() = default;
974 MOCK_CONST_METHOD0(IsActive, bool());
975 MOCK_METHOD1(Write, bool(const std::string&));
976};
977
Seth Hampson2f0d7022018-02-20 11:54:42 -0800978// This helper object is used for both specifying how many audio/video frames
979// are expected to be received for a caller/callee. It provides helper functions
980// to specify these expectations. The object initially starts in a state of no
981// expectations.
982class MediaExpectations {
983 public:
984 enum ExpectFrames {
985 kExpectSomeFrames,
986 kExpectNoFrames,
987 kNoExpectation,
988 };
989
990 void ExpectBidirectionalAudioAndVideo() {
991 ExpectBidirectionalAudio();
992 ExpectBidirectionalVideo();
993 }
994
995 void ExpectBidirectionalAudio() {
996 CallerExpectsSomeAudio();
997 CalleeExpectsSomeAudio();
998 }
999
1000 void ExpectNoAudio() {
1001 CallerExpectsNoAudio();
1002 CalleeExpectsNoAudio();
1003 }
1004
1005 void ExpectBidirectionalVideo() {
1006 CallerExpectsSomeVideo();
1007 CalleeExpectsSomeVideo();
1008 }
1009
1010 void ExpectNoVideo() {
1011 CallerExpectsNoVideo();
1012 CalleeExpectsNoVideo();
1013 }
1014
1015 void CallerExpectsSomeAudioAndVideo() {
1016 CallerExpectsSomeAudio();
1017 CallerExpectsSomeVideo();
1018 }
1019
1020 void CalleeExpectsSomeAudioAndVideo() {
1021 CalleeExpectsSomeAudio();
1022 CalleeExpectsSomeVideo();
1023 }
1024
1025 // Caller's audio functions.
1026 void CallerExpectsSomeAudio(
1027 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1028 caller_audio_expectation_ = kExpectSomeFrames;
1029 caller_audio_frames_expected_ = expected_audio_frames;
1030 }
1031
1032 void CallerExpectsNoAudio() {
1033 caller_audio_expectation_ = kExpectNoFrames;
1034 caller_audio_frames_expected_ = 0;
1035 }
1036
1037 // Caller's video functions.
1038 void CallerExpectsSomeVideo(
1039 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1040 caller_video_expectation_ = kExpectSomeFrames;
1041 caller_video_frames_expected_ = expected_video_frames;
1042 }
1043
1044 void CallerExpectsNoVideo() {
1045 caller_video_expectation_ = kExpectNoFrames;
1046 caller_video_frames_expected_ = 0;
1047 }
1048
1049 // Callee's audio functions.
1050 void CalleeExpectsSomeAudio(
1051 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1052 callee_audio_expectation_ = kExpectSomeFrames;
1053 callee_audio_frames_expected_ = expected_audio_frames;
1054 }
1055
1056 void CalleeExpectsNoAudio() {
1057 callee_audio_expectation_ = kExpectNoFrames;
1058 callee_audio_frames_expected_ = 0;
1059 }
1060
1061 // Callee's video functions.
1062 void CalleeExpectsSomeVideo(
1063 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1064 callee_video_expectation_ = kExpectSomeFrames;
1065 callee_video_frames_expected_ = expected_video_frames;
1066 }
1067
1068 void CalleeExpectsNoVideo() {
1069 callee_video_expectation_ = kExpectNoFrames;
1070 callee_video_frames_expected_ = 0;
1071 }
1072
1073 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1074 ExpectFrames caller_video_expectation_ = kNoExpectation;
1075 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1076 ExpectFrames callee_video_expectation_ = kNoExpectation;
1077 int caller_audio_frames_expected_ = 0;
1078 int caller_video_frames_expected_ = 0;
1079 int callee_audio_frames_expected_ = 0;
1080 int callee_video_frames_expected_ = 0;
1081};
1082
deadbeef1dcb1642017-03-29 21:08:16 -07001083// Tests two PeerConnections connecting to each other end-to-end, using a
1084// virtual network, fake A/V capture and fake encoder/decoders. The
1085// PeerConnections share the threads/socket servers, but use separate versions
1086// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001087class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001088 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001089 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1090 : sdp_semantics_(sdp_semantics),
1091 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001092 fss_(new rtc::FirewallSocketServer(ss_.get())),
1093 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001094 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001095 network_thread_->SetName("PCNetworkThread", this);
1096 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001097 RTC_CHECK(network_thread_->Start());
1098 RTC_CHECK(worker_thread_->Start());
1099 }
1100
Seth Hampson2f0d7022018-02-20 11:54:42 -08001101 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001102 if (caller_) {
1103 caller_->set_signaling_message_receiver(nullptr);
1104 }
1105 if (callee_) {
1106 callee_->set_signaling_message_receiver(nullptr);
1107 }
1108 }
1109
1110 bool SignalingStateStable() {
1111 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1112 }
1113
deadbeef71452802017-05-07 17:21:01 -07001114 bool DtlsConnected() {
1115 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1116 // are connected. This is an important distinction. Once we have separate
1117 // ICE and DTLS state, this check needs to use the DTLS state.
1118 return (callee()->ice_connection_state() ==
1119 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1120 callee()->ice_connection_state() ==
1121 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1122 (caller()->ice_connection_state() ==
1123 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1124 caller()->ice_connection_state() ==
1125 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1126 }
1127
Seth Hampson2f0d7022018-02-20 11:54:42 -08001128 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1129 const std::string& debug_name,
1130 const MediaConstraintsInterface* constraints,
1131 const PeerConnectionFactory::Options* options,
1132 const RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001133 webrtc::PeerConnectionDependencies dependencies) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001134 RTCConfiguration modified_config;
1135 if (config) {
1136 modified_config = *config;
1137 }
Steve Anton3acffc32018-04-12 17:21:03 -07001138 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001139 if (!dependencies.cert_generator) {
1140 dependencies.cert_generator =
1141 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001142 }
1143 std::unique_ptr<PeerConnectionWrapper> client(
1144 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001145
Seth Hampson2f0d7022018-02-20 11:54:42 -08001146 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001147 std::move(dependencies), network_thread_.get(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001148 worker_thread_.get())) {
1149 return nullptr;
1150 }
1151 return client;
1152 }
1153
deadbeef1dcb1642017-03-29 21:08:16 -07001154 bool CreatePeerConnectionWrappers() {
1155 return CreatePeerConnectionWrappersWithConfig(
1156 PeerConnectionInterface::RTCConfiguration(),
1157 PeerConnectionInterface::RTCConfiguration());
1158 }
1159
Steve Anton3acffc32018-04-12 17:21:03 -07001160 bool CreatePeerConnectionWrappersWithSdpSemantics(
1161 SdpSemantics caller_semantics,
1162 SdpSemantics callee_semantics) {
1163 // Can't specify the sdp_semantics in the passed-in configuration since it
1164 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1165 // stored in sdp_semantics_. So get around this by modifying the instance
1166 // variable before calling CreatePeerConnectionWrapper for the caller and
1167 // callee PeerConnections.
1168 SdpSemantics original_semantics = sdp_semantics_;
1169 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001170 caller_ = CreatePeerConnectionWrapper(
1171 "Caller", nullptr, nullptr, nullptr,
1172 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001173 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001174 callee_ = CreatePeerConnectionWrapper(
1175 "Callee", nullptr, nullptr, nullptr,
1176 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001177 sdp_semantics_ = original_semantics;
1178 return caller_ && callee_;
1179 }
1180
deadbeef1dcb1642017-03-29 21:08:16 -07001181 bool CreatePeerConnectionWrappersWithConstraints(
1182 MediaConstraintsInterface* caller_constraints,
1183 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001184 caller_ = CreatePeerConnectionWrapper(
1185 "Caller", caller_constraints, nullptr, nullptr,
1186 webrtc::PeerConnectionDependencies(nullptr));
1187 callee_ = CreatePeerConnectionWrapper(
1188 "Callee", callee_constraints, nullptr, nullptr,
1189 webrtc::PeerConnectionDependencies(nullptr));
1190
deadbeef1dcb1642017-03-29 21:08:16 -07001191 return caller_ && callee_;
1192 }
1193
1194 bool CreatePeerConnectionWrappersWithConfig(
1195 const PeerConnectionInterface::RTCConfiguration& caller_config,
1196 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001197 caller_ = CreatePeerConnectionWrapper(
1198 "Caller", nullptr, nullptr, &caller_config,
1199 webrtc::PeerConnectionDependencies(nullptr));
1200 callee_ = CreatePeerConnectionWrapper(
1201 "Callee", nullptr, nullptr, &callee_config,
1202 webrtc::PeerConnectionDependencies(nullptr));
1203 return caller_ && callee_;
1204 }
1205
1206 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1207 const PeerConnectionInterface::RTCConfiguration& caller_config,
1208 webrtc::PeerConnectionDependencies caller_dependencies,
1209 const PeerConnectionInterface::RTCConfiguration& callee_config,
1210 webrtc::PeerConnectionDependencies callee_dependencies) {
1211 caller_ =
1212 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
1213 std::move(caller_dependencies));
1214 callee_ =
1215 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
1216 std::move(callee_dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001217 return caller_ && callee_;
1218 }
1219
1220 bool CreatePeerConnectionWrappersWithOptions(
1221 const PeerConnectionFactory::Options& caller_options,
1222 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001223 caller_ = CreatePeerConnectionWrapper(
1224 "Caller", nullptr, &caller_options, nullptr,
1225 webrtc::PeerConnectionDependencies(nullptr));
1226 callee_ = CreatePeerConnectionWrapper(
1227 "Callee", nullptr, &callee_options, nullptr,
1228 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001229 return caller_ && callee_;
1230 }
1231
Seth Hampson2f0d7022018-02-20 11:54:42 -08001232 std::unique_ptr<PeerConnectionWrapper>
1233 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001234 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1235 new FakeRTCCertificateGenerator());
1236 cert_generator->use_alternate_key();
1237
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001238 webrtc::PeerConnectionDependencies dependencies(nullptr);
1239 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001240 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001241 std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001242 }
1243
1244 // Once called, SDP blobs and ICE candidates will be automatically signaled
1245 // between PeerConnections.
1246 void ConnectFakeSignaling() {
1247 caller_->set_signaling_message_receiver(callee_.get());
1248 callee_->set_signaling_message_receiver(caller_.get());
1249 }
1250
Steve Antonede9ca52017-10-16 13:04:27 -07001251 // Once called, SDP blobs will be automatically signaled between
1252 // PeerConnections. Note that ICE candidates will not be signaled unless they
1253 // are in the exchanged SDP blobs.
1254 void ConnectFakeSignalingForSdpOnly() {
1255 ConnectFakeSignaling();
1256 SetSignalIceCandidates(false);
1257 }
1258
deadbeef1dcb1642017-03-29 21:08:16 -07001259 void SetSignalingDelayMs(int delay_ms) {
1260 caller_->set_signaling_delay_ms(delay_ms);
1261 callee_->set_signaling_delay_ms(delay_ms);
1262 }
1263
Steve Antonede9ca52017-10-16 13:04:27 -07001264 void SetSignalIceCandidates(bool signal) {
1265 caller_->set_signal_ice_candidates(signal);
1266 callee_->set_signal_ice_candidates(signal);
1267 }
1268
deadbeef1dcb1642017-03-29 21:08:16 -07001269 // Messages may get lost on the unreliable DataChannel, so we send multiple
1270 // times to avoid test flakiness.
1271 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1272 const std::string& data,
1273 int retries) {
1274 for (int i = 0; i < retries; ++i) {
1275 dc->Send(DataBuffer(data));
1276 }
1277 }
1278
1279 rtc::Thread* network_thread() { return network_thread_.get(); }
1280
1281 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1282
1283 PeerConnectionWrapper* caller() { return caller_.get(); }
1284
1285 // Set the |caller_| to the |wrapper| passed in and return the
1286 // original |caller_|.
1287 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1288 PeerConnectionWrapper* wrapper) {
1289 PeerConnectionWrapper* old = caller_.release();
1290 caller_.reset(wrapper);
1291 return old;
1292 }
1293
1294 PeerConnectionWrapper* callee() { return callee_.get(); }
1295
1296 // Set the |callee_| to the |wrapper| passed in and return the
1297 // original |callee_|.
1298 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1299 PeerConnectionWrapper* wrapper) {
1300 PeerConnectionWrapper* old = callee_.release();
1301 callee_.reset(wrapper);
1302 return old;
1303 }
1304
Steve Antonede9ca52017-10-16 13:04:27 -07001305 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1306
Seth Hampson2f0d7022018-02-20 11:54:42 -08001307 // Expects the provided number of new frames to be received within
1308 // kMaxWaitForFramesMs. The new expected frames are specified in
1309 // |media_expectations|. Returns false if any of the expectations were
1310 // not met.
1311 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1312 // First initialize the expected frame counts based upon the current
1313 // frame count.
1314 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1315 if (media_expectations.caller_audio_expectation_ ==
1316 MediaExpectations::kExpectSomeFrames) {
1317 total_caller_audio_frames_expected +=
1318 media_expectations.caller_audio_frames_expected_;
1319 }
1320 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001321 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 if (media_expectations.caller_video_expectation_ ==
1323 MediaExpectations::kExpectSomeFrames) {
1324 total_caller_video_frames_expected +=
1325 media_expectations.caller_video_frames_expected_;
1326 }
1327 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1328 if (media_expectations.callee_audio_expectation_ ==
1329 MediaExpectations::kExpectSomeFrames) {
1330 total_callee_audio_frames_expected +=
1331 media_expectations.callee_audio_frames_expected_;
1332 }
1333 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001334 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001335 if (media_expectations.callee_video_expectation_ ==
1336 MediaExpectations::kExpectSomeFrames) {
1337 total_callee_video_frames_expected +=
1338 media_expectations.callee_video_frames_expected_;
1339 }
deadbeef1dcb1642017-03-29 21:08:16 -07001340
Seth Hampson2f0d7022018-02-20 11:54:42 -08001341 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001342 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001343 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001344 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001345 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001346 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001347 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001348 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001349 total_callee_video_frames_expected,
1350 kMaxWaitForFramesMs);
1351 bool expectations_correct =
1352 caller()->audio_frames_received() >=
1353 total_caller_audio_frames_expected &&
1354 caller()->min_video_frames_received_per_track() >=
1355 total_caller_video_frames_expected &&
1356 callee()->audio_frames_received() >=
1357 total_callee_audio_frames_expected &&
1358 callee()->min_video_frames_received_per_track() >=
1359 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001360
Seth Hampson2f0d7022018-02-20 11:54:42 -08001361 // After the combined wait, print out a more detailed message upon
1362 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001363 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001364 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001365 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001366 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001367 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001368 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001369 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001370 total_callee_video_frames_expected);
1371
1372 // We want to make sure nothing unexpected was received.
1373 if (media_expectations.caller_audio_expectation_ ==
1374 MediaExpectations::kExpectNoFrames) {
1375 EXPECT_EQ(caller()->audio_frames_received(),
1376 total_caller_audio_frames_expected);
1377 if (caller()->audio_frames_received() !=
1378 total_caller_audio_frames_expected) {
1379 expectations_correct = false;
1380 }
1381 }
1382 if (media_expectations.caller_video_expectation_ ==
1383 MediaExpectations::kExpectNoFrames) {
1384 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1385 total_caller_video_frames_expected);
1386 if (caller()->min_video_frames_received_per_track() !=
1387 total_caller_video_frames_expected) {
1388 expectations_correct = false;
1389 }
1390 }
1391 if (media_expectations.callee_audio_expectation_ ==
1392 MediaExpectations::kExpectNoFrames) {
1393 EXPECT_EQ(callee()->audio_frames_received(),
1394 total_callee_audio_frames_expected);
1395 if (callee()->audio_frames_received() !=
1396 total_callee_audio_frames_expected) {
1397 expectations_correct = false;
1398 }
1399 }
1400 if (media_expectations.callee_video_expectation_ ==
1401 MediaExpectations::kExpectNoFrames) {
1402 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1403 total_callee_video_frames_expected);
1404 if (callee()->min_video_frames_received_per_track() !=
1405 total_callee_video_frames_expected) {
1406 expectations_correct = false;
1407 }
1408 }
1409 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001410 }
1411
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001412 void TestNegotiatedCipherSuite(
1413 const PeerConnectionFactory::Options& caller_options,
1414 const PeerConnectionFactory::Options& callee_options,
1415 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001416 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1417 callee_options));
1418 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1419 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1420 caller()->pc()->RegisterUMAObserver(caller_observer);
1421 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001422 caller()->AddAudioVideoTracks();
1423 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001424 caller()->CreateAndSetAndSignalOffer();
1425 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1426 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001427 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001428 EXPECT_EQ(
1429 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1430 expected_cipher_suite));
1431 caller()->pc()->RegisterUMAObserver(nullptr);
1432 }
1433
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001434 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1435 bool remote_gcm_enabled,
1436 int expected_cipher_suite) {
1437 PeerConnectionFactory::Options caller_options;
1438 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1439 PeerConnectionFactory::Options callee_options;
1440 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1441 TestNegotiatedCipherSuite(caller_options, callee_options,
1442 expected_cipher_suite);
1443 }
1444
Seth Hampson2f0d7022018-02-20 11:54:42 -08001445 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001446 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001447
deadbeef1dcb1642017-03-29 21:08:16 -07001448 private:
1449 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001450 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001451 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001452 // |network_thread_| and |worker_thread_| are used by both
1453 // |caller_| and |callee_| so they must be destroyed
1454 // later.
1455 std::unique_ptr<rtc::Thread> network_thread_;
1456 std::unique_ptr<rtc::Thread> worker_thread_;
1457 std::unique_ptr<PeerConnectionWrapper> caller_;
1458 std::unique_ptr<PeerConnectionWrapper> callee_;
1459};
1460
Seth Hampson2f0d7022018-02-20 11:54:42 -08001461class PeerConnectionIntegrationTest
1462 : public PeerConnectionIntegrationBaseTest,
1463 public ::testing::WithParamInterface<SdpSemantics> {
1464 protected:
1465 PeerConnectionIntegrationTest()
1466 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1467};
1468
1469class PeerConnectionIntegrationTestPlanB
1470 : public PeerConnectionIntegrationBaseTest {
1471 protected:
1472 PeerConnectionIntegrationTestPlanB()
1473 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1474};
1475
1476class PeerConnectionIntegrationTestUnifiedPlan
1477 : public PeerConnectionIntegrationBaseTest {
1478 protected:
1479 PeerConnectionIntegrationTestUnifiedPlan()
1480 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1481};
1482
deadbeef1dcb1642017-03-29 21:08:16 -07001483// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1484// includes testing that the callback is invoked if an observer is connected
1485// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001487 RtpReceiverObserverOnFirstPacketReceived) {
1488 ASSERT_TRUE(CreatePeerConnectionWrappers());
1489 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001490 caller()->AddAudioVideoTracks();
1491 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001492 // Start offer/answer exchange and wait for it to complete.
1493 caller()->CreateAndSetAndSignalOffer();
1494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1495 // Should be one receiver each for audio/video.
1496 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1497 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1498 // Wait for all "first packet received" callbacks to be fired.
1499 EXPECT_TRUE_WAIT(
1500 std::all_of(caller()->rtp_receiver_observers().begin(),
1501 caller()->rtp_receiver_observers().end(),
1502 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1503 return o->first_packet_received();
1504 }),
1505 kMaxWaitForFramesMs);
1506 EXPECT_TRUE_WAIT(
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 kMaxWaitForFramesMs);
1513 // If new observers are set after the first packet was already received, the
1514 // callback should still be invoked.
1515 caller()->ResetRtpReceiverObservers();
1516 callee()->ResetRtpReceiverObservers();
1517 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1518 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1519 EXPECT_TRUE(
1520 std::all_of(caller()->rtp_receiver_observers().begin(),
1521 caller()->rtp_receiver_observers().end(),
1522 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1523 return o->first_packet_received();
1524 }));
1525 EXPECT_TRUE(
1526 std::all_of(callee()->rtp_receiver_observers().begin(),
1527 callee()->rtp_receiver_observers().end(),
1528 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1529 return o->first_packet_received();
1530 }));
1531}
1532
1533class DummyDtmfObserver : public DtmfSenderObserverInterface {
1534 public:
1535 DummyDtmfObserver() : completed_(false) {}
1536
1537 // Implements DtmfSenderObserverInterface.
1538 void OnToneChange(const std::string& tone) override {
1539 tones_.push_back(tone);
1540 if (tone.empty()) {
1541 completed_ = true;
1542 }
1543 }
1544
1545 const std::vector<std::string>& tones() const { return tones_; }
1546 bool completed() const { return completed_; }
1547
1548 private:
1549 bool completed_;
1550 std::vector<std::string> tones_;
1551};
1552
1553// Assumes |sender| already has an audio track added and the offer/answer
1554// exchange is done.
1555void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1556 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001557 // We should be able to get a DTMF sender from the local sender.
1558 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1559 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1560 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001561 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001562 dtmf_sender->RegisterObserver(&observer);
1563
1564 // Test the DtmfSender object just created.
1565 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1566 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1567
1568 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1569 std::vector<std::string> tones = {"1", "a", ""};
1570 EXPECT_EQ(tones, observer.tones());
1571 dtmf_sender->UnregisterObserver();
1572 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1573}
1574
1575// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1576// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001577TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001578 ASSERT_TRUE(CreatePeerConnectionWrappers());
1579 ConnectFakeSignaling();
1580 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001581 caller()->AddAudioTrack();
1582 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001583 caller()->CreateAndSetAndSignalOffer();
1584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001585 // DTLS must finish before the DTMF sender can be used reliably.
1586 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001587 TestDtmfFromSenderToReceiver(caller(), callee());
1588 TestDtmfFromSenderToReceiver(callee(), caller());
1589}
1590
1591// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1592// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001593TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001594 ASSERT_TRUE(CreatePeerConnectionWrappers());
1595 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001596 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1597 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1598 caller()->pc()->RegisterUMAObserver(caller_observer);
1599
deadbeef1dcb1642017-03-29 21:08:16 -07001600 // Do normal offer/answer and wait for some frames to be received in each
1601 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001602 caller()->AddAudioVideoTracks();
1603 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001604 caller()->CreateAndSetAndSignalOffer();
1605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001606 MediaExpectations media_expectations;
1607 media_expectations.ExpectBidirectionalAudioAndVideo();
1608 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001609 EXPECT_LE(
1610 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1611 webrtc::kEnumCounterKeyProtocolDtls));
1612 EXPECT_EQ(
1613 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1614 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001615}
1616
1617// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001618TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001619 PeerConnectionInterface::RTCConfiguration sdes_config;
1620 sdes_config.enable_dtls_srtp.emplace(false);
1621 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1622 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001623 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1624 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1625 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001626
1627 // Do normal offer/answer and wait for some frames to be received in each
1628 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001629 caller()->AddAudioVideoTracks();
1630 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001631 caller()->CreateAndSetAndSignalOffer();
1632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001633 MediaExpectations media_expectations;
1634 media_expectations.ExpectBidirectionalAudioAndVideo();
1635 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001636 EXPECT_LE(
1637 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1638 webrtc::kEnumCounterKeyProtocolSdes));
1639 EXPECT_EQ(
1640 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1641 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001642}
1643
Steve Anton8c0f7a72017-10-03 10:03:10 -07001644// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1645// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001646TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001647 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1648 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1649 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1650 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1651 return pc->GetRemoteAudioSSLCertificate();
1652 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001653 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1654 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1655 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1656 return pc->GetRemoteAudioSSLCertChain();
1657 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001658
1659 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1660 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1661
1662 // Configure each side with a known certificate so they can be compared later.
1663 PeerConnectionInterface::RTCConfiguration caller_config;
1664 caller_config.enable_dtls_srtp.emplace(true);
1665 caller_config.certificates.push_back(caller_cert);
1666 PeerConnectionInterface::RTCConfiguration callee_config;
1667 callee_config.enable_dtls_srtp.emplace(true);
1668 callee_config.certificates.push_back(callee_cert);
1669 ASSERT_TRUE(
1670 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1671 ConnectFakeSignaling();
1672
1673 // When first initialized, there should not be a remote SSL certificate (and
1674 // calling this method should not crash).
1675 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1676 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001677 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1678 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001679
Steve Anton15324772018-01-16 10:26:49 -08001680 caller()->AddAudioTrack();
1681 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001682 caller()->CreateAndSetAndSignalOffer();
1683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1684 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1685
1686 // Once DTLS has been connected, each side should return the other's SSL
1687 // certificate when calling GetRemoteAudioSSLCertificate.
1688
1689 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1690 ASSERT_TRUE(caller_remote_cert);
1691 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1692 caller_remote_cert->ToPEMString());
1693
1694 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1695 ASSERT_TRUE(callee_remote_cert);
1696 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1697 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001698
1699 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1700 ASSERT_TRUE(caller_remote_cert_chain);
1701 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1702 auto remote_cert = &caller_remote_cert_chain->Get(0);
1703 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1704 remote_cert->ToPEMString());
1705
1706 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1707 ASSERT_TRUE(callee_remote_cert_chain);
1708 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1709 remote_cert = &callee_remote_cert_chain->Get(0);
1710 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1711 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001712}
1713
deadbeef1dcb1642017-03-29 21:08:16 -07001714// This test sets up a call between two parties with a source resolution of
1715// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001716TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001717 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1718 ASSERT_TRUE(CreatePeerConnectionWrappers());
1719 ConnectFakeSignaling();
1720
Niels Möller5c7efe72018-05-11 10:34:46 +02001721 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1722 webrtc::FakePeriodicVideoSource::Config config;
1723 config.width = 1280;
1724 config.height = 720;
1725 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1726 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001727
1728 // Do normal offer/answer and wait for at least one frame to be received in
1729 // each direction.
1730 caller()->CreateAndSetAndSignalOffer();
1731 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1732 callee()->min_video_frames_received_per_track() > 0,
1733 kMaxWaitForFramesMs);
1734
1735 // Check rendered aspect ratio.
1736 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1737 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1738 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1739 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1740}
1741
1742// This test sets up an one-way call, with media only from caller to
1743// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001744TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001745 ASSERT_TRUE(CreatePeerConnectionWrappers());
1746 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001747 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001748 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001749 MediaExpectations media_expectations;
1750 media_expectations.CalleeExpectsSomeAudioAndVideo();
1751 media_expectations.CallerExpectsNoAudio();
1752 media_expectations.CallerExpectsNoVideo();
1753 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001754}
1755
1756// This test sets up a audio call initially, with the callee rejecting video
1757// initially. Then later the callee decides to upgrade to audio/video, and
1758// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001759TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001760 ASSERT_TRUE(CreatePeerConnectionWrappers());
1761 ConnectFakeSignaling();
1762 // Initially, offer an audio/video stream from the caller, but refuse to
1763 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001764 caller()->AddAudioVideoTracks();
1765 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001766 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1767 PeerConnectionInterface::RTCOfferAnswerOptions options;
1768 options.offer_to_receive_video = 0;
1769 callee()->SetOfferAnswerOptions(options);
1770 } else {
1771 callee()->SetRemoteOfferHandler([this] {
1772 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1773 });
1774 }
deadbeef1dcb1642017-03-29 21:08:16 -07001775 // Do offer/answer and make sure audio is still received end-to-end.
1776 caller()->CreateAndSetAndSignalOffer();
1777 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778 {
1779 MediaExpectations media_expectations;
1780 media_expectations.ExpectBidirectionalAudio();
1781 media_expectations.ExpectNoVideo();
1782 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1783 }
deadbeef1dcb1642017-03-29 21:08:16 -07001784 // Sanity check that the callee's description has a rejected video section.
1785 ASSERT_NE(nullptr, callee()->pc()->local_description());
1786 const ContentInfo* callee_video_content =
1787 GetFirstVideoContent(callee()->pc()->local_description()->description());
1788 ASSERT_NE(nullptr, callee_video_content);
1789 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001790
deadbeef1dcb1642017-03-29 21:08:16 -07001791 // Now negotiate with video and ensure negotiation succeeds, with video
1792 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001793 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001794 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1795 PeerConnectionInterface::RTCOfferAnswerOptions options;
1796 options.offer_to_receive_video = 1;
1797 callee()->SetOfferAnswerOptions(options);
1798 } else {
1799 callee()->SetRemoteOfferHandler(nullptr);
1800 caller()->SetRemoteOfferHandler([this] {
1801 // The caller creates a new transceiver to receive video on when receiving
1802 // the offer, but by default it is send only.
1803 auto transceivers = caller()->pc()->GetTransceivers();
1804 ASSERT_EQ(3, transceivers.size());
1805 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1806 transceivers[2]->receiver()->media_type());
1807 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1808 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1809 });
1810 }
deadbeef1dcb1642017-03-29 21:08:16 -07001811 callee()->CreateAndSetAndSignalOffer();
1812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001813 {
1814 // Expect additional audio frames to be received after the upgrade.
1815 MediaExpectations media_expectations;
1816 media_expectations.ExpectBidirectionalAudioAndVideo();
1817 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1818 }
deadbeef1dcb1642017-03-29 21:08:16 -07001819}
1820
deadbeef4389b4d2017-09-07 09:07:36 -07001821// Simpler than the above test; just add an audio track to an established
1822// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001823TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001824 ASSERT_TRUE(CreatePeerConnectionWrappers());
1825 ConnectFakeSignaling();
1826 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001827 caller()->AddVideoTrack();
1828 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001829 caller()->CreateAndSetAndSignalOffer();
1830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1831 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001832 caller()->AddAudioTrack();
1833 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001834 caller()->CreateAndSetAndSignalOffer();
1835 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1836 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001837 MediaExpectations media_expectations;
1838 media_expectations.ExpectBidirectionalAudioAndVideo();
1839 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001840}
1841
deadbeef1dcb1642017-03-29 21:08:16 -07001842// This test sets up a call that's transferred to a new caller with a different
1843// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001844TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001845 ASSERT_TRUE(CreatePeerConnectionWrappers());
1846 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001847 caller()->AddAudioVideoTracks();
1848 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001849 caller()->CreateAndSetAndSignalOffer();
1850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1851
1852 // Keep the original peer around which will still send packets to the
1853 // receiving client. These SRTP packets will be dropped.
1854 std::unique_ptr<PeerConnectionWrapper> original_peer(
1855 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001856 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001857 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1858 // directly above.
1859 original_peer->pc()->Close();
1860
1861 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001862 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001863 caller()->CreateAndSetAndSignalOffer();
1864 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1865 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001866 MediaExpectations media_expectations;
1867 media_expectations.ExpectBidirectionalAudioAndVideo();
1868 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001869}
1870
1871// This test sets up a call that's transferred to a new callee with a different
1872// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001873TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001874 ASSERT_TRUE(CreatePeerConnectionWrappers());
1875 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001876 caller()->AddAudioVideoTracks();
1877 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001878 caller()->CreateAndSetAndSignalOffer();
1879 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1880
1881 // Keep the original peer around which will still send packets to the
1882 // receiving client. These SRTP packets will be dropped.
1883 std::unique_ptr<PeerConnectionWrapper> original_peer(
1884 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001885 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001886 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1887 // directly above.
1888 original_peer->pc()->Close();
1889
1890 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001891 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001892 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1893 caller()->CreateAndSetAndSignalOffer();
1894 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1895 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001896 MediaExpectations media_expectations;
1897 media_expectations.ExpectBidirectionalAudioAndVideo();
1898 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001899}
1900
1901// This test sets up a non-bundled call and negotiates bundling at the same
1902// time as starting an ICE restart. When bundling is in effect in the restart,
1903// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001904TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001905 ASSERT_TRUE(CreatePeerConnectionWrappers());
1906 ConnectFakeSignaling();
1907
Steve Anton15324772018-01-16 10:26:49 -08001908 caller()->AddAudioVideoTracks();
1909 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001910 // Remove the bundle group from the SDP received by the callee.
1911 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1912 desc->RemoveGroupByName("BUNDLE");
1913 });
1914 caller()->CreateAndSetAndSignalOffer();
1915 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001916 {
1917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1920 }
deadbeef1dcb1642017-03-29 21:08:16 -07001921 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1922 callee()->SetReceivedSdpMunger(nullptr);
1923 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1924 caller()->CreateAndSetAndSignalOffer();
1925 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1926
1927 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001928 {
1929 MediaExpectations media_expectations;
1930 media_expectations.ExpectBidirectionalAudioAndVideo();
1931 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1932 }
deadbeef1dcb1642017-03-29 21:08:16 -07001933}
1934
1935// Test CVO (Coordination of Video Orientation). If a video source is rotated
1936// and both peers support the CVO RTP header extension, the actual video frames
1937// don't need to be encoded in different resolutions, since the rotation is
1938// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001939TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001940 ASSERT_TRUE(CreatePeerConnectionWrappers());
1941 ConnectFakeSignaling();
1942 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001943 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001944 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001945 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001946 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1947
1948 // Wait for video frames to be received by both sides.
1949 caller()->CreateAndSetAndSignalOffer();
1950 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1951 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1952 callee()->min_video_frames_received_per_track() > 0,
1953 kMaxWaitForFramesMs);
1954
1955 // Ensure that the aspect ratio is unmodified.
1956 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1957 // not just assumed.
1958 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1959 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1960 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1961 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1962 // Ensure that the CVO bits were surfaced to the renderer.
1963 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1964 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1965}
1966
1967// Test that when the CVO extension isn't supported, video is rotated the
1968// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001969TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001970 ASSERT_TRUE(CreatePeerConnectionWrappers());
1971 ConnectFakeSignaling();
1972 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001973 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001974 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001975 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001976 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1977
1978 // Remove the CVO extension from the offered SDP.
1979 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1980 cricket::VideoContentDescription* video =
1981 GetFirstVideoContentDescription(desc);
1982 video->ClearRtpHeaderExtensions();
1983 });
1984 // Wait for video frames to be received by both sides.
1985 caller()->CreateAndSetAndSignalOffer();
1986 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1987 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1988 callee()->min_video_frames_received_per_track() > 0,
1989 kMaxWaitForFramesMs);
1990
1991 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1992 // rotation.
1993 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1994 // not just assumed.
1995 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1996 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1997 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1998 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1999 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2000 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2001 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2002}
2003
deadbeef1dcb1642017-03-29 21:08:16 -07002004// Test that if the answerer rejects the audio m= section, no audio is sent or
2005// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002006TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002007 ASSERT_TRUE(CreatePeerConnectionWrappers());
2008 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002009 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002010 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2011 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2012 // it will reject the audio m= section completely.
2013 PeerConnectionInterface::RTCOfferAnswerOptions options;
2014 options.offer_to_receive_audio = 0;
2015 callee()->SetOfferAnswerOptions(options);
2016 } else {
2017 // Stopping the audio RtpTransceiver will cause the media section to be
2018 // rejected in the answer.
2019 callee()->SetRemoteOfferHandler([this] {
2020 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2021 });
2022 }
Steve Anton15324772018-01-16 10:26:49 -08002023 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002024 // Do offer/answer and wait for successful end-to-end video frames.
2025 caller()->CreateAndSetAndSignalOffer();
2026 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 MediaExpectations media_expectations;
2028 media_expectations.ExpectBidirectionalVideo();
2029 media_expectations.ExpectNoAudio();
2030 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2031
deadbeef1dcb1642017-03-29 21:08:16 -07002032 // Sanity check that the callee's description has a rejected audio section.
2033 ASSERT_NE(nullptr, callee()->pc()->local_description());
2034 const ContentInfo* callee_audio_content =
2035 GetFirstAudioContent(callee()->pc()->local_description()->description());
2036 ASSERT_NE(nullptr, callee_audio_content);
2037 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002038 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2039 // The caller's transceiver should have stopped after receiving the answer.
2040 EXPECT_TRUE(caller()
2041 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2042 ->stopped());
2043 }
deadbeef1dcb1642017-03-29 21:08:16 -07002044}
2045
2046// Test that if the answerer rejects the video m= section, no video is sent or
2047// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002049 ASSERT_TRUE(CreatePeerConnectionWrappers());
2050 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002051 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002052 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2053 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2054 // it will reject the video m= section completely.
2055 PeerConnectionInterface::RTCOfferAnswerOptions options;
2056 options.offer_to_receive_video = 0;
2057 callee()->SetOfferAnswerOptions(options);
2058 } else {
2059 // Stopping the video RtpTransceiver will cause the media section to be
2060 // rejected in the answer.
2061 callee()->SetRemoteOfferHandler([this] {
2062 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2063 });
2064 }
Steve Anton15324772018-01-16 10:26:49 -08002065 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002066 // Do offer/answer and wait for successful end-to-end audio frames.
2067 caller()->CreateAndSetAndSignalOffer();
2068 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002069 MediaExpectations media_expectations;
2070 media_expectations.ExpectBidirectionalAudio();
2071 media_expectations.ExpectNoVideo();
2072 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2073
deadbeef1dcb1642017-03-29 21:08:16 -07002074 // Sanity check that the callee's description has a rejected video section.
2075 ASSERT_NE(nullptr, callee()->pc()->local_description());
2076 const ContentInfo* callee_video_content =
2077 GetFirstVideoContent(callee()->pc()->local_description()->description());
2078 ASSERT_NE(nullptr, callee_video_content);
2079 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002080 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2081 // The caller's transceiver should have stopped after receiving the answer.
2082 EXPECT_TRUE(caller()
2083 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2084 ->stopped());
2085 }
deadbeef1dcb1642017-03-29 21:08:16 -07002086}
2087
2088// Test that if the answerer rejects both audio and video m= sections, nothing
2089// bad happens.
2090// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2091// test anything but the fact that negotiation succeeds, which doesn't mean
2092// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002093TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002094 ASSERT_TRUE(CreatePeerConnectionWrappers());
2095 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002096 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2098 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2099 // will reject both audio and video m= sections.
2100 PeerConnectionInterface::RTCOfferAnswerOptions options;
2101 options.offer_to_receive_audio = 0;
2102 options.offer_to_receive_video = 0;
2103 callee()->SetOfferAnswerOptions(options);
2104 } else {
2105 callee()->SetRemoteOfferHandler([this] {
2106 // Stopping all transceivers will cause all media sections to be rejected.
2107 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2108 transceiver->Stop();
2109 }
2110 });
2111 }
deadbeef1dcb1642017-03-29 21:08:16 -07002112 // Do offer/answer and wait for stable signaling state.
2113 caller()->CreateAndSetAndSignalOffer();
2114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002115
deadbeef1dcb1642017-03-29 21:08:16 -07002116 // Sanity check that the callee's description has rejected m= sections.
2117 ASSERT_NE(nullptr, callee()->pc()->local_description());
2118 const ContentInfo* callee_audio_content =
2119 GetFirstAudioContent(callee()->pc()->local_description()->description());
2120 ASSERT_NE(nullptr, callee_audio_content);
2121 EXPECT_TRUE(callee_audio_content->rejected);
2122 const ContentInfo* callee_video_content =
2123 GetFirstVideoContent(callee()->pc()->local_description()->description());
2124 ASSERT_NE(nullptr, callee_video_content);
2125 EXPECT_TRUE(callee_video_content->rejected);
2126}
2127
2128// This test sets up an audio and video call between two parties. After the
2129// call runs for a while, the caller sends an updated offer with video being
2130// rejected. Once the re-negotiation is done, the video flow should stop and
2131// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002132TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002133 ASSERT_TRUE(CreatePeerConnectionWrappers());
2134 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002135 caller()->AddAudioVideoTracks();
2136 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002137 caller()->CreateAndSetAndSignalOffer();
2138 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002139 {
2140 MediaExpectations media_expectations;
2141 media_expectations.ExpectBidirectionalAudioAndVideo();
2142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2143 }
deadbeef1dcb1642017-03-29 21:08:16 -07002144 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002145 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2146 caller()->SetGeneratedSdpMunger(
2147 [](cricket::SessionDescription* description) {
2148 for (cricket::ContentInfo& content : description->contents()) {
2149 if (cricket::IsVideoContent(&content)) {
2150 content.rejected = true;
2151 }
2152 }
2153 });
2154 } else {
2155 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2156 }
deadbeef1dcb1642017-03-29 21:08:16 -07002157 caller()->CreateAndSetAndSignalOffer();
2158 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2159
2160 // Sanity check that the caller's description has a rejected video section.
2161 ASSERT_NE(nullptr, caller()->pc()->local_description());
2162 const ContentInfo* caller_video_content =
2163 GetFirstVideoContent(caller()->pc()->local_description()->description());
2164 ASSERT_NE(nullptr, caller_video_content);
2165 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002166 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002167 {
2168 MediaExpectations media_expectations;
2169 media_expectations.ExpectBidirectionalAudio();
2170 media_expectations.ExpectNoVideo();
2171 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2172 }
deadbeef1dcb1642017-03-29 21:08:16 -07002173}
2174
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002175// Do one offer/answer with audio, another that disables it (rejecting the m=
2176// section), and another that re-enables it. Regression test for:
2177// bugs.webrtc.org/6023
2178TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2179 ASSERT_TRUE(CreatePeerConnectionWrappers());
2180 ConnectFakeSignaling();
2181
2182 // Add audio track, do normal offer/answer.
2183 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2184 caller()->CreateLocalAudioTrack();
2185 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2186 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2187 caller()->CreateAndSetAndSignalOffer();
2188 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2189
2190 // Remove audio track, and set offer_to_receive_audio to false to cause the
2191 // m= section to be completely disabled, not just "recvonly".
2192 caller()->pc()->RemoveTrack(sender);
2193 PeerConnectionInterface::RTCOfferAnswerOptions options;
2194 options.offer_to_receive_audio = 0;
2195 caller()->SetOfferAnswerOptions(options);
2196 caller()->CreateAndSetAndSignalOffer();
2197 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2198
2199 // Add the audio track again, expecting negotiation to succeed and frames to
2200 // flow.
2201 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2202 options.offer_to_receive_audio = 1;
2203 caller()->SetOfferAnswerOptions(options);
2204 caller()->CreateAndSetAndSignalOffer();
2205 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2206
2207 MediaExpectations media_expectations;
2208 media_expectations.CalleeExpectsSomeAudio();
2209 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2210}
2211
deadbeef1dcb1642017-03-29 21:08:16 -07002212// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2213// is needed to support legacy endpoints.
2214// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2215// add a test for an end-to-end test without MID signaling either (basically,
2216// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002217TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002218 ASSERT_TRUE(CreatePeerConnectionWrappers());
2219 ConnectFakeSignaling();
2220 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002221 caller()->AddAudioVideoTracks();
2222 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002223 // Remove SSRCs and MSIDs from the received offer SDP.
2224 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002225 caller()->CreateAndSetAndSignalOffer();
2226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002227 MediaExpectations media_expectations;
2228 media_expectations.ExpectBidirectionalAudioAndVideo();
2229 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002230}
2231
Seth Hampson5897a6e2018-04-03 11:16:33 -07002232// Basic end-to-end test, without SSRC signaling. This means that the track
2233// was created properly and frames are delivered when the MSIDs are communicated
2234// with a=msid lines and no a=ssrc lines.
2235TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2236 EndToEndCallWithoutSsrcSignaling) {
2237 const char kStreamId[] = "streamId";
2238 ASSERT_TRUE(CreatePeerConnectionWrappers());
2239 ConnectFakeSignaling();
2240 // Add just audio tracks.
2241 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2242 callee()->AddAudioTrack();
2243
2244 // Remove SSRCs from the received offer SDP.
2245 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2248 MediaExpectations media_expectations;
2249 media_expectations.ExpectBidirectionalAudio();
2250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2251}
2252
Steve Antondf527fd2018-04-27 15:52:03 -07002253// Tests that video flows between multiple video tracks when SSRCs are not
2254// signaled. This exercises the MID RTP header extension which is needed to
2255// demux the incoming video tracks.
2256TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2257 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2258 ASSERT_TRUE(CreatePeerConnectionWrappers());
2259 ConnectFakeSignaling();
2260 caller()->AddVideoTrack();
2261 caller()->AddVideoTrack();
2262 callee()->AddVideoTrack();
2263 callee()->AddVideoTrack();
2264
2265 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2266 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2267 caller()->CreateAndSetAndSignalOffer();
2268 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2269 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2270 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2271
2272 // Expect video to be received in both directions on both tracks.
2273 MediaExpectations media_expectations;
2274 media_expectations.ExpectBidirectionalVideo();
2275 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2276}
2277
deadbeef1dcb1642017-03-29 21:08:16 -07002278// Test that if two video tracks are sent (from caller to callee, in this test),
2279// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002280TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002281 ASSERT_TRUE(CreatePeerConnectionWrappers());
2282 ConnectFakeSignaling();
2283 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002284 caller()->AddAudioVideoTracks();
2285 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002286 caller()->CreateAndSetAndSignalOffer();
2287 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002288 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002289
2290 MediaExpectations media_expectations;
2291 media_expectations.CalleeExpectsSomeAudioAndVideo();
2292 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002293}
2294
2295static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2296 bool first = true;
2297 for (cricket::ContentInfo& content : desc->contents()) {
2298 if (first) {
2299 first = false;
2300 continue;
2301 }
2302 content.bundle_only = true;
2303 }
2304 first = true;
2305 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2306 if (first) {
2307 first = false;
2308 continue;
2309 }
2310 transport.description.ice_ufrag.clear();
2311 transport.description.ice_pwd.clear();
2312 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2313 transport.description.identity_fingerprint.reset(nullptr);
2314 }
2315}
2316
2317// Test that if applying a true "max bundle" offer, which uses ports of 0,
2318// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2319// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2320// successfully and media flows.
2321// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2322// TODO(deadbeef): Won't need this test once we start generating actual
2323// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002324TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002325 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2326 ASSERT_TRUE(CreatePeerConnectionWrappers());
2327 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002328 caller()->AddAudioVideoTracks();
2329 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002330 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2331 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2332 // but the first m= section.
2333 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2334 caller()->CreateAndSetAndSignalOffer();
2335 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002336 MediaExpectations media_expectations;
2337 media_expectations.ExpectBidirectionalAudioAndVideo();
2338 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002339}
2340
2341// Test that we can receive the audio output level from a remote audio track.
2342// TODO(deadbeef): Use a fake audio source and verify that the output level is
2343// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002344TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002345 ASSERT_TRUE(CreatePeerConnectionWrappers());
2346 ConnectFakeSignaling();
2347 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002348 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002349 caller()->CreateAndSetAndSignalOffer();
2350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2351
2352 // Get the audio output level stats. Note that the level is not available
2353 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002354 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002355 kMaxWaitForFramesMs);
2356}
2357
2358// Test that an audio input level is reported.
2359// TODO(deadbeef): Use a fake audio source and verify that the input level is
2360// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002361TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002362 ASSERT_TRUE(CreatePeerConnectionWrappers());
2363 ConnectFakeSignaling();
2364 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002365 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002366 caller()->CreateAndSetAndSignalOffer();
2367 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2368
2369 // Get the audio input level stats. The level should be available very
2370 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002371 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002372 kMaxWaitForStatsMs);
2373}
2374
2375// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002376TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002377 ASSERT_TRUE(CreatePeerConnectionWrappers());
2378 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002379 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002380 // Do offer/answer, wait for the callee to receive some frames.
2381 caller()->CreateAndSetAndSignalOffer();
2382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002383
2384 MediaExpectations media_expectations;
2385 media_expectations.CalleeExpectsSomeAudioAndVideo();
2386 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002387
2388 // Get a handle to the remote tracks created, so they can be used as GetStats
2389 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002390 for (auto receiver : callee()->pc()->GetReceivers()) {
2391 // We received frames, so we definitely should have nonzero "received bytes"
2392 // stats at this point.
2393 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2394 0);
2395 }
deadbeef1dcb1642017-03-29 21:08:16 -07002396}
2397
2398// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002399TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002400 ASSERT_TRUE(CreatePeerConnectionWrappers());
2401 ConnectFakeSignaling();
2402 auto audio_track = caller()->CreateLocalAudioTrack();
2403 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002404 caller()->AddTrack(audio_track);
2405 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002406 // Do offer/answer, wait for the callee to receive some frames.
2407 caller()->CreateAndSetAndSignalOffer();
2408 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002409 MediaExpectations media_expectations;
2410 media_expectations.CalleeExpectsSomeAudioAndVideo();
2411 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002412
2413 // The callee received frames, so we definitely should have nonzero "sent
2414 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002415 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2416 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2417}
2418
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002419// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002420TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002421 ASSERT_TRUE(CreatePeerConnectionWrappers());
2422 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002423 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002424
Steve Anton15324772018-01-16 10:26:49 -08002425 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002426
2427 // Do offer/answer, wait for the callee to receive some frames.
2428 caller()->CreateAndSetAndSignalOffer();
2429 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2430
2431 // Get the remote audio track created on the receiver, so they can be used as
2432 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002433 auto receivers = callee()->pc()->GetReceivers();
2434 ASSERT_EQ(1u, receivers.size());
2435 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002436
2437 // Get the audio output level stats. Note that the level is not available
2438 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002439 EXPECT_TRUE_WAIT(
2440 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2441 0,
2442 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002443}
2444
deadbeefd8ad7882017-04-18 16:01:17 -07002445// Test that we can get stats (using the new stats implemnetation) for
2446// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2447// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002448TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002449 GetStatsForUnsignaledStreamWithNewStatsApi) {
2450 ASSERT_TRUE(CreatePeerConnectionWrappers());
2451 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002452 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002453 // Remove SSRCs and MSIDs from the received offer SDP.
2454 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2455 caller()->CreateAndSetAndSignalOffer();
2456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002457 MediaExpectations media_expectations;
2458 media_expectations.CalleeExpectsSomeAudio(1);
2459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002460
2461 // We received a frame, so we should have nonzero "bytes received" stats for
2462 // the unsignaled stream, if stats are working for it.
2463 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2464 callee()->NewGetStats();
2465 ASSERT_NE(nullptr, report);
2466 auto inbound_stream_stats =
2467 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2468 ASSERT_EQ(1U, inbound_stream_stats.size());
2469 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2470 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002471 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2472}
2473
2474// Test that we can successfully get the media related stats (audio level
2475// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002476TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002477 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2478 ASSERT_TRUE(CreatePeerConnectionWrappers());
2479 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002480 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002481 // Remove SSRCs and MSIDs from the received offer SDP.
2482 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2483 caller()->CreateAndSetAndSignalOffer();
2484 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002485 MediaExpectations media_expectations;
2486 media_expectations.CalleeExpectsSomeAudio(1);
2487 media_expectations.CalleeExpectsSomeVideo(1);
2488 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002489
2490 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2491 callee()->NewGetStats();
2492 ASSERT_NE(nullptr, report);
2493
2494 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2495 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2496 ASSERT_GE(audio_index, 0);
2497 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002498}
2499
deadbeef4e2deab2017-09-20 13:56:21 -07002500// Helper for test below.
2501void ModifySsrcs(cricket::SessionDescription* desc) {
2502 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002503 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002504 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002505 for (uint32_t& ssrc : stream.ssrcs) {
2506 ssrc = rtc::CreateRandomId();
2507 }
2508 }
2509 }
2510}
2511
2512// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2513// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2514// This should result in two "RTCInboundRTPStreamStats", but only one
2515// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2516// being reset to 0 once the SSRC change occurs.
2517//
2518// Regression test for this bug:
2519// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2520//
2521// The bug causes the track stats to only represent one of the two streams:
2522// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2523// that the track stat counters would reset to 0 when the new stream is
2524// received, and a 50% chance that they'll stop updating (while
2525// "concealed_samples" continues increasing, due to silence being generated for
2526// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002527TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002528 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002529 ASSERT_TRUE(CreatePeerConnectionWrappers());
2530 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002531 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002532 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2533 // that doesn't signal SSRCs (from the callee's perspective).
2534 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2535 caller()->CreateAndSetAndSignalOffer();
2536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2537 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002538 {
2539 MediaExpectations media_expectations;
2540 media_expectations.CalleeExpectsSomeAudio(50);
2541 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2542 }
deadbeef4e2deab2017-09-20 13:56:21 -07002543 // Some audio frames were received, so we should have nonzero "samples
2544 // received" for the track.
2545 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2546 callee()->NewGetStats();
2547 ASSERT_NE(nullptr, report);
2548 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2549 ASSERT_EQ(1U, track_stats.size());
2550 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2551 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2552 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2553
2554 // Create a new offer and munge it to cause the caller to use a new SSRC.
2555 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2556 caller()->CreateAndSetAndSignalOffer();
2557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2558 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2559 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002560 {
2561 MediaExpectations media_expectations;
2562 media_expectations.CalleeExpectsSomeAudio(25);
2563 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2564 }
deadbeef4e2deab2017-09-20 13:56:21 -07002565
2566 report = callee()->NewGetStats();
2567 ASSERT_NE(nullptr, report);
2568 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2569 ASSERT_EQ(1U, track_stats.size());
2570 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2571 // The "total samples received" stat should only be greater than it was
2572 // before.
2573 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2574 // Right now, the new SSRC will cause the counters to reset to 0.
2575 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2576
2577 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002578 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002579 // good sign that we're seeing stats from the old stream that's no longer
2580 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002581 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002582 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2583 EXPECT_LT(*track_stats[0]->concealed_samples,
2584 *track_stats[0]->total_samples_received *
2585 kAcceptableConcealedSamplesPercentage);
2586
2587 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2588 // sanity check that the SSRC really changed.
2589 // TODO(deadbeef): This isn't working right now, because we're not returning
2590 // *any* stats for the inactive stream. Uncomment when the bug is completely
2591 // fixed.
2592 // auto inbound_stream_stats =
2593 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2594 // ASSERT_EQ(2U, inbound_stream_stats.size());
2595}
2596
deadbeef1dcb1642017-03-29 21:08:16 -07002597// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002598TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002599 PeerConnectionFactory::Options dtls_10_options;
2600 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2601 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2602 dtls_10_options));
2603 ConnectFakeSignaling();
2604 // Do normal offer/answer and wait for some frames to be received in each
2605 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002606 caller()->AddAudioVideoTracks();
2607 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002608 caller()->CreateAndSetAndSignalOffer();
2609 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002610 MediaExpectations media_expectations;
2611 media_expectations.ExpectBidirectionalAudioAndVideo();
2612 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002613}
2614
2615// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002616TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002617 PeerConnectionFactory::Options dtls_10_options;
2618 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2619 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2620 dtls_10_options));
2621 ConnectFakeSignaling();
2622 // Register UMA observer before signaling begins.
2623 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2624 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2625 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002626 caller()->AddAudioVideoTracks();
2627 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002628 caller()->CreateAndSetAndSignalOffer();
2629 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2630 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002631 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002632 kDefaultTimeout);
2633 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002634 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002635 EXPECT_EQ(1,
2636 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2637 kDefaultSrtpCryptoSuite));
2638}
2639
2640// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002641TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002642 PeerConnectionFactory::Options dtls_12_options;
2643 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2644 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2645 dtls_12_options));
2646 ConnectFakeSignaling();
2647 // Register UMA observer before signaling begins.
2648 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2649 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2650 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002651 caller()->AddAudioVideoTracks();
2652 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002653 caller()->CreateAndSetAndSignalOffer();
2654 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2655 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002656 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002657 kDefaultTimeout);
2658 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002659 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002660 EXPECT_EQ(1,
2661 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2662 kDefaultSrtpCryptoSuite));
2663}
2664
2665// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2666// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002667TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002668 PeerConnectionFactory::Options caller_options;
2669 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2670 PeerConnectionFactory::Options callee_options;
2671 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2672 ASSERT_TRUE(
2673 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2674 ConnectFakeSignaling();
2675 // Do normal offer/answer and wait for some frames to be received in each
2676 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002677 caller()->AddAudioVideoTracks();
2678 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002679 caller()->CreateAndSetAndSignalOffer();
2680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002681 MediaExpectations media_expectations;
2682 media_expectations.ExpectBidirectionalAudioAndVideo();
2683 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002684}
2685
2686// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2687// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002688TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002689 PeerConnectionFactory::Options caller_options;
2690 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2691 PeerConnectionFactory::Options callee_options;
2692 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2693 ASSERT_TRUE(
2694 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2695 ConnectFakeSignaling();
2696 // Do normal offer/answer and wait for some frames to be received in each
2697 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002698 caller()->AddAudioVideoTracks();
2699 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002700 caller()->CreateAndSetAndSignalOffer();
2701 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002702 MediaExpectations media_expectations;
2703 media_expectations.ExpectBidirectionalAudioAndVideo();
2704 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002705}
2706
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002707// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2708// works as expected; the cipher should only be used if enabled by both sides.
2709TEST_P(PeerConnectionIntegrationTest,
2710 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2711 PeerConnectionFactory::Options caller_options;
2712 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2713 PeerConnectionFactory::Options callee_options;
2714 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2715 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2716 TestNegotiatedCipherSuite(caller_options, callee_options,
2717 expected_cipher_suite);
2718}
2719
2720TEST_P(PeerConnectionIntegrationTest,
2721 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2722 PeerConnectionFactory::Options caller_options;
2723 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2724 PeerConnectionFactory::Options callee_options;
2725 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2726 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2727 TestNegotiatedCipherSuite(caller_options, callee_options,
2728 expected_cipher_suite);
2729}
2730
2731TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2732 PeerConnectionFactory::Options caller_options;
2733 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2734 PeerConnectionFactory::Options callee_options;
2735 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2736 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2737 TestNegotiatedCipherSuite(caller_options, callee_options,
2738 expected_cipher_suite);
2739}
2740
deadbeef1dcb1642017-03-29 21:08:16 -07002741// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002742TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002743 bool local_gcm_enabled = false;
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 a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002751TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002752 bool local_gcm_enabled = true;
2753 bool remote_gcm_enabled = true;
2754 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2755 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2756 expected_cipher_suite);
2757}
2758
2759// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002760TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002761 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2762 bool local_gcm_enabled = true;
2763 bool remote_gcm_enabled = false;
2764 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2765 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2766 expected_cipher_suite);
2767}
2768
2769// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002770TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002771 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2772 bool local_gcm_enabled = false;
2773 bool remote_gcm_enabled = true;
2774 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2775 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2776 expected_cipher_suite);
2777}
2778
deadbeef7914b8c2017-04-21 03:23:33 -07002779// Verify that media can be transmitted end-to-end when GCM crypto suites are
2780// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2781// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2782// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002783TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002784 PeerConnectionFactory::Options gcm_options;
2785 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2786 ASSERT_TRUE(
2787 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2788 ConnectFakeSignaling();
2789 // Do normal offer/answer and wait for some frames to be received in each
2790 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002791 caller()->AddAudioVideoTracks();
2792 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002793 caller()->CreateAndSetAndSignalOffer();
2794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002795 MediaExpectations media_expectations;
2796 media_expectations.ExpectBidirectionalAudioAndVideo();
2797 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002798}
2799
deadbeef1dcb1642017-03-29 21:08:16 -07002800// This test sets up a call between two parties with audio, video and an RTP
2801// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002802TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002803 FakeConstraints setup_constraints;
2804 setup_constraints.SetAllowRtpDataChannels();
2805 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2806 &setup_constraints));
2807 ConnectFakeSignaling();
2808 // Expect that data channel created on caller side will show up for callee as
2809 // well.
2810 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002811 caller()->AddAudioVideoTracks();
2812 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002813 caller()->CreateAndSetAndSignalOffer();
2814 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2815 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002816 MediaExpectations media_expectations;
2817 media_expectations.ExpectBidirectionalAudioAndVideo();
2818 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002819 ASSERT_NE(nullptr, caller()->data_channel());
2820 ASSERT_NE(nullptr, callee()->data_channel());
2821 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2822 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2823
2824 // Ensure data can be sent in both directions.
2825 std::string data = "hello world";
2826 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2827 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2828 kDefaultTimeout);
2829 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2830 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2831 kDefaultTimeout);
2832}
2833
2834// Ensure that an RTP data channel is signaled as closed for the caller when
2835// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002836TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002837 RtpDataChannelSignaledClosedInCalleeOffer) {
2838 // Same procedure as above test.
2839 FakeConstraints setup_constraints;
2840 setup_constraints.SetAllowRtpDataChannels();
2841 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2842 &setup_constraints));
2843 ConnectFakeSignaling();
2844 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002845 caller()->AddAudioVideoTracks();
2846 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002847 caller()->CreateAndSetAndSignalOffer();
2848 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2849 ASSERT_NE(nullptr, caller()->data_channel());
2850 ASSERT_NE(nullptr, callee()->data_channel());
2851 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2852 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2853
2854 // Close the data channel on the callee, and do an updated offer/answer.
2855 callee()->data_channel()->Close();
2856 callee()->CreateAndSetAndSignalOffer();
2857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2858 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2859 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2860}
2861
2862// Tests that data is buffered in an RTP data channel until an observer is
2863// registered for it.
2864//
2865// NOTE: RTP data channels can receive data before the underlying
2866// transport has detected that a channel is writable and thus data can be
2867// received before the data channel state changes to open. That is hard to test
2868// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002869TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002870 DataBufferedUntilRtpDataChannelObserverRegistered) {
2871 // Use fake clock and simulated network delay so that we predictably can wait
2872 // until an SCTP message has been delivered without "sleep()"ing.
2873 rtc::ScopedFakeClock fake_clock;
2874 // Some things use a time of "0" as a special value, so we need to start out
2875 // the fake clock at a nonzero time.
2876 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02002877 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07002878 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2879 virtual_socket_server()->UpdateDelayDistribution();
2880
2881 FakeConstraints constraints;
2882 constraints.SetAllowRtpDataChannels();
2883 ASSERT_TRUE(
2884 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2885 ConnectFakeSignaling();
2886 caller()->CreateDataChannel();
2887 caller()->CreateAndSetAndSignalOffer();
2888 ASSERT_TRUE(caller()->data_channel() != nullptr);
2889 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2890 kDefaultTimeout, fake_clock);
2891 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2892 kDefaultTimeout, fake_clock);
2893 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2894 callee()->data_channel()->state(), kDefaultTimeout,
2895 fake_clock);
2896
2897 // Unregister the observer which is normally automatically registered.
2898 callee()->data_channel()->UnregisterObserver();
2899 // Send data and advance fake clock until it should have been received.
2900 std::string data = "hello world";
2901 caller()->data_channel()->Send(DataBuffer(data));
2902 SIMULATED_WAIT(false, 50, fake_clock);
2903
2904 // Attach data channel and expect data to be received immediately. Note that
2905 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2906 // further, but data can be received even if the callback is asynchronous.
2907 MockDataChannelObserver new_observer(callee()->data_channel());
2908 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2909 fake_clock);
2910}
2911
2912// This test sets up a call between two parties with audio, video and but only
2913// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002914TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002915 FakeConstraints setup_constraints_1;
2916 setup_constraints_1.SetAllowRtpDataChannels();
2917 // Must disable DTLS to make negotiation succeed.
2918 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2919 false);
2920 FakeConstraints setup_constraints_2;
2921 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2922 false);
2923 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2924 &setup_constraints_1, &setup_constraints_2));
2925 ConnectFakeSignaling();
2926 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002927 caller()->AddAudioVideoTracks();
2928 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002929 caller()->CreateAndSetAndSignalOffer();
2930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2931 // The caller should still have a data channel, but it should be closed, and
2932 // one should ever have been created for the callee.
2933 EXPECT_TRUE(caller()->data_channel() != nullptr);
2934 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2935 EXPECT_EQ(nullptr, callee()->data_channel());
2936}
2937
2938// This test sets up a call between two parties with audio, and video. When
2939// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002940TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002941 FakeConstraints setup_constraints;
2942 setup_constraints.SetAllowRtpDataChannels();
2943 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2944 &setup_constraints));
2945 ConnectFakeSignaling();
2946 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002947 caller()->AddAudioVideoTracks();
2948 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002949 caller()->CreateAndSetAndSignalOffer();
2950 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2951 // Create data channel and do new offer and answer.
2952 caller()->CreateDataChannel();
2953 caller()->CreateAndSetAndSignalOffer();
2954 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2955 ASSERT_NE(nullptr, caller()->data_channel());
2956 ASSERT_NE(nullptr, callee()->data_channel());
2957 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2958 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2959 // Ensure data can be sent in both directions.
2960 std::string data = "hello world";
2961 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2962 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2963 kDefaultTimeout);
2964 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2965 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2966 kDefaultTimeout);
2967}
2968
2969#ifdef HAVE_SCTP
2970
2971// This test sets up a call between two parties with audio, video and an SCTP
2972// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002973TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002974 ASSERT_TRUE(CreatePeerConnectionWrappers());
2975 ConnectFakeSignaling();
2976 // Expect that data channel created on caller side will show up for callee as
2977 // well.
2978 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002979 caller()->AddAudioVideoTracks();
2980 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002981 caller()->CreateAndSetAndSignalOffer();
2982 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2983 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002984 MediaExpectations media_expectations;
2985 media_expectations.ExpectBidirectionalAudioAndVideo();
2986 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002987 // Caller data channel should already exist (it created one). Callee data
2988 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2989 ASSERT_NE(nullptr, caller()->data_channel());
2990 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2991 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2992 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2993
2994 // Ensure data can be sent in both directions.
2995 std::string data = "hello world";
2996 caller()->data_channel()->Send(DataBuffer(data));
2997 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2998 kDefaultTimeout);
2999 callee()->data_channel()->Send(DataBuffer(data));
3000 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3001 kDefaultTimeout);
3002}
3003
3004// Ensure that when the callee closes an SCTP data channel, the closing
3005// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003006TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003007 // Same procedure as above test.
3008 ASSERT_TRUE(CreatePeerConnectionWrappers());
3009 ConnectFakeSignaling();
3010 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003011 caller()->AddAudioVideoTracks();
3012 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003013 caller()->CreateAndSetAndSignalOffer();
3014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3015 ASSERT_NE(nullptr, caller()->data_channel());
3016 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3017 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3018 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3019
3020 // Close the data channel on the callee side, and wait for it to reach the
3021 // "closed" state on both sides.
3022 callee()->data_channel()->Close();
3023 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3024 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3025}
3026
Seth Hampson2f0d7022018-02-20 11:54:42 -08003027TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003028 ASSERT_TRUE(CreatePeerConnectionWrappers());
3029 ConnectFakeSignaling();
3030 webrtc::DataChannelInit init;
3031 init.id = 53;
3032 init.maxRetransmits = 52;
3033 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003034 caller()->AddAudioVideoTracks();
3035 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003036 caller()->CreateAndSetAndSignalOffer();
3037 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003038 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3039 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003040 EXPECT_EQ(init.id, callee()->data_channel()->id());
3041 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3042 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3043 EXPECT_FALSE(callee()->data_channel()->negotiated());
3044}
3045
deadbeef1dcb1642017-03-29 21:08:16 -07003046// Test usrsctp's ability to process unordered data stream, where data actually
3047// arrives out of order using simulated delays. Previously there have been some
3048// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003049TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003050 // Introduce random network delays.
3051 // Otherwise it's not a true "unordered" test.
3052 virtual_socket_server()->set_delay_mean(20);
3053 virtual_socket_server()->set_delay_stddev(5);
3054 virtual_socket_server()->UpdateDelayDistribution();
3055 // Normal procedure, but with unordered data channel config.
3056 ASSERT_TRUE(CreatePeerConnectionWrappers());
3057 ConnectFakeSignaling();
3058 webrtc::DataChannelInit init;
3059 init.ordered = false;
3060 caller()->CreateDataChannel(&init);
3061 caller()->CreateAndSetAndSignalOffer();
3062 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3063 ASSERT_NE(nullptr, caller()->data_channel());
3064 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3065 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3066 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3067
3068 static constexpr int kNumMessages = 100;
3069 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3070 static constexpr size_t kMaxMessageSize = 4096;
3071 // Create and send random messages.
3072 std::vector<std::string> sent_messages;
3073 for (int i = 0; i < kNumMessages; ++i) {
3074 size_t length =
3075 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3076 std::string message;
3077 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3078 caller()->data_channel()->Send(DataBuffer(message));
3079 callee()->data_channel()->Send(DataBuffer(message));
3080 sent_messages.push_back(message);
3081 }
3082
3083 // Wait for all messages to be received.
3084 EXPECT_EQ_WAIT(kNumMessages,
3085 caller()->data_observer()->received_message_count(),
3086 kDefaultTimeout);
3087 EXPECT_EQ_WAIT(kNumMessages,
3088 callee()->data_observer()->received_message_count(),
3089 kDefaultTimeout);
3090
3091 // Sort and compare to make sure none of the messages were corrupted.
3092 std::vector<std::string> caller_received_messages =
3093 caller()->data_observer()->messages();
3094 std::vector<std::string> callee_received_messages =
3095 callee()->data_observer()->messages();
3096 std::sort(sent_messages.begin(), sent_messages.end());
3097 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3098 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3099 EXPECT_EQ(sent_messages, caller_received_messages);
3100 EXPECT_EQ(sent_messages, callee_received_messages);
3101}
3102
3103// This test sets up a call between two parties with audio, and video. When
3104// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003105TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003106 ASSERT_TRUE(CreatePeerConnectionWrappers());
3107 ConnectFakeSignaling();
3108 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003109 caller()->AddAudioVideoTracks();
3110 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003111 caller()->CreateAndSetAndSignalOffer();
3112 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3113 // Create data channel and do new offer and answer.
3114 caller()->CreateDataChannel();
3115 caller()->CreateAndSetAndSignalOffer();
3116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3117 // Caller data channel should already exist (it created one). Callee data
3118 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3119 ASSERT_NE(nullptr, caller()->data_channel());
3120 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3121 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3122 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3123 // Ensure data can be sent in both directions.
3124 std::string data = "hello world";
3125 caller()->data_channel()->Send(DataBuffer(data));
3126 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3127 kDefaultTimeout);
3128 callee()->data_channel()->Send(DataBuffer(data));
3129 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3130 kDefaultTimeout);
3131}
3132
deadbeef7914b8c2017-04-21 03:23:33 -07003133// Set up a connection initially just using SCTP data channels, later upgrading
3134// to audio/video, ensuring frames are received end-to-end. Effectively the
3135// inverse of the test above.
3136// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003137TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003138 ASSERT_TRUE(CreatePeerConnectionWrappers());
3139 ConnectFakeSignaling();
3140 // Do initial offer/answer with just data channel.
3141 caller()->CreateDataChannel();
3142 caller()->CreateAndSetAndSignalOffer();
3143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3144 // Wait until data can be sent over the data channel.
3145 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3146 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3147 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3148
3149 // Do subsequent offer/answer with two-way audio and video. Audio and video
3150 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003151 caller()->AddAudioVideoTracks();
3152 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003153 caller()->CreateAndSetAndSignalOffer();
3154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003155 MediaExpectations media_expectations;
3156 media_expectations.ExpectBidirectionalAudioAndVideo();
3157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003158}
3159
deadbeef8b7e9ad2017-05-25 09:38:55 -07003160static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003161 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003162 GetFirstDataContentDescription(desc);
3163 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003164 dcd_offer->set_use_sctpmap(false);
3165 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3166}
3167
3168// Test that the data channel works when a spec-compliant SCTP m= section is
3169// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3170// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003171TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003172 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3173 ASSERT_TRUE(CreatePeerConnectionWrappers());
3174 ConnectFakeSignaling();
3175 caller()->CreateDataChannel();
3176 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3177 caller()->CreateAndSetAndSignalOffer();
3178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3179 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3180 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3181 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3182
3183 // Ensure data can be sent in both directions.
3184 std::string data = "hello world";
3185 caller()->data_channel()->Send(DataBuffer(data));
3186 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3187 kDefaultTimeout);
3188 callee()->data_channel()->Send(DataBuffer(data));
3189 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3190 kDefaultTimeout);
3191}
3192
deadbeef1dcb1642017-03-29 21:08:16 -07003193#endif // HAVE_SCTP
3194
3195// Test that the ICE connection and gathering states eventually reach
3196// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003197TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003198 ASSERT_TRUE(CreatePeerConnectionWrappers());
3199 ConnectFakeSignaling();
3200 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003201 caller()->AddAudioVideoTracks();
3202 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003203 caller()->CreateAndSetAndSignalOffer();
3204 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3205 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3206 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3207 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3208 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3209 // After the best candidate pair is selected and all candidates are signaled,
3210 // the ICE connection state should reach "complete".
3211 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3212 // answerer/"callee" by default) only reaches "connected". When this is
3213 // fixed, this test should be updated.
3214 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3215 caller()->ice_connection_state(), kDefaultTimeout);
3216 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3217 callee()->ice_connection_state(), kDefaultTimeout);
3218}
3219
Steve Antonede9ca52017-10-16 13:04:27 -07003220// Test that firewalling the ICE connection causes the clients to identify the
3221// disconnected state and then removing the firewall causes them to reconnect.
3222class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003223 : public PeerConnectionIntegrationBaseTest,
3224 public ::testing::WithParamInterface<
3225 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003226 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003227 PeerConnectionIntegrationIceStatesTest()
3228 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3229 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003230 }
3231
3232 void StartStunServer(const SocketAddress& server_address) {
3233 stun_server_.reset(
3234 cricket::TestStunServer::Create(network_thread(), server_address));
3235 }
3236
3237 bool TestIPv6() {
3238 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3239 }
3240
3241 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003242 network_thread()->Invoke<void>(
3243 RTC_FROM_HERE,
3244 rtc::Bind(&cricket::PortAllocator::set_flags,
3245 caller()->port_allocator(), port_allocator_flags_));
3246 network_thread()->Invoke<void>(
3247 RTC_FROM_HERE,
3248 rtc::Bind(&cricket::PortAllocator::set_flags,
3249 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003250 }
3251
3252 std::vector<SocketAddress> CallerAddresses() {
3253 std::vector<SocketAddress> addresses;
3254 addresses.push_back(SocketAddress("1.1.1.1", 0));
3255 if (TestIPv6()) {
3256 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3257 }
3258 return addresses;
3259 }
3260
3261 std::vector<SocketAddress> CalleeAddresses() {
3262 std::vector<SocketAddress> addresses;
3263 addresses.push_back(SocketAddress("2.2.2.2", 0));
3264 if (TestIPv6()) {
3265 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3266 }
3267 return addresses;
3268 }
3269
3270 void SetUpNetworkInterfaces() {
3271 // Remove the default interfaces added by the test infrastructure.
3272 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3273 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3274
3275 // Add network addresses for test.
3276 for (const auto& caller_address : CallerAddresses()) {
3277 caller()->network()->AddInterface(caller_address);
3278 }
3279 for (const auto& callee_address : CalleeAddresses()) {
3280 callee()->network()->AddInterface(callee_address);
3281 }
3282 }
3283
3284 private:
3285 uint32_t port_allocator_flags_;
3286 std::unique_ptr<cricket::TestStunServer> stun_server_;
3287};
3288
3289// Tests that the PeerConnection goes through all the ICE gathering/connection
3290// states over the duration of the call. This includes Disconnected and Failed
3291// states, induced by putting a firewall between the peers and waiting for them
3292// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003293TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3294 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3295 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3296 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003297
3298 const SocketAddress kStunServerAddress =
3299 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3300 StartStunServer(kStunServerAddress);
3301
3302 PeerConnectionInterface::RTCConfiguration config;
3303 PeerConnectionInterface::IceServer ice_stun_server;
3304 ice_stun_server.urls.push_back(
3305 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3306 kStunServerAddress.PortAsString());
3307 config.servers.push_back(ice_stun_server);
3308
3309 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3310 ConnectFakeSignaling();
3311 SetPortAllocatorFlags();
3312 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003313 caller()->AddAudioVideoTracks();
3314 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003315
3316 // Initial state before anything happens.
3317 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3318 caller()->ice_gathering_state());
3319 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3320 caller()->ice_connection_state());
3321
3322 // Start the call by creating the offer, setting it as the local description,
3323 // then sending it to the peer who will respond with an answer. This happens
3324 // asynchronously so that we can watch the states as it runs in the
3325 // background.
3326 caller()->CreateAndSetAndSignalOffer();
3327
Steve Anton83119dd2017-11-10 16:19:52 -08003328 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3329 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003330
3331 // Verify that the observer was notified of the intermediate transitions.
3332 EXPECT_THAT(caller()->ice_connection_state_history(),
3333 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3334 PeerConnectionInterface::kIceConnectionConnected,
3335 PeerConnectionInterface::kIceConnectionCompleted));
3336 EXPECT_THAT(caller()->ice_gathering_state_history(),
3337 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3338 PeerConnectionInterface::kIceGatheringComplete));
3339
3340 // Block connections to/from the caller and wait for ICE to become
3341 // disconnected.
3342 for (const auto& caller_address : CallerAddresses()) {
3343 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3344 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003345 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003346 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3347 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003348
3349 // Let ICE re-establish by removing the firewall rules.
3350 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003351 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003352 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3353 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003354
3355 // According to RFC7675, if there is no response within 30 seconds then the
3356 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003357 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003358 constexpr int kConsentTimeout = 30000;
3359 for (const auto& caller_address : CallerAddresses()) {
3360 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3361 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003362 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003363 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3364 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003365}
3366
3367// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3368// and that the statistics in the metric observers are updated correctly.
3369TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3370 ASSERT_TRUE(CreatePeerConnectionWrappers());
3371 ConnectFakeSignaling();
3372 SetPortAllocatorFlags();
3373 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003374 caller()->AddAudioVideoTracks();
3375 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003376
3377 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3378 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3379 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3380
3381 caller()->CreateAndSetAndSignalOffer();
3382
3383 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3384
3385 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3386 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3387 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3388 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3389 if (TestIPv6()) {
3390 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3391 // connection.
3392 EXPECT_EQ(0u, num_best_ipv4);
3393 EXPECT_EQ(1u, num_best_ipv6);
3394 } else {
3395 EXPECT_EQ(1u, num_best_ipv4);
3396 EXPECT_EQ(0u, num_best_ipv6);
3397 }
3398
3399 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3400 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3401 webrtc::kIceCandidatePairHostHost));
3402 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3403 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3404 webrtc::kIceCandidatePairHostPublicHostPublic));
3405}
3406
3407constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3408 cricket::PORTALLOCATOR_DISABLE_STUN |
3409 cricket::PORTALLOCATOR_DISABLE_RELAY;
3410constexpr uint32_t kFlagsIPv6NoStun =
3411 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3412 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3413constexpr uint32_t kFlagsIPv4Stun =
3414 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3415
Seth Hampson2f0d7022018-02-20 11:54:42 -08003416INSTANTIATE_TEST_CASE_P(
3417 PeerConnectionIntegrationTest,
3418 PeerConnectionIntegrationIceStatesTest,
3419 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3420 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3421 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3422 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003423
deadbeef1dcb1642017-03-29 21:08:16 -07003424// This test sets up a call between two parties with audio and video.
3425// During the call, the caller restarts ICE and the test verifies that
3426// new ICE candidates are generated and audio and video still can flow, and the
3427// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003428TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003429 ASSERT_TRUE(CreatePeerConnectionWrappers());
3430 ConnectFakeSignaling();
3431 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003432 caller()->AddAudioVideoTracks();
3433 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003434 caller()->CreateAndSetAndSignalOffer();
3435 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3436 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3437 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3438 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3439 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3440
3441 // To verify that the ICE restart actually occurs, get
3442 // ufrag/password/candidates before and after restart.
3443 // Create an SDP string of the first audio candidate for both clients.
3444 const webrtc::IceCandidateCollection* audio_candidates_caller =
3445 caller()->pc()->local_description()->candidates(0);
3446 const webrtc::IceCandidateCollection* audio_candidates_callee =
3447 callee()->pc()->local_description()->candidates(0);
3448 ASSERT_GT(audio_candidates_caller->count(), 0u);
3449 ASSERT_GT(audio_candidates_callee->count(), 0u);
3450 std::string caller_candidate_pre_restart;
3451 ASSERT_TRUE(
3452 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3453 std::string callee_candidate_pre_restart;
3454 ASSERT_TRUE(
3455 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3456 const cricket::SessionDescription* desc =
3457 caller()->pc()->local_description()->description();
3458 std::string caller_ufrag_pre_restart =
3459 desc->transport_infos()[0].description.ice_ufrag;
3460 desc = callee()->pc()->local_description()->description();
3461 std::string callee_ufrag_pre_restart =
3462 desc->transport_infos()[0].description.ice_ufrag;
3463
3464 // Have the caller initiate an ICE restart.
3465 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3466 caller()->CreateAndSetAndSignalOffer();
3467 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3468 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3469 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3470 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3471 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3472
3473 // Grab the ufrags/candidates again.
3474 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3475 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3476 ASSERT_GT(audio_candidates_caller->count(), 0u);
3477 ASSERT_GT(audio_candidates_callee->count(), 0u);
3478 std::string caller_candidate_post_restart;
3479 ASSERT_TRUE(
3480 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3481 std::string callee_candidate_post_restart;
3482 ASSERT_TRUE(
3483 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3484 desc = caller()->pc()->local_description()->description();
3485 std::string caller_ufrag_post_restart =
3486 desc->transport_infos()[0].description.ice_ufrag;
3487 desc = callee()->pc()->local_description()->description();
3488 std::string callee_ufrag_post_restart =
3489 desc->transport_infos()[0].description.ice_ufrag;
3490 // Sanity check that an ICE restart was actually negotiated in SDP.
3491 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3492 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3493 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3494 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3495
3496 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003497 MediaExpectations media_expectations;
3498 media_expectations.ExpectBidirectionalAudioAndVideo();
3499 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003500}
3501
3502// Verify that audio/video can be received end-to-end when ICE renomination is
3503// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003504TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003505 PeerConnectionInterface::RTCConfiguration config;
3506 config.enable_ice_renomination = true;
3507 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3508 ConnectFakeSignaling();
3509 // Do normal offer/answer and wait for some frames to be received in each
3510 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003511 caller()->AddAudioVideoTracks();
3512 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003513 caller()->CreateAndSetAndSignalOffer();
3514 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3515 // Sanity check that ICE renomination was actually negotiated.
3516 const cricket::SessionDescription* desc =
3517 caller()->pc()->local_description()->description();
3518 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003519 ASSERT_NE(
3520 info.description.transport_options.end(),
3521 std::find(info.description.transport_options.begin(),
3522 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003523 }
3524 desc = callee()->pc()->local_description()->description();
3525 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003526 ASSERT_NE(
3527 info.description.transport_options.end(),
3528 std::find(info.description.transport_options.begin(),
3529 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003530 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003531 MediaExpectations media_expectations;
3532 media_expectations.ExpectBidirectionalAudioAndVideo();
3533 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003534}
3535
Steve Anton6f25b092017-10-23 09:39:20 -07003536// With a max bundle policy and RTCP muxing, adding a new media description to
3537// the connection should not affect ICE at all because the new media will use
3538// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003539TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003540 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003541 PeerConnectionInterface::RTCConfiguration config;
3542 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3543 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3544 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3545 config, PeerConnectionInterface::RTCConfiguration()));
3546 ConnectFakeSignaling();
3547
Steve Anton15324772018-01-16 10:26:49 -08003548 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003549 caller()->CreateAndSetAndSignalOffer();
3550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003551 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3552 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003553
3554 caller()->clear_ice_connection_state_history();
3555
Steve Anton15324772018-01-16 10:26:49 -08003556 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003557 caller()->CreateAndSetAndSignalOffer();
3558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3559
3560 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3561}
3562
deadbeef1dcb1642017-03-29 21:08:16 -07003563// This test sets up a call between two parties with audio and video. It then
3564// renegotiates setting the video m-line to "port 0", then later renegotiates
3565// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003566TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003567 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3568 ASSERT_TRUE(CreatePeerConnectionWrappers());
3569 ConnectFakeSignaling();
3570
3571 // Do initial negotiation, only sending media from the caller. Will result in
3572 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003573 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003574 caller()->CreateAndSetAndSignalOffer();
3575 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3576
3577 // Negotiate again, disabling the video "m=" section (the callee will set the
3578 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003579 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3580 PeerConnectionInterface::RTCOfferAnswerOptions options;
3581 options.offer_to_receive_video = 0;
3582 callee()->SetOfferAnswerOptions(options);
3583 } else {
3584 callee()->SetRemoteOfferHandler([this] {
3585 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3586 });
3587 }
deadbeef1dcb1642017-03-29 21:08:16 -07003588 caller()->CreateAndSetAndSignalOffer();
3589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3590 // Sanity check that video "m=" section was actually rejected.
3591 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3592 callee()->pc()->local_description()->description());
3593 ASSERT_NE(nullptr, answer_video_content);
3594 ASSERT_TRUE(answer_video_content->rejected);
3595
3596 // Enable video and do negotiation again, making sure video is received
3597 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003598 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3599 PeerConnectionInterface::RTCOfferAnswerOptions options;
3600 options.offer_to_receive_video = 1;
3601 callee()->SetOfferAnswerOptions(options);
3602 } else {
3603 // The caller's transceiver is stopped, so we need to add another track.
3604 auto caller_transceiver =
3605 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3606 EXPECT_TRUE(caller_transceiver->stopped());
3607 caller()->AddVideoTrack();
3608 }
3609 callee()->AddVideoTrack();
3610 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003611 caller()->CreateAndSetAndSignalOffer();
3612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003613
deadbeef1dcb1642017-03-29 21:08:16 -07003614 // Verify the caller receives frames from the newly added stream, and the
3615 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003616 MediaExpectations media_expectations;
3617 media_expectations.CalleeExpectsSomeAudio();
3618 media_expectations.ExpectBidirectionalVideo();
3619 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003620}
3621
deadbeef1dcb1642017-03-29 21:08:16 -07003622// This tests that if we negotiate after calling CreateSender but before we
3623// have a track, then set a track later, frames from the newly-set track are
3624// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003625TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003626 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3627 ASSERT_TRUE(CreatePeerConnectionWrappers());
3628 ConnectFakeSignaling();
3629 auto caller_audio_sender =
3630 caller()->pc()->CreateSender("audio", "caller_stream");
3631 auto caller_video_sender =
3632 caller()->pc()->CreateSender("video", "caller_stream");
3633 auto callee_audio_sender =
3634 callee()->pc()->CreateSender("audio", "callee_stream");
3635 auto callee_video_sender =
3636 callee()->pc()->CreateSender("video", "callee_stream");
3637 caller()->CreateAndSetAndSignalOffer();
3638 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3639 // Wait for ICE to complete, without any tracks being set.
3640 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3641 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3642 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3643 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3644 // Now set the tracks, and expect frames to immediately start flowing.
3645 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3646 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3647 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3648 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003649 MediaExpectations media_expectations;
3650 media_expectations.ExpectBidirectionalAudioAndVideo();
3651 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3652}
3653
3654// This tests that if we negotiate after calling AddTransceiver but before we
3655// have a track, then set a track later, frames from the newly-set tracks are
3656// received end-to-end.
3657TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3658 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3659 ASSERT_TRUE(CreatePeerConnectionWrappers());
3660 ConnectFakeSignaling();
3661 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3662 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3663 auto caller_audio_sender = audio_result.MoveValue()->sender();
3664 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3665 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3666 auto caller_video_sender = video_result.MoveValue()->sender();
3667 callee()->SetRemoteOfferHandler([this] {
3668 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3669 callee()->pc()->GetTransceivers()[0]->SetDirection(
3670 RtpTransceiverDirection::kSendRecv);
3671 callee()->pc()->GetTransceivers()[1]->SetDirection(
3672 RtpTransceiverDirection::kSendRecv);
3673 });
3674 caller()->CreateAndSetAndSignalOffer();
3675 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3676 // Wait for ICE to complete, without any tracks being set.
3677 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3678 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3679 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3680 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3681 // Now set the tracks, and expect frames to immediately start flowing.
3682 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3683 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3684 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3685 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3686 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3687 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3688 MediaExpectations media_expectations;
3689 media_expectations.ExpectBidirectionalAudioAndVideo();
3690 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003691}
3692
3693// This test verifies that a remote video track can be added via AddStream,
3694// and sent end-to-end. For this particular test, it's simply echoed back
3695// from the caller to the callee, rather than being forwarded to a third
3696// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003697TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003698 ASSERT_TRUE(CreatePeerConnectionWrappers());
3699 ConnectFakeSignaling();
3700 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003701 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003702 caller()->CreateAndSetAndSignalOffer();
3703 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3704 ASSERT_EQ(1, callee()->remote_streams()->count());
3705
3706 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3707 // time).
3708 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3709 callee()->CreateAndSetAndSignalOffer();
3710 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3711
Seth Hampson2f0d7022018-02-20 11:54:42 -08003712 MediaExpectations media_expectations;
3713 media_expectations.ExpectBidirectionalVideo();
3714 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003715}
3716
3717// Test that we achieve the expected end-to-end connection time, using a
3718// fake clock and simulated latency on the media and signaling paths.
3719// We use a TURN<->TURN connection because this is usually the quickest to
3720// set up initially, especially when we're confident the connection will work
3721// and can start sending media before we get a STUN response.
3722//
3723// With various optimizations enabled, here are the network delays we expect to
3724// be on the critical path:
3725// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3726// signaling answer (with DTLS fingerprint).
3727// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3728// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3729// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003730TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003731 rtc::ScopedFakeClock fake_clock;
3732 // Some things use a time of "0" as a special value, so we need to start out
3733 // the fake clock at a nonzero time.
3734 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003735 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003736
3737 static constexpr int media_hop_delay_ms = 50;
3738 static constexpr int signaling_trip_delay_ms = 500;
3739 // For explanation of these values, see comment above.
3740 static constexpr int required_media_hops = 9;
3741 static constexpr int required_signaling_trips = 2;
3742 // For internal delays (such as posting an event asychronously).
3743 static constexpr int allowed_internal_delay_ms = 20;
3744 static constexpr int total_connection_time_ms =
3745 media_hop_delay_ms * required_media_hops +
3746 signaling_trip_delay_ms * required_signaling_trips +
3747 allowed_internal_delay_ms;
3748
3749 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3750 3478};
3751 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3752 0};
3753 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3754 3478};
3755 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3756 0};
3757 cricket::TestTurnServer turn_server_1(network_thread(),
3758 turn_server_1_internal_address,
3759 turn_server_1_external_address);
3760 cricket::TestTurnServer turn_server_2(network_thread(),
3761 turn_server_2_internal_address,
3762 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003763
deadbeef1dcb1642017-03-29 21:08:16 -07003764 // Bypass permission check on received packets so media can be sent before
3765 // the candidate is signaled.
3766 turn_server_1.set_enable_permission_checks(false);
3767 turn_server_2.set_enable_permission_checks(false);
3768
3769 PeerConnectionInterface::RTCConfiguration client_1_config;
3770 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3771 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3772 ice_server_1.username = "test";
3773 ice_server_1.password = "test";
3774 client_1_config.servers.push_back(ice_server_1);
3775 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3776 client_1_config.presume_writable_when_fully_relayed = true;
3777
3778 PeerConnectionInterface::RTCConfiguration client_2_config;
3779 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3780 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3781 ice_server_2.username = "test";
3782 ice_server_2.password = "test";
3783 client_2_config.servers.push_back(ice_server_2);
3784 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3785 client_2_config.presume_writable_when_fully_relayed = true;
3786
3787 ASSERT_TRUE(
3788 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3789 // Set up the simulated delays.
3790 SetSignalingDelayMs(signaling_trip_delay_ms);
3791 ConnectFakeSignaling();
3792 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3793 virtual_socket_server()->UpdateDelayDistribution();
3794
3795 // Set "offer to receive audio/video" without adding any tracks, so we just
3796 // set up ICE/DTLS with no media.
3797 PeerConnectionInterface::RTCOfferAnswerOptions options;
3798 options.offer_to_receive_audio = 1;
3799 options.offer_to_receive_video = 1;
3800 caller()->SetOfferAnswerOptions(options);
3801 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003802 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3803 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003804 // Need to free the clients here since they're using things we created on
3805 // the stack.
3806 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3807 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3808}
3809
Jonas Orelandbdcee282017-10-10 14:01:40 +02003810// Verify that a TurnCustomizer passed in through RTCConfiguration
3811// is actually used by the underlying TURN candidate pair.
3812// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003813TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003814 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3815 3478};
3816 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3817 0};
3818 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3819 3478};
3820 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3821 0};
3822 cricket::TestTurnServer turn_server_1(network_thread(),
3823 turn_server_1_internal_address,
3824 turn_server_1_external_address);
3825 cricket::TestTurnServer turn_server_2(network_thread(),
3826 turn_server_2_internal_address,
3827 turn_server_2_external_address);
3828
3829 PeerConnectionInterface::RTCConfiguration client_1_config;
3830 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3831 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3832 ice_server_1.username = "test";
3833 ice_server_1.password = "test";
3834 client_1_config.servers.push_back(ice_server_1);
3835 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3836 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3837 client_1_config.turn_customizer = customizer1.get();
3838
3839 PeerConnectionInterface::RTCConfiguration client_2_config;
3840 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3841 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3842 ice_server_2.username = "test";
3843 ice_server_2.password = "test";
3844 client_2_config.servers.push_back(ice_server_2);
3845 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3846 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3847 client_2_config.turn_customizer = customizer2.get();
3848
3849 ASSERT_TRUE(
3850 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3851 ConnectFakeSignaling();
3852
3853 // Set "offer to receive audio/video" without adding any tracks, so we just
3854 // set up ICE/DTLS with no media.
3855 PeerConnectionInterface::RTCOfferAnswerOptions options;
3856 options.offer_to_receive_audio = 1;
3857 options.offer_to_receive_video = 1;
3858 caller()->SetOfferAnswerOptions(options);
3859 caller()->CreateAndSetAndSignalOffer();
3860 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3861
3862 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3863 EXPECT_GT(customizer1->modify_cnt_, 0u);
3864
3865 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3866 EXPECT_GT(customizer2->modify_cnt_, 0u);
3867
3868 // Need to free the clients here since they're using things we created on
3869 // the stack.
3870 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3871 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3872}
3873
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07003874// Verify that a SSLCertificateVerifier passed in through
3875// PeerConnectionDependencies is actually used by the underlying SSL
3876// implementation to determine whether a certificate presented by the TURN
3877// server is accepted by the client. Note that openssladapter_unittest.cc
3878// contains more detailed, lower-level tests.
3879TEST_P(PeerConnectionIntegrationTest,
3880 SSLCertificateVerifierUsedForTurnConnections) {
3881 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3882 3478};
3883 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3884
3885 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3886 // that host name verification passes on the fake certificate.
3887 cricket::TestTurnServer turn_server(
3888 network_thread(), turn_server_internal_address,
3889 turn_server_external_address, cricket::PROTO_TLS,
3890 /*ignore_bad_certs=*/true, "88.88.88.0");
3891
3892 webrtc::PeerConnectionInterface::IceServer ice_server;
3893 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3894 ice_server.username = "test";
3895 ice_server.password = "test";
3896
3897 PeerConnectionInterface::RTCConfiguration client_1_config;
3898 client_1_config.servers.push_back(ice_server);
3899 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3900
3901 PeerConnectionInterface::RTCConfiguration client_2_config;
3902 client_2_config.servers.push_back(ice_server);
3903 // Setting the type to kRelay forces the connection to go through a TURN
3904 // server.
3905 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3906
3907 // Get a copy to the pointer so we can verify calls later.
3908 rtc::TestCertificateVerifier* client_1_cert_verifier =
3909 new rtc::TestCertificateVerifier();
3910 client_1_cert_verifier->verify_certificate_ = true;
3911 rtc::TestCertificateVerifier* client_2_cert_verifier =
3912 new rtc::TestCertificateVerifier();
3913 client_2_cert_verifier->verify_certificate_ = true;
3914
3915 // Create the dependencies with the test certificate verifier.
3916 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3917 client_1_deps.tls_cert_verifier =
3918 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3919 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3920 client_2_deps.tls_cert_verifier =
3921 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3922
3923 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3924 client_1_config, std::move(client_1_deps), client_2_config,
3925 std::move(client_2_deps)));
3926 ConnectFakeSignaling();
3927
3928 // Set "offer to receive audio/video" without adding any tracks, so we just
3929 // set up ICE/DTLS with no media.
3930 PeerConnectionInterface::RTCOfferAnswerOptions options;
3931 options.offer_to_receive_audio = 1;
3932 options.offer_to_receive_video = 1;
3933 caller()->SetOfferAnswerOptions(options);
3934 caller()->CreateAndSetAndSignalOffer();
3935 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3936
3937 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
3938 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
3939
3940 // Need to free the clients here since they're using things we created on
3941 // the stack.
3942 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3943 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3944}
3945
3946TEST_P(PeerConnectionIntegrationTest,
3947 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
3948 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3949 3478};
3950 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3951
3952 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3953 // that host name verification passes on the fake certificate.
3954 cricket::TestTurnServer turn_server(
3955 network_thread(), turn_server_internal_address,
3956 turn_server_external_address, cricket::PROTO_TLS,
3957 /*ignore_bad_certs=*/true, "88.88.88.0");
3958
3959 webrtc::PeerConnectionInterface::IceServer ice_server;
3960 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3961 ice_server.username = "test";
3962 ice_server.password = "test";
3963
3964 PeerConnectionInterface::RTCConfiguration client_1_config;
3965 client_1_config.servers.push_back(ice_server);
3966 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3967
3968 PeerConnectionInterface::RTCConfiguration client_2_config;
3969 client_2_config.servers.push_back(ice_server);
3970 // Setting the type to kRelay forces the connection to go through a TURN
3971 // server.
3972 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3973
3974 // Get a copy to the pointer so we can verify calls later.
3975 rtc::TestCertificateVerifier* client_1_cert_verifier =
3976 new rtc::TestCertificateVerifier();
3977 client_1_cert_verifier->verify_certificate_ = false;
3978 rtc::TestCertificateVerifier* client_2_cert_verifier =
3979 new rtc::TestCertificateVerifier();
3980 client_2_cert_verifier->verify_certificate_ = false;
3981
3982 // Create the dependencies with the test certificate verifier.
3983 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3984 client_1_deps.tls_cert_verifier =
3985 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3986 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3987 client_2_deps.tls_cert_verifier =
3988 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3989
3990 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3991 client_1_config, std::move(client_1_deps), client_2_config,
3992 std::move(client_2_deps)));
3993 ConnectFakeSignaling();
3994
3995 // Set "offer to receive audio/video" without adding any tracks, so we just
3996 // set up ICE/DTLS with no media.
3997 PeerConnectionInterface::RTCOfferAnswerOptions options;
3998 options.offer_to_receive_audio = 1;
3999 options.offer_to_receive_video = 1;
4000 caller()->SetOfferAnswerOptions(options);
4001 caller()->CreateAndSetAndSignalOffer();
4002 bool wait_res = true;
4003 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4004 // properly, should be able to just wait for a state of "failed" instead of
4005 // waiting a fixed 10 seconds.
4006 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4007 ASSERT_FALSE(wait_res);
4008
4009 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4010 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4011
4012 // Need to free the clients here since they're using things we created on
4013 // the stack.
4014 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4015 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4016}
4017
deadbeefc964d0b2017-04-03 10:03:35 -07004018// Test that audio and video flow end-to-end when codec names don't use the
4019// expected casing, given that they're supposed to be case insensitive. To test
4020// this, all but one codec is removed from each media description, and its
4021// casing is changed.
4022//
4023// In the past, this has regressed and caused crashes/black video, due to the
4024// fact that code at some layers was doing case-insensitive comparisons and
4025// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004026TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004027 ASSERT_TRUE(CreatePeerConnectionWrappers());
4028 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004029 caller()->AddAudioVideoTracks();
4030 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004031
4032 // Remove all but one audio/video codec (opus and VP8), and change the
4033 // casing of the caller's generated offer.
4034 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4035 cricket::AudioContentDescription* audio =
4036 GetFirstAudioContentDescription(description);
4037 ASSERT_NE(nullptr, audio);
4038 auto audio_codecs = audio->codecs();
4039 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4040 [](const cricket::AudioCodec& codec) {
4041 return codec.name != "opus";
4042 }),
4043 audio_codecs.end());
4044 ASSERT_EQ(1u, audio_codecs.size());
4045 audio_codecs[0].name = "OpUs";
4046 audio->set_codecs(audio_codecs);
4047
4048 cricket::VideoContentDescription* video =
4049 GetFirstVideoContentDescription(description);
4050 ASSERT_NE(nullptr, video);
4051 auto video_codecs = video->codecs();
4052 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4053 [](const cricket::VideoCodec& codec) {
4054 return codec.name != "VP8";
4055 }),
4056 video_codecs.end());
4057 ASSERT_EQ(1u, video_codecs.size());
4058 video_codecs[0].name = "vP8";
4059 video->set_codecs(video_codecs);
4060 });
4061
4062 caller()->CreateAndSetAndSignalOffer();
4063 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4064
4065 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004066 MediaExpectations media_expectations;
4067 media_expectations.ExpectBidirectionalAudioAndVideo();
4068 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004069}
4070
Seth Hampson2f0d7022018-02-20 11:54:42 -08004071TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004072 ASSERT_TRUE(CreatePeerConnectionWrappers());
4073 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004074 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004075 caller()->CreateAndSetAndSignalOffer();
4076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004077 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004078 MediaExpectations media_expectations;
4079 media_expectations.CalleeExpectsSomeAudio(1);
4080 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004081 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4082 auto receiver = callee()->pc()->GetReceivers()[0];
4083 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4084
4085 auto contributing_sources = receiver->GetSources();
4086 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4087 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4088 contributing_sources[0].source_id());
4089}
4090
deadbeef2f425aa2017-04-14 10:41:32 -07004091// Test that if a track is removed and added again with a different stream ID,
4092// the new stream ID is successfully communicated in SDP and media continues to
4093// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004094// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4095// it will not reuse a transceiver that has already been sending. After creating
4096// a new transceiver it tries to create an offer with two senders of the same
4097// track ids and it fails.
4098TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004099 ASSERT_TRUE(CreatePeerConnectionWrappers());
4100 ConnectFakeSignaling();
4101
4102 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4103 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4104 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4105 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4106
4107 // Add track using stream 1, do offer/answer.
4108 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4109 caller()->CreateLocalAudioTrack();
4110 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4111 caller()->pc()->AddTrack(track, {stream_1.get()});
4112 caller()->CreateAndSetAndSignalOffer();
4113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004114 {
4115 MediaExpectations media_expectations;
4116 media_expectations.CalleeExpectsSomeAudio(1);
4117 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4118 }
deadbeef2f425aa2017-04-14 10:41:32 -07004119 // Remove the sender, and create a new one with the new stream.
4120 caller()->pc()->RemoveTrack(sender);
4121 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4122 caller()->CreateAndSetAndSignalOffer();
4123 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4124 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004125 {
4126 MediaExpectations media_expectations;
4127 media_expectations.CalleeExpectsSomeAudio();
4128 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4129 }
deadbeef2f425aa2017-04-14 10:41:32 -07004130}
4131
Seth Hampson2f0d7022018-02-20 11:54:42 -08004132TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004133 ASSERT_TRUE(CreatePeerConnectionWrappers());
4134 ConnectFakeSignaling();
4135
4136 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4137 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4138 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4139 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004140 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4141 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004142
Steve Anton15324772018-01-16 10:26:49 -08004143 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004144 caller()->CreateAndSetAndSignalOffer();
4145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4146}
4147
Steve Antonede9ca52017-10-16 13:04:27 -07004148// Test that if candidates are only signaled by applying full session
4149// descriptions (instead of using AddIceCandidate), the peers can connect to
4150// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004151TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004152 ASSERT_TRUE(CreatePeerConnectionWrappers());
4153 // Each side will signal the session descriptions but not candidates.
4154 ConnectFakeSignalingForSdpOnly();
4155
4156 // Add audio video track and exchange the initial offer/answer with media
4157 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004158 caller()->AddAudioVideoTracks();
4159 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004160 caller()->CreateAndSetAndSignalOffer();
4161
4162 // Wait for all candidates to be gathered on both the caller and callee.
4163 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4164 caller()->ice_gathering_state(), kDefaultTimeout);
4165 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4166 callee()->ice_gathering_state(), kDefaultTimeout);
4167
4168 // The candidates will now be included in the session description, so
4169 // signaling them will start the ICE connection.
4170 caller()->CreateAndSetAndSignalOffer();
4171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4172
4173 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004174 MediaExpectations media_expectations;
4175 media_expectations.ExpectBidirectionalAudioAndVideo();
4176 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004177}
4178
henrika5f6bf242017-11-01 11:06:56 +01004179// Test that SetAudioPlayout can be used to disable audio playout from the
4180// start, then later enable it. This may be useful, for example, if the caller
4181// needs to play a local ringtone until some event occurs, after which it
4182// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004183TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004184 ASSERT_TRUE(CreatePeerConnectionWrappers());
4185 ConnectFakeSignaling();
4186
4187 // Set up audio-only call where audio playout is disabled on caller's side.
4188 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004189 caller()->AddAudioTrack();
4190 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004191 caller()->CreateAndSetAndSignalOffer();
4192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4193
4194 // Pump messages for a second.
4195 WAIT(false, 1000);
4196 // Since audio playout is disabled, the caller shouldn't have received
4197 // anything (at the playout level, at least).
4198 EXPECT_EQ(0, caller()->audio_frames_received());
4199 // As a sanity check, make sure the callee (for which playout isn't disabled)
4200 // did still see frames on its audio level.
4201 ASSERT_GT(callee()->audio_frames_received(), 0);
4202
4203 // Enable playout again, and ensure audio starts flowing.
4204 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004205 MediaExpectations media_expectations;
4206 media_expectations.ExpectBidirectionalAudio();
4207 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004208}
4209
4210double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4211 auto report = pc->NewGetStats();
4212 auto track_stats_list =
4213 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4214 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4215 for (const auto* track_stats : track_stats_list) {
4216 if (track_stats->remote_source.is_defined() &&
4217 *track_stats->remote_source) {
4218 remote_track_stats = track_stats;
4219 break;
4220 }
4221 }
4222
4223 if (!remote_track_stats->total_audio_energy.is_defined()) {
4224 return 0.0;
4225 }
4226 return *remote_track_stats->total_audio_energy;
4227}
4228
4229// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4230// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004231TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004232 DisableAudioPlayoutStillGeneratesAudioStats) {
4233 ASSERT_TRUE(CreatePeerConnectionWrappers());
4234 ConnectFakeSignaling();
4235
4236 // Set up audio-only call where playout is disabled but audio-processing is
4237 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004238 caller()->AddAudioTrack();
4239 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004240 caller()->pc()->SetAudioPlayout(false);
4241
4242 caller()->CreateAndSetAndSignalOffer();
4243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4244
4245 // Wait for the callee to receive audio stats.
4246 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4247}
4248
henrika4f167df2017-11-01 14:45:55 +01004249// Test that SetAudioRecording can be used to disable audio recording from the
4250// start, then later enable it. This may be useful, for example, if the caller
4251// wants to ensure that no audio resources are active before a certain state
4252// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004253TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004254 ASSERT_TRUE(CreatePeerConnectionWrappers());
4255 ConnectFakeSignaling();
4256
4257 // Set up audio-only call where audio recording is disabled on caller's side.
4258 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004259 caller()->AddAudioTrack();
4260 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004261 caller()->CreateAndSetAndSignalOffer();
4262 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4263
4264 // Pump messages for a second.
4265 WAIT(false, 1000);
4266 // Since caller has disabled audio recording, the callee shouldn't have
4267 // received anything.
4268 EXPECT_EQ(0, callee()->audio_frames_received());
4269 // As a sanity check, make sure the caller did still see frames on its
4270 // audio level since audio recording is enabled on the calle side.
4271 ASSERT_GT(caller()->audio_frames_received(), 0);
4272
4273 // Enable audio recording again, and ensure audio starts flowing.
4274 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004275 MediaExpectations media_expectations;
4276 media_expectations.ExpectBidirectionalAudio();
4277 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004278}
4279
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004280// Test that after closing PeerConnections, they stop sending any packets (ICE,
4281// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004282TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004283 // Set up audio/video/data, wait for some frames to be received.
4284 ASSERT_TRUE(CreatePeerConnectionWrappers());
4285 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004286 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004287#ifdef HAVE_SCTP
4288 caller()->CreateDataChannel();
4289#endif
4290 caller()->CreateAndSetAndSignalOffer();
4291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004292 MediaExpectations media_expectations;
4293 media_expectations.CalleeExpectsSomeAudioAndVideo();
4294 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004295 // Close PeerConnections.
4296 caller()->pc()->Close();
4297 callee()->pc()->Close();
4298 // Pump messages for a second, and ensure no new packets end up sent.
4299 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4300 WAIT(false, 1000);
4301 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4302 EXPECT_EQ(sent_packets_a, sent_packets_b);
4303}
4304
Steve Anton7eca0932018-03-30 15:18:41 -07004305// Test that transport stats are generated by the RTCStatsCollector for a
4306// connection that only involves data channels. This is a regression test for
4307// crbug.com/826972.
4308#ifdef HAVE_SCTP
4309TEST_P(PeerConnectionIntegrationTest,
4310 TransportStatsReportedForDataChannelOnlyConnection) {
4311 ASSERT_TRUE(CreatePeerConnectionWrappers());
4312 ConnectFakeSignaling();
4313 caller()->CreateDataChannel();
4314
4315 caller()->CreateAndSetAndSignalOffer();
4316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4317 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4318
4319 auto caller_report = caller()->NewGetStats();
4320 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4321 auto callee_report = callee()->NewGetStats();
4322 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4323}
4324#endif // HAVE_SCTP
4325
Seth Hampson2f0d7022018-02-20 11:54:42 -08004326INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4327 PeerConnectionIntegrationTest,
4328 Values(SdpSemantics::kPlanB,
4329 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004330
Steve Anton74255ff2018-01-24 18:32:57 -08004331// Tests that verify interoperability between Plan B and Unified Plan
4332// PeerConnections.
4333class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004334 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004335 public ::testing::WithParamInterface<
4336 std::tuple<SdpSemantics, SdpSemantics>> {
4337 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004338 // Setting the SdpSemantics for the base test to kDefault does not matter
4339 // because we specify not to use the test semantics when creating
4340 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004341 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004342 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004343 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004344 callee_semantics_(std::get<1>(GetParam())) {}
4345
4346 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004347 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4348 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004349 }
4350
4351 const SdpSemantics caller_semantics_;
4352 const SdpSemantics callee_semantics_;
4353};
4354
4355TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4356 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4357 ConnectFakeSignaling();
4358
4359 caller()->CreateAndSetAndSignalOffer();
4360 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4361}
4362
4363TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4364 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4365 ConnectFakeSignaling();
4366 auto audio_sender = caller()->AddAudioTrack();
4367
4368 caller()->CreateAndSetAndSignalOffer();
4369 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4370
4371 // Verify that one audio receiver has been created on the remote and that it
4372 // has the same track ID as the sending track.
4373 auto receivers = callee()->pc()->GetReceivers();
4374 ASSERT_EQ(1u, receivers.size());
4375 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4376 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4377
Seth Hampson2f0d7022018-02-20 11:54:42 -08004378 MediaExpectations media_expectations;
4379 media_expectations.CalleeExpectsSomeAudio();
4380 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004381}
4382
4383TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4384 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4385 ConnectFakeSignaling();
4386 auto video_sender = caller()->AddVideoTrack();
4387 auto audio_sender = caller()->AddAudioTrack();
4388
4389 caller()->CreateAndSetAndSignalOffer();
4390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4391
4392 // Verify that one audio and one video receiver have been created on the
4393 // remote and that they have the same track IDs as the sending tracks.
4394 auto audio_receivers =
4395 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4396 ASSERT_EQ(1u, audio_receivers.size());
4397 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4398 auto video_receivers =
4399 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4400 ASSERT_EQ(1u, video_receivers.size());
4401 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4402
Seth Hampson2f0d7022018-02-20 11:54:42 -08004403 MediaExpectations media_expectations;
4404 media_expectations.CalleeExpectsSomeAudioAndVideo();
4405 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004406}
4407
4408TEST_P(PeerConnectionIntegrationInteropTest,
4409 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4410 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4411 ConnectFakeSignaling();
4412 caller()->AddAudioVideoTracks();
4413 callee()->AddAudioVideoTracks();
4414
4415 caller()->CreateAndSetAndSignalOffer();
4416 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4417
Seth Hampson2f0d7022018-02-20 11:54:42 -08004418 MediaExpectations media_expectations;
4419 media_expectations.ExpectBidirectionalAudioAndVideo();
4420 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004421}
4422
4423TEST_P(PeerConnectionIntegrationInteropTest,
4424 ReverseRolesOneAudioLocalToOneVideoRemote) {
4425 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4426 ConnectFakeSignaling();
4427 caller()->AddAudioTrack();
4428 callee()->AddVideoTrack();
4429
4430 caller()->CreateAndSetAndSignalOffer();
4431 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4432
4433 // Verify that only the audio track has been negotiated.
4434 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4435 // Might also check that the callee's NegotiationNeeded flag is set.
4436
4437 // Reverse roles.
4438 callee()->CreateAndSetAndSignalOffer();
4439 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4440
Seth Hampson2f0d7022018-02-20 11:54:42 -08004441 MediaExpectations media_expectations;
4442 media_expectations.CallerExpectsSomeVideo();
4443 media_expectations.CalleeExpectsSomeAudio();
4444 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004445}
4446
Steve Antonba42e992018-04-09 14:10:01 -07004447INSTANTIATE_TEST_CASE_P(
4448 PeerConnectionIntegrationTest,
4449 PeerConnectionIntegrationInteropTest,
4450 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4451 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4452
4453// Test that if the Unified Plan side offers two video tracks then the Plan B
4454// side will only see the first one and ignore the second.
4455TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004456 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4457 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004458 ConnectFakeSignaling();
4459 auto first_sender = caller()->AddVideoTrack();
4460 caller()->AddVideoTrack();
4461
4462 caller()->CreateAndSetAndSignalOffer();
4463 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4464
4465 // Verify that there is only one receiver and it corresponds to the first
4466 // added track.
4467 auto receivers = callee()->pc()->GetReceivers();
4468 ASSERT_EQ(1u, receivers.size());
4469 EXPECT_TRUE(receivers[0]->track()->enabled());
4470 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4471
Seth Hampson2f0d7022018-02-20 11:54:42 -08004472 MediaExpectations media_expectations;
4473 media_expectations.CalleeExpectsSomeVideo();
4474 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004475}
4476
deadbeef1dcb1642017-03-29 21:08:16 -07004477} // namespace
4478
4479#endif // if !defined(THREAD_SANITIZER)