blob: 7cd03a431158360f346689834e486e057605c8f9 [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/mediastreaminterface.h"
28#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070029#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010030#include "api/rtpreceiverinterface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080031#include "api/test/loopback_media_transport.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020032#include "api/umametrics.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"
Qingsi Wang7685e862018-06-11 20:15:46 -070036#include "call/call.h"
37#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
38#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "media/engine/fakewebrtcvideoengine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "media/engine/webrtcmediaengine.h"
41#include "modules/audio_processing/include/audio_processing.h"
Zach Stein6fcdc2f2018-08-23 16:25:55 -070042#include "p2p/base/mockasyncresolver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "p2p/base/p2pconstants.h"
44#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070045#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020046#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "p2p/base/testturnserver.h"
48#include "p2p/client/basicportallocator.h"
49#include "pc/dtmfsender.h"
50#include "pc/localaudiosource.h"
51#include "pc/mediasession.h"
52#include "pc/peerconnection.h"
53#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080054#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080055#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020057#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "pc/test/fakertccertificategenerator.h"
59#include "pc/test/fakevideotrackrenderer.h"
60#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070062#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020064#include "rtc_base/numerics/safe_conversions.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070065#include "rtc_base/testcertificateverifier.h"
Johannes Kron965e7942018-09-13 15:36:20 +020066#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#include "rtc_base/virtualsocketserver.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020068#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020069#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070070
71using cricket::ContentInfo;
72using cricket::FakeWebRtcVideoDecoder;
73using cricket::FakeWebRtcVideoDecoderFactory;
74using cricket::FakeWebRtcVideoEncoder;
75using cricket::FakeWebRtcVideoEncoderFactory;
76using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070077using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070078using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080079using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070080using ::testing::ElementsAre;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070081using ::testing::Return;
82using ::testing::SetArgPointee;
Steve Antonede9ca52017-10-16 13:04:27 -070083using ::testing::Values;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070084using ::testing::_;
deadbeef1dcb1642017-03-29 21:08:16 -070085using webrtc::DataBuffer;
86using webrtc::DataChannelInterface;
87using webrtc::DtmfSender;
88using webrtc::DtmfSenderInterface;
89using webrtc::DtmfSenderObserverInterface;
Steve Anton15324772018-01-16 10:26:49 -080090using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070091using webrtc::MediaStreamInterface;
92using webrtc::MediaStreamTrackInterface;
93using webrtc::MockCreateSessionDescriptionObserver;
94using webrtc::MockDataChannelObserver;
95using webrtc::MockSetSessionDescriptionObserver;
96using webrtc::MockStatsObserver;
97using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070098using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070099using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -0800100using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -0700101using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -0700102using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -0800103using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -0700104using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -0800105using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100106using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800107using webrtc::RtpSenderInterface;
108using webrtc::RtpTransceiverDirection;
109using webrtc::RtpTransceiverInit;
110using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800111using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800112using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700113using webrtc::SessionDescriptionInterface;
114using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800115using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700116
117namespace {
118
119static const int kDefaultTimeout = 10000;
120static const int kMaxWaitForStatsMs = 3000;
121static const int kMaxWaitForActivationMs = 5000;
122static const int kMaxWaitForFramesMs = 10000;
123// Default number of audio/video frames to wait for before considering a test
124// successful.
125static const int kDefaultExpectedAudioFrameCount = 3;
126static const int kDefaultExpectedVideoFrameCount = 3;
127
deadbeef1dcb1642017-03-29 21:08:16 -0700128static const char kDataChannelLabel[] = "data_channel";
129
130// SRTP cipher name negotiated by the tests. This must be updated if the
131// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700132static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700133static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
134
Steve Antonede9ca52017-10-16 13:04:27 -0700135static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
136
deadbeef1dcb1642017-03-29 21:08:16 -0700137// Helper function for constructing offer/answer options to initiate an ICE
138// restart.
139PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
140 PeerConnectionInterface::RTCOfferAnswerOptions options;
141 options.ice_restart = true;
142 return options;
143}
144
deadbeefd8ad7882017-04-18 16:01:17 -0700145// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
146// attribute from received SDP, simulating a legacy endpoint.
147void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
148 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800149 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700150 }
151 desc->set_msid_supported(false);
152}
153
Seth Hampson5897a6e2018-04-03 11:16:33 -0700154// Removes all stream information besides the stream ids, simulating an
155// endpoint that only signals a=msid lines to convey stream_ids.
156void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
157 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700158 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700159 std::vector<std::string> stream_ids;
160 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700161 const StreamParams& first_stream =
162 content.media_description()->streams()[0];
163 track_id = first_stream.id;
164 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700165 }
166 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700167 StreamParams new_stream;
168 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700169 new_stream.set_stream_ids(stream_ids);
170 content.media_description()->AddStream(new_stream);
171 }
172}
173
zhihuangf8164932017-05-19 13:09:47 -0700174int FindFirstMediaStatsIndexByKind(
175 const std::string& kind,
176 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
177 media_stats_vec) {
178 for (size_t i = 0; i < media_stats_vec.size(); i++) {
179 if (media_stats_vec[i]->kind.ValueToString() == kind) {
180 return i;
181 }
182 }
183 return -1;
184}
185
deadbeef1dcb1642017-03-29 21:08:16 -0700186class SignalingMessageReceiver {
187 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800188 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700189 virtual void ReceiveIceMessage(const std::string& sdp_mid,
190 int sdp_mline_index,
191 const std::string& msg) = 0;
192
193 protected:
194 SignalingMessageReceiver() {}
195 virtual ~SignalingMessageReceiver() {}
196};
197
198class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
199 public:
200 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
201 : expected_media_type_(media_type) {}
202
203 void OnFirstPacketReceived(cricket::MediaType media_type) override {
204 ASSERT_EQ(expected_media_type_, media_type);
205 first_packet_received_ = true;
206 }
207
208 bool first_packet_received() const { return first_packet_received_; }
209
210 virtual ~MockRtpReceiverObserver() {}
211
212 private:
213 bool first_packet_received_ = false;
214 cricket::MediaType expected_media_type_;
215};
216
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700217// Used by PeerConnectionWrapper::OnIceCandidate to allow a test to modify an
218// ICE candidate before it is signaled.
219class IceCandidateReplacerInterface {
220 public:
221 virtual ~IceCandidateReplacerInterface() = default;
222 // Return nullptr to drop the candidate (it won't be signaled to the other
223 // side).
224 virtual std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
225 const webrtc::IceCandidateInterface*) = 0;
226};
227
deadbeef1dcb1642017-03-29 21:08:16 -0700228// Helper class that wraps a peer connection, observes it, and can accept
229// signaling messages from another wrapper.
230//
231// Uses a fake network, fake A/V capture, and optionally fake
232// encoders/decoders, though they aren't used by default since they don't
233// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700234// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800235// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700236class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800237 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700238 public:
239 // Different factory methods for convenience.
240 // TODO(deadbeef): Could use the pattern of:
241 //
242 // PeerConnectionWrapper =
243 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
244 //
245 // To reduce some code duplication.
246 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
247 const std::string& debug_name,
248 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
249 rtc::Thread* network_thread,
250 rtc::Thread* worker_thread) {
251 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700252 webrtc::PeerConnectionDependencies dependencies(nullptr);
253 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200254 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800255 worker_thread, nullptr,
256 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700257 delete client;
258 return nullptr;
259 }
260 return client;
261 }
262
deadbeef2f425aa2017-04-14 10:41:32 -0700263 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
264 return peer_connection_factory_.get();
265 }
266
deadbeef1dcb1642017-03-29 21:08:16 -0700267 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
268
269 // If a signaling message receiver is set (via ConnectFakeSignaling), this
270 // will set the whole offer/answer exchange in motion. Just need to wait for
271 // the signaling state to reach "stable".
272 void CreateAndSetAndSignalOffer() {
273 auto offer = CreateOffer();
274 ASSERT_NE(nullptr, offer);
275 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
276 }
277
278 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
279 // when a remote offer is received (via fake signaling) and an answer is
280 // generated. By default, uses default options.
281 void SetOfferAnswerOptions(
282 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
283 offer_answer_options_ = options;
284 }
285
286 // Set a callback to be invoked when SDP is received via the fake signaling
287 // channel, which provides an opportunity to munge (modify) the SDP. This is
288 // used to test SDP being applied that a PeerConnection would normally not
289 // generate, but a non-JSEP endpoint might.
290 void SetReceivedSdpMunger(
291 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100292 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700293 }
294
deadbeefc964d0b2017-04-03 10:03:35 -0700295 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700296 // generated.
297 void SetGeneratedSdpMunger(
298 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100299 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700300 }
301
Seth Hampson2f0d7022018-02-20 11:54:42 -0800302 // Set a callback to be invoked when a remote offer is received via the fake
303 // signaling channel. This provides an opportunity to change the
304 // PeerConnection state before an answer is created and sent to the caller.
305 void SetRemoteOfferHandler(std::function<void()> handler) {
306 remote_offer_handler_ = std::move(handler);
307 }
308
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700309 void SetLocalIceCandidateReplacer(
310 std::unique_ptr<IceCandidateReplacerInterface> replacer) {
311 local_ice_candidate_replacer_ = std::move(replacer);
312 }
313
Steve Antonede9ca52017-10-16 13:04:27 -0700314 // Every ICE connection state in order that has been seen by the observer.
315 std::vector<PeerConnectionInterface::IceConnectionState>
316 ice_connection_state_history() const {
317 return ice_connection_state_history_;
318 }
Steve Anton6f25b092017-10-23 09:39:20 -0700319 void clear_ice_connection_state_history() {
320 ice_connection_state_history_.clear();
321 }
Steve Antonede9ca52017-10-16 13:04:27 -0700322
Jonas Olsson635474e2018-10-18 15:58:17 +0200323 // Every PeerConnection state in order that has been seen by the observer.
324 std::vector<PeerConnectionInterface::PeerConnectionState>
325 peer_connection_state_history() const {
326 return peer_connection_state_history_;
327 }
328
Steve Antonede9ca52017-10-16 13:04:27 -0700329 // Every ICE gathering state in order that has been seen by the observer.
330 std::vector<PeerConnectionInterface::IceGatheringState>
331 ice_gathering_state_history() const {
332 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700333 }
334
Steve Anton15324772018-01-16 10:26:49 -0800335 void AddAudioVideoTracks() {
336 AddAudioTrack();
337 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700338 }
339
Steve Anton74255ff2018-01-24 18:32:57 -0800340 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
341 return AddTrack(CreateLocalAudioTrack());
342 }
deadbeef1dcb1642017-03-29 21:08:16 -0700343
Steve Anton74255ff2018-01-24 18:32:57 -0800344 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
345 return AddTrack(CreateLocalVideoTrack());
346 }
deadbeef1dcb1642017-03-29 21:08:16 -0700347
348 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200349 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700350 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200351 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700352 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200353 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700354 // TODO(perkj): Test audio source when it is implemented. Currently audio
355 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700356 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700357 source);
358 }
359
360 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200361 webrtc::FakePeriodicVideoSource::Config config;
362 config.timestamp_offset_ms = rtc::TimeMillis();
363 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700364 }
365
366 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200367 CreateLocalVideoTrackWithConfig(
368 webrtc::FakePeriodicVideoSource::Config config) {
369 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700370 }
371
372 rtc::scoped_refptr<webrtc::VideoTrackInterface>
373 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200374 webrtc::FakePeriodicVideoSource::Config config;
375 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200376 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200377 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700378 }
379
Steve Anton74255ff2018-01-24 18:32:57 -0800380 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
381 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800382 const std::vector<std::string>& stream_ids = {}) {
383 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800384 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800385 return result.MoveValue();
386 }
387
388 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
389 cricket::MediaType media_type) {
390 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
391 for (auto receiver : pc()->GetReceivers()) {
392 if (receiver->media_type() == media_type) {
393 receivers.push_back(receiver);
394 }
395 }
396 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700397 }
398
Seth Hampson2f0d7022018-02-20 11:54:42 -0800399 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
400 cricket::MediaType media_type) {
401 for (auto transceiver : pc()->GetTransceivers()) {
402 if (transceiver->receiver()->media_type() == media_type) {
403 return transceiver;
404 }
405 }
406 return nullptr;
407 }
408
deadbeef1dcb1642017-03-29 21:08:16 -0700409 bool SignalingStateStable() {
410 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
411 }
412
413 void CreateDataChannel() { CreateDataChannel(nullptr); }
414
415 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700416 CreateDataChannel(kDataChannelLabel, init);
417 }
418
419 void CreateDataChannel(const std::string& label,
420 const webrtc::DataChannelInit* init) {
421 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700422 ASSERT_TRUE(data_channel_.get() != nullptr);
423 data_observer_.reset(new MockDataChannelObserver(data_channel_));
424 }
425
426 DataChannelInterface* data_channel() { return data_channel_; }
427 const MockDataChannelObserver* data_observer() const {
428 return data_observer_.get();
429 }
430
431 int audio_frames_received() const {
432 return fake_audio_capture_module_->frames_received();
433 }
434
435 // Takes minimum of video frames received for each track.
436 //
437 // Can be used like:
438 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
439 //
440 // To ensure that all video tracks received at least a certain number of
441 // frames.
442 int min_video_frames_received_per_track() const {
443 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200444 if (fake_video_renderers_.empty()) {
445 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700446 }
deadbeef1dcb1642017-03-29 21:08:16 -0700447
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200448 for (const auto& pair : fake_video_renderers_) {
449 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700450 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200451 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700452 }
453
454 // Returns a MockStatsObserver in a state after stats gathering finished,
455 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700456 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700457 webrtc::MediaStreamTrackInterface* track) {
458 rtc::scoped_refptr<MockStatsObserver> observer(
459 new rtc::RefCountedObject<MockStatsObserver>());
460 EXPECT_TRUE(peer_connection_->GetStats(
461 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
462 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
463 return observer;
464 }
465
466 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700467 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
468 return OldGetStatsForTrack(nullptr);
469 }
470
471 // Synchronously gets stats and returns them. If it times out, fails the test
472 // and returns null.
473 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
474 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
475 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
476 peer_connection_->GetStats(callback);
477 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
478 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700479 }
480
481 int rendered_width() {
482 EXPECT_FALSE(fake_video_renderers_.empty());
483 return fake_video_renderers_.empty()
484 ? 0
485 : fake_video_renderers_.begin()->second->width();
486 }
487
488 int rendered_height() {
489 EXPECT_FALSE(fake_video_renderers_.empty());
490 return fake_video_renderers_.empty()
491 ? 0
492 : fake_video_renderers_.begin()->second->height();
493 }
494
495 double rendered_aspect_ratio() {
496 if (rendered_height() == 0) {
497 return 0.0;
498 }
499 return static_cast<double>(rendered_width()) / rendered_height();
500 }
501
502 webrtc::VideoRotation rendered_rotation() {
503 EXPECT_FALSE(fake_video_renderers_.empty());
504 return fake_video_renderers_.empty()
505 ? webrtc::kVideoRotation_0
506 : fake_video_renderers_.begin()->second->rotation();
507 }
508
509 int local_rendered_width() {
510 return local_video_renderer_ ? local_video_renderer_->width() : 0;
511 }
512
513 int local_rendered_height() {
514 return local_video_renderer_ ? local_video_renderer_->height() : 0;
515 }
516
517 double local_rendered_aspect_ratio() {
518 if (local_rendered_height() == 0) {
519 return 0.0;
520 }
521 return static_cast<double>(local_rendered_width()) /
522 local_rendered_height();
523 }
524
525 size_t number_of_remote_streams() {
526 if (!pc()) {
527 return 0;
528 }
529 return pc()->remote_streams()->count();
530 }
531
532 StreamCollectionInterface* remote_streams() const {
533 if (!pc()) {
534 ADD_FAILURE();
535 return nullptr;
536 }
537 return pc()->remote_streams();
538 }
539
540 StreamCollectionInterface* local_streams() {
541 if (!pc()) {
542 ADD_FAILURE();
543 return nullptr;
544 }
545 return pc()->local_streams();
546 }
547
548 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
549 return pc()->signaling_state();
550 }
551
552 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
553 return pc()->ice_connection_state();
554 }
555
Jonas Olsson1e87b4f2018-11-22 16:50:37 +0100556 webrtc::PeerConnectionInterface::PeerConnectionState peer_connection_state() {
557 return pc()->peer_connection_state();
558 }
559
deadbeef1dcb1642017-03-29 21:08:16 -0700560 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
561 return pc()->ice_gathering_state();
562 }
563
564 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
565 // GetReceivers. They're updated automatically when a remote offer/answer
566 // from the fake signaling channel is applied, or when
567 // ResetRtpReceiverObservers below is called.
568 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
569 rtp_receiver_observers() {
570 return rtp_receiver_observers_;
571 }
572
573 void ResetRtpReceiverObservers() {
574 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100575 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
576 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700577 std::unique_ptr<MockRtpReceiverObserver> observer(
578 new MockRtpReceiverObserver(receiver->media_type()));
579 receiver->SetObserver(observer.get());
580 rtp_receiver_observers_.push_back(std::move(observer));
581 }
582 }
583
Steve Antonede9ca52017-10-16 13:04:27 -0700584 rtc::FakeNetworkManager* network() const {
585 return fake_network_manager_.get();
586 }
587 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
588
Qingsi Wang7685e862018-06-11 20:15:46 -0700589 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
590 return event_log_factory_;
591 }
592
deadbeef1dcb1642017-03-29 21:08:16 -0700593 private:
594 explicit PeerConnectionWrapper(const std::string& debug_name)
595 : debug_name_(debug_name) {}
596
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800597 bool Init(
598 const PeerConnectionFactory::Options* options,
599 const PeerConnectionInterface::RTCConfiguration* config,
600 webrtc::PeerConnectionDependencies dependencies,
601 rtc::Thread* network_thread,
602 rtc::Thread* worker_thread,
603 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
604 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700605 // There's an error in this test code if Init ends up being called twice.
606 RTC_DCHECK(!peer_connection_);
607 RTC_DCHECK(!peer_connection_factory_);
608
609 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700610 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700611
612 std::unique_ptr<cricket::PortAllocator> port_allocator(
613 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700614 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700615 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
616 if (!fake_audio_capture_module_) {
617 return false;
618 }
deadbeef1dcb1642017-03-29 21:08:16 -0700619 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700620
621 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
622 pc_factory_dependencies.network_thread = network_thread;
623 pc_factory_dependencies.worker_thread = worker_thread;
624 pc_factory_dependencies.signaling_thread = signaling_thread;
625 pc_factory_dependencies.media_engine =
626 cricket::WebRtcMediaEngineFactory::Create(
627 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
628 fake_audio_capture_module_),
629 webrtc::CreateBuiltinAudioEncoderFactory(),
630 webrtc::CreateBuiltinAudioDecoderFactory(),
631 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000632 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700633 webrtc::AudioProcessingBuilder().Create());
634 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
635 if (event_log_factory) {
636 event_log_factory_ = event_log_factory.get();
637 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
638 } else {
639 pc_factory_dependencies.event_log_factory =
640 webrtc::CreateRtcEventLogFactory();
641 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800642 if (media_transport_factory) {
643 pc_factory_dependencies.media_transport_factory =
644 std::move(media_transport_factory);
645 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700646 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
647 std::move(pc_factory_dependencies));
648
deadbeef1dcb1642017-03-29 21:08:16 -0700649 if (!peer_connection_factory_) {
650 return false;
651 }
652 if (options) {
653 peer_connection_factory_->SetOptions(*options);
654 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800655 if (config) {
656 sdp_semantics_ = config->sdp_semantics;
657 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700658
659 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200660 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700661 return peer_connection_.get() != nullptr;
662 }
663
664 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700665 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700666 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700667 PeerConnectionInterface::RTCConfiguration modified_config;
668 // If |config| is null, this will result in a default configuration being
669 // used.
670 if (config) {
671 modified_config = *config;
672 }
673 // Disable resolution adaptation; we don't want it interfering with the
674 // test results.
675 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
676 // ratios and not specific resolutions, is this even necessary?
677 modified_config.set_cpu_adaptation(false);
678
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700679 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700680 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700681 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700682 }
683
684 void set_signaling_message_receiver(
685 SignalingMessageReceiver* signaling_message_receiver) {
686 signaling_message_receiver_ = signaling_message_receiver;
687 }
688
689 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
690
Steve Antonede9ca52017-10-16 13:04:27 -0700691 void set_signal_ice_candidates(bool signal) {
692 signal_ice_candidates_ = signal;
693 }
694
deadbeef1dcb1642017-03-29 21:08:16 -0700695 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200696 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700697 // Set max frame rate to 10fps to reduce the risk of test flakiness.
698 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200699 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700700
Niels Möller5c7efe72018-05-11 10:34:46 +0200701 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200702 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
703 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700704 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200705 peer_connection_factory_->CreateVideoTrack(
706 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700707 if (!local_video_renderer_) {
708 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
709 }
710 return track;
711 }
712
713 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100714 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800715 std::unique_ptr<SessionDescriptionInterface> desc =
716 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700717 if (received_sdp_munger_) {
718 received_sdp_munger_(desc->description());
719 }
720
721 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
722 // Setting a remote description may have changed the number of receivers,
723 // so reset the receiver observers.
724 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800725 if (remote_offer_handler_) {
726 remote_offer_handler_();
727 }
deadbeef1dcb1642017-03-29 21:08:16 -0700728 auto answer = CreateAnswer();
729 ASSERT_NE(nullptr, answer);
730 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
731 }
732
733 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100734 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800735 std::unique_ptr<SessionDescriptionInterface> desc =
736 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700737 if (received_sdp_munger_) {
738 received_sdp_munger_(desc->description());
739 }
740
741 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
742 // Set the RtpReceiverObserver after receivers are created.
743 ResetRtpReceiverObservers();
744 }
745
746 // Returns null on failure.
747 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
748 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
749 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
750 pc()->CreateOffer(observer, offer_answer_options_);
751 return WaitForDescriptionFromObserver(observer);
752 }
753
754 // Returns null on failure.
755 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
756 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
757 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
758 pc()->CreateAnswer(observer, offer_answer_options_);
759 return WaitForDescriptionFromObserver(observer);
760 }
761
762 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100763 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700764 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
765 if (!observer->result()) {
766 return nullptr;
767 }
768 auto description = observer->MoveDescription();
769 if (generated_sdp_munger_) {
770 generated_sdp_munger_(description->description());
771 }
772 return description;
773 }
774
775 // Setting the local description and sending the SDP message over the fake
776 // signaling channel are combined into the same method because the SDP
777 // message needs to be sent as soon as SetLocalDescription finishes, without
778 // waiting for the observer to be called. This ensures that ICE candidates
779 // don't outrace the description.
780 bool SetLocalDescriptionAndSendSdpMessage(
781 std::unique_ptr<SessionDescriptionInterface> desc) {
782 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
783 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100784 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800785 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700786 std::string sdp;
787 EXPECT_TRUE(desc->ToString(&sdp));
788 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800789 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
790 RemoveUnusedVideoRenderers();
791 }
deadbeef1dcb1642017-03-29 21:08:16 -0700792 // As mentioned above, we need to send the message immediately after
793 // SetLocalDescription.
794 SendSdpMessage(type, sdp);
795 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
796 return true;
797 }
798
799 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
800 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
801 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100802 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700803 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800804 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
805 RemoveUnusedVideoRenderers();
806 }
deadbeef1dcb1642017-03-29 21:08:16 -0700807 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
808 return observer->result();
809 }
810
Seth Hampson2f0d7022018-02-20 11:54:42 -0800811 // This is a work around to remove unused fake_video_renderers from
812 // transceivers that have either stopped or are no longer receiving.
813 void RemoveUnusedVideoRenderers() {
814 auto transceivers = pc()->GetTransceivers();
815 for (auto& transceiver : transceivers) {
816 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
817 continue;
818 }
819 // Remove fake video renderers from any stopped transceivers.
820 if (transceiver->stopped()) {
821 auto it =
822 fake_video_renderers_.find(transceiver->receiver()->track()->id());
823 if (it != fake_video_renderers_.end()) {
824 fake_video_renderers_.erase(it);
825 }
826 }
827 // Remove fake video renderers from any transceivers that are no longer
828 // receiving.
829 if ((transceiver->current_direction() &&
830 !webrtc::RtpTransceiverDirectionHasRecv(
831 *transceiver->current_direction()))) {
832 auto it =
833 fake_video_renderers_.find(transceiver->receiver()->track()->id());
834 if (it != fake_video_renderers_.end()) {
835 fake_video_renderers_.erase(it);
836 }
837 }
838 }
839 }
840
deadbeef1dcb1642017-03-29 21:08:16 -0700841 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
842 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800843 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700844 if (signaling_delay_ms_ == 0) {
845 RelaySdpMessageIfReceiverExists(type, msg);
846 } else {
847 invoker_.AsyncInvokeDelayed<void>(
848 RTC_FROM_HERE, rtc::Thread::Current(),
849 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
850 this, type, msg),
851 signaling_delay_ms_);
852 }
853 }
854
Steve Antona3a92c22017-12-07 10:27:41 -0800855 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700856 if (signaling_message_receiver_) {
857 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
858 }
859 }
860
861 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
862 // default).
863 void SendIceMessage(const std::string& sdp_mid,
864 int sdp_mline_index,
865 const std::string& msg) {
866 if (signaling_delay_ms_ == 0) {
867 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
868 } else {
869 invoker_.AsyncInvokeDelayed<void>(
870 RTC_FROM_HERE, rtc::Thread::Current(),
871 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
872 this, sdp_mid, sdp_mline_index, msg),
873 signaling_delay_ms_);
874 }
875 }
876
877 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
878 int sdp_mline_index,
879 const std::string& msg) {
880 if (signaling_message_receiver_) {
881 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
882 msg);
883 }
884 }
885
886 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800887 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
888 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700889 HandleIncomingOffer(msg);
890 } else {
891 HandleIncomingAnswer(msg);
892 }
893 }
894
895 void ReceiveIceMessage(const std::string& sdp_mid,
896 int sdp_mline_index,
897 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100898 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700899 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
900 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
901 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
902 }
903
904 // PeerConnectionObserver callbacks.
905 void OnSignalingChange(
906 webrtc::PeerConnectionInterface::SignalingState new_state) override {
907 EXPECT_EQ(pc()->signaling_state(), new_state);
908 }
Steve Anton15324772018-01-16 10:26:49 -0800909 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
910 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
911 streams) override {
912 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
913 rtc::scoped_refptr<VideoTrackInterface> video_track(
914 static_cast<VideoTrackInterface*>(receiver->track().get()));
915 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700916 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800917 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200918 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700919 }
920 }
Steve Anton15324772018-01-16 10:26:49 -0800921 void OnRemoveTrack(
922 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
923 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
924 auto it = fake_video_renderers_.find(receiver->track()->id());
925 RTC_DCHECK(it != fake_video_renderers_.end());
926 fake_video_renderers_.erase(it);
927 }
928 }
deadbeef1dcb1642017-03-29 21:08:16 -0700929 void OnRenegotiationNeeded() override {}
930 void OnIceConnectionChange(
931 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
932 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700933 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700934 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200935 void OnConnectionChange(
936 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
937 peer_connection_state_history_.push_back(new_state);
938 }
939
deadbeef1dcb1642017-03-29 21:08:16 -0700940 void OnIceGatheringChange(
941 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700942 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700943 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700944 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700945 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceIceCandidate(
946 const webrtc::IceCandidateInterface* candidate) {
947 std::string candidate_string;
948 candidate->ToString(&candidate_string);
949
950 auto owned_candidate =
951 local_ice_candidate_replacer_->ReplaceCandidate(candidate);
952 if (!owned_candidate) {
953 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer dropped \""
954 << candidate_string << "\"";
955 return nullptr;
956 }
957 std::string owned_candidate_string;
958 owned_candidate->ToString(&owned_candidate_string);
959 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer changed \""
960 << candidate_string << "\" to \"" << owned_candidate_string
961 << "\"";
962 return owned_candidate;
963 }
deadbeef1dcb1642017-03-29 21:08:16 -0700964 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100965 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700966
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700967 const webrtc::IceCandidateInterface* new_candidate = candidate;
968 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate;
969 if (local_ice_candidate_replacer_) {
970 owned_candidate = ReplaceIceCandidate(candidate);
971 if (!owned_candidate) {
972 return; // The candidate was dropped.
973 }
974 new_candidate = owned_candidate.get();
975 }
976
deadbeef1dcb1642017-03-29 21:08:16 -0700977 std::string ice_sdp;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700978 EXPECT_TRUE(new_candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700979 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700980 // Remote party may be deleted.
981 return;
982 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700983 SendIceMessage(new_candidate->sdp_mid(), new_candidate->sdp_mline_index(),
984 ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700985 }
986 void OnDataChannel(
987 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100988 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700989 data_channel_ = data_channel;
990 data_observer_.reset(new MockDataChannelObserver(data_channel));
991 }
992
deadbeef1dcb1642017-03-29 21:08:16 -0700993 std::string debug_name_;
994
995 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
996
997 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
998 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
999 peer_connection_factory_;
1000
Steve Antonede9ca52017-10-16 13:04:27 -07001001 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001002 // Needed to keep track of number of frames sent.
1003 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1004 // Needed to keep track of number of frames received.
1005 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1006 fake_video_renderers_;
1007 // Needed to ensure frames aren't received for removed tracks.
1008 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1009 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001010
1011 // For remote peer communication.
1012 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1013 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001014 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -07001015
Niels Möller5c7efe72018-05-11 10:34:46 +02001016 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001017 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001018 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1019 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001020 // |local_video_renderer_| attached to the first created local video track.
1021 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1022
Seth Hampson2f0d7022018-02-20 11:54:42 -08001023 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001024 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1025 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1026 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001027 std::function<void()> remote_offer_handler_;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001028 std::unique_ptr<IceCandidateReplacerInterface> local_ice_candidate_replacer_;
deadbeef1dcb1642017-03-29 21:08:16 -07001029 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1030 std::unique_ptr<MockDataChannelObserver> data_observer_;
1031
1032 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1033
Steve Antonede9ca52017-10-16 13:04:27 -07001034 std::vector<PeerConnectionInterface::IceConnectionState>
1035 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001036 std::vector<PeerConnectionInterface::PeerConnectionState>
1037 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001038 std::vector<PeerConnectionInterface::IceGatheringState>
1039 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001040
Qingsi Wang7685e862018-06-11 20:15:46 -07001041 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1042
deadbeef1dcb1642017-03-29 21:08:16 -07001043 rtc::AsyncInvoker invoker_;
1044
Seth Hampson2f0d7022018-02-20 11:54:42 -08001045 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001046};
1047
Elad Alon99c3fe52017-10-13 16:29:40 +02001048class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1049 public:
1050 virtual ~MockRtcEventLogOutput() = default;
1051 MOCK_CONST_METHOD0(IsActive, bool());
1052 MOCK_METHOD1(Write, bool(const std::string&));
1053};
1054
Seth Hampson2f0d7022018-02-20 11:54:42 -08001055// This helper object is used for both specifying how many audio/video frames
1056// are expected to be received for a caller/callee. It provides helper functions
1057// to specify these expectations. The object initially starts in a state of no
1058// expectations.
1059class MediaExpectations {
1060 public:
1061 enum ExpectFrames {
1062 kExpectSomeFrames,
1063 kExpectNoFrames,
1064 kNoExpectation,
1065 };
1066
1067 void ExpectBidirectionalAudioAndVideo() {
1068 ExpectBidirectionalAudio();
1069 ExpectBidirectionalVideo();
1070 }
1071
1072 void ExpectBidirectionalAudio() {
1073 CallerExpectsSomeAudio();
1074 CalleeExpectsSomeAudio();
1075 }
1076
1077 void ExpectNoAudio() {
1078 CallerExpectsNoAudio();
1079 CalleeExpectsNoAudio();
1080 }
1081
1082 void ExpectBidirectionalVideo() {
1083 CallerExpectsSomeVideo();
1084 CalleeExpectsSomeVideo();
1085 }
1086
1087 void ExpectNoVideo() {
1088 CallerExpectsNoVideo();
1089 CalleeExpectsNoVideo();
1090 }
1091
1092 void CallerExpectsSomeAudioAndVideo() {
1093 CallerExpectsSomeAudio();
1094 CallerExpectsSomeVideo();
1095 }
1096
1097 void CalleeExpectsSomeAudioAndVideo() {
1098 CalleeExpectsSomeAudio();
1099 CalleeExpectsSomeVideo();
1100 }
1101
1102 // Caller's audio functions.
1103 void CallerExpectsSomeAudio(
1104 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1105 caller_audio_expectation_ = kExpectSomeFrames;
1106 caller_audio_frames_expected_ = expected_audio_frames;
1107 }
1108
1109 void CallerExpectsNoAudio() {
1110 caller_audio_expectation_ = kExpectNoFrames;
1111 caller_audio_frames_expected_ = 0;
1112 }
1113
1114 // Caller's video functions.
1115 void CallerExpectsSomeVideo(
1116 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1117 caller_video_expectation_ = kExpectSomeFrames;
1118 caller_video_frames_expected_ = expected_video_frames;
1119 }
1120
1121 void CallerExpectsNoVideo() {
1122 caller_video_expectation_ = kExpectNoFrames;
1123 caller_video_frames_expected_ = 0;
1124 }
1125
1126 // Callee's audio functions.
1127 void CalleeExpectsSomeAudio(
1128 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1129 callee_audio_expectation_ = kExpectSomeFrames;
1130 callee_audio_frames_expected_ = expected_audio_frames;
1131 }
1132
1133 void CalleeExpectsNoAudio() {
1134 callee_audio_expectation_ = kExpectNoFrames;
1135 callee_audio_frames_expected_ = 0;
1136 }
1137
1138 // Callee's video functions.
1139 void CalleeExpectsSomeVideo(
1140 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1141 callee_video_expectation_ = kExpectSomeFrames;
1142 callee_video_frames_expected_ = expected_video_frames;
1143 }
1144
1145 void CalleeExpectsNoVideo() {
1146 callee_video_expectation_ = kExpectNoFrames;
1147 callee_video_frames_expected_ = 0;
1148 }
1149
1150 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1151 ExpectFrames caller_video_expectation_ = kNoExpectation;
1152 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1153 ExpectFrames callee_video_expectation_ = kNoExpectation;
1154 int caller_audio_frames_expected_ = 0;
1155 int caller_video_frames_expected_ = 0;
1156 int callee_audio_frames_expected_ = 0;
1157 int callee_video_frames_expected_ = 0;
1158};
1159
deadbeef1dcb1642017-03-29 21:08:16 -07001160// Tests two PeerConnections connecting to each other end-to-end, using a
1161// virtual network, fake A/V capture and fake encoder/decoders. The
1162// PeerConnections share the threads/socket servers, but use separate versions
1163// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001164class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001165 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001166 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1167 : sdp_semantics_(sdp_semantics),
1168 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001169 fss_(new rtc::FirewallSocketServer(ss_.get())),
1170 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001171 worker_thread_(rtc::Thread::Create()),
1172 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001173 network_thread_->SetName("PCNetworkThread", this);
1174 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001175 RTC_CHECK(network_thread_->Start());
1176 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001177 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001178 }
1179
Seth Hampson2f0d7022018-02-20 11:54:42 -08001180 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001181 // The PeerConnections should deleted before the TurnCustomizers.
1182 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1183 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1184 // that the TurnCustomizer outlives the life of the PeerConnection or else
1185 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001186 if (caller_) {
1187 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001188 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001189 }
1190 if (callee_) {
1191 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001192 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001193 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001194
1195 // If turn servers were created for the test they need to be destroyed on
1196 // the network thread.
1197 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1198 turn_servers_.clear();
1199 turn_customizers_.clear();
1200 });
deadbeef1dcb1642017-03-29 21:08:16 -07001201 }
1202
1203 bool SignalingStateStable() {
1204 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1205 }
1206
deadbeef71452802017-05-07 17:21:01 -07001207 bool DtlsConnected() {
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01001208 return callee()->peer_connection_state() ==
1209 webrtc::PeerConnectionInterface::PeerConnectionState::
1210 kConnected &&
1211 caller()->peer_connection_state() ==
1212 webrtc::PeerConnectionInterface::PeerConnectionState::kConnected;
deadbeef71452802017-05-07 17:21:01 -07001213 }
1214
Qingsi Wang7685e862018-06-11 20:15:46 -07001215 // When |event_log_factory| is null, the default implementation of the event
1216 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001217 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1218 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001219 const PeerConnectionFactory::Options* options,
1220 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001221 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001222 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1223 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001224 RTCConfiguration modified_config;
1225 if (config) {
1226 modified_config = *config;
1227 }
Steve Anton3acffc32018-04-12 17:21:03 -07001228 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001229 if (!dependencies.cert_generator) {
1230 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001231 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001232 }
1233 std::unique_ptr<PeerConnectionWrapper> client(
1234 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001235
Niels Möllerf06f9232018-08-07 12:32:18 +02001236 if (!client->Init(options, &modified_config, std::move(dependencies),
1237 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001238 std::move(event_log_factory),
1239 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001240 return nullptr;
1241 }
1242 return client;
1243 }
1244
Qingsi Wang7685e862018-06-11 20:15:46 -07001245 std::unique_ptr<PeerConnectionWrapper>
1246 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1247 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001248 const PeerConnectionFactory::Options* options,
1249 const RTCConfiguration* config,
1250 webrtc::PeerConnectionDependencies dependencies) {
1251 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1252 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001253 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001254 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001255 std::move(event_log_factory),
1256 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001257 }
1258
deadbeef1dcb1642017-03-29 21:08:16 -07001259 bool CreatePeerConnectionWrappers() {
1260 return CreatePeerConnectionWrappersWithConfig(
1261 PeerConnectionInterface::RTCConfiguration(),
1262 PeerConnectionInterface::RTCConfiguration());
1263 }
1264
Steve Anton3acffc32018-04-12 17:21:03 -07001265 bool CreatePeerConnectionWrappersWithSdpSemantics(
1266 SdpSemantics caller_semantics,
1267 SdpSemantics callee_semantics) {
1268 // Can't specify the sdp_semantics in the passed-in configuration since it
1269 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1270 // stored in sdp_semantics_. So get around this by modifying the instance
1271 // variable before calling CreatePeerConnectionWrapper for the caller and
1272 // callee PeerConnections.
1273 SdpSemantics original_semantics = sdp_semantics_;
1274 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001275 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001276 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001277 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001278 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001280 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001281 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001282 sdp_semantics_ = original_semantics;
1283 return caller_ && callee_;
1284 }
1285
deadbeef1dcb1642017-03-29 21:08:16 -07001286 bool CreatePeerConnectionWrappersWithConfig(
1287 const PeerConnectionInterface::RTCConfiguration& caller_config,
1288 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001289 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001290 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001291 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1292 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001293 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001294 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001295 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1296 /*media_transport_factory=*/nullptr);
1297 return caller_ && callee_;
1298 }
1299
1300 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1301 const PeerConnectionInterface::RTCConfiguration& caller_config,
1302 const PeerConnectionInterface::RTCConfiguration& callee_config,
1303 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1304 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1305 caller_ =
1306 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1307 webrtc::PeerConnectionDependencies(nullptr),
1308 nullptr, std::move(caller_factory));
1309 callee_ =
1310 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1311 webrtc::PeerConnectionDependencies(nullptr),
1312 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001313 return caller_ && callee_;
1314 }
1315
1316 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1317 const PeerConnectionInterface::RTCConfiguration& caller_config,
1318 webrtc::PeerConnectionDependencies caller_dependencies,
1319 const PeerConnectionInterface::RTCConfiguration& callee_config,
1320 webrtc::PeerConnectionDependencies callee_dependencies) {
1321 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001322 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001323 std::move(caller_dependencies), nullptr,
1324 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001325 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001326 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001327 std::move(callee_dependencies), nullptr,
1328 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001329 return caller_ && callee_;
1330 }
1331
1332 bool CreatePeerConnectionWrappersWithOptions(
1333 const PeerConnectionFactory::Options& caller_options,
1334 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001335 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001336 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001337 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1338 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001339 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001340 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001341 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1342 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001343 return caller_ && callee_;
1344 }
1345
1346 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1347 PeerConnectionInterface::RTCConfiguration default_config;
1348 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001349 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001350 webrtc::PeerConnectionDependencies(nullptr));
1351 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001352 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001353 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001354 return caller_ && callee_;
1355 }
1356
Seth Hampson2f0d7022018-02-20 11:54:42 -08001357 std::unique_ptr<PeerConnectionWrapper>
1358 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001359 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1360 new FakeRTCCertificateGenerator());
1361 cert_generator->use_alternate_key();
1362
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001363 webrtc::PeerConnectionDependencies dependencies(nullptr);
1364 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001365 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001366 std::move(dependencies), nullptr,
1367 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001368 }
1369
Seth Hampsonaed71642018-06-11 07:41:32 -07001370 cricket::TestTurnServer* CreateTurnServer(
1371 rtc::SocketAddress internal_address,
1372 rtc::SocketAddress external_address,
1373 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1374 const std::string& common_name = "test turn server") {
1375 rtc::Thread* thread = network_thread();
1376 std::unique_ptr<cricket::TestTurnServer> turn_server =
1377 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1378 RTC_FROM_HERE,
1379 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001380 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001381 thread, internal_address, external_address, type,
1382 /*ignore_bad_certs=*/true, common_name);
1383 });
1384 turn_servers_.push_back(std::move(turn_server));
1385 // Interactions with the turn server should be done on the network thread.
1386 return turn_servers_.back().get();
1387 }
1388
1389 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1390 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1391 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1392 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001393 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001394 turn_customizers_.push_back(std::move(turn_customizer));
1395 // Interactions with the turn customizer should be done on the network
1396 // thread.
1397 return turn_customizers_.back().get();
1398 }
1399
1400 // Checks that the function counters for a TestTurnCustomizer are greater than
1401 // 0.
1402 void ExpectTurnCustomizerCountersIncremented(
1403 cricket::TestTurnCustomizer* turn_customizer) {
1404 unsigned int allow_channel_data_counter =
1405 network_thread()->Invoke<unsigned int>(
1406 RTC_FROM_HERE, [turn_customizer] {
1407 return turn_customizer->allow_channel_data_cnt_;
1408 });
1409 EXPECT_GT(allow_channel_data_counter, 0u);
1410 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1411 RTC_FROM_HERE,
1412 [turn_customizer] { return turn_customizer->modify_cnt_; });
1413 EXPECT_GT(modify_counter, 0u);
1414 }
1415
deadbeef1dcb1642017-03-29 21:08:16 -07001416 // Once called, SDP blobs and ICE candidates will be automatically signaled
1417 // between PeerConnections.
1418 void ConnectFakeSignaling() {
1419 caller_->set_signaling_message_receiver(callee_.get());
1420 callee_->set_signaling_message_receiver(caller_.get());
1421 }
1422
Steve Antonede9ca52017-10-16 13:04:27 -07001423 // Once called, SDP blobs will be automatically signaled between
1424 // PeerConnections. Note that ICE candidates will not be signaled unless they
1425 // are in the exchanged SDP blobs.
1426 void ConnectFakeSignalingForSdpOnly() {
1427 ConnectFakeSignaling();
1428 SetSignalIceCandidates(false);
1429 }
1430
deadbeef1dcb1642017-03-29 21:08:16 -07001431 void SetSignalingDelayMs(int delay_ms) {
1432 caller_->set_signaling_delay_ms(delay_ms);
1433 callee_->set_signaling_delay_ms(delay_ms);
1434 }
1435
Steve Antonede9ca52017-10-16 13:04:27 -07001436 void SetSignalIceCandidates(bool signal) {
1437 caller_->set_signal_ice_candidates(signal);
1438 callee_->set_signal_ice_candidates(signal);
1439 }
1440
deadbeef1dcb1642017-03-29 21:08:16 -07001441 // Messages may get lost on the unreliable DataChannel, so we send multiple
1442 // times to avoid test flakiness.
1443 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1444 const std::string& data,
1445 int retries) {
1446 for (int i = 0; i < retries; ++i) {
1447 dc->Send(DataBuffer(data));
1448 }
1449 }
1450
1451 rtc::Thread* network_thread() { return network_thread_.get(); }
1452
1453 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1454
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001455 webrtc::MediaTransportPair* loopback_media_transports() {
1456 return &loopback_media_transports_;
1457 }
1458
deadbeef1dcb1642017-03-29 21:08:16 -07001459 PeerConnectionWrapper* caller() { return caller_.get(); }
1460
1461 // Set the |caller_| to the |wrapper| passed in and return the
1462 // original |caller_|.
1463 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1464 PeerConnectionWrapper* wrapper) {
1465 PeerConnectionWrapper* old = caller_.release();
1466 caller_.reset(wrapper);
1467 return old;
1468 }
1469
1470 PeerConnectionWrapper* callee() { return callee_.get(); }
1471
1472 // Set the |callee_| to the |wrapper| passed in and return the
1473 // original |callee_|.
1474 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1475 PeerConnectionWrapper* wrapper) {
1476 PeerConnectionWrapper* old = callee_.release();
1477 callee_.reset(wrapper);
1478 return old;
1479 }
1480
Steve Antonede9ca52017-10-16 13:04:27 -07001481 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1482
Seth Hampson2f0d7022018-02-20 11:54:42 -08001483 // Expects the provided number of new frames to be received within
1484 // kMaxWaitForFramesMs. The new expected frames are specified in
1485 // |media_expectations|. Returns false if any of the expectations were
1486 // not met.
1487 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1488 // First initialize the expected frame counts based upon the current
1489 // frame count.
1490 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1491 if (media_expectations.caller_audio_expectation_ ==
1492 MediaExpectations::kExpectSomeFrames) {
1493 total_caller_audio_frames_expected +=
1494 media_expectations.caller_audio_frames_expected_;
1495 }
1496 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001497 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001498 if (media_expectations.caller_video_expectation_ ==
1499 MediaExpectations::kExpectSomeFrames) {
1500 total_caller_video_frames_expected +=
1501 media_expectations.caller_video_frames_expected_;
1502 }
1503 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1504 if (media_expectations.callee_audio_expectation_ ==
1505 MediaExpectations::kExpectSomeFrames) {
1506 total_callee_audio_frames_expected +=
1507 media_expectations.callee_audio_frames_expected_;
1508 }
1509 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001510 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001511 if (media_expectations.callee_video_expectation_ ==
1512 MediaExpectations::kExpectSomeFrames) {
1513 total_callee_video_frames_expected +=
1514 media_expectations.callee_video_frames_expected_;
1515 }
deadbeef1dcb1642017-03-29 21:08:16 -07001516
Seth Hampson2f0d7022018-02-20 11:54:42 -08001517 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001518 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001519 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001520 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001521 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001522 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001523 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001524 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001525 total_callee_video_frames_expected,
1526 kMaxWaitForFramesMs);
1527 bool expectations_correct =
1528 caller()->audio_frames_received() >=
1529 total_caller_audio_frames_expected &&
1530 caller()->min_video_frames_received_per_track() >=
1531 total_caller_video_frames_expected &&
1532 callee()->audio_frames_received() >=
1533 total_callee_audio_frames_expected &&
1534 callee()->min_video_frames_received_per_track() >=
1535 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001536
Seth Hampson2f0d7022018-02-20 11:54:42 -08001537 // After the combined wait, print out a more detailed message upon
1538 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001539 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001540 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001541 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001542 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001543 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001544 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001545 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001546 total_callee_video_frames_expected);
1547
1548 // We want to make sure nothing unexpected was received.
1549 if (media_expectations.caller_audio_expectation_ ==
1550 MediaExpectations::kExpectNoFrames) {
1551 EXPECT_EQ(caller()->audio_frames_received(),
1552 total_caller_audio_frames_expected);
1553 if (caller()->audio_frames_received() !=
1554 total_caller_audio_frames_expected) {
1555 expectations_correct = false;
1556 }
1557 }
1558 if (media_expectations.caller_video_expectation_ ==
1559 MediaExpectations::kExpectNoFrames) {
1560 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1561 total_caller_video_frames_expected);
1562 if (caller()->min_video_frames_received_per_track() !=
1563 total_caller_video_frames_expected) {
1564 expectations_correct = false;
1565 }
1566 }
1567 if (media_expectations.callee_audio_expectation_ ==
1568 MediaExpectations::kExpectNoFrames) {
1569 EXPECT_EQ(callee()->audio_frames_received(),
1570 total_callee_audio_frames_expected);
1571 if (callee()->audio_frames_received() !=
1572 total_callee_audio_frames_expected) {
1573 expectations_correct = false;
1574 }
1575 }
1576 if (media_expectations.callee_video_expectation_ ==
1577 MediaExpectations::kExpectNoFrames) {
1578 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1579 total_callee_video_frames_expected);
1580 if (callee()->min_video_frames_received_per_track() !=
1581 total_callee_video_frames_expected) {
1582 expectations_correct = false;
1583 }
1584 }
1585 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001586 }
1587
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001588 void TestNegotiatedCipherSuite(
1589 const PeerConnectionFactory::Options& caller_options,
1590 const PeerConnectionFactory::Options& callee_options,
1591 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001592 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1593 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001594 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001595 caller()->AddAudioVideoTracks();
1596 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001597 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001598 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001599 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001600 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001601 // TODO(bugs.webrtc.org/9456): Fix it.
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01001602 EXPECT_LE(1, webrtc::metrics::NumEvents(
1603 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1604 expected_cipher_suite));
1605 EXPECT_GE(2, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001606 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1607 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001608 }
1609
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001610 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1611 bool remote_gcm_enabled,
1612 int expected_cipher_suite) {
1613 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001614 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1615 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001616 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001617 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1618 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001619 TestNegotiatedCipherSuite(caller_options, callee_options,
1620 expected_cipher_suite);
1621 }
1622
Seth Hampson2f0d7022018-02-20 11:54:42 -08001623 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001624 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001625
deadbeef1dcb1642017-03-29 21:08:16 -07001626 private:
1627 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001628 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001629 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001630 // |network_thread_| and |worker_thread_| are used by both
1631 // |caller_| and |callee_| so they must be destroyed
1632 // later.
1633 std::unique_ptr<rtc::Thread> network_thread_;
1634 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001635 // The turn servers and turn customizers should be accessed & deleted on the
1636 // network thread to avoid a race with the socket read/write that occurs
1637 // on the network thread.
1638 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1639 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001640 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001641 std::unique_ptr<PeerConnectionWrapper> caller_;
1642 std::unique_ptr<PeerConnectionWrapper> callee_;
1643};
1644
Seth Hampson2f0d7022018-02-20 11:54:42 -08001645class PeerConnectionIntegrationTest
1646 : public PeerConnectionIntegrationBaseTest,
1647 public ::testing::WithParamInterface<SdpSemantics> {
1648 protected:
1649 PeerConnectionIntegrationTest()
1650 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1651};
1652
1653class PeerConnectionIntegrationTestPlanB
1654 : public PeerConnectionIntegrationBaseTest {
1655 protected:
1656 PeerConnectionIntegrationTestPlanB()
1657 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1658};
1659
1660class PeerConnectionIntegrationTestUnifiedPlan
1661 : public PeerConnectionIntegrationBaseTest {
1662 protected:
1663 PeerConnectionIntegrationTestUnifiedPlan()
1664 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1665};
1666
deadbeef1dcb1642017-03-29 21:08:16 -07001667// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1668// includes testing that the callback is invoked if an observer is connected
1669// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001670TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001671 RtpReceiverObserverOnFirstPacketReceived) {
1672 ASSERT_TRUE(CreatePeerConnectionWrappers());
1673 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001674 caller()->AddAudioVideoTracks();
1675 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001676 // Start offer/answer exchange and wait for it to complete.
1677 caller()->CreateAndSetAndSignalOffer();
1678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1679 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001680 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1681 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001682 // Wait for all "first packet received" callbacks to be fired.
1683 EXPECT_TRUE_WAIT(
1684 std::all_of(caller()->rtp_receiver_observers().begin(),
1685 caller()->rtp_receiver_observers().end(),
1686 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1687 return o->first_packet_received();
1688 }),
1689 kMaxWaitForFramesMs);
1690 EXPECT_TRUE_WAIT(
1691 std::all_of(callee()->rtp_receiver_observers().begin(),
1692 callee()->rtp_receiver_observers().end(),
1693 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1694 return o->first_packet_received();
1695 }),
1696 kMaxWaitForFramesMs);
1697 // If new observers are set after the first packet was already received, the
1698 // callback should still be invoked.
1699 caller()->ResetRtpReceiverObservers();
1700 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001701 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1702 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001703 EXPECT_TRUE(
1704 std::all_of(caller()->rtp_receiver_observers().begin(),
1705 caller()->rtp_receiver_observers().end(),
1706 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1707 return o->first_packet_received();
1708 }));
1709 EXPECT_TRUE(
1710 std::all_of(callee()->rtp_receiver_observers().begin(),
1711 callee()->rtp_receiver_observers().end(),
1712 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1713 return o->first_packet_received();
1714 }));
1715}
1716
1717class DummyDtmfObserver : public DtmfSenderObserverInterface {
1718 public:
1719 DummyDtmfObserver() : completed_(false) {}
1720
1721 // Implements DtmfSenderObserverInterface.
1722 void OnToneChange(const std::string& tone) override {
1723 tones_.push_back(tone);
1724 if (tone.empty()) {
1725 completed_ = true;
1726 }
1727 }
1728
1729 const std::vector<std::string>& tones() const { return tones_; }
1730 bool completed() const { return completed_; }
1731
1732 private:
1733 bool completed_;
1734 std::vector<std::string> tones_;
1735};
1736
1737// Assumes |sender| already has an audio track added and the offer/answer
1738// exchange is done.
1739void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1740 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001741 // We should be able to get a DTMF sender from the local sender.
1742 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1743 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1744 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001745 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001746 dtmf_sender->RegisterObserver(&observer);
1747
1748 // Test the DtmfSender object just created.
1749 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1750 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1751
1752 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1753 std::vector<std::string> tones = {"1", "a", ""};
1754 EXPECT_EQ(tones, observer.tones());
1755 dtmf_sender->UnregisterObserver();
1756 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1757}
1758
1759// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1760// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001761TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001762 ASSERT_TRUE(CreatePeerConnectionWrappers());
1763 ConnectFakeSignaling();
1764 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001765 caller()->AddAudioTrack();
1766 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001767 caller()->CreateAndSetAndSignalOffer();
1768 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001769 // DTLS must finish before the DTMF sender can be used reliably.
1770 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001771 TestDtmfFromSenderToReceiver(caller(), callee());
1772 TestDtmfFromSenderToReceiver(callee(), caller());
1773}
1774
1775// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1776// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001777TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001778 ASSERT_TRUE(CreatePeerConnectionWrappers());
1779 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001780
deadbeef1dcb1642017-03-29 21:08:16 -07001781 // Do normal offer/answer and wait for some frames to be received in each
1782 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001783 caller()->AddAudioVideoTracks();
1784 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001785 caller()->CreateAndSetAndSignalOffer();
1786 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001787 MediaExpectations media_expectations;
1788 media_expectations.ExpectBidirectionalAudioAndVideo();
1789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001790 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1791 webrtc::kEnumCounterKeyProtocolDtls));
1792 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1793 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001794}
1795
1796// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001797TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001798 PeerConnectionInterface::RTCConfiguration sdes_config;
1799 sdes_config.enable_dtls_srtp.emplace(false);
1800 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1801 ConnectFakeSignaling();
1802
1803 // Do normal offer/answer and wait for some frames to be received in each
1804 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001805 caller()->AddAudioVideoTracks();
1806 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001807 caller()->CreateAndSetAndSignalOffer();
1808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001809 MediaExpectations media_expectations;
1810 media_expectations.ExpectBidirectionalAudioAndVideo();
1811 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001812 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1813 webrtc::kEnumCounterKeyProtocolSdes));
1814 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1815 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001816}
1817
Steve Anton8c0f7a72017-10-03 10:03:10 -07001818// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1819// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001820TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001821 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1822 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1823 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1824 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1825 return pc->GetRemoteAudioSSLCertificate();
1826 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001827 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1828 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1829 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1830 return pc->GetRemoteAudioSSLCertChain();
1831 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001832
1833 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1834 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1835
1836 // Configure each side with a known certificate so they can be compared later.
1837 PeerConnectionInterface::RTCConfiguration caller_config;
1838 caller_config.enable_dtls_srtp.emplace(true);
1839 caller_config.certificates.push_back(caller_cert);
1840 PeerConnectionInterface::RTCConfiguration callee_config;
1841 callee_config.enable_dtls_srtp.emplace(true);
1842 callee_config.certificates.push_back(callee_cert);
1843 ASSERT_TRUE(
1844 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1845 ConnectFakeSignaling();
1846
1847 // When first initialized, there should not be a remote SSL certificate (and
1848 // calling this method should not crash).
1849 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1850 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001851 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1852 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001853
Steve Anton15324772018-01-16 10:26:49 -08001854 caller()->AddAudioTrack();
1855 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001856 caller()->CreateAndSetAndSignalOffer();
1857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1858 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1859
1860 // Once DTLS has been connected, each side should return the other's SSL
1861 // certificate when calling GetRemoteAudioSSLCertificate.
1862
1863 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1864 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001865 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001866 caller_remote_cert->ToPEMString());
1867
1868 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1869 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001870 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001871 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001872
1873 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1874 ASSERT_TRUE(caller_remote_cert_chain);
1875 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1876 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001877 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001878 remote_cert->ToPEMString());
1879
1880 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1881 ASSERT_TRUE(callee_remote_cert_chain);
1882 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1883 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001884 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001885 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001886}
1887
deadbeef1dcb1642017-03-29 21:08:16 -07001888// This test sets up a call between two parties with a source resolution of
1889// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001890TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001891 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1892 ASSERT_TRUE(CreatePeerConnectionWrappers());
1893 ConnectFakeSignaling();
1894
Niels Möller5c7efe72018-05-11 10:34:46 +02001895 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1896 webrtc::FakePeriodicVideoSource::Config config;
1897 config.width = 1280;
1898 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001899 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001900 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1901 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001902
1903 // Do normal offer/answer and wait for at least one frame to be received in
1904 // each direction.
1905 caller()->CreateAndSetAndSignalOffer();
1906 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1907 callee()->min_video_frames_received_per_track() > 0,
1908 kMaxWaitForFramesMs);
1909
1910 // Check rendered aspect ratio.
1911 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1912 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1913 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1914 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1915}
1916
1917// This test sets up an one-way call, with media only from caller to
1918// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001919TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001920 ASSERT_TRUE(CreatePeerConnectionWrappers());
1921 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001922 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001923 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001924 MediaExpectations media_expectations;
1925 media_expectations.CalleeExpectsSomeAudioAndVideo();
1926 media_expectations.CallerExpectsNoAudio();
1927 media_expectations.CallerExpectsNoVideo();
1928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001929}
1930
1931// This test sets up a audio call initially, with the callee rejecting video
1932// initially. Then later the callee decides to upgrade to audio/video, and
1933// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001934TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001935 ASSERT_TRUE(CreatePeerConnectionWrappers());
1936 ConnectFakeSignaling();
1937 // Initially, offer an audio/video stream from the caller, but refuse to
1938 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001939 caller()->AddAudioVideoTracks();
1940 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001941 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1942 PeerConnectionInterface::RTCOfferAnswerOptions options;
1943 options.offer_to_receive_video = 0;
1944 callee()->SetOfferAnswerOptions(options);
1945 } else {
1946 callee()->SetRemoteOfferHandler([this] {
1947 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1948 });
1949 }
deadbeef1dcb1642017-03-29 21:08:16 -07001950 // Do offer/answer and make sure audio is still received end-to-end.
1951 caller()->CreateAndSetAndSignalOffer();
1952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001953 {
1954 MediaExpectations media_expectations;
1955 media_expectations.ExpectBidirectionalAudio();
1956 media_expectations.ExpectNoVideo();
1957 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1958 }
deadbeef1dcb1642017-03-29 21:08:16 -07001959 // Sanity check that the callee's description has a rejected video section.
1960 ASSERT_NE(nullptr, callee()->pc()->local_description());
1961 const ContentInfo* callee_video_content =
1962 GetFirstVideoContent(callee()->pc()->local_description()->description());
1963 ASSERT_NE(nullptr, callee_video_content);
1964 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001965
deadbeef1dcb1642017-03-29 21:08:16 -07001966 // Now negotiate with video and ensure negotiation succeeds, with video
1967 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001968 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001969 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1970 PeerConnectionInterface::RTCOfferAnswerOptions options;
1971 options.offer_to_receive_video = 1;
1972 callee()->SetOfferAnswerOptions(options);
1973 } else {
1974 callee()->SetRemoteOfferHandler(nullptr);
1975 caller()->SetRemoteOfferHandler([this] {
1976 // The caller creates a new transceiver to receive video on when receiving
1977 // the offer, but by default it is send only.
1978 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001979 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001980 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1981 transceivers[2]->receiver()->media_type());
1982 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1983 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1984 });
1985 }
deadbeef1dcb1642017-03-29 21:08:16 -07001986 callee()->CreateAndSetAndSignalOffer();
1987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988 {
1989 // Expect additional audio frames to be received after the upgrade.
1990 MediaExpectations media_expectations;
1991 media_expectations.ExpectBidirectionalAudioAndVideo();
1992 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1993 }
deadbeef1dcb1642017-03-29 21:08:16 -07001994}
1995
deadbeef4389b4d2017-09-07 09:07:36 -07001996// Simpler than the above test; just add an audio track to an established
1997// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001999 ASSERT_TRUE(CreatePeerConnectionWrappers());
2000 ConnectFakeSignaling();
2001 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002002 caller()->AddVideoTrack();
2003 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002004 caller()->CreateAndSetAndSignalOffer();
2005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2006 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioTrack();
2008 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002012 MediaExpectations media_expectations;
2013 media_expectations.ExpectBidirectionalAudioAndVideo();
2014 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002015}
2016
deadbeef1dcb1642017-03-29 21:08:16 -07002017// This test sets up a call that's transferred to a new caller with a different
2018// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002019TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002020 ASSERT_TRUE(CreatePeerConnectionWrappers());
2021 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002022 caller()->AddAudioVideoTracks();
2023 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002024 caller()->CreateAndSetAndSignalOffer();
2025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2026
2027 // Keep the original peer around which will still send packets to the
2028 // receiving client. These SRTP packets will be dropped.
2029 std::unique_ptr<PeerConnectionWrapper> original_peer(
2030 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002031 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002032 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2033 // directly above.
2034 original_peer->pc()->Close();
2035
2036 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002037 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002038 caller()->CreateAndSetAndSignalOffer();
2039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2040 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041 MediaExpectations media_expectations;
2042 media_expectations.ExpectBidirectionalAudioAndVideo();
2043 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002044}
2045
2046// This test sets up a call that's transferred to a new callee with a different
2047// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002049 ASSERT_TRUE(CreatePeerConnectionWrappers());
2050 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002051 caller()->AddAudioVideoTracks();
2052 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002053 caller()->CreateAndSetAndSignalOffer();
2054 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2055
2056 // Keep the original peer around which will still send packets to the
2057 // receiving client. These SRTP packets will be dropped.
2058 std::unique_ptr<PeerConnectionWrapper> original_peer(
2059 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002060 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002061 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2062 // directly above.
2063 original_peer->pc()->Close();
2064
2065 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002066 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002067 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2068 caller()->CreateAndSetAndSignalOffer();
2069 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2070 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002071 MediaExpectations media_expectations;
2072 media_expectations.ExpectBidirectionalAudioAndVideo();
2073 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002074}
2075
2076// This test sets up a non-bundled call and negotiates bundling at the same
2077// time as starting an ICE restart. When bundling is in effect in the restart,
2078// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002080 ASSERT_TRUE(CreatePeerConnectionWrappers());
2081 ConnectFakeSignaling();
2082
Steve Anton15324772018-01-16 10:26:49 -08002083 caller()->AddAudioVideoTracks();
2084 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002085 // Remove the bundle group from the SDP received by the callee.
2086 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2087 desc->RemoveGroupByName("BUNDLE");
2088 });
2089 caller()->CreateAndSetAndSignalOffer();
2090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002091 {
2092 MediaExpectations media_expectations;
2093 media_expectations.ExpectBidirectionalAudioAndVideo();
2094 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2095 }
deadbeef1dcb1642017-03-29 21:08:16 -07002096 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2097 callee()->SetReceivedSdpMunger(nullptr);
2098 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2099 caller()->CreateAndSetAndSignalOffer();
2100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2101
2102 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002103 {
2104 MediaExpectations media_expectations;
2105 media_expectations.ExpectBidirectionalAudioAndVideo();
2106 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2107 }
deadbeef1dcb1642017-03-29 21:08:16 -07002108}
2109
2110// Test CVO (Coordination of Video Orientation). If a video source is rotated
2111// and both peers support the CVO RTP header extension, the actual video frames
2112// don't need to be encoded in different resolutions, since the rotation is
2113// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002114TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002115 ASSERT_TRUE(CreatePeerConnectionWrappers());
2116 ConnectFakeSignaling();
2117 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002118 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002119 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002120 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002121 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2122
2123 // Wait for video frames to be received by both sides.
2124 caller()->CreateAndSetAndSignalOffer();
2125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2126 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2127 callee()->min_video_frames_received_per_track() > 0,
2128 kMaxWaitForFramesMs);
2129
2130 // Ensure that the aspect ratio is unmodified.
2131 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2132 // not just assumed.
2133 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2134 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2135 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2136 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2137 // Ensure that the CVO bits were surfaced to the renderer.
2138 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2139 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2140}
2141
2142// Test that when the CVO extension isn't supported, video is rotated the
2143// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002144TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002145 ASSERT_TRUE(CreatePeerConnectionWrappers());
2146 ConnectFakeSignaling();
2147 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002148 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002149 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002150 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002151 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2152
2153 // Remove the CVO extension from the offered SDP.
2154 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2155 cricket::VideoContentDescription* video =
2156 GetFirstVideoContentDescription(desc);
2157 video->ClearRtpHeaderExtensions();
2158 });
2159 // Wait for video frames to be received by both sides.
2160 caller()->CreateAndSetAndSignalOffer();
2161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2162 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2163 callee()->min_video_frames_received_per_track() > 0,
2164 kMaxWaitForFramesMs);
2165
2166 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2167 // rotation.
2168 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2169 // not just assumed.
2170 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2171 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2172 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2173 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2174 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2175 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2176 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2177}
2178
deadbeef1dcb1642017-03-29 21:08:16 -07002179// Test that if the answerer rejects the audio m= section, no audio is sent or
2180// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002181TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002182 ASSERT_TRUE(CreatePeerConnectionWrappers());
2183 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002184 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002185 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2186 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2187 // it will reject the audio m= section completely.
2188 PeerConnectionInterface::RTCOfferAnswerOptions options;
2189 options.offer_to_receive_audio = 0;
2190 callee()->SetOfferAnswerOptions(options);
2191 } else {
2192 // Stopping the audio RtpTransceiver will cause the media section to be
2193 // rejected in the answer.
2194 callee()->SetRemoteOfferHandler([this] {
2195 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2196 });
2197 }
Steve Anton15324772018-01-16 10:26:49 -08002198 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002199 // Do offer/answer and wait for successful end-to-end video frames.
2200 caller()->CreateAndSetAndSignalOffer();
2201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002202 MediaExpectations media_expectations;
2203 media_expectations.ExpectBidirectionalVideo();
2204 media_expectations.ExpectNoAudio();
2205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2206
deadbeef1dcb1642017-03-29 21:08:16 -07002207 // Sanity check that the callee's description has a rejected audio section.
2208 ASSERT_NE(nullptr, callee()->pc()->local_description());
2209 const ContentInfo* callee_audio_content =
2210 GetFirstAudioContent(callee()->pc()->local_description()->description());
2211 ASSERT_NE(nullptr, callee_audio_content);
2212 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002213 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2214 // The caller's transceiver should have stopped after receiving the answer.
2215 EXPECT_TRUE(caller()
2216 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2217 ->stopped());
2218 }
deadbeef1dcb1642017-03-29 21:08:16 -07002219}
2220
2221// Test that if the answerer rejects the video m= section, no video is sent or
2222// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002223TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002224 ASSERT_TRUE(CreatePeerConnectionWrappers());
2225 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002226 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002227 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2228 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2229 // it will reject the video m= section completely.
2230 PeerConnectionInterface::RTCOfferAnswerOptions options;
2231 options.offer_to_receive_video = 0;
2232 callee()->SetOfferAnswerOptions(options);
2233 } else {
2234 // Stopping the video RtpTransceiver will cause the media section to be
2235 // rejected in the answer.
2236 callee()->SetRemoteOfferHandler([this] {
2237 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2238 });
2239 }
Steve Anton15324772018-01-16 10:26:49 -08002240 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002241 // Do offer/answer and wait for successful end-to-end audio frames.
2242 caller()->CreateAndSetAndSignalOffer();
2243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002244 MediaExpectations media_expectations;
2245 media_expectations.ExpectBidirectionalAudio();
2246 media_expectations.ExpectNoVideo();
2247 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2248
deadbeef1dcb1642017-03-29 21:08:16 -07002249 // Sanity check that the callee's description has a rejected video section.
2250 ASSERT_NE(nullptr, callee()->pc()->local_description());
2251 const ContentInfo* callee_video_content =
2252 GetFirstVideoContent(callee()->pc()->local_description()->description());
2253 ASSERT_NE(nullptr, callee_video_content);
2254 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002255 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2256 // The caller's transceiver should have stopped after receiving the answer.
2257 EXPECT_TRUE(caller()
2258 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2259 ->stopped());
2260 }
deadbeef1dcb1642017-03-29 21:08:16 -07002261}
2262
2263// Test that if the answerer rejects both audio and video m= sections, nothing
2264// bad happens.
2265// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2266// test anything but the fact that negotiation succeeds, which doesn't mean
2267// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002268TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002269 ASSERT_TRUE(CreatePeerConnectionWrappers());
2270 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002271 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002272 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2273 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2274 // will reject both audio and video m= sections.
2275 PeerConnectionInterface::RTCOfferAnswerOptions options;
2276 options.offer_to_receive_audio = 0;
2277 options.offer_to_receive_video = 0;
2278 callee()->SetOfferAnswerOptions(options);
2279 } else {
2280 callee()->SetRemoteOfferHandler([this] {
2281 // Stopping all transceivers will cause all media sections to be rejected.
2282 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2283 transceiver->Stop();
2284 }
2285 });
2286 }
deadbeef1dcb1642017-03-29 21:08:16 -07002287 // Do offer/answer and wait for stable signaling state.
2288 caller()->CreateAndSetAndSignalOffer();
2289 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002290
deadbeef1dcb1642017-03-29 21:08:16 -07002291 // Sanity check that the callee's description has rejected m= sections.
2292 ASSERT_NE(nullptr, callee()->pc()->local_description());
2293 const ContentInfo* callee_audio_content =
2294 GetFirstAudioContent(callee()->pc()->local_description()->description());
2295 ASSERT_NE(nullptr, callee_audio_content);
2296 EXPECT_TRUE(callee_audio_content->rejected);
2297 const ContentInfo* callee_video_content =
2298 GetFirstVideoContent(callee()->pc()->local_description()->description());
2299 ASSERT_NE(nullptr, callee_video_content);
2300 EXPECT_TRUE(callee_video_content->rejected);
2301}
2302
2303// This test sets up an audio and video call between two parties. After the
2304// call runs for a while, the caller sends an updated offer with video being
2305// rejected. Once the re-negotiation is done, the video flow should stop and
2306// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002308 ASSERT_TRUE(CreatePeerConnectionWrappers());
2309 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002310 caller()->AddAudioVideoTracks();
2311 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002312 caller()->CreateAndSetAndSignalOffer();
2313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002314 {
2315 MediaExpectations media_expectations;
2316 media_expectations.ExpectBidirectionalAudioAndVideo();
2317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2318 }
deadbeef1dcb1642017-03-29 21:08:16 -07002319 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2321 caller()->SetGeneratedSdpMunger(
2322 [](cricket::SessionDescription* description) {
2323 for (cricket::ContentInfo& content : description->contents()) {
2324 if (cricket::IsVideoContent(&content)) {
2325 content.rejected = true;
2326 }
2327 }
2328 });
2329 } else {
2330 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2331 }
deadbeef1dcb1642017-03-29 21:08:16 -07002332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2334
2335 // Sanity check that the caller's description has a rejected video section.
2336 ASSERT_NE(nullptr, caller()->pc()->local_description());
2337 const ContentInfo* caller_video_content =
2338 GetFirstVideoContent(caller()->pc()->local_description()->description());
2339 ASSERT_NE(nullptr, caller_video_content);
2340 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002341 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002342 {
2343 MediaExpectations media_expectations;
2344 media_expectations.ExpectBidirectionalAudio();
2345 media_expectations.ExpectNoVideo();
2346 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2347 }
deadbeef1dcb1642017-03-29 21:08:16 -07002348}
2349
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002350// Do one offer/answer with audio, another that disables it (rejecting the m=
2351// section), and another that re-enables it. Regression test for:
2352// bugs.webrtc.org/6023
2353TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2354 ASSERT_TRUE(CreatePeerConnectionWrappers());
2355 ConnectFakeSignaling();
2356
2357 // Add audio track, do normal offer/answer.
2358 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2359 caller()->CreateLocalAudioTrack();
2360 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2361 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2364
2365 // Remove audio track, and set offer_to_receive_audio to false to cause the
2366 // m= section to be completely disabled, not just "recvonly".
2367 caller()->pc()->RemoveTrack(sender);
2368 PeerConnectionInterface::RTCOfferAnswerOptions options;
2369 options.offer_to_receive_audio = 0;
2370 caller()->SetOfferAnswerOptions(options);
2371 caller()->CreateAndSetAndSignalOffer();
2372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2373
2374 // Add the audio track again, expecting negotiation to succeed and frames to
2375 // flow.
2376 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2377 options.offer_to_receive_audio = 1;
2378 caller()->SetOfferAnswerOptions(options);
2379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2381
2382 MediaExpectations media_expectations;
2383 media_expectations.CalleeExpectsSomeAudio();
2384 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2385}
2386
deadbeef1dcb1642017-03-29 21:08:16 -07002387// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2388// is needed to support legacy endpoints.
2389// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2390// add a test for an end-to-end test without MID signaling either (basically,
2391// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002392TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002393 ASSERT_TRUE(CreatePeerConnectionWrappers());
2394 ConnectFakeSignaling();
2395 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002396 caller()->AddAudioVideoTracks();
2397 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002398 // Remove SSRCs and MSIDs from the received offer SDP.
2399 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002400 caller()->CreateAndSetAndSignalOffer();
2401 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002402 MediaExpectations media_expectations;
2403 media_expectations.ExpectBidirectionalAudioAndVideo();
2404 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002405}
2406
Seth Hampson5897a6e2018-04-03 11:16:33 -07002407// Basic end-to-end test, without SSRC signaling. This means that the track
2408// was created properly and frames are delivered when the MSIDs are communicated
2409// with a=msid lines and no a=ssrc lines.
2410TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2411 EndToEndCallWithoutSsrcSignaling) {
2412 const char kStreamId[] = "streamId";
2413 ASSERT_TRUE(CreatePeerConnectionWrappers());
2414 ConnectFakeSignaling();
2415 // Add just audio tracks.
2416 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2417 callee()->AddAudioTrack();
2418
2419 // Remove SSRCs from the received offer SDP.
2420 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2421 caller()->CreateAndSetAndSignalOffer();
2422 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2423 MediaExpectations media_expectations;
2424 media_expectations.ExpectBidirectionalAudio();
2425 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2426}
2427
Steve Antondf527fd2018-04-27 15:52:03 -07002428// Tests that video flows between multiple video tracks when SSRCs are not
2429// signaled. This exercises the MID RTP header extension which is needed to
2430// demux the incoming video tracks.
2431TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2432 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2433 ASSERT_TRUE(CreatePeerConnectionWrappers());
2434 ConnectFakeSignaling();
2435 caller()->AddVideoTrack();
2436 caller()->AddVideoTrack();
2437 callee()->AddVideoTrack();
2438 callee()->AddVideoTrack();
2439
2440 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2441 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2442 caller()->CreateAndSetAndSignalOffer();
2443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2444 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2445 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2446
2447 // Expect video to be received in both directions on both tracks.
2448 MediaExpectations media_expectations;
2449 media_expectations.ExpectBidirectionalVideo();
2450 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2451}
2452
deadbeef1dcb1642017-03-29 21:08:16 -07002453// Test that if two video tracks are sent (from caller to callee, in this test),
2454// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002456 ASSERT_TRUE(CreatePeerConnectionWrappers());
2457 ConnectFakeSignaling();
2458 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002459 caller()->AddAudioVideoTracks();
2460 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002461 caller()->CreateAndSetAndSignalOffer();
2462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002463 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002464
2465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudioAndVideo();
2467 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002468}
2469
2470static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2471 bool first = true;
2472 for (cricket::ContentInfo& content : desc->contents()) {
2473 if (first) {
2474 first = false;
2475 continue;
2476 }
2477 content.bundle_only = true;
2478 }
2479 first = true;
2480 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2481 if (first) {
2482 first = false;
2483 continue;
2484 }
2485 transport.description.ice_ufrag.clear();
2486 transport.description.ice_pwd.clear();
2487 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2488 transport.description.identity_fingerprint.reset(nullptr);
2489 }
2490}
2491
2492// Test that if applying a true "max bundle" offer, which uses ports of 0,
2493// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2494// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2495// successfully and media flows.
2496// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2497// TODO(deadbeef): Won't need this test once we start generating actual
2498// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002499TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002500 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2501 ASSERT_TRUE(CreatePeerConnectionWrappers());
2502 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002503 caller()->AddAudioVideoTracks();
2504 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002505 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2506 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2507 // but the first m= section.
2508 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2509 caller()->CreateAndSetAndSignalOffer();
2510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002511 MediaExpectations media_expectations;
2512 media_expectations.ExpectBidirectionalAudioAndVideo();
2513 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002514}
2515
2516// Test that we can receive the audio output level from a remote audio track.
2517// TODO(deadbeef): Use a fake audio source and verify that the output level is
2518// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002520 ASSERT_TRUE(CreatePeerConnectionWrappers());
2521 ConnectFakeSignaling();
2522 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002523 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526
2527 // Get the audio output level stats. Note that the level is not available
2528 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002529 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002530 kMaxWaitForFramesMs);
2531}
2532
2533// Test that an audio input level is reported.
2534// TODO(deadbeef): Use a fake audio source and verify that the input level is
2535// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002536TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002537 ASSERT_TRUE(CreatePeerConnectionWrappers());
2538 ConnectFakeSignaling();
2539 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002540 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002541 caller()->CreateAndSetAndSignalOffer();
2542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2543
2544 // Get the audio input level stats. The level should be available very
2545 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002546 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002547 kMaxWaitForStatsMs);
2548}
2549
2550// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002551TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002552 ASSERT_TRUE(CreatePeerConnectionWrappers());
2553 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002554 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002555 // Do offer/answer, wait for the callee to receive some frames.
2556 caller()->CreateAndSetAndSignalOffer();
2557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558
2559 MediaExpectations media_expectations;
2560 media_expectations.CalleeExpectsSomeAudioAndVideo();
2561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002562
2563 // Get a handle to the remote tracks created, so they can be used as GetStats
2564 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002565 for (auto receiver : callee()->pc()->GetReceivers()) {
2566 // We received frames, so we definitely should have nonzero "received bytes"
2567 // stats at this point.
2568 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2569 0);
2570 }
deadbeef1dcb1642017-03-29 21:08:16 -07002571}
2572
2573// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002574TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002575 ASSERT_TRUE(CreatePeerConnectionWrappers());
2576 ConnectFakeSignaling();
2577 auto audio_track = caller()->CreateLocalAudioTrack();
2578 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002579 caller()->AddTrack(audio_track);
2580 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002581 // Do offer/answer, wait for the callee to receive some frames.
2582 caller()->CreateAndSetAndSignalOffer();
2583 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584 MediaExpectations media_expectations;
2585 media_expectations.CalleeExpectsSomeAudioAndVideo();
2586 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002587
2588 // The callee received frames, so we definitely should have nonzero "sent
2589 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002590 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2591 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2592}
2593
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002594// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002596 ASSERT_TRUE(CreatePeerConnectionWrappers());
2597 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002598 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002599
Steve Anton15324772018-01-16 10:26:49 -08002600 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002601
2602 // Do offer/answer, wait for the callee to receive some frames.
2603 caller()->CreateAndSetAndSignalOffer();
2604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2605
2606 // Get the remote audio track created on the receiver, so they can be used as
2607 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002608 auto receivers = callee()->pc()->GetReceivers();
2609 ASSERT_EQ(1u, receivers.size());
2610 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002611
2612 // Get the audio output level stats. Note that the level is not available
2613 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002614 EXPECT_TRUE_WAIT(
2615 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2616 0,
2617 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002618}
2619
deadbeefd8ad7882017-04-18 16:01:17 -07002620// Test that we can get stats (using the new stats implemnetation) for
2621// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2622// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002623TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002624 GetStatsForUnsignaledStreamWithNewStatsApi) {
2625 ASSERT_TRUE(CreatePeerConnectionWrappers());
2626 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002627 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002628 // Remove SSRCs and MSIDs from the received offer SDP.
2629 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2630 caller()->CreateAndSetAndSignalOffer();
2631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002632 MediaExpectations media_expectations;
2633 media_expectations.CalleeExpectsSomeAudio(1);
2634 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002635
2636 // We received a frame, so we should have nonzero "bytes received" stats for
2637 // the unsignaled stream, if stats are working for it.
2638 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2639 callee()->NewGetStats();
2640 ASSERT_NE(nullptr, report);
2641 auto inbound_stream_stats =
2642 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2643 ASSERT_EQ(1U, inbound_stream_stats.size());
2644 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2645 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002646 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2647}
2648
Taylor Brandstettera4653442018-06-19 09:44:26 -07002649// Same as above but for the legacy stats implementation.
2650TEST_P(PeerConnectionIntegrationTest,
2651 GetStatsForUnsignaledStreamWithOldStatsApi) {
2652 ASSERT_TRUE(CreatePeerConnectionWrappers());
2653 ConnectFakeSignaling();
2654 caller()->AddAudioTrack();
2655 // Remove SSRCs and MSIDs from the received offer SDP.
2656 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2657 caller()->CreateAndSetAndSignalOffer();
2658 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2659
2660 // Note that, since the old stats implementation associates SSRCs with tracks
2661 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2662 // associated track ID. So we can't use the track "selector" argument.
2663 //
2664 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2665 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002666 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002667 kDefaultTimeout);
2668}
2669
zhihuangf8164932017-05-19 13:09:47 -07002670// Test that we can successfully get the media related stats (audio level
2671// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002672TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002673 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2674 ASSERT_TRUE(CreatePeerConnectionWrappers());
2675 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002676 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002677 // Remove SSRCs and MSIDs from the received offer SDP.
2678 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2679 caller()->CreateAndSetAndSignalOffer();
2680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002681 MediaExpectations media_expectations;
2682 media_expectations.CalleeExpectsSomeAudio(1);
2683 media_expectations.CalleeExpectsSomeVideo(1);
2684 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002685
2686 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2687 callee()->NewGetStats();
2688 ASSERT_NE(nullptr, report);
2689
2690 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2691 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2692 ASSERT_GE(audio_index, 0);
2693 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002694}
2695
deadbeef4e2deab2017-09-20 13:56:21 -07002696// Helper for test below.
2697void ModifySsrcs(cricket::SessionDescription* desc) {
2698 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002699 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002700 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002701 for (uint32_t& ssrc : stream.ssrcs) {
2702 ssrc = rtc::CreateRandomId();
2703 }
2704 }
2705 }
2706}
2707
2708// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2709// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2710// This should result in two "RTCInboundRTPStreamStats", but only one
2711// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2712// being reset to 0 once the SSRC change occurs.
2713//
2714// Regression test for this bug:
2715// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2716//
2717// The bug causes the track stats to only represent one of the two streams:
2718// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2719// that the track stat counters would reset to 0 when the new stream is
2720// received, and a 50% chance that they'll stop updating (while
2721// "concealed_samples" continues increasing, due to silence being generated for
2722// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002723TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002724 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002725 ASSERT_TRUE(CreatePeerConnectionWrappers());
2726 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002727 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002728 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2729 // that doesn't signal SSRCs (from the callee's perspective).
2730 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2731 caller()->CreateAndSetAndSignalOffer();
2732 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2733 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002734 {
2735 MediaExpectations media_expectations;
2736 media_expectations.CalleeExpectsSomeAudio(50);
2737 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2738 }
deadbeef4e2deab2017-09-20 13:56:21 -07002739 // Some audio frames were received, so we should have nonzero "samples
2740 // received" for the track.
2741 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2742 callee()->NewGetStats();
2743 ASSERT_NE(nullptr, report);
2744 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2745 ASSERT_EQ(1U, track_stats.size());
2746 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2747 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2748 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2749
2750 // Create a new offer and munge it to cause the caller to use a new SSRC.
2751 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2752 caller()->CreateAndSetAndSignalOffer();
2753 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2754 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2755 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002756 {
2757 MediaExpectations media_expectations;
2758 media_expectations.CalleeExpectsSomeAudio(25);
2759 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2760 }
deadbeef4e2deab2017-09-20 13:56:21 -07002761
2762 report = callee()->NewGetStats();
2763 ASSERT_NE(nullptr, report);
2764 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2765 ASSERT_EQ(1U, track_stats.size());
2766 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2767 // The "total samples received" stat should only be greater than it was
2768 // before.
2769 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2770 // Right now, the new SSRC will cause the counters to reset to 0.
2771 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2772
2773 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002774 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002775 // good sign that we're seeing stats from the old stream that's no longer
2776 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002777 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002778 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2779 EXPECT_LT(*track_stats[0]->concealed_samples,
2780 *track_stats[0]->total_samples_received *
2781 kAcceptableConcealedSamplesPercentage);
2782
2783 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2784 // sanity check that the SSRC really changed.
2785 // TODO(deadbeef): This isn't working right now, because we're not returning
2786 // *any* stats for the inactive stream. Uncomment when the bug is completely
2787 // fixed.
2788 // auto inbound_stream_stats =
2789 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2790 // ASSERT_EQ(2U, inbound_stream_stats.size());
2791}
2792
deadbeef1dcb1642017-03-29 21:08:16 -07002793// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002794TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002795 PeerConnectionFactory::Options dtls_10_options;
2796 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2797 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2798 dtls_10_options));
2799 ConnectFakeSignaling();
2800 // Do normal offer/answer and wait for some frames to be received in each
2801 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002802 caller()->AddAudioVideoTracks();
2803 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002804 caller()->CreateAndSetAndSignalOffer();
2805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002806 MediaExpectations media_expectations;
2807 media_expectations.ExpectBidirectionalAudioAndVideo();
2808 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002809}
2810
2811// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002812TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002813 PeerConnectionFactory::Options dtls_10_options;
2814 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2815 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2816 dtls_10_options));
2817 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002818 caller()->AddAudioVideoTracks();
2819 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002820 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002821 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002822 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002823 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002824 kDefaultTimeout);
2825 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002826 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002827 // TODO(bugs.webrtc.org/9456): Fix it.
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01002828 EXPECT_LE(1, webrtc::metrics::NumEvents(
2829 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2830 kDefaultSrtpCryptoSuite));
2831 EXPECT_GE(2, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002832 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2833 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002834}
2835
2836// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002837TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002838 PeerConnectionFactory::Options dtls_12_options;
2839 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2840 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2841 dtls_12_options));
2842 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002843 caller()->AddAudioVideoTracks();
2844 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002845 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002846 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002847 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002848 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002849 kDefaultTimeout);
2850 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002851 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002852 // TODO(bugs.webrtc.org/9456): Fix it.
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01002853 EXPECT_LE(1, webrtc::metrics::NumEvents(
2854 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2855 kDefaultSrtpCryptoSuite));
2856 EXPECT_GE(2, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002857 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2858 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002859}
2860
2861// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2862// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002863TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002864 PeerConnectionFactory::Options caller_options;
2865 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2866 PeerConnectionFactory::Options callee_options;
2867 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2868 ASSERT_TRUE(
2869 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2870 ConnectFakeSignaling();
2871 // Do normal offer/answer and wait for some frames to be received in each
2872 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002873 caller()->AddAudioVideoTracks();
2874 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002875 caller()->CreateAndSetAndSignalOffer();
2876 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002877 MediaExpectations media_expectations;
2878 media_expectations.ExpectBidirectionalAudioAndVideo();
2879 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002880}
2881
2882// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2883// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002884TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002885 PeerConnectionFactory::Options caller_options;
2886 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2887 PeerConnectionFactory::Options callee_options;
2888 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2889 ASSERT_TRUE(
2890 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2891 ConnectFakeSignaling();
2892 // Do normal offer/answer and wait for some frames to be received in each
2893 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002894 caller()->AddAudioVideoTracks();
2895 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002896 caller()->CreateAndSetAndSignalOffer();
2897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002898 MediaExpectations media_expectations;
2899 media_expectations.ExpectBidirectionalAudioAndVideo();
2900 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002901}
2902
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002903// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2904// works as expected; the cipher should only be used if enabled by both sides.
2905TEST_P(PeerConnectionIntegrationTest,
2906 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2907 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002908 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002909 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002910 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2911 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002912 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2913 TestNegotiatedCipherSuite(caller_options, callee_options,
2914 expected_cipher_suite);
2915}
2916
2917TEST_P(PeerConnectionIntegrationTest,
2918 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2919 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002920 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2921 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002922 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002923 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002924 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2925 TestNegotiatedCipherSuite(caller_options, callee_options,
2926 expected_cipher_suite);
2927}
2928
2929TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2930 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002931 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002932 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002933 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002934 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2935 TestNegotiatedCipherSuite(caller_options, callee_options,
2936 expected_cipher_suite);
2937}
2938
deadbeef1dcb1642017-03-29 21:08:16 -07002939// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002940TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002941 bool local_gcm_enabled = false;
2942 bool remote_gcm_enabled = false;
2943 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2944 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2945 expected_cipher_suite);
2946}
2947
2948// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002949TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002950 bool local_gcm_enabled = true;
2951 bool remote_gcm_enabled = true;
2952 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2953 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2954 expected_cipher_suite);
2955}
2956
2957// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002958TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002959 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2960 bool local_gcm_enabled = true;
2961 bool remote_gcm_enabled = false;
2962 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2963 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2964 expected_cipher_suite);
2965}
2966
2967// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002968TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002969 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2970 bool local_gcm_enabled = false;
2971 bool remote_gcm_enabled = true;
2972 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2973 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2974 expected_cipher_suite);
2975}
2976
deadbeef7914b8c2017-04-21 03:23:33 -07002977// Verify that media can be transmitted end-to-end when GCM crypto suites are
2978// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2979// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2980// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002981TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002982 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002983 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07002984 ASSERT_TRUE(
2985 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2986 ConnectFakeSignaling();
2987 // Do normal offer/answer and wait for some frames to be received in each
2988 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002989 caller()->AddAudioVideoTracks();
2990 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002991 caller()->CreateAndSetAndSignalOffer();
2992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002993 MediaExpectations media_expectations;
2994 media_expectations.ExpectBidirectionalAudioAndVideo();
2995 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002996}
2997
deadbeef1dcb1642017-03-29 21:08:16 -07002998// This test sets up a call between two parties with audio, video and an RTP
2999// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003000TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003001 PeerConnectionInterface::RTCConfiguration rtc_config;
3002 rtc_config.enable_rtp_data_channel = true;
3003 rtc_config.enable_dtls_srtp = false;
3004 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003005 ConnectFakeSignaling();
3006 // Expect that data channel created on caller side will show up for callee as
3007 // well.
3008 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003009 caller()->AddAudioVideoTracks();
3010 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003011 caller()->CreateAndSetAndSignalOffer();
3012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3013 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003014 MediaExpectations media_expectations;
3015 media_expectations.ExpectBidirectionalAudioAndVideo();
3016 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003017 ASSERT_NE(nullptr, caller()->data_channel());
3018 ASSERT_NE(nullptr, callee()->data_channel());
3019 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3020 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3021
3022 // Ensure data can be sent in both directions.
3023 std::string data = "hello world";
3024 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3025 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3026 kDefaultTimeout);
3027 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3028 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3029 kDefaultTimeout);
3030}
3031
3032// Ensure that an RTP data channel is signaled as closed for the caller when
3033// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003034TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003035 RtpDataChannelSignaledClosedInCalleeOffer) {
3036 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003037 PeerConnectionInterface::RTCConfiguration rtc_config;
3038 rtc_config.enable_rtp_data_channel = true;
3039 rtc_config.enable_dtls_srtp = false;
3040 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003041 ConnectFakeSignaling();
3042 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003043 caller()->AddAudioVideoTracks();
3044 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003045 caller()->CreateAndSetAndSignalOffer();
3046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3047 ASSERT_NE(nullptr, caller()->data_channel());
3048 ASSERT_NE(nullptr, callee()->data_channel());
3049 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3050 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3051
3052 // Close the data channel on the callee, and do an updated offer/answer.
3053 callee()->data_channel()->Close();
3054 callee()->CreateAndSetAndSignalOffer();
3055 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3056 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3057 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3058}
3059
3060// Tests that data is buffered in an RTP data channel until an observer is
3061// registered for it.
3062//
3063// NOTE: RTP data channels can receive data before the underlying
3064// transport has detected that a channel is writable and thus data can be
3065// received before the data channel state changes to open. That is hard to test
3066// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003067TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003068 DataBufferedUntilRtpDataChannelObserverRegistered) {
3069 // Use fake clock and simulated network delay so that we predictably can wait
3070 // until an SCTP message has been delivered without "sleep()"ing.
3071 rtc::ScopedFakeClock fake_clock;
3072 // Some things use a time of "0" as a special value, so we need to start out
3073 // the fake clock at a nonzero time.
3074 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003075 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003076 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3077 virtual_socket_server()->UpdateDelayDistribution();
3078
Niels Möllerf06f9232018-08-07 12:32:18 +02003079 PeerConnectionInterface::RTCConfiguration rtc_config;
3080 rtc_config.enable_rtp_data_channel = true;
3081 rtc_config.enable_dtls_srtp = false;
3082 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003083 ConnectFakeSignaling();
3084 caller()->CreateDataChannel();
3085 caller()->CreateAndSetAndSignalOffer();
3086 ASSERT_TRUE(caller()->data_channel() != nullptr);
3087 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3088 kDefaultTimeout, fake_clock);
3089 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3090 kDefaultTimeout, fake_clock);
3091 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3092 callee()->data_channel()->state(), kDefaultTimeout,
3093 fake_clock);
3094
3095 // Unregister the observer which is normally automatically registered.
3096 callee()->data_channel()->UnregisterObserver();
3097 // Send data and advance fake clock until it should have been received.
3098 std::string data = "hello world";
3099 caller()->data_channel()->Send(DataBuffer(data));
3100 SIMULATED_WAIT(false, 50, fake_clock);
3101
3102 // Attach data channel and expect data to be received immediately. Note that
3103 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3104 // further, but data can be received even if the callback is asynchronous.
3105 MockDataChannelObserver new_observer(callee()->data_channel());
3106 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3107 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003108 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3109 // If this is not done a DCHECK can be hit in ports.cc, because a large
3110 // negative number is calculated for the rtt due to the global clock changing.
3111 caller()->pc()->Close();
3112 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003113}
3114
3115// This test sets up a call between two parties with audio, video and but only
3116// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003117TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003118 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3119 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003120 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003121 rtc_config_1.enable_dtls_srtp = false;
3122 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3123 rtc_config_2.enable_dtls_srtp = false;
3124 rtc_config_2.enable_dtls_srtp = false;
3125 ASSERT_TRUE(
3126 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003127 ConnectFakeSignaling();
3128 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003129 caller()->AddAudioVideoTracks();
3130 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003131 caller()->CreateAndSetAndSignalOffer();
3132 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3133 // The caller should still have a data channel, but it should be closed, and
3134 // one should ever have been created for the callee.
3135 EXPECT_TRUE(caller()->data_channel() != nullptr);
3136 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3137 EXPECT_EQ(nullptr, callee()->data_channel());
3138}
3139
3140// This test sets up a call between two parties with audio, and video. When
3141// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003142TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003143 PeerConnectionInterface::RTCConfiguration rtc_config;
3144 rtc_config.enable_rtp_data_channel = true;
3145 rtc_config.enable_dtls_srtp = false;
3146 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003147 ConnectFakeSignaling();
3148 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003149 caller()->AddAudioVideoTracks();
3150 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003151 caller()->CreateAndSetAndSignalOffer();
3152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3153 // Create data channel and do new offer and answer.
3154 caller()->CreateDataChannel();
3155 caller()->CreateAndSetAndSignalOffer();
3156 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3157 ASSERT_NE(nullptr, caller()->data_channel());
3158 ASSERT_NE(nullptr, callee()->data_channel());
3159 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3160 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3161 // Ensure data can be sent in both directions.
3162 std::string data = "hello world";
3163 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3164 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3165 kDefaultTimeout);
3166 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3167 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3168 kDefaultTimeout);
3169}
3170
3171#ifdef HAVE_SCTP
3172
3173// This test sets up a call between two parties with audio, video and an SCTP
3174// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003175TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003176 ASSERT_TRUE(CreatePeerConnectionWrappers());
3177 ConnectFakeSignaling();
3178 // Expect that data channel created on caller side will show up for callee as
3179 // well.
3180 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003181 caller()->AddAudioVideoTracks();
3182 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003183 caller()->CreateAndSetAndSignalOffer();
3184 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3185 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003186 MediaExpectations media_expectations;
3187 media_expectations.ExpectBidirectionalAudioAndVideo();
3188 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003189 // Caller data channel should already exist (it created one). Callee data
3190 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3191 ASSERT_NE(nullptr, caller()->data_channel());
3192 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3193 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3194 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3195
3196 // Ensure data can be sent in both directions.
3197 std::string data = "hello world";
3198 caller()->data_channel()->Send(DataBuffer(data));
3199 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3200 kDefaultTimeout);
3201 callee()->data_channel()->Send(DataBuffer(data));
3202 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3203 kDefaultTimeout);
3204}
3205
3206// Ensure that when the callee closes an SCTP data channel, the closing
3207// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003208TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003209 // Same procedure as above test.
3210 ASSERT_TRUE(CreatePeerConnectionWrappers());
3211 ConnectFakeSignaling();
3212 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003213 caller()->AddAudioVideoTracks();
3214 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003215 caller()->CreateAndSetAndSignalOffer();
3216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3217 ASSERT_NE(nullptr, caller()->data_channel());
3218 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3219 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3220 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3221
3222 // Close the data channel on the callee side, and wait for it to reach the
3223 // "closed" state on both sides.
3224 callee()->data_channel()->Close();
3225 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3226 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3227}
3228
Seth Hampson2f0d7022018-02-20 11:54:42 -08003229TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003230 ASSERT_TRUE(CreatePeerConnectionWrappers());
3231 ConnectFakeSignaling();
3232 webrtc::DataChannelInit init;
3233 init.id = 53;
3234 init.maxRetransmits = 52;
3235 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003236 caller()->AddAudioVideoTracks();
3237 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003238 caller()->CreateAndSetAndSignalOffer();
3239 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003240 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3241 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003242 EXPECT_EQ(init.id, callee()->data_channel()->id());
3243 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3244 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3245 EXPECT_FALSE(callee()->data_channel()->negotiated());
3246}
3247
deadbeef1dcb1642017-03-29 21:08:16 -07003248// Test usrsctp's ability to process unordered data stream, where data actually
3249// arrives out of order using simulated delays. Previously there have been some
3250// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003251TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003252 // Introduce random network delays.
3253 // Otherwise it's not a true "unordered" test.
3254 virtual_socket_server()->set_delay_mean(20);
3255 virtual_socket_server()->set_delay_stddev(5);
3256 virtual_socket_server()->UpdateDelayDistribution();
3257 // Normal procedure, but with unordered data channel config.
3258 ASSERT_TRUE(CreatePeerConnectionWrappers());
3259 ConnectFakeSignaling();
3260 webrtc::DataChannelInit init;
3261 init.ordered = false;
3262 caller()->CreateDataChannel(&init);
3263 caller()->CreateAndSetAndSignalOffer();
3264 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3265 ASSERT_NE(nullptr, caller()->data_channel());
3266 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3267 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3268 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3269
3270 static constexpr int kNumMessages = 100;
3271 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3272 static constexpr size_t kMaxMessageSize = 4096;
3273 // Create and send random messages.
3274 std::vector<std::string> sent_messages;
3275 for (int i = 0; i < kNumMessages; ++i) {
3276 size_t length =
3277 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3278 std::string message;
3279 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3280 caller()->data_channel()->Send(DataBuffer(message));
3281 callee()->data_channel()->Send(DataBuffer(message));
3282 sent_messages.push_back(message);
3283 }
3284
3285 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003286 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003287 caller()->data_observer()->received_message_count(),
3288 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003289 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003290 callee()->data_observer()->received_message_count(),
3291 kDefaultTimeout);
3292
3293 // Sort and compare to make sure none of the messages were corrupted.
3294 std::vector<std::string> caller_received_messages =
3295 caller()->data_observer()->messages();
3296 std::vector<std::string> callee_received_messages =
3297 callee()->data_observer()->messages();
3298 std::sort(sent_messages.begin(), sent_messages.end());
3299 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3300 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3301 EXPECT_EQ(sent_messages, caller_received_messages);
3302 EXPECT_EQ(sent_messages, callee_received_messages);
3303}
3304
3305// This test sets up a call between two parties with audio, and video. When
3306// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003307TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003308 ASSERT_TRUE(CreatePeerConnectionWrappers());
3309 ConnectFakeSignaling();
3310 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003311 caller()->AddAudioVideoTracks();
3312 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003313 caller()->CreateAndSetAndSignalOffer();
3314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3315 // Create data channel and do new offer and answer.
3316 caller()->CreateDataChannel();
3317 caller()->CreateAndSetAndSignalOffer();
3318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3319 // Caller data channel should already exist (it created one). Callee data
3320 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3321 ASSERT_NE(nullptr, caller()->data_channel());
3322 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3323 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3324 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3325 // Ensure data can be sent in both directions.
3326 std::string data = "hello world";
3327 caller()->data_channel()->Send(DataBuffer(data));
3328 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3329 kDefaultTimeout);
3330 callee()->data_channel()->Send(DataBuffer(data));
3331 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3332 kDefaultTimeout);
3333}
3334
deadbeef7914b8c2017-04-21 03:23:33 -07003335// Set up a connection initially just using SCTP data channels, later upgrading
3336// to audio/video, ensuring frames are received end-to-end. Effectively the
3337// inverse of the test above.
3338// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003339TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003340 ASSERT_TRUE(CreatePeerConnectionWrappers());
3341 ConnectFakeSignaling();
3342 // Do initial offer/answer with just data channel.
3343 caller()->CreateDataChannel();
3344 caller()->CreateAndSetAndSignalOffer();
3345 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3346 // Wait until data can be sent over the data channel.
3347 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3348 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3349 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3350
3351 // Do subsequent offer/answer with two-way audio and video. Audio and video
3352 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003353 caller()->AddAudioVideoTracks();
3354 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003355 caller()->CreateAndSetAndSignalOffer();
3356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003357 MediaExpectations media_expectations;
3358 media_expectations.ExpectBidirectionalAudioAndVideo();
3359 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003360}
3361
deadbeef8b7e9ad2017-05-25 09:38:55 -07003362static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003363 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003364 GetFirstDataContentDescription(desc);
3365 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003366 dcd_offer->set_use_sctpmap(false);
3367 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3368}
3369
3370// Test that the data channel works when a spec-compliant SCTP m= section is
3371// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3372// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003373TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003374 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3375 ASSERT_TRUE(CreatePeerConnectionWrappers());
3376 ConnectFakeSignaling();
3377 caller()->CreateDataChannel();
3378 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3379 caller()->CreateAndSetAndSignalOffer();
3380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3381 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3382 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3383 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3384
3385 // Ensure data can be sent in both directions.
3386 std::string data = "hello world";
3387 caller()->data_channel()->Send(DataBuffer(data));
3388 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3389 kDefaultTimeout);
3390 callee()->data_channel()->Send(DataBuffer(data));
3391 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3392 kDefaultTimeout);
3393}
3394
deadbeef1dcb1642017-03-29 21:08:16 -07003395#endif // HAVE_SCTP
3396
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003397// This test sets up a call between two parties with a media transport data
3398// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003399TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3400 PeerConnectionInterface::RTCConfiguration rtc_config;
3401 rtc_config.use_media_transport_for_data_channels = true;
3402 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3403 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3404 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3405 loopback_media_transports()->second_factory()));
3406 ConnectFakeSignaling();
3407
3408 // Expect that data channel created on caller side will show up for callee as
3409 // well.
3410 caller()->CreateDataChannel();
3411 caller()->CreateAndSetAndSignalOffer();
3412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3413
3414 // Ensure that the media transport is ready.
3415 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3416 loopback_media_transports()->FlushAsyncInvokes();
3417
3418 // Caller data channel should already exist (it created one). Callee data
3419 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3420 ASSERT_NE(nullptr, caller()->data_channel());
3421 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3422 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3423 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3424
3425 // Ensure data can be sent in both directions.
3426 std::string data = "hello world";
3427 caller()->data_channel()->Send(DataBuffer(data));
3428 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3429 kDefaultTimeout);
3430 callee()->data_channel()->Send(DataBuffer(data));
3431 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3432 kDefaultTimeout);
3433}
3434
3435// Ensure that when the callee closes a media transport data channel, the
3436// closing procedure results in the data channel being closed for the caller
3437// as well.
3438TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3439 PeerConnectionInterface::RTCConfiguration rtc_config;
3440 rtc_config.use_media_transport_for_data_channels = true;
3441 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3442 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3443 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3444 loopback_media_transports()->second_factory()));
3445 ConnectFakeSignaling();
3446
3447 // Create a data channel on the caller and signal it to the callee.
3448 caller()->CreateDataChannel();
3449 caller()->CreateAndSetAndSignalOffer();
3450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3451
3452 // Ensure that the media transport is ready.
3453 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3454 loopback_media_transports()->FlushAsyncInvokes();
3455
3456 // Data channels exist and open on both ends of the connection.
3457 ASSERT_NE(nullptr, caller()->data_channel());
3458 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3459 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3460 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3461
3462 // Close the data channel on the callee side, and wait for it to reach the
3463 // "closed" state on both sides.
3464 callee()->data_channel()->Close();
3465 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3466 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3467}
3468
3469TEST_P(PeerConnectionIntegrationTest,
3470 MediaTransportDataChannelConfigSentToOtherSide) {
3471 PeerConnectionInterface::RTCConfiguration rtc_config;
3472 rtc_config.use_media_transport_for_data_channels = true;
3473 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3474 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3475 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3476 loopback_media_transports()->second_factory()));
3477 ConnectFakeSignaling();
3478
3479 // Create a data channel with a non-default configuration and signal it to the
3480 // callee.
3481 webrtc::DataChannelInit init;
3482 init.id = 53;
3483 init.maxRetransmits = 52;
3484 caller()->CreateDataChannel("data-channel", &init);
3485 caller()->CreateAndSetAndSignalOffer();
3486 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3487
3488 // Ensure that the media transport is ready.
3489 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3490 loopback_media_transports()->FlushAsyncInvokes();
3491
3492 // Ensure that the data channel exists on the callee with the correct
3493 // configuration.
3494 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3495 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3496 EXPECT_EQ(init.id, callee()->data_channel()->id());
3497 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3498 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3499 EXPECT_FALSE(callee()->data_channel()->negotiated());
3500}
3501
Niels Möllerc68d2822018-11-20 14:52:05 +01003502TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3503 PeerConnectionInterface::RTCConfiguration rtc_config;
3504 rtc_config.use_media_transport = true;
3505 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3506 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3507 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3508 loopback_media_transports()->second_factory()));
3509 ConnectFakeSignaling();
3510
3511 caller()->AddAudioTrack();
3512 callee()->AddAudioTrack();
3513 // Start offer/answer exchange and wait for it to complete.
3514 caller()->CreateAndSetAndSignalOffer();
3515 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3516
3517 // Ensure that the media transport is ready.
3518 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3519 loopback_media_transports()->FlushAsyncInvokes();
3520
3521 MediaExpectations media_expectations;
3522 media_expectations.ExpectBidirectionalAudio();
3523 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3524
3525 webrtc::MediaTransportPair::Stats first_stats =
3526 loopback_media_transports()->FirstStats();
3527 webrtc::MediaTransportPair::Stats second_stats =
3528 loopback_media_transports()->SecondStats();
3529
3530 EXPECT_GT(first_stats.received_audio_frames, 0);
3531 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3532
3533 EXPECT_GT(second_stats.received_audio_frames, 0);
3534 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3535}
3536
deadbeef1dcb1642017-03-29 21:08:16 -07003537// Test that the ICE connection and gathering states eventually reach
3538// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003539TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003540 ASSERT_TRUE(CreatePeerConnectionWrappers());
3541 ConnectFakeSignaling();
3542 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003543 caller()->AddAudioVideoTracks();
3544 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003545 caller()->CreateAndSetAndSignalOffer();
3546 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3547 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3548 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3549 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3550 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3551 // After the best candidate pair is selected and all candidates are signaled,
3552 // the ICE connection state should reach "complete".
3553 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3554 // answerer/"callee" by default) only reaches "connected". When this is
3555 // fixed, this test should be updated.
3556 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3557 caller()->ice_connection_state(), kDefaultTimeout);
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01003558 EXPECT_TRUE_WAIT(
3559 callee()->ice_connection_state() ==
3560 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
3561 callee()->ice_connection_state() ==
3562 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3563 kDefaultTimeout)
3564 << callee()->ice_connection_state();
deadbeef1dcb1642017-03-29 21:08:16 -07003565}
3566
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003567// Replaces the first candidate with a static address and configures a
3568// MockAsyncResolver to return the replaced address the first time the static
3569// address is resolved. Candidates past the first will not be signaled.
3570class ReplaceFirstCandidateAddressDropOthers final
3571 : public IceCandidateReplacerInterface {
3572 public:
3573 ReplaceFirstCandidateAddressDropOthers(
3574 const SocketAddress& new_address,
3575 rtc::MockAsyncResolver* mock_async_resolver)
3576 : mock_async_resolver_(mock_async_resolver), new_address_(new_address) {
3577 RTC_DCHECK(mock_async_resolver);
3578 }
3579
3580 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3581 const webrtc::IceCandidateInterface* candidate) override {
3582 if (replaced_candidate_) {
3583 return nullptr;
3584 }
3585
3586 replaced_candidate_ = true;
3587 cricket::Candidate new_candidate(candidate->candidate());
3588 new_candidate.set_address(new_address_);
3589 EXPECT_CALL(*mock_async_resolver_, GetResolvedAddress(_, _))
3590 .WillOnce(DoAll(SetArgPointee<1>(candidate->candidate().address()),
3591 Return(true)));
3592 EXPECT_CALL(*mock_async_resolver_, Destroy(_));
3593 return webrtc::CreateIceCandidate(
3594 candidate->sdp_mid(), candidate->sdp_mline_index(), new_candidate);
3595 }
3596
3597 private:
3598 rtc::MockAsyncResolver* mock_async_resolver_;
3599 SocketAddress new_address_;
3600 bool replaced_candidate_ = false;
3601};
3602
3603// Drops all candidates before they are signaled.
3604class DropAllCandidates final : public IceCandidateReplacerInterface {
3605 public:
3606 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3607 const webrtc::IceCandidateInterface*) override {
3608 return nullptr;
3609 }
3610};
3611
3612// Replace the first caller ICE candidate IP with a fake hostname and drop the
3613// other candidates. Drop all candidates on the callee side (to avoid a prflx
3614// connection). Use a mock resolver to resolve the hostname back to the original
3615// IP on the callee side and check that the ice connection connects.
3616TEST_P(PeerConnectionIntegrationTest,
3617 IceStatesReachCompletionWithRemoteHostname) {
3618 webrtc::MockAsyncResolverFactory* callee_mock_async_resolver_factory;
3619 {
3620 auto resolver_factory =
3621 absl::make_unique<webrtc::MockAsyncResolverFactory>();
3622 callee_mock_async_resolver_factory = resolver_factory.get();
3623 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3624 callee_deps.async_resolver_factory = std::move(resolver_factory);
3625
3626 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3627 RTCConfiguration(), webrtc::PeerConnectionDependencies(nullptr),
3628 RTCConfiguration(), std::move(callee_deps)));
3629 }
3630
3631 rtc::MockAsyncResolver mock_async_resolver;
3632
3633 // This also verifies that the injected AsyncResolverFactory is used by
3634 // P2PTransportChannel.
3635 EXPECT_CALL(*callee_mock_async_resolver_factory, Create())
3636 .WillOnce(Return(&mock_async_resolver));
3637 caller()->SetLocalIceCandidateReplacer(
3638 absl::make_unique<ReplaceFirstCandidateAddressDropOthers>(
3639 SocketAddress("a.b", 10000), &mock_async_resolver));
3640 callee()->SetLocalIceCandidateReplacer(
3641 absl::make_unique<DropAllCandidates>());
3642
3643 ConnectFakeSignaling();
3644 caller()->AddAudioVideoTracks();
3645 callee()->AddAudioVideoTracks();
3646 caller()->CreateAndSetAndSignalOffer();
3647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3648 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3649 caller()->ice_connection_state(), kDefaultTimeout);
3650 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3651 callee()->ice_connection_state(), kDefaultTimeout);
3652}
3653
Steve Antonede9ca52017-10-16 13:04:27 -07003654// Test that firewalling the ICE connection causes the clients to identify the
3655// disconnected state and then removing the firewall causes them to reconnect.
3656class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003657 : public PeerConnectionIntegrationBaseTest,
3658 public ::testing::WithParamInterface<
3659 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003660 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003661 PeerConnectionIntegrationIceStatesTest()
3662 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3663 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003664 }
3665
3666 void StartStunServer(const SocketAddress& server_address) {
3667 stun_server_.reset(
3668 cricket::TestStunServer::Create(network_thread(), server_address));
3669 }
3670
3671 bool TestIPv6() {
3672 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3673 }
3674
3675 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003676 network_thread()->Invoke<void>(
3677 RTC_FROM_HERE,
3678 rtc::Bind(&cricket::PortAllocator::set_flags,
3679 caller()->port_allocator(), port_allocator_flags_));
3680 network_thread()->Invoke<void>(
3681 RTC_FROM_HERE,
3682 rtc::Bind(&cricket::PortAllocator::set_flags,
3683 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003684 }
3685
3686 std::vector<SocketAddress> CallerAddresses() {
3687 std::vector<SocketAddress> addresses;
3688 addresses.push_back(SocketAddress("1.1.1.1", 0));
3689 if (TestIPv6()) {
3690 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3691 }
3692 return addresses;
3693 }
3694
3695 std::vector<SocketAddress> CalleeAddresses() {
3696 std::vector<SocketAddress> addresses;
3697 addresses.push_back(SocketAddress("2.2.2.2", 0));
3698 if (TestIPv6()) {
3699 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3700 }
3701 return addresses;
3702 }
3703
3704 void SetUpNetworkInterfaces() {
3705 // Remove the default interfaces added by the test infrastructure.
3706 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3707 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3708
3709 // Add network addresses for test.
3710 for (const auto& caller_address : CallerAddresses()) {
3711 caller()->network()->AddInterface(caller_address);
3712 }
3713 for (const auto& callee_address : CalleeAddresses()) {
3714 callee()->network()->AddInterface(callee_address);
3715 }
3716 }
3717
3718 private:
3719 uint32_t port_allocator_flags_;
3720 std::unique_ptr<cricket::TestStunServer> stun_server_;
3721};
3722
3723// Tests that the PeerConnection goes through all the ICE gathering/connection
3724// states over the duration of the call. This includes Disconnected and Failed
3725// states, induced by putting a firewall between the peers and waiting for them
3726// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003727TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3728 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3729 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3730 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003731
3732 const SocketAddress kStunServerAddress =
3733 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3734 StartStunServer(kStunServerAddress);
3735
3736 PeerConnectionInterface::RTCConfiguration config;
3737 PeerConnectionInterface::IceServer ice_stun_server;
3738 ice_stun_server.urls.push_back(
3739 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3740 kStunServerAddress.PortAsString());
3741 config.servers.push_back(ice_stun_server);
3742
3743 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3744 ConnectFakeSignaling();
3745 SetPortAllocatorFlags();
3746 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003747 caller()->AddAudioVideoTracks();
3748 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003749
3750 // Initial state before anything happens.
3751 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3752 caller()->ice_gathering_state());
3753 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3754 caller()->ice_connection_state());
3755
3756 // Start the call by creating the offer, setting it as the local description,
3757 // then sending it to the peer who will respond with an answer. This happens
3758 // asynchronously so that we can watch the states as it runs in the
3759 // background.
3760 caller()->CreateAndSetAndSignalOffer();
3761
Steve Anton83119dd2017-11-10 16:19:52 -08003762 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3763 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003764
3765 // Verify that the observer was notified of the intermediate transitions.
3766 EXPECT_THAT(caller()->ice_connection_state_history(),
3767 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3768 PeerConnectionInterface::kIceConnectionConnected,
3769 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003770 // After the ice transport transitions from checking to connected we revert
3771 // back to new as the standard requires, as at that point the DTLS transport
3772 // is in the "new" state while no transports are "connecting", "checking",
3773 // "failed" or disconnected. This is pretty unintuitive, and we might want to
3774 // amend the spec to handle this case more gracefully.
3775 EXPECT_THAT(
3776 caller()->peer_connection_state_history(),
3777 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
3778 PeerConnectionInterface::PeerConnectionState::kNew,
3779 PeerConnectionInterface::PeerConnectionState::kConnecting,
3780 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003781 EXPECT_THAT(caller()->ice_gathering_state_history(),
3782 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3783 PeerConnectionInterface::kIceGatheringComplete));
3784
3785 // Block connections to/from the caller and wait for ICE to become
3786 // disconnected.
3787 for (const auto& caller_address : CallerAddresses()) {
3788 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3789 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003790 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003791 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3792 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003793
3794 // Let ICE re-establish by removing the firewall rules.
3795 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003796 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003797 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3798 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003799
3800 // According to RFC7675, if there is no response within 30 seconds then the
3801 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003802 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003803 constexpr int kConsentTimeout = 30000;
3804 for (const auto& caller_address : CallerAddresses()) {
3805 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3806 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003807 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003808 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3809 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003810}
3811
3812// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3813// and that the statistics in the metric observers are updated correctly.
3814TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3815 ASSERT_TRUE(CreatePeerConnectionWrappers());
3816 ConnectFakeSignaling();
3817 SetPortAllocatorFlags();
3818 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003819 caller()->AddAudioVideoTracks();
3820 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003821 caller()->CreateAndSetAndSignalOffer();
3822
3823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3824
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003825 // TODO(bugs.webrtc.org/9456): Fix it.
3826 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3827 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3828 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3829 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003830 if (TestIPv6()) {
3831 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3832 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003833 EXPECT_EQ(0, num_best_ipv4);
3834 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003835 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003836 EXPECT_EQ(1, num_best_ipv4);
3837 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003838 }
3839
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003840 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3841 "WebRTC.PeerConnection.CandidatePairType_UDP",
3842 webrtc::kIceCandidatePairHostHost));
3843 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3844 "WebRTC.PeerConnection.CandidatePairType_UDP",
3845 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003846}
3847
3848constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3849 cricket::PORTALLOCATOR_DISABLE_STUN |
3850 cricket::PORTALLOCATOR_DISABLE_RELAY;
3851constexpr uint32_t kFlagsIPv6NoStun =
3852 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3853 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3854constexpr uint32_t kFlagsIPv4Stun =
3855 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3856
Seth Hampson2f0d7022018-02-20 11:54:42 -08003857INSTANTIATE_TEST_CASE_P(
3858 PeerConnectionIntegrationTest,
3859 PeerConnectionIntegrationIceStatesTest,
3860 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3861 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3862 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3863 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003864
deadbeef1dcb1642017-03-29 21:08:16 -07003865// This test sets up a call between two parties with audio and video.
3866// During the call, the caller restarts ICE and the test verifies that
3867// new ICE candidates are generated and audio and video still can flow, and the
3868// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003869TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003870 ASSERT_TRUE(CreatePeerConnectionWrappers());
3871 ConnectFakeSignaling();
3872 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003873 caller()->AddAudioVideoTracks();
3874 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003875 caller()->CreateAndSetAndSignalOffer();
3876 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3877 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3878 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01003879 EXPECT_TRUE_WAIT(
3880 callee()->ice_connection_state() ==
3881 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
3882 callee()->ice_connection_state() ==
3883 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3884 kDefaultTimeout)
3885 << callee()->ice_connection_state();
deadbeef1dcb1642017-03-29 21:08:16 -07003886
3887 // To verify that the ICE restart actually occurs, get
3888 // ufrag/password/candidates before and after restart.
3889 // Create an SDP string of the first audio candidate for both clients.
3890 const webrtc::IceCandidateCollection* audio_candidates_caller =
3891 caller()->pc()->local_description()->candidates(0);
3892 const webrtc::IceCandidateCollection* audio_candidates_callee =
3893 callee()->pc()->local_description()->candidates(0);
3894 ASSERT_GT(audio_candidates_caller->count(), 0u);
3895 ASSERT_GT(audio_candidates_callee->count(), 0u);
3896 std::string caller_candidate_pre_restart;
3897 ASSERT_TRUE(
3898 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3899 std::string callee_candidate_pre_restart;
3900 ASSERT_TRUE(
3901 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3902 const cricket::SessionDescription* desc =
3903 caller()->pc()->local_description()->description();
3904 std::string caller_ufrag_pre_restart =
3905 desc->transport_infos()[0].description.ice_ufrag;
3906 desc = callee()->pc()->local_description()->description();
3907 std::string callee_ufrag_pre_restart =
3908 desc->transport_infos()[0].description.ice_ufrag;
3909
3910 // Have the caller initiate an ICE restart.
3911 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3912 caller()->CreateAndSetAndSignalOffer();
3913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3914 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3915 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Jonas Olsson1e87b4f2018-11-22 16:50:37 +01003916 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
deadbeef1dcb1642017-03-29 21:08:16 -07003917 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3918
3919 // Grab the ufrags/candidates again.
3920 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3921 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3922 ASSERT_GT(audio_candidates_caller->count(), 0u);
3923 ASSERT_GT(audio_candidates_callee->count(), 0u);
3924 std::string caller_candidate_post_restart;
3925 ASSERT_TRUE(
3926 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3927 std::string callee_candidate_post_restart;
3928 ASSERT_TRUE(
3929 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3930 desc = caller()->pc()->local_description()->description();
3931 std::string caller_ufrag_post_restart =
3932 desc->transport_infos()[0].description.ice_ufrag;
3933 desc = callee()->pc()->local_description()->description();
3934 std::string callee_ufrag_post_restart =
3935 desc->transport_infos()[0].description.ice_ufrag;
3936 // Sanity check that an ICE restart was actually negotiated in SDP.
3937 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3938 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3939 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3940 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3941
3942 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003943 MediaExpectations media_expectations;
3944 media_expectations.ExpectBidirectionalAudioAndVideo();
3945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003946}
3947
3948// Verify that audio/video can be received end-to-end when ICE renomination is
3949// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003950TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003951 PeerConnectionInterface::RTCConfiguration config;
3952 config.enable_ice_renomination = true;
3953 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3954 ConnectFakeSignaling();
3955 // Do normal offer/answer and wait for some frames to be received in each
3956 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003957 caller()->AddAudioVideoTracks();
3958 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003959 caller()->CreateAndSetAndSignalOffer();
3960 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3961 // Sanity check that ICE renomination was actually negotiated.
3962 const cricket::SessionDescription* desc =
3963 caller()->pc()->local_description()->description();
3964 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003965 ASSERT_NE(
3966 info.description.transport_options.end(),
3967 std::find(info.description.transport_options.begin(),
3968 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003969 }
3970 desc = callee()->pc()->local_description()->description();
3971 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003972 ASSERT_NE(
3973 info.description.transport_options.end(),
3974 std::find(info.description.transport_options.begin(),
3975 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003976 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003977 MediaExpectations media_expectations;
3978 media_expectations.ExpectBidirectionalAudioAndVideo();
3979 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003980}
3981
Steve Anton6f25b092017-10-23 09:39:20 -07003982// With a max bundle policy and RTCP muxing, adding a new media description to
3983// the connection should not affect ICE at all because the new media will use
3984// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003985TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003986 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003987 PeerConnectionInterface::RTCConfiguration config;
3988 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3989 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3990 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3991 config, PeerConnectionInterface::RTCConfiguration()));
3992 ConnectFakeSignaling();
3993
Steve Anton15324772018-01-16 10:26:49 -08003994 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003995 caller()->CreateAndSetAndSignalOffer();
3996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003997 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3998 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003999
4000 caller()->clear_ice_connection_state_history();
4001
Steve Anton15324772018-01-16 10:26:49 -08004002 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004003 caller()->CreateAndSetAndSignalOffer();
4004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4005
4006 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4007}
4008
deadbeef1dcb1642017-03-29 21:08:16 -07004009// This test sets up a call between two parties with audio and video. It then
4010// renegotiates setting the video m-line to "port 0", then later renegotiates
4011// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004012TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004013 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4014 ASSERT_TRUE(CreatePeerConnectionWrappers());
4015 ConnectFakeSignaling();
4016
4017 // Do initial negotiation, only sending media from the caller. Will result in
4018 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004019 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004020 caller()->CreateAndSetAndSignalOffer();
4021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4022
4023 // Negotiate again, disabling the video "m=" section (the callee will set the
4024 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004025 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4026 PeerConnectionInterface::RTCOfferAnswerOptions options;
4027 options.offer_to_receive_video = 0;
4028 callee()->SetOfferAnswerOptions(options);
4029 } else {
4030 callee()->SetRemoteOfferHandler([this] {
4031 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4032 });
4033 }
deadbeef1dcb1642017-03-29 21:08:16 -07004034 caller()->CreateAndSetAndSignalOffer();
4035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4036 // Sanity check that video "m=" section was actually rejected.
4037 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4038 callee()->pc()->local_description()->description());
4039 ASSERT_NE(nullptr, answer_video_content);
4040 ASSERT_TRUE(answer_video_content->rejected);
4041
4042 // Enable video and do negotiation again, making sure video is received
4043 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004044 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4045 PeerConnectionInterface::RTCOfferAnswerOptions options;
4046 options.offer_to_receive_video = 1;
4047 callee()->SetOfferAnswerOptions(options);
4048 } else {
4049 // The caller's transceiver is stopped, so we need to add another track.
4050 auto caller_transceiver =
4051 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4052 EXPECT_TRUE(caller_transceiver->stopped());
4053 caller()->AddVideoTrack();
4054 }
4055 callee()->AddVideoTrack();
4056 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004057 caller()->CreateAndSetAndSignalOffer();
4058 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004059
deadbeef1dcb1642017-03-29 21:08:16 -07004060 // Verify the caller receives frames from the newly added stream, and the
4061 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004062 MediaExpectations media_expectations;
4063 media_expectations.CalleeExpectsSomeAudio();
4064 media_expectations.ExpectBidirectionalVideo();
4065 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004066}
4067
deadbeef1dcb1642017-03-29 21:08:16 -07004068// This tests that if we negotiate after calling CreateSender but before we
4069// have a track, then set a track later, frames from the newly-set track are
4070// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004071TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004072 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4073 ASSERT_TRUE(CreatePeerConnectionWrappers());
4074 ConnectFakeSignaling();
4075 auto caller_audio_sender =
4076 caller()->pc()->CreateSender("audio", "caller_stream");
4077 auto caller_video_sender =
4078 caller()->pc()->CreateSender("video", "caller_stream");
4079 auto callee_audio_sender =
4080 callee()->pc()->CreateSender("audio", "callee_stream");
4081 auto callee_video_sender =
4082 callee()->pc()->CreateSender("video", "callee_stream");
4083 caller()->CreateAndSetAndSignalOffer();
4084 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4085 // Wait for ICE to complete, without any tracks being set.
4086 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4087 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4088 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4089 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4090 // Now set the tracks, and expect frames to immediately start flowing.
4091 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4092 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4093 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4094 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004095 MediaExpectations media_expectations;
4096 media_expectations.ExpectBidirectionalAudioAndVideo();
4097 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4098}
4099
4100// This tests that if we negotiate after calling AddTransceiver but before we
4101// have a track, then set a track later, frames from the newly-set tracks are
4102// received end-to-end.
4103TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4104 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4105 ASSERT_TRUE(CreatePeerConnectionWrappers());
4106 ConnectFakeSignaling();
4107 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4108 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4109 auto caller_audio_sender = audio_result.MoveValue()->sender();
4110 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4111 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4112 auto caller_video_sender = video_result.MoveValue()->sender();
4113 callee()->SetRemoteOfferHandler([this] {
4114 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4115 callee()->pc()->GetTransceivers()[0]->SetDirection(
4116 RtpTransceiverDirection::kSendRecv);
4117 callee()->pc()->GetTransceivers()[1]->SetDirection(
4118 RtpTransceiverDirection::kSendRecv);
4119 });
4120 caller()->CreateAndSetAndSignalOffer();
4121 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4122 // Wait for ICE to complete, without any tracks being set.
4123 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4124 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4125 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4126 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4127 // Now set the tracks, and expect frames to immediately start flowing.
4128 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4129 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4130 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4131 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4132 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4133 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4134 MediaExpectations media_expectations;
4135 media_expectations.ExpectBidirectionalAudioAndVideo();
4136 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004137}
4138
4139// This test verifies that a remote video track can be added via AddStream,
4140// and sent end-to-end. For this particular test, it's simply echoed back
4141// from the caller to the callee, rather than being forwarded to a third
4142// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004143TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004144 ASSERT_TRUE(CreatePeerConnectionWrappers());
4145 ConnectFakeSignaling();
4146 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004147 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004148 caller()->CreateAndSetAndSignalOffer();
4149 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004150 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004151
4152 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4153 // time).
4154 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4155 callee()->CreateAndSetAndSignalOffer();
4156 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4157
Seth Hampson2f0d7022018-02-20 11:54:42 -08004158 MediaExpectations media_expectations;
4159 media_expectations.ExpectBidirectionalVideo();
4160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004161}
4162
4163// Test that we achieve the expected end-to-end connection time, using a
4164// fake clock and simulated latency on the media and signaling paths.
4165// We use a TURN<->TURN connection because this is usually the quickest to
4166// set up initially, especially when we're confident the connection will work
4167// and can start sending media before we get a STUN response.
4168//
4169// With various optimizations enabled, here are the network delays we expect to
4170// be on the critical path:
4171// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4172// signaling answer (with DTLS fingerprint).
4173// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4174// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4175// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004176TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004177 rtc::ScopedFakeClock fake_clock;
4178 // Some things use a time of "0" as a special value, so we need to start out
4179 // the fake clock at a nonzero time.
4180 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004181 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004182
4183 static constexpr int media_hop_delay_ms = 50;
4184 static constexpr int signaling_trip_delay_ms = 500;
4185 // For explanation of these values, see comment above.
4186 static constexpr int required_media_hops = 9;
4187 static constexpr int required_signaling_trips = 2;
4188 // For internal delays (such as posting an event asychronously).
4189 static constexpr int allowed_internal_delay_ms = 20;
4190 static constexpr int total_connection_time_ms =
4191 media_hop_delay_ms * required_media_hops +
4192 signaling_trip_delay_ms * required_signaling_trips +
4193 allowed_internal_delay_ms;
4194
4195 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4196 3478};
4197 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4198 0};
4199 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4200 3478};
4201 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4202 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004203 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4204 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004205
Seth Hampsonaed71642018-06-11 07:41:32 -07004206 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4207 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004208 // Bypass permission check on received packets so media can be sent before
4209 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004210 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4211 turn_server_1->set_enable_permission_checks(false);
4212 });
4213 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4214 turn_server_2->set_enable_permission_checks(false);
4215 });
deadbeef1dcb1642017-03-29 21:08:16 -07004216
4217 PeerConnectionInterface::RTCConfiguration client_1_config;
4218 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4219 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4220 ice_server_1.username = "test";
4221 ice_server_1.password = "test";
4222 client_1_config.servers.push_back(ice_server_1);
4223 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4224 client_1_config.presume_writable_when_fully_relayed = true;
4225
4226 PeerConnectionInterface::RTCConfiguration client_2_config;
4227 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4228 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4229 ice_server_2.username = "test";
4230 ice_server_2.password = "test";
4231 client_2_config.servers.push_back(ice_server_2);
4232 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4233 client_2_config.presume_writable_when_fully_relayed = true;
4234
4235 ASSERT_TRUE(
4236 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4237 // Set up the simulated delays.
4238 SetSignalingDelayMs(signaling_trip_delay_ms);
4239 ConnectFakeSignaling();
4240 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4241 virtual_socket_server()->UpdateDelayDistribution();
4242
4243 // Set "offer to receive audio/video" without adding any tracks, so we just
4244 // set up ICE/DTLS with no media.
4245 PeerConnectionInterface::RTCOfferAnswerOptions options;
4246 options.offer_to_receive_audio = 1;
4247 options.offer_to_receive_video = 1;
4248 caller()->SetOfferAnswerOptions(options);
4249 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004250 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4251 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004252 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4253 // If this is not done a DCHECK can be hit in ports.cc, because a large
4254 // negative number is calculated for the rtt due to the global clock changing.
4255 caller()->pc()->Close();
4256 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004257}
4258
Jonas Orelandbdcee282017-10-10 14:01:40 +02004259// Verify that a TurnCustomizer passed in through RTCConfiguration
4260// is actually used by the underlying TURN candidate pair.
4261// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004262TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004263 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4264 3478};
4265 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4266 0};
4267 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4268 3478};
4269 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4270 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004271 CreateTurnServer(turn_server_1_internal_address,
4272 turn_server_1_external_address);
4273 CreateTurnServer(turn_server_2_internal_address,
4274 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004275
4276 PeerConnectionInterface::RTCConfiguration client_1_config;
4277 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4278 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4279 ice_server_1.username = "test";
4280 ice_server_1.password = "test";
4281 client_1_config.servers.push_back(ice_server_1);
4282 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004283 auto* customizer1 = CreateTurnCustomizer();
4284 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004285
4286 PeerConnectionInterface::RTCConfiguration client_2_config;
4287 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4288 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4289 ice_server_2.username = "test";
4290 ice_server_2.password = "test";
4291 client_2_config.servers.push_back(ice_server_2);
4292 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004293 auto* customizer2 = CreateTurnCustomizer();
4294 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004295
4296 ASSERT_TRUE(
4297 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4298 ConnectFakeSignaling();
4299
4300 // Set "offer to receive audio/video" without adding any tracks, so we just
4301 // set up ICE/DTLS with no media.
4302 PeerConnectionInterface::RTCOfferAnswerOptions options;
4303 options.offer_to_receive_audio = 1;
4304 options.offer_to_receive_video = 1;
4305 caller()->SetOfferAnswerOptions(options);
4306 caller()->CreateAndSetAndSignalOffer();
4307 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4308
Seth Hampsonaed71642018-06-11 07:41:32 -07004309 ExpectTurnCustomizerCountersIncremented(customizer1);
4310 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004311}
4312
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004313// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4314// send media between the caller and the callee.
4315TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4316 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4317 3478};
4318 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4319
4320 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004321 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4322 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004323
4324 webrtc::PeerConnectionInterface::IceServer ice_server;
4325 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4326 ice_server.username = "test";
4327 ice_server.password = "test";
4328
4329 PeerConnectionInterface::RTCConfiguration client_1_config;
4330 client_1_config.servers.push_back(ice_server);
4331 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4332
4333 PeerConnectionInterface::RTCConfiguration client_2_config;
4334 client_2_config.servers.push_back(ice_server);
4335 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4336
4337 ASSERT_TRUE(
4338 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4339
4340 // Do normal offer/answer and wait for ICE to complete.
4341 ConnectFakeSignaling();
4342 caller()->AddAudioVideoTracks();
4343 callee()->AddAudioVideoTracks();
4344 caller()->CreateAndSetAndSignalOffer();
4345 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4346 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4347 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4348
4349 MediaExpectations media_expectations;
4350 media_expectations.ExpectBidirectionalAudioAndVideo();
4351 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4352}
4353
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004354// Verify that a SSLCertificateVerifier passed in through
4355// PeerConnectionDependencies is actually used by the underlying SSL
4356// implementation to determine whether a certificate presented by the TURN
4357// server is accepted by the client. Note that openssladapter_unittest.cc
4358// contains more detailed, lower-level tests.
4359TEST_P(PeerConnectionIntegrationTest,
4360 SSLCertificateVerifierUsedForTurnConnections) {
4361 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4362 3478};
4363 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4364
4365 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4366 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004367 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4368 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004369
4370 webrtc::PeerConnectionInterface::IceServer ice_server;
4371 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4372 ice_server.username = "test";
4373 ice_server.password = "test";
4374
4375 PeerConnectionInterface::RTCConfiguration client_1_config;
4376 client_1_config.servers.push_back(ice_server);
4377 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4378
4379 PeerConnectionInterface::RTCConfiguration client_2_config;
4380 client_2_config.servers.push_back(ice_server);
4381 // Setting the type to kRelay forces the connection to go through a TURN
4382 // server.
4383 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4384
4385 // Get a copy to the pointer so we can verify calls later.
4386 rtc::TestCertificateVerifier* client_1_cert_verifier =
4387 new rtc::TestCertificateVerifier();
4388 client_1_cert_verifier->verify_certificate_ = true;
4389 rtc::TestCertificateVerifier* client_2_cert_verifier =
4390 new rtc::TestCertificateVerifier();
4391 client_2_cert_verifier->verify_certificate_ = true;
4392
4393 // Create the dependencies with the test certificate verifier.
4394 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4395 client_1_deps.tls_cert_verifier =
4396 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4397 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4398 client_2_deps.tls_cert_verifier =
4399 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4400
4401 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4402 client_1_config, std::move(client_1_deps), client_2_config,
4403 std::move(client_2_deps)));
4404 ConnectFakeSignaling();
4405
4406 // Set "offer to receive audio/video" without adding any tracks, so we just
4407 // set up ICE/DTLS with no media.
4408 PeerConnectionInterface::RTCOfferAnswerOptions options;
4409 options.offer_to_receive_audio = 1;
4410 options.offer_to_receive_video = 1;
4411 caller()->SetOfferAnswerOptions(options);
4412 caller()->CreateAndSetAndSignalOffer();
4413 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4414
4415 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4416 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004417}
4418
4419TEST_P(PeerConnectionIntegrationTest,
4420 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4421 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4422 3478};
4423 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4424
4425 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4426 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004427 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4428 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004429
4430 webrtc::PeerConnectionInterface::IceServer ice_server;
4431 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4432 ice_server.username = "test";
4433 ice_server.password = "test";
4434
4435 PeerConnectionInterface::RTCConfiguration client_1_config;
4436 client_1_config.servers.push_back(ice_server);
4437 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4438
4439 PeerConnectionInterface::RTCConfiguration client_2_config;
4440 client_2_config.servers.push_back(ice_server);
4441 // Setting the type to kRelay forces the connection to go through a TURN
4442 // server.
4443 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4444
4445 // Get a copy to the pointer so we can verify calls later.
4446 rtc::TestCertificateVerifier* client_1_cert_verifier =
4447 new rtc::TestCertificateVerifier();
4448 client_1_cert_verifier->verify_certificate_ = false;
4449 rtc::TestCertificateVerifier* client_2_cert_verifier =
4450 new rtc::TestCertificateVerifier();
4451 client_2_cert_verifier->verify_certificate_ = false;
4452
4453 // Create the dependencies with the test certificate verifier.
4454 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4455 client_1_deps.tls_cert_verifier =
4456 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4457 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4458 client_2_deps.tls_cert_verifier =
4459 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4460
4461 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4462 client_1_config, std::move(client_1_deps), client_2_config,
4463 std::move(client_2_deps)));
4464 ConnectFakeSignaling();
4465
4466 // Set "offer to receive audio/video" without adding any tracks, so we just
4467 // set up ICE/DTLS with no media.
4468 PeerConnectionInterface::RTCOfferAnswerOptions options;
4469 options.offer_to_receive_audio = 1;
4470 options.offer_to_receive_video = 1;
4471 caller()->SetOfferAnswerOptions(options);
4472 caller()->CreateAndSetAndSignalOffer();
4473 bool wait_res = true;
4474 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4475 // properly, should be able to just wait for a state of "failed" instead of
4476 // waiting a fixed 10 seconds.
4477 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4478 ASSERT_FALSE(wait_res);
4479
4480 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4481 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004482}
4483
deadbeefc964d0b2017-04-03 10:03:35 -07004484// Test that audio and video flow end-to-end when codec names don't use the
4485// expected casing, given that they're supposed to be case insensitive. To test
4486// this, all but one codec is removed from each media description, and its
4487// casing is changed.
4488//
4489// In the past, this has regressed and caused crashes/black video, due to the
4490// fact that code at some layers was doing case-insensitive comparisons and
4491// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004492TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004493 ASSERT_TRUE(CreatePeerConnectionWrappers());
4494 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004495 caller()->AddAudioVideoTracks();
4496 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004497
4498 // Remove all but one audio/video codec (opus and VP8), and change the
4499 // casing of the caller's generated offer.
4500 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4501 cricket::AudioContentDescription* audio =
4502 GetFirstAudioContentDescription(description);
4503 ASSERT_NE(nullptr, audio);
4504 auto audio_codecs = audio->codecs();
4505 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4506 [](const cricket::AudioCodec& codec) {
4507 return codec.name != "opus";
4508 }),
4509 audio_codecs.end());
4510 ASSERT_EQ(1u, audio_codecs.size());
4511 audio_codecs[0].name = "OpUs";
4512 audio->set_codecs(audio_codecs);
4513
4514 cricket::VideoContentDescription* video =
4515 GetFirstVideoContentDescription(description);
4516 ASSERT_NE(nullptr, video);
4517 auto video_codecs = video->codecs();
4518 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4519 [](const cricket::VideoCodec& codec) {
4520 return codec.name != "VP8";
4521 }),
4522 video_codecs.end());
4523 ASSERT_EQ(1u, video_codecs.size());
4524 video_codecs[0].name = "vP8";
4525 video->set_codecs(video_codecs);
4526 });
4527
4528 caller()->CreateAndSetAndSignalOffer();
4529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4530
4531 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004532 MediaExpectations media_expectations;
4533 media_expectations.ExpectBidirectionalAudioAndVideo();
4534 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004535}
4536
Jonas Oreland49ac5952018-09-26 16:04:32 +02004537TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004538 ASSERT_TRUE(CreatePeerConnectionWrappers());
4539 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004540 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004541 caller()->CreateAndSetAndSignalOffer();
4542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004543 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004544 MediaExpectations media_expectations;
4545 media_expectations.CalleeExpectsSomeAudio(1);
4546 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004547 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004548 auto receiver = callee()->pc()->GetReceivers()[0];
4549 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004550 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004551 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4552 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004553 sources[0].source_id());
4554 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4555}
4556
4557TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4558 ASSERT_TRUE(CreatePeerConnectionWrappers());
4559 ConnectFakeSignaling();
4560 caller()->AddVideoTrack();
4561 caller()->CreateAndSetAndSignalOffer();
4562 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4563 // Wait for one video frame to be received by the callee.
4564 MediaExpectations media_expectations;
4565 media_expectations.CalleeExpectsSomeVideo(1);
4566 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4567 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4568 auto receiver = callee()->pc()->GetReceivers()[0];
4569 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4570 auto sources = receiver->GetSources();
4571 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4572 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4573 sources[0].source_id());
4574 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004575}
4576
deadbeef2f425aa2017-04-14 10:41:32 -07004577// Test that if a track is removed and added again with a different stream ID,
4578// the new stream ID is successfully communicated in SDP and media continues to
4579// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004580// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4581// it will not reuse a transceiver that has already been sending. After creating
4582// a new transceiver it tries to create an offer with two senders of the same
4583// track ids and it fails.
4584TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004585 ASSERT_TRUE(CreatePeerConnectionWrappers());
4586 ConnectFakeSignaling();
4587
deadbeef2f425aa2017-04-14 10:41:32 -07004588 // Add track using stream 1, do offer/answer.
4589 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4590 caller()->CreateLocalAudioTrack();
4591 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004592 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004593 caller()->CreateAndSetAndSignalOffer();
4594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004595 {
4596 MediaExpectations media_expectations;
4597 media_expectations.CalleeExpectsSomeAudio(1);
4598 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4599 }
deadbeef2f425aa2017-04-14 10:41:32 -07004600 // Remove the sender, and create a new one with the new stream.
4601 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004602 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004603 caller()->CreateAndSetAndSignalOffer();
4604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4605 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004606 {
4607 MediaExpectations media_expectations;
4608 media_expectations.CalleeExpectsSomeAudio();
4609 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4610 }
deadbeef2f425aa2017-04-14 10:41:32 -07004611}
4612
Seth Hampson2f0d7022018-02-20 11:54:42 -08004613TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004614 ASSERT_TRUE(CreatePeerConnectionWrappers());
4615 ConnectFakeSignaling();
4616
Karl Wiberg918f50c2018-07-05 11:40:33 +02004617 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004618 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4619 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4620 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004621 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4622 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004623
Steve Anton15324772018-01-16 10:26:49 -08004624 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004625 caller()->CreateAndSetAndSignalOffer();
4626 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4627}
4628
Steve Antonede9ca52017-10-16 13:04:27 -07004629// Test that if candidates are only signaled by applying full session
4630// descriptions (instead of using AddIceCandidate), the peers can connect to
4631// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004632TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004633 ASSERT_TRUE(CreatePeerConnectionWrappers());
4634 // Each side will signal the session descriptions but not candidates.
4635 ConnectFakeSignalingForSdpOnly();
4636
4637 // Add audio video track and exchange the initial offer/answer with media
4638 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004639 caller()->AddAudioVideoTracks();
4640 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004641 caller()->CreateAndSetAndSignalOffer();
4642
4643 // Wait for all candidates to be gathered on both the caller and callee.
4644 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4645 caller()->ice_gathering_state(), kDefaultTimeout);
4646 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4647 callee()->ice_gathering_state(), kDefaultTimeout);
4648
4649 // The candidates will now be included in the session description, so
4650 // signaling them will start the ICE connection.
4651 caller()->CreateAndSetAndSignalOffer();
4652 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4653
4654 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004655 MediaExpectations media_expectations;
4656 media_expectations.ExpectBidirectionalAudioAndVideo();
4657 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004658}
4659
henrika5f6bf242017-11-01 11:06:56 +01004660// Test that SetAudioPlayout can be used to disable audio playout from the
4661// start, then later enable it. This may be useful, for example, if the caller
4662// needs to play a local ringtone until some event occurs, after which it
4663// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004664TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004665 ASSERT_TRUE(CreatePeerConnectionWrappers());
4666 ConnectFakeSignaling();
4667
4668 // Set up audio-only call where audio playout is disabled on caller's side.
4669 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004670 caller()->AddAudioTrack();
4671 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004672 caller()->CreateAndSetAndSignalOffer();
4673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4674
4675 // Pump messages for a second.
4676 WAIT(false, 1000);
4677 // Since audio playout is disabled, the caller shouldn't have received
4678 // anything (at the playout level, at least).
4679 EXPECT_EQ(0, caller()->audio_frames_received());
4680 // As a sanity check, make sure the callee (for which playout isn't disabled)
4681 // did still see frames on its audio level.
4682 ASSERT_GT(callee()->audio_frames_received(), 0);
4683
4684 // Enable playout again, and ensure audio starts flowing.
4685 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004686 MediaExpectations media_expectations;
4687 media_expectations.ExpectBidirectionalAudio();
4688 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004689}
4690
4691double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4692 auto report = pc->NewGetStats();
4693 auto track_stats_list =
4694 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4695 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4696 for (const auto* track_stats : track_stats_list) {
4697 if (track_stats->remote_source.is_defined() &&
4698 *track_stats->remote_source) {
4699 remote_track_stats = track_stats;
4700 break;
4701 }
4702 }
4703
4704 if (!remote_track_stats->total_audio_energy.is_defined()) {
4705 return 0.0;
4706 }
4707 return *remote_track_stats->total_audio_energy;
4708}
4709
4710// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4711// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004712TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004713 DisableAudioPlayoutStillGeneratesAudioStats) {
4714 ASSERT_TRUE(CreatePeerConnectionWrappers());
4715 ConnectFakeSignaling();
4716
4717 // Set up audio-only call where playout is disabled but audio-processing is
4718 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004719 caller()->AddAudioTrack();
4720 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004721 caller()->pc()->SetAudioPlayout(false);
4722
4723 caller()->CreateAndSetAndSignalOffer();
4724 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4725
4726 // Wait for the callee to receive audio stats.
4727 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4728}
4729
henrika4f167df2017-11-01 14:45:55 +01004730// Test that SetAudioRecording can be used to disable audio recording from the
4731// start, then later enable it. This may be useful, for example, if the caller
4732// wants to ensure that no audio resources are active before a certain state
4733// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004734TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004735 ASSERT_TRUE(CreatePeerConnectionWrappers());
4736 ConnectFakeSignaling();
4737
4738 // Set up audio-only call where audio recording is disabled on caller's side.
4739 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004740 caller()->AddAudioTrack();
4741 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004742 caller()->CreateAndSetAndSignalOffer();
4743 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4744
4745 // Pump messages for a second.
4746 WAIT(false, 1000);
4747 // Since caller has disabled audio recording, the callee shouldn't have
4748 // received anything.
4749 EXPECT_EQ(0, callee()->audio_frames_received());
4750 // As a sanity check, make sure the caller did still see frames on its
4751 // audio level since audio recording is enabled on the calle side.
4752 ASSERT_GT(caller()->audio_frames_received(), 0);
4753
4754 // Enable audio recording again, and ensure audio starts flowing.
4755 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004756 MediaExpectations media_expectations;
4757 media_expectations.ExpectBidirectionalAudio();
4758 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004759}
4760
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004761// Test that after closing PeerConnections, they stop sending any packets (ICE,
4762// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004763TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004764 // Set up audio/video/data, wait for some frames to be received.
4765 ASSERT_TRUE(CreatePeerConnectionWrappers());
4766 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004767 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004768#ifdef HAVE_SCTP
4769 caller()->CreateDataChannel();
4770#endif
4771 caller()->CreateAndSetAndSignalOffer();
4772 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004773 MediaExpectations media_expectations;
4774 media_expectations.CalleeExpectsSomeAudioAndVideo();
4775 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004776 // Close PeerConnections.
4777 caller()->pc()->Close();
4778 callee()->pc()->Close();
4779 // Pump messages for a second, and ensure no new packets end up sent.
4780 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4781 WAIT(false, 1000);
4782 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4783 EXPECT_EQ(sent_packets_a, sent_packets_b);
4784}
4785
Steve Anton7eca0932018-03-30 15:18:41 -07004786// Test that transport stats are generated by the RTCStatsCollector for a
4787// connection that only involves data channels. This is a regression test for
4788// crbug.com/826972.
4789#ifdef HAVE_SCTP
4790TEST_P(PeerConnectionIntegrationTest,
4791 TransportStatsReportedForDataChannelOnlyConnection) {
4792 ASSERT_TRUE(CreatePeerConnectionWrappers());
4793 ConnectFakeSignaling();
4794 caller()->CreateDataChannel();
4795
4796 caller()->CreateAndSetAndSignalOffer();
4797 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4798 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4799
4800 auto caller_report = caller()->NewGetStats();
4801 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4802 auto callee_report = callee()->NewGetStats();
4803 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4804}
4805#endif // HAVE_SCTP
4806
Qingsi Wang7685e862018-06-11 20:15:46 -07004807TEST_P(PeerConnectionIntegrationTest,
4808 IceEventsGeneratedAndLoggedInRtcEventLog) {
4809 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4810 ConnectFakeSignaling();
4811 PeerConnectionInterface::RTCOfferAnswerOptions options;
4812 options.offer_to_receive_audio = 1;
4813 caller()->SetOfferAnswerOptions(options);
4814 caller()->CreateAndSetAndSignalOffer();
4815 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4816 ASSERT_NE(nullptr, caller()->event_log_factory());
4817 ASSERT_NE(nullptr, callee()->event_log_factory());
4818 webrtc::FakeRtcEventLog* caller_event_log =
4819 static_cast<webrtc::FakeRtcEventLog*>(
4820 caller()->event_log_factory()->last_log_created());
4821 webrtc::FakeRtcEventLog* callee_event_log =
4822 static_cast<webrtc::FakeRtcEventLog*>(
4823 callee()->event_log_factory()->last_log_created());
4824 ASSERT_NE(nullptr, caller_event_log);
4825 ASSERT_NE(nullptr, callee_event_log);
4826 int caller_ice_config_count = caller_event_log->GetEventCount(
4827 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4828 int caller_ice_event_count = caller_event_log->GetEventCount(
4829 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4830 int callee_ice_config_count = callee_event_log->GetEventCount(
4831 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4832 int callee_ice_event_count = callee_event_log->GetEventCount(
4833 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4834 EXPECT_LT(0, caller_ice_config_count);
4835 EXPECT_LT(0, caller_ice_event_count);
4836 EXPECT_LT(0, callee_ice_config_count);
4837 EXPECT_LT(0, callee_ice_event_count);
4838}
4839
Seth Hampson2f0d7022018-02-20 11:54:42 -08004840INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4841 PeerConnectionIntegrationTest,
4842 Values(SdpSemantics::kPlanB,
4843 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004844
Steve Anton74255ff2018-01-24 18:32:57 -08004845// Tests that verify interoperability between Plan B and Unified Plan
4846// PeerConnections.
4847class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004848 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004849 public ::testing::WithParamInterface<
4850 std::tuple<SdpSemantics, SdpSemantics>> {
4851 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004852 // Setting the SdpSemantics for the base test to kDefault does not matter
4853 // because we specify not to use the test semantics when creating
4854 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004855 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004856 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004857 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004858 callee_semantics_(std::get<1>(GetParam())) {}
4859
4860 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004861 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4862 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004863 }
4864
4865 const SdpSemantics caller_semantics_;
4866 const SdpSemantics callee_semantics_;
4867};
4868
4869TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4870 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4871 ConnectFakeSignaling();
4872
4873 caller()->CreateAndSetAndSignalOffer();
4874 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4875}
4876
4877TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4878 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4879 ConnectFakeSignaling();
4880 auto audio_sender = caller()->AddAudioTrack();
4881
4882 caller()->CreateAndSetAndSignalOffer();
4883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4884
4885 // Verify that one audio receiver has been created on the remote and that it
4886 // has the same track ID as the sending track.
4887 auto receivers = callee()->pc()->GetReceivers();
4888 ASSERT_EQ(1u, receivers.size());
4889 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4890 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4891
Seth Hampson2f0d7022018-02-20 11:54:42 -08004892 MediaExpectations media_expectations;
4893 media_expectations.CalleeExpectsSomeAudio();
4894 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004895}
4896
4897TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4898 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4899 ConnectFakeSignaling();
4900 auto video_sender = caller()->AddVideoTrack();
4901 auto audio_sender = caller()->AddAudioTrack();
4902
4903 caller()->CreateAndSetAndSignalOffer();
4904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4905
4906 // Verify that one audio and one video receiver have been created on the
4907 // remote and that they have the same track IDs as the sending tracks.
4908 auto audio_receivers =
4909 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4910 ASSERT_EQ(1u, audio_receivers.size());
4911 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4912 auto video_receivers =
4913 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4914 ASSERT_EQ(1u, video_receivers.size());
4915 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4916
Seth Hampson2f0d7022018-02-20 11:54:42 -08004917 MediaExpectations media_expectations;
4918 media_expectations.CalleeExpectsSomeAudioAndVideo();
4919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004920}
4921
4922TEST_P(PeerConnectionIntegrationInteropTest,
4923 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4924 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4925 ConnectFakeSignaling();
4926 caller()->AddAudioVideoTracks();
4927 callee()->AddAudioVideoTracks();
4928
4929 caller()->CreateAndSetAndSignalOffer();
4930 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4931
Seth Hampson2f0d7022018-02-20 11:54:42 -08004932 MediaExpectations media_expectations;
4933 media_expectations.ExpectBidirectionalAudioAndVideo();
4934 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004935}
4936
4937TEST_P(PeerConnectionIntegrationInteropTest,
4938 ReverseRolesOneAudioLocalToOneVideoRemote) {
4939 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4940 ConnectFakeSignaling();
4941 caller()->AddAudioTrack();
4942 callee()->AddVideoTrack();
4943
4944 caller()->CreateAndSetAndSignalOffer();
4945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4946
4947 // Verify that only the audio track has been negotiated.
4948 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4949 // Might also check that the callee's NegotiationNeeded flag is set.
4950
4951 // Reverse roles.
4952 callee()->CreateAndSetAndSignalOffer();
4953 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4954
Seth Hampson2f0d7022018-02-20 11:54:42 -08004955 MediaExpectations media_expectations;
4956 media_expectations.CallerExpectsSomeVideo();
4957 media_expectations.CalleeExpectsSomeAudio();
4958 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004959}
4960
Steve Antonba42e992018-04-09 14:10:01 -07004961INSTANTIATE_TEST_CASE_P(
4962 PeerConnectionIntegrationTest,
4963 PeerConnectionIntegrationInteropTest,
4964 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4965 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4966
4967// Test that if the Unified Plan side offers two video tracks then the Plan B
4968// side will only see the first one and ignore the second.
4969TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004970 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4971 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004972 ConnectFakeSignaling();
4973 auto first_sender = caller()->AddVideoTrack();
4974 caller()->AddVideoTrack();
4975
4976 caller()->CreateAndSetAndSignalOffer();
4977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4978
4979 // Verify that there is only one receiver and it corresponds to the first
4980 // added track.
4981 auto receivers = callee()->pc()->GetReceivers();
4982 ASSERT_EQ(1u, receivers.size());
4983 EXPECT_TRUE(receivers[0]->track()->enabled());
4984 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4985
Seth Hampson2f0d7022018-02-20 11:54:42 -08004986 MediaExpectations media_expectations;
4987 media_expectations.CalleeExpectsSomeVideo();
4988 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004989}
4990
deadbeef1dcb1642017-03-29 21:08:16 -07004991} // namespace
4992
4993#endif // if !defined(THREAD_SANITIZER)