blob: ccd7d05cb51de9d3bb0d954ae9fbe20ecb5e4442 [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
556 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
557 return pc()->ice_gathering_state();
558 }
559
560 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
561 // GetReceivers. They're updated automatically when a remote offer/answer
562 // from the fake signaling channel is applied, or when
563 // ResetRtpReceiverObservers below is called.
564 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
565 rtp_receiver_observers() {
566 return rtp_receiver_observers_;
567 }
568
569 void ResetRtpReceiverObservers() {
570 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100571 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
572 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700573 std::unique_ptr<MockRtpReceiverObserver> observer(
574 new MockRtpReceiverObserver(receiver->media_type()));
575 receiver->SetObserver(observer.get());
576 rtp_receiver_observers_.push_back(std::move(observer));
577 }
578 }
579
Steve Antonede9ca52017-10-16 13:04:27 -0700580 rtc::FakeNetworkManager* network() const {
581 return fake_network_manager_.get();
582 }
583 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
584
Qingsi Wang7685e862018-06-11 20:15:46 -0700585 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
586 return event_log_factory_;
587 }
588
deadbeef1dcb1642017-03-29 21:08:16 -0700589 private:
590 explicit PeerConnectionWrapper(const std::string& debug_name)
591 : debug_name_(debug_name) {}
592
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800593 bool Init(
594 const PeerConnectionFactory::Options* options,
595 const PeerConnectionInterface::RTCConfiguration* config,
596 webrtc::PeerConnectionDependencies dependencies,
597 rtc::Thread* network_thread,
598 rtc::Thread* worker_thread,
599 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
600 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700601 // There's an error in this test code if Init ends up being called twice.
602 RTC_DCHECK(!peer_connection_);
603 RTC_DCHECK(!peer_connection_factory_);
604
605 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700606 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700607
608 std::unique_ptr<cricket::PortAllocator> port_allocator(
609 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700610 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700611 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
612 if (!fake_audio_capture_module_) {
613 return false;
614 }
deadbeef1dcb1642017-03-29 21:08:16 -0700615 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700616
617 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
618 pc_factory_dependencies.network_thread = network_thread;
619 pc_factory_dependencies.worker_thread = worker_thread;
620 pc_factory_dependencies.signaling_thread = signaling_thread;
621 pc_factory_dependencies.media_engine =
622 cricket::WebRtcMediaEngineFactory::Create(
623 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
624 fake_audio_capture_module_),
625 webrtc::CreateBuiltinAudioEncoderFactory(),
626 webrtc::CreateBuiltinAudioDecoderFactory(),
627 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000628 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700629 webrtc::AudioProcessingBuilder().Create());
630 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
631 if (event_log_factory) {
632 event_log_factory_ = event_log_factory.get();
633 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
634 } else {
635 pc_factory_dependencies.event_log_factory =
636 webrtc::CreateRtcEventLogFactory();
637 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800638 if (media_transport_factory) {
639 pc_factory_dependencies.media_transport_factory =
640 std::move(media_transport_factory);
641 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700642 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
643 std::move(pc_factory_dependencies));
644
deadbeef1dcb1642017-03-29 21:08:16 -0700645 if (!peer_connection_factory_) {
646 return false;
647 }
648 if (options) {
649 peer_connection_factory_->SetOptions(*options);
650 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800651 if (config) {
652 sdp_semantics_ = config->sdp_semantics;
653 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700654
655 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200656 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700657 return peer_connection_.get() != nullptr;
658 }
659
660 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700661 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700662 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700663 PeerConnectionInterface::RTCConfiguration modified_config;
664 // If |config| is null, this will result in a default configuration being
665 // used.
666 if (config) {
667 modified_config = *config;
668 }
669 // Disable resolution adaptation; we don't want it interfering with the
670 // test results.
671 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
672 // ratios and not specific resolutions, is this even necessary?
673 modified_config.set_cpu_adaptation(false);
674
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700675 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700676 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700677 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700678 }
679
680 void set_signaling_message_receiver(
681 SignalingMessageReceiver* signaling_message_receiver) {
682 signaling_message_receiver_ = signaling_message_receiver;
683 }
684
685 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
686
Steve Antonede9ca52017-10-16 13:04:27 -0700687 void set_signal_ice_candidates(bool signal) {
688 signal_ice_candidates_ = signal;
689 }
690
deadbeef1dcb1642017-03-29 21:08:16 -0700691 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200692 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700693 // Set max frame rate to 10fps to reduce the risk of test flakiness.
694 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200695 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700696
Niels Möller5c7efe72018-05-11 10:34:46 +0200697 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200698 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
699 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700700 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200701 peer_connection_factory_->CreateVideoTrack(
702 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700703 if (!local_video_renderer_) {
704 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
705 }
706 return track;
707 }
708
709 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100710 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800711 std::unique_ptr<SessionDescriptionInterface> desc =
712 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700713 if (received_sdp_munger_) {
714 received_sdp_munger_(desc->description());
715 }
716
717 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
718 // Setting a remote description may have changed the number of receivers,
719 // so reset the receiver observers.
720 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800721 if (remote_offer_handler_) {
722 remote_offer_handler_();
723 }
deadbeef1dcb1642017-03-29 21:08:16 -0700724 auto answer = CreateAnswer();
725 ASSERT_NE(nullptr, answer);
726 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
727 }
728
729 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100730 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800731 std::unique_ptr<SessionDescriptionInterface> desc =
732 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700733 if (received_sdp_munger_) {
734 received_sdp_munger_(desc->description());
735 }
736
737 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
738 // Set the RtpReceiverObserver after receivers are created.
739 ResetRtpReceiverObservers();
740 }
741
742 // Returns null on failure.
743 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
744 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
745 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
746 pc()->CreateOffer(observer, offer_answer_options_);
747 return WaitForDescriptionFromObserver(observer);
748 }
749
750 // Returns null on failure.
751 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
752 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
753 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
754 pc()->CreateAnswer(observer, offer_answer_options_);
755 return WaitForDescriptionFromObserver(observer);
756 }
757
758 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100759 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700760 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
761 if (!observer->result()) {
762 return nullptr;
763 }
764 auto description = observer->MoveDescription();
765 if (generated_sdp_munger_) {
766 generated_sdp_munger_(description->description());
767 }
768 return description;
769 }
770
771 // Setting the local description and sending the SDP message over the fake
772 // signaling channel are combined into the same method because the SDP
773 // message needs to be sent as soon as SetLocalDescription finishes, without
774 // waiting for the observer to be called. This ensures that ICE candidates
775 // don't outrace the description.
776 bool SetLocalDescriptionAndSendSdpMessage(
777 std::unique_ptr<SessionDescriptionInterface> desc) {
778 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
779 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100780 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800781 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700782 std::string sdp;
783 EXPECT_TRUE(desc->ToString(&sdp));
784 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800785 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
786 RemoveUnusedVideoRenderers();
787 }
deadbeef1dcb1642017-03-29 21:08:16 -0700788 // As mentioned above, we need to send the message immediately after
789 // SetLocalDescription.
790 SendSdpMessage(type, sdp);
791 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
792 return true;
793 }
794
795 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
796 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
797 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100798 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700799 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800800 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
801 RemoveUnusedVideoRenderers();
802 }
deadbeef1dcb1642017-03-29 21:08:16 -0700803 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
804 return observer->result();
805 }
806
Seth Hampson2f0d7022018-02-20 11:54:42 -0800807 // This is a work around to remove unused fake_video_renderers from
808 // transceivers that have either stopped or are no longer receiving.
809 void RemoveUnusedVideoRenderers() {
810 auto transceivers = pc()->GetTransceivers();
811 for (auto& transceiver : transceivers) {
812 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
813 continue;
814 }
815 // Remove fake video renderers from any stopped transceivers.
816 if (transceiver->stopped()) {
817 auto it =
818 fake_video_renderers_.find(transceiver->receiver()->track()->id());
819 if (it != fake_video_renderers_.end()) {
820 fake_video_renderers_.erase(it);
821 }
822 }
823 // Remove fake video renderers from any transceivers that are no longer
824 // receiving.
825 if ((transceiver->current_direction() &&
826 !webrtc::RtpTransceiverDirectionHasRecv(
827 *transceiver->current_direction()))) {
828 auto it =
829 fake_video_renderers_.find(transceiver->receiver()->track()->id());
830 if (it != fake_video_renderers_.end()) {
831 fake_video_renderers_.erase(it);
832 }
833 }
834 }
835 }
836
deadbeef1dcb1642017-03-29 21:08:16 -0700837 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
838 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800839 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700840 if (signaling_delay_ms_ == 0) {
841 RelaySdpMessageIfReceiverExists(type, msg);
842 } else {
843 invoker_.AsyncInvokeDelayed<void>(
844 RTC_FROM_HERE, rtc::Thread::Current(),
845 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
846 this, type, msg),
847 signaling_delay_ms_);
848 }
849 }
850
Steve Antona3a92c22017-12-07 10:27:41 -0800851 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700852 if (signaling_message_receiver_) {
853 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
854 }
855 }
856
857 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
858 // default).
859 void SendIceMessage(const std::string& sdp_mid,
860 int sdp_mline_index,
861 const std::string& msg) {
862 if (signaling_delay_ms_ == 0) {
863 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
864 } else {
865 invoker_.AsyncInvokeDelayed<void>(
866 RTC_FROM_HERE, rtc::Thread::Current(),
867 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
868 this, sdp_mid, sdp_mline_index, msg),
869 signaling_delay_ms_);
870 }
871 }
872
873 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
874 int sdp_mline_index,
875 const std::string& msg) {
876 if (signaling_message_receiver_) {
877 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
878 msg);
879 }
880 }
881
882 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800883 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
884 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700885 HandleIncomingOffer(msg);
886 } else {
887 HandleIncomingAnswer(msg);
888 }
889 }
890
891 void ReceiveIceMessage(const std::string& sdp_mid,
892 int sdp_mline_index,
893 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700895 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
896 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
897 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
898 }
899
900 // PeerConnectionObserver callbacks.
901 void OnSignalingChange(
902 webrtc::PeerConnectionInterface::SignalingState new_state) override {
903 EXPECT_EQ(pc()->signaling_state(), new_state);
904 }
Steve Anton15324772018-01-16 10:26:49 -0800905 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
906 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
907 streams) override {
908 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
909 rtc::scoped_refptr<VideoTrackInterface> video_track(
910 static_cast<VideoTrackInterface*>(receiver->track().get()));
911 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700912 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800913 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200914 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700915 }
916 }
Steve Anton15324772018-01-16 10:26:49 -0800917 void OnRemoveTrack(
918 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
919 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
920 auto it = fake_video_renderers_.find(receiver->track()->id());
921 RTC_DCHECK(it != fake_video_renderers_.end());
922 fake_video_renderers_.erase(it);
923 }
924 }
deadbeef1dcb1642017-03-29 21:08:16 -0700925 void OnRenegotiationNeeded() override {}
926 void OnIceConnectionChange(
927 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
928 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700929 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700930 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200931 void OnConnectionChange(
932 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
933 peer_connection_state_history_.push_back(new_state);
934 }
935
deadbeef1dcb1642017-03-29 21:08:16 -0700936 void OnIceGatheringChange(
937 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700938 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700939 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700940 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700941 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceIceCandidate(
942 const webrtc::IceCandidateInterface* candidate) {
943 std::string candidate_string;
944 candidate->ToString(&candidate_string);
945
946 auto owned_candidate =
947 local_ice_candidate_replacer_->ReplaceCandidate(candidate);
948 if (!owned_candidate) {
949 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer dropped \""
950 << candidate_string << "\"";
951 return nullptr;
952 }
953 std::string owned_candidate_string;
954 owned_candidate->ToString(&owned_candidate_string);
955 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer changed \""
956 << candidate_string << "\" to \"" << owned_candidate_string
957 << "\"";
958 return owned_candidate;
959 }
deadbeef1dcb1642017-03-29 21:08:16 -0700960 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100961 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700962
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700963 const webrtc::IceCandidateInterface* new_candidate = candidate;
964 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate;
965 if (local_ice_candidate_replacer_) {
966 owned_candidate = ReplaceIceCandidate(candidate);
967 if (!owned_candidate) {
968 return; // The candidate was dropped.
969 }
970 new_candidate = owned_candidate.get();
971 }
972
deadbeef1dcb1642017-03-29 21:08:16 -0700973 std::string ice_sdp;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700974 EXPECT_TRUE(new_candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700975 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700976 // Remote party may be deleted.
977 return;
978 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700979 SendIceMessage(new_candidate->sdp_mid(), new_candidate->sdp_mline_index(),
980 ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700981 }
982 void OnDataChannel(
983 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100984 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700985 data_channel_ = data_channel;
986 data_observer_.reset(new MockDataChannelObserver(data_channel));
987 }
988
deadbeef1dcb1642017-03-29 21:08:16 -0700989 std::string debug_name_;
990
991 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
992
993 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
994 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
995 peer_connection_factory_;
996
Steve Antonede9ca52017-10-16 13:04:27 -0700997 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700998 // Needed to keep track of number of frames sent.
999 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1000 // Needed to keep track of number of frames received.
1001 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1002 fake_video_renderers_;
1003 // Needed to ensure frames aren't received for removed tracks.
1004 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1005 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001006
1007 // For remote peer communication.
1008 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1009 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001010 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -07001011
Niels Möller5c7efe72018-05-11 10:34:46 +02001012 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001013 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001014 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1015 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001016 // |local_video_renderer_| attached to the first created local video track.
1017 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1018
Seth Hampson2f0d7022018-02-20 11:54:42 -08001019 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001020 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1021 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1022 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001023 std::function<void()> remote_offer_handler_;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001024 std::unique_ptr<IceCandidateReplacerInterface> local_ice_candidate_replacer_;
deadbeef1dcb1642017-03-29 21:08:16 -07001025 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1026 std::unique_ptr<MockDataChannelObserver> data_observer_;
1027
1028 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1029
Steve Antonede9ca52017-10-16 13:04:27 -07001030 std::vector<PeerConnectionInterface::IceConnectionState>
1031 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001032 std::vector<PeerConnectionInterface::PeerConnectionState>
1033 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001034 std::vector<PeerConnectionInterface::IceGatheringState>
1035 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001036
Qingsi Wang7685e862018-06-11 20:15:46 -07001037 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1038
deadbeef1dcb1642017-03-29 21:08:16 -07001039 rtc::AsyncInvoker invoker_;
1040
Seth Hampson2f0d7022018-02-20 11:54:42 -08001041 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001042};
1043
Elad Alon99c3fe52017-10-13 16:29:40 +02001044class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1045 public:
1046 virtual ~MockRtcEventLogOutput() = default;
1047 MOCK_CONST_METHOD0(IsActive, bool());
1048 MOCK_METHOD1(Write, bool(const std::string&));
1049};
1050
Seth Hampson2f0d7022018-02-20 11:54:42 -08001051// This helper object is used for both specifying how many audio/video frames
1052// are expected to be received for a caller/callee. It provides helper functions
1053// to specify these expectations. The object initially starts in a state of no
1054// expectations.
1055class MediaExpectations {
1056 public:
1057 enum ExpectFrames {
1058 kExpectSomeFrames,
1059 kExpectNoFrames,
1060 kNoExpectation,
1061 };
1062
1063 void ExpectBidirectionalAudioAndVideo() {
1064 ExpectBidirectionalAudio();
1065 ExpectBidirectionalVideo();
1066 }
1067
1068 void ExpectBidirectionalAudio() {
1069 CallerExpectsSomeAudio();
1070 CalleeExpectsSomeAudio();
1071 }
1072
1073 void ExpectNoAudio() {
1074 CallerExpectsNoAudio();
1075 CalleeExpectsNoAudio();
1076 }
1077
1078 void ExpectBidirectionalVideo() {
1079 CallerExpectsSomeVideo();
1080 CalleeExpectsSomeVideo();
1081 }
1082
1083 void ExpectNoVideo() {
1084 CallerExpectsNoVideo();
1085 CalleeExpectsNoVideo();
1086 }
1087
1088 void CallerExpectsSomeAudioAndVideo() {
1089 CallerExpectsSomeAudio();
1090 CallerExpectsSomeVideo();
1091 }
1092
1093 void CalleeExpectsSomeAudioAndVideo() {
1094 CalleeExpectsSomeAudio();
1095 CalleeExpectsSomeVideo();
1096 }
1097
1098 // Caller's audio functions.
1099 void CallerExpectsSomeAudio(
1100 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1101 caller_audio_expectation_ = kExpectSomeFrames;
1102 caller_audio_frames_expected_ = expected_audio_frames;
1103 }
1104
1105 void CallerExpectsNoAudio() {
1106 caller_audio_expectation_ = kExpectNoFrames;
1107 caller_audio_frames_expected_ = 0;
1108 }
1109
1110 // Caller's video functions.
1111 void CallerExpectsSomeVideo(
1112 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1113 caller_video_expectation_ = kExpectSomeFrames;
1114 caller_video_frames_expected_ = expected_video_frames;
1115 }
1116
1117 void CallerExpectsNoVideo() {
1118 caller_video_expectation_ = kExpectNoFrames;
1119 caller_video_frames_expected_ = 0;
1120 }
1121
1122 // Callee's audio functions.
1123 void CalleeExpectsSomeAudio(
1124 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1125 callee_audio_expectation_ = kExpectSomeFrames;
1126 callee_audio_frames_expected_ = expected_audio_frames;
1127 }
1128
1129 void CalleeExpectsNoAudio() {
1130 callee_audio_expectation_ = kExpectNoFrames;
1131 callee_audio_frames_expected_ = 0;
1132 }
1133
1134 // Callee's video functions.
1135 void CalleeExpectsSomeVideo(
1136 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1137 callee_video_expectation_ = kExpectSomeFrames;
1138 callee_video_frames_expected_ = expected_video_frames;
1139 }
1140
1141 void CalleeExpectsNoVideo() {
1142 callee_video_expectation_ = kExpectNoFrames;
1143 callee_video_frames_expected_ = 0;
1144 }
1145
1146 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1147 ExpectFrames caller_video_expectation_ = kNoExpectation;
1148 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1149 ExpectFrames callee_video_expectation_ = kNoExpectation;
1150 int caller_audio_frames_expected_ = 0;
1151 int caller_video_frames_expected_ = 0;
1152 int callee_audio_frames_expected_ = 0;
1153 int callee_video_frames_expected_ = 0;
1154};
1155
deadbeef1dcb1642017-03-29 21:08:16 -07001156// Tests two PeerConnections connecting to each other end-to-end, using a
1157// virtual network, fake A/V capture and fake encoder/decoders. The
1158// PeerConnections share the threads/socket servers, but use separate versions
1159// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001160class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001161 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001162 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1163 : sdp_semantics_(sdp_semantics),
1164 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001165 fss_(new rtc::FirewallSocketServer(ss_.get())),
1166 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001167 worker_thread_(rtc::Thread::Create()),
1168 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001169 network_thread_->SetName("PCNetworkThread", this);
1170 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001171 RTC_CHECK(network_thread_->Start());
1172 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001173 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001174 }
1175
Seth Hampson2f0d7022018-02-20 11:54:42 -08001176 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001177 // The PeerConnections should deleted before the TurnCustomizers.
1178 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1179 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1180 // that the TurnCustomizer outlives the life of the PeerConnection or else
1181 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001182 if (caller_) {
1183 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001184 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001185 }
1186 if (callee_) {
1187 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001188 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001189 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001190
1191 // If turn servers were created for the test they need to be destroyed on
1192 // the network thread.
1193 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1194 turn_servers_.clear();
1195 turn_customizers_.clear();
1196 });
deadbeef1dcb1642017-03-29 21:08:16 -07001197 }
1198
1199 bool SignalingStateStable() {
1200 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1201 }
1202
deadbeef71452802017-05-07 17:21:01 -07001203 bool DtlsConnected() {
1204 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1205 // are connected. This is an important distinction. Once we have separate
1206 // ICE and DTLS state, this check needs to use the DTLS state.
1207 return (callee()->ice_connection_state() ==
1208 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1209 callee()->ice_connection_state() ==
1210 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1211 (caller()->ice_connection_state() ==
1212 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1213 caller()->ice_connection_state() ==
1214 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1215 }
1216
Qingsi Wang7685e862018-06-11 20:15:46 -07001217 // When |event_log_factory| is null, the default implementation of the event
1218 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001219 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1220 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001221 const PeerConnectionFactory::Options* options,
1222 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001223 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001224 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1225 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001226 RTCConfiguration modified_config;
1227 if (config) {
1228 modified_config = *config;
1229 }
Steve Anton3acffc32018-04-12 17:21:03 -07001230 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001231 if (!dependencies.cert_generator) {
1232 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001233 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001234 }
1235 std::unique_ptr<PeerConnectionWrapper> client(
1236 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001237
Niels Möllerf06f9232018-08-07 12:32:18 +02001238 if (!client->Init(options, &modified_config, std::move(dependencies),
1239 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001240 std::move(event_log_factory),
1241 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001242 return nullptr;
1243 }
1244 return client;
1245 }
1246
Qingsi Wang7685e862018-06-11 20:15:46 -07001247 std::unique_ptr<PeerConnectionWrapper>
1248 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1249 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001250 const PeerConnectionFactory::Options* options,
1251 const RTCConfiguration* config,
1252 webrtc::PeerConnectionDependencies dependencies) {
1253 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1254 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001255 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001256 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001257 std::move(event_log_factory),
1258 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001259 }
1260
deadbeef1dcb1642017-03-29 21:08:16 -07001261 bool CreatePeerConnectionWrappers() {
1262 return CreatePeerConnectionWrappersWithConfig(
1263 PeerConnectionInterface::RTCConfiguration(),
1264 PeerConnectionInterface::RTCConfiguration());
1265 }
1266
Steve Anton3acffc32018-04-12 17:21:03 -07001267 bool CreatePeerConnectionWrappersWithSdpSemantics(
1268 SdpSemantics caller_semantics,
1269 SdpSemantics callee_semantics) {
1270 // Can't specify the sdp_semantics in the passed-in configuration since it
1271 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1272 // stored in sdp_semantics_. So get around this by modifying the instance
1273 // variable before calling CreatePeerConnectionWrapper for the caller and
1274 // callee PeerConnections.
1275 SdpSemantics original_semantics = sdp_semantics_;
1276 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001277 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001278 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001279 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001280 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001281 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001282 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001283 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001284 sdp_semantics_ = original_semantics;
1285 return caller_ && callee_;
1286 }
1287
deadbeef1dcb1642017-03-29 21:08:16 -07001288 bool CreatePeerConnectionWrappersWithConfig(
1289 const PeerConnectionInterface::RTCConfiguration& caller_config,
1290 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001291 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001292 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001293 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1294 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001295 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001296 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001297 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1298 /*media_transport_factory=*/nullptr);
1299 return caller_ && callee_;
1300 }
1301
1302 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1303 const PeerConnectionInterface::RTCConfiguration& caller_config,
1304 const PeerConnectionInterface::RTCConfiguration& callee_config,
1305 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1306 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1307 caller_ =
1308 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1309 webrtc::PeerConnectionDependencies(nullptr),
1310 nullptr, std::move(caller_factory));
1311 callee_ =
1312 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1313 webrtc::PeerConnectionDependencies(nullptr),
1314 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001315 return caller_ && callee_;
1316 }
1317
1318 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1319 const PeerConnectionInterface::RTCConfiguration& caller_config,
1320 webrtc::PeerConnectionDependencies caller_dependencies,
1321 const PeerConnectionInterface::RTCConfiguration& callee_config,
1322 webrtc::PeerConnectionDependencies callee_dependencies) {
1323 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001324 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001325 std::move(caller_dependencies), nullptr,
1326 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001327 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001328 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001329 std::move(callee_dependencies), nullptr,
1330 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001331 return caller_ && callee_;
1332 }
1333
1334 bool CreatePeerConnectionWrappersWithOptions(
1335 const PeerConnectionFactory::Options& caller_options,
1336 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001337 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001338 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001339 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1340 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001341 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001342 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001343 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1344 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001345 return caller_ && callee_;
1346 }
1347
1348 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1349 PeerConnectionInterface::RTCConfiguration default_config;
1350 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001351 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001352 webrtc::PeerConnectionDependencies(nullptr));
1353 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001354 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001355 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001356 return caller_ && callee_;
1357 }
1358
Seth Hampson2f0d7022018-02-20 11:54:42 -08001359 std::unique_ptr<PeerConnectionWrapper>
1360 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001361 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1362 new FakeRTCCertificateGenerator());
1363 cert_generator->use_alternate_key();
1364
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001365 webrtc::PeerConnectionDependencies dependencies(nullptr);
1366 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001367 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001368 std::move(dependencies), nullptr,
1369 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001370 }
1371
Seth Hampsonaed71642018-06-11 07:41:32 -07001372 cricket::TestTurnServer* CreateTurnServer(
1373 rtc::SocketAddress internal_address,
1374 rtc::SocketAddress external_address,
1375 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1376 const std::string& common_name = "test turn server") {
1377 rtc::Thread* thread = network_thread();
1378 std::unique_ptr<cricket::TestTurnServer> turn_server =
1379 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1380 RTC_FROM_HERE,
1381 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001382 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001383 thread, internal_address, external_address, type,
1384 /*ignore_bad_certs=*/true, common_name);
1385 });
1386 turn_servers_.push_back(std::move(turn_server));
1387 // Interactions with the turn server should be done on the network thread.
1388 return turn_servers_.back().get();
1389 }
1390
1391 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1392 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1393 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1394 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001395 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001396 turn_customizers_.push_back(std::move(turn_customizer));
1397 // Interactions with the turn customizer should be done on the network
1398 // thread.
1399 return turn_customizers_.back().get();
1400 }
1401
1402 // Checks that the function counters for a TestTurnCustomizer are greater than
1403 // 0.
1404 void ExpectTurnCustomizerCountersIncremented(
1405 cricket::TestTurnCustomizer* turn_customizer) {
1406 unsigned int allow_channel_data_counter =
1407 network_thread()->Invoke<unsigned int>(
1408 RTC_FROM_HERE, [turn_customizer] {
1409 return turn_customizer->allow_channel_data_cnt_;
1410 });
1411 EXPECT_GT(allow_channel_data_counter, 0u);
1412 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1413 RTC_FROM_HERE,
1414 [turn_customizer] { return turn_customizer->modify_cnt_; });
1415 EXPECT_GT(modify_counter, 0u);
1416 }
1417
deadbeef1dcb1642017-03-29 21:08:16 -07001418 // Once called, SDP blobs and ICE candidates will be automatically signaled
1419 // between PeerConnections.
1420 void ConnectFakeSignaling() {
1421 caller_->set_signaling_message_receiver(callee_.get());
1422 callee_->set_signaling_message_receiver(caller_.get());
1423 }
1424
Steve Antonede9ca52017-10-16 13:04:27 -07001425 // Once called, SDP blobs will be automatically signaled between
1426 // PeerConnections. Note that ICE candidates will not be signaled unless they
1427 // are in the exchanged SDP blobs.
1428 void ConnectFakeSignalingForSdpOnly() {
1429 ConnectFakeSignaling();
1430 SetSignalIceCandidates(false);
1431 }
1432
deadbeef1dcb1642017-03-29 21:08:16 -07001433 void SetSignalingDelayMs(int delay_ms) {
1434 caller_->set_signaling_delay_ms(delay_ms);
1435 callee_->set_signaling_delay_ms(delay_ms);
1436 }
1437
Steve Antonede9ca52017-10-16 13:04:27 -07001438 void SetSignalIceCandidates(bool signal) {
1439 caller_->set_signal_ice_candidates(signal);
1440 callee_->set_signal_ice_candidates(signal);
1441 }
1442
deadbeef1dcb1642017-03-29 21:08:16 -07001443 // Messages may get lost on the unreliable DataChannel, so we send multiple
1444 // times to avoid test flakiness.
1445 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1446 const std::string& data,
1447 int retries) {
1448 for (int i = 0; i < retries; ++i) {
1449 dc->Send(DataBuffer(data));
1450 }
1451 }
1452
1453 rtc::Thread* network_thread() { return network_thread_.get(); }
1454
1455 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1456
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001457 webrtc::MediaTransportPair* loopback_media_transports() {
1458 return &loopback_media_transports_;
1459 }
1460
deadbeef1dcb1642017-03-29 21:08:16 -07001461 PeerConnectionWrapper* caller() { return caller_.get(); }
1462
1463 // Set the |caller_| to the |wrapper| passed in and return the
1464 // original |caller_|.
1465 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1466 PeerConnectionWrapper* wrapper) {
1467 PeerConnectionWrapper* old = caller_.release();
1468 caller_.reset(wrapper);
1469 return old;
1470 }
1471
1472 PeerConnectionWrapper* callee() { return callee_.get(); }
1473
1474 // Set the |callee_| to the |wrapper| passed in and return the
1475 // original |callee_|.
1476 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1477 PeerConnectionWrapper* wrapper) {
1478 PeerConnectionWrapper* old = callee_.release();
1479 callee_.reset(wrapper);
1480 return old;
1481 }
1482
Steve Antonede9ca52017-10-16 13:04:27 -07001483 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1484
Seth Hampson2f0d7022018-02-20 11:54:42 -08001485 // Expects the provided number of new frames to be received within
1486 // kMaxWaitForFramesMs. The new expected frames are specified in
1487 // |media_expectations|. Returns false if any of the expectations were
1488 // not met.
1489 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1490 // First initialize the expected frame counts based upon the current
1491 // frame count.
1492 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1493 if (media_expectations.caller_audio_expectation_ ==
1494 MediaExpectations::kExpectSomeFrames) {
1495 total_caller_audio_frames_expected +=
1496 media_expectations.caller_audio_frames_expected_;
1497 }
1498 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001499 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001500 if (media_expectations.caller_video_expectation_ ==
1501 MediaExpectations::kExpectSomeFrames) {
1502 total_caller_video_frames_expected +=
1503 media_expectations.caller_video_frames_expected_;
1504 }
1505 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1506 if (media_expectations.callee_audio_expectation_ ==
1507 MediaExpectations::kExpectSomeFrames) {
1508 total_callee_audio_frames_expected +=
1509 media_expectations.callee_audio_frames_expected_;
1510 }
1511 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001512 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001513 if (media_expectations.callee_video_expectation_ ==
1514 MediaExpectations::kExpectSomeFrames) {
1515 total_callee_video_frames_expected +=
1516 media_expectations.callee_video_frames_expected_;
1517 }
deadbeef1dcb1642017-03-29 21:08:16 -07001518
Seth Hampson2f0d7022018-02-20 11:54:42 -08001519 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001520 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001521 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001522 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001523 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001524 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001525 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001526 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001527 total_callee_video_frames_expected,
1528 kMaxWaitForFramesMs);
1529 bool expectations_correct =
1530 caller()->audio_frames_received() >=
1531 total_caller_audio_frames_expected &&
1532 caller()->min_video_frames_received_per_track() >=
1533 total_caller_video_frames_expected &&
1534 callee()->audio_frames_received() >=
1535 total_callee_audio_frames_expected &&
1536 callee()->min_video_frames_received_per_track() >=
1537 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001538
Seth Hampson2f0d7022018-02-20 11:54:42 -08001539 // After the combined wait, print out a more detailed message upon
1540 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001541 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001542 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001543 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001544 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001545 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001546 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001547 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001548 total_callee_video_frames_expected);
1549
1550 // We want to make sure nothing unexpected was received.
1551 if (media_expectations.caller_audio_expectation_ ==
1552 MediaExpectations::kExpectNoFrames) {
1553 EXPECT_EQ(caller()->audio_frames_received(),
1554 total_caller_audio_frames_expected);
1555 if (caller()->audio_frames_received() !=
1556 total_caller_audio_frames_expected) {
1557 expectations_correct = false;
1558 }
1559 }
1560 if (media_expectations.caller_video_expectation_ ==
1561 MediaExpectations::kExpectNoFrames) {
1562 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1563 total_caller_video_frames_expected);
1564 if (caller()->min_video_frames_received_per_track() !=
1565 total_caller_video_frames_expected) {
1566 expectations_correct = false;
1567 }
1568 }
1569 if (media_expectations.callee_audio_expectation_ ==
1570 MediaExpectations::kExpectNoFrames) {
1571 EXPECT_EQ(callee()->audio_frames_received(),
1572 total_callee_audio_frames_expected);
1573 if (callee()->audio_frames_received() !=
1574 total_callee_audio_frames_expected) {
1575 expectations_correct = false;
1576 }
1577 }
1578 if (media_expectations.callee_video_expectation_ ==
1579 MediaExpectations::kExpectNoFrames) {
1580 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1581 total_callee_video_frames_expected);
1582 if (callee()->min_video_frames_received_per_track() !=
1583 total_callee_video_frames_expected) {
1584 expectations_correct = false;
1585 }
1586 }
1587 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001588 }
1589
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001590 void TestNegotiatedCipherSuite(
1591 const PeerConnectionFactory::Options& caller_options,
1592 const PeerConnectionFactory::Options& callee_options,
1593 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001594 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1595 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001596 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001597 caller()->AddAudioVideoTracks();
1598 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001599 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001600 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001601 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001602 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001603 // TODO(bugs.webrtc.org/9456): Fix it.
1604 EXPECT_EQ(1, webrtc::metrics::NumEvents(
1605 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1606 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001607 }
1608
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001609 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1610 bool remote_gcm_enabled,
1611 int expected_cipher_suite) {
1612 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001613 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1614 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001615 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001616 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1617 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001618 TestNegotiatedCipherSuite(caller_options, callee_options,
1619 expected_cipher_suite);
1620 }
1621
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001623 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001624
deadbeef1dcb1642017-03-29 21:08:16 -07001625 private:
1626 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001627 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001628 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001629 // |network_thread_| and |worker_thread_| are used by both
1630 // |caller_| and |callee_| so they must be destroyed
1631 // later.
1632 std::unique_ptr<rtc::Thread> network_thread_;
1633 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001634 // The turn servers and turn customizers should be accessed & deleted on the
1635 // network thread to avoid a race with the socket read/write that occurs
1636 // on the network thread.
1637 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1638 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001639 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001640 std::unique_ptr<PeerConnectionWrapper> caller_;
1641 std::unique_ptr<PeerConnectionWrapper> callee_;
1642};
1643
Seth Hampson2f0d7022018-02-20 11:54:42 -08001644class PeerConnectionIntegrationTest
1645 : public PeerConnectionIntegrationBaseTest,
1646 public ::testing::WithParamInterface<SdpSemantics> {
1647 protected:
1648 PeerConnectionIntegrationTest()
1649 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1650};
1651
1652class PeerConnectionIntegrationTestPlanB
1653 : public PeerConnectionIntegrationBaseTest {
1654 protected:
1655 PeerConnectionIntegrationTestPlanB()
1656 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1657};
1658
1659class PeerConnectionIntegrationTestUnifiedPlan
1660 : public PeerConnectionIntegrationBaseTest {
1661 protected:
1662 PeerConnectionIntegrationTestUnifiedPlan()
1663 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1664};
1665
deadbeef1dcb1642017-03-29 21:08:16 -07001666// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1667// includes testing that the callback is invoked if an observer is connected
1668// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001669TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001670 RtpReceiverObserverOnFirstPacketReceived) {
1671 ASSERT_TRUE(CreatePeerConnectionWrappers());
1672 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001673 caller()->AddAudioVideoTracks();
1674 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001675 // Start offer/answer exchange and wait for it to complete.
1676 caller()->CreateAndSetAndSignalOffer();
1677 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1678 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001679 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1680 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001681 // Wait for all "first packet received" callbacks to be fired.
1682 EXPECT_TRUE_WAIT(
1683 std::all_of(caller()->rtp_receiver_observers().begin(),
1684 caller()->rtp_receiver_observers().end(),
1685 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1686 return o->first_packet_received();
1687 }),
1688 kMaxWaitForFramesMs);
1689 EXPECT_TRUE_WAIT(
1690 std::all_of(callee()->rtp_receiver_observers().begin(),
1691 callee()->rtp_receiver_observers().end(),
1692 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1693 return o->first_packet_received();
1694 }),
1695 kMaxWaitForFramesMs);
1696 // If new observers are set after the first packet was already received, the
1697 // callback should still be invoked.
1698 caller()->ResetRtpReceiverObservers();
1699 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001700 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1701 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001702 EXPECT_TRUE(
1703 std::all_of(caller()->rtp_receiver_observers().begin(),
1704 caller()->rtp_receiver_observers().end(),
1705 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1706 return o->first_packet_received();
1707 }));
1708 EXPECT_TRUE(
1709 std::all_of(callee()->rtp_receiver_observers().begin(),
1710 callee()->rtp_receiver_observers().end(),
1711 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1712 return o->first_packet_received();
1713 }));
1714}
1715
1716class DummyDtmfObserver : public DtmfSenderObserverInterface {
1717 public:
1718 DummyDtmfObserver() : completed_(false) {}
1719
1720 // Implements DtmfSenderObserverInterface.
1721 void OnToneChange(const std::string& tone) override {
1722 tones_.push_back(tone);
1723 if (tone.empty()) {
1724 completed_ = true;
1725 }
1726 }
1727
1728 const std::vector<std::string>& tones() const { return tones_; }
1729 bool completed() const { return completed_; }
1730
1731 private:
1732 bool completed_;
1733 std::vector<std::string> tones_;
1734};
1735
1736// Assumes |sender| already has an audio track added and the offer/answer
1737// exchange is done.
1738void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1739 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001740 // We should be able to get a DTMF sender from the local sender.
1741 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1742 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1743 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001744 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001745 dtmf_sender->RegisterObserver(&observer);
1746
1747 // Test the DtmfSender object just created.
1748 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1749 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1750
1751 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1752 std::vector<std::string> tones = {"1", "a", ""};
1753 EXPECT_EQ(tones, observer.tones());
1754 dtmf_sender->UnregisterObserver();
1755 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1756}
1757
1758// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1759// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001760TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001761 ASSERT_TRUE(CreatePeerConnectionWrappers());
1762 ConnectFakeSignaling();
1763 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001764 caller()->AddAudioTrack();
1765 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001766 caller()->CreateAndSetAndSignalOffer();
1767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001768 // DTLS must finish before the DTMF sender can be used reliably.
1769 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001770 TestDtmfFromSenderToReceiver(caller(), callee());
1771 TestDtmfFromSenderToReceiver(callee(), caller());
1772}
1773
1774// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1775// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001777 ASSERT_TRUE(CreatePeerConnectionWrappers());
1778 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001779
deadbeef1dcb1642017-03-29 21:08:16 -07001780 // Do normal offer/answer and wait for some frames to be received in each
1781 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001782 caller()->AddAudioVideoTracks();
1783 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001784 caller()->CreateAndSetAndSignalOffer();
1785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001786 MediaExpectations media_expectations;
1787 media_expectations.ExpectBidirectionalAudioAndVideo();
1788 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001789 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1790 webrtc::kEnumCounterKeyProtocolDtls));
1791 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1792 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001793}
1794
1795// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001796TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001797 PeerConnectionInterface::RTCConfiguration sdes_config;
1798 sdes_config.enable_dtls_srtp.emplace(false);
1799 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1800 ConnectFakeSignaling();
1801
1802 // Do normal offer/answer and wait for some frames to be received in each
1803 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001804 caller()->AddAudioVideoTracks();
1805 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001806 caller()->CreateAndSetAndSignalOffer();
1807 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001808 MediaExpectations media_expectations;
1809 media_expectations.ExpectBidirectionalAudioAndVideo();
1810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001811 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1812 webrtc::kEnumCounterKeyProtocolSdes));
1813 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1814 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001815}
1816
Steve Anton8c0f7a72017-10-03 10:03:10 -07001817// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1818// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001819TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001820 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1821 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1822 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1823 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1824 return pc->GetRemoteAudioSSLCertificate();
1825 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001826 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1827 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1828 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1829 return pc->GetRemoteAudioSSLCertChain();
1830 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001831
1832 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1833 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1834
1835 // Configure each side with a known certificate so they can be compared later.
1836 PeerConnectionInterface::RTCConfiguration caller_config;
1837 caller_config.enable_dtls_srtp.emplace(true);
1838 caller_config.certificates.push_back(caller_cert);
1839 PeerConnectionInterface::RTCConfiguration callee_config;
1840 callee_config.enable_dtls_srtp.emplace(true);
1841 callee_config.certificates.push_back(callee_cert);
1842 ASSERT_TRUE(
1843 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1844 ConnectFakeSignaling();
1845
1846 // When first initialized, there should not be a remote SSL certificate (and
1847 // calling this method should not crash).
1848 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1849 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001850 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1851 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001852
Steve Anton15324772018-01-16 10:26:49 -08001853 caller()->AddAudioTrack();
1854 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001855 caller()->CreateAndSetAndSignalOffer();
1856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1857 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1858
1859 // Once DTLS has been connected, each side should return the other's SSL
1860 // certificate when calling GetRemoteAudioSSLCertificate.
1861
1862 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1863 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001864 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001865 caller_remote_cert->ToPEMString());
1866
1867 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1868 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001869 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001870 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001871
1872 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1873 ASSERT_TRUE(caller_remote_cert_chain);
1874 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1875 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001876 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001877 remote_cert->ToPEMString());
1878
1879 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1880 ASSERT_TRUE(callee_remote_cert_chain);
1881 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1882 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001883 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001884 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001885}
1886
deadbeef1dcb1642017-03-29 21:08:16 -07001887// This test sets up a call between two parties with a source resolution of
1888// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001889TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001890 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1891 ASSERT_TRUE(CreatePeerConnectionWrappers());
1892 ConnectFakeSignaling();
1893
Niels Möller5c7efe72018-05-11 10:34:46 +02001894 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1895 webrtc::FakePeriodicVideoSource::Config config;
1896 config.width = 1280;
1897 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001898 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001899 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1900 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001901
1902 // Do normal offer/answer and wait for at least one frame to be received in
1903 // each direction.
1904 caller()->CreateAndSetAndSignalOffer();
1905 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1906 callee()->min_video_frames_received_per_track() > 0,
1907 kMaxWaitForFramesMs);
1908
1909 // Check rendered aspect ratio.
1910 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1911 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1912 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1913 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1914}
1915
1916// This test sets up an one-way call, with media only from caller to
1917// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001918TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001919 ASSERT_TRUE(CreatePeerConnectionWrappers());
1920 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001921 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001922 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923 MediaExpectations media_expectations;
1924 media_expectations.CalleeExpectsSomeAudioAndVideo();
1925 media_expectations.CallerExpectsNoAudio();
1926 media_expectations.CallerExpectsNoVideo();
1927 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001928}
1929
1930// This test sets up a audio call initially, with the callee rejecting video
1931// initially. Then later the callee decides to upgrade to audio/video, and
1932// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001933TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001934 ASSERT_TRUE(CreatePeerConnectionWrappers());
1935 ConnectFakeSignaling();
1936 // Initially, offer an audio/video stream from the caller, but refuse to
1937 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001938 caller()->AddAudioVideoTracks();
1939 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001940 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1941 PeerConnectionInterface::RTCOfferAnswerOptions options;
1942 options.offer_to_receive_video = 0;
1943 callee()->SetOfferAnswerOptions(options);
1944 } else {
1945 callee()->SetRemoteOfferHandler([this] {
1946 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1947 });
1948 }
deadbeef1dcb1642017-03-29 21:08:16 -07001949 // Do offer/answer and make sure audio is still received end-to-end.
1950 caller()->CreateAndSetAndSignalOffer();
1951 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001952 {
1953 MediaExpectations media_expectations;
1954 media_expectations.ExpectBidirectionalAudio();
1955 media_expectations.ExpectNoVideo();
1956 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1957 }
deadbeef1dcb1642017-03-29 21:08:16 -07001958 // Sanity check that the callee's description has a rejected video section.
1959 ASSERT_NE(nullptr, callee()->pc()->local_description());
1960 const ContentInfo* callee_video_content =
1961 GetFirstVideoContent(callee()->pc()->local_description()->description());
1962 ASSERT_NE(nullptr, callee_video_content);
1963 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001964
deadbeef1dcb1642017-03-29 21:08:16 -07001965 // Now negotiate with video and ensure negotiation succeeds, with video
1966 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001967 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1969 PeerConnectionInterface::RTCOfferAnswerOptions options;
1970 options.offer_to_receive_video = 1;
1971 callee()->SetOfferAnswerOptions(options);
1972 } else {
1973 callee()->SetRemoteOfferHandler(nullptr);
1974 caller()->SetRemoteOfferHandler([this] {
1975 // The caller creates a new transceiver to receive video on when receiving
1976 // the offer, but by default it is send only.
1977 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001978 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001979 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1980 transceivers[2]->receiver()->media_type());
1981 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1982 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1983 });
1984 }
deadbeef1dcb1642017-03-29 21:08:16 -07001985 callee()->CreateAndSetAndSignalOffer();
1986 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001987 {
1988 // Expect additional audio frames to be received after the upgrade.
1989 MediaExpectations media_expectations;
1990 media_expectations.ExpectBidirectionalAudioAndVideo();
1991 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1992 }
deadbeef1dcb1642017-03-29 21:08:16 -07001993}
1994
deadbeef4389b4d2017-09-07 09:07:36 -07001995// Simpler than the above test; just add an audio track to an established
1996// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001997TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001998 ASSERT_TRUE(CreatePeerConnectionWrappers());
1999 ConnectFakeSignaling();
2000 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002001 caller()->AddVideoTrack();
2002 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002003 caller()->CreateAndSetAndSignalOffer();
2004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2005 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002006 caller()->AddAudioTrack();
2007 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002008 caller()->CreateAndSetAndSignalOffer();
2009 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2010 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002011 MediaExpectations media_expectations;
2012 media_expectations.ExpectBidirectionalAudioAndVideo();
2013 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002014}
2015
deadbeef1dcb1642017-03-29 21:08:16 -07002016// This test sets up a call that's transferred to a new caller with a different
2017// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002018TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002019 ASSERT_TRUE(CreatePeerConnectionWrappers());
2020 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002021 caller()->AddAudioVideoTracks();
2022 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002023 caller()->CreateAndSetAndSignalOffer();
2024 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2025
2026 // Keep the original peer around which will still send packets to the
2027 // receiving client. These SRTP packets will be dropped.
2028 std::unique_ptr<PeerConnectionWrapper> original_peer(
2029 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002030 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002031 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2032 // directly above.
2033 original_peer->pc()->Close();
2034
2035 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002036 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002037 caller()->CreateAndSetAndSignalOffer();
2038 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2039 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002040 MediaExpectations media_expectations;
2041 media_expectations.ExpectBidirectionalAudioAndVideo();
2042 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002043}
2044
2045// This test sets up a call that's transferred to a new callee with a different
2046// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002047TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002048 ASSERT_TRUE(CreatePeerConnectionWrappers());
2049 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002050 caller()->AddAudioVideoTracks();
2051 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002052 caller()->CreateAndSetAndSignalOffer();
2053 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2054
2055 // Keep the original peer around which will still send packets to the
2056 // receiving client. These SRTP packets will be dropped.
2057 std::unique_ptr<PeerConnectionWrapper> original_peer(
2058 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002060 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2061 // directly above.
2062 original_peer->pc()->Close();
2063
2064 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002065 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002066 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2067 caller()->CreateAndSetAndSignalOffer();
2068 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2069 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002070 MediaExpectations media_expectations;
2071 media_expectations.ExpectBidirectionalAudioAndVideo();
2072 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002073}
2074
2075// This test sets up a non-bundled call and negotiates bundling at the same
2076// time as starting an ICE restart. When bundling is in effect in the restart,
2077// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002078TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002079 ASSERT_TRUE(CreatePeerConnectionWrappers());
2080 ConnectFakeSignaling();
2081
Steve Anton15324772018-01-16 10:26:49 -08002082 caller()->AddAudioVideoTracks();
2083 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002084 // Remove the bundle group from the SDP received by the callee.
2085 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2086 desc->RemoveGroupByName("BUNDLE");
2087 });
2088 caller()->CreateAndSetAndSignalOffer();
2089 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002090 {
2091 MediaExpectations media_expectations;
2092 media_expectations.ExpectBidirectionalAudioAndVideo();
2093 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2094 }
deadbeef1dcb1642017-03-29 21:08:16 -07002095 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2096 callee()->SetReceivedSdpMunger(nullptr);
2097 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2098 caller()->CreateAndSetAndSignalOffer();
2099 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2100
2101 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002102 {
2103 MediaExpectations media_expectations;
2104 media_expectations.ExpectBidirectionalAudioAndVideo();
2105 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2106 }
deadbeef1dcb1642017-03-29 21:08:16 -07002107}
2108
2109// Test CVO (Coordination of Video Orientation). If a video source is rotated
2110// and both peers support the CVO RTP header extension, the actual video frames
2111// don't need to be encoded in different resolutions, since the rotation is
2112// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002113TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
2116 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002117 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002118 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002119 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002120 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2121
2122 // Wait for video frames to be received by both sides.
2123 caller()->CreateAndSetAndSignalOffer();
2124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2125 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2126 callee()->min_video_frames_received_per_track() > 0,
2127 kMaxWaitForFramesMs);
2128
2129 // Ensure that the aspect ratio is unmodified.
2130 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2131 // not just assumed.
2132 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2133 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2134 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2135 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2136 // Ensure that the CVO bits were surfaced to the renderer.
2137 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2138 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2139}
2140
2141// Test that when the CVO extension isn't supported, video is rotated the
2142// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002143TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002144 ASSERT_TRUE(CreatePeerConnectionWrappers());
2145 ConnectFakeSignaling();
2146 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002147 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002148 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002149 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002150 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2151
2152 // Remove the CVO extension from the offered SDP.
2153 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2154 cricket::VideoContentDescription* video =
2155 GetFirstVideoContentDescription(desc);
2156 video->ClearRtpHeaderExtensions();
2157 });
2158 // Wait for video frames to be received by both sides.
2159 caller()->CreateAndSetAndSignalOffer();
2160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2161 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2162 callee()->min_video_frames_received_per_track() > 0,
2163 kMaxWaitForFramesMs);
2164
2165 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2166 // rotation.
2167 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2168 // not just assumed.
2169 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2170 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2171 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2172 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2173 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2174 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2175 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2176}
2177
deadbeef1dcb1642017-03-29 21:08:16 -07002178// Test that if the answerer rejects the audio m= section, no audio is sent or
2179// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002180TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002181 ASSERT_TRUE(CreatePeerConnectionWrappers());
2182 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002183 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002184 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2185 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2186 // it will reject the audio m= section completely.
2187 PeerConnectionInterface::RTCOfferAnswerOptions options;
2188 options.offer_to_receive_audio = 0;
2189 callee()->SetOfferAnswerOptions(options);
2190 } else {
2191 // Stopping the audio RtpTransceiver will cause the media section to be
2192 // rejected in the answer.
2193 callee()->SetRemoteOfferHandler([this] {
2194 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2195 });
2196 }
Steve Anton15324772018-01-16 10:26:49 -08002197 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002198 // Do offer/answer and wait for successful end-to-end video frames.
2199 caller()->CreateAndSetAndSignalOffer();
2200 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002201 MediaExpectations media_expectations;
2202 media_expectations.ExpectBidirectionalVideo();
2203 media_expectations.ExpectNoAudio();
2204 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2205
deadbeef1dcb1642017-03-29 21:08:16 -07002206 // Sanity check that the callee's description has a rejected audio section.
2207 ASSERT_NE(nullptr, callee()->pc()->local_description());
2208 const ContentInfo* callee_audio_content =
2209 GetFirstAudioContent(callee()->pc()->local_description()->description());
2210 ASSERT_NE(nullptr, callee_audio_content);
2211 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002212 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2213 // The caller's transceiver should have stopped after receiving the answer.
2214 EXPECT_TRUE(caller()
2215 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2216 ->stopped());
2217 }
deadbeef1dcb1642017-03-29 21:08:16 -07002218}
2219
2220// Test that if the answerer rejects the video m= section, no video is sent or
2221// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002222TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002223 ASSERT_TRUE(CreatePeerConnectionWrappers());
2224 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002225 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002226 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2227 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2228 // it will reject the video m= section completely.
2229 PeerConnectionInterface::RTCOfferAnswerOptions options;
2230 options.offer_to_receive_video = 0;
2231 callee()->SetOfferAnswerOptions(options);
2232 } else {
2233 // Stopping the video RtpTransceiver will cause the media section to be
2234 // rejected in the answer.
2235 callee()->SetRemoteOfferHandler([this] {
2236 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2237 });
2238 }
Steve Anton15324772018-01-16 10:26:49 -08002239 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002240 // Do offer/answer and wait for successful end-to-end audio frames.
2241 caller()->CreateAndSetAndSignalOffer();
2242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002243 MediaExpectations media_expectations;
2244 media_expectations.ExpectBidirectionalAudio();
2245 media_expectations.ExpectNoVideo();
2246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2247
deadbeef1dcb1642017-03-29 21:08:16 -07002248 // Sanity check that the callee's description has a rejected video section.
2249 ASSERT_NE(nullptr, callee()->pc()->local_description());
2250 const ContentInfo* callee_video_content =
2251 GetFirstVideoContent(callee()->pc()->local_description()->description());
2252 ASSERT_NE(nullptr, callee_video_content);
2253 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002254 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2255 // The caller's transceiver should have stopped after receiving the answer.
2256 EXPECT_TRUE(caller()
2257 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2258 ->stopped());
2259 }
deadbeef1dcb1642017-03-29 21:08:16 -07002260}
2261
2262// Test that if the answerer rejects both audio and video m= sections, nothing
2263// bad happens.
2264// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2265// test anything but the fact that negotiation succeeds, which doesn't mean
2266// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002267TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002268 ASSERT_TRUE(CreatePeerConnectionWrappers());
2269 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002270 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002271 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2272 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2273 // will reject both audio and video m= sections.
2274 PeerConnectionInterface::RTCOfferAnswerOptions options;
2275 options.offer_to_receive_audio = 0;
2276 options.offer_to_receive_video = 0;
2277 callee()->SetOfferAnswerOptions(options);
2278 } else {
2279 callee()->SetRemoteOfferHandler([this] {
2280 // Stopping all transceivers will cause all media sections to be rejected.
2281 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2282 transceiver->Stop();
2283 }
2284 });
2285 }
deadbeef1dcb1642017-03-29 21:08:16 -07002286 // Do offer/answer and wait for stable signaling state.
2287 caller()->CreateAndSetAndSignalOffer();
2288 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002289
deadbeef1dcb1642017-03-29 21:08:16 -07002290 // Sanity check that the callee's description has rejected m= sections.
2291 ASSERT_NE(nullptr, callee()->pc()->local_description());
2292 const ContentInfo* callee_audio_content =
2293 GetFirstAudioContent(callee()->pc()->local_description()->description());
2294 ASSERT_NE(nullptr, callee_audio_content);
2295 EXPECT_TRUE(callee_audio_content->rejected);
2296 const ContentInfo* callee_video_content =
2297 GetFirstVideoContent(callee()->pc()->local_description()->description());
2298 ASSERT_NE(nullptr, callee_video_content);
2299 EXPECT_TRUE(callee_video_content->rejected);
2300}
2301
2302// This test sets up an audio and video call between two parties. After the
2303// call runs for a while, the caller sends an updated offer with video being
2304// rejected. Once the re-negotiation is done, the video flow should stop and
2305// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002306TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002307 ASSERT_TRUE(CreatePeerConnectionWrappers());
2308 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002309 caller()->AddAudioVideoTracks();
2310 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002311 caller()->CreateAndSetAndSignalOffer();
2312 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002313 {
2314 MediaExpectations media_expectations;
2315 media_expectations.ExpectBidirectionalAudioAndVideo();
2316 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2317 }
deadbeef1dcb1642017-03-29 21:08:16 -07002318 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002319 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2320 caller()->SetGeneratedSdpMunger(
2321 [](cricket::SessionDescription* description) {
2322 for (cricket::ContentInfo& content : description->contents()) {
2323 if (cricket::IsVideoContent(&content)) {
2324 content.rejected = true;
2325 }
2326 }
2327 });
2328 } else {
2329 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2330 }
deadbeef1dcb1642017-03-29 21:08:16 -07002331 caller()->CreateAndSetAndSignalOffer();
2332 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2333
2334 // Sanity check that the caller's description has a rejected video section.
2335 ASSERT_NE(nullptr, caller()->pc()->local_description());
2336 const ContentInfo* caller_video_content =
2337 GetFirstVideoContent(caller()->pc()->local_description()->description());
2338 ASSERT_NE(nullptr, caller_video_content);
2339 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002340 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002341 {
2342 MediaExpectations media_expectations;
2343 media_expectations.ExpectBidirectionalAudio();
2344 media_expectations.ExpectNoVideo();
2345 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2346 }
deadbeef1dcb1642017-03-29 21:08:16 -07002347}
2348
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002349// Do one offer/answer with audio, another that disables it (rejecting the m=
2350// section), and another that re-enables it. Regression test for:
2351// bugs.webrtc.org/6023
2352TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2353 ASSERT_TRUE(CreatePeerConnectionWrappers());
2354 ConnectFakeSignaling();
2355
2356 // Add audio track, do normal offer/answer.
2357 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2358 caller()->CreateLocalAudioTrack();
2359 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2360 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2361 caller()->CreateAndSetAndSignalOffer();
2362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2363
2364 // Remove audio track, and set offer_to_receive_audio to false to cause the
2365 // m= section to be completely disabled, not just "recvonly".
2366 caller()->pc()->RemoveTrack(sender);
2367 PeerConnectionInterface::RTCOfferAnswerOptions options;
2368 options.offer_to_receive_audio = 0;
2369 caller()->SetOfferAnswerOptions(options);
2370 caller()->CreateAndSetAndSignalOffer();
2371 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2372
2373 // Add the audio track again, expecting negotiation to succeed and frames to
2374 // flow.
2375 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2376 options.offer_to_receive_audio = 1;
2377 caller()->SetOfferAnswerOptions(options);
2378 caller()->CreateAndSetAndSignalOffer();
2379 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2380
2381 MediaExpectations media_expectations;
2382 media_expectations.CalleeExpectsSomeAudio();
2383 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2384}
2385
deadbeef1dcb1642017-03-29 21:08:16 -07002386// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2387// is needed to support legacy endpoints.
2388// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2389// add a test for an end-to-end test without MID signaling either (basically,
2390// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002391TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002392 ASSERT_TRUE(CreatePeerConnectionWrappers());
2393 ConnectFakeSignaling();
2394 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002395 caller()->AddAudioVideoTracks();
2396 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002397 // Remove SSRCs and MSIDs from the received offer SDP.
2398 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002399 caller()->CreateAndSetAndSignalOffer();
2400 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002401 MediaExpectations media_expectations;
2402 media_expectations.ExpectBidirectionalAudioAndVideo();
2403 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002404}
2405
Seth Hampson5897a6e2018-04-03 11:16:33 -07002406// Basic end-to-end test, without SSRC signaling. This means that the track
2407// was created properly and frames are delivered when the MSIDs are communicated
2408// with a=msid lines and no a=ssrc lines.
2409TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2410 EndToEndCallWithoutSsrcSignaling) {
2411 const char kStreamId[] = "streamId";
2412 ASSERT_TRUE(CreatePeerConnectionWrappers());
2413 ConnectFakeSignaling();
2414 // Add just audio tracks.
2415 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2416 callee()->AddAudioTrack();
2417
2418 // Remove SSRCs from the received offer SDP.
2419 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2420 caller()->CreateAndSetAndSignalOffer();
2421 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2422 MediaExpectations media_expectations;
2423 media_expectations.ExpectBidirectionalAudio();
2424 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2425}
2426
Steve Antondf527fd2018-04-27 15:52:03 -07002427// Tests that video flows between multiple video tracks when SSRCs are not
2428// signaled. This exercises the MID RTP header extension which is needed to
2429// demux the incoming video tracks.
2430TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2431 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2432 ASSERT_TRUE(CreatePeerConnectionWrappers());
2433 ConnectFakeSignaling();
2434 caller()->AddVideoTrack();
2435 caller()->AddVideoTrack();
2436 callee()->AddVideoTrack();
2437 callee()->AddVideoTrack();
2438
2439 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2440 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2441 caller()->CreateAndSetAndSignalOffer();
2442 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2443 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2444 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2445
2446 // Expect video to be received in both directions on both tracks.
2447 MediaExpectations media_expectations;
2448 media_expectations.ExpectBidirectionalVideo();
2449 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2450}
2451
deadbeef1dcb1642017-03-29 21:08:16 -07002452// Test that if two video tracks are sent (from caller to callee, in this test),
2453// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002454TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002455 ASSERT_TRUE(CreatePeerConnectionWrappers());
2456 ConnectFakeSignaling();
2457 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002458 caller()->AddAudioVideoTracks();
2459 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002460 caller()->CreateAndSetAndSignalOffer();
2461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002462 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002463
2464 MediaExpectations media_expectations;
2465 media_expectations.CalleeExpectsSomeAudioAndVideo();
2466 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002467}
2468
2469static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2470 bool first = true;
2471 for (cricket::ContentInfo& content : desc->contents()) {
2472 if (first) {
2473 first = false;
2474 continue;
2475 }
2476 content.bundle_only = true;
2477 }
2478 first = true;
2479 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2480 if (first) {
2481 first = false;
2482 continue;
2483 }
2484 transport.description.ice_ufrag.clear();
2485 transport.description.ice_pwd.clear();
2486 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2487 transport.description.identity_fingerprint.reset(nullptr);
2488 }
2489}
2490
2491// Test that if applying a true "max bundle" offer, which uses ports of 0,
2492// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2493// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2494// successfully and media flows.
2495// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2496// TODO(deadbeef): Won't need this test once we start generating actual
2497// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002498TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002499 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2500 ASSERT_TRUE(CreatePeerConnectionWrappers());
2501 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002502 caller()->AddAudioVideoTracks();
2503 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002504 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2505 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2506 // but the first m= section.
2507 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2508 caller()->CreateAndSetAndSignalOffer();
2509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002510 MediaExpectations media_expectations;
2511 media_expectations.ExpectBidirectionalAudioAndVideo();
2512 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002513}
2514
2515// Test that we can receive the audio output level from a remote audio track.
2516// TODO(deadbeef): Use a fake audio source and verify that the output level is
2517// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002518TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002519 ASSERT_TRUE(CreatePeerConnectionWrappers());
2520 ConnectFakeSignaling();
2521 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002522 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002523 caller()->CreateAndSetAndSignalOffer();
2524 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2525
2526 // Get the audio output level stats. Note that the level is not available
2527 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002528 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002529 kMaxWaitForFramesMs);
2530}
2531
2532// Test that an audio input level is reported.
2533// TODO(deadbeef): Use a fake audio source and verify that the input level is
2534// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002535TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002536 ASSERT_TRUE(CreatePeerConnectionWrappers());
2537 ConnectFakeSignaling();
2538 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002539 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002540 caller()->CreateAndSetAndSignalOffer();
2541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2542
2543 // Get the audio input level stats. The level should be available very
2544 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002545 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002546 kMaxWaitForStatsMs);
2547}
2548
2549// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002550TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002551 ASSERT_TRUE(CreatePeerConnectionWrappers());
2552 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002553 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002554 // Do offer/answer, wait for the callee to receive some frames.
2555 caller()->CreateAndSetAndSignalOffer();
2556 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002557
2558 MediaExpectations media_expectations;
2559 media_expectations.CalleeExpectsSomeAudioAndVideo();
2560 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002561
2562 // Get a handle to the remote tracks created, so they can be used as GetStats
2563 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002564 for (auto receiver : callee()->pc()->GetReceivers()) {
2565 // We received frames, so we definitely should have nonzero "received bytes"
2566 // stats at this point.
2567 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2568 0);
2569 }
deadbeef1dcb1642017-03-29 21:08:16 -07002570}
2571
2572// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002573TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002574 ASSERT_TRUE(CreatePeerConnectionWrappers());
2575 ConnectFakeSignaling();
2576 auto audio_track = caller()->CreateLocalAudioTrack();
2577 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002578 caller()->AddTrack(audio_track);
2579 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002580 // Do offer/answer, wait for the callee to receive some frames.
2581 caller()->CreateAndSetAndSignalOffer();
2582 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002583 MediaExpectations media_expectations;
2584 media_expectations.CalleeExpectsSomeAudioAndVideo();
2585 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002586
2587 // The callee received frames, so we definitely should have nonzero "sent
2588 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002589 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2590 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2591}
2592
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002593// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002595 ASSERT_TRUE(CreatePeerConnectionWrappers());
2596 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002597 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002598
Steve Anton15324772018-01-16 10:26:49 -08002599 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002600
2601 // Do offer/answer, wait for the callee to receive some frames.
2602 caller()->CreateAndSetAndSignalOffer();
2603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2604
2605 // Get the remote audio track created on the receiver, so they can be used as
2606 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002607 auto receivers = callee()->pc()->GetReceivers();
2608 ASSERT_EQ(1u, receivers.size());
2609 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002610
2611 // Get the audio output level stats. Note that the level is not available
2612 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002613 EXPECT_TRUE_WAIT(
2614 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2615 0,
2616 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002617}
2618
deadbeefd8ad7882017-04-18 16:01:17 -07002619// Test that we can get stats (using the new stats implemnetation) for
2620// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2621// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002622TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002623 GetStatsForUnsignaledStreamWithNewStatsApi) {
2624 ASSERT_TRUE(CreatePeerConnectionWrappers());
2625 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002626 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002627 // Remove SSRCs and MSIDs from the received offer SDP.
2628 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2629 caller()->CreateAndSetAndSignalOffer();
2630 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002631 MediaExpectations media_expectations;
2632 media_expectations.CalleeExpectsSomeAudio(1);
2633 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002634
2635 // We received a frame, so we should have nonzero "bytes received" stats for
2636 // the unsignaled stream, if stats are working for it.
2637 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2638 callee()->NewGetStats();
2639 ASSERT_NE(nullptr, report);
2640 auto inbound_stream_stats =
2641 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2642 ASSERT_EQ(1U, inbound_stream_stats.size());
2643 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2644 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002645 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2646}
2647
Taylor Brandstettera4653442018-06-19 09:44:26 -07002648// Same as above but for the legacy stats implementation.
2649TEST_P(PeerConnectionIntegrationTest,
2650 GetStatsForUnsignaledStreamWithOldStatsApi) {
2651 ASSERT_TRUE(CreatePeerConnectionWrappers());
2652 ConnectFakeSignaling();
2653 caller()->AddAudioTrack();
2654 // Remove SSRCs and MSIDs from the received offer SDP.
2655 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2656 caller()->CreateAndSetAndSignalOffer();
2657 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2658
2659 // Note that, since the old stats implementation associates SSRCs with tracks
2660 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2661 // associated track ID. So we can't use the track "selector" argument.
2662 //
2663 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2664 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002665 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002666 kDefaultTimeout);
2667}
2668
zhihuangf8164932017-05-19 13:09:47 -07002669// Test that we can successfully get the media related stats (audio level
2670// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002671TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002672 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2673 ASSERT_TRUE(CreatePeerConnectionWrappers());
2674 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002675 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002676 // Remove SSRCs and MSIDs from the received offer SDP.
2677 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2678 caller()->CreateAndSetAndSignalOffer();
2679 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002680 MediaExpectations media_expectations;
2681 media_expectations.CalleeExpectsSomeAudio(1);
2682 media_expectations.CalleeExpectsSomeVideo(1);
2683 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002684
2685 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2686 callee()->NewGetStats();
2687 ASSERT_NE(nullptr, report);
2688
2689 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2690 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2691 ASSERT_GE(audio_index, 0);
2692 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002693}
2694
deadbeef4e2deab2017-09-20 13:56:21 -07002695// Helper for test below.
2696void ModifySsrcs(cricket::SessionDescription* desc) {
2697 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002698 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002699 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002700 for (uint32_t& ssrc : stream.ssrcs) {
2701 ssrc = rtc::CreateRandomId();
2702 }
2703 }
2704 }
2705}
2706
2707// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2708// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2709// This should result in two "RTCInboundRTPStreamStats", but only one
2710// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2711// being reset to 0 once the SSRC change occurs.
2712//
2713// Regression test for this bug:
2714// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2715//
2716// The bug causes the track stats to only represent one of the two streams:
2717// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2718// that the track stat counters would reset to 0 when the new stream is
2719// received, and a 50% chance that they'll stop updating (while
2720// "concealed_samples" continues increasing, due to silence being generated for
2721// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002722TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002723 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002724 ASSERT_TRUE(CreatePeerConnectionWrappers());
2725 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002726 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002727 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2728 // that doesn't signal SSRCs (from the callee's perspective).
2729 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2730 caller()->CreateAndSetAndSignalOffer();
2731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2732 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002733 {
2734 MediaExpectations media_expectations;
2735 media_expectations.CalleeExpectsSomeAudio(50);
2736 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2737 }
deadbeef4e2deab2017-09-20 13:56:21 -07002738 // Some audio frames were received, so we should have nonzero "samples
2739 // received" for the track.
2740 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2741 callee()->NewGetStats();
2742 ASSERT_NE(nullptr, report);
2743 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2744 ASSERT_EQ(1U, track_stats.size());
2745 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2746 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2747 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2748
2749 // Create a new offer and munge it to cause the caller to use a new SSRC.
2750 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2751 caller()->CreateAndSetAndSignalOffer();
2752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2753 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2754 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002755 {
2756 MediaExpectations media_expectations;
2757 media_expectations.CalleeExpectsSomeAudio(25);
2758 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2759 }
deadbeef4e2deab2017-09-20 13:56:21 -07002760
2761 report = callee()->NewGetStats();
2762 ASSERT_NE(nullptr, report);
2763 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2764 ASSERT_EQ(1U, track_stats.size());
2765 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2766 // The "total samples received" stat should only be greater than it was
2767 // before.
2768 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2769 // Right now, the new SSRC will cause the counters to reset to 0.
2770 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2771
2772 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002773 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002774 // good sign that we're seeing stats from the old stream that's no longer
2775 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002776 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002777 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2778 EXPECT_LT(*track_stats[0]->concealed_samples,
2779 *track_stats[0]->total_samples_received *
2780 kAcceptableConcealedSamplesPercentage);
2781
2782 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2783 // sanity check that the SSRC really changed.
2784 // TODO(deadbeef): This isn't working right now, because we're not returning
2785 // *any* stats for the inactive stream. Uncomment when the bug is completely
2786 // fixed.
2787 // auto inbound_stream_stats =
2788 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2789 // ASSERT_EQ(2U, inbound_stream_stats.size());
2790}
2791
deadbeef1dcb1642017-03-29 21:08:16 -07002792// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002793TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002794 PeerConnectionFactory::Options dtls_10_options;
2795 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2796 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2797 dtls_10_options));
2798 ConnectFakeSignaling();
2799 // Do normal offer/answer and wait for some frames to be received in each
2800 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002801 caller()->AddAudioVideoTracks();
2802 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002803 caller()->CreateAndSetAndSignalOffer();
2804 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002805 MediaExpectations media_expectations;
2806 media_expectations.ExpectBidirectionalAudioAndVideo();
2807 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002808}
2809
2810// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002811TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002812 PeerConnectionFactory::Options dtls_10_options;
2813 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2814 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2815 dtls_10_options));
2816 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002817 caller()->AddAudioVideoTracks();
2818 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002819 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002820 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002821 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002822 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002823 kDefaultTimeout);
2824 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002825 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002826 // TODO(bugs.webrtc.org/9456): Fix it.
2827 EXPECT_EQ(1, webrtc::metrics::NumEvents(
2828 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2829 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002830}
2831
2832// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002833TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002834 PeerConnectionFactory::Options dtls_12_options;
2835 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2836 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2837 dtls_12_options));
2838 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002839 caller()->AddAudioVideoTracks();
2840 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002841 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002842 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002843 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002844 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002845 kDefaultTimeout);
2846 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002847 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002848 // TODO(bugs.webrtc.org/9456): Fix it.
2849 EXPECT_EQ(1, webrtc::metrics::NumEvents(
2850 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2851 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002852}
2853
2854// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2855// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002856TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002857 PeerConnectionFactory::Options caller_options;
2858 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2859 PeerConnectionFactory::Options callee_options;
2860 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2861 ASSERT_TRUE(
2862 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2863 ConnectFakeSignaling();
2864 // Do normal offer/answer and wait for some frames to be received in each
2865 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002866 caller()->AddAudioVideoTracks();
2867 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002868 caller()->CreateAndSetAndSignalOffer();
2869 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002870 MediaExpectations media_expectations;
2871 media_expectations.ExpectBidirectionalAudioAndVideo();
2872 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002873}
2874
2875// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2876// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002877TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002878 PeerConnectionFactory::Options caller_options;
2879 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2880 PeerConnectionFactory::Options callee_options;
2881 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2882 ASSERT_TRUE(
2883 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2884 ConnectFakeSignaling();
2885 // Do normal offer/answer and wait for some frames to be received in each
2886 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002887 caller()->AddAudioVideoTracks();
2888 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002889 caller()->CreateAndSetAndSignalOffer();
2890 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002891 MediaExpectations media_expectations;
2892 media_expectations.ExpectBidirectionalAudioAndVideo();
2893 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002894}
2895
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002896// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2897// works as expected; the cipher should only be used if enabled by both sides.
2898TEST_P(PeerConnectionIntegrationTest,
2899 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2900 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002901 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002902 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002903 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2904 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002905 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2906 TestNegotiatedCipherSuite(caller_options, callee_options,
2907 expected_cipher_suite);
2908}
2909
2910TEST_P(PeerConnectionIntegrationTest,
2911 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2912 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002913 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2914 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002915 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002916 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002917 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2918 TestNegotiatedCipherSuite(caller_options, callee_options,
2919 expected_cipher_suite);
2920}
2921
2922TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2923 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002924 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002925 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002926 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002927 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2928 TestNegotiatedCipherSuite(caller_options, callee_options,
2929 expected_cipher_suite);
2930}
2931
deadbeef1dcb1642017-03-29 21:08:16 -07002932// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002933TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002934 bool local_gcm_enabled = false;
2935 bool remote_gcm_enabled = false;
2936 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2937 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2938 expected_cipher_suite);
2939}
2940
2941// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002942TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002943 bool local_gcm_enabled = true;
2944 bool remote_gcm_enabled = true;
2945 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2946 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2947 expected_cipher_suite);
2948}
2949
2950// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002951TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002952 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2953 bool local_gcm_enabled = true;
2954 bool remote_gcm_enabled = false;
2955 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2956 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2957 expected_cipher_suite);
2958}
2959
2960// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002961TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002962 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2963 bool local_gcm_enabled = false;
2964 bool remote_gcm_enabled = true;
2965 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2966 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2967 expected_cipher_suite);
2968}
2969
deadbeef7914b8c2017-04-21 03:23:33 -07002970// Verify that media can be transmitted end-to-end when GCM crypto suites are
2971// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2972// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2973// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002974TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002975 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002976 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07002977 ASSERT_TRUE(
2978 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2979 ConnectFakeSignaling();
2980 // Do normal offer/answer and wait for some frames to be received in each
2981 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002982 caller()->AddAudioVideoTracks();
2983 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002984 caller()->CreateAndSetAndSignalOffer();
2985 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002986 MediaExpectations media_expectations;
2987 media_expectations.ExpectBidirectionalAudioAndVideo();
2988 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002989}
2990
deadbeef1dcb1642017-03-29 21:08:16 -07002991// This test sets up a call between two parties with audio, video and an RTP
2992// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002993TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02002994 PeerConnectionInterface::RTCConfiguration rtc_config;
2995 rtc_config.enable_rtp_data_channel = true;
2996 rtc_config.enable_dtls_srtp = false;
2997 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07002998 ConnectFakeSignaling();
2999 // Expect that data channel created on caller side will show up for callee as
3000 // well.
3001 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003002 caller()->AddAudioVideoTracks();
3003 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003004 caller()->CreateAndSetAndSignalOffer();
3005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3006 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003007 MediaExpectations media_expectations;
3008 media_expectations.ExpectBidirectionalAudioAndVideo();
3009 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003010 ASSERT_NE(nullptr, caller()->data_channel());
3011 ASSERT_NE(nullptr, callee()->data_channel());
3012 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3013 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3014
3015 // Ensure data can be sent in both directions.
3016 std::string data = "hello world";
3017 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3018 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3019 kDefaultTimeout);
3020 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3021 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3022 kDefaultTimeout);
3023}
3024
3025// Ensure that an RTP data channel is signaled as closed for the caller when
3026// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003027TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003028 RtpDataChannelSignaledClosedInCalleeOffer) {
3029 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003030 PeerConnectionInterface::RTCConfiguration rtc_config;
3031 rtc_config.enable_rtp_data_channel = true;
3032 rtc_config.enable_dtls_srtp = false;
3033 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003034 ConnectFakeSignaling();
3035 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003036 caller()->AddAudioVideoTracks();
3037 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003038 caller()->CreateAndSetAndSignalOffer();
3039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3040 ASSERT_NE(nullptr, caller()->data_channel());
3041 ASSERT_NE(nullptr, callee()->data_channel());
3042 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3043 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3044
3045 // Close the data channel on the callee, and do an updated offer/answer.
3046 callee()->data_channel()->Close();
3047 callee()->CreateAndSetAndSignalOffer();
3048 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3049 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3050 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3051}
3052
3053// Tests that data is buffered in an RTP data channel until an observer is
3054// registered for it.
3055//
3056// NOTE: RTP data channels can receive data before the underlying
3057// transport has detected that a channel is writable and thus data can be
3058// received before the data channel state changes to open. That is hard to test
3059// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003060TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003061 DataBufferedUntilRtpDataChannelObserverRegistered) {
3062 // Use fake clock and simulated network delay so that we predictably can wait
3063 // until an SCTP message has been delivered without "sleep()"ing.
3064 rtc::ScopedFakeClock fake_clock;
3065 // Some things use a time of "0" as a special value, so we need to start out
3066 // the fake clock at a nonzero time.
3067 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003068 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003069 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3070 virtual_socket_server()->UpdateDelayDistribution();
3071
Niels Möllerf06f9232018-08-07 12:32:18 +02003072 PeerConnectionInterface::RTCConfiguration rtc_config;
3073 rtc_config.enable_rtp_data_channel = true;
3074 rtc_config.enable_dtls_srtp = false;
3075 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003076 ConnectFakeSignaling();
3077 caller()->CreateDataChannel();
3078 caller()->CreateAndSetAndSignalOffer();
3079 ASSERT_TRUE(caller()->data_channel() != nullptr);
3080 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3081 kDefaultTimeout, fake_clock);
3082 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3083 kDefaultTimeout, fake_clock);
3084 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3085 callee()->data_channel()->state(), kDefaultTimeout,
3086 fake_clock);
3087
3088 // Unregister the observer which is normally automatically registered.
3089 callee()->data_channel()->UnregisterObserver();
3090 // Send data and advance fake clock until it should have been received.
3091 std::string data = "hello world";
3092 caller()->data_channel()->Send(DataBuffer(data));
3093 SIMULATED_WAIT(false, 50, fake_clock);
3094
3095 // Attach data channel and expect data to be received immediately. Note that
3096 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3097 // further, but data can be received even if the callback is asynchronous.
3098 MockDataChannelObserver new_observer(callee()->data_channel());
3099 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3100 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003101 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3102 // If this is not done a DCHECK can be hit in ports.cc, because a large
3103 // negative number is calculated for the rtt due to the global clock changing.
3104 caller()->pc()->Close();
3105 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003106}
3107
3108// This test sets up a call between two parties with audio, video and but only
3109// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003110TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003111 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3112 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003113 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003114 rtc_config_1.enable_dtls_srtp = false;
3115 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3116 rtc_config_2.enable_dtls_srtp = false;
3117 rtc_config_2.enable_dtls_srtp = false;
3118 ASSERT_TRUE(
3119 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003120 ConnectFakeSignaling();
3121 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003122 caller()->AddAudioVideoTracks();
3123 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003124 caller()->CreateAndSetAndSignalOffer();
3125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3126 // The caller should still have a data channel, but it should be closed, and
3127 // one should ever have been created for the callee.
3128 EXPECT_TRUE(caller()->data_channel() != nullptr);
3129 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3130 EXPECT_EQ(nullptr, callee()->data_channel());
3131}
3132
3133// This test sets up a call between two parties with audio, and video. When
3134// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003135TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003136 PeerConnectionInterface::RTCConfiguration rtc_config;
3137 rtc_config.enable_rtp_data_channel = true;
3138 rtc_config.enable_dtls_srtp = false;
3139 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003140 ConnectFakeSignaling();
3141 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003142 caller()->AddAudioVideoTracks();
3143 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003144 caller()->CreateAndSetAndSignalOffer();
3145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3146 // Create data channel and do new offer and answer.
3147 caller()->CreateDataChannel();
3148 caller()->CreateAndSetAndSignalOffer();
3149 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3150 ASSERT_NE(nullptr, caller()->data_channel());
3151 ASSERT_NE(nullptr, callee()->data_channel());
3152 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3153 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3154 // Ensure data can be sent in both directions.
3155 std::string data = "hello world";
3156 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3157 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3158 kDefaultTimeout);
3159 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3160 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3161 kDefaultTimeout);
3162}
3163
3164#ifdef HAVE_SCTP
3165
3166// This test sets up a call between two parties with audio, video and an SCTP
3167// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003168TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003169 ASSERT_TRUE(CreatePeerConnectionWrappers());
3170 ConnectFakeSignaling();
3171 // Expect that data channel created on caller side will show up for callee as
3172 // well.
3173 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003174 caller()->AddAudioVideoTracks();
3175 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003176 caller()->CreateAndSetAndSignalOffer();
3177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3178 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003179 MediaExpectations media_expectations;
3180 media_expectations.ExpectBidirectionalAudioAndVideo();
3181 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003182 // Caller data channel should already exist (it created one). Callee data
3183 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3184 ASSERT_NE(nullptr, caller()->data_channel());
3185 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3186 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3187 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3188
3189 // Ensure data can be sent in both directions.
3190 std::string data = "hello world";
3191 caller()->data_channel()->Send(DataBuffer(data));
3192 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3193 kDefaultTimeout);
3194 callee()->data_channel()->Send(DataBuffer(data));
3195 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3196 kDefaultTimeout);
3197}
3198
3199// Ensure that when the callee closes an SCTP data channel, the closing
3200// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003201TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003202 // Same procedure as above test.
3203 ASSERT_TRUE(CreatePeerConnectionWrappers());
3204 ConnectFakeSignaling();
3205 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003206 caller()->AddAudioVideoTracks();
3207 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003208 caller()->CreateAndSetAndSignalOffer();
3209 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3210 ASSERT_NE(nullptr, caller()->data_channel());
3211 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3212 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3213 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3214
3215 // Close the data channel on the callee side, and wait for it to reach the
3216 // "closed" state on both sides.
3217 callee()->data_channel()->Close();
3218 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3219 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3220}
3221
Seth Hampson2f0d7022018-02-20 11:54:42 -08003222TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003223 ASSERT_TRUE(CreatePeerConnectionWrappers());
3224 ConnectFakeSignaling();
3225 webrtc::DataChannelInit init;
3226 init.id = 53;
3227 init.maxRetransmits = 52;
3228 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003229 caller()->AddAudioVideoTracks();
3230 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003231 caller()->CreateAndSetAndSignalOffer();
3232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003233 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3234 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003235 EXPECT_EQ(init.id, callee()->data_channel()->id());
3236 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3237 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3238 EXPECT_FALSE(callee()->data_channel()->negotiated());
3239}
3240
deadbeef1dcb1642017-03-29 21:08:16 -07003241// Test usrsctp's ability to process unordered data stream, where data actually
3242// arrives out of order using simulated delays. Previously there have been some
3243// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003244TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003245 // Introduce random network delays.
3246 // Otherwise it's not a true "unordered" test.
3247 virtual_socket_server()->set_delay_mean(20);
3248 virtual_socket_server()->set_delay_stddev(5);
3249 virtual_socket_server()->UpdateDelayDistribution();
3250 // Normal procedure, but with unordered data channel config.
3251 ASSERT_TRUE(CreatePeerConnectionWrappers());
3252 ConnectFakeSignaling();
3253 webrtc::DataChannelInit init;
3254 init.ordered = false;
3255 caller()->CreateDataChannel(&init);
3256 caller()->CreateAndSetAndSignalOffer();
3257 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3258 ASSERT_NE(nullptr, caller()->data_channel());
3259 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3260 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3261 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3262
3263 static constexpr int kNumMessages = 100;
3264 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3265 static constexpr size_t kMaxMessageSize = 4096;
3266 // Create and send random messages.
3267 std::vector<std::string> sent_messages;
3268 for (int i = 0; i < kNumMessages; ++i) {
3269 size_t length =
3270 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3271 std::string message;
3272 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3273 caller()->data_channel()->Send(DataBuffer(message));
3274 callee()->data_channel()->Send(DataBuffer(message));
3275 sent_messages.push_back(message);
3276 }
3277
3278 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003279 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003280 caller()->data_observer()->received_message_count(),
3281 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003282 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003283 callee()->data_observer()->received_message_count(),
3284 kDefaultTimeout);
3285
3286 // Sort and compare to make sure none of the messages were corrupted.
3287 std::vector<std::string> caller_received_messages =
3288 caller()->data_observer()->messages();
3289 std::vector<std::string> callee_received_messages =
3290 callee()->data_observer()->messages();
3291 std::sort(sent_messages.begin(), sent_messages.end());
3292 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3293 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3294 EXPECT_EQ(sent_messages, caller_received_messages);
3295 EXPECT_EQ(sent_messages, callee_received_messages);
3296}
3297
3298// This test sets up a call between two parties with audio, and video. When
3299// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003300TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003301 ASSERT_TRUE(CreatePeerConnectionWrappers());
3302 ConnectFakeSignaling();
3303 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003304 caller()->AddAudioVideoTracks();
3305 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003306 caller()->CreateAndSetAndSignalOffer();
3307 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3308 // Create data channel and do new offer and answer.
3309 caller()->CreateDataChannel();
3310 caller()->CreateAndSetAndSignalOffer();
3311 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3312 // Caller data channel should already exist (it created one). Callee data
3313 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3314 ASSERT_NE(nullptr, caller()->data_channel());
3315 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3316 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3317 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3318 // Ensure data can be sent in both directions.
3319 std::string data = "hello world";
3320 caller()->data_channel()->Send(DataBuffer(data));
3321 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3322 kDefaultTimeout);
3323 callee()->data_channel()->Send(DataBuffer(data));
3324 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3325 kDefaultTimeout);
3326}
3327
deadbeef7914b8c2017-04-21 03:23:33 -07003328// Set up a connection initially just using SCTP data channels, later upgrading
3329// to audio/video, ensuring frames are received end-to-end. Effectively the
3330// inverse of the test above.
3331// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003332TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003333 ASSERT_TRUE(CreatePeerConnectionWrappers());
3334 ConnectFakeSignaling();
3335 // Do initial offer/answer with just data channel.
3336 caller()->CreateDataChannel();
3337 caller()->CreateAndSetAndSignalOffer();
3338 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3339 // Wait until data can be sent over the data channel.
3340 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3341 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3342 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3343
3344 // Do subsequent offer/answer with two-way audio and video. Audio and video
3345 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003346 caller()->AddAudioVideoTracks();
3347 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003348 caller()->CreateAndSetAndSignalOffer();
3349 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003350 MediaExpectations media_expectations;
3351 media_expectations.ExpectBidirectionalAudioAndVideo();
3352 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003353}
3354
deadbeef8b7e9ad2017-05-25 09:38:55 -07003355static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003356 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003357 GetFirstDataContentDescription(desc);
3358 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003359 dcd_offer->set_use_sctpmap(false);
3360 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3361}
3362
3363// Test that the data channel works when a spec-compliant SCTP m= section is
3364// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3365// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003366TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003367 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3368 ASSERT_TRUE(CreatePeerConnectionWrappers());
3369 ConnectFakeSignaling();
3370 caller()->CreateDataChannel();
3371 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3372 caller()->CreateAndSetAndSignalOffer();
3373 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3374 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3375 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3376 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3377
3378 // Ensure data can be sent in both directions.
3379 std::string data = "hello world";
3380 caller()->data_channel()->Send(DataBuffer(data));
3381 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3382 kDefaultTimeout);
3383 callee()->data_channel()->Send(DataBuffer(data));
3384 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3385 kDefaultTimeout);
3386}
3387
deadbeef1dcb1642017-03-29 21:08:16 -07003388#endif // HAVE_SCTP
3389
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003390// This test sets up a call between two parties with audio, video, and a media
3391// transport data channel.
3392TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3393 PeerConnectionInterface::RTCConfiguration rtc_config;
3394 rtc_config.use_media_transport_for_data_channels = true;
3395 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3396 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3397 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3398 loopback_media_transports()->second_factory()));
3399 ConnectFakeSignaling();
3400
3401 // Expect that data channel created on caller side will show up for callee as
3402 // well.
3403 caller()->CreateDataChannel();
3404 caller()->CreateAndSetAndSignalOffer();
3405 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3406
3407 // Ensure that the media transport is ready.
3408 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3409 loopback_media_transports()->FlushAsyncInvokes();
3410
3411 // Caller data channel should already exist (it created one). Callee data
3412 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3413 ASSERT_NE(nullptr, caller()->data_channel());
3414 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3415 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3416 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3417
3418 // Ensure data can be sent in both directions.
3419 std::string data = "hello world";
3420 caller()->data_channel()->Send(DataBuffer(data));
3421 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3422 kDefaultTimeout);
3423 callee()->data_channel()->Send(DataBuffer(data));
3424 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3425 kDefaultTimeout);
3426}
3427
3428// Ensure that when the callee closes a media transport data channel, the
3429// closing procedure results in the data channel being closed for the caller
3430// as well.
3431TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3432 PeerConnectionInterface::RTCConfiguration rtc_config;
3433 rtc_config.use_media_transport_for_data_channels = true;
3434 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3435 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3436 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3437 loopback_media_transports()->second_factory()));
3438 ConnectFakeSignaling();
3439
3440 // Create a data channel on the caller and signal it to the callee.
3441 caller()->CreateDataChannel();
3442 caller()->CreateAndSetAndSignalOffer();
3443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3444
3445 // Ensure that the media transport is ready.
3446 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3447 loopback_media_transports()->FlushAsyncInvokes();
3448
3449 // Data channels exist and open on both ends of the connection.
3450 ASSERT_NE(nullptr, caller()->data_channel());
3451 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3452 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3453 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3454
3455 // Close the data channel on the callee side, and wait for it to reach the
3456 // "closed" state on both sides.
3457 callee()->data_channel()->Close();
3458 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3459 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3460}
3461
3462TEST_P(PeerConnectionIntegrationTest,
3463 MediaTransportDataChannelConfigSentToOtherSide) {
3464 PeerConnectionInterface::RTCConfiguration rtc_config;
3465 rtc_config.use_media_transport_for_data_channels = true;
3466 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3467 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3468 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3469 loopback_media_transports()->second_factory()));
3470 ConnectFakeSignaling();
3471
3472 // Create a data channel with a non-default configuration and signal it to the
3473 // callee.
3474 webrtc::DataChannelInit init;
3475 init.id = 53;
3476 init.maxRetransmits = 52;
3477 caller()->CreateDataChannel("data-channel", &init);
3478 caller()->CreateAndSetAndSignalOffer();
3479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3480
3481 // Ensure that the media transport is ready.
3482 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3483 loopback_media_transports()->FlushAsyncInvokes();
3484
3485 // Ensure that the data channel exists on the callee with the correct
3486 // configuration.
3487 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3488 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3489 EXPECT_EQ(init.id, callee()->data_channel()->id());
3490 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3491 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3492 EXPECT_FALSE(callee()->data_channel()->negotiated());
3493}
3494
deadbeef1dcb1642017-03-29 21:08:16 -07003495// Test that the ICE connection and gathering states eventually reach
3496// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003497TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003498 ASSERT_TRUE(CreatePeerConnectionWrappers());
3499 ConnectFakeSignaling();
3500 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003501 caller()->AddAudioVideoTracks();
3502 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003503 caller()->CreateAndSetAndSignalOffer();
3504 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3505 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3506 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3507 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3508 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3509 // After the best candidate pair is selected and all candidates are signaled,
3510 // the ICE connection state should reach "complete".
3511 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3512 // answerer/"callee" by default) only reaches "connected". When this is
3513 // fixed, this test should be updated.
3514 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3515 caller()->ice_connection_state(), kDefaultTimeout);
3516 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3517 callee()->ice_connection_state(), kDefaultTimeout);
3518}
3519
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003520// Replaces the first candidate with a static address and configures a
3521// MockAsyncResolver to return the replaced address the first time the static
3522// address is resolved. Candidates past the first will not be signaled.
3523class ReplaceFirstCandidateAddressDropOthers final
3524 : public IceCandidateReplacerInterface {
3525 public:
3526 ReplaceFirstCandidateAddressDropOthers(
3527 const SocketAddress& new_address,
3528 rtc::MockAsyncResolver* mock_async_resolver)
3529 : mock_async_resolver_(mock_async_resolver), new_address_(new_address) {
3530 RTC_DCHECK(mock_async_resolver);
3531 }
3532
3533 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3534 const webrtc::IceCandidateInterface* candidate) override {
3535 if (replaced_candidate_) {
3536 return nullptr;
3537 }
3538
3539 replaced_candidate_ = true;
3540 cricket::Candidate new_candidate(candidate->candidate());
3541 new_candidate.set_address(new_address_);
3542 EXPECT_CALL(*mock_async_resolver_, GetResolvedAddress(_, _))
3543 .WillOnce(DoAll(SetArgPointee<1>(candidate->candidate().address()),
3544 Return(true)));
3545 EXPECT_CALL(*mock_async_resolver_, Destroy(_));
3546 return webrtc::CreateIceCandidate(
3547 candidate->sdp_mid(), candidate->sdp_mline_index(), new_candidate);
3548 }
3549
3550 private:
3551 rtc::MockAsyncResolver* mock_async_resolver_;
3552 SocketAddress new_address_;
3553 bool replaced_candidate_ = false;
3554};
3555
3556// Drops all candidates before they are signaled.
3557class DropAllCandidates final : public IceCandidateReplacerInterface {
3558 public:
3559 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3560 const webrtc::IceCandidateInterface*) override {
3561 return nullptr;
3562 }
3563};
3564
3565// Replace the first caller ICE candidate IP with a fake hostname and drop the
3566// other candidates. Drop all candidates on the callee side (to avoid a prflx
3567// connection). Use a mock resolver to resolve the hostname back to the original
3568// IP on the callee side and check that the ice connection connects.
3569TEST_P(PeerConnectionIntegrationTest,
3570 IceStatesReachCompletionWithRemoteHostname) {
3571 webrtc::MockAsyncResolverFactory* callee_mock_async_resolver_factory;
3572 {
3573 auto resolver_factory =
3574 absl::make_unique<webrtc::MockAsyncResolverFactory>();
3575 callee_mock_async_resolver_factory = resolver_factory.get();
3576 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3577 callee_deps.async_resolver_factory = std::move(resolver_factory);
3578
3579 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3580 RTCConfiguration(), webrtc::PeerConnectionDependencies(nullptr),
3581 RTCConfiguration(), std::move(callee_deps)));
3582 }
3583
3584 rtc::MockAsyncResolver mock_async_resolver;
3585
3586 // This also verifies that the injected AsyncResolverFactory is used by
3587 // P2PTransportChannel.
3588 EXPECT_CALL(*callee_mock_async_resolver_factory, Create())
3589 .WillOnce(Return(&mock_async_resolver));
3590 caller()->SetLocalIceCandidateReplacer(
3591 absl::make_unique<ReplaceFirstCandidateAddressDropOthers>(
3592 SocketAddress("a.b", 10000), &mock_async_resolver));
3593 callee()->SetLocalIceCandidateReplacer(
3594 absl::make_unique<DropAllCandidates>());
3595
3596 ConnectFakeSignaling();
3597 caller()->AddAudioVideoTracks();
3598 callee()->AddAudioVideoTracks();
3599 caller()->CreateAndSetAndSignalOffer();
3600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3601 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3602 caller()->ice_connection_state(), kDefaultTimeout);
3603 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3604 callee()->ice_connection_state(), kDefaultTimeout);
3605}
3606
Steve Antonede9ca52017-10-16 13:04:27 -07003607// Test that firewalling the ICE connection causes the clients to identify the
3608// disconnected state and then removing the firewall causes them to reconnect.
3609class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003610 : public PeerConnectionIntegrationBaseTest,
3611 public ::testing::WithParamInterface<
3612 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003613 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003614 PeerConnectionIntegrationIceStatesTest()
3615 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3616 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003617 }
3618
3619 void StartStunServer(const SocketAddress& server_address) {
3620 stun_server_.reset(
3621 cricket::TestStunServer::Create(network_thread(), server_address));
3622 }
3623
3624 bool TestIPv6() {
3625 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3626 }
3627
3628 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003629 network_thread()->Invoke<void>(
3630 RTC_FROM_HERE,
3631 rtc::Bind(&cricket::PortAllocator::set_flags,
3632 caller()->port_allocator(), port_allocator_flags_));
3633 network_thread()->Invoke<void>(
3634 RTC_FROM_HERE,
3635 rtc::Bind(&cricket::PortAllocator::set_flags,
3636 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003637 }
3638
3639 std::vector<SocketAddress> CallerAddresses() {
3640 std::vector<SocketAddress> addresses;
3641 addresses.push_back(SocketAddress("1.1.1.1", 0));
3642 if (TestIPv6()) {
3643 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3644 }
3645 return addresses;
3646 }
3647
3648 std::vector<SocketAddress> CalleeAddresses() {
3649 std::vector<SocketAddress> addresses;
3650 addresses.push_back(SocketAddress("2.2.2.2", 0));
3651 if (TestIPv6()) {
3652 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3653 }
3654 return addresses;
3655 }
3656
3657 void SetUpNetworkInterfaces() {
3658 // Remove the default interfaces added by the test infrastructure.
3659 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3660 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3661
3662 // Add network addresses for test.
3663 for (const auto& caller_address : CallerAddresses()) {
3664 caller()->network()->AddInterface(caller_address);
3665 }
3666 for (const auto& callee_address : CalleeAddresses()) {
3667 callee()->network()->AddInterface(callee_address);
3668 }
3669 }
3670
3671 private:
3672 uint32_t port_allocator_flags_;
3673 std::unique_ptr<cricket::TestStunServer> stun_server_;
3674};
3675
3676// Tests that the PeerConnection goes through all the ICE gathering/connection
3677// states over the duration of the call. This includes Disconnected and Failed
3678// states, induced by putting a firewall between the peers and waiting for them
3679// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003680TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3681 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3682 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3683 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003684
3685 const SocketAddress kStunServerAddress =
3686 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3687 StartStunServer(kStunServerAddress);
3688
3689 PeerConnectionInterface::RTCConfiguration config;
3690 PeerConnectionInterface::IceServer ice_stun_server;
3691 ice_stun_server.urls.push_back(
3692 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3693 kStunServerAddress.PortAsString());
3694 config.servers.push_back(ice_stun_server);
3695
3696 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3697 ConnectFakeSignaling();
3698 SetPortAllocatorFlags();
3699 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003700 caller()->AddAudioVideoTracks();
3701 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003702
3703 // Initial state before anything happens.
3704 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3705 caller()->ice_gathering_state());
3706 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3707 caller()->ice_connection_state());
3708
3709 // Start the call by creating the offer, setting it as the local description,
3710 // then sending it to the peer who will respond with an answer. This happens
3711 // asynchronously so that we can watch the states as it runs in the
3712 // background.
3713 caller()->CreateAndSetAndSignalOffer();
3714
Steve Anton83119dd2017-11-10 16:19:52 -08003715 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3716 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003717
3718 // Verify that the observer was notified of the intermediate transitions.
3719 EXPECT_THAT(caller()->ice_connection_state_history(),
3720 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3721 PeerConnectionInterface::kIceConnectionConnected,
3722 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003723 // After the ice transport transitions from checking to connected we revert
3724 // back to new as the standard requires, as at that point the DTLS transport
3725 // is in the "new" state while no transports are "connecting", "checking",
3726 // "failed" or disconnected. This is pretty unintuitive, and we might want to
3727 // amend the spec to handle this case more gracefully.
3728 EXPECT_THAT(
3729 caller()->peer_connection_state_history(),
3730 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
3731 PeerConnectionInterface::PeerConnectionState::kNew,
3732 PeerConnectionInterface::PeerConnectionState::kConnecting,
3733 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003734 EXPECT_THAT(caller()->ice_gathering_state_history(),
3735 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3736 PeerConnectionInterface::kIceGatheringComplete));
3737
3738 // Block connections to/from the caller and wait for ICE to become
3739 // disconnected.
3740 for (const auto& caller_address : CallerAddresses()) {
3741 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3742 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003743 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003744 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3745 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003746
3747 // Let ICE re-establish by removing the firewall rules.
3748 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003749 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003750 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3751 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003752
3753 // According to RFC7675, if there is no response within 30 seconds then the
3754 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003755 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003756 constexpr int kConsentTimeout = 30000;
3757 for (const auto& caller_address : CallerAddresses()) {
3758 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3759 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003760 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003761 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3762 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003763}
3764
3765// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3766// and that the statistics in the metric observers are updated correctly.
3767TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3768 ASSERT_TRUE(CreatePeerConnectionWrappers());
3769 ConnectFakeSignaling();
3770 SetPortAllocatorFlags();
3771 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003772 caller()->AddAudioVideoTracks();
3773 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003774 caller()->CreateAndSetAndSignalOffer();
3775
3776 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3777
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003778 // TODO(bugs.webrtc.org/9456): Fix it.
3779 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3780 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3781 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3782 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003783 if (TestIPv6()) {
3784 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3785 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003786 EXPECT_EQ(0, num_best_ipv4);
3787 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003788 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003789 EXPECT_EQ(1, num_best_ipv4);
3790 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003791 }
3792
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003793 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3794 "WebRTC.PeerConnection.CandidatePairType_UDP",
3795 webrtc::kIceCandidatePairHostHost));
3796 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3797 "WebRTC.PeerConnection.CandidatePairType_UDP",
3798 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003799}
3800
3801constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3802 cricket::PORTALLOCATOR_DISABLE_STUN |
3803 cricket::PORTALLOCATOR_DISABLE_RELAY;
3804constexpr uint32_t kFlagsIPv6NoStun =
3805 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3806 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3807constexpr uint32_t kFlagsIPv4Stun =
3808 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3809
Seth Hampson2f0d7022018-02-20 11:54:42 -08003810INSTANTIATE_TEST_CASE_P(
3811 PeerConnectionIntegrationTest,
3812 PeerConnectionIntegrationIceStatesTest,
3813 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3814 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3815 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3816 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003817
deadbeef1dcb1642017-03-29 21:08:16 -07003818// This test sets up a call between two parties with audio and video.
3819// During the call, the caller restarts ICE and the test verifies that
3820// new ICE candidates are generated and audio and video still can flow, and the
3821// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003822TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003823 ASSERT_TRUE(CreatePeerConnectionWrappers());
3824 ConnectFakeSignaling();
3825 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003826 caller()->AddAudioVideoTracks();
3827 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003828 caller()->CreateAndSetAndSignalOffer();
3829 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3830 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3831 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3832 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3833 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3834
3835 // To verify that the ICE restart actually occurs, get
3836 // ufrag/password/candidates before and after restart.
3837 // Create an SDP string of the first audio candidate for both clients.
3838 const webrtc::IceCandidateCollection* audio_candidates_caller =
3839 caller()->pc()->local_description()->candidates(0);
3840 const webrtc::IceCandidateCollection* audio_candidates_callee =
3841 callee()->pc()->local_description()->candidates(0);
3842 ASSERT_GT(audio_candidates_caller->count(), 0u);
3843 ASSERT_GT(audio_candidates_callee->count(), 0u);
3844 std::string caller_candidate_pre_restart;
3845 ASSERT_TRUE(
3846 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3847 std::string callee_candidate_pre_restart;
3848 ASSERT_TRUE(
3849 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3850 const cricket::SessionDescription* desc =
3851 caller()->pc()->local_description()->description();
3852 std::string caller_ufrag_pre_restart =
3853 desc->transport_infos()[0].description.ice_ufrag;
3854 desc = callee()->pc()->local_description()->description();
3855 std::string callee_ufrag_pre_restart =
3856 desc->transport_infos()[0].description.ice_ufrag;
3857
3858 // Have the caller initiate an ICE restart.
3859 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3860 caller()->CreateAndSetAndSignalOffer();
3861 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3862 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3863 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3864 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3865 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3866
3867 // Grab the ufrags/candidates again.
3868 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3869 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3870 ASSERT_GT(audio_candidates_caller->count(), 0u);
3871 ASSERT_GT(audio_candidates_callee->count(), 0u);
3872 std::string caller_candidate_post_restart;
3873 ASSERT_TRUE(
3874 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3875 std::string callee_candidate_post_restart;
3876 ASSERT_TRUE(
3877 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3878 desc = caller()->pc()->local_description()->description();
3879 std::string caller_ufrag_post_restart =
3880 desc->transport_infos()[0].description.ice_ufrag;
3881 desc = callee()->pc()->local_description()->description();
3882 std::string callee_ufrag_post_restart =
3883 desc->transport_infos()[0].description.ice_ufrag;
3884 // Sanity check that an ICE restart was actually negotiated in SDP.
3885 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3886 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3887 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3888 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3889
3890 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003891 MediaExpectations media_expectations;
3892 media_expectations.ExpectBidirectionalAudioAndVideo();
3893 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003894}
3895
3896// Verify that audio/video can be received end-to-end when ICE renomination is
3897// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003898TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003899 PeerConnectionInterface::RTCConfiguration config;
3900 config.enable_ice_renomination = true;
3901 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3902 ConnectFakeSignaling();
3903 // Do normal offer/answer and wait for some frames to be received in each
3904 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003905 caller()->AddAudioVideoTracks();
3906 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003907 caller()->CreateAndSetAndSignalOffer();
3908 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3909 // Sanity check that ICE renomination was actually negotiated.
3910 const cricket::SessionDescription* desc =
3911 caller()->pc()->local_description()->description();
3912 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003913 ASSERT_NE(
3914 info.description.transport_options.end(),
3915 std::find(info.description.transport_options.begin(),
3916 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003917 }
3918 desc = callee()->pc()->local_description()->description();
3919 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003920 ASSERT_NE(
3921 info.description.transport_options.end(),
3922 std::find(info.description.transport_options.begin(),
3923 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003924 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003925 MediaExpectations media_expectations;
3926 media_expectations.ExpectBidirectionalAudioAndVideo();
3927 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003928}
3929
Steve Anton6f25b092017-10-23 09:39:20 -07003930// With a max bundle policy and RTCP muxing, adding a new media description to
3931// the connection should not affect ICE at all because the new media will use
3932// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003933TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003934 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003935 PeerConnectionInterface::RTCConfiguration config;
3936 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3937 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3938 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3939 config, PeerConnectionInterface::RTCConfiguration()));
3940 ConnectFakeSignaling();
3941
Steve Anton15324772018-01-16 10:26:49 -08003942 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003943 caller()->CreateAndSetAndSignalOffer();
3944 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003945 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3946 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003947
3948 caller()->clear_ice_connection_state_history();
3949
Steve Anton15324772018-01-16 10:26:49 -08003950 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003951 caller()->CreateAndSetAndSignalOffer();
3952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3953
3954 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3955}
3956
deadbeef1dcb1642017-03-29 21:08:16 -07003957// This test sets up a call between two parties with audio and video. It then
3958// renegotiates setting the video m-line to "port 0", then later renegotiates
3959// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003960TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003961 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3962 ASSERT_TRUE(CreatePeerConnectionWrappers());
3963 ConnectFakeSignaling();
3964
3965 // Do initial negotiation, only sending media from the caller. Will result in
3966 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003967 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003968 caller()->CreateAndSetAndSignalOffer();
3969 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3970
3971 // Negotiate again, disabling the video "m=" section (the callee will set the
3972 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003973 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3974 PeerConnectionInterface::RTCOfferAnswerOptions options;
3975 options.offer_to_receive_video = 0;
3976 callee()->SetOfferAnswerOptions(options);
3977 } else {
3978 callee()->SetRemoteOfferHandler([this] {
3979 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3980 });
3981 }
deadbeef1dcb1642017-03-29 21:08:16 -07003982 caller()->CreateAndSetAndSignalOffer();
3983 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3984 // Sanity check that video "m=" section was actually rejected.
3985 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3986 callee()->pc()->local_description()->description());
3987 ASSERT_NE(nullptr, answer_video_content);
3988 ASSERT_TRUE(answer_video_content->rejected);
3989
3990 // Enable video and do negotiation again, making sure video is received
3991 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003992 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3993 PeerConnectionInterface::RTCOfferAnswerOptions options;
3994 options.offer_to_receive_video = 1;
3995 callee()->SetOfferAnswerOptions(options);
3996 } else {
3997 // The caller's transceiver is stopped, so we need to add another track.
3998 auto caller_transceiver =
3999 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4000 EXPECT_TRUE(caller_transceiver->stopped());
4001 caller()->AddVideoTrack();
4002 }
4003 callee()->AddVideoTrack();
4004 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004005 caller()->CreateAndSetAndSignalOffer();
4006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004007
deadbeef1dcb1642017-03-29 21:08:16 -07004008 // Verify the caller receives frames from the newly added stream, and the
4009 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004010 MediaExpectations media_expectations;
4011 media_expectations.CalleeExpectsSomeAudio();
4012 media_expectations.ExpectBidirectionalVideo();
4013 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004014}
4015
deadbeef1dcb1642017-03-29 21:08:16 -07004016// This tests that if we negotiate after calling CreateSender but before we
4017// have a track, then set a track later, frames from the newly-set track are
4018// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004019TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004020 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4021 ASSERT_TRUE(CreatePeerConnectionWrappers());
4022 ConnectFakeSignaling();
4023 auto caller_audio_sender =
4024 caller()->pc()->CreateSender("audio", "caller_stream");
4025 auto caller_video_sender =
4026 caller()->pc()->CreateSender("video", "caller_stream");
4027 auto callee_audio_sender =
4028 callee()->pc()->CreateSender("audio", "callee_stream");
4029 auto callee_video_sender =
4030 callee()->pc()->CreateSender("video", "callee_stream");
4031 caller()->CreateAndSetAndSignalOffer();
4032 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4033 // Wait for ICE to complete, without any tracks being set.
4034 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4035 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4036 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4037 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4038 // Now set the tracks, and expect frames to immediately start flowing.
4039 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4040 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4041 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4042 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004043 MediaExpectations media_expectations;
4044 media_expectations.ExpectBidirectionalAudioAndVideo();
4045 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4046}
4047
4048// This tests that if we negotiate after calling AddTransceiver but before we
4049// have a track, then set a track later, frames from the newly-set tracks are
4050// received end-to-end.
4051TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4052 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4053 ASSERT_TRUE(CreatePeerConnectionWrappers());
4054 ConnectFakeSignaling();
4055 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4056 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4057 auto caller_audio_sender = audio_result.MoveValue()->sender();
4058 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4059 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4060 auto caller_video_sender = video_result.MoveValue()->sender();
4061 callee()->SetRemoteOfferHandler([this] {
4062 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4063 callee()->pc()->GetTransceivers()[0]->SetDirection(
4064 RtpTransceiverDirection::kSendRecv);
4065 callee()->pc()->GetTransceivers()[1]->SetDirection(
4066 RtpTransceiverDirection::kSendRecv);
4067 });
4068 caller()->CreateAndSetAndSignalOffer();
4069 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4070 // Wait for ICE to complete, without any tracks being set.
4071 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4072 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4073 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4074 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4075 // Now set the tracks, and expect frames to immediately start flowing.
4076 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4077 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4078 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4079 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4080 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4081 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4082 MediaExpectations media_expectations;
4083 media_expectations.ExpectBidirectionalAudioAndVideo();
4084 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004085}
4086
4087// This test verifies that a remote video track can be added via AddStream,
4088// and sent end-to-end. For this particular test, it's simply echoed back
4089// from the caller to the callee, rather than being forwarded to a third
4090// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004091TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004092 ASSERT_TRUE(CreatePeerConnectionWrappers());
4093 ConnectFakeSignaling();
4094 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004095 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004096 caller()->CreateAndSetAndSignalOffer();
4097 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004098 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004099
4100 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4101 // time).
4102 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4103 callee()->CreateAndSetAndSignalOffer();
4104 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4105
Seth Hampson2f0d7022018-02-20 11:54:42 -08004106 MediaExpectations media_expectations;
4107 media_expectations.ExpectBidirectionalVideo();
4108 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004109}
4110
4111// Test that we achieve the expected end-to-end connection time, using a
4112// fake clock and simulated latency on the media and signaling paths.
4113// We use a TURN<->TURN connection because this is usually the quickest to
4114// set up initially, especially when we're confident the connection will work
4115// and can start sending media before we get a STUN response.
4116//
4117// With various optimizations enabled, here are the network delays we expect to
4118// be on the critical path:
4119// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4120// signaling answer (with DTLS fingerprint).
4121// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4122// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4123// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004124TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004125 rtc::ScopedFakeClock fake_clock;
4126 // Some things use a time of "0" as a special value, so we need to start out
4127 // the fake clock at a nonzero time.
4128 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004129 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004130
4131 static constexpr int media_hop_delay_ms = 50;
4132 static constexpr int signaling_trip_delay_ms = 500;
4133 // For explanation of these values, see comment above.
4134 static constexpr int required_media_hops = 9;
4135 static constexpr int required_signaling_trips = 2;
4136 // For internal delays (such as posting an event asychronously).
4137 static constexpr int allowed_internal_delay_ms = 20;
4138 static constexpr int total_connection_time_ms =
4139 media_hop_delay_ms * required_media_hops +
4140 signaling_trip_delay_ms * required_signaling_trips +
4141 allowed_internal_delay_ms;
4142
4143 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4144 3478};
4145 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4146 0};
4147 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4148 3478};
4149 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4150 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004151 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4152 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004153
Seth Hampsonaed71642018-06-11 07:41:32 -07004154 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4155 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004156 // Bypass permission check on received packets so media can be sent before
4157 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004158 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4159 turn_server_1->set_enable_permission_checks(false);
4160 });
4161 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4162 turn_server_2->set_enable_permission_checks(false);
4163 });
deadbeef1dcb1642017-03-29 21:08:16 -07004164
4165 PeerConnectionInterface::RTCConfiguration client_1_config;
4166 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4167 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4168 ice_server_1.username = "test";
4169 ice_server_1.password = "test";
4170 client_1_config.servers.push_back(ice_server_1);
4171 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4172 client_1_config.presume_writable_when_fully_relayed = true;
4173
4174 PeerConnectionInterface::RTCConfiguration client_2_config;
4175 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4176 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4177 ice_server_2.username = "test";
4178 ice_server_2.password = "test";
4179 client_2_config.servers.push_back(ice_server_2);
4180 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4181 client_2_config.presume_writable_when_fully_relayed = true;
4182
4183 ASSERT_TRUE(
4184 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4185 // Set up the simulated delays.
4186 SetSignalingDelayMs(signaling_trip_delay_ms);
4187 ConnectFakeSignaling();
4188 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4189 virtual_socket_server()->UpdateDelayDistribution();
4190
4191 // Set "offer to receive audio/video" without adding any tracks, so we just
4192 // set up ICE/DTLS with no media.
4193 PeerConnectionInterface::RTCOfferAnswerOptions options;
4194 options.offer_to_receive_audio = 1;
4195 options.offer_to_receive_video = 1;
4196 caller()->SetOfferAnswerOptions(options);
4197 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004198 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4199 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004200 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4201 // If this is not done a DCHECK can be hit in ports.cc, because a large
4202 // negative number is calculated for the rtt due to the global clock changing.
4203 caller()->pc()->Close();
4204 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004205}
4206
Jonas Orelandbdcee282017-10-10 14:01:40 +02004207// Verify that a TurnCustomizer passed in through RTCConfiguration
4208// is actually used by the underlying TURN candidate pair.
4209// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004210TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004211 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4212 3478};
4213 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4214 0};
4215 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4216 3478};
4217 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4218 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004219 CreateTurnServer(turn_server_1_internal_address,
4220 turn_server_1_external_address);
4221 CreateTurnServer(turn_server_2_internal_address,
4222 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004223
4224 PeerConnectionInterface::RTCConfiguration client_1_config;
4225 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4226 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4227 ice_server_1.username = "test";
4228 ice_server_1.password = "test";
4229 client_1_config.servers.push_back(ice_server_1);
4230 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004231 auto* customizer1 = CreateTurnCustomizer();
4232 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004233
4234 PeerConnectionInterface::RTCConfiguration client_2_config;
4235 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4236 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4237 ice_server_2.username = "test";
4238 ice_server_2.password = "test";
4239 client_2_config.servers.push_back(ice_server_2);
4240 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004241 auto* customizer2 = CreateTurnCustomizer();
4242 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004243
4244 ASSERT_TRUE(
4245 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4246 ConnectFakeSignaling();
4247
4248 // Set "offer to receive audio/video" without adding any tracks, so we just
4249 // set up ICE/DTLS with no media.
4250 PeerConnectionInterface::RTCOfferAnswerOptions options;
4251 options.offer_to_receive_audio = 1;
4252 options.offer_to_receive_video = 1;
4253 caller()->SetOfferAnswerOptions(options);
4254 caller()->CreateAndSetAndSignalOffer();
4255 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4256
Seth Hampsonaed71642018-06-11 07:41:32 -07004257 ExpectTurnCustomizerCountersIncremented(customizer1);
4258 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004259}
4260
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004261// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4262// send media between the caller and the callee.
4263TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4264 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4265 3478};
4266 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4267
4268 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004269 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4270 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004271
4272 webrtc::PeerConnectionInterface::IceServer ice_server;
4273 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4274 ice_server.username = "test";
4275 ice_server.password = "test";
4276
4277 PeerConnectionInterface::RTCConfiguration client_1_config;
4278 client_1_config.servers.push_back(ice_server);
4279 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4280
4281 PeerConnectionInterface::RTCConfiguration client_2_config;
4282 client_2_config.servers.push_back(ice_server);
4283 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4284
4285 ASSERT_TRUE(
4286 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4287
4288 // Do normal offer/answer and wait for ICE to complete.
4289 ConnectFakeSignaling();
4290 caller()->AddAudioVideoTracks();
4291 callee()->AddAudioVideoTracks();
4292 caller()->CreateAndSetAndSignalOffer();
4293 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4294 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4295 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4296
4297 MediaExpectations media_expectations;
4298 media_expectations.ExpectBidirectionalAudioAndVideo();
4299 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4300}
4301
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004302// Verify that a SSLCertificateVerifier passed in through
4303// PeerConnectionDependencies is actually used by the underlying SSL
4304// implementation to determine whether a certificate presented by the TURN
4305// server is accepted by the client. Note that openssladapter_unittest.cc
4306// contains more detailed, lower-level tests.
4307TEST_P(PeerConnectionIntegrationTest,
4308 SSLCertificateVerifierUsedForTurnConnections) {
4309 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4310 3478};
4311 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4312
4313 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4314 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004315 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4316 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004317
4318 webrtc::PeerConnectionInterface::IceServer ice_server;
4319 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4320 ice_server.username = "test";
4321 ice_server.password = "test";
4322
4323 PeerConnectionInterface::RTCConfiguration client_1_config;
4324 client_1_config.servers.push_back(ice_server);
4325 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4326
4327 PeerConnectionInterface::RTCConfiguration client_2_config;
4328 client_2_config.servers.push_back(ice_server);
4329 // Setting the type to kRelay forces the connection to go through a TURN
4330 // server.
4331 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4332
4333 // Get a copy to the pointer so we can verify calls later.
4334 rtc::TestCertificateVerifier* client_1_cert_verifier =
4335 new rtc::TestCertificateVerifier();
4336 client_1_cert_verifier->verify_certificate_ = true;
4337 rtc::TestCertificateVerifier* client_2_cert_verifier =
4338 new rtc::TestCertificateVerifier();
4339 client_2_cert_verifier->verify_certificate_ = true;
4340
4341 // Create the dependencies with the test certificate verifier.
4342 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4343 client_1_deps.tls_cert_verifier =
4344 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4345 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4346 client_2_deps.tls_cert_verifier =
4347 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4348
4349 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4350 client_1_config, std::move(client_1_deps), client_2_config,
4351 std::move(client_2_deps)));
4352 ConnectFakeSignaling();
4353
4354 // Set "offer to receive audio/video" without adding any tracks, so we just
4355 // set up ICE/DTLS with no media.
4356 PeerConnectionInterface::RTCOfferAnswerOptions options;
4357 options.offer_to_receive_audio = 1;
4358 options.offer_to_receive_video = 1;
4359 caller()->SetOfferAnswerOptions(options);
4360 caller()->CreateAndSetAndSignalOffer();
4361 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4362
4363 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4364 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004365}
4366
4367TEST_P(PeerConnectionIntegrationTest,
4368 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4369 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4370 3478};
4371 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4372
4373 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4374 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004375 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4376 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004377
4378 webrtc::PeerConnectionInterface::IceServer ice_server;
4379 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4380 ice_server.username = "test";
4381 ice_server.password = "test";
4382
4383 PeerConnectionInterface::RTCConfiguration client_1_config;
4384 client_1_config.servers.push_back(ice_server);
4385 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4386
4387 PeerConnectionInterface::RTCConfiguration client_2_config;
4388 client_2_config.servers.push_back(ice_server);
4389 // Setting the type to kRelay forces the connection to go through a TURN
4390 // server.
4391 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4392
4393 // Get a copy to the pointer so we can verify calls later.
4394 rtc::TestCertificateVerifier* client_1_cert_verifier =
4395 new rtc::TestCertificateVerifier();
4396 client_1_cert_verifier->verify_certificate_ = false;
4397 rtc::TestCertificateVerifier* client_2_cert_verifier =
4398 new rtc::TestCertificateVerifier();
4399 client_2_cert_verifier->verify_certificate_ = false;
4400
4401 // Create the dependencies with the test certificate verifier.
4402 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4403 client_1_deps.tls_cert_verifier =
4404 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4405 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4406 client_2_deps.tls_cert_verifier =
4407 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4408
4409 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4410 client_1_config, std::move(client_1_deps), client_2_config,
4411 std::move(client_2_deps)));
4412 ConnectFakeSignaling();
4413
4414 // Set "offer to receive audio/video" without adding any tracks, so we just
4415 // set up ICE/DTLS with no media.
4416 PeerConnectionInterface::RTCOfferAnswerOptions options;
4417 options.offer_to_receive_audio = 1;
4418 options.offer_to_receive_video = 1;
4419 caller()->SetOfferAnswerOptions(options);
4420 caller()->CreateAndSetAndSignalOffer();
4421 bool wait_res = true;
4422 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4423 // properly, should be able to just wait for a state of "failed" instead of
4424 // waiting a fixed 10 seconds.
4425 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4426 ASSERT_FALSE(wait_res);
4427
4428 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4429 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004430}
4431
deadbeefc964d0b2017-04-03 10:03:35 -07004432// Test that audio and video flow end-to-end when codec names don't use the
4433// expected casing, given that they're supposed to be case insensitive. To test
4434// this, all but one codec is removed from each media description, and its
4435// casing is changed.
4436//
4437// In the past, this has regressed and caused crashes/black video, due to the
4438// fact that code at some layers was doing case-insensitive comparisons and
4439// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004440TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004441 ASSERT_TRUE(CreatePeerConnectionWrappers());
4442 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004443 caller()->AddAudioVideoTracks();
4444 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004445
4446 // Remove all but one audio/video codec (opus and VP8), and change the
4447 // casing of the caller's generated offer.
4448 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4449 cricket::AudioContentDescription* audio =
4450 GetFirstAudioContentDescription(description);
4451 ASSERT_NE(nullptr, audio);
4452 auto audio_codecs = audio->codecs();
4453 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4454 [](const cricket::AudioCodec& codec) {
4455 return codec.name != "opus";
4456 }),
4457 audio_codecs.end());
4458 ASSERT_EQ(1u, audio_codecs.size());
4459 audio_codecs[0].name = "OpUs";
4460 audio->set_codecs(audio_codecs);
4461
4462 cricket::VideoContentDescription* video =
4463 GetFirstVideoContentDescription(description);
4464 ASSERT_NE(nullptr, video);
4465 auto video_codecs = video->codecs();
4466 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4467 [](const cricket::VideoCodec& codec) {
4468 return codec.name != "VP8";
4469 }),
4470 video_codecs.end());
4471 ASSERT_EQ(1u, video_codecs.size());
4472 video_codecs[0].name = "vP8";
4473 video->set_codecs(video_codecs);
4474 });
4475
4476 caller()->CreateAndSetAndSignalOffer();
4477 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4478
4479 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004480 MediaExpectations media_expectations;
4481 media_expectations.ExpectBidirectionalAudioAndVideo();
4482 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004483}
4484
Jonas Oreland49ac5952018-09-26 16:04:32 +02004485TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004486 ASSERT_TRUE(CreatePeerConnectionWrappers());
4487 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004488 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004489 caller()->CreateAndSetAndSignalOffer();
4490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004491 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004492 MediaExpectations media_expectations;
4493 media_expectations.CalleeExpectsSomeAudio(1);
4494 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004495 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004496 auto receiver = callee()->pc()->GetReceivers()[0];
4497 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004498 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004499 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4500 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004501 sources[0].source_id());
4502 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4503}
4504
4505TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4506 ASSERT_TRUE(CreatePeerConnectionWrappers());
4507 ConnectFakeSignaling();
4508 caller()->AddVideoTrack();
4509 caller()->CreateAndSetAndSignalOffer();
4510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4511 // Wait for one video frame to be received by the callee.
4512 MediaExpectations media_expectations;
4513 media_expectations.CalleeExpectsSomeVideo(1);
4514 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4515 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4516 auto receiver = callee()->pc()->GetReceivers()[0];
4517 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4518 auto sources = receiver->GetSources();
4519 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4520 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4521 sources[0].source_id());
4522 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004523}
4524
deadbeef2f425aa2017-04-14 10:41:32 -07004525// Test that if a track is removed and added again with a different stream ID,
4526// the new stream ID is successfully communicated in SDP and media continues to
4527// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004528// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4529// it will not reuse a transceiver that has already been sending. After creating
4530// a new transceiver it tries to create an offer with two senders of the same
4531// track ids and it fails.
4532TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004533 ASSERT_TRUE(CreatePeerConnectionWrappers());
4534 ConnectFakeSignaling();
4535
deadbeef2f425aa2017-04-14 10:41:32 -07004536 // Add track using stream 1, do offer/answer.
4537 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4538 caller()->CreateLocalAudioTrack();
4539 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004540 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004541 caller()->CreateAndSetAndSignalOffer();
4542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004543 {
4544 MediaExpectations media_expectations;
4545 media_expectations.CalleeExpectsSomeAudio(1);
4546 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4547 }
deadbeef2f425aa2017-04-14 10:41:32 -07004548 // Remove the sender, and create a new one with the new stream.
4549 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004550 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004551 caller()->CreateAndSetAndSignalOffer();
4552 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4553 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004554 {
4555 MediaExpectations media_expectations;
4556 media_expectations.CalleeExpectsSomeAudio();
4557 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4558 }
deadbeef2f425aa2017-04-14 10:41:32 -07004559}
4560
Seth Hampson2f0d7022018-02-20 11:54:42 -08004561TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004562 ASSERT_TRUE(CreatePeerConnectionWrappers());
4563 ConnectFakeSignaling();
4564
Karl Wiberg918f50c2018-07-05 11:40:33 +02004565 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004566 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4567 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4568 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004569 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4570 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004571
Steve Anton15324772018-01-16 10:26:49 -08004572 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004573 caller()->CreateAndSetAndSignalOffer();
4574 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4575}
4576
Steve Antonede9ca52017-10-16 13:04:27 -07004577// Test that if candidates are only signaled by applying full session
4578// descriptions (instead of using AddIceCandidate), the peers can connect to
4579// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004580TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004581 ASSERT_TRUE(CreatePeerConnectionWrappers());
4582 // Each side will signal the session descriptions but not candidates.
4583 ConnectFakeSignalingForSdpOnly();
4584
4585 // Add audio video track and exchange the initial offer/answer with media
4586 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004587 caller()->AddAudioVideoTracks();
4588 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004589 caller()->CreateAndSetAndSignalOffer();
4590
4591 // Wait for all candidates to be gathered on both the caller and callee.
4592 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4593 caller()->ice_gathering_state(), kDefaultTimeout);
4594 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4595 callee()->ice_gathering_state(), kDefaultTimeout);
4596
4597 // The candidates will now be included in the session description, so
4598 // signaling them will start the ICE connection.
4599 caller()->CreateAndSetAndSignalOffer();
4600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4601
4602 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004603 MediaExpectations media_expectations;
4604 media_expectations.ExpectBidirectionalAudioAndVideo();
4605 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004606}
4607
henrika5f6bf242017-11-01 11:06:56 +01004608// Test that SetAudioPlayout can be used to disable audio playout from the
4609// start, then later enable it. This may be useful, for example, if the caller
4610// needs to play a local ringtone until some event occurs, after which it
4611// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004612TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004613 ASSERT_TRUE(CreatePeerConnectionWrappers());
4614 ConnectFakeSignaling();
4615
4616 // Set up audio-only call where audio playout is disabled on caller's side.
4617 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004618 caller()->AddAudioTrack();
4619 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004620 caller()->CreateAndSetAndSignalOffer();
4621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4622
4623 // Pump messages for a second.
4624 WAIT(false, 1000);
4625 // Since audio playout is disabled, the caller shouldn't have received
4626 // anything (at the playout level, at least).
4627 EXPECT_EQ(0, caller()->audio_frames_received());
4628 // As a sanity check, make sure the callee (for which playout isn't disabled)
4629 // did still see frames on its audio level.
4630 ASSERT_GT(callee()->audio_frames_received(), 0);
4631
4632 // Enable playout again, and ensure audio starts flowing.
4633 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004634 MediaExpectations media_expectations;
4635 media_expectations.ExpectBidirectionalAudio();
4636 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004637}
4638
4639double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4640 auto report = pc->NewGetStats();
4641 auto track_stats_list =
4642 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4643 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4644 for (const auto* track_stats : track_stats_list) {
4645 if (track_stats->remote_source.is_defined() &&
4646 *track_stats->remote_source) {
4647 remote_track_stats = track_stats;
4648 break;
4649 }
4650 }
4651
4652 if (!remote_track_stats->total_audio_energy.is_defined()) {
4653 return 0.0;
4654 }
4655 return *remote_track_stats->total_audio_energy;
4656}
4657
4658// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4659// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004660TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004661 DisableAudioPlayoutStillGeneratesAudioStats) {
4662 ASSERT_TRUE(CreatePeerConnectionWrappers());
4663 ConnectFakeSignaling();
4664
4665 // Set up audio-only call where playout is disabled but audio-processing is
4666 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004667 caller()->AddAudioTrack();
4668 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004669 caller()->pc()->SetAudioPlayout(false);
4670
4671 caller()->CreateAndSetAndSignalOffer();
4672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4673
4674 // Wait for the callee to receive audio stats.
4675 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4676}
4677
henrika4f167df2017-11-01 14:45:55 +01004678// Test that SetAudioRecording can be used to disable audio recording from the
4679// start, then later enable it. This may be useful, for example, if the caller
4680// wants to ensure that no audio resources are active before a certain state
4681// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004682TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004683 ASSERT_TRUE(CreatePeerConnectionWrappers());
4684 ConnectFakeSignaling();
4685
4686 // Set up audio-only call where audio recording is disabled on caller's side.
4687 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004688 caller()->AddAudioTrack();
4689 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004690 caller()->CreateAndSetAndSignalOffer();
4691 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4692
4693 // Pump messages for a second.
4694 WAIT(false, 1000);
4695 // Since caller has disabled audio recording, the callee shouldn't have
4696 // received anything.
4697 EXPECT_EQ(0, callee()->audio_frames_received());
4698 // As a sanity check, make sure the caller did still see frames on its
4699 // audio level since audio recording is enabled on the calle side.
4700 ASSERT_GT(caller()->audio_frames_received(), 0);
4701
4702 // Enable audio recording again, and ensure audio starts flowing.
4703 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004704 MediaExpectations media_expectations;
4705 media_expectations.ExpectBidirectionalAudio();
4706 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004707}
4708
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004709// Test that after closing PeerConnections, they stop sending any packets (ICE,
4710// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004711TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004712 // Set up audio/video/data, wait for some frames to be received.
4713 ASSERT_TRUE(CreatePeerConnectionWrappers());
4714 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004715 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004716#ifdef HAVE_SCTP
4717 caller()->CreateDataChannel();
4718#endif
4719 caller()->CreateAndSetAndSignalOffer();
4720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004721 MediaExpectations media_expectations;
4722 media_expectations.CalleeExpectsSomeAudioAndVideo();
4723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004724 // Close PeerConnections.
4725 caller()->pc()->Close();
4726 callee()->pc()->Close();
4727 // Pump messages for a second, and ensure no new packets end up sent.
4728 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4729 WAIT(false, 1000);
4730 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4731 EXPECT_EQ(sent_packets_a, sent_packets_b);
4732}
4733
Steve Anton7eca0932018-03-30 15:18:41 -07004734// Test that transport stats are generated by the RTCStatsCollector for a
4735// connection that only involves data channels. This is a regression test for
4736// crbug.com/826972.
4737#ifdef HAVE_SCTP
4738TEST_P(PeerConnectionIntegrationTest,
4739 TransportStatsReportedForDataChannelOnlyConnection) {
4740 ASSERT_TRUE(CreatePeerConnectionWrappers());
4741 ConnectFakeSignaling();
4742 caller()->CreateDataChannel();
4743
4744 caller()->CreateAndSetAndSignalOffer();
4745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4746 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4747
4748 auto caller_report = caller()->NewGetStats();
4749 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4750 auto callee_report = callee()->NewGetStats();
4751 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4752}
4753#endif // HAVE_SCTP
4754
Qingsi Wang7685e862018-06-11 20:15:46 -07004755TEST_P(PeerConnectionIntegrationTest,
4756 IceEventsGeneratedAndLoggedInRtcEventLog) {
4757 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4758 ConnectFakeSignaling();
4759 PeerConnectionInterface::RTCOfferAnswerOptions options;
4760 options.offer_to_receive_audio = 1;
4761 caller()->SetOfferAnswerOptions(options);
4762 caller()->CreateAndSetAndSignalOffer();
4763 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4764 ASSERT_NE(nullptr, caller()->event_log_factory());
4765 ASSERT_NE(nullptr, callee()->event_log_factory());
4766 webrtc::FakeRtcEventLog* caller_event_log =
4767 static_cast<webrtc::FakeRtcEventLog*>(
4768 caller()->event_log_factory()->last_log_created());
4769 webrtc::FakeRtcEventLog* callee_event_log =
4770 static_cast<webrtc::FakeRtcEventLog*>(
4771 callee()->event_log_factory()->last_log_created());
4772 ASSERT_NE(nullptr, caller_event_log);
4773 ASSERT_NE(nullptr, callee_event_log);
4774 int caller_ice_config_count = caller_event_log->GetEventCount(
4775 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4776 int caller_ice_event_count = caller_event_log->GetEventCount(
4777 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4778 int callee_ice_config_count = callee_event_log->GetEventCount(
4779 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4780 int callee_ice_event_count = callee_event_log->GetEventCount(
4781 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4782 EXPECT_LT(0, caller_ice_config_count);
4783 EXPECT_LT(0, caller_ice_event_count);
4784 EXPECT_LT(0, callee_ice_config_count);
4785 EXPECT_LT(0, callee_ice_event_count);
4786}
4787
Seth Hampson2f0d7022018-02-20 11:54:42 -08004788INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4789 PeerConnectionIntegrationTest,
4790 Values(SdpSemantics::kPlanB,
4791 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004792
Steve Anton74255ff2018-01-24 18:32:57 -08004793// Tests that verify interoperability between Plan B and Unified Plan
4794// PeerConnections.
4795class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004796 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004797 public ::testing::WithParamInterface<
4798 std::tuple<SdpSemantics, SdpSemantics>> {
4799 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004800 // Setting the SdpSemantics for the base test to kDefault does not matter
4801 // because we specify not to use the test semantics when creating
4802 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004803 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004804 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004805 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004806 callee_semantics_(std::get<1>(GetParam())) {}
4807
4808 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004809 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4810 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004811 }
4812
4813 const SdpSemantics caller_semantics_;
4814 const SdpSemantics callee_semantics_;
4815};
4816
4817TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4818 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4819 ConnectFakeSignaling();
4820
4821 caller()->CreateAndSetAndSignalOffer();
4822 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4823}
4824
4825TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4826 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4827 ConnectFakeSignaling();
4828 auto audio_sender = caller()->AddAudioTrack();
4829
4830 caller()->CreateAndSetAndSignalOffer();
4831 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4832
4833 // Verify that one audio receiver has been created on the remote and that it
4834 // has the same track ID as the sending track.
4835 auto receivers = callee()->pc()->GetReceivers();
4836 ASSERT_EQ(1u, receivers.size());
4837 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4838 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4839
Seth Hampson2f0d7022018-02-20 11:54:42 -08004840 MediaExpectations media_expectations;
4841 media_expectations.CalleeExpectsSomeAudio();
4842 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004843}
4844
4845TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4846 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4847 ConnectFakeSignaling();
4848 auto video_sender = caller()->AddVideoTrack();
4849 auto audio_sender = caller()->AddAudioTrack();
4850
4851 caller()->CreateAndSetAndSignalOffer();
4852 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4853
4854 // Verify that one audio and one video receiver have been created on the
4855 // remote and that they have the same track IDs as the sending tracks.
4856 auto audio_receivers =
4857 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4858 ASSERT_EQ(1u, audio_receivers.size());
4859 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4860 auto video_receivers =
4861 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4862 ASSERT_EQ(1u, video_receivers.size());
4863 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4864
Seth Hampson2f0d7022018-02-20 11:54:42 -08004865 MediaExpectations media_expectations;
4866 media_expectations.CalleeExpectsSomeAudioAndVideo();
4867 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004868}
4869
4870TEST_P(PeerConnectionIntegrationInteropTest,
4871 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4872 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4873 ConnectFakeSignaling();
4874 caller()->AddAudioVideoTracks();
4875 callee()->AddAudioVideoTracks();
4876
4877 caller()->CreateAndSetAndSignalOffer();
4878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4879
Seth Hampson2f0d7022018-02-20 11:54:42 -08004880 MediaExpectations media_expectations;
4881 media_expectations.ExpectBidirectionalAudioAndVideo();
4882 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004883}
4884
4885TEST_P(PeerConnectionIntegrationInteropTest,
4886 ReverseRolesOneAudioLocalToOneVideoRemote) {
4887 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4888 ConnectFakeSignaling();
4889 caller()->AddAudioTrack();
4890 callee()->AddVideoTrack();
4891
4892 caller()->CreateAndSetAndSignalOffer();
4893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4894
4895 // Verify that only the audio track has been negotiated.
4896 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4897 // Might also check that the callee's NegotiationNeeded flag is set.
4898
4899 // Reverse roles.
4900 callee()->CreateAndSetAndSignalOffer();
4901 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4902
Seth Hampson2f0d7022018-02-20 11:54:42 -08004903 MediaExpectations media_expectations;
4904 media_expectations.CallerExpectsSomeVideo();
4905 media_expectations.CalleeExpectsSomeAudio();
4906 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004907}
4908
Steve Antonba42e992018-04-09 14:10:01 -07004909INSTANTIATE_TEST_CASE_P(
4910 PeerConnectionIntegrationTest,
4911 PeerConnectionIntegrationInteropTest,
4912 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4913 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4914
4915// Test that if the Unified Plan side offers two video tracks then the Plan B
4916// side will only see the first one and ignore the second.
4917TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004918 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4919 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004920 ConnectFakeSignaling();
4921 auto first_sender = caller()->AddVideoTrack();
4922 caller()->AddVideoTrack();
4923
4924 caller()->CreateAndSetAndSignalOffer();
4925 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4926
4927 // Verify that there is only one receiver and it corresponds to the first
4928 // added track.
4929 auto receivers = callee()->pc()->GetReceivers();
4930 ASSERT_EQ(1u, receivers.size());
4931 EXPECT_TRUE(receivers[0]->track()->enabled());
4932 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4933
Seth Hampson2f0d7022018-02-20 11:54:42 -08004934 MediaExpectations media_expectations;
4935 media_expectations.CalleeExpectsSomeVideo();
4936 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004937}
4938
deadbeef1dcb1642017-03-29 21:08:16 -07004939} // namespace
4940
4941#endif // if !defined(THREAD_SANITIZER)