blob: 295006f203c5e543ed2fc346c1f295727cb38ec8 [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;
Steve Antona41959e2018-11-28 11:15:33 -080081using ::testing::UnorderedElementsAreArray;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070082using ::testing::Return;
83using ::testing::SetArgPointee;
Steve Antonede9ca52017-10-16 13:04:27 -070084using ::testing::Values;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070085using ::testing::_;
deadbeef1dcb1642017-03-29 21:08:16 -070086using webrtc::DataBuffer;
87using webrtc::DataChannelInterface;
88using webrtc::DtmfSender;
89using webrtc::DtmfSenderInterface;
90using webrtc::DtmfSenderObserverInterface;
Steve Anton15324772018-01-16 10:26:49 -080091using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070092using webrtc::MediaStreamInterface;
93using webrtc::MediaStreamTrackInterface;
94using webrtc::MockCreateSessionDescriptionObserver;
95using webrtc::MockDataChannelObserver;
96using webrtc::MockSetSessionDescriptionObserver;
97using webrtc::MockStatsObserver;
98using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070099using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -0700100using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -0800101using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -0700102using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -0700103using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -0800104using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -0700105using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -0800106using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100107using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800108using webrtc::RtpSenderInterface;
109using webrtc::RtpTransceiverDirection;
110using webrtc::RtpTransceiverInit;
111using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800112using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800113using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700114using webrtc::SessionDescriptionInterface;
115using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800116using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700117
118namespace {
119
120static const int kDefaultTimeout = 10000;
121static const int kMaxWaitForStatsMs = 3000;
122static const int kMaxWaitForActivationMs = 5000;
123static const int kMaxWaitForFramesMs = 10000;
124// Default number of audio/video frames to wait for before considering a test
125// successful.
126static const int kDefaultExpectedAudioFrameCount = 3;
127static const int kDefaultExpectedVideoFrameCount = 3;
128
deadbeef1dcb1642017-03-29 21:08:16 -0700129static const char kDataChannelLabel[] = "data_channel";
130
131// SRTP cipher name negotiated by the tests. This must be updated if the
132// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700133static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700134static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
135
Steve Antonede9ca52017-10-16 13:04:27 -0700136static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
137
deadbeef1dcb1642017-03-29 21:08:16 -0700138// Helper function for constructing offer/answer options to initiate an ICE
139// restart.
140PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
141 PeerConnectionInterface::RTCOfferAnswerOptions options;
142 options.ice_restart = true;
143 return options;
144}
145
deadbeefd8ad7882017-04-18 16:01:17 -0700146// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
147// attribute from received SDP, simulating a legacy endpoint.
148void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
149 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800150 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700151 }
152 desc->set_msid_supported(false);
153}
154
Seth Hampson5897a6e2018-04-03 11:16:33 -0700155// Removes all stream information besides the stream ids, simulating an
156// endpoint that only signals a=msid lines to convey stream_ids.
157void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
158 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700159 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700160 std::vector<std::string> stream_ids;
161 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700162 const StreamParams& first_stream =
163 content.media_description()->streams()[0];
164 track_id = first_stream.id;
165 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700166 }
167 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700168 StreamParams new_stream;
169 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700170 new_stream.set_stream_ids(stream_ids);
171 content.media_description()->AddStream(new_stream);
172 }
173}
174
zhihuangf8164932017-05-19 13:09:47 -0700175int FindFirstMediaStatsIndexByKind(
176 const std::string& kind,
177 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
178 media_stats_vec) {
179 for (size_t i = 0; i < media_stats_vec.size(); i++) {
180 if (media_stats_vec[i]->kind.ValueToString() == kind) {
181 return i;
182 }
183 }
184 return -1;
185}
186
deadbeef1dcb1642017-03-29 21:08:16 -0700187class SignalingMessageReceiver {
188 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800189 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700190 virtual void ReceiveIceMessage(const std::string& sdp_mid,
191 int sdp_mline_index,
192 const std::string& msg) = 0;
193
194 protected:
195 SignalingMessageReceiver() {}
196 virtual ~SignalingMessageReceiver() {}
197};
198
199class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
200 public:
201 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
202 : expected_media_type_(media_type) {}
203
204 void OnFirstPacketReceived(cricket::MediaType media_type) override {
205 ASSERT_EQ(expected_media_type_, media_type);
206 first_packet_received_ = true;
207 }
208
209 bool first_packet_received() const { return first_packet_received_; }
210
211 virtual ~MockRtpReceiverObserver() {}
212
213 private:
214 bool first_packet_received_ = false;
215 cricket::MediaType expected_media_type_;
216};
217
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700218// Used by PeerConnectionWrapper::OnIceCandidate to allow a test to modify an
219// ICE candidate before it is signaled.
220class IceCandidateReplacerInterface {
221 public:
222 virtual ~IceCandidateReplacerInterface() = default;
223 // Return nullptr to drop the candidate (it won't be signaled to the other
224 // side).
225 virtual std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
226 const webrtc::IceCandidateInterface*) = 0;
227};
228
deadbeef1dcb1642017-03-29 21:08:16 -0700229// Helper class that wraps a peer connection, observes it, and can accept
230// signaling messages from another wrapper.
231//
232// Uses a fake network, fake A/V capture, and optionally fake
233// encoders/decoders, though they aren't used by default since they don't
234// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700235// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800236// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700237class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800238 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700239 public:
240 // Different factory methods for convenience.
241 // TODO(deadbeef): Could use the pattern of:
242 //
243 // PeerConnectionWrapper =
244 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
245 //
246 // To reduce some code duplication.
247 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
248 const std::string& debug_name,
249 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
250 rtc::Thread* network_thread,
251 rtc::Thread* worker_thread) {
252 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700253 webrtc::PeerConnectionDependencies dependencies(nullptr);
254 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200255 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800256 worker_thread, nullptr,
257 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700258 delete client;
259 return nullptr;
260 }
261 return client;
262 }
263
deadbeef2f425aa2017-04-14 10:41:32 -0700264 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
265 return peer_connection_factory_.get();
266 }
267
deadbeef1dcb1642017-03-29 21:08:16 -0700268 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
269
270 // If a signaling message receiver is set (via ConnectFakeSignaling), this
271 // will set the whole offer/answer exchange in motion. Just need to wait for
272 // the signaling state to reach "stable".
273 void CreateAndSetAndSignalOffer() {
274 auto offer = CreateOffer();
275 ASSERT_NE(nullptr, offer);
276 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
277 }
278
279 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
280 // when a remote offer is received (via fake signaling) and an answer is
281 // generated. By default, uses default options.
282 void SetOfferAnswerOptions(
283 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
284 offer_answer_options_ = options;
285 }
286
287 // Set a callback to be invoked when SDP is received via the fake signaling
288 // channel, which provides an opportunity to munge (modify) the SDP. This is
289 // used to test SDP being applied that a PeerConnection would normally not
290 // generate, but a non-JSEP endpoint might.
291 void SetReceivedSdpMunger(
292 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100293 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700294 }
295
deadbeefc964d0b2017-04-03 10:03:35 -0700296 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700297 // generated.
298 void SetGeneratedSdpMunger(
299 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100300 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700301 }
302
Seth Hampson2f0d7022018-02-20 11:54:42 -0800303 // Set a callback to be invoked when a remote offer is received via the fake
304 // signaling channel. This provides an opportunity to change the
305 // PeerConnection state before an answer is created and sent to the caller.
306 void SetRemoteOfferHandler(std::function<void()> handler) {
307 remote_offer_handler_ = std::move(handler);
308 }
309
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700310 void SetLocalIceCandidateReplacer(
311 std::unique_ptr<IceCandidateReplacerInterface> replacer) {
312 local_ice_candidate_replacer_ = std::move(replacer);
313 }
314
Steve Antonede9ca52017-10-16 13:04:27 -0700315 // Every ICE connection state in order that has been seen by the observer.
316 std::vector<PeerConnectionInterface::IceConnectionState>
317 ice_connection_state_history() const {
318 return ice_connection_state_history_;
319 }
Steve Anton6f25b092017-10-23 09:39:20 -0700320 void clear_ice_connection_state_history() {
321 ice_connection_state_history_.clear();
322 }
Steve Antonede9ca52017-10-16 13:04:27 -0700323
Jonas Olsson635474e2018-10-18 15:58:17 +0200324 // Every PeerConnection state in order that has been seen by the observer.
325 std::vector<PeerConnectionInterface::PeerConnectionState>
326 peer_connection_state_history() const {
327 return peer_connection_state_history_;
328 }
329
Steve Antonede9ca52017-10-16 13:04:27 -0700330 // Every ICE gathering state in order that has been seen by the observer.
331 std::vector<PeerConnectionInterface::IceGatheringState>
332 ice_gathering_state_history() const {
333 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700334 }
335
Steve Anton15324772018-01-16 10:26:49 -0800336 void AddAudioVideoTracks() {
337 AddAudioTrack();
338 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700339 }
340
Steve Anton74255ff2018-01-24 18:32:57 -0800341 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
342 return AddTrack(CreateLocalAudioTrack());
343 }
deadbeef1dcb1642017-03-29 21:08:16 -0700344
Steve Anton74255ff2018-01-24 18:32:57 -0800345 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
346 return AddTrack(CreateLocalVideoTrack());
347 }
deadbeef1dcb1642017-03-29 21:08:16 -0700348
349 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200350 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700351 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200352 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700353 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200354 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700355 // TODO(perkj): Test audio source when it is implemented. Currently audio
356 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700357 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700358 source);
359 }
360
361 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200362 webrtc::FakePeriodicVideoSource::Config config;
363 config.timestamp_offset_ms = rtc::TimeMillis();
364 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700365 }
366
367 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200368 CreateLocalVideoTrackWithConfig(
369 webrtc::FakePeriodicVideoSource::Config config) {
370 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700371 }
372
373 rtc::scoped_refptr<webrtc::VideoTrackInterface>
374 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200375 webrtc::FakePeriodicVideoSource::Config config;
376 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200377 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200378 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700379 }
380
Steve Anton74255ff2018-01-24 18:32:57 -0800381 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
382 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800383 const std::vector<std::string>& stream_ids = {}) {
384 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800385 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800386 return result.MoveValue();
387 }
388
389 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
390 cricket::MediaType media_type) {
391 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
392 for (auto receiver : pc()->GetReceivers()) {
393 if (receiver->media_type() == media_type) {
394 receivers.push_back(receiver);
395 }
396 }
397 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700398 }
399
Seth Hampson2f0d7022018-02-20 11:54:42 -0800400 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
401 cricket::MediaType media_type) {
402 for (auto transceiver : pc()->GetTransceivers()) {
403 if (transceiver->receiver()->media_type() == media_type) {
404 return transceiver;
405 }
406 }
407 return nullptr;
408 }
409
deadbeef1dcb1642017-03-29 21:08:16 -0700410 bool SignalingStateStable() {
411 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
412 }
413
414 void CreateDataChannel() { CreateDataChannel(nullptr); }
415
416 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700417 CreateDataChannel(kDataChannelLabel, init);
418 }
419
420 void CreateDataChannel(const std::string& label,
421 const webrtc::DataChannelInit* init) {
422 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700423 ASSERT_TRUE(data_channel_.get() != nullptr);
424 data_observer_.reset(new MockDataChannelObserver(data_channel_));
425 }
426
427 DataChannelInterface* data_channel() { return data_channel_; }
428 const MockDataChannelObserver* data_observer() const {
429 return data_observer_.get();
430 }
431
432 int audio_frames_received() const {
433 return fake_audio_capture_module_->frames_received();
434 }
435
436 // Takes minimum of video frames received for each track.
437 //
438 // Can be used like:
439 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
440 //
441 // To ensure that all video tracks received at least a certain number of
442 // frames.
443 int min_video_frames_received_per_track() const {
444 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200445 if (fake_video_renderers_.empty()) {
446 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
deadbeef1dcb1642017-03-29 21:08:16 -0700448
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200449 for (const auto& pair : fake_video_renderers_) {
450 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700451 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200452 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700453 }
454
455 // Returns a MockStatsObserver in a state after stats gathering finished,
456 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700457 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700458 webrtc::MediaStreamTrackInterface* track) {
459 rtc::scoped_refptr<MockStatsObserver> observer(
460 new rtc::RefCountedObject<MockStatsObserver>());
461 EXPECT_TRUE(peer_connection_->GetStats(
462 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
463 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
464 return observer;
465 }
466
467 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700468 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
469 return OldGetStatsForTrack(nullptr);
470 }
471
472 // Synchronously gets stats and returns them. If it times out, fails the test
473 // and returns null.
474 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
475 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
476 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
477 peer_connection_->GetStats(callback);
478 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
479 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700480 }
481
482 int rendered_width() {
483 EXPECT_FALSE(fake_video_renderers_.empty());
484 return fake_video_renderers_.empty()
485 ? 0
486 : fake_video_renderers_.begin()->second->width();
487 }
488
489 int rendered_height() {
490 EXPECT_FALSE(fake_video_renderers_.empty());
491 return fake_video_renderers_.empty()
492 ? 0
493 : fake_video_renderers_.begin()->second->height();
494 }
495
496 double rendered_aspect_ratio() {
497 if (rendered_height() == 0) {
498 return 0.0;
499 }
500 return static_cast<double>(rendered_width()) / rendered_height();
501 }
502
503 webrtc::VideoRotation rendered_rotation() {
504 EXPECT_FALSE(fake_video_renderers_.empty());
505 return fake_video_renderers_.empty()
506 ? webrtc::kVideoRotation_0
507 : fake_video_renderers_.begin()->second->rotation();
508 }
509
510 int local_rendered_width() {
511 return local_video_renderer_ ? local_video_renderer_->width() : 0;
512 }
513
514 int local_rendered_height() {
515 return local_video_renderer_ ? local_video_renderer_->height() : 0;
516 }
517
518 double local_rendered_aspect_ratio() {
519 if (local_rendered_height() == 0) {
520 return 0.0;
521 }
522 return static_cast<double>(local_rendered_width()) /
523 local_rendered_height();
524 }
525
526 size_t number_of_remote_streams() {
527 if (!pc()) {
528 return 0;
529 }
530 return pc()->remote_streams()->count();
531 }
532
533 StreamCollectionInterface* remote_streams() const {
534 if (!pc()) {
535 ADD_FAILURE();
536 return nullptr;
537 }
538 return pc()->remote_streams();
539 }
540
541 StreamCollectionInterface* local_streams() {
542 if (!pc()) {
543 ADD_FAILURE();
544 return nullptr;
545 }
546 return pc()->local_streams();
547 }
548
549 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
550 return pc()->signaling_state();
551 }
552
553 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
554 return pc()->ice_connection_state();
555 }
556
557 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
558 return pc()->ice_gathering_state();
559 }
560
561 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
562 // GetReceivers. They're updated automatically when a remote offer/answer
563 // from the fake signaling channel is applied, or when
564 // ResetRtpReceiverObservers below is called.
565 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
566 rtp_receiver_observers() {
567 return rtp_receiver_observers_;
568 }
569
570 void ResetRtpReceiverObservers() {
571 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100572 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
573 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700574 std::unique_ptr<MockRtpReceiverObserver> observer(
575 new MockRtpReceiverObserver(receiver->media_type()));
576 receiver->SetObserver(observer.get());
577 rtp_receiver_observers_.push_back(std::move(observer));
578 }
579 }
580
Steve Antonede9ca52017-10-16 13:04:27 -0700581 rtc::FakeNetworkManager* network() const {
582 return fake_network_manager_.get();
583 }
584 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
585
Qingsi Wang7685e862018-06-11 20:15:46 -0700586 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
587 return event_log_factory_;
588 }
589
deadbeef1dcb1642017-03-29 21:08:16 -0700590 private:
591 explicit PeerConnectionWrapper(const std::string& debug_name)
592 : debug_name_(debug_name) {}
593
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800594 bool Init(
595 const PeerConnectionFactory::Options* options,
596 const PeerConnectionInterface::RTCConfiguration* config,
597 webrtc::PeerConnectionDependencies dependencies,
598 rtc::Thread* network_thread,
599 rtc::Thread* worker_thread,
600 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
601 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700602 // There's an error in this test code if Init ends up being called twice.
603 RTC_DCHECK(!peer_connection_);
604 RTC_DCHECK(!peer_connection_factory_);
605
606 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700607 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700608
609 std::unique_ptr<cricket::PortAllocator> port_allocator(
610 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700611 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700612 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
613 if (!fake_audio_capture_module_) {
614 return false;
615 }
deadbeef1dcb1642017-03-29 21:08:16 -0700616 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700617
618 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
619 pc_factory_dependencies.network_thread = network_thread;
620 pc_factory_dependencies.worker_thread = worker_thread;
621 pc_factory_dependencies.signaling_thread = signaling_thread;
622 pc_factory_dependencies.media_engine =
623 cricket::WebRtcMediaEngineFactory::Create(
624 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
625 fake_audio_capture_module_),
626 webrtc::CreateBuiltinAudioEncoderFactory(),
627 webrtc::CreateBuiltinAudioDecoderFactory(),
628 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000629 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700630 webrtc::AudioProcessingBuilder().Create());
631 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
632 if (event_log_factory) {
633 event_log_factory_ = event_log_factory.get();
634 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
635 } else {
636 pc_factory_dependencies.event_log_factory =
637 webrtc::CreateRtcEventLogFactory();
638 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800639 if (media_transport_factory) {
640 pc_factory_dependencies.media_transport_factory =
641 std::move(media_transport_factory);
642 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700643 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
644 std::move(pc_factory_dependencies));
645
deadbeef1dcb1642017-03-29 21:08:16 -0700646 if (!peer_connection_factory_) {
647 return false;
648 }
649 if (options) {
650 peer_connection_factory_->SetOptions(*options);
651 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800652 if (config) {
653 sdp_semantics_ = config->sdp_semantics;
654 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700655
656 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200657 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700658 return peer_connection_.get() != nullptr;
659 }
660
661 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700662 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700663 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700664 PeerConnectionInterface::RTCConfiguration modified_config;
665 // If |config| is null, this will result in a default configuration being
666 // used.
667 if (config) {
668 modified_config = *config;
669 }
670 // Disable resolution adaptation; we don't want it interfering with the
671 // test results.
672 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
673 // ratios and not specific resolutions, is this even necessary?
674 modified_config.set_cpu_adaptation(false);
675
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700676 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700677 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700678 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700679 }
680
681 void set_signaling_message_receiver(
682 SignalingMessageReceiver* signaling_message_receiver) {
683 signaling_message_receiver_ = signaling_message_receiver;
684 }
685
686 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
687
Steve Antonede9ca52017-10-16 13:04:27 -0700688 void set_signal_ice_candidates(bool signal) {
689 signal_ice_candidates_ = signal;
690 }
691
deadbeef1dcb1642017-03-29 21:08:16 -0700692 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200693 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700694 // Set max frame rate to 10fps to reduce the risk of test flakiness.
695 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200696 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700697
Niels Möller5c7efe72018-05-11 10:34:46 +0200698 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200699 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
700 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700701 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200702 peer_connection_factory_->CreateVideoTrack(
703 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700704 if (!local_video_renderer_) {
705 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
706 }
707 return track;
708 }
709
710 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100711 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800712 std::unique_ptr<SessionDescriptionInterface> desc =
713 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700714 if (received_sdp_munger_) {
715 received_sdp_munger_(desc->description());
716 }
717
718 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
719 // Setting a remote description may have changed the number of receivers,
720 // so reset the receiver observers.
721 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800722 if (remote_offer_handler_) {
723 remote_offer_handler_();
724 }
deadbeef1dcb1642017-03-29 21:08:16 -0700725 auto answer = CreateAnswer();
726 ASSERT_NE(nullptr, answer);
727 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
728 }
729
730 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100731 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800732 std::unique_ptr<SessionDescriptionInterface> desc =
733 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700734 if (received_sdp_munger_) {
735 received_sdp_munger_(desc->description());
736 }
737
738 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
739 // Set the RtpReceiverObserver after receivers are created.
740 ResetRtpReceiverObservers();
741 }
742
743 // Returns null on failure.
744 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
745 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
746 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
747 pc()->CreateOffer(observer, offer_answer_options_);
748 return WaitForDescriptionFromObserver(observer);
749 }
750
751 // Returns null on failure.
752 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
753 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
754 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
755 pc()->CreateAnswer(observer, offer_answer_options_);
756 return WaitForDescriptionFromObserver(observer);
757 }
758
759 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100760 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700761 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
762 if (!observer->result()) {
763 return nullptr;
764 }
765 auto description = observer->MoveDescription();
766 if (generated_sdp_munger_) {
767 generated_sdp_munger_(description->description());
768 }
769 return description;
770 }
771
772 // Setting the local description and sending the SDP message over the fake
773 // signaling channel are combined into the same method because the SDP
774 // message needs to be sent as soon as SetLocalDescription finishes, without
775 // waiting for the observer to be called. This ensures that ICE candidates
776 // don't outrace the description.
777 bool SetLocalDescriptionAndSendSdpMessage(
778 std::unique_ptr<SessionDescriptionInterface> desc) {
779 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
780 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100781 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800782 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700783 std::string sdp;
784 EXPECT_TRUE(desc->ToString(&sdp));
785 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800786 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
787 RemoveUnusedVideoRenderers();
788 }
deadbeef1dcb1642017-03-29 21:08:16 -0700789 // As mentioned above, we need to send the message immediately after
790 // SetLocalDescription.
791 SendSdpMessage(type, sdp);
792 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
793 return true;
794 }
795
796 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
797 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
798 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100799 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700800 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800801 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
802 RemoveUnusedVideoRenderers();
803 }
deadbeef1dcb1642017-03-29 21:08:16 -0700804 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
805 return observer->result();
806 }
807
Seth Hampson2f0d7022018-02-20 11:54:42 -0800808 // This is a work around to remove unused fake_video_renderers from
809 // transceivers that have either stopped or are no longer receiving.
810 void RemoveUnusedVideoRenderers() {
811 auto transceivers = pc()->GetTransceivers();
812 for (auto& transceiver : transceivers) {
813 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
814 continue;
815 }
816 // Remove fake video renderers from any stopped transceivers.
817 if (transceiver->stopped()) {
818 auto it =
819 fake_video_renderers_.find(transceiver->receiver()->track()->id());
820 if (it != fake_video_renderers_.end()) {
821 fake_video_renderers_.erase(it);
822 }
823 }
824 // Remove fake video renderers from any transceivers that are no longer
825 // receiving.
826 if ((transceiver->current_direction() &&
827 !webrtc::RtpTransceiverDirectionHasRecv(
828 *transceiver->current_direction()))) {
829 auto it =
830 fake_video_renderers_.find(transceiver->receiver()->track()->id());
831 if (it != fake_video_renderers_.end()) {
832 fake_video_renderers_.erase(it);
833 }
834 }
835 }
836 }
837
deadbeef1dcb1642017-03-29 21:08:16 -0700838 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
839 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700841 if (signaling_delay_ms_ == 0) {
842 RelaySdpMessageIfReceiverExists(type, msg);
843 } else {
844 invoker_.AsyncInvokeDelayed<void>(
845 RTC_FROM_HERE, rtc::Thread::Current(),
846 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
847 this, type, msg),
848 signaling_delay_ms_);
849 }
850 }
851
Steve Antona3a92c22017-12-07 10:27:41 -0800852 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700853 if (signaling_message_receiver_) {
854 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
855 }
856 }
857
858 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
859 // default).
860 void SendIceMessage(const std::string& sdp_mid,
861 int sdp_mline_index,
862 const std::string& msg) {
863 if (signaling_delay_ms_ == 0) {
864 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
865 } else {
866 invoker_.AsyncInvokeDelayed<void>(
867 RTC_FROM_HERE, rtc::Thread::Current(),
868 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
869 this, sdp_mid, sdp_mline_index, msg),
870 signaling_delay_ms_);
871 }
872 }
873
874 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
875 int sdp_mline_index,
876 const std::string& msg) {
877 if (signaling_message_receiver_) {
878 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
879 msg);
880 }
881 }
882
883 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800884 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
885 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700886 HandleIncomingOffer(msg);
887 } else {
888 HandleIncomingAnswer(msg);
889 }
890 }
891
892 void ReceiveIceMessage(const std::string& sdp_mid,
893 int sdp_mline_index,
894 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100895 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700896 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
897 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
898 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
899 }
900
901 // PeerConnectionObserver callbacks.
902 void OnSignalingChange(
903 webrtc::PeerConnectionInterface::SignalingState new_state) override {
904 EXPECT_EQ(pc()->signaling_state(), new_state);
905 }
Steve Anton15324772018-01-16 10:26:49 -0800906 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
907 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
908 streams) override {
909 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
910 rtc::scoped_refptr<VideoTrackInterface> video_track(
911 static_cast<VideoTrackInterface*>(receiver->track().get()));
912 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700913 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800914 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200915 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700916 }
917 }
Steve Anton15324772018-01-16 10:26:49 -0800918 void OnRemoveTrack(
919 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
920 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
921 auto it = fake_video_renderers_.find(receiver->track()->id());
922 RTC_DCHECK(it != fake_video_renderers_.end());
923 fake_video_renderers_.erase(it);
924 }
925 }
deadbeef1dcb1642017-03-29 21:08:16 -0700926 void OnRenegotiationNeeded() override {}
927 void OnIceConnectionChange(
928 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
929 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700930 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700931 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200932 void OnConnectionChange(
933 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
934 peer_connection_state_history_.push_back(new_state);
935 }
936
deadbeef1dcb1642017-03-29 21:08:16 -0700937 void OnIceGatheringChange(
938 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700939 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700940 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700941 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700942 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceIceCandidate(
943 const webrtc::IceCandidateInterface* candidate) {
944 std::string candidate_string;
945 candidate->ToString(&candidate_string);
946
947 auto owned_candidate =
948 local_ice_candidate_replacer_->ReplaceCandidate(candidate);
949 if (!owned_candidate) {
950 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer dropped \""
951 << candidate_string << "\"";
952 return nullptr;
953 }
954 std::string owned_candidate_string;
955 owned_candidate->ToString(&owned_candidate_string);
956 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer changed \""
957 << candidate_string << "\" to \"" << owned_candidate_string
958 << "\"";
959 return owned_candidate;
960 }
deadbeef1dcb1642017-03-29 21:08:16 -0700961 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100962 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700963
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700964 const webrtc::IceCandidateInterface* new_candidate = candidate;
965 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate;
966 if (local_ice_candidate_replacer_) {
967 owned_candidate = ReplaceIceCandidate(candidate);
968 if (!owned_candidate) {
969 return; // The candidate was dropped.
970 }
971 new_candidate = owned_candidate.get();
972 }
973
deadbeef1dcb1642017-03-29 21:08:16 -0700974 std::string ice_sdp;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700975 EXPECT_TRUE(new_candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700976 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700977 // Remote party may be deleted.
978 return;
979 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700980 SendIceMessage(new_candidate->sdp_mid(), new_candidate->sdp_mline_index(),
981 ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700982 }
983 void OnDataChannel(
984 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100985 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700986 data_channel_ = data_channel;
987 data_observer_.reset(new MockDataChannelObserver(data_channel));
988 }
989
deadbeef1dcb1642017-03-29 21:08:16 -0700990 std::string debug_name_;
991
992 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
993
994 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
995 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
996 peer_connection_factory_;
997
Steve Antonede9ca52017-10-16 13:04:27 -0700998 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700999 // Needed to keep track of number of frames sent.
1000 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1001 // Needed to keep track of number of frames received.
1002 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1003 fake_video_renderers_;
1004 // Needed to ensure frames aren't received for removed tracks.
1005 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1006 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001007
1008 // For remote peer communication.
1009 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1010 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001011 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -07001012
Niels Möller5c7efe72018-05-11 10:34:46 +02001013 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001014 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001015 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1016 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001017 // |local_video_renderer_| attached to the first created local video track.
1018 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1019
Seth Hampson2f0d7022018-02-20 11:54:42 -08001020 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001021 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1022 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1023 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001024 std::function<void()> remote_offer_handler_;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07001025 std::unique_ptr<IceCandidateReplacerInterface> local_ice_candidate_replacer_;
deadbeef1dcb1642017-03-29 21:08:16 -07001026 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1027 std::unique_ptr<MockDataChannelObserver> data_observer_;
1028
1029 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1030
Steve Antonede9ca52017-10-16 13:04:27 -07001031 std::vector<PeerConnectionInterface::IceConnectionState>
1032 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001033 std::vector<PeerConnectionInterface::PeerConnectionState>
1034 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001035 std::vector<PeerConnectionInterface::IceGatheringState>
1036 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001037
Qingsi Wang7685e862018-06-11 20:15:46 -07001038 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1039
deadbeef1dcb1642017-03-29 21:08:16 -07001040 rtc::AsyncInvoker invoker_;
1041
Seth Hampson2f0d7022018-02-20 11:54:42 -08001042 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001043};
1044
Elad Alon99c3fe52017-10-13 16:29:40 +02001045class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1046 public:
1047 virtual ~MockRtcEventLogOutput() = default;
1048 MOCK_CONST_METHOD0(IsActive, bool());
1049 MOCK_METHOD1(Write, bool(const std::string&));
1050};
1051
Seth Hampson2f0d7022018-02-20 11:54:42 -08001052// This helper object is used for both specifying how many audio/video frames
1053// are expected to be received for a caller/callee. It provides helper functions
1054// to specify these expectations. The object initially starts in a state of no
1055// expectations.
1056class MediaExpectations {
1057 public:
1058 enum ExpectFrames {
1059 kExpectSomeFrames,
1060 kExpectNoFrames,
1061 kNoExpectation,
1062 };
1063
1064 void ExpectBidirectionalAudioAndVideo() {
1065 ExpectBidirectionalAudio();
1066 ExpectBidirectionalVideo();
1067 }
1068
1069 void ExpectBidirectionalAudio() {
1070 CallerExpectsSomeAudio();
1071 CalleeExpectsSomeAudio();
1072 }
1073
1074 void ExpectNoAudio() {
1075 CallerExpectsNoAudio();
1076 CalleeExpectsNoAudio();
1077 }
1078
1079 void ExpectBidirectionalVideo() {
1080 CallerExpectsSomeVideo();
1081 CalleeExpectsSomeVideo();
1082 }
1083
1084 void ExpectNoVideo() {
1085 CallerExpectsNoVideo();
1086 CalleeExpectsNoVideo();
1087 }
1088
1089 void CallerExpectsSomeAudioAndVideo() {
1090 CallerExpectsSomeAudio();
1091 CallerExpectsSomeVideo();
1092 }
1093
1094 void CalleeExpectsSomeAudioAndVideo() {
1095 CalleeExpectsSomeAudio();
1096 CalleeExpectsSomeVideo();
1097 }
1098
1099 // Caller's audio functions.
1100 void CallerExpectsSomeAudio(
1101 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1102 caller_audio_expectation_ = kExpectSomeFrames;
1103 caller_audio_frames_expected_ = expected_audio_frames;
1104 }
1105
1106 void CallerExpectsNoAudio() {
1107 caller_audio_expectation_ = kExpectNoFrames;
1108 caller_audio_frames_expected_ = 0;
1109 }
1110
1111 // Caller's video functions.
1112 void CallerExpectsSomeVideo(
1113 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1114 caller_video_expectation_ = kExpectSomeFrames;
1115 caller_video_frames_expected_ = expected_video_frames;
1116 }
1117
1118 void CallerExpectsNoVideo() {
1119 caller_video_expectation_ = kExpectNoFrames;
1120 caller_video_frames_expected_ = 0;
1121 }
1122
1123 // Callee's audio functions.
1124 void CalleeExpectsSomeAudio(
1125 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1126 callee_audio_expectation_ = kExpectSomeFrames;
1127 callee_audio_frames_expected_ = expected_audio_frames;
1128 }
1129
1130 void CalleeExpectsNoAudio() {
1131 callee_audio_expectation_ = kExpectNoFrames;
1132 callee_audio_frames_expected_ = 0;
1133 }
1134
1135 // Callee's video functions.
1136 void CalleeExpectsSomeVideo(
1137 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1138 callee_video_expectation_ = kExpectSomeFrames;
1139 callee_video_frames_expected_ = expected_video_frames;
1140 }
1141
1142 void CalleeExpectsNoVideo() {
1143 callee_video_expectation_ = kExpectNoFrames;
1144 callee_video_frames_expected_ = 0;
1145 }
1146
1147 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1148 ExpectFrames caller_video_expectation_ = kNoExpectation;
1149 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1150 ExpectFrames callee_video_expectation_ = kNoExpectation;
1151 int caller_audio_frames_expected_ = 0;
1152 int caller_video_frames_expected_ = 0;
1153 int callee_audio_frames_expected_ = 0;
1154 int callee_video_frames_expected_ = 0;
1155};
1156
deadbeef1dcb1642017-03-29 21:08:16 -07001157// Tests two PeerConnections connecting to each other end-to-end, using a
1158// virtual network, fake A/V capture and fake encoder/decoders. The
1159// PeerConnections share the threads/socket servers, but use separate versions
1160// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001161class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001162 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001163 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1164 : sdp_semantics_(sdp_semantics),
1165 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001166 fss_(new rtc::FirewallSocketServer(ss_.get())),
1167 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001168 worker_thread_(rtc::Thread::Create()),
1169 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001170 network_thread_->SetName("PCNetworkThread", this);
1171 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001172 RTC_CHECK(network_thread_->Start());
1173 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001174 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001175 }
1176
Seth Hampson2f0d7022018-02-20 11:54:42 -08001177 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001178 // The PeerConnections should deleted before the TurnCustomizers.
1179 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1180 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1181 // that the TurnCustomizer outlives the life of the PeerConnection or else
1182 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001183 if (caller_) {
1184 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001185 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001186 }
1187 if (callee_) {
1188 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001189 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001190 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001191
1192 // If turn servers were created for the test they need to be destroyed on
1193 // the network thread.
1194 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1195 turn_servers_.clear();
1196 turn_customizers_.clear();
1197 });
deadbeef1dcb1642017-03-29 21:08:16 -07001198 }
1199
1200 bool SignalingStateStable() {
1201 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1202 }
1203
deadbeef71452802017-05-07 17:21:01 -07001204 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001205 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1206 // are connected. This is an important distinction. Once we have separate
1207 // ICE and DTLS state, this check needs to use the DTLS state.
1208 return (callee()->ice_connection_state() ==
1209 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1210 callee()->ice_connection_state() ==
1211 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1212 (caller()->ice_connection_state() ==
1213 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1214 caller()->ice_connection_state() ==
1215 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001216 }
1217
Qingsi Wang7685e862018-06-11 20:15:46 -07001218 // When |event_log_factory| is null, the default implementation of the event
1219 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001220 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1221 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001222 const PeerConnectionFactory::Options* options,
1223 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001224 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001225 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1226 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001227 RTCConfiguration modified_config;
1228 if (config) {
1229 modified_config = *config;
1230 }
Steve Anton3acffc32018-04-12 17:21:03 -07001231 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001232 if (!dependencies.cert_generator) {
1233 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001234 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001235 }
1236 std::unique_ptr<PeerConnectionWrapper> client(
1237 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001238
Niels Möllerf06f9232018-08-07 12:32:18 +02001239 if (!client->Init(options, &modified_config, std::move(dependencies),
1240 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001241 std::move(event_log_factory),
1242 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001243 return nullptr;
1244 }
1245 return client;
1246 }
1247
Qingsi Wang7685e862018-06-11 20:15:46 -07001248 std::unique_ptr<PeerConnectionWrapper>
1249 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1250 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001251 const PeerConnectionFactory::Options* options,
1252 const RTCConfiguration* config,
1253 webrtc::PeerConnectionDependencies dependencies) {
1254 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1255 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001256 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001257 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001258 std::move(event_log_factory),
1259 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001260 }
1261
deadbeef1dcb1642017-03-29 21:08:16 -07001262 bool CreatePeerConnectionWrappers() {
1263 return CreatePeerConnectionWrappersWithConfig(
1264 PeerConnectionInterface::RTCConfiguration(),
1265 PeerConnectionInterface::RTCConfiguration());
1266 }
1267
Steve Anton3acffc32018-04-12 17:21:03 -07001268 bool CreatePeerConnectionWrappersWithSdpSemantics(
1269 SdpSemantics caller_semantics,
1270 SdpSemantics callee_semantics) {
1271 // Can't specify the sdp_semantics in the passed-in configuration since it
1272 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1273 // stored in sdp_semantics_. So get around this by modifying the instance
1274 // variable before calling CreatePeerConnectionWrapper for the caller and
1275 // callee PeerConnections.
1276 SdpSemantics original_semantics = sdp_semantics_;
1277 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001278 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001279 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001280 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001281 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001282 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001283 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001284 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001285 sdp_semantics_ = original_semantics;
1286 return caller_ && callee_;
1287 }
1288
deadbeef1dcb1642017-03-29 21:08:16 -07001289 bool CreatePeerConnectionWrappersWithConfig(
1290 const PeerConnectionInterface::RTCConfiguration& caller_config,
1291 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001292 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001293 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001294 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1295 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001296 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001297 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001298 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1299 /*media_transport_factory=*/nullptr);
1300 return caller_ && callee_;
1301 }
1302
1303 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1304 const PeerConnectionInterface::RTCConfiguration& caller_config,
1305 const PeerConnectionInterface::RTCConfiguration& callee_config,
1306 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1307 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1308 caller_ =
1309 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1310 webrtc::PeerConnectionDependencies(nullptr),
1311 nullptr, std::move(caller_factory));
1312 callee_ =
1313 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1314 webrtc::PeerConnectionDependencies(nullptr),
1315 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001316 return caller_ && callee_;
1317 }
1318
1319 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1320 const PeerConnectionInterface::RTCConfiguration& caller_config,
1321 webrtc::PeerConnectionDependencies caller_dependencies,
1322 const PeerConnectionInterface::RTCConfiguration& callee_config,
1323 webrtc::PeerConnectionDependencies callee_dependencies) {
1324 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001325 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001326 std::move(caller_dependencies), nullptr,
1327 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001328 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001329 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001330 std::move(callee_dependencies), nullptr,
1331 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001332 return caller_ && callee_;
1333 }
1334
1335 bool CreatePeerConnectionWrappersWithOptions(
1336 const PeerConnectionFactory::Options& caller_options,
1337 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001338 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001339 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001340 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1341 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001342 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001343 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001344 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1345 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001346 return caller_ && callee_;
1347 }
1348
1349 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1350 PeerConnectionInterface::RTCConfiguration default_config;
1351 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001352 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001353 webrtc::PeerConnectionDependencies(nullptr));
1354 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001355 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001356 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001357 return caller_ && callee_;
1358 }
1359
Seth Hampson2f0d7022018-02-20 11:54:42 -08001360 std::unique_ptr<PeerConnectionWrapper>
1361 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001362 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1363 new FakeRTCCertificateGenerator());
1364 cert_generator->use_alternate_key();
1365
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001366 webrtc::PeerConnectionDependencies dependencies(nullptr);
1367 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001368 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001369 std::move(dependencies), nullptr,
1370 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001371 }
1372
Seth Hampsonaed71642018-06-11 07:41:32 -07001373 cricket::TestTurnServer* CreateTurnServer(
1374 rtc::SocketAddress internal_address,
1375 rtc::SocketAddress external_address,
1376 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1377 const std::string& common_name = "test turn server") {
1378 rtc::Thread* thread = network_thread();
1379 std::unique_ptr<cricket::TestTurnServer> turn_server =
1380 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1381 RTC_FROM_HERE,
1382 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001383 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001384 thread, internal_address, external_address, type,
1385 /*ignore_bad_certs=*/true, common_name);
1386 });
1387 turn_servers_.push_back(std::move(turn_server));
1388 // Interactions with the turn server should be done on the network thread.
1389 return turn_servers_.back().get();
1390 }
1391
1392 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1393 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1394 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1395 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001396 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001397 turn_customizers_.push_back(std::move(turn_customizer));
1398 // Interactions with the turn customizer should be done on the network
1399 // thread.
1400 return turn_customizers_.back().get();
1401 }
1402
1403 // Checks that the function counters for a TestTurnCustomizer are greater than
1404 // 0.
1405 void ExpectTurnCustomizerCountersIncremented(
1406 cricket::TestTurnCustomizer* turn_customizer) {
1407 unsigned int allow_channel_data_counter =
1408 network_thread()->Invoke<unsigned int>(
1409 RTC_FROM_HERE, [turn_customizer] {
1410 return turn_customizer->allow_channel_data_cnt_;
1411 });
1412 EXPECT_GT(allow_channel_data_counter, 0u);
1413 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1414 RTC_FROM_HERE,
1415 [turn_customizer] { return turn_customizer->modify_cnt_; });
1416 EXPECT_GT(modify_counter, 0u);
1417 }
1418
deadbeef1dcb1642017-03-29 21:08:16 -07001419 // Once called, SDP blobs and ICE candidates will be automatically signaled
1420 // between PeerConnections.
1421 void ConnectFakeSignaling() {
1422 caller_->set_signaling_message_receiver(callee_.get());
1423 callee_->set_signaling_message_receiver(caller_.get());
1424 }
1425
Steve Antonede9ca52017-10-16 13:04:27 -07001426 // Once called, SDP blobs will be automatically signaled between
1427 // PeerConnections. Note that ICE candidates will not be signaled unless they
1428 // are in the exchanged SDP blobs.
1429 void ConnectFakeSignalingForSdpOnly() {
1430 ConnectFakeSignaling();
1431 SetSignalIceCandidates(false);
1432 }
1433
deadbeef1dcb1642017-03-29 21:08:16 -07001434 void SetSignalingDelayMs(int delay_ms) {
1435 caller_->set_signaling_delay_ms(delay_ms);
1436 callee_->set_signaling_delay_ms(delay_ms);
1437 }
1438
Steve Antonede9ca52017-10-16 13:04:27 -07001439 void SetSignalIceCandidates(bool signal) {
1440 caller_->set_signal_ice_candidates(signal);
1441 callee_->set_signal_ice_candidates(signal);
1442 }
1443
deadbeef1dcb1642017-03-29 21:08:16 -07001444 // Messages may get lost on the unreliable DataChannel, so we send multiple
1445 // times to avoid test flakiness.
1446 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1447 const std::string& data,
1448 int retries) {
1449 for (int i = 0; i < retries; ++i) {
1450 dc->Send(DataBuffer(data));
1451 }
1452 }
1453
1454 rtc::Thread* network_thread() { return network_thread_.get(); }
1455
1456 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1457
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001458 webrtc::MediaTransportPair* loopback_media_transports() {
1459 return &loopback_media_transports_;
1460 }
1461
deadbeef1dcb1642017-03-29 21:08:16 -07001462 PeerConnectionWrapper* caller() { return caller_.get(); }
1463
1464 // Set the |caller_| to the |wrapper| passed in and return the
1465 // original |caller_|.
1466 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1467 PeerConnectionWrapper* wrapper) {
1468 PeerConnectionWrapper* old = caller_.release();
1469 caller_.reset(wrapper);
1470 return old;
1471 }
1472
1473 PeerConnectionWrapper* callee() { return callee_.get(); }
1474
1475 // Set the |callee_| to the |wrapper| passed in and return the
1476 // original |callee_|.
1477 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1478 PeerConnectionWrapper* wrapper) {
1479 PeerConnectionWrapper* old = callee_.release();
1480 callee_.reset(wrapper);
1481 return old;
1482 }
1483
Steve Antonede9ca52017-10-16 13:04:27 -07001484 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1485
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486 // Expects the provided number of new frames to be received within
1487 // kMaxWaitForFramesMs. The new expected frames are specified in
1488 // |media_expectations|. Returns false if any of the expectations were
1489 // not met.
1490 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1491 // First initialize the expected frame counts based upon the current
1492 // frame count.
1493 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1494 if (media_expectations.caller_audio_expectation_ ==
1495 MediaExpectations::kExpectSomeFrames) {
1496 total_caller_audio_frames_expected +=
1497 media_expectations.caller_audio_frames_expected_;
1498 }
1499 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001500 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 if (media_expectations.caller_video_expectation_ ==
1502 MediaExpectations::kExpectSomeFrames) {
1503 total_caller_video_frames_expected +=
1504 media_expectations.caller_video_frames_expected_;
1505 }
1506 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1507 if (media_expectations.callee_audio_expectation_ ==
1508 MediaExpectations::kExpectSomeFrames) {
1509 total_callee_audio_frames_expected +=
1510 media_expectations.callee_audio_frames_expected_;
1511 }
1512 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001513 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 if (media_expectations.callee_video_expectation_ ==
1515 MediaExpectations::kExpectSomeFrames) {
1516 total_callee_video_frames_expected +=
1517 media_expectations.callee_video_frames_expected_;
1518 }
deadbeef1dcb1642017-03-29 21:08:16 -07001519
Seth Hampson2f0d7022018-02-20 11:54:42 -08001520 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001521 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001522 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001523 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001524 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001525 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001526 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001527 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001528 total_callee_video_frames_expected,
1529 kMaxWaitForFramesMs);
1530 bool expectations_correct =
1531 caller()->audio_frames_received() >=
1532 total_caller_audio_frames_expected &&
1533 caller()->min_video_frames_received_per_track() >=
1534 total_caller_video_frames_expected &&
1535 callee()->audio_frames_received() >=
1536 total_callee_audio_frames_expected &&
1537 callee()->min_video_frames_received_per_track() >=
1538 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001539
Seth Hampson2f0d7022018-02-20 11:54:42 -08001540 // After the combined wait, print out a more detailed message upon
1541 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001542 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001543 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001544 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001545 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001546 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001547 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001548 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549 total_callee_video_frames_expected);
1550
1551 // We want to make sure nothing unexpected was received.
1552 if (media_expectations.caller_audio_expectation_ ==
1553 MediaExpectations::kExpectNoFrames) {
1554 EXPECT_EQ(caller()->audio_frames_received(),
1555 total_caller_audio_frames_expected);
1556 if (caller()->audio_frames_received() !=
1557 total_caller_audio_frames_expected) {
1558 expectations_correct = false;
1559 }
1560 }
1561 if (media_expectations.caller_video_expectation_ ==
1562 MediaExpectations::kExpectNoFrames) {
1563 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1564 total_caller_video_frames_expected);
1565 if (caller()->min_video_frames_received_per_track() !=
1566 total_caller_video_frames_expected) {
1567 expectations_correct = false;
1568 }
1569 }
1570 if (media_expectations.callee_audio_expectation_ ==
1571 MediaExpectations::kExpectNoFrames) {
1572 EXPECT_EQ(callee()->audio_frames_received(),
1573 total_callee_audio_frames_expected);
1574 if (callee()->audio_frames_received() !=
1575 total_callee_audio_frames_expected) {
1576 expectations_correct = false;
1577 }
1578 }
1579 if (media_expectations.callee_video_expectation_ ==
1580 MediaExpectations::kExpectNoFrames) {
1581 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1582 total_callee_video_frames_expected);
1583 if (callee()->min_video_frames_received_per_track() !=
1584 total_callee_video_frames_expected) {
1585 expectations_correct = false;
1586 }
1587 }
1588 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001589 }
1590
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001591 void TestNegotiatedCipherSuite(
1592 const PeerConnectionFactory::Options& caller_options,
1593 const PeerConnectionFactory::Options& callee_options,
1594 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001595 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1596 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001597 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001598 caller()->AddAudioVideoTracks();
1599 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001600 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001601 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001602 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001603 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001604 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001605 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001606 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1607 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001608 }
1609
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001610 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1611 bool remote_gcm_enabled,
1612 int expected_cipher_suite) {
1613 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001614 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1615 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001616 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001617 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1618 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001619 TestNegotiatedCipherSuite(caller_options, callee_options,
1620 expected_cipher_suite);
1621 }
1622
Seth Hampson2f0d7022018-02-20 11:54:42 -08001623 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001624 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001625
deadbeef1dcb1642017-03-29 21:08:16 -07001626 private:
1627 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001628 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001629 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001630 // |network_thread_| and |worker_thread_| are used by both
1631 // |caller_| and |callee_| so they must be destroyed
1632 // later.
1633 std::unique_ptr<rtc::Thread> network_thread_;
1634 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001635 // The turn servers and turn customizers should be accessed & deleted on the
1636 // network thread to avoid a race with the socket read/write that occurs
1637 // on the network thread.
1638 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1639 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001640 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001641 std::unique_ptr<PeerConnectionWrapper> caller_;
1642 std::unique_ptr<PeerConnectionWrapper> callee_;
1643};
1644
Seth Hampson2f0d7022018-02-20 11:54:42 -08001645class PeerConnectionIntegrationTest
1646 : public PeerConnectionIntegrationBaseTest,
1647 public ::testing::WithParamInterface<SdpSemantics> {
1648 protected:
1649 PeerConnectionIntegrationTest()
1650 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1651};
1652
1653class PeerConnectionIntegrationTestPlanB
1654 : public PeerConnectionIntegrationBaseTest {
1655 protected:
1656 PeerConnectionIntegrationTestPlanB()
1657 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1658};
1659
1660class PeerConnectionIntegrationTestUnifiedPlan
1661 : public PeerConnectionIntegrationBaseTest {
1662 protected:
1663 PeerConnectionIntegrationTestUnifiedPlan()
1664 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1665};
1666
deadbeef1dcb1642017-03-29 21:08:16 -07001667// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1668// includes testing that the callback is invoked if an observer is connected
1669// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001670TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001671 RtpReceiverObserverOnFirstPacketReceived) {
1672 ASSERT_TRUE(CreatePeerConnectionWrappers());
1673 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001674 caller()->AddAudioVideoTracks();
1675 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001676 // Start offer/answer exchange and wait for it to complete.
1677 caller()->CreateAndSetAndSignalOffer();
1678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1679 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001680 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1681 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001682 // Wait for all "first packet received" callbacks to be fired.
1683 EXPECT_TRUE_WAIT(
1684 std::all_of(caller()->rtp_receiver_observers().begin(),
1685 caller()->rtp_receiver_observers().end(),
1686 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1687 return o->first_packet_received();
1688 }),
1689 kMaxWaitForFramesMs);
1690 EXPECT_TRUE_WAIT(
1691 std::all_of(callee()->rtp_receiver_observers().begin(),
1692 callee()->rtp_receiver_observers().end(),
1693 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1694 return o->first_packet_received();
1695 }),
1696 kMaxWaitForFramesMs);
1697 // If new observers are set after the first packet was already received, the
1698 // callback should still be invoked.
1699 caller()->ResetRtpReceiverObservers();
1700 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001701 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1702 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001703 EXPECT_TRUE(
1704 std::all_of(caller()->rtp_receiver_observers().begin(),
1705 caller()->rtp_receiver_observers().end(),
1706 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1707 return o->first_packet_received();
1708 }));
1709 EXPECT_TRUE(
1710 std::all_of(callee()->rtp_receiver_observers().begin(),
1711 callee()->rtp_receiver_observers().end(),
1712 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1713 return o->first_packet_received();
1714 }));
1715}
1716
1717class DummyDtmfObserver : public DtmfSenderObserverInterface {
1718 public:
1719 DummyDtmfObserver() : completed_(false) {}
1720
1721 // Implements DtmfSenderObserverInterface.
1722 void OnToneChange(const std::string& tone) override {
1723 tones_.push_back(tone);
1724 if (tone.empty()) {
1725 completed_ = true;
1726 }
1727 }
1728
1729 const std::vector<std::string>& tones() const { return tones_; }
1730 bool completed() const { return completed_; }
1731
1732 private:
1733 bool completed_;
1734 std::vector<std::string> tones_;
1735};
1736
1737// Assumes |sender| already has an audio track added and the offer/answer
1738// exchange is done.
1739void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1740 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001741 // We should be able to get a DTMF sender from the local sender.
1742 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1743 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1744 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001745 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001746 dtmf_sender->RegisterObserver(&observer);
1747
1748 // Test the DtmfSender object just created.
1749 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1750 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1751
1752 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1753 std::vector<std::string> tones = {"1", "a", ""};
1754 EXPECT_EQ(tones, observer.tones());
1755 dtmf_sender->UnregisterObserver();
1756 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1757}
1758
1759// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1760// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001761TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001762 ASSERT_TRUE(CreatePeerConnectionWrappers());
1763 ConnectFakeSignaling();
1764 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001765 caller()->AddAudioTrack();
1766 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001767 caller()->CreateAndSetAndSignalOffer();
1768 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001769 // DTLS must finish before the DTMF sender can be used reliably.
1770 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001771 TestDtmfFromSenderToReceiver(caller(), callee());
1772 TestDtmfFromSenderToReceiver(callee(), caller());
1773}
1774
1775// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1776// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001777TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001778 ASSERT_TRUE(CreatePeerConnectionWrappers());
1779 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001780
deadbeef1dcb1642017-03-29 21:08:16 -07001781 // Do normal offer/answer and wait for some frames to be received in each
1782 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001783 caller()->AddAudioVideoTracks();
1784 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001785 caller()->CreateAndSetAndSignalOffer();
1786 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001787 MediaExpectations media_expectations;
1788 media_expectations.ExpectBidirectionalAudioAndVideo();
1789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001790 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1791 webrtc::kEnumCounterKeyProtocolDtls));
1792 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1793 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001794}
1795
1796// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001797TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001798 PeerConnectionInterface::RTCConfiguration sdes_config;
1799 sdes_config.enable_dtls_srtp.emplace(false);
1800 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1801 ConnectFakeSignaling();
1802
1803 // Do normal offer/answer and wait for some frames to be received in each
1804 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001805 caller()->AddAudioVideoTracks();
1806 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001807 caller()->CreateAndSetAndSignalOffer();
1808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001809 MediaExpectations media_expectations;
1810 media_expectations.ExpectBidirectionalAudioAndVideo();
1811 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001812 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1813 webrtc::kEnumCounterKeyProtocolSdes));
1814 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1815 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001816}
1817
Steve Anton8c0f7a72017-10-03 10:03:10 -07001818// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1819// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001820TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001821 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1822 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1823 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1824 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1825 return pc->GetRemoteAudioSSLCertificate();
1826 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001827 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1828 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1829 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1830 return pc->GetRemoteAudioSSLCertChain();
1831 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001832
1833 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1834 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1835
1836 // Configure each side with a known certificate so they can be compared later.
1837 PeerConnectionInterface::RTCConfiguration caller_config;
1838 caller_config.enable_dtls_srtp.emplace(true);
1839 caller_config.certificates.push_back(caller_cert);
1840 PeerConnectionInterface::RTCConfiguration callee_config;
1841 callee_config.enable_dtls_srtp.emplace(true);
1842 callee_config.certificates.push_back(callee_cert);
1843 ASSERT_TRUE(
1844 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1845 ConnectFakeSignaling();
1846
1847 // When first initialized, there should not be a remote SSL certificate (and
1848 // calling this method should not crash).
1849 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1850 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001851 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1852 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001853
Steve Anton15324772018-01-16 10:26:49 -08001854 caller()->AddAudioTrack();
1855 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001856 caller()->CreateAndSetAndSignalOffer();
1857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1858 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1859
1860 // Once DTLS has been connected, each side should return the other's SSL
1861 // certificate when calling GetRemoteAudioSSLCertificate.
1862
1863 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1864 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001865 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001866 caller_remote_cert->ToPEMString());
1867
1868 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1869 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001870 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001871 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001872
1873 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1874 ASSERT_TRUE(caller_remote_cert_chain);
1875 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1876 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001877 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001878 remote_cert->ToPEMString());
1879
1880 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1881 ASSERT_TRUE(callee_remote_cert_chain);
1882 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1883 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001884 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001885 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001886}
1887
deadbeef1dcb1642017-03-29 21:08:16 -07001888// This test sets up a call between two parties with a source resolution of
1889// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001890TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001891 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1892 ASSERT_TRUE(CreatePeerConnectionWrappers());
1893 ConnectFakeSignaling();
1894
Niels Möller5c7efe72018-05-11 10:34:46 +02001895 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1896 webrtc::FakePeriodicVideoSource::Config config;
1897 config.width = 1280;
1898 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001899 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001900 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1901 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001902
1903 // Do normal offer/answer and wait for at least one frame to be received in
1904 // each direction.
1905 caller()->CreateAndSetAndSignalOffer();
1906 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1907 callee()->min_video_frames_received_per_track() > 0,
1908 kMaxWaitForFramesMs);
1909
1910 // Check rendered aspect ratio.
1911 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1912 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1913 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1914 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1915}
1916
1917// This test sets up an one-way call, with media only from caller to
1918// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001919TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001920 ASSERT_TRUE(CreatePeerConnectionWrappers());
1921 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001922 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001923 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001924 MediaExpectations media_expectations;
1925 media_expectations.CalleeExpectsSomeAudioAndVideo();
1926 media_expectations.CallerExpectsNoAudio();
1927 media_expectations.CallerExpectsNoVideo();
1928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001929}
1930
1931// This test sets up a audio call initially, with the callee rejecting video
1932// initially. Then later the callee decides to upgrade to audio/video, and
1933// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001934TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001935 ASSERT_TRUE(CreatePeerConnectionWrappers());
1936 ConnectFakeSignaling();
1937 // Initially, offer an audio/video stream from the caller, but refuse to
1938 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001939 caller()->AddAudioVideoTracks();
1940 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001941 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1942 PeerConnectionInterface::RTCOfferAnswerOptions options;
1943 options.offer_to_receive_video = 0;
1944 callee()->SetOfferAnswerOptions(options);
1945 } else {
1946 callee()->SetRemoteOfferHandler([this] {
1947 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1948 });
1949 }
deadbeef1dcb1642017-03-29 21:08:16 -07001950 // Do offer/answer and make sure audio is still received end-to-end.
1951 caller()->CreateAndSetAndSignalOffer();
1952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001953 {
1954 MediaExpectations media_expectations;
1955 media_expectations.ExpectBidirectionalAudio();
1956 media_expectations.ExpectNoVideo();
1957 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1958 }
deadbeef1dcb1642017-03-29 21:08:16 -07001959 // Sanity check that the callee's description has a rejected video section.
1960 ASSERT_NE(nullptr, callee()->pc()->local_description());
1961 const ContentInfo* callee_video_content =
1962 GetFirstVideoContent(callee()->pc()->local_description()->description());
1963 ASSERT_NE(nullptr, callee_video_content);
1964 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001965
deadbeef1dcb1642017-03-29 21:08:16 -07001966 // Now negotiate with video and ensure negotiation succeeds, with video
1967 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001968 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001969 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1970 PeerConnectionInterface::RTCOfferAnswerOptions options;
1971 options.offer_to_receive_video = 1;
1972 callee()->SetOfferAnswerOptions(options);
1973 } else {
1974 callee()->SetRemoteOfferHandler(nullptr);
1975 caller()->SetRemoteOfferHandler([this] {
1976 // The caller creates a new transceiver to receive video on when receiving
1977 // the offer, but by default it is send only.
1978 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001979 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001980 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1981 transceivers[2]->receiver()->media_type());
1982 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1983 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1984 });
1985 }
deadbeef1dcb1642017-03-29 21:08:16 -07001986 callee()->CreateAndSetAndSignalOffer();
1987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988 {
1989 // Expect additional audio frames to be received after the upgrade.
1990 MediaExpectations media_expectations;
1991 media_expectations.ExpectBidirectionalAudioAndVideo();
1992 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1993 }
deadbeef1dcb1642017-03-29 21:08:16 -07001994}
1995
deadbeef4389b4d2017-09-07 09:07:36 -07001996// Simpler than the above test; just add an audio track to an established
1997// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001999 ASSERT_TRUE(CreatePeerConnectionWrappers());
2000 ConnectFakeSignaling();
2001 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002002 caller()->AddVideoTrack();
2003 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002004 caller()->CreateAndSetAndSignalOffer();
2005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2006 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioTrack();
2008 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002012 MediaExpectations media_expectations;
2013 media_expectations.ExpectBidirectionalAudioAndVideo();
2014 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002015}
2016
deadbeef1dcb1642017-03-29 21:08:16 -07002017// This test sets up a call that's transferred to a new caller with a different
2018// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002019TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002020 ASSERT_TRUE(CreatePeerConnectionWrappers());
2021 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002022 caller()->AddAudioVideoTracks();
2023 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002024 caller()->CreateAndSetAndSignalOffer();
2025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2026
2027 // Keep the original peer around which will still send packets to the
2028 // receiving client. These SRTP packets will be dropped.
2029 std::unique_ptr<PeerConnectionWrapper> original_peer(
2030 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002031 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002032 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2033 // directly above.
2034 original_peer->pc()->Close();
2035
2036 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002037 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002038 caller()->CreateAndSetAndSignalOffer();
2039 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2040 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041 MediaExpectations media_expectations;
2042 media_expectations.ExpectBidirectionalAudioAndVideo();
2043 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002044}
2045
2046// This test sets up a call that's transferred to a new callee with a different
2047// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002049 ASSERT_TRUE(CreatePeerConnectionWrappers());
2050 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002051 caller()->AddAudioVideoTracks();
2052 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002053 caller()->CreateAndSetAndSignalOffer();
2054 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2055
2056 // Keep the original peer around which will still send packets to the
2057 // receiving client. These SRTP packets will be dropped.
2058 std::unique_ptr<PeerConnectionWrapper> original_peer(
2059 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002060 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002061 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2062 // directly above.
2063 original_peer->pc()->Close();
2064
2065 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002066 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002067 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2068 caller()->CreateAndSetAndSignalOffer();
2069 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2070 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002071 MediaExpectations media_expectations;
2072 media_expectations.ExpectBidirectionalAudioAndVideo();
2073 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002074}
2075
2076// This test sets up a non-bundled call and negotiates bundling at the same
2077// time as starting an ICE restart. When bundling is in effect in the restart,
2078// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002080 ASSERT_TRUE(CreatePeerConnectionWrappers());
2081 ConnectFakeSignaling();
2082
Steve Anton15324772018-01-16 10:26:49 -08002083 caller()->AddAudioVideoTracks();
2084 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002085 // Remove the bundle group from the SDP received by the callee.
2086 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2087 desc->RemoveGroupByName("BUNDLE");
2088 });
2089 caller()->CreateAndSetAndSignalOffer();
2090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002091 {
2092 MediaExpectations media_expectations;
2093 media_expectations.ExpectBidirectionalAudioAndVideo();
2094 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2095 }
deadbeef1dcb1642017-03-29 21:08:16 -07002096 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2097 callee()->SetReceivedSdpMunger(nullptr);
2098 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2099 caller()->CreateAndSetAndSignalOffer();
2100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2101
2102 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002103 {
2104 MediaExpectations media_expectations;
2105 media_expectations.ExpectBidirectionalAudioAndVideo();
2106 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2107 }
deadbeef1dcb1642017-03-29 21:08:16 -07002108}
2109
2110// Test CVO (Coordination of Video Orientation). If a video source is rotated
2111// and both peers support the CVO RTP header extension, the actual video frames
2112// don't need to be encoded in different resolutions, since the rotation is
2113// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002114TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002115 ASSERT_TRUE(CreatePeerConnectionWrappers());
2116 ConnectFakeSignaling();
2117 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002118 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002119 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002120 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002121 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2122
2123 // Wait for video frames to be received by both sides.
2124 caller()->CreateAndSetAndSignalOffer();
2125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2126 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2127 callee()->min_video_frames_received_per_track() > 0,
2128 kMaxWaitForFramesMs);
2129
2130 // Ensure that the aspect ratio is unmodified.
2131 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2132 // not just assumed.
2133 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2134 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2135 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2136 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2137 // Ensure that the CVO bits were surfaced to the renderer.
2138 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2139 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2140}
2141
2142// Test that when the CVO extension isn't supported, video is rotated the
2143// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002144TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002145 ASSERT_TRUE(CreatePeerConnectionWrappers());
2146 ConnectFakeSignaling();
2147 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002148 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002149 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002150 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002151 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2152
2153 // Remove the CVO extension from the offered SDP.
2154 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2155 cricket::VideoContentDescription* video =
2156 GetFirstVideoContentDescription(desc);
2157 video->ClearRtpHeaderExtensions();
2158 });
2159 // Wait for video frames to be received by both sides.
2160 caller()->CreateAndSetAndSignalOffer();
2161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2162 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2163 callee()->min_video_frames_received_per_track() > 0,
2164 kMaxWaitForFramesMs);
2165
2166 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2167 // rotation.
2168 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2169 // not just assumed.
2170 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2171 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2172 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2173 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2174 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2175 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2176 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2177}
2178
deadbeef1dcb1642017-03-29 21:08:16 -07002179// Test that if the answerer rejects the audio m= section, no audio is sent or
2180// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002181TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002182 ASSERT_TRUE(CreatePeerConnectionWrappers());
2183 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002184 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002185 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2186 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2187 // it will reject the audio m= section completely.
2188 PeerConnectionInterface::RTCOfferAnswerOptions options;
2189 options.offer_to_receive_audio = 0;
2190 callee()->SetOfferAnswerOptions(options);
2191 } else {
2192 // Stopping the audio RtpTransceiver will cause the media section to be
2193 // rejected in the answer.
2194 callee()->SetRemoteOfferHandler([this] {
2195 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2196 });
2197 }
Steve Anton15324772018-01-16 10:26:49 -08002198 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002199 // Do offer/answer and wait for successful end-to-end video frames.
2200 caller()->CreateAndSetAndSignalOffer();
2201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002202 MediaExpectations media_expectations;
2203 media_expectations.ExpectBidirectionalVideo();
2204 media_expectations.ExpectNoAudio();
2205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2206
deadbeef1dcb1642017-03-29 21:08:16 -07002207 // Sanity check that the callee's description has a rejected audio section.
2208 ASSERT_NE(nullptr, callee()->pc()->local_description());
2209 const ContentInfo* callee_audio_content =
2210 GetFirstAudioContent(callee()->pc()->local_description()->description());
2211 ASSERT_NE(nullptr, callee_audio_content);
2212 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002213 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2214 // The caller's transceiver should have stopped after receiving the answer.
2215 EXPECT_TRUE(caller()
2216 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2217 ->stopped());
2218 }
deadbeef1dcb1642017-03-29 21:08:16 -07002219}
2220
2221// Test that if the answerer rejects the video m= section, no video is sent or
2222// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002223TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002224 ASSERT_TRUE(CreatePeerConnectionWrappers());
2225 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002226 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002227 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2228 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2229 // it will reject the video m= section completely.
2230 PeerConnectionInterface::RTCOfferAnswerOptions options;
2231 options.offer_to_receive_video = 0;
2232 callee()->SetOfferAnswerOptions(options);
2233 } else {
2234 // Stopping the video RtpTransceiver will cause the media section to be
2235 // rejected in the answer.
2236 callee()->SetRemoteOfferHandler([this] {
2237 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2238 });
2239 }
Steve Anton15324772018-01-16 10:26:49 -08002240 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002241 // Do offer/answer and wait for successful end-to-end audio frames.
2242 caller()->CreateAndSetAndSignalOffer();
2243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002244 MediaExpectations media_expectations;
2245 media_expectations.ExpectBidirectionalAudio();
2246 media_expectations.ExpectNoVideo();
2247 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2248
deadbeef1dcb1642017-03-29 21:08:16 -07002249 // Sanity check that the callee's description has a rejected video section.
2250 ASSERT_NE(nullptr, callee()->pc()->local_description());
2251 const ContentInfo* callee_video_content =
2252 GetFirstVideoContent(callee()->pc()->local_description()->description());
2253 ASSERT_NE(nullptr, callee_video_content);
2254 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002255 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2256 // The caller's transceiver should have stopped after receiving the answer.
2257 EXPECT_TRUE(caller()
2258 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2259 ->stopped());
2260 }
deadbeef1dcb1642017-03-29 21:08:16 -07002261}
2262
2263// Test that if the answerer rejects both audio and video m= sections, nothing
2264// bad happens.
2265// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2266// test anything but the fact that negotiation succeeds, which doesn't mean
2267// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002268TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002269 ASSERT_TRUE(CreatePeerConnectionWrappers());
2270 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002271 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002272 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2273 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2274 // will reject both audio and video m= sections.
2275 PeerConnectionInterface::RTCOfferAnswerOptions options;
2276 options.offer_to_receive_audio = 0;
2277 options.offer_to_receive_video = 0;
2278 callee()->SetOfferAnswerOptions(options);
2279 } else {
2280 callee()->SetRemoteOfferHandler([this] {
2281 // Stopping all transceivers will cause all media sections to be rejected.
2282 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2283 transceiver->Stop();
2284 }
2285 });
2286 }
deadbeef1dcb1642017-03-29 21:08:16 -07002287 // Do offer/answer and wait for stable signaling state.
2288 caller()->CreateAndSetAndSignalOffer();
2289 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002290
deadbeef1dcb1642017-03-29 21:08:16 -07002291 // Sanity check that the callee's description has rejected m= sections.
2292 ASSERT_NE(nullptr, callee()->pc()->local_description());
2293 const ContentInfo* callee_audio_content =
2294 GetFirstAudioContent(callee()->pc()->local_description()->description());
2295 ASSERT_NE(nullptr, callee_audio_content);
2296 EXPECT_TRUE(callee_audio_content->rejected);
2297 const ContentInfo* callee_video_content =
2298 GetFirstVideoContent(callee()->pc()->local_description()->description());
2299 ASSERT_NE(nullptr, callee_video_content);
2300 EXPECT_TRUE(callee_video_content->rejected);
2301}
2302
2303// This test sets up an audio and video call between two parties. After the
2304// call runs for a while, the caller sends an updated offer with video being
2305// rejected. Once the re-negotiation is done, the video flow should stop and
2306// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002308 ASSERT_TRUE(CreatePeerConnectionWrappers());
2309 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002310 caller()->AddAudioVideoTracks();
2311 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002312 caller()->CreateAndSetAndSignalOffer();
2313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002314 {
2315 MediaExpectations media_expectations;
2316 media_expectations.ExpectBidirectionalAudioAndVideo();
2317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2318 }
deadbeef1dcb1642017-03-29 21:08:16 -07002319 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2321 caller()->SetGeneratedSdpMunger(
2322 [](cricket::SessionDescription* description) {
2323 for (cricket::ContentInfo& content : description->contents()) {
2324 if (cricket::IsVideoContent(&content)) {
2325 content.rejected = true;
2326 }
2327 }
2328 });
2329 } else {
2330 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2331 }
deadbeef1dcb1642017-03-29 21:08:16 -07002332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2334
2335 // Sanity check that the caller's description has a rejected video section.
2336 ASSERT_NE(nullptr, caller()->pc()->local_description());
2337 const ContentInfo* caller_video_content =
2338 GetFirstVideoContent(caller()->pc()->local_description()->description());
2339 ASSERT_NE(nullptr, caller_video_content);
2340 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002341 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002342 {
2343 MediaExpectations media_expectations;
2344 media_expectations.ExpectBidirectionalAudio();
2345 media_expectations.ExpectNoVideo();
2346 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2347 }
deadbeef1dcb1642017-03-29 21:08:16 -07002348}
2349
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002350// Do one offer/answer with audio, another that disables it (rejecting the m=
2351// section), and another that re-enables it. Regression test for:
2352// bugs.webrtc.org/6023
2353TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2354 ASSERT_TRUE(CreatePeerConnectionWrappers());
2355 ConnectFakeSignaling();
2356
2357 // Add audio track, do normal offer/answer.
2358 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2359 caller()->CreateLocalAudioTrack();
2360 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2361 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2364
2365 // Remove audio track, and set offer_to_receive_audio to false to cause the
2366 // m= section to be completely disabled, not just "recvonly".
2367 caller()->pc()->RemoveTrack(sender);
2368 PeerConnectionInterface::RTCOfferAnswerOptions options;
2369 options.offer_to_receive_audio = 0;
2370 caller()->SetOfferAnswerOptions(options);
2371 caller()->CreateAndSetAndSignalOffer();
2372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2373
2374 // Add the audio track again, expecting negotiation to succeed and frames to
2375 // flow.
2376 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2377 options.offer_to_receive_audio = 1;
2378 caller()->SetOfferAnswerOptions(options);
2379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2381
2382 MediaExpectations media_expectations;
2383 media_expectations.CalleeExpectsSomeAudio();
2384 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2385}
2386
deadbeef1dcb1642017-03-29 21:08:16 -07002387// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2388// is needed to support legacy endpoints.
2389// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2390// add a test for an end-to-end test without MID signaling either (basically,
2391// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002392TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002393 ASSERT_TRUE(CreatePeerConnectionWrappers());
2394 ConnectFakeSignaling();
2395 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002396 caller()->AddAudioVideoTracks();
2397 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002398 // Remove SSRCs and MSIDs from the received offer SDP.
2399 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002400 caller()->CreateAndSetAndSignalOffer();
2401 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002402 MediaExpectations media_expectations;
2403 media_expectations.ExpectBidirectionalAudioAndVideo();
2404 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002405}
2406
Seth Hampson5897a6e2018-04-03 11:16:33 -07002407// Basic end-to-end test, without SSRC signaling. This means that the track
2408// was created properly and frames are delivered when the MSIDs are communicated
2409// with a=msid lines and no a=ssrc lines.
2410TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2411 EndToEndCallWithoutSsrcSignaling) {
2412 const char kStreamId[] = "streamId";
2413 ASSERT_TRUE(CreatePeerConnectionWrappers());
2414 ConnectFakeSignaling();
2415 // Add just audio tracks.
2416 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2417 callee()->AddAudioTrack();
2418
2419 // Remove SSRCs from the received offer SDP.
2420 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2421 caller()->CreateAndSetAndSignalOffer();
2422 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2423 MediaExpectations media_expectations;
2424 media_expectations.ExpectBidirectionalAudio();
2425 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2426}
2427
Steve Antondf527fd2018-04-27 15:52:03 -07002428// Tests that video flows between multiple video tracks when SSRCs are not
2429// signaled. This exercises the MID RTP header extension which is needed to
2430// demux the incoming video tracks.
2431TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2432 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2433 ASSERT_TRUE(CreatePeerConnectionWrappers());
2434 ConnectFakeSignaling();
2435 caller()->AddVideoTrack();
2436 caller()->AddVideoTrack();
2437 callee()->AddVideoTrack();
2438 callee()->AddVideoTrack();
2439
2440 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2441 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2442 caller()->CreateAndSetAndSignalOffer();
2443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2444 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2445 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2446
2447 // Expect video to be received in both directions on both tracks.
2448 MediaExpectations media_expectations;
2449 media_expectations.ExpectBidirectionalVideo();
2450 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2451}
2452
deadbeef1dcb1642017-03-29 21:08:16 -07002453// Test that if two video tracks are sent (from caller to callee, in this test),
2454// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002456 ASSERT_TRUE(CreatePeerConnectionWrappers());
2457 ConnectFakeSignaling();
2458 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002459 caller()->AddAudioVideoTracks();
2460 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002461 caller()->CreateAndSetAndSignalOffer();
2462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002463 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002464
2465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudioAndVideo();
2467 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002468}
2469
2470static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2471 bool first = true;
2472 for (cricket::ContentInfo& content : desc->contents()) {
2473 if (first) {
2474 first = false;
2475 continue;
2476 }
2477 content.bundle_only = true;
2478 }
2479 first = true;
2480 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2481 if (first) {
2482 first = false;
2483 continue;
2484 }
2485 transport.description.ice_ufrag.clear();
2486 transport.description.ice_pwd.clear();
2487 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2488 transport.description.identity_fingerprint.reset(nullptr);
2489 }
2490}
2491
2492// Test that if applying a true "max bundle" offer, which uses ports of 0,
2493// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2494// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2495// successfully and media flows.
2496// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2497// TODO(deadbeef): Won't need this test once we start generating actual
2498// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002499TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002500 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2501 ASSERT_TRUE(CreatePeerConnectionWrappers());
2502 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002503 caller()->AddAudioVideoTracks();
2504 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002505 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2506 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2507 // but the first m= section.
2508 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2509 caller()->CreateAndSetAndSignalOffer();
2510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002511 MediaExpectations media_expectations;
2512 media_expectations.ExpectBidirectionalAudioAndVideo();
2513 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002514}
2515
2516// Test that we can receive the audio output level from a remote audio track.
2517// TODO(deadbeef): Use a fake audio source and verify that the output level is
2518// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002520 ASSERT_TRUE(CreatePeerConnectionWrappers());
2521 ConnectFakeSignaling();
2522 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002523 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526
2527 // Get the audio output level stats. Note that the level is not available
2528 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002529 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002530 kMaxWaitForFramesMs);
2531}
2532
2533// Test that an audio input level is reported.
2534// TODO(deadbeef): Use a fake audio source and verify that the input level is
2535// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002536TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002537 ASSERT_TRUE(CreatePeerConnectionWrappers());
2538 ConnectFakeSignaling();
2539 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002540 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002541 caller()->CreateAndSetAndSignalOffer();
2542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2543
2544 // Get the audio input level stats. The level should be available very
2545 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002546 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002547 kMaxWaitForStatsMs);
2548}
2549
2550// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002551TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002552 ASSERT_TRUE(CreatePeerConnectionWrappers());
2553 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002554 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002555 // Do offer/answer, wait for the callee to receive some frames.
2556 caller()->CreateAndSetAndSignalOffer();
2557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558
2559 MediaExpectations media_expectations;
2560 media_expectations.CalleeExpectsSomeAudioAndVideo();
2561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002562
2563 // Get a handle to the remote tracks created, so they can be used as GetStats
2564 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002565 for (auto receiver : callee()->pc()->GetReceivers()) {
2566 // We received frames, so we definitely should have nonzero "received bytes"
2567 // stats at this point.
2568 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2569 0);
2570 }
deadbeef1dcb1642017-03-29 21:08:16 -07002571}
2572
2573// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002574TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002575 ASSERT_TRUE(CreatePeerConnectionWrappers());
2576 ConnectFakeSignaling();
2577 auto audio_track = caller()->CreateLocalAudioTrack();
2578 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002579 caller()->AddTrack(audio_track);
2580 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002581 // Do offer/answer, wait for the callee to receive some frames.
2582 caller()->CreateAndSetAndSignalOffer();
2583 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584 MediaExpectations media_expectations;
2585 media_expectations.CalleeExpectsSomeAudioAndVideo();
2586 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002587
2588 // The callee received frames, so we definitely should have nonzero "sent
2589 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002590 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2591 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2592}
2593
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002594// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002596 ASSERT_TRUE(CreatePeerConnectionWrappers());
2597 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002598 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002599
Steve Anton15324772018-01-16 10:26:49 -08002600 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002601
2602 // Do offer/answer, wait for the callee to receive some frames.
2603 caller()->CreateAndSetAndSignalOffer();
2604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2605
2606 // Get the remote audio track created on the receiver, so they can be used as
2607 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002608 auto receivers = callee()->pc()->GetReceivers();
2609 ASSERT_EQ(1u, receivers.size());
2610 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002611
2612 // Get the audio output level stats. Note that the level is not available
2613 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002614 EXPECT_TRUE_WAIT(
2615 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2616 0,
2617 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002618}
2619
Steve Antona41959e2018-11-28 11:15:33 -08002620// Test that the track ID is associated with all local and remote SSRC stats
2621// using the old GetStats() and more than 1 audio and more than 1 video track.
2622// This is a regression test for crbug.com/906988
2623TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2624 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2625 ASSERT_TRUE(CreatePeerConnectionWrappers());
2626 ConnectFakeSignaling();
2627 auto audio_sender_1 = caller()->AddAudioTrack();
2628 auto video_sender_1 = caller()->AddVideoTrack();
2629 auto audio_sender_2 = caller()->AddAudioTrack();
2630 auto video_sender_2 = caller()->AddVideoTrack();
2631 caller()->CreateAndSetAndSignalOffer();
2632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2633
2634 MediaExpectations media_expectations;
2635 media_expectations.CalleeExpectsSomeAudioAndVideo();
2636 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2637
2638 std::vector<std::string> track_ids = {
2639 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2640 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2641
2642 auto caller_stats = caller()->OldGetStats();
2643 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2644 auto callee_stats = callee()->OldGetStats();
2645 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2646}
2647
deadbeefd8ad7882017-04-18 16:01:17 -07002648// Test that we can get stats (using the new stats implemnetation) for
2649// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2650// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002651TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002652 GetStatsForUnsignaledStreamWithNewStatsApi) {
2653 ASSERT_TRUE(CreatePeerConnectionWrappers());
2654 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002655 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002656 // Remove SSRCs and MSIDs from the received offer SDP.
2657 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2658 caller()->CreateAndSetAndSignalOffer();
2659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002660 MediaExpectations media_expectations;
2661 media_expectations.CalleeExpectsSomeAudio(1);
2662 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002663
2664 // We received a frame, so we should have nonzero "bytes received" stats for
2665 // the unsignaled stream, if stats are working for it.
2666 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2667 callee()->NewGetStats();
2668 ASSERT_NE(nullptr, report);
2669 auto inbound_stream_stats =
2670 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2671 ASSERT_EQ(1U, inbound_stream_stats.size());
2672 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2673 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002674 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2675}
2676
Taylor Brandstettera4653442018-06-19 09:44:26 -07002677// Same as above but for the legacy stats implementation.
2678TEST_P(PeerConnectionIntegrationTest,
2679 GetStatsForUnsignaledStreamWithOldStatsApi) {
2680 ASSERT_TRUE(CreatePeerConnectionWrappers());
2681 ConnectFakeSignaling();
2682 caller()->AddAudioTrack();
2683 // Remove SSRCs and MSIDs from the received offer SDP.
2684 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2685 caller()->CreateAndSetAndSignalOffer();
2686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2687
2688 // Note that, since the old stats implementation associates SSRCs with tracks
2689 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2690 // associated track ID. So we can't use the track "selector" argument.
2691 //
2692 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2693 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002694 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002695 kDefaultTimeout);
2696}
2697
zhihuangf8164932017-05-19 13:09:47 -07002698// Test that we can successfully get the media related stats (audio level
2699// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002700TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002701 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2702 ASSERT_TRUE(CreatePeerConnectionWrappers());
2703 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002704 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002705 // Remove SSRCs and MSIDs from the received offer SDP.
2706 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2707 caller()->CreateAndSetAndSignalOffer();
2708 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002709 MediaExpectations media_expectations;
2710 media_expectations.CalleeExpectsSomeAudio(1);
2711 media_expectations.CalleeExpectsSomeVideo(1);
2712 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002713
2714 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2715 callee()->NewGetStats();
2716 ASSERT_NE(nullptr, report);
2717
2718 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2719 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2720 ASSERT_GE(audio_index, 0);
2721 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002722}
2723
deadbeef4e2deab2017-09-20 13:56:21 -07002724// Helper for test below.
2725void ModifySsrcs(cricket::SessionDescription* desc) {
2726 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002727 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002728 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002729 for (uint32_t& ssrc : stream.ssrcs) {
2730 ssrc = rtc::CreateRandomId();
2731 }
2732 }
2733 }
2734}
2735
2736// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2737// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2738// This should result in two "RTCInboundRTPStreamStats", but only one
2739// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2740// being reset to 0 once the SSRC change occurs.
2741//
2742// Regression test for this bug:
2743// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2744//
2745// The bug causes the track stats to only represent one of the two streams:
2746// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2747// that the track stat counters would reset to 0 when the new stream is
2748// received, and a 50% chance that they'll stop updating (while
2749// "concealed_samples" continues increasing, due to silence being generated for
2750// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002751TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002752 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002753 ASSERT_TRUE(CreatePeerConnectionWrappers());
2754 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002755 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002756 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2757 // that doesn't signal SSRCs (from the callee's perspective).
2758 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2759 caller()->CreateAndSetAndSignalOffer();
2760 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2761 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002762 {
2763 MediaExpectations media_expectations;
2764 media_expectations.CalleeExpectsSomeAudio(50);
2765 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2766 }
deadbeef4e2deab2017-09-20 13:56:21 -07002767 // Some audio frames were received, so we should have nonzero "samples
2768 // received" for the track.
2769 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2770 callee()->NewGetStats();
2771 ASSERT_NE(nullptr, report);
2772 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2773 ASSERT_EQ(1U, track_stats.size());
2774 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2775 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2776 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2777
2778 // Create a new offer and munge it to cause the caller to use a new SSRC.
2779 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2780 caller()->CreateAndSetAndSignalOffer();
2781 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2782 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2783 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002784 {
2785 MediaExpectations media_expectations;
2786 media_expectations.CalleeExpectsSomeAudio(25);
2787 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2788 }
deadbeef4e2deab2017-09-20 13:56:21 -07002789
2790 report = callee()->NewGetStats();
2791 ASSERT_NE(nullptr, report);
2792 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2793 ASSERT_EQ(1U, track_stats.size());
2794 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2795 // The "total samples received" stat should only be greater than it was
2796 // before.
2797 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2798 // Right now, the new SSRC will cause the counters to reset to 0.
2799 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2800
2801 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002802 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002803 // good sign that we're seeing stats from the old stream that's no longer
2804 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002805 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002806 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2807 EXPECT_LT(*track_stats[0]->concealed_samples,
2808 *track_stats[0]->total_samples_received *
2809 kAcceptableConcealedSamplesPercentage);
2810
2811 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2812 // sanity check that the SSRC really changed.
2813 // TODO(deadbeef): This isn't working right now, because we're not returning
2814 // *any* stats for the inactive stream. Uncomment when the bug is completely
2815 // fixed.
2816 // auto inbound_stream_stats =
2817 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2818 // ASSERT_EQ(2U, inbound_stream_stats.size());
2819}
2820
deadbeef1dcb1642017-03-29 21:08:16 -07002821// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002822TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002823 PeerConnectionFactory::Options dtls_10_options;
2824 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2825 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2826 dtls_10_options));
2827 ConnectFakeSignaling();
2828 // Do normal offer/answer and wait for some frames to be received in each
2829 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002830 caller()->AddAudioVideoTracks();
2831 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002832 caller()->CreateAndSetAndSignalOffer();
2833 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002834 MediaExpectations media_expectations;
2835 media_expectations.ExpectBidirectionalAudioAndVideo();
2836 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002837}
2838
2839// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002840TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002841 PeerConnectionFactory::Options dtls_10_options;
2842 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2843 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2844 dtls_10_options));
2845 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002846 caller()->AddAudioVideoTracks();
2847 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002848 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002849 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002850 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002851 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002852 kDefaultTimeout);
2853 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002854 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002855 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002856 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002857 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2858 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002859}
2860
2861// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002862TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002863 PeerConnectionFactory::Options dtls_12_options;
2864 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2865 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2866 dtls_12_options));
2867 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002868 caller()->AddAudioVideoTracks();
2869 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002870 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002871 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002872 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002873 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002874 kDefaultTimeout);
2875 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002876 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002877 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002878 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002879 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2880 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002881}
2882
2883// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2884// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002885TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002886 PeerConnectionFactory::Options caller_options;
2887 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2888 PeerConnectionFactory::Options callee_options;
2889 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2890 ASSERT_TRUE(
2891 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2892 ConnectFakeSignaling();
2893 // Do normal offer/answer and wait for some frames to be received in each
2894 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002895 caller()->AddAudioVideoTracks();
2896 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002897 caller()->CreateAndSetAndSignalOffer();
2898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899 MediaExpectations media_expectations;
2900 media_expectations.ExpectBidirectionalAudioAndVideo();
2901 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002902}
2903
2904// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2905// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002906TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002907 PeerConnectionFactory::Options caller_options;
2908 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2909 PeerConnectionFactory::Options callee_options;
2910 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2911 ASSERT_TRUE(
2912 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2913 ConnectFakeSignaling();
2914 // Do normal offer/answer and wait for some frames to be received in each
2915 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002916 caller()->AddAudioVideoTracks();
2917 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002918 caller()->CreateAndSetAndSignalOffer();
2919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002920 MediaExpectations media_expectations;
2921 media_expectations.ExpectBidirectionalAudioAndVideo();
2922 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002923}
2924
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002925// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2926// works as expected; the cipher should only be used if enabled by both sides.
2927TEST_P(PeerConnectionIntegrationTest,
2928 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2929 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002930 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002931 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002932 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2933 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002934 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2935 TestNegotiatedCipherSuite(caller_options, callee_options,
2936 expected_cipher_suite);
2937}
2938
2939TEST_P(PeerConnectionIntegrationTest,
2940 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2941 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002942 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2943 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002944 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002945 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002946 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2947 TestNegotiatedCipherSuite(caller_options, callee_options,
2948 expected_cipher_suite);
2949}
2950
2951TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2952 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002953 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002954 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002955 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002956 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2957 TestNegotiatedCipherSuite(caller_options, callee_options,
2958 expected_cipher_suite);
2959}
2960
deadbeef1dcb1642017-03-29 21:08:16 -07002961// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002963 bool local_gcm_enabled = false;
2964 bool remote_gcm_enabled = false;
2965 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2966 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2967 expected_cipher_suite);
2968}
2969
2970// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002971TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002972 bool local_gcm_enabled = true;
2973 bool remote_gcm_enabled = true;
2974 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2975 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2976 expected_cipher_suite);
2977}
2978
2979// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002980TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002981 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2982 bool local_gcm_enabled = true;
2983 bool remote_gcm_enabled = false;
2984 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2985 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2986 expected_cipher_suite);
2987}
2988
2989// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002990TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002991 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2992 bool local_gcm_enabled = false;
2993 bool remote_gcm_enabled = true;
2994 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2995 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2996 expected_cipher_suite);
2997}
2998
deadbeef7914b8c2017-04-21 03:23:33 -07002999// Verify that media can be transmitted end-to-end when GCM crypto suites are
3000// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3001// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3002// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003003TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003004 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003005 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003006 ASSERT_TRUE(
3007 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3008 ConnectFakeSignaling();
3009 // Do normal offer/answer and wait for some frames to be received in each
3010 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003011 caller()->AddAudioVideoTracks();
3012 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003013 caller()->CreateAndSetAndSignalOffer();
3014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003015 MediaExpectations media_expectations;
3016 media_expectations.ExpectBidirectionalAudioAndVideo();
3017 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003018}
3019
deadbeef1dcb1642017-03-29 21:08:16 -07003020// This test sets up a call between two parties with audio, video and an RTP
3021// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003022TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003023 PeerConnectionInterface::RTCConfiguration rtc_config;
3024 rtc_config.enable_rtp_data_channel = true;
3025 rtc_config.enable_dtls_srtp = false;
3026 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003027 ConnectFakeSignaling();
3028 // Expect that data channel created on caller side will show up for callee as
3029 // well.
3030 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003031 caller()->AddAudioVideoTracks();
3032 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003033 caller()->CreateAndSetAndSignalOffer();
3034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3035 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003036 MediaExpectations media_expectations;
3037 media_expectations.ExpectBidirectionalAudioAndVideo();
3038 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003039 ASSERT_NE(nullptr, caller()->data_channel());
3040 ASSERT_NE(nullptr, callee()->data_channel());
3041 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3042 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3043
3044 // Ensure data can be sent in both directions.
3045 std::string data = "hello world";
3046 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3047 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3048 kDefaultTimeout);
3049 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3050 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3051 kDefaultTimeout);
3052}
3053
3054// Ensure that an RTP data channel is signaled as closed for the caller when
3055// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003056TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003057 RtpDataChannelSignaledClosedInCalleeOffer) {
3058 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003059 PeerConnectionInterface::RTCConfiguration rtc_config;
3060 rtc_config.enable_rtp_data_channel = true;
3061 rtc_config.enable_dtls_srtp = false;
3062 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003063 ConnectFakeSignaling();
3064 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003065 caller()->AddAudioVideoTracks();
3066 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003067 caller()->CreateAndSetAndSignalOffer();
3068 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3069 ASSERT_NE(nullptr, caller()->data_channel());
3070 ASSERT_NE(nullptr, callee()->data_channel());
3071 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3072 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3073
3074 // Close the data channel on the callee, and do an updated offer/answer.
3075 callee()->data_channel()->Close();
3076 callee()->CreateAndSetAndSignalOffer();
3077 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3078 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3079 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3080}
3081
3082// Tests that data is buffered in an RTP data channel until an observer is
3083// registered for it.
3084//
3085// NOTE: RTP data channels can receive data before the underlying
3086// transport has detected that a channel is writable and thus data can be
3087// received before the data channel state changes to open. That is hard to test
3088// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003089TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003090 DataBufferedUntilRtpDataChannelObserverRegistered) {
3091 // Use fake clock and simulated network delay so that we predictably can wait
3092 // until an SCTP message has been delivered without "sleep()"ing.
3093 rtc::ScopedFakeClock fake_clock;
3094 // Some things use a time of "0" as a special value, so we need to start out
3095 // the fake clock at a nonzero time.
3096 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003097 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003098 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3099 virtual_socket_server()->UpdateDelayDistribution();
3100
Niels Möllerf06f9232018-08-07 12:32:18 +02003101 PeerConnectionInterface::RTCConfiguration rtc_config;
3102 rtc_config.enable_rtp_data_channel = true;
3103 rtc_config.enable_dtls_srtp = false;
3104 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003105 ConnectFakeSignaling();
3106 caller()->CreateDataChannel();
3107 caller()->CreateAndSetAndSignalOffer();
3108 ASSERT_TRUE(caller()->data_channel() != nullptr);
3109 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3110 kDefaultTimeout, fake_clock);
3111 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3112 kDefaultTimeout, fake_clock);
3113 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3114 callee()->data_channel()->state(), kDefaultTimeout,
3115 fake_clock);
3116
3117 // Unregister the observer which is normally automatically registered.
3118 callee()->data_channel()->UnregisterObserver();
3119 // Send data and advance fake clock until it should have been received.
3120 std::string data = "hello world";
3121 caller()->data_channel()->Send(DataBuffer(data));
3122 SIMULATED_WAIT(false, 50, fake_clock);
3123
3124 // Attach data channel and expect data to be received immediately. Note that
3125 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3126 // further, but data can be received even if the callback is asynchronous.
3127 MockDataChannelObserver new_observer(callee()->data_channel());
3128 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3129 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003130 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3131 // If this is not done a DCHECK can be hit in ports.cc, because a large
3132 // negative number is calculated for the rtt due to the global clock changing.
3133 caller()->pc()->Close();
3134 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003135}
3136
3137// This test sets up a call between two parties with audio, video and but only
3138// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003139TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003140 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3141 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003142 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003143 rtc_config_1.enable_dtls_srtp = false;
3144 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3145 rtc_config_2.enable_dtls_srtp = false;
3146 rtc_config_2.enable_dtls_srtp = false;
3147 ASSERT_TRUE(
3148 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003149 ConnectFakeSignaling();
3150 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003151 caller()->AddAudioVideoTracks();
3152 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003153 caller()->CreateAndSetAndSignalOffer();
3154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3155 // The caller should still have a data channel, but it should be closed, and
3156 // one should ever have been created for the callee.
3157 EXPECT_TRUE(caller()->data_channel() != nullptr);
3158 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3159 EXPECT_EQ(nullptr, callee()->data_channel());
3160}
3161
3162// This test sets up a call between two parties with audio, and video. When
3163// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003164TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003165 PeerConnectionInterface::RTCConfiguration rtc_config;
3166 rtc_config.enable_rtp_data_channel = true;
3167 rtc_config.enable_dtls_srtp = false;
3168 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003169 ConnectFakeSignaling();
3170 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003171 caller()->AddAudioVideoTracks();
3172 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003173 caller()->CreateAndSetAndSignalOffer();
3174 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3175 // Create data channel and do new offer and answer.
3176 caller()->CreateDataChannel();
3177 caller()->CreateAndSetAndSignalOffer();
3178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3179 ASSERT_NE(nullptr, caller()->data_channel());
3180 ASSERT_NE(nullptr, callee()->data_channel());
3181 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3182 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3183 // Ensure data can be sent in both directions.
3184 std::string data = "hello world";
3185 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3186 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3187 kDefaultTimeout);
3188 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3189 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3190 kDefaultTimeout);
3191}
3192
3193#ifdef HAVE_SCTP
3194
3195// This test sets up a call between two parties with audio, video and an SCTP
3196// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003197TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003198 ASSERT_TRUE(CreatePeerConnectionWrappers());
3199 ConnectFakeSignaling();
3200 // Expect that data channel created on caller side will show up for callee as
3201 // well.
3202 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003203 caller()->AddAudioVideoTracks();
3204 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003205 caller()->CreateAndSetAndSignalOffer();
3206 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3207 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003208 MediaExpectations media_expectations;
3209 media_expectations.ExpectBidirectionalAudioAndVideo();
3210 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003211 // Caller data channel should already exist (it created one). Callee data
3212 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3213 ASSERT_NE(nullptr, caller()->data_channel());
3214 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3215 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3216 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3217
3218 // Ensure data can be sent in both directions.
3219 std::string data = "hello world";
3220 caller()->data_channel()->Send(DataBuffer(data));
3221 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3222 kDefaultTimeout);
3223 callee()->data_channel()->Send(DataBuffer(data));
3224 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3225 kDefaultTimeout);
3226}
3227
3228// Ensure that when the callee closes an SCTP data channel, the closing
3229// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003230TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003231 // Same procedure as above test.
3232 ASSERT_TRUE(CreatePeerConnectionWrappers());
3233 ConnectFakeSignaling();
3234 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003235 caller()->AddAudioVideoTracks();
3236 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003237 caller()->CreateAndSetAndSignalOffer();
3238 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3239 ASSERT_NE(nullptr, caller()->data_channel());
3240 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3241 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3242 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3243
3244 // Close the data channel on the callee side, and wait for it to reach the
3245 // "closed" state on both sides.
3246 callee()->data_channel()->Close();
3247 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3248 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3249}
3250
Seth Hampson2f0d7022018-02-20 11:54:42 -08003251TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003252 ASSERT_TRUE(CreatePeerConnectionWrappers());
3253 ConnectFakeSignaling();
3254 webrtc::DataChannelInit init;
3255 init.id = 53;
3256 init.maxRetransmits = 52;
3257 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003258 caller()->AddAudioVideoTracks();
3259 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003260 caller()->CreateAndSetAndSignalOffer();
3261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003262 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3263 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003264 EXPECT_EQ(init.id, callee()->data_channel()->id());
3265 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3266 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3267 EXPECT_FALSE(callee()->data_channel()->negotiated());
3268}
3269
deadbeef1dcb1642017-03-29 21:08:16 -07003270// Test usrsctp's ability to process unordered data stream, where data actually
3271// arrives out of order using simulated delays. Previously there have been some
3272// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003273TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003274 // Introduce random network delays.
3275 // Otherwise it's not a true "unordered" test.
3276 virtual_socket_server()->set_delay_mean(20);
3277 virtual_socket_server()->set_delay_stddev(5);
3278 virtual_socket_server()->UpdateDelayDistribution();
3279 // Normal procedure, but with unordered data channel config.
3280 ASSERT_TRUE(CreatePeerConnectionWrappers());
3281 ConnectFakeSignaling();
3282 webrtc::DataChannelInit init;
3283 init.ordered = false;
3284 caller()->CreateDataChannel(&init);
3285 caller()->CreateAndSetAndSignalOffer();
3286 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3287 ASSERT_NE(nullptr, caller()->data_channel());
3288 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3289 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3290 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3291
3292 static constexpr int kNumMessages = 100;
3293 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3294 static constexpr size_t kMaxMessageSize = 4096;
3295 // Create and send random messages.
3296 std::vector<std::string> sent_messages;
3297 for (int i = 0; i < kNumMessages; ++i) {
3298 size_t length =
3299 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3300 std::string message;
3301 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3302 caller()->data_channel()->Send(DataBuffer(message));
3303 callee()->data_channel()->Send(DataBuffer(message));
3304 sent_messages.push_back(message);
3305 }
3306
3307 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003308 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003309 caller()->data_observer()->received_message_count(),
3310 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003311 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003312 callee()->data_observer()->received_message_count(),
3313 kDefaultTimeout);
3314
3315 // Sort and compare to make sure none of the messages were corrupted.
3316 std::vector<std::string> caller_received_messages =
3317 caller()->data_observer()->messages();
3318 std::vector<std::string> callee_received_messages =
3319 callee()->data_observer()->messages();
3320 std::sort(sent_messages.begin(), sent_messages.end());
3321 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3322 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3323 EXPECT_EQ(sent_messages, caller_received_messages);
3324 EXPECT_EQ(sent_messages, callee_received_messages);
3325}
3326
3327// This test sets up a call between two parties with audio, and video. When
3328// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003329TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003330 ASSERT_TRUE(CreatePeerConnectionWrappers());
3331 ConnectFakeSignaling();
3332 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003333 caller()->AddAudioVideoTracks();
3334 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003335 caller()->CreateAndSetAndSignalOffer();
3336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3337 // Create data channel and do new offer and answer.
3338 caller()->CreateDataChannel();
3339 caller()->CreateAndSetAndSignalOffer();
3340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3341 // Caller data channel should already exist (it created one). Callee data
3342 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3343 ASSERT_NE(nullptr, caller()->data_channel());
3344 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3345 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3346 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3347 // Ensure data can be sent in both directions.
3348 std::string data = "hello world";
3349 caller()->data_channel()->Send(DataBuffer(data));
3350 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3351 kDefaultTimeout);
3352 callee()->data_channel()->Send(DataBuffer(data));
3353 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3354 kDefaultTimeout);
3355}
3356
deadbeef7914b8c2017-04-21 03:23:33 -07003357// Set up a connection initially just using SCTP data channels, later upgrading
3358// to audio/video, ensuring frames are received end-to-end. Effectively the
3359// inverse of the test above.
3360// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003361TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003362 ASSERT_TRUE(CreatePeerConnectionWrappers());
3363 ConnectFakeSignaling();
3364 // Do initial offer/answer with just data channel.
3365 caller()->CreateDataChannel();
3366 caller()->CreateAndSetAndSignalOffer();
3367 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3368 // Wait until data can be sent over the data channel.
3369 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3370 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3371 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3372
3373 // Do subsequent offer/answer with two-way audio and video. Audio and video
3374 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003375 caller()->AddAudioVideoTracks();
3376 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003377 caller()->CreateAndSetAndSignalOffer();
3378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003379 MediaExpectations media_expectations;
3380 media_expectations.ExpectBidirectionalAudioAndVideo();
3381 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003382}
3383
deadbeef8b7e9ad2017-05-25 09:38:55 -07003384static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003385 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003386 GetFirstDataContentDescription(desc);
3387 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003388 dcd_offer->set_use_sctpmap(false);
3389 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3390}
3391
3392// Test that the data channel works when a spec-compliant SCTP m= section is
3393// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3394// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003395TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003396 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3397 ASSERT_TRUE(CreatePeerConnectionWrappers());
3398 ConnectFakeSignaling();
3399 caller()->CreateDataChannel();
3400 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3401 caller()->CreateAndSetAndSignalOffer();
3402 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3403 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3404 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3405 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3406
3407 // Ensure data can be sent in both directions.
3408 std::string data = "hello world";
3409 caller()->data_channel()->Send(DataBuffer(data));
3410 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3411 kDefaultTimeout);
3412 callee()->data_channel()->Send(DataBuffer(data));
3413 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3414 kDefaultTimeout);
3415}
3416
deadbeef1dcb1642017-03-29 21:08:16 -07003417#endif // HAVE_SCTP
3418
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003419// This test sets up a call between two parties with a media transport data
3420// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003421TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3422 PeerConnectionInterface::RTCConfiguration rtc_config;
3423 rtc_config.use_media_transport_for_data_channels = true;
3424 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3425 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3426 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3427 loopback_media_transports()->second_factory()));
3428 ConnectFakeSignaling();
3429
3430 // Expect that data channel created on caller side will show up for callee as
3431 // well.
3432 caller()->CreateDataChannel();
3433 caller()->CreateAndSetAndSignalOffer();
3434 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3435
3436 // Ensure that the media transport is ready.
3437 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3438 loopback_media_transports()->FlushAsyncInvokes();
3439
3440 // Caller data channel should already exist (it created one). Callee data
3441 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3442 ASSERT_NE(nullptr, caller()->data_channel());
3443 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3444 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3445 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3446
3447 // Ensure data can be sent in both directions.
3448 std::string data = "hello world";
3449 caller()->data_channel()->Send(DataBuffer(data));
3450 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3451 kDefaultTimeout);
3452 callee()->data_channel()->Send(DataBuffer(data));
3453 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3454 kDefaultTimeout);
3455}
3456
3457// Ensure that when the callee closes a media transport data channel, the
3458// closing procedure results in the data channel being closed for the caller
3459// as well.
3460TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3461 PeerConnectionInterface::RTCConfiguration rtc_config;
3462 rtc_config.use_media_transport_for_data_channels = true;
3463 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3464 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3465 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3466 loopback_media_transports()->second_factory()));
3467 ConnectFakeSignaling();
3468
3469 // Create a data channel on the caller and signal it to the callee.
3470 caller()->CreateDataChannel();
3471 caller()->CreateAndSetAndSignalOffer();
3472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3473
3474 // Ensure that the media transport is ready.
3475 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3476 loopback_media_transports()->FlushAsyncInvokes();
3477
3478 // Data channels exist and open on both ends of the connection.
3479 ASSERT_NE(nullptr, caller()->data_channel());
3480 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3481 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3482 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3483
3484 // Close the data channel on the callee side, and wait for it to reach the
3485 // "closed" state on both sides.
3486 callee()->data_channel()->Close();
3487 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3488 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3489}
3490
3491TEST_P(PeerConnectionIntegrationTest,
3492 MediaTransportDataChannelConfigSentToOtherSide) {
3493 PeerConnectionInterface::RTCConfiguration rtc_config;
3494 rtc_config.use_media_transport_for_data_channels = true;
3495 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3496 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3497 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3498 loopback_media_transports()->second_factory()));
3499 ConnectFakeSignaling();
3500
3501 // Create a data channel with a non-default configuration and signal it to the
3502 // callee.
3503 webrtc::DataChannelInit init;
3504 init.id = 53;
3505 init.maxRetransmits = 52;
3506 caller()->CreateDataChannel("data-channel", &init);
3507 caller()->CreateAndSetAndSignalOffer();
3508 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3509
3510 // Ensure that the media transport is ready.
3511 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3512 loopback_media_transports()->FlushAsyncInvokes();
3513
3514 // Ensure that the data channel exists on the callee with the correct
3515 // configuration.
3516 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3517 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3518 EXPECT_EQ(init.id, callee()->data_channel()->id());
3519 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3520 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3521 EXPECT_FALSE(callee()->data_channel()->negotiated());
3522}
3523
Niels Möllerc68d2822018-11-20 14:52:05 +01003524TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3525 PeerConnectionInterface::RTCConfiguration rtc_config;
3526 rtc_config.use_media_transport = true;
3527 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3528 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3529 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3530 loopback_media_transports()->second_factory()));
3531 ConnectFakeSignaling();
3532
3533 caller()->AddAudioTrack();
3534 callee()->AddAudioTrack();
3535 // Start offer/answer exchange and wait for it to complete.
3536 caller()->CreateAndSetAndSignalOffer();
3537 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3538
3539 // Ensure that the media transport is ready.
3540 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3541 loopback_media_transports()->FlushAsyncInvokes();
3542
3543 MediaExpectations media_expectations;
3544 media_expectations.ExpectBidirectionalAudio();
3545 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3546
3547 webrtc::MediaTransportPair::Stats first_stats =
3548 loopback_media_transports()->FirstStats();
3549 webrtc::MediaTransportPair::Stats second_stats =
3550 loopback_media_transports()->SecondStats();
3551
3552 EXPECT_GT(first_stats.received_audio_frames, 0);
3553 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3554
3555 EXPECT_GT(second_stats.received_audio_frames, 0);
3556 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3557}
3558
deadbeef1dcb1642017-03-29 21:08:16 -07003559// Test that the ICE connection and gathering states eventually reach
3560// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003561TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003562 ASSERT_TRUE(CreatePeerConnectionWrappers());
3563 ConnectFakeSignaling();
3564 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003565 caller()->AddAudioVideoTracks();
3566 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003567 caller()->CreateAndSetAndSignalOffer();
3568 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3569 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3570 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3571 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3572 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3573 // After the best candidate pair is selected and all candidates are signaled,
3574 // the ICE connection state should reach "complete".
3575 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3576 // answerer/"callee" by default) only reaches "connected". When this is
3577 // fixed, this test should be updated.
3578 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3579 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003580 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3581 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003582}
3583
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003584// Replaces the first candidate with a static address and configures a
3585// MockAsyncResolver to return the replaced address the first time the static
3586// address is resolved. Candidates past the first will not be signaled.
3587class ReplaceFirstCandidateAddressDropOthers final
3588 : public IceCandidateReplacerInterface {
3589 public:
3590 ReplaceFirstCandidateAddressDropOthers(
3591 const SocketAddress& new_address,
3592 rtc::MockAsyncResolver* mock_async_resolver)
3593 : mock_async_resolver_(mock_async_resolver), new_address_(new_address) {
3594 RTC_DCHECK(mock_async_resolver);
3595 }
3596
3597 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3598 const webrtc::IceCandidateInterface* candidate) override {
3599 if (replaced_candidate_) {
3600 return nullptr;
3601 }
3602
3603 replaced_candidate_ = true;
3604 cricket::Candidate new_candidate(candidate->candidate());
3605 new_candidate.set_address(new_address_);
3606 EXPECT_CALL(*mock_async_resolver_, GetResolvedAddress(_, _))
3607 .WillOnce(DoAll(SetArgPointee<1>(candidate->candidate().address()),
3608 Return(true)));
3609 EXPECT_CALL(*mock_async_resolver_, Destroy(_));
3610 return webrtc::CreateIceCandidate(
3611 candidate->sdp_mid(), candidate->sdp_mline_index(), new_candidate);
3612 }
3613
3614 private:
3615 rtc::MockAsyncResolver* mock_async_resolver_;
3616 SocketAddress new_address_;
3617 bool replaced_candidate_ = false;
3618};
3619
3620// Drops all candidates before they are signaled.
3621class DropAllCandidates final : public IceCandidateReplacerInterface {
3622 public:
3623 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3624 const webrtc::IceCandidateInterface*) override {
3625 return nullptr;
3626 }
3627};
3628
3629// Replace the first caller ICE candidate IP with a fake hostname and drop the
3630// other candidates. Drop all candidates on the callee side (to avoid a prflx
3631// connection). Use a mock resolver to resolve the hostname back to the original
3632// IP on the callee side and check that the ice connection connects.
3633TEST_P(PeerConnectionIntegrationTest,
3634 IceStatesReachCompletionWithRemoteHostname) {
3635 webrtc::MockAsyncResolverFactory* callee_mock_async_resolver_factory;
3636 {
3637 auto resolver_factory =
3638 absl::make_unique<webrtc::MockAsyncResolverFactory>();
3639 callee_mock_async_resolver_factory = resolver_factory.get();
3640 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3641 callee_deps.async_resolver_factory = std::move(resolver_factory);
3642
3643 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3644 RTCConfiguration(), webrtc::PeerConnectionDependencies(nullptr),
3645 RTCConfiguration(), std::move(callee_deps)));
3646 }
3647
3648 rtc::MockAsyncResolver mock_async_resolver;
3649
3650 // This also verifies that the injected AsyncResolverFactory is used by
3651 // P2PTransportChannel.
3652 EXPECT_CALL(*callee_mock_async_resolver_factory, Create())
3653 .WillOnce(Return(&mock_async_resolver));
3654 caller()->SetLocalIceCandidateReplacer(
3655 absl::make_unique<ReplaceFirstCandidateAddressDropOthers>(
3656 SocketAddress("a.b", 10000), &mock_async_resolver));
3657 callee()->SetLocalIceCandidateReplacer(
3658 absl::make_unique<DropAllCandidates>());
3659
3660 ConnectFakeSignaling();
3661 caller()->AddAudioVideoTracks();
3662 callee()->AddAudioVideoTracks();
3663 caller()->CreateAndSetAndSignalOffer();
3664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3665 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3666 caller()->ice_connection_state(), kDefaultTimeout);
3667 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3668 callee()->ice_connection_state(), kDefaultTimeout);
3669}
3670
Steve Antonede9ca52017-10-16 13:04:27 -07003671// Test that firewalling the ICE connection causes the clients to identify the
3672// disconnected state and then removing the firewall causes them to reconnect.
3673class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003674 : public PeerConnectionIntegrationBaseTest,
3675 public ::testing::WithParamInterface<
3676 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003677 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003678 PeerConnectionIntegrationIceStatesTest()
3679 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3680 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003681 }
3682
3683 void StartStunServer(const SocketAddress& server_address) {
3684 stun_server_.reset(
3685 cricket::TestStunServer::Create(network_thread(), server_address));
3686 }
3687
3688 bool TestIPv6() {
3689 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3690 }
3691
3692 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003693 network_thread()->Invoke<void>(
3694 RTC_FROM_HERE,
3695 rtc::Bind(&cricket::PortAllocator::set_flags,
3696 caller()->port_allocator(), port_allocator_flags_));
3697 network_thread()->Invoke<void>(
3698 RTC_FROM_HERE,
3699 rtc::Bind(&cricket::PortAllocator::set_flags,
3700 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003701 }
3702
3703 std::vector<SocketAddress> CallerAddresses() {
3704 std::vector<SocketAddress> addresses;
3705 addresses.push_back(SocketAddress("1.1.1.1", 0));
3706 if (TestIPv6()) {
3707 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3708 }
3709 return addresses;
3710 }
3711
3712 std::vector<SocketAddress> CalleeAddresses() {
3713 std::vector<SocketAddress> addresses;
3714 addresses.push_back(SocketAddress("2.2.2.2", 0));
3715 if (TestIPv6()) {
3716 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3717 }
3718 return addresses;
3719 }
3720
3721 void SetUpNetworkInterfaces() {
3722 // Remove the default interfaces added by the test infrastructure.
3723 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3724 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3725
3726 // Add network addresses for test.
3727 for (const auto& caller_address : CallerAddresses()) {
3728 caller()->network()->AddInterface(caller_address);
3729 }
3730 for (const auto& callee_address : CalleeAddresses()) {
3731 callee()->network()->AddInterface(callee_address);
3732 }
3733 }
3734
3735 private:
3736 uint32_t port_allocator_flags_;
3737 std::unique_ptr<cricket::TestStunServer> stun_server_;
3738};
3739
3740// Tests that the PeerConnection goes through all the ICE gathering/connection
3741// states over the duration of the call. This includes Disconnected and Failed
3742// states, induced by putting a firewall between the peers and waiting for them
3743// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003744TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3745 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3746 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3747 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003748
3749 const SocketAddress kStunServerAddress =
3750 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3751 StartStunServer(kStunServerAddress);
3752
3753 PeerConnectionInterface::RTCConfiguration config;
3754 PeerConnectionInterface::IceServer ice_stun_server;
3755 ice_stun_server.urls.push_back(
3756 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3757 kStunServerAddress.PortAsString());
3758 config.servers.push_back(ice_stun_server);
3759
3760 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3761 ConnectFakeSignaling();
3762 SetPortAllocatorFlags();
3763 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003764 caller()->AddAudioVideoTracks();
3765 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003766
3767 // Initial state before anything happens.
3768 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3769 caller()->ice_gathering_state());
3770 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3771 caller()->ice_connection_state());
3772
3773 // Start the call by creating the offer, setting it as the local description,
3774 // then sending it to the peer who will respond with an answer. This happens
3775 // asynchronously so that we can watch the states as it runs in the
3776 // background.
3777 caller()->CreateAndSetAndSignalOffer();
3778
Steve Anton83119dd2017-11-10 16:19:52 -08003779 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3780 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003781
3782 // Verify that the observer was notified of the intermediate transitions.
3783 EXPECT_THAT(caller()->ice_connection_state_history(),
3784 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3785 PeerConnectionInterface::kIceConnectionConnected,
3786 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003787 // After the ice transport transitions from checking to connected we revert
3788 // back to new as the standard requires, as at that point the DTLS transport
3789 // is in the "new" state while no transports are "connecting", "checking",
3790 // "failed" or disconnected. This is pretty unintuitive, and we might want to
3791 // amend the spec to handle this case more gracefully.
3792 EXPECT_THAT(
3793 caller()->peer_connection_state_history(),
3794 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
3795 PeerConnectionInterface::PeerConnectionState::kNew,
3796 PeerConnectionInterface::PeerConnectionState::kConnecting,
3797 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003798 EXPECT_THAT(caller()->ice_gathering_state_history(),
3799 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3800 PeerConnectionInterface::kIceGatheringComplete));
3801
3802 // Block connections to/from the caller and wait for ICE to become
3803 // disconnected.
3804 for (const auto& caller_address : CallerAddresses()) {
3805 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3806 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003807 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003808 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3809 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003810
3811 // Let ICE re-establish by removing the firewall rules.
3812 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003813 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003814 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3815 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003816
3817 // According to RFC7675, if there is no response within 30 seconds then the
3818 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003819 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003820 constexpr int kConsentTimeout = 30000;
3821 for (const auto& caller_address : CallerAddresses()) {
3822 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3823 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003824 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003825 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3826 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003827}
3828
3829// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3830// and that the statistics in the metric observers are updated correctly.
3831TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3832 ASSERT_TRUE(CreatePeerConnectionWrappers());
3833 ConnectFakeSignaling();
3834 SetPortAllocatorFlags();
3835 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003836 caller()->AddAudioVideoTracks();
3837 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003838 caller()->CreateAndSetAndSignalOffer();
3839
3840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3841
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003842 // TODO(bugs.webrtc.org/9456): Fix it.
3843 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3844 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3845 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3846 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003847 if (TestIPv6()) {
3848 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3849 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003850 EXPECT_EQ(0, num_best_ipv4);
3851 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003852 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003853 EXPECT_EQ(1, num_best_ipv4);
3854 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003855 }
3856
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003857 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3858 "WebRTC.PeerConnection.CandidatePairType_UDP",
3859 webrtc::kIceCandidatePairHostHost));
3860 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3861 "WebRTC.PeerConnection.CandidatePairType_UDP",
3862 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003863}
3864
3865constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3866 cricket::PORTALLOCATOR_DISABLE_STUN |
3867 cricket::PORTALLOCATOR_DISABLE_RELAY;
3868constexpr uint32_t kFlagsIPv6NoStun =
3869 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3870 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3871constexpr uint32_t kFlagsIPv4Stun =
3872 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3873
Seth Hampson2f0d7022018-02-20 11:54:42 -08003874INSTANTIATE_TEST_CASE_P(
3875 PeerConnectionIntegrationTest,
3876 PeerConnectionIntegrationIceStatesTest,
3877 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3878 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3879 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3880 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003881
deadbeef1dcb1642017-03-29 21:08:16 -07003882// This test sets up a call between two parties with audio and video.
3883// During the call, the caller restarts ICE and the test verifies that
3884// new ICE candidates are generated and audio and video still can flow, and the
3885// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003886TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003887 ASSERT_TRUE(CreatePeerConnectionWrappers());
3888 ConnectFakeSignaling();
3889 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003890 caller()->AddAudioVideoTracks();
3891 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003892 caller()->CreateAndSetAndSignalOffer();
3893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3894 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3895 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003896 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3897 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07003898
3899 // To verify that the ICE restart actually occurs, get
3900 // ufrag/password/candidates before and after restart.
3901 // Create an SDP string of the first audio candidate for both clients.
3902 const webrtc::IceCandidateCollection* audio_candidates_caller =
3903 caller()->pc()->local_description()->candidates(0);
3904 const webrtc::IceCandidateCollection* audio_candidates_callee =
3905 callee()->pc()->local_description()->candidates(0);
3906 ASSERT_GT(audio_candidates_caller->count(), 0u);
3907 ASSERT_GT(audio_candidates_callee->count(), 0u);
3908 std::string caller_candidate_pre_restart;
3909 ASSERT_TRUE(
3910 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3911 std::string callee_candidate_pre_restart;
3912 ASSERT_TRUE(
3913 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3914 const cricket::SessionDescription* desc =
3915 caller()->pc()->local_description()->description();
3916 std::string caller_ufrag_pre_restart =
3917 desc->transport_infos()[0].description.ice_ufrag;
3918 desc = callee()->pc()->local_description()->description();
3919 std::string callee_ufrag_pre_restart =
3920 desc->transport_infos()[0].description.ice_ufrag;
3921
3922 // Have the caller initiate an ICE restart.
3923 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3924 caller()->CreateAndSetAndSignalOffer();
3925 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3926 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3927 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003928 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07003929 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3930
3931 // Grab the ufrags/candidates again.
3932 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3933 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3934 ASSERT_GT(audio_candidates_caller->count(), 0u);
3935 ASSERT_GT(audio_candidates_callee->count(), 0u);
3936 std::string caller_candidate_post_restart;
3937 ASSERT_TRUE(
3938 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3939 std::string callee_candidate_post_restart;
3940 ASSERT_TRUE(
3941 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3942 desc = caller()->pc()->local_description()->description();
3943 std::string caller_ufrag_post_restart =
3944 desc->transport_infos()[0].description.ice_ufrag;
3945 desc = callee()->pc()->local_description()->description();
3946 std::string callee_ufrag_post_restart =
3947 desc->transport_infos()[0].description.ice_ufrag;
3948 // Sanity check that an ICE restart was actually negotiated in SDP.
3949 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3950 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3951 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3952 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3953
3954 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003955 MediaExpectations media_expectations;
3956 media_expectations.ExpectBidirectionalAudioAndVideo();
3957 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003958}
3959
3960// Verify that audio/video can be received end-to-end when ICE renomination is
3961// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003962TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003963 PeerConnectionInterface::RTCConfiguration config;
3964 config.enable_ice_renomination = true;
3965 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3966 ConnectFakeSignaling();
3967 // Do normal offer/answer and wait for some frames to be received in each
3968 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003969 caller()->AddAudioVideoTracks();
3970 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003971 caller()->CreateAndSetAndSignalOffer();
3972 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3973 // Sanity check that ICE renomination was actually negotiated.
3974 const cricket::SessionDescription* desc =
3975 caller()->pc()->local_description()->description();
3976 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003977 ASSERT_NE(
3978 info.description.transport_options.end(),
3979 std::find(info.description.transport_options.begin(),
3980 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003981 }
3982 desc = callee()->pc()->local_description()->description();
3983 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003984 ASSERT_NE(
3985 info.description.transport_options.end(),
3986 std::find(info.description.transport_options.begin(),
3987 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003988 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003989 MediaExpectations media_expectations;
3990 media_expectations.ExpectBidirectionalAudioAndVideo();
3991 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003992}
3993
Steve Anton6f25b092017-10-23 09:39:20 -07003994// With a max bundle policy and RTCP muxing, adding a new media description to
3995// the connection should not affect ICE at all because the new media will use
3996// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003997TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003998 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003999 PeerConnectionInterface::RTCConfiguration config;
4000 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4001 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4002 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4003 config, PeerConnectionInterface::RTCConfiguration()));
4004 ConnectFakeSignaling();
4005
Steve Anton15324772018-01-16 10:26:49 -08004006 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004007 caller()->CreateAndSetAndSignalOffer();
4008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004009 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4010 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004011
4012 caller()->clear_ice_connection_state_history();
4013
Steve Anton15324772018-01-16 10:26:49 -08004014 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004015 caller()->CreateAndSetAndSignalOffer();
4016 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4017
4018 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4019}
4020
deadbeef1dcb1642017-03-29 21:08:16 -07004021// This test sets up a call between two parties with audio and video. It then
4022// renegotiates setting the video m-line to "port 0", then later renegotiates
4023// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004024TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004025 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4026 ASSERT_TRUE(CreatePeerConnectionWrappers());
4027 ConnectFakeSignaling();
4028
4029 // Do initial negotiation, only sending media from the caller. Will result in
4030 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004031 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004032 caller()->CreateAndSetAndSignalOffer();
4033 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4034
4035 // Negotiate again, disabling the video "m=" section (the callee will set the
4036 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004037 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4038 PeerConnectionInterface::RTCOfferAnswerOptions options;
4039 options.offer_to_receive_video = 0;
4040 callee()->SetOfferAnswerOptions(options);
4041 } else {
4042 callee()->SetRemoteOfferHandler([this] {
4043 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4044 });
4045 }
deadbeef1dcb1642017-03-29 21:08:16 -07004046 caller()->CreateAndSetAndSignalOffer();
4047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4048 // Sanity check that video "m=" section was actually rejected.
4049 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4050 callee()->pc()->local_description()->description());
4051 ASSERT_NE(nullptr, answer_video_content);
4052 ASSERT_TRUE(answer_video_content->rejected);
4053
4054 // Enable video and do negotiation again, making sure video is received
4055 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004056 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4057 PeerConnectionInterface::RTCOfferAnswerOptions options;
4058 options.offer_to_receive_video = 1;
4059 callee()->SetOfferAnswerOptions(options);
4060 } else {
4061 // The caller's transceiver is stopped, so we need to add another track.
4062 auto caller_transceiver =
4063 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4064 EXPECT_TRUE(caller_transceiver->stopped());
4065 caller()->AddVideoTrack();
4066 }
4067 callee()->AddVideoTrack();
4068 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004069 caller()->CreateAndSetAndSignalOffer();
4070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004071
deadbeef1dcb1642017-03-29 21:08:16 -07004072 // Verify the caller receives frames from the newly added stream, and the
4073 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004074 MediaExpectations media_expectations;
4075 media_expectations.CalleeExpectsSomeAudio();
4076 media_expectations.ExpectBidirectionalVideo();
4077 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004078}
4079
deadbeef1dcb1642017-03-29 21:08:16 -07004080// This tests that if we negotiate after calling CreateSender but before we
4081// have a track, then set a track later, frames from the newly-set track are
4082// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004083TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004084 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4085 ASSERT_TRUE(CreatePeerConnectionWrappers());
4086 ConnectFakeSignaling();
4087 auto caller_audio_sender =
4088 caller()->pc()->CreateSender("audio", "caller_stream");
4089 auto caller_video_sender =
4090 caller()->pc()->CreateSender("video", "caller_stream");
4091 auto callee_audio_sender =
4092 callee()->pc()->CreateSender("audio", "callee_stream");
4093 auto callee_video_sender =
4094 callee()->pc()->CreateSender("video", "callee_stream");
4095 caller()->CreateAndSetAndSignalOffer();
4096 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4097 // Wait for ICE to complete, without any tracks being set.
4098 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4099 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4100 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4101 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4102 // Now set the tracks, and expect frames to immediately start flowing.
4103 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4104 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4105 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4106 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004107 MediaExpectations media_expectations;
4108 media_expectations.ExpectBidirectionalAudioAndVideo();
4109 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4110}
4111
4112// This tests that if we negotiate after calling AddTransceiver but before we
4113// have a track, then set a track later, frames from the newly-set tracks are
4114// received end-to-end.
4115TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4116 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4117 ASSERT_TRUE(CreatePeerConnectionWrappers());
4118 ConnectFakeSignaling();
4119 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4120 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4121 auto caller_audio_sender = audio_result.MoveValue()->sender();
4122 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4123 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4124 auto caller_video_sender = video_result.MoveValue()->sender();
4125 callee()->SetRemoteOfferHandler([this] {
4126 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4127 callee()->pc()->GetTransceivers()[0]->SetDirection(
4128 RtpTransceiverDirection::kSendRecv);
4129 callee()->pc()->GetTransceivers()[1]->SetDirection(
4130 RtpTransceiverDirection::kSendRecv);
4131 });
4132 caller()->CreateAndSetAndSignalOffer();
4133 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4134 // Wait for ICE to complete, without any tracks being set.
4135 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4136 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4137 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4138 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4139 // Now set the tracks, and expect frames to immediately start flowing.
4140 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4141 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4142 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4143 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4144 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4145 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4146 MediaExpectations media_expectations;
4147 media_expectations.ExpectBidirectionalAudioAndVideo();
4148 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004149}
4150
4151// This test verifies that a remote video track can be added via AddStream,
4152// and sent end-to-end. For this particular test, it's simply echoed back
4153// from the caller to the callee, rather than being forwarded to a third
4154// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004155TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004156 ASSERT_TRUE(CreatePeerConnectionWrappers());
4157 ConnectFakeSignaling();
4158 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004159 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004160 caller()->CreateAndSetAndSignalOffer();
4161 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004162 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004163
4164 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4165 // time).
4166 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4167 callee()->CreateAndSetAndSignalOffer();
4168 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4169
Seth Hampson2f0d7022018-02-20 11:54:42 -08004170 MediaExpectations media_expectations;
4171 media_expectations.ExpectBidirectionalVideo();
4172 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004173}
4174
4175// Test that we achieve the expected end-to-end connection time, using a
4176// fake clock and simulated latency on the media and signaling paths.
4177// We use a TURN<->TURN connection because this is usually the quickest to
4178// set up initially, especially when we're confident the connection will work
4179// and can start sending media before we get a STUN response.
4180//
4181// With various optimizations enabled, here are the network delays we expect to
4182// be on the critical path:
4183// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4184// signaling answer (with DTLS fingerprint).
4185// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4186// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4187// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004188TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004189 rtc::ScopedFakeClock fake_clock;
4190 // Some things use a time of "0" as a special value, so we need to start out
4191 // the fake clock at a nonzero time.
4192 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004193 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004194
4195 static constexpr int media_hop_delay_ms = 50;
4196 static constexpr int signaling_trip_delay_ms = 500;
4197 // For explanation of these values, see comment above.
4198 static constexpr int required_media_hops = 9;
4199 static constexpr int required_signaling_trips = 2;
4200 // For internal delays (such as posting an event asychronously).
4201 static constexpr int allowed_internal_delay_ms = 20;
4202 static constexpr int total_connection_time_ms =
4203 media_hop_delay_ms * required_media_hops +
4204 signaling_trip_delay_ms * required_signaling_trips +
4205 allowed_internal_delay_ms;
4206
4207 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4208 3478};
4209 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4210 0};
4211 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4212 3478};
4213 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4214 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004215 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4216 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004217
Seth Hampsonaed71642018-06-11 07:41:32 -07004218 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4219 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004220 // Bypass permission check on received packets so media can be sent before
4221 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004222 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4223 turn_server_1->set_enable_permission_checks(false);
4224 });
4225 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4226 turn_server_2->set_enable_permission_checks(false);
4227 });
deadbeef1dcb1642017-03-29 21:08:16 -07004228
4229 PeerConnectionInterface::RTCConfiguration client_1_config;
4230 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4231 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4232 ice_server_1.username = "test";
4233 ice_server_1.password = "test";
4234 client_1_config.servers.push_back(ice_server_1);
4235 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4236 client_1_config.presume_writable_when_fully_relayed = true;
4237
4238 PeerConnectionInterface::RTCConfiguration client_2_config;
4239 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4240 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4241 ice_server_2.username = "test";
4242 ice_server_2.password = "test";
4243 client_2_config.servers.push_back(ice_server_2);
4244 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4245 client_2_config.presume_writable_when_fully_relayed = true;
4246
4247 ASSERT_TRUE(
4248 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4249 // Set up the simulated delays.
4250 SetSignalingDelayMs(signaling_trip_delay_ms);
4251 ConnectFakeSignaling();
4252 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4253 virtual_socket_server()->UpdateDelayDistribution();
4254
4255 // Set "offer to receive audio/video" without adding any tracks, so we just
4256 // set up ICE/DTLS with no media.
4257 PeerConnectionInterface::RTCOfferAnswerOptions options;
4258 options.offer_to_receive_audio = 1;
4259 options.offer_to_receive_video = 1;
4260 caller()->SetOfferAnswerOptions(options);
4261 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004262 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4263 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004264 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4265 // If this is not done a DCHECK can be hit in ports.cc, because a large
4266 // negative number is calculated for the rtt due to the global clock changing.
4267 caller()->pc()->Close();
4268 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004269}
4270
Jonas Orelandbdcee282017-10-10 14:01:40 +02004271// Verify that a TurnCustomizer passed in through RTCConfiguration
4272// is actually used by the underlying TURN candidate pair.
4273// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004274TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004275 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4276 3478};
4277 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4278 0};
4279 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4280 3478};
4281 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4282 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004283 CreateTurnServer(turn_server_1_internal_address,
4284 turn_server_1_external_address);
4285 CreateTurnServer(turn_server_2_internal_address,
4286 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004287
4288 PeerConnectionInterface::RTCConfiguration client_1_config;
4289 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4290 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4291 ice_server_1.username = "test";
4292 ice_server_1.password = "test";
4293 client_1_config.servers.push_back(ice_server_1);
4294 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004295 auto* customizer1 = CreateTurnCustomizer();
4296 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004297
4298 PeerConnectionInterface::RTCConfiguration client_2_config;
4299 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4300 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4301 ice_server_2.username = "test";
4302 ice_server_2.password = "test";
4303 client_2_config.servers.push_back(ice_server_2);
4304 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004305 auto* customizer2 = CreateTurnCustomizer();
4306 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004307
4308 ASSERT_TRUE(
4309 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4310 ConnectFakeSignaling();
4311
4312 // Set "offer to receive audio/video" without adding any tracks, so we just
4313 // set up ICE/DTLS with no media.
4314 PeerConnectionInterface::RTCOfferAnswerOptions options;
4315 options.offer_to_receive_audio = 1;
4316 options.offer_to_receive_video = 1;
4317 caller()->SetOfferAnswerOptions(options);
4318 caller()->CreateAndSetAndSignalOffer();
4319 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4320
Seth Hampsonaed71642018-06-11 07:41:32 -07004321 ExpectTurnCustomizerCountersIncremented(customizer1);
4322 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004323}
4324
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004325// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4326// send media between the caller and the callee.
4327TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4328 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4329 3478};
4330 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4331
4332 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004333 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4334 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004335
4336 webrtc::PeerConnectionInterface::IceServer ice_server;
4337 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4338 ice_server.username = "test";
4339 ice_server.password = "test";
4340
4341 PeerConnectionInterface::RTCConfiguration client_1_config;
4342 client_1_config.servers.push_back(ice_server);
4343 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4344
4345 PeerConnectionInterface::RTCConfiguration client_2_config;
4346 client_2_config.servers.push_back(ice_server);
4347 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4348
4349 ASSERT_TRUE(
4350 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4351
4352 // Do normal offer/answer and wait for ICE to complete.
4353 ConnectFakeSignaling();
4354 caller()->AddAudioVideoTracks();
4355 callee()->AddAudioVideoTracks();
4356 caller()->CreateAndSetAndSignalOffer();
4357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4358 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4359 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4360
4361 MediaExpectations media_expectations;
4362 media_expectations.ExpectBidirectionalAudioAndVideo();
4363 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4364}
4365
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004366// Verify that a SSLCertificateVerifier passed in through
4367// PeerConnectionDependencies is actually used by the underlying SSL
4368// implementation to determine whether a certificate presented by the TURN
4369// server is accepted by the client. Note that openssladapter_unittest.cc
4370// contains more detailed, lower-level tests.
4371TEST_P(PeerConnectionIntegrationTest,
4372 SSLCertificateVerifierUsedForTurnConnections) {
4373 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4374 3478};
4375 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4376
4377 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4378 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004379 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4380 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004381
4382 webrtc::PeerConnectionInterface::IceServer ice_server;
4383 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4384 ice_server.username = "test";
4385 ice_server.password = "test";
4386
4387 PeerConnectionInterface::RTCConfiguration client_1_config;
4388 client_1_config.servers.push_back(ice_server);
4389 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4390
4391 PeerConnectionInterface::RTCConfiguration client_2_config;
4392 client_2_config.servers.push_back(ice_server);
4393 // Setting the type to kRelay forces the connection to go through a TURN
4394 // server.
4395 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4396
4397 // Get a copy to the pointer so we can verify calls later.
4398 rtc::TestCertificateVerifier* client_1_cert_verifier =
4399 new rtc::TestCertificateVerifier();
4400 client_1_cert_verifier->verify_certificate_ = true;
4401 rtc::TestCertificateVerifier* client_2_cert_verifier =
4402 new rtc::TestCertificateVerifier();
4403 client_2_cert_verifier->verify_certificate_ = true;
4404
4405 // Create the dependencies with the test certificate verifier.
4406 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4407 client_1_deps.tls_cert_verifier =
4408 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4409 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4410 client_2_deps.tls_cert_verifier =
4411 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4412
4413 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4414 client_1_config, std::move(client_1_deps), client_2_config,
4415 std::move(client_2_deps)));
4416 ConnectFakeSignaling();
4417
4418 // Set "offer to receive audio/video" without adding any tracks, so we just
4419 // set up ICE/DTLS with no media.
4420 PeerConnectionInterface::RTCOfferAnswerOptions options;
4421 options.offer_to_receive_audio = 1;
4422 options.offer_to_receive_video = 1;
4423 caller()->SetOfferAnswerOptions(options);
4424 caller()->CreateAndSetAndSignalOffer();
4425 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4426
4427 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4428 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004429}
4430
4431TEST_P(PeerConnectionIntegrationTest,
4432 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4433 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4434 3478};
4435 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4436
4437 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4438 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004439 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4440 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004441
4442 webrtc::PeerConnectionInterface::IceServer ice_server;
4443 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4444 ice_server.username = "test";
4445 ice_server.password = "test";
4446
4447 PeerConnectionInterface::RTCConfiguration client_1_config;
4448 client_1_config.servers.push_back(ice_server);
4449 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4450
4451 PeerConnectionInterface::RTCConfiguration client_2_config;
4452 client_2_config.servers.push_back(ice_server);
4453 // Setting the type to kRelay forces the connection to go through a TURN
4454 // server.
4455 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4456
4457 // Get a copy to the pointer so we can verify calls later.
4458 rtc::TestCertificateVerifier* client_1_cert_verifier =
4459 new rtc::TestCertificateVerifier();
4460 client_1_cert_verifier->verify_certificate_ = false;
4461 rtc::TestCertificateVerifier* client_2_cert_verifier =
4462 new rtc::TestCertificateVerifier();
4463 client_2_cert_verifier->verify_certificate_ = false;
4464
4465 // Create the dependencies with the test certificate verifier.
4466 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4467 client_1_deps.tls_cert_verifier =
4468 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4469 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4470 client_2_deps.tls_cert_verifier =
4471 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4472
4473 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4474 client_1_config, std::move(client_1_deps), client_2_config,
4475 std::move(client_2_deps)));
4476 ConnectFakeSignaling();
4477
4478 // Set "offer to receive audio/video" without adding any tracks, so we just
4479 // set up ICE/DTLS with no media.
4480 PeerConnectionInterface::RTCOfferAnswerOptions options;
4481 options.offer_to_receive_audio = 1;
4482 options.offer_to_receive_video = 1;
4483 caller()->SetOfferAnswerOptions(options);
4484 caller()->CreateAndSetAndSignalOffer();
4485 bool wait_res = true;
4486 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4487 // properly, should be able to just wait for a state of "failed" instead of
4488 // waiting a fixed 10 seconds.
4489 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4490 ASSERT_FALSE(wait_res);
4491
4492 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4493 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004494}
4495
deadbeefc964d0b2017-04-03 10:03:35 -07004496// Test that audio and video flow end-to-end when codec names don't use the
4497// expected casing, given that they're supposed to be case insensitive. To test
4498// this, all but one codec is removed from each media description, and its
4499// casing is changed.
4500//
4501// In the past, this has regressed and caused crashes/black video, due to the
4502// fact that code at some layers was doing case-insensitive comparisons and
4503// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004504TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004505 ASSERT_TRUE(CreatePeerConnectionWrappers());
4506 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004507 caller()->AddAudioVideoTracks();
4508 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004509
4510 // Remove all but one audio/video codec (opus and VP8), and change the
4511 // casing of the caller's generated offer.
4512 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4513 cricket::AudioContentDescription* audio =
4514 GetFirstAudioContentDescription(description);
4515 ASSERT_NE(nullptr, audio);
4516 auto audio_codecs = audio->codecs();
4517 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4518 [](const cricket::AudioCodec& codec) {
4519 return codec.name != "opus";
4520 }),
4521 audio_codecs.end());
4522 ASSERT_EQ(1u, audio_codecs.size());
4523 audio_codecs[0].name = "OpUs";
4524 audio->set_codecs(audio_codecs);
4525
4526 cricket::VideoContentDescription* video =
4527 GetFirstVideoContentDescription(description);
4528 ASSERT_NE(nullptr, video);
4529 auto video_codecs = video->codecs();
4530 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4531 [](const cricket::VideoCodec& codec) {
4532 return codec.name != "VP8";
4533 }),
4534 video_codecs.end());
4535 ASSERT_EQ(1u, video_codecs.size());
4536 video_codecs[0].name = "vP8";
4537 video->set_codecs(video_codecs);
4538 });
4539
4540 caller()->CreateAndSetAndSignalOffer();
4541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4542
4543 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004544 MediaExpectations media_expectations;
4545 media_expectations.ExpectBidirectionalAudioAndVideo();
4546 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004547}
4548
Jonas Oreland49ac5952018-09-26 16:04:32 +02004549TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004550 ASSERT_TRUE(CreatePeerConnectionWrappers());
4551 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004552 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004553 caller()->CreateAndSetAndSignalOffer();
4554 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004555 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004556 MediaExpectations media_expectations;
4557 media_expectations.CalleeExpectsSomeAudio(1);
4558 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004559 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004560 auto receiver = callee()->pc()->GetReceivers()[0];
4561 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004562 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004563 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4564 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004565 sources[0].source_id());
4566 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4567}
4568
4569TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4570 ASSERT_TRUE(CreatePeerConnectionWrappers());
4571 ConnectFakeSignaling();
4572 caller()->AddVideoTrack();
4573 caller()->CreateAndSetAndSignalOffer();
4574 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4575 // Wait for one video frame to be received by the callee.
4576 MediaExpectations media_expectations;
4577 media_expectations.CalleeExpectsSomeVideo(1);
4578 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4579 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4580 auto receiver = callee()->pc()->GetReceivers()[0];
4581 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4582 auto sources = receiver->GetSources();
4583 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4584 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4585 sources[0].source_id());
4586 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004587}
4588
deadbeef2f425aa2017-04-14 10:41:32 -07004589// Test that if a track is removed and added again with a different stream ID,
4590// the new stream ID is successfully communicated in SDP and media continues to
4591// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004592// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4593// it will not reuse a transceiver that has already been sending. After creating
4594// a new transceiver it tries to create an offer with two senders of the same
4595// track ids and it fails.
4596TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004597 ASSERT_TRUE(CreatePeerConnectionWrappers());
4598 ConnectFakeSignaling();
4599
deadbeef2f425aa2017-04-14 10:41:32 -07004600 // Add track using stream 1, do offer/answer.
4601 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4602 caller()->CreateLocalAudioTrack();
4603 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004604 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004605 caller()->CreateAndSetAndSignalOffer();
4606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004607 {
4608 MediaExpectations media_expectations;
4609 media_expectations.CalleeExpectsSomeAudio(1);
4610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4611 }
deadbeef2f425aa2017-04-14 10:41:32 -07004612 // Remove the sender, and create a new one with the new stream.
4613 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004614 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004615 caller()->CreateAndSetAndSignalOffer();
4616 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4617 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004618 {
4619 MediaExpectations media_expectations;
4620 media_expectations.CalleeExpectsSomeAudio();
4621 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4622 }
deadbeef2f425aa2017-04-14 10:41:32 -07004623}
4624
Seth Hampson2f0d7022018-02-20 11:54:42 -08004625TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004626 ASSERT_TRUE(CreatePeerConnectionWrappers());
4627 ConnectFakeSignaling();
4628
Karl Wiberg918f50c2018-07-05 11:40:33 +02004629 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004630 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4631 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4632 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004633 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4634 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004635
Steve Anton15324772018-01-16 10:26:49 -08004636 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004637 caller()->CreateAndSetAndSignalOffer();
4638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4639}
4640
Steve Antonede9ca52017-10-16 13:04:27 -07004641// Test that if candidates are only signaled by applying full session
4642// descriptions (instead of using AddIceCandidate), the peers can connect to
4643// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004644TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004645 ASSERT_TRUE(CreatePeerConnectionWrappers());
4646 // Each side will signal the session descriptions but not candidates.
4647 ConnectFakeSignalingForSdpOnly();
4648
4649 // Add audio video track and exchange the initial offer/answer with media
4650 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004651 caller()->AddAudioVideoTracks();
4652 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004653 caller()->CreateAndSetAndSignalOffer();
4654
4655 // Wait for all candidates to be gathered on both the caller and callee.
4656 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4657 caller()->ice_gathering_state(), kDefaultTimeout);
4658 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4659 callee()->ice_gathering_state(), kDefaultTimeout);
4660
4661 // The candidates will now be included in the session description, so
4662 // signaling them will start the ICE connection.
4663 caller()->CreateAndSetAndSignalOffer();
4664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4665
4666 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004667 MediaExpectations media_expectations;
4668 media_expectations.ExpectBidirectionalAudioAndVideo();
4669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004670}
4671
henrika5f6bf242017-11-01 11:06:56 +01004672// Test that SetAudioPlayout can be used to disable audio playout from the
4673// start, then later enable it. This may be useful, for example, if the caller
4674// needs to play a local ringtone until some event occurs, after which it
4675// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004676TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004677 ASSERT_TRUE(CreatePeerConnectionWrappers());
4678 ConnectFakeSignaling();
4679
4680 // Set up audio-only call where audio playout is disabled on caller's side.
4681 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004682 caller()->AddAudioTrack();
4683 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004684 caller()->CreateAndSetAndSignalOffer();
4685 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4686
4687 // Pump messages for a second.
4688 WAIT(false, 1000);
4689 // Since audio playout is disabled, the caller shouldn't have received
4690 // anything (at the playout level, at least).
4691 EXPECT_EQ(0, caller()->audio_frames_received());
4692 // As a sanity check, make sure the callee (for which playout isn't disabled)
4693 // did still see frames on its audio level.
4694 ASSERT_GT(callee()->audio_frames_received(), 0);
4695
4696 // Enable playout again, and ensure audio starts flowing.
4697 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004698 MediaExpectations media_expectations;
4699 media_expectations.ExpectBidirectionalAudio();
4700 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004701}
4702
4703double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4704 auto report = pc->NewGetStats();
4705 auto track_stats_list =
4706 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4707 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4708 for (const auto* track_stats : track_stats_list) {
4709 if (track_stats->remote_source.is_defined() &&
4710 *track_stats->remote_source) {
4711 remote_track_stats = track_stats;
4712 break;
4713 }
4714 }
4715
4716 if (!remote_track_stats->total_audio_energy.is_defined()) {
4717 return 0.0;
4718 }
4719 return *remote_track_stats->total_audio_energy;
4720}
4721
4722// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4723// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004724TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004725 DisableAudioPlayoutStillGeneratesAudioStats) {
4726 ASSERT_TRUE(CreatePeerConnectionWrappers());
4727 ConnectFakeSignaling();
4728
4729 // Set up audio-only call where playout is disabled but audio-processing is
4730 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004731 caller()->AddAudioTrack();
4732 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004733 caller()->pc()->SetAudioPlayout(false);
4734
4735 caller()->CreateAndSetAndSignalOffer();
4736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4737
4738 // Wait for the callee to receive audio stats.
4739 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4740}
4741
henrika4f167df2017-11-01 14:45:55 +01004742// Test that SetAudioRecording can be used to disable audio recording from the
4743// start, then later enable it. This may be useful, for example, if the caller
4744// wants to ensure that no audio resources are active before a certain state
4745// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004746TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004747 ASSERT_TRUE(CreatePeerConnectionWrappers());
4748 ConnectFakeSignaling();
4749
4750 // Set up audio-only call where audio recording is disabled on caller's side.
4751 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004752 caller()->AddAudioTrack();
4753 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004754 caller()->CreateAndSetAndSignalOffer();
4755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4756
4757 // Pump messages for a second.
4758 WAIT(false, 1000);
4759 // Since caller has disabled audio recording, the callee shouldn't have
4760 // received anything.
4761 EXPECT_EQ(0, callee()->audio_frames_received());
4762 // As a sanity check, make sure the caller did still see frames on its
4763 // audio level since audio recording is enabled on the calle side.
4764 ASSERT_GT(caller()->audio_frames_received(), 0);
4765
4766 // Enable audio recording again, and ensure audio starts flowing.
4767 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004768 MediaExpectations media_expectations;
4769 media_expectations.ExpectBidirectionalAudio();
4770 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004771}
4772
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004773// Test that after closing PeerConnections, they stop sending any packets (ICE,
4774// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004775TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004776 // Set up audio/video/data, wait for some frames to be received.
4777 ASSERT_TRUE(CreatePeerConnectionWrappers());
4778 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004779 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004780#ifdef HAVE_SCTP
4781 caller()->CreateDataChannel();
4782#endif
4783 caller()->CreateAndSetAndSignalOffer();
4784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004785 MediaExpectations media_expectations;
4786 media_expectations.CalleeExpectsSomeAudioAndVideo();
4787 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004788 // Close PeerConnections.
4789 caller()->pc()->Close();
4790 callee()->pc()->Close();
4791 // Pump messages for a second, and ensure no new packets end up sent.
4792 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4793 WAIT(false, 1000);
4794 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4795 EXPECT_EQ(sent_packets_a, sent_packets_b);
4796}
4797
Steve Anton7eca0932018-03-30 15:18:41 -07004798// Test that transport stats are generated by the RTCStatsCollector for a
4799// connection that only involves data channels. This is a regression test for
4800// crbug.com/826972.
4801#ifdef HAVE_SCTP
4802TEST_P(PeerConnectionIntegrationTest,
4803 TransportStatsReportedForDataChannelOnlyConnection) {
4804 ASSERT_TRUE(CreatePeerConnectionWrappers());
4805 ConnectFakeSignaling();
4806 caller()->CreateDataChannel();
4807
4808 caller()->CreateAndSetAndSignalOffer();
4809 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4810 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4811
4812 auto caller_report = caller()->NewGetStats();
4813 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4814 auto callee_report = callee()->NewGetStats();
4815 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4816}
4817#endif // HAVE_SCTP
4818
Qingsi Wang7685e862018-06-11 20:15:46 -07004819TEST_P(PeerConnectionIntegrationTest,
4820 IceEventsGeneratedAndLoggedInRtcEventLog) {
4821 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4822 ConnectFakeSignaling();
4823 PeerConnectionInterface::RTCOfferAnswerOptions options;
4824 options.offer_to_receive_audio = 1;
4825 caller()->SetOfferAnswerOptions(options);
4826 caller()->CreateAndSetAndSignalOffer();
4827 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4828 ASSERT_NE(nullptr, caller()->event_log_factory());
4829 ASSERT_NE(nullptr, callee()->event_log_factory());
4830 webrtc::FakeRtcEventLog* caller_event_log =
4831 static_cast<webrtc::FakeRtcEventLog*>(
4832 caller()->event_log_factory()->last_log_created());
4833 webrtc::FakeRtcEventLog* callee_event_log =
4834 static_cast<webrtc::FakeRtcEventLog*>(
4835 callee()->event_log_factory()->last_log_created());
4836 ASSERT_NE(nullptr, caller_event_log);
4837 ASSERT_NE(nullptr, callee_event_log);
4838 int caller_ice_config_count = caller_event_log->GetEventCount(
4839 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4840 int caller_ice_event_count = caller_event_log->GetEventCount(
4841 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4842 int callee_ice_config_count = callee_event_log->GetEventCount(
4843 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4844 int callee_ice_event_count = callee_event_log->GetEventCount(
4845 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4846 EXPECT_LT(0, caller_ice_config_count);
4847 EXPECT_LT(0, caller_ice_event_count);
4848 EXPECT_LT(0, callee_ice_config_count);
4849 EXPECT_LT(0, callee_ice_event_count);
4850}
4851
Seth Hampson2f0d7022018-02-20 11:54:42 -08004852INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4853 PeerConnectionIntegrationTest,
4854 Values(SdpSemantics::kPlanB,
4855 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004856
Steve Anton74255ff2018-01-24 18:32:57 -08004857// Tests that verify interoperability between Plan B and Unified Plan
4858// PeerConnections.
4859class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004860 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004861 public ::testing::WithParamInterface<
4862 std::tuple<SdpSemantics, SdpSemantics>> {
4863 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004864 // Setting the SdpSemantics for the base test to kDefault does not matter
4865 // because we specify not to use the test semantics when creating
4866 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004867 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004868 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004869 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004870 callee_semantics_(std::get<1>(GetParam())) {}
4871
4872 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004873 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4874 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004875 }
4876
4877 const SdpSemantics caller_semantics_;
4878 const SdpSemantics callee_semantics_;
4879};
4880
4881TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4882 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4883 ConnectFakeSignaling();
4884
4885 caller()->CreateAndSetAndSignalOffer();
4886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4887}
4888
4889TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4890 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4891 ConnectFakeSignaling();
4892 auto audio_sender = caller()->AddAudioTrack();
4893
4894 caller()->CreateAndSetAndSignalOffer();
4895 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4896
4897 // Verify that one audio receiver has been created on the remote and that it
4898 // has the same track ID as the sending track.
4899 auto receivers = callee()->pc()->GetReceivers();
4900 ASSERT_EQ(1u, receivers.size());
4901 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4902 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4903
Seth Hampson2f0d7022018-02-20 11:54:42 -08004904 MediaExpectations media_expectations;
4905 media_expectations.CalleeExpectsSomeAudio();
4906 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004907}
4908
4909TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4910 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4911 ConnectFakeSignaling();
4912 auto video_sender = caller()->AddVideoTrack();
4913 auto audio_sender = caller()->AddAudioTrack();
4914
4915 caller()->CreateAndSetAndSignalOffer();
4916 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4917
4918 // Verify that one audio and one video receiver have been created on the
4919 // remote and that they have the same track IDs as the sending tracks.
4920 auto audio_receivers =
4921 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4922 ASSERT_EQ(1u, audio_receivers.size());
4923 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4924 auto video_receivers =
4925 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4926 ASSERT_EQ(1u, video_receivers.size());
4927 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4928
Seth Hampson2f0d7022018-02-20 11:54:42 -08004929 MediaExpectations media_expectations;
4930 media_expectations.CalleeExpectsSomeAudioAndVideo();
4931 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004932}
4933
4934TEST_P(PeerConnectionIntegrationInteropTest,
4935 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4936 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4937 ConnectFakeSignaling();
4938 caller()->AddAudioVideoTracks();
4939 callee()->AddAudioVideoTracks();
4940
4941 caller()->CreateAndSetAndSignalOffer();
4942 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4943
Seth Hampson2f0d7022018-02-20 11:54:42 -08004944 MediaExpectations media_expectations;
4945 media_expectations.ExpectBidirectionalAudioAndVideo();
4946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004947}
4948
4949TEST_P(PeerConnectionIntegrationInteropTest,
4950 ReverseRolesOneAudioLocalToOneVideoRemote) {
4951 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4952 ConnectFakeSignaling();
4953 caller()->AddAudioTrack();
4954 callee()->AddVideoTrack();
4955
4956 caller()->CreateAndSetAndSignalOffer();
4957 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4958
4959 // Verify that only the audio track has been negotiated.
4960 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4961 // Might also check that the callee's NegotiationNeeded flag is set.
4962
4963 // Reverse roles.
4964 callee()->CreateAndSetAndSignalOffer();
4965 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4966
Seth Hampson2f0d7022018-02-20 11:54:42 -08004967 MediaExpectations media_expectations;
4968 media_expectations.CallerExpectsSomeVideo();
4969 media_expectations.CalleeExpectsSomeAudio();
4970 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004971}
4972
Steve Antonba42e992018-04-09 14:10:01 -07004973INSTANTIATE_TEST_CASE_P(
4974 PeerConnectionIntegrationTest,
4975 PeerConnectionIntegrationInteropTest,
4976 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4977 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4978
4979// Test that if the Unified Plan side offers two video tracks then the Plan B
4980// side will only see the first one and ignore the second.
4981TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004982 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4983 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004984 ConnectFakeSignaling();
4985 auto first_sender = caller()->AddVideoTrack();
4986 caller()->AddVideoTrack();
4987
4988 caller()->CreateAndSetAndSignalOffer();
4989 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4990
4991 // Verify that there is only one receiver and it corresponds to the first
4992 // added track.
4993 auto receivers = callee()->pc()->GetReceivers();
4994 ASSERT_EQ(1u, receivers.size());
4995 EXPECT_TRUE(receivers[0]->track()->enabled());
4996 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4997
Seth Hampson2f0d7022018-02-20 11:54:42 -08004998 MediaExpectations media_expectations;
4999 media_expectations.CalleeExpectsSomeVideo();
5000 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005001}
5002
deadbeef1dcb1642017-03-29 21:08:16 -07005003} // namespace
5004
5005#endif // if !defined(THREAD_SANITIZER)