blob: dc0f06f1c93eb48ea326d7fe7cdc33acca1ec354 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/builtin_video_decoder_factory.h"
34#include "api/video_codecs/builtin_video_encoder_factory.h"
35#include "api/video_codecs/sdp_video_format.h"
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "p2p/base/p2pconstants.h"
43#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070044#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020045#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "p2p/base/testturnserver.h"
47#include "p2p/client/basicportallocator.h"
48#include "pc/dtmfsender.h"
49#include "pc/localaudiosource.h"
50#include "pc/mediasession.h"
51#include "pc/peerconnection.h"
52#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080053#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080054#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020056#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "pc/test/fakertccertificategenerator.h"
58#include "pc/test/fakevideotrackrenderer.h"
59#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070061#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "rtc_base/gunit.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070063#include "rtc_base/testcertificateverifier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020065#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070066
67using cricket::ContentInfo;
68using cricket::FakeWebRtcVideoDecoder;
69using cricket::FakeWebRtcVideoDecoderFactory;
70using cricket::FakeWebRtcVideoEncoder;
71using cricket::FakeWebRtcVideoEncoderFactory;
72using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070073using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070074using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080075using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070076using ::testing::ElementsAre;
77using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070078using webrtc::DataBuffer;
79using webrtc::DataChannelInterface;
80using webrtc::DtmfSender;
81using webrtc::DtmfSenderInterface;
82using webrtc::DtmfSenderObserverInterface;
83using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080084using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070085using webrtc::MediaConstraintsInterface;
86using webrtc::MediaStreamInterface;
87using webrtc::MediaStreamTrackInterface;
88using webrtc::MockCreateSessionDescriptionObserver;
89using webrtc::MockDataChannelObserver;
90using webrtc::MockSetSessionDescriptionObserver;
91using webrtc::MockStatsObserver;
92using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070093using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070094using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080095using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070096using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070097using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080098using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070099using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -0800100using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100101using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800102using webrtc::RtpSenderInterface;
103using webrtc::RtpTransceiverDirection;
104using webrtc::RtpTransceiverInit;
105using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800106using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800107using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700108using webrtc::SessionDescriptionInterface;
109using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800110using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700111
112namespace {
113
114static const int kDefaultTimeout = 10000;
115static const int kMaxWaitForStatsMs = 3000;
116static const int kMaxWaitForActivationMs = 5000;
117static const int kMaxWaitForFramesMs = 10000;
118// Default number of audio/video frames to wait for before considering a test
119// successful.
120static const int kDefaultExpectedAudioFrameCount = 3;
121static const int kDefaultExpectedVideoFrameCount = 3;
122
deadbeef1dcb1642017-03-29 21:08:16 -0700123static const char kDataChannelLabel[] = "data_channel";
124
125// SRTP cipher name negotiated by the tests. This must be updated if the
126// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700127static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700128static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
129
Steve Antonede9ca52017-10-16 13:04:27 -0700130static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
131
deadbeef1dcb1642017-03-29 21:08:16 -0700132// Helper function for constructing offer/answer options to initiate an ICE
133// restart.
134PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
135 PeerConnectionInterface::RTCOfferAnswerOptions options;
136 options.ice_restart = true;
137 return options;
138}
139
deadbeefd8ad7882017-04-18 16:01:17 -0700140// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
141// attribute from received SDP, simulating a legacy endpoint.
142void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
143 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800144 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700145 }
146 desc->set_msid_supported(false);
147}
148
Seth Hampson5897a6e2018-04-03 11:16:33 -0700149// Removes all stream information besides the stream ids, simulating an
150// endpoint that only signals a=msid lines to convey stream_ids.
151void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
152 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700153 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700154 std::vector<std::string> stream_ids;
155 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700156 const StreamParams& first_stream =
157 content.media_description()->streams()[0];
158 track_id = first_stream.id;
159 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700160 }
161 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700162 StreamParams new_stream;
163 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700164 new_stream.set_stream_ids(stream_ids);
165 content.media_description()->AddStream(new_stream);
166 }
167}
168
zhihuangf8164932017-05-19 13:09:47 -0700169int FindFirstMediaStatsIndexByKind(
170 const std::string& kind,
171 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
172 media_stats_vec) {
173 for (size_t i = 0; i < media_stats_vec.size(); i++) {
174 if (media_stats_vec[i]->kind.ValueToString() == kind) {
175 return i;
176 }
177 }
178 return -1;
179}
180
deadbeef1dcb1642017-03-29 21:08:16 -0700181class SignalingMessageReceiver {
182 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800183 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700184 virtual void ReceiveIceMessage(const std::string& sdp_mid,
185 int sdp_mline_index,
186 const std::string& msg) = 0;
187
188 protected:
189 SignalingMessageReceiver() {}
190 virtual ~SignalingMessageReceiver() {}
191};
192
193class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
194 public:
195 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
196 : expected_media_type_(media_type) {}
197
198 void OnFirstPacketReceived(cricket::MediaType media_type) override {
199 ASSERT_EQ(expected_media_type_, media_type);
200 first_packet_received_ = true;
201 }
202
203 bool first_packet_received() const { return first_packet_received_; }
204
205 virtual ~MockRtpReceiverObserver() {}
206
207 private:
208 bool first_packet_received_ = false;
209 cricket::MediaType expected_media_type_;
210};
211
212// Helper class that wraps a peer connection, observes it, and can accept
213// signaling messages from another wrapper.
214//
215// Uses a fake network, fake A/V capture, and optionally fake
216// encoders/decoders, though they aren't used by default since they don't
217// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700218// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800219// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700220class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800221 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700222 public:
223 // Different factory methods for convenience.
224 // TODO(deadbeef): Could use the pattern of:
225 //
226 // PeerConnectionWrapper =
227 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
228 //
229 // To reduce some code duplication.
230 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
231 const std::string& debug_name,
232 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
233 rtc::Thread* network_thread,
234 rtc::Thread* worker_thread) {
235 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700236 webrtc::PeerConnectionDependencies dependencies(nullptr);
237 dependencies.cert_generator = std::move(cert_generator);
238 if (!client->Init(nullptr, nullptr, nullptr, std::move(dependencies),
Qingsi Wang7685e862018-06-11 20:15:46 -0700239 network_thread, worker_thread, nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700240 delete client;
241 return nullptr;
242 }
243 return client;
244 }
245
deadbeef2f425aa2017-04-14 10:41:32 -0700246 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
247 return peer_connection_factory_.get();
248 }
249
deadbeef1dcb1642017-03-29 21:08:16 -0700250 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
251
252 // If a signaling message receiver is set (via ConnectFakeSignaling), this
253 // will set the whole offer/answer exchange in motion. Just need to wait for
254 // the signaling state to reach "stable".
255 void CreateAndSetAndSignalOffer() {
256 auto offer = CreateOffer();
257 ASSERT_NE(nullptr, offer);
258 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
259 }
260
261 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
262 // when a remote offer is received (via fake signaling) and an answer is
263 // generated. By default, uses default options.
264 void SetOfferAnswerOptions(
265 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
266 offer_answer_options_ = options;
267 }
268
269 // Set a callback to be invoked when SDP is received via the fake signaling
270 // channel, which provides an opportunity to munge (modify) the SDP. This is
271 // used to test SDP being applied that a PeerConnection would normally not
272 // generate, but a non-JSEP endpoint might.
273 void SetReceivedSdpMunger(
274 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100275 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700276 }
277
deadbeefc964d0b2017-04-03 10:03:35 -0700278 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700279 // generated.
280 void SetGeneratedSdpMunger(
281 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100282 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700283 }
284
Seth Hampson2f0d7022018-02-20 11:54:42 -0800285 // Set a callback to be invoked when a remote offer is received via the fake
286 // signaling channel. This provides an opportunity to change the
287 // PeerConnection state before an answer is created and sent to the caller.
288 void SetRemoteOfferHandler(std::function<void()> handler) {
289 remote_offer_handler_ = std::move(handler);
290 }
291
Steve Antonede9ca52017-10-16 13:04:27 -0700292 // Every ICE connection state in order that has been seen by the observer.
293 std::vector<PeerConnectionInterface::IceConnectionState>
294 ice_connection_state_history() const {
295 return ice_connection_state_history_;
296 }
Steve Anton6f25b092017-10-23 09:39:20 -0700297 void clear_ice_connection_state_history() {
298 ice_connection_state_history_.clear();
299 }
Steve Antonede9ca52017-10-16 13:04:27 -0700300
301 // Every ICE gathering state in order that has been seen by the observer.
302 std::vector<PeerConnectionInterface::IceGatheringState>
303 ice_gathering_state_history() const {
304 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700305 }
306
Steve Anton15324772018-01-16 10:26:49 -0800307 void AddAudioVideoTracks() {
308 AddAudioTrack();
309 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700310 }
311
Steve Anton74255ff2018-01-24 18:32:57 -0800312 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
313 return AddTrack(CreateLocalAudioTrack());
314 }
deadbeef1dcb1642017-03-29 21:08:16 -0700315
Steve Anton74255ff2018-01-24 18:32:57 -0800316 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
317 return AddTrack(CreateLocalVideoTrack());
318 }
deadbeef1dcb1642017-03-29 21:08:16 -0700319
320 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200321 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700322 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200323 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700324 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200325 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700326 // TODO(perkj): Test audio source when it is implemented. Currently audio
327 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700328 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700329 source);
330 }
331
332 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200333 return CreateLocalVideoTrackInternal(
334 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700335 }
336
337 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200338 CreateLocalVideoTrackWithConfig(
339 webrtc::FakePeriodicVideoSource::Config config) {
340 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700341 }
342
343 rtc::scoped_refptr<webrtc::VideoTrackInterface>
344 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200345 webrtc::FakePeriodicVideoSource::Config config;
346 config.rotation = rotation;
347 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700348 }
349
Steve Anton74255ff2018-01-24 18:32:57 -0800350 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
351 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800352 const std::vector<std::string>& stream_ids = {}) {
353 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800354 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800355 return result.MoveValue();
356 }
357
358 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
359 cricket::MediaType media_type) {
360 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
361 for (auto receiver : pc()->GetReceivers()) {
362 if (receiver->media_type() == media_type) {
363 receivers.push_back(receiver);
364 }
365 }
366 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700367 }
368
Seth Hampson2f0d7022018-02-20 11:54:42 -0800369 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
370 cricket::MediaType media_type) {
371 for (auto transceiver : pc()->GetTransceivers()) {
372 if (transceiver->receiver()->media_type() == media_type) {
373 return transceiver;
374 }
375 }
376 return nullptr;
377 }
378
deadbeef1dcb1642017-03-29 21:08:16 -0700379 bool SignalingStateStable() {
380 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
381 }
382
383 void CreateDataChannel() { CreateDataChannel(nullptr); }
384
385 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700386 CreateDataChannel(kDataChannelLabel, init);
387 }
388
389 void CreateDataChannel(const std::string& label,
390 const webrtc::DataChannelInit* init) {
391 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700392 ASSERT_TRUE(data_channel_.get() != nullptr);
393 data_observer_.reset(new MockDataChannelObserver(data_channel_));
394 }
395
396 DataChannelInterface* data_channel() { return data_channel_; }
397 const MockDataChannelObserver* data_observer() const {
398 return data_observer_.get();
399 }
400
401 int audio_frames_received() const {
402 return fake_audio_capture_module_->frames_received();
403 }
404
405 // Takes minimum of video frames received for each track.
406 //
407 // Can be used like:
408 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
409 //
410 // To ensure that all video tracks received at least a certain number of
411 // frames.
412 int min_video_frames_received_per_track() const {
413 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200414 if (fake_video_renderers_.empty()) {
415 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700416 }
deadbeef1dcb1642017-03-29 21:08:16 -0700417
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200418 for (const auto& pair : fake_video_renderers_) {
419 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700420 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200421 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700422 }
423
424 // Returns a MockStatsObserver in a state after stats gathering finished,
425 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700426 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700427 webrtc::MediaStreamTrackInterface* track) {
428 rtc::scoped_refptr<MockStatsObserver> observer(
429 new rtc::RefCountedObject<MockStatsObserver>());
430 EXPECT_TRUE(peer_connection_->GetStats(
431 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
432 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
433 return observer;
434 }
435
436 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700437 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
438 return OldGetStatsForTrack(nullptr);
439 }
440
441 // Synchronously gets stats and returns them. If it times out, fails the test
442 // and returns null.
443 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
444 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
445 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
446 peer_connection_->GetStats(callback);
447 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
448 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700449 }
450
451 int rendered_width() {
452 EXPECT_FALSE(fake_video_renderers_.empty());
453 return fake_video_renderers_.empty()
454 ? 0
455 : fake_video_renderers_.begin()->second->width();
456 }
457
458 int rendered_height() {
459 EXPECT_FALSE(fake_video_renderers_.empty());
460 return fake_video_renderers_.empty()
461 ? 0
462 : fake_video_renderers_.begin()->second->height();
463 }
464
465 double rendered_aspect_ratio() {
466 if (rendered_height() == 0) {
467 return 0.0;
468 }
469 return static_cast<double>(rendered_width()) / rendered_height();
470 }
471
472 webrtc::VideoRotation rendered_rotation() {
473 EXPECT_FALSE(fake_video_renderers_.empty());
474 return fake_video_renderers_.empty()
475 ? webrtc::kVideoRotation_0
476 : fake_video_renderers_.begin()->second->rotation();
477 }
478
479 int local_rendered_width() {
480 return local_video_renderer_ ? local_video_renderer_->width() : 0;
481 }
482
483 int local_rendered_height() {
484 return local_video_renderer_ ? local_video_renderer_->height() : 0;
485 }
486
487 double local_rendered_aspect_ratio() {
488 if (local_rendered_height() == 0) {
489 return 0.0;
490 }
491 return static_cast<double>(local_rendered_width()) /
492 local_rendered_height();
493 }
494
495 size_t number_of_remote_streams() {
496 if (!pc()) {
497 return 0;
498 }
499 return pc()->remote_streams()->count();
500 }
501
502 StreamCollectionInterface* remote_streams() const {
503 if (!pc()) {
504 ADD_FAILURE();
505 return nullptr;
506 }
507 return pc()->remote_streams();
508 }
509
510 StreamCollectionInterface* local_streams() {
511 if (!pc()) {
512 ADD_FAILURE();
513 return nullptr;
514 }
515 return pc()->local_streams();
516 }
517
518 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
519 return pc()->signaling_state();
520 }
521
522 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
523 return pc()->ice_connection_state();
524 }
525
526 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
527 return pc()->ice_gathering_state();
528 }
529
530 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
531 // GetReceivers. They're updated automatically when a remote offer/answer
532 // from the fake signaling channel is applied, or when
533 // ResetRtpReceiverObservers below is called.
534 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
535 rtp_receiver_observers() {
536 return rtp_receiver_observers_;
537 }
538
539 void ResetRtpReceiverObservers() {
540 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100541 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
542 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700543 std::unique_ptr<MockRtpReceiverObserver> observer(
544 new MockRtpReceiverObserver(receiver->media_type()));
545 receiver->SetObserver(observer.get());
546 rtp_receiver_observers_.push_back(std::move(observer));
547 }
548 }
549
Steve Antonede9ca52017-10-16 13:04:27 -0700550 rtc::FakeNetworkManager* network() const {
551 return fake_network_manager_.get();
552 }
553 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
554
Qingsi Wang7685e862018-06-11 20:15:46 -0700555 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
556 return event_log_factory_;
557 }
558
deadbeef1dcb1642017-03-29 21:08:16 -0700559 private:
560 explicit PeerConnectionWrapper(const std::string& debug_name)
561 : debug_name_(debug_name) {}
562
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700563 bool Init(const MediaConstraintsInterface* constraints,
564 const PeerConnectionFactory::Options* options,
565 const PeerConnectionInterface::RTCConfiguration* config,
566 webrtc::PeerConnectionDependencies dependencies,
567 rtc::Thread* network_thread,
Qingsi Wang7685e862018-06-11 20:15:46 -0700568 rtc::Thread* worker_thread,
569 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700570 // There's an error in this test code if Init ends up being called twice.
571 RTC_DCHECK(!peer_connection_);
572 RTC_DCHECK(!peer_connection_factory_);
573
574 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700575 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700576
577 std::unique_ptr<cricket::PortAllocator> port_allocator(
578 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700579 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700580 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
581 if (!fake_audio_capture_module_) {
582 return false;
583 }
deadbeef1dcb1642017-03-29 21:08:16 -0700584 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700585
586 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
587 pc_factory_dependencies.network_thread = network_thread;
588 pc_factory_dependencies.worker_thread = worker_thread;
589 pc_factory_dependencies.signaling_thread = signaling_thread;
590 pc_factory_dependencies.media_engine =
591 cricket::WebRtcMediaEngineFactory::Create(
592 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
593 fake_audio_capture_module_),
594 webrtc::CreateBuiltinAudioEncoderFactory(),
595 webrtc::CreateBuiltinAudioDecoderFactory(),
596 webrtc::CreateBuiltinVideoEncoderFactory(),
597 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
598 webrtc::AudioProcessingBuilder().Create());
599 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
600 if (event_log_factory) {
601 event_log_factory_ = event_log_factory.get();
602 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
603 } else {
604 pc_factory_dependencies.event_log_factory =
605 webrtc::CreateRtcEventLogFactory();
606 }
607 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
608 std::move(pc_factory_dependencies));
609
deadbeef1dcb1642017-03-29 21:08:16 -0700610 if (!peer_connection_factory_) {
611 return false;
612 }
613 if (options) {
614 peer_connection_factory_->SetOptions(*options);
615 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800616 if (config) {
617 sdp_semantics_ = config->sdp_semantics;
618 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700619
620 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700621 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700622 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700623 return peer_connection_.get() != nullptr;
624 }
625
626 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700627 const MediaConstraintsInterface* constraints,
628 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700629 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700630 PeerConnectionInterface::RTCConfiguration modified_config;
631 // If |config| is null, this will result in a default configuration being
632 // used.
633 if (config) {
634 modified_config = *config;
635 }
636 // Disable resolution adaptation; we don't want it interfering with the
637 // test results.
638 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
639 // ratios and not specific resolutions, is this even necessary?
640 modified_config.set_cpu_adaptation(false);
641
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700642 // Use the legacy interface.
643 if (constraints != nullptr) {
644 return peer_connection_factory_->CreatePeerConnection(
645 modified_config, constraints, std::move(dependencies.allocator),
646 std::move(dependencies.cert_generator), this);
647 }
648 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700649 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700650 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700651 }
652
653 void set_signaling_message_receiver(
654 SignalingMessageReceiver* signaling_message_receiver) {
655 signaling_message_receiver_ = signaling_message_receiver;
656 }
657
658 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
659
Steve Antonede9ca52017-10-16 13:04:27 -0700660 void set_signal_ice_candidates(bool signal) {
661 signal_ice_candidates_ = signal;
662 }
663
deadbeef1dcb1642017-03-29 21:08:16 -0700664 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200665 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700666 // Set max frame rate to 10fps to reduce the risk of test flakiness.
667 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200668 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700669
Niels Möller5c7efe72018-05-11 10:34:46 +0200670 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200671 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
672 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700673 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200674 peer_connection_factory_->CreateVideoTrack(
675 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700676 if (!local_video_renderer_) {
677 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
678 }
679 return track;
680 }
681
682 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100683 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800684 std::unique_ptr<SessionDescriptionInterface> desc =
685 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700686 if (received_sdp_munger_) {
687 received_sdp_munger_(desc->description());
688 }
689
690 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
691 // Setting a remote description may have changed the number of receivers,
692 // so reset the receiver observers.
693 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800694 if (remote_offer_handler_) {
695 remote_offer_handler_();
696 }
deadbeef1dcb1642017-03-29 21:08:16 -0700697 auto answer = CreateAnswer();
698 ASSERT_NE(nullptr, answer);
699 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
700 }
701
702 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800704 std::unique_ptr<SessionDescriptionInterface> desc =
705 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700706 if (received_sdp_munger_) {
707 received_sdp_munger_(desc->description());
708 }
709
710 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
711 // Set the RtpReceiverObserver after receivers are created.
712 ResetRtpReceiverObservers();
713 }
714
715 // Returns null on failure.
716 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
717 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
718 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
719 pc()->CreateOffer(observer, offer_answer_options_);
720 return WaitForDescriptionFromObserver(observer);
721 }
722
723 // Returns null on failure.
724 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
725 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
726 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
727 pc()->CreateAnswer(observer, offer_answer_options_);
728 return WaitForDescriptionFromObserver(observer);
729 }
730
731 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100732 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700733 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
734 if (!observer->result()) {
735 return nullptr;
736 }
737 auto description = observer->MoveDescription();
738 if (generated_sdp_munger_) {
739 generated_sdp_munger_(description->description());
740 }
741 return description;
742 }
743
744 // Setting the local description and sending the SDP message over the fake
745 // signaling channel are combined into the same method because the SDP
746 // message needs to be sent as soon as SetLocalDescription finishes, without
747 // waiting for the observer to be called. This ensures that ICE candidates
748 // don't outrace the description.
749 bool SetLocalDescriptionAndSendSdpMessage(
750 std::unique_ptr<SessionDescriptionInterface> desc) {
751 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
752 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100753 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800754 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700755 std::string sdp;
756 EXPECT_TRUE(desc->ToString(&sdp));
757 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800758 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
759 RemoveUnusedVideoRenderers();
760 }
deadbeef1dcb1642017-03-29 21:08:16 -0700761 // As mentioned above, we need to send the message immediately after
762 // SetLocalDescription.
763 SendSdpMessage(type, sdp);
764 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
765 return true;
766 }
767
768 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
769 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
770 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100771 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700772 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800773 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
774 RemoveUnusedVideoRenderers();
775 }
deadbeef1dcb1642017-03-29 21:08:16 -0700776 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
777 return observer->result();
778 }
779
Seth Hampson2f0d7022018-02-20 11:54:42 -0800780 // This is a work around to remove unused fake_video_renderers from
781 // transceivers that have either stopped or are no longer receiving.
782 void RemoveUnusedVideoRenderers() {
783 auto transceivers = pc()->GetTransceivers();
784 for (auto& transceiver : transceivers) {
785 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
786 continue;
787 }
788 // Remove fake video renderers from any stopped transceivers.
789 if (transceiver->stopped()) {
790 auto it =
791 fake_video_renderers_.find(transceiver->receiver()->track()->id());
792 if (it != fake_video_renderers_.end()) {
793 fake_video_renderers_.erase(it);
794 }
795 }
796 // Remove fake video renderers from any transceivers that are no longer
797 // receiving.
798 if ((transceiver->current_direction() &&
799 !webrtc::RtpTransceiverDirectionHasRecv(
800 *transceiver->current_direction()))) {
801 auto it =
802 fake_video_renderers_.find(transceiver->receiver()->track()->id());
803 if (it != fake_video_renderers_.end()) {
804 fake_video_renderers_.erase(it);
805 }
806 }
807 }
808 }
809
deadbeef1dcb1642017-03-29 21:08:16 -0700810 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
811 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800812 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700813 if (signaling_delay_ms_ == 0) {
814 RelaySdpMessageIfReceiverExists(type, msg);
815 } else {
816 invoker_.AsyncInvokeDelayed<void>(
817 RTC_FROM_HERE, rtc::Thread::Current(),
818 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
819 this, type, msg),
820 signaling_delay_ms_);
821 }
822 }
823
Steve Antona3a92c22017-12-07 10:27:41 -0800824 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700825 if (signaling_message_receiver_) {
826 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
827 }
828 }
829
830 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
831 // default).
832 void SendIceMessage(const std::string& sdp_mid,
833 int sdp_mline_index,
834 const std::string& msg) {
835 if (signaling_delay_ms_ == 0) {
836 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
837 } else {
838 invoker_.AsyncInvokeDelayed<void>(
839 RTC_FROM_HERE, rtc::Thread::Current(),
840 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
841 this, sdp_mid, sdp_mline_index, msg),
842 signaling_delay_ms_);
843 }
844 }
845
846 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
847 int sdp_mline_index,
848 const std::string& msg) {
849 if (signaling_message_receiver_) {
850 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
851 msg);
852 }
853 }
854
855 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800856 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
857 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700858 HandleIncomingOffer(msg);
859 } else {
860 HandleIncomingAnswer(msg);
861 }
862 }
863
864 void ReceiveIceMessage(const std::string& sdp_mid,
865 int sdp_mline_index,
866 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100867 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700868 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
869 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
870 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
871 }
872
873 // PeerConnectionObserver callbacks.
874 void OnSignalingChange(
875 webrtc::PeerConnectionInterface::SignalingState new_state) override {
876 EXPECT_EQ(pc()->signaling_state(), new_state);
877 }
Steve Anton15324772018-01-16 10:26:49 -0800878 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
879 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
880 streams) override {
881 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
882 rtc::scoped_refptr<VideoTrackInterface> video_track(
883 static_cast<VideoTrackInterface*>(receiver->track().get()));
884 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700885 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800886 fake_video_renderers_[video_track->id()] =
887 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700888 }
889 }
Steve Anton15324772018-01-16 10:26:49 -0800890 void OnRemoveTrack(
891 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
892 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
893 auto it = fake_video_renderers_.find(receiver->track()->id());
894 RTC_DCHECK(it != fake_video_renderers_.end());
895 fake_video_renderers_.erase(it);
896 }
897 }
deadbeef1dcb1642017-03-29 21:08:16 -0700898 void OnRenegotiationNeeded() override {}
899 void OnIceConnectionChange(
900 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
901 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700902 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700903 }
904 void OnIceGatheringChange(
905 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700906 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700907 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700908 }
909 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100910 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700911
912 std::string ice_sdp;
913 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700914 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700915 // Remote party may be deleted.
916 return;
917 }
918 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
919 }
920 void OnDataChannel(
921 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100922 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700923 data_channel_ = data_channel;
924 data_observer_.reset(new MockDataChannelObserver(data_channel));
925 }
926
deadbeef1dcb1642017-03-29 21:08:16 -0700927 std::string debug_name_;
928
929 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
930
931 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
932 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
933 peer_connection_factory_;
934
Steve Antonede9ca52017-10-16 13:04:27 -0700935 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700936 // Needed to keep track of number of frames sent.
937 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
938 // Needed to keep track of number of frames received.
939 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
940 fake_video_renderers_;
941 // Needed to ensure frames aren't received for removed tracks.
942 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
943 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700944
945 // For remote peer communication.
946 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
947 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700948 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700949
Niels Möller5c7efe72018-05-11 10:34:46 +0200950 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700951 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200952 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
953 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700954 // |local_video_renderer_| attached to the first created local video track.
955 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
956
Seth Hampson2f0d7022018-02-20 11:54:42 -0800957 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700958 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
959 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
960 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800961 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700962
963 rtc::scoped_refptr<DataChannelInterface> data_channel_;
964 std::unique_ptr<MockDataChannelObserver> data_observer_;
965
966 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
967
Steve Antonede9ca52017-10-16 13:04:27 -0700968 std::vector<PeerConnectionInterface::IceConnectionState>
969 ice_connection_state_history_;
970 std::vector<PeerConnectionInterface::IceGatheringState>
971 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700972
Qingsi Wang7685e862018-06-11 20:15:46 -0700973 webrtc::FakeRtcEventLogFactory* event_log_factory_;
974
deadbeef1dcb1642017-03-29 21:08:16 -0700975 rtc::AsyncInvoker invoker_;
976
Seth Hampson2f0d7022018-02-20 11:54:42 -0800977 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700978};
979
Elad Alon99c3fe52017-10-13 16:29:40 +0200980class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
981 public:
982 virtual ~MockRtcEventLogOutput() = default;
983 MOCK_CONST_METHOD0(IsActive, bool());
984 MOCK_METHOD1(Write, bool(const std::string&));
985};
986
Seth Hampson2f0d7022018-02-20 11:54:42 -0800987// This helper object is used for both specifying how many audio/video frames
988// are expected to be received for a caller/callee. It provides helper functions
989// to specify these expectations. The object initially starts in a state of no
990// expectations.
991class MediaExpectations {
992 public:
993 enum ExpectFrames {
994 kExpectSomeFrames,
995 kExpectNoFrames,
996 kNoExpectation,
997 };
998
999 void ExpectBidirectionalAudioAndVideo() {
1000 ExpectBidirectionalAudio();
1001 ExpectBidirectionalVideo();
1002 }
1003
1004 void ExpectBidirectionalAudio() {
1005 CallerExpectsSomeAudio();
1006 CalleeExpectsSomeAudio();
1007 }
1008
1009 void ExpectNoAudio() {
1010 CallerExpectsNoAudio();
1011 CalleeExpectsNoAudio();
1012 }
1013
1014 void ExpectBidirectionalVideo() {
1015 CallerExpectsSomeVideo();
1016 CalleeExpectsSomeVideo();
1017 }
1018
1019 void ExpectNoVideo() {
1020 CallerExpectsNoVideo();
1021 CalleeExpectsNoVideo();
1022 }
1023
1024 void CallerExpectsSomeAudioAndVideo() {
1025 CallerExpectsSomeAudio();
1026 CallerExpectsSomeVideo();
1027 }
1028
1029 void CalleeExpectsSomeAudioAndVideo() {
1030 CalleeExpectsSomeAudio();
1031 CalleeExpectsSomeVideo();
1032 }
1033
1034 // Caller's audio functions.
1035 void CallerExpectsSomeAudio(
1036 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1037 caller_audio_expectation_ = kExpectSomeFrames;
1038 caller_audio_frames_expected_ = expected_audio_frames;
1039 }
1040
1041 void CallerExpectsNoAudio() {
1042 caller_audio_expectation_ = kExpectNoFrames;
1043 caller_audio_frames_expected_ = 0;
1044 }
1045
1046 // Caller's video functions.
1047 void CallerExpectsSomeVideo(
1048 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1049 caller_video_expectation_ = kExpectSomeFrames;
1050 caller_video_frames_expected_ = expected_video_frames;
1051 }
1052
1053 void CallerExpectsNoVideo() {
1054 caller_video_expectation_ = kExpectNoFrames;
1055 caller_video_frames_expected_ = 0;
1056 }
1057
1058 // Callee's audio functions.
1059 void CalleeExpectsSomeAudio(
1060 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1061 callee_audio_expectation_ = kExpectSomeFrames;
1062 callee_audio_frames_expected_ = expected_audio_frames;
1063 }
1064
1065 void CalleeExpectsNoAudio() {
1066 callee_audio_expectation_ = kExpectNoFrames;
1067 callee_audio_frames_expected_ = 0;
1068 }
1069
1070 // Callee's video functions.
1071 void CalleeExpectsSomeVideo(
1072 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1073 callee_video_expectation_ = kExpectSomeFrames;
1074 callee_video_frames_expected_ = expected_video_frames;
1075 }
1076
1077 void CalleeExpectsNoVideo() {
1078 callee_video_expectation_ = kExpectNoFrames;
1079 callee_video_frames_expected_ = 0;
1080 }
1081
1082 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1083 ExpectFrames caller_video_expectation_ = kNoExpectation;
1084 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1085 ExpectFrames callee_video_expectation_ = kNoExpectation;
1086 int caller_audio_frames_expected_ = 0;
1087 int caller_video_frames_expected_ = 0;
1088 int callee_audio_frames_expected_ = 0;
1089 int callee_video_frames_expected_ = 0;
1090};
1091
deadbeef1dcb1642017-03-29 21:08:16 -07001092// Tests two PeerConnections connecting to each other end-to-end, using a
1093// virtual network, fake A/V capture and fake encoder/decoders. The
1094// PeerConnections share the threads/socket servers, but use separate versions
1095// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001096class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001097 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001098 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1099 : sdp_semantics_(sdp_semantics),
1100 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001101 fss_(new rtc::FirewallSocketServer(ss_.get())),
1102 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001103 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001104 network_thread_->SetName("PCNetworkThread", this);
1105 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001106 RTC_CHECK(network_thread_->Start());
1107 RTC_CHECK(worker_thread_->Start());
1108 }
1109
Seth Hampson2f0d7022018-02-20 11:54:42 -08001110 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001111 // The PeerConnections should deleted before the TurnCustomizers.
1112 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1113 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1114 // that the TurnCustomizer outlives the life of the PeerConnection or else
1115 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001116 if (caller_) {
1117 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001118 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001119 }
1120 if (callee_) {
1121 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001122 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001123 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001124
1125 // If turn servers were created for the test they need to be destroyed on
1126 // the network thread.
1127 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1128 turn_servers_.clear();
1129 turn_customizers_.clear();
1130 });
deadbeef1dcb1642017-03-29 21:08:16 -07001131 }
1132
1133 bool SignalingStateStable() {
1134 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1135 }
1136
deadbeef71452802017-05-07 17:21:01 -07001137 bool DtlsConnected() {
1138 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1139 // are connected. This is an important distinction. Once we have separate
1140 // ICE and DTLS state, this check needs to use the DTLS state.
1141 return (callee()->ice_connection_state() ==
1142 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1143 callee()->ice_connection_state() ==
1144 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1145 (caller()->ice_connection_state() ==
1146 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1147 caller()->ice_connection_state() ==
1148 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1149 }
1150
Qingsi Wang7685e862018-06-11 20:15:46 -07001151 // When |event_log_factory| is null, the default implementation of the event
1152 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001153 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1154 const std::string& debug_name,
1155 const MediaConstraintsInterface* constraints,
1156 const PeerConnectionFactory::Options* options,
1157 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001158 webrtc::PeerConnectionDependencies dependencies,
1159 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001160 RTCConfiguration modified_config;
1161 if (config) {
1162 modified_config = *config;
1163 }
Steve Anton3acffc32018-04-12 17:21:03 -07001164 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001165 if (!dependencies.cert_generator) {
1166 dependencies.cert_generator =
1167 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001168 }
1169 std::unique_ptr<PeerConnectionWrapper> client(
1170 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001171
Seth Hampson2f0d7022018-02-20 11:54:42 -08001172 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001173 std::move(dependencies), network_thread_.get(),
Qingsi Wang7685e862018-06-11 20:15:46 -07001174 worker_thread_.get(), std::move(event_log_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001175 return nullptr;
1176 }
1177 return client;
1178 }
1179
Qingsi Wang7685e862018-06-11 20:15:46 -07001180 std::unique_ptr<PeerConnectionWrapper>
1181 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1182 const std::string& debug_name,
1183 const MediaConstraintsInterface* constraints,
1184 const PeerConnectionFactory::Options* options,
1185 const RTCConfiguration* config,
1186 webrtc::PeerConnectionDependencies dependencies) {
1187 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1188 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
1189 return CreatePeerConnectionWrapper(debug_name, constraints, options, config,
1190 std::move(dependencies),
1191 std::move(event_log_factory));
1192 }
1193
deadbeef1dcb1642017-03-29 21:08:16 -07001194 bool CreatePeerConnectionWrappers() {
1195 return CreatePeerConnectionWrappersWithConfig(
1196 PeerConnectionInterface::RTCConfiguration(),
1197 PeerConnectionInterface::RTCConfiguration());
1198 }
1199
Steve Anton3acffc32018-04-12 17:21:03 -07001200 bool CreatePeerConnectionWrappersWithSdpSemantics(
1201 SdpSemantics caller_semantics,
1202 SdpSemantics callee_semantics) {
1203 // Can't specify the sdp_semantics in the passed-in configuration since it
1204 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1205 // stored in sdp_semantics_. So get around this by modifying the instance
1206 // variable before calling CreatePeerConnectionWrapper for the caller and
1207 // callee PeerConnections.
1208 SdpSemantics original_semantics = sdp_semantics_;
1209 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001210 caller_ = CreatePeerConnectionWrapper(
1211 "Caller", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001212 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001213 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001214 callee_ = CreatePeerConnectionWrapper(
1215 "Callee", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001216 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001217 sdp_semantics_ = original_semantics;
1218 return caller_ && callee_;
1219 }
1220
deadbeef1dcb1642017-03-29 21:08:16 -07001221 bool CreatePeerConnectionWrappersWithConstraints(
1222 MediaConstraintsInterface* caller_constraints,
1223 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001224 caller_ = CreatePeerConnectionWrapper(
1225 "Caller", caller_constraints, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001226 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001227 callee_ = CreatePeerConnectionWrapper(
1228 "Callee", callee_constraints, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001229 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001230
deadbeef1dcb1642017-03-29 21:08:16 -07001231 return caller_ && callee_;
1232 }
1233
1234 bool CreatePeerConnectionWrappersWithConfig(
1235 const PeerConnectionInterface::RTCConfiguration& caller_config,
1236 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001237 caller_ = CreatePeerConnectionWrapper(
1238 "Caller", nullptr, nullptr, &caller_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001239 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001240 callee_ = CreatePeerConnectionWrapper(
1241 "Callee", nullptr, nullptr, &callee_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001242 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001243 return caller_ && callee_;
1244 }
1245
1246 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1247 const PeerConnectionInterface::RTCConfiguration& caller_config,
1248 webrtc::PeerConnectionDependencies caller_dependencies,
1249 const PeerConnectionInterface::RTCConfiguration& callee_config,
1250 webrtc::PeerConnectionDependencies callee_dependencies) {
1251 caller_ =
1252 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001253 std::move(caller_dependencies), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001254 callee_ =
1255 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001256 std::move(callee_dependencies), nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001257 return caller_ && callee_;
1258 }
1259
1260 bool CreatePeerConnectionWrappersWithOptions(
1261 const PeerConnectionFactory::Options& caller_options,
1262 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001263 caller_ = CreatePeerConnectionWrapper(
1264 "Caller", nullptr, &caller_options, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001265 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001266 callee_ = CreatePeerConnectionWrapper(
1267 "Callee", nullptr, &callee_options, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001268 webrtc::PeerConnectionDependencies(nullptr), nullptr);
1269 return caller_ && callee_;
1270 }
1271
1272 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1273 PeerConnectionInterface::RTCConfiguration default_config;
1274 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1275 "Caller", nullptr, nullptr, &default_config,
1276 webrtc::PeerConnectionDependencies(nullptr));
1277 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1278 "Callee", nullptr, nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001280 return caller_ && callee_;
1281 }
1282
Seth Hampson2f0d7022018-02-20 11:54:42 -08001283 std::unique_ptr<PeerConnectionWrapper>
1284 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001285 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1286 new FakeRTCCertificateGenerator());
1287 cert_generator->use_alternate_key();
1288
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001289 webrtc::PeerConnectionDependencies dependencies(nullptr);
1290 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001292 std::move(dependencies), nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001293 }
1294
Seth Hampsonaed71642018-06-11 07:41:32 -07001295 cricket::TestTurnServer* CreateTurnServer(
1296 rtc::SocketAddress internal_address,
1297 rtc::SocketAddress external_address,
1298 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1299 const std::string& common_name = "test turn server") {
1300 rtc::Thread* thread = network_thread();
1301 std::unique_ptr<cricket::TestTurnServer> turn_server =
1302 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1303 RTC_FROM_HERE,
1304 [thread, internal_address, external_address, type, common_name] {
1305 return rtc::MakeUnique<cricket::TestTurnServer>(
1306 thread, internal_address, external_address, type,
1307 /*ignore_bad_certs=*/true, common_name);
1308 });
1309 turn_servers_.push_back(std::move(turn_server));
1310 // Interactions with the turn server should be done on the network thread.
1311 return turn_servers_.back().get();
1312 }
1313
1314 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1315 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1316 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1317 RTC_FROM_HERE,
1318 [] { return rtc::MakeUnique<cricket::TestTurnCustomizer>(); });
1319 turn_customizers_.push_back(std::move(turn_customizer));
1320 // Interactions with the turn customizer should be done on the network
1321 // thread.
1322 return turn_customizers_.back().get();
1323 }
1324
1325 // Checks that the function counters for a TestTurnCustomizer are greater than
1326 // 0.
1327 void ExpectTurnCustomizerCountersIncremented(
1328 cricket::TestTurnCustomizer* turn_customizer) {
1329 unsigned int allow_channel_data_counter =
1330 network_thread()->Invoke<unsigned int>(
1331 RTC_FROM_HERE, [turn_customizer] {
1332 return turn_customizer->allow_channel_data_cnt_;
1333 });
1334 EXPECT_GT(allow_channel_data_counter, 0u);
1335 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1336 RTC_FROM_HERE,
1337 [turn_customizer] { return turn_customizer->modify_cnt_; });
1338 EXPECT_GT(modify_counter, 0u);
1339 }
1340
deadbeef1dcb1642017-03-29 21:08:16 -07001341 // Once called, SDP blobs and ICE candidates will be automatically signaled
1342 // between PeerConnections.
1343 void ConnectFakeSignaling() {
1344 caller_->set_signaling_message_receiver(callee_.get());
1345 callee_->set_signaling_message_receiver(caller_.get());
1346 }
1347
Steve Antonede9ca52017-10-16 13:04:27 -07001348 // Once called, SDP blobs will be automatically signaled between
1349 // PeerConnections. Note that ICE candidates will not be signaled unless they
1350 // are in the exchanged SDP blobs.
1351 void ConnectFakeSignalingForSdpOnly() {
1352 ConnectFakeSignaling();
1353 SetSignalIceCandidates(false);
1354 }
1355
deadbeef1dcb1642017-03-29 21:08:16 -07001356 void SetSignalingDelayMs(int delay_ms) {
1357 caller_->set_signaling_delay_ms(delay_ms);
1358 callee_->set_signaling_delay_ms(delay_ms);
1359 }
1360
Steve Antonede9ca52017-10-16 13:04:27 -07001361 void SetSignalIceCandidates(bool signal) {
1362 caller_->set_signal_ice_candidates(signal);
1363 callee_->set_signal_ice_candidates(signal);
1364 }
1365
deadbeef1dcb1642017-03-29 21:08:16 -07001366 // Messages may get lost on the unreliable DataChannel, so we send multiple
1367 // times to avoid test flakiness.
1368 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1369 const std::string& data,
1370 int retries) {
1371 for (int i = 0; i < retries; ++i) {
1372 dc->Send(DataBuffer(data));
1373 }
1374 }
1375
1376 rtc::Thread* network_thread() { return network_thread_.get(); }
1377
1378 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1379
1380 PeerConnectionWrapper* caller() { return caller_.get(); }
1381
1382 // Set the |caller_| to the |wrapper| passed in and return the
1383 // original |caller_|.
1384 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1385 PeerConnectionWrapper* wrapper) {
1386 PeerConnectionWrapper* old = caller_.release();
1387 caller_.reset(wrapper);
1388 return old;
1389 }
1390
1391 PeerConnectionWrapper* callee() { return callee_.get(); }
1392
1393 // Set the |callee_| to the |wrapper| passed in and return the
1394 // original |callee_|.
1395 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1396 PeerConnectionWrapper* wrapper) {
1397 PeerConnectionWrapper* old = callee_.release();
1398 callee_.reset(wrapper);
1399 return old;
1400 }
1401
Steve Antonede9ca52017-10-16 13:04:27 -07001402 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1403
Seth Hampson2f0d7022018-02-20 11:54:42 -08001404 // Expects the provided number of new frames to be received within
1405 // kMaxWaitForFramesMs. The new expected frames are specified in
1406 // |media_expectations|. Returns false if any of the expectations were
1407 // not met.
1408 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1409 // First initialize the expected frame counts based upon the current
1410 // frame count.
1411 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1412 if (media_expectations.caller_audio_expectation_ ==
1413 MediaExpectations::kExpectSomeFrames) {
1414 total_caller_audio_frames_expected +=
1415 media_expectations.caller_audio_frames_expected_;
1416 }
1417 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001418 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001419 if (media_expectations.caller_video_expectation_ ==
1420 MediaExpectations::kExpectSomeFrames) {
1421 total_caller_video_frames_expected +=
1422 media_expectations.caller_video_frames_expected_;
1423 }
1424 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1425 if (media_expectations.callee_audio_expectation_ ==
1426 MediaExpectations::kExpectSomeFrames) {
1427 total_callee_audio_frames_expected +=
1428 media_expectations.callee_audio_frames_expected_;
1429 }
1430 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001431 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001432 if (media_expectations.callee_video_expectation_ ==
1433 MediaExpectations::kExpectSomeFrames) {
1434 total_callee_video_frames_expected +=
1435 media_expectations.callee_video_frames_expected_;
1436 }
deadbeef1dcb1642017-03-29 21:08:16 -07001437
Seth Hampson2f0d7022018-02-20 11:54:42 -08001438 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001439 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001440 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001441 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001442 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001443 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001444 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001445 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001446 total_callee_video_frames_expected,
1447 kMaxWaitForFramesMs);
1448 bool expectations_correct =
1449 caller()->audio_frames_received() >=
1450 total_caller_audio_frames_expected &&
1451 caller()->min_video_frames_received_per_track() >=
1452 total_caller_video_frames_expected &&
1453 callee()->audio_frames_received() >=
1454 total_callee_audio_frames_expected &&
1455 callee()->min_video_frames_received_per_track() >=
1456 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001457
Seth Hampson2f0d7022018-02-20 11:54:42 -08001458 // After the combined wait, print out a more detailed message upon
1459 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001460 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001461 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001462 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001463 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001464 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001465 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001466 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001467 total_callee_video_frames_expected);
1468
1469 // We want to make sure nothing unexpected was received.
1470 if (media_expectations.caller_audio_expectation_ ==
1471 MediaExpectations::kExpectNoFrames) {
1472 EXPECT_EQ(caller()->audio_frames_received(),
1473 total_caller_audio_frames_expected);
1474 if (caller()->audio_frames_received() !=
1475 total_caller_audio_frames_expected) {
1476 expectations_correct = false;
1477 }
1478 }
1479 if (media_expectations.caller_video_expectation_ ==
1480 MediaExpectations::kExpectNoFrames) {
1481 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1482 total_caller_video_frames_expected);
1483 if (caller()->min_video_frames_received_per_track() !=
1484 total_caller_video_frames_expected) {
1485 expectations_correct = false;
1486 }
1487 }
1488 if (media_expectations.callee_audio_expectation_ ==
1489 MediaExpectations::kExpectNoFrames) {
1490 EXPECT_EQ(callee()->audio_frames_received(),
1491 total_callee_audio_frames_expected);
1492 if (callee()->audio_frames_received() !=
1493 total_callee_audio_frames_expected) {
1494 expectations_correct = false;
1495 }
1496 }
1497 if (media_expectations.callee_video_expectation_ ==
1498 MediaExpectations::kExpectNoFrames) {
1499 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1500 total_callee_video_frames_expected);
1501 if (callee()->min_video_frames_received_per_track() !=
1502 total_callee_video_frames_expected) {
1503 expectations_correct = false;
1504 }
1505 }
1506 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001507 }
1508
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001509 void TestNegotiatedCipherSuite(
1510 const PeerConnectionFactory::Options& caller_options,
1511 const PeerConnectionFactory::Options& callee_options,
1512 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001513 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1514 callee_options));
1515 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1516 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1517 caller()->pc()->RegisterUMAObserver(caller_observer);
1518 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001519 caller()->AddAudioVideoTracks();
1520 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001521 caller()->CreateAndSetAndSignalOffer();
1522 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1523 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001524 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001525 EXPECT_EQ(
1526 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1527 expected_cipher_suite));
1528 caller()->pc()->RegisterUMAObserver(nullptr);
1529 }
1530
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001531 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1532 bool remote_gcm_enabled,
1533 int expected_cipher_suite) {
1534 PeerConnectionFactory::Options caller_options;
1535 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1536 PeerConnectionFactory::Options callee_options;
1537 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1538 TestNegotiatedCipherSuite(caller_options, callee_options,
1539 expected_cipher_suite);
1540 }
1541
Seth Hampson2f0d7022018-02-20 11:54:42 -08001542 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001543 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001544
deadbeef1dcb1642017-03-29 21:08:16 -07001545 private:
1546 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001547 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001548 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001549 // |network_thread_| and |worker_thread_| are used by both
1550 // |caller_| and |callee_| so they must be destroyed
1551 // later.
1552 std::unique_ptr<rtc::Thread> network_thread_;
1553 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001554 // The turn servers and turn customizers should be accessed & deleted on the
1555 // network thread to avoid a race with the socket read/write that occurs
1556 // on the network thread.
1557 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1558 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001559 std::unique_ptr<PeerConnectionWrapper> caller_;
1560 std::unique_ptr<PeerConnectionWrapper> callee_;
1561};
1562
Seth Hampson2f0d7022018-02-20 11:54:42 -08001563class PeerConnectionIntegrationTest
1564 : public PeerConnectionIntegrationBaseTest,
1565 public ::testing::WithParamInterface<SdpSemantics> {
1566 protected:
1567 PeerConnectionIntegrationTest()
1568 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1569};
1570
1571class PeerConnectionIntegrationTestPlanB
1572 : public PeerConnectionIntegrationBaseTest {
1573 protected:
1574 PeerConnectionIntegrationTestPlanB()
1575 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1576};
1577
1578class PeerConnectionIntegrationTestUnifiedPlan
1579 : public PeerConnectionIntegrationBaseTest {
1580 protected:
1581 PeerConnectionIntegrationTestUnifiedPlan()
1582 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1583};
1584
deadbeef1dcb1642017-03-29 21:08:16 -07001585// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1586// includes testing that the callback is invoked if an observer is connected
1587// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001588TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001589 RtpReceiverObserverOnFirstPacketReceived) {
1590 ASSERT_TRUE(CreatePeerConnectionWrappers());
1591 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001592 caller()->AddAudioVideoTracks();
1593 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001594 // Start offer/answer exchange and wait for it to complete.
1595 caller()->CreateAndSetAndSignalOffer();
1596 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1597 // Should be one receiver each for audio/video.
1598 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1599 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1600 // Wait for all "first packet received" callbacks to be fired.
1601 EXPECT_TRUE_WAIT(
1602 std::all_of(caller()->rtp_receiver_observers().begin(),
1603 caller()->rtp_receiver_observers().end(),
1604 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1605 return o->first_packet_received();
1606 }),
1607 kMaxWaitForFramesMs);
1608 EXPECT_TRUE_WAIT(
1609 std::all_of(callee()->rtp_receiver_observers().begin(),
1610 callee()->rtp_receiver_observers().end(),
1611 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1612 return o->first_packet_received();
1613 }),
1614 kMaxWaitForFramesMs);
1615 // If new observers are set after the first packet was already received, the
1616 // callback should still be invoked.
1617 caller()->ResetRtpReceiverObservers();
1618 callee()->ResetRtpReceiverObservers();
1619 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1620 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1621 EXPECT_TRUE(
1622 std::all_of(caller()->rtp_receiver_observers().begin(),
1623 caller()->rtp_receiver_observers().end(),
1624 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1625 return o->first_packet_received();
1626 }));
1627 EXPECT_TRUE(
1628 std::all_of(callee()->rtp_receiver_observers().begin(),
1629 callee()->rtp_receiver_observers().end(),
1630 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1631 return o->first_packet_received();
1632 }));
1633}
1634
1635class DummyDtmfObserver : public DtmfSenderObserverInterface {
1636 public:
1637 DummyDtmfObserver() : completed_(false) {}
1638
1639 // Implements DtmfSenderObserverInterface.
1640 void OnToneChange(const std::string& tone) override {
1641 tones_.push_back(tone);
1642 if (tone.empty()) {
1643 completed_ = true;
1644 }
1645 }
1646
1647 const std::vector<std::string>& tones() const { return tones_; }
1648 bool completed() const { return completed_; }
1649
1650 private:
1651 bool completed_;
1652 std::vector<std::string> tones_;
1653};
1654
1655// Assumes |sender| already has an audio track added and the offer/answer
1656// exchange is done.
1657void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1658 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001659 // We should be able to get a DTMF sender from the local sender.
1660 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1661 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1662 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001663 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001664 dtmf_sender->RegisterObserver(&observer);
1665
1666 // Test the DtmfSender object just created.
1667 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1668 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1669
1670 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1671 std::vector<std::string> tones = {"1", "a", ""};
1672 EXPECT_EQ(tones, observer.tones());
1673 dtmf_sender->UnregisterObserver();
1674 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1675}
1676
1677// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1678// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001679TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001680 ASSERT_TRUE(CreatePeerConnectionWrappers());
1681 ConnectFakeSignaling();
1682 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001683 caller()->AddAudioTrack();
1684 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001685 caller()->CreateAndSetAndSignalOffer();
1686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001687 // DTLS must finish before the DTMF sender can be used reliably.
1688 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001689 TestDtmfFromSenderToReceiver(caller(), callee());
1690 TestDtmfFromSenderToReceiver(callee(), caller());
1691}
1692
1693// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1694// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001695TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001696 ASSERT_TRUE(CreatePeerConnectionWrappers());
1697 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001698 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1699 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1700 caller()->pc()->RegisterUMAObserver(caller_observer);
1701
deadbeef1dcb1642017-03-29 21:08:16 -07001702 // Do normal offer/answer and wait for some frames to be received in each
1703 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001704 caller()->AddAudioVideoTracks();
1705 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001706 caller()->CreateAndSetAndSignalOffer();
1707 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001708 MediaExpectations media_expectations;
1709 media_expectations.ExpectBidirectionalAudioAndVideo();
1710 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001711 EXPECT_LE(
1712 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1713 webrtc::kEnumCounterKeyProtocolDtls));
1714 EXPECT_EQ(
1715 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1716 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001717}
1718
1719// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001720TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001721 PeerConnectionInterface::RTCConfiguration sdes_config;
1722 sdes_config.enable_dtls_srtp.emplace(false);
1723 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1724 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001725 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1726 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1727 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001728
1729 // Do normal offer/answer and wait for some frames to be received in each
1730 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001731 caller()->AddAudioVideoTracks();
1732 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001733 caller()->CreateAndSetAndSignalOffer();
1734 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001735 MediaExpectations media_expectations;
1736 media_expectations.ExpectBidirectionalAudioAndVideo();
1737 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001738 EXPECT_LE(
1739 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1740 webrtc::kEnumCounterKeyProtocolSdes));
1741 EXPECT_EQ(
1742 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1743 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001744}
1745
Steve Anton8c0f7a72017-10-03 10:03:10 -07001746// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1747// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001748TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001749 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1750 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1751 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1752 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1753 return pc->GetRemoteAudioSSLCertificate();
1754 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001755 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1756 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1757 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1758 return pc->GetRemoteAudioSSLCertChain();
1759 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001760
1761 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1762 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1763
1764 // Configure each side with a known certificate so they can be compared later.
1765 PeerConnectionInterface::RTCConfiguration caller_config;
1766 caller_config.enable_dtls_srtp.emplace(true);
1767 caller_config.certificates.push_back(caller_cert);
1768 PeerConnectionInterface::RTCConfiguration callee_config;
1769 callee_config.enable_dtls_srtp.emplace(true);
1770 callee_config.certificates.push_back(callee_cert);
1771 ASSERT_TRUE(
1772 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1773 ConnectFakeSignaling();
1774
1775 // When first initialized, there should not be a remote SSL certificate (and
1776 // calling this method should not crash).
1777 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1778 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001779 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1780 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001781
Steve Anton15324772018-01-16 10:26:49 -08001782 caller()->AddAudioTrack();
1783 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001784 caller()->CreateAndSetAndSignalOffer();
1785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1786 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1787
1788 // Once DTLS has been connected, each side should return the other's SSL
1789 // certificate when calling GetRemoteAudioSSLCertificate.
1790
1791 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1792 ASSERT_TRUE(caller_remote_cert);
1793 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1794 caller_remote_cert->ToPEMString());
1795
1796 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1797 ASSERT_TRUE(callee_remote_cert);
1798 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1799 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001800
1801 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1802 ASSERT_TRUE(caller_remote_cert_chain);
1803 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1804 auto remote_cert = &caller_remote_cert_chain->Get(0);
1805 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1806 remote_cert->ToPEMString());
1807
1808 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1809 ASSERT_TRUE(callee_remote_cert_chain);
1810 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1811 remote_cert = &callee_remote_cert_chain->Get(0);
1812 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1813 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001814}
1815
deadbeef1dcb1642017-03-29 21:08:16 -07001816// This test sets up a call between two parties with a source resolution of
1817// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001818TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001819 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1820 ASSERT_TRUE(CreatePeerConnectionWrappers());
1821 ConnectFakeSignaling();
1822
Niels Möller5c7efe72018-05-11 10:34:46 +02001823 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1824 webrtc::FakePeriodicVideoSource::Config config;
1825 config.width = 1280;
1826 config.height = 720;
1827 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1828 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001829
1830 // Do normal offer/answer and wait for at least one frame to be received in
1831 // each direction.
1832 caller()->CreateAndSetAndSignalOffer();
1833 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1834 callee()->min_video_frames_received_per_track() > 0,
1835 kMaxWaitForFramesMs);
1836
1837 // Check rendered aspect ratio.
1838 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1839 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1840 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1841 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1842}
1843
1844// This test sets up an one-way call, with media only from caller to
1845// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001846TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001847 ASSERT_TRUE(CreatePeerConnectionWrappers());
1848 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001849 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001850 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001851 MediaExpectations media_expectations;
1852 media_expectations.CalleeExpectsSomeAudioAndVideo();
1853 media_expectations.CallerExpectsNoAudio();
1854 media_expectations.CallerExpectsNoVideo();
1855 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001856}
1857
1858// This test sets up a audio call initially, with the callee rejecting video
1859// initially. Then later the callee decides to upgrade to audio/video, and
1860// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001861TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001862 ASSERT_TRUE(CreatePeerConnectionWrappers());
1863 ConnectFakeSignaling();
1864 // Initially, offer an audio/video stream from the caller, but refuse to
1865 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001866 caller()->AddAudioVideoTracks();
1867 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001868 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1869 PeerConnectionInterface::RTCOfferAnswerOptions options;
1870 options.offer_to_receive_video = 0;
1871 callee()->SetOfferAnswerOptions(options);
1872 } else {
1873 callee()->SetRemoteOfferHandler([this] {
1874 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1875 });
1876 }
deadbeef1dcb1642017-03-29 21:08:16 -07001877 // Do offer/answer and make sure audio is still received end-to-end.
1878 caller()->CreateAndSetAndSignalOffer();
1879 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001880 {
1881 MediaExpectations media_expectations;
1882 media_expectations.ExpectBidirectionalAudio();
1883 media_expectations.ExpectNoVideo();
1884 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1885 }
deadbeef1dcb1642017-03-29 21:08:16 -07001886 // Sanity check that the callee's description has a rejected video section.
1887 ASSERT_NE(nullptr, callee()->pc()->local_description());
1888 const ContentInfo* callee_video_content =
1889 GetFirstVideoContent(callee()->pc()->local_description()->description());
1890 ASSERT_NE(nullptr, callee_video_content);
1891 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001892
deadbeef1dcb1642017-03-29 21:08:16 -07001893 // Now negotiate with video and ensure negotiation succeeds, with video
1894 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001895 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001896 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1897 PeerConnectionInterface::RTCOfferAnswerOptions options;
1898 options.offer_to_receive_video = 1;
1899 callee()->SetOfferAnswerOptions(options);
1900 } else {
1901 callee()->SetRemoteOfferHandler(nullptr);
1902 caller()->SetRemoteOfferHandler([this] {
1903 // The caller creates a new transceiver to receive video on when receiving
1904 // the offer, but by default it is send only.
1905 auto transceivers = caller()->pc()->GetTransceivers();
1906 ASSERT_EQ(3, transceivers.size());
1907 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1908 transceivers[2]->receiver()->media_type());
1909 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1910 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1911 });
1912 }
deadbeef1dcb1642017-03-29 21:08:16 -07001913 callee()->CreateAndSetAndSignalOffer();
1914 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001915 {
1916 // Expect additional audio frames to be received after the upgrade.
1917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1920 }
deadbeef1dcb1642017-03-29 21:08:16 -07001921}
1922
deadbeef4389b4d2017-09-07 09:07:36 -07001923// Simpler than the above test; just add an audio track to an established
1924// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001925TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001926 ASSERT_TRUE(CreatePeerConnectionWrappers());
1927 ConnectFakeSignaling();
1928 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001929 caller()->AddVideoTrack();
1930 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001931 caller()->CreateAndSetAndSignalOffer();
1932 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1933 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001934 caller()->AddAudioTrack();
1935 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001936 caller()->CreateAndSetAndSignalOffer();
1937 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1938 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001939 MediaExpectations media_expectations;
1940 media_expectations.ExpectBidirectionalAudioAndVideo();
1941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001942}
1943
deadbeef1dcb1642017-03-29 21:08:16 -07001944// This test sets up a call that's transferred to a new caller with a different
1945// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001946TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001947 ASSERT_TRUE(CreatePeerConnectionWrappers());
1948 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001949 caller()->AddAudioVideoTracks();
1950 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001951 caller()->CreateAndSetAndSignalOffer();
1952 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1953
1954 // Keep the original peer around which will still send packets to the
1955 // receiving client. These SRTP packets will be dropped.
1956 std::unique_ptr<PeerConnectionWrapper> original_peer(
1957 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001958 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001959 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1960 // directly above.
1961 original_peer->pc()->Close();
1962
1963 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001964 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001965 caller()->CreateAndSetAndSignalOffer();
1966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1967 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968 MediaExpectations media_expectations;
1969 media_expectations.ExpectBidirectionalAudioAndVideo();
1970 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001971}
1972
1973// This test sets up a call that's transferred to a new callee with a different
1974// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001975TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001976 ASSERT_TRUE(CreatePeerConnectionWrappers());
1977 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001978 caller()->AddAudioVideoTracks();
1979 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001980 caller()->CreateAndSetAndSignalOffer();
1981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1982
1983 // Keep the original peer around which will still send packets to the
1984 // receiving client. These SRTP packets will be dropped.
1985 std::unique_ptr<PeerConnectionWrapper> original_peer(
1986 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001987 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001988 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1989 // directly above.
1990 original_peer->pc()->Close();
1991
1992 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001993 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001994 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1995 caller()->CreateAndSetAndSignalOffer();
1996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1997 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998 MediaExpectations media_expectations;
1999 media_expectations.ExpectBidirectionalAudioAndVideo();
2000 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002001}
2002
2003// This test sets up a non-bundled call and negotiates bundling at the same
2004// time as starting an ICE restart. When bundling is in effect in the restart,
2005// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002006TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002007 ASSERT_TRUE(CreatePeerConnectionWrappers());
2008 ConnectFakeSignaling();
2009
Steve Anton15324772018-01-16 10:26:49 -08002010 caller()->AddAudioVideoTracks();
2011 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002012 // Remove the bundle group from the SDP received by the callee.
2013 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2014 desc->RemoveGroupByName("BUNDLE");
2015 });
2016 caller()->CreateAndSetAndSignalOffer();
2017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002018 {
2019 MediaExpectations media_expectations;
2020 media_expectations.ExpectBidirectionalAudioAndVideo();
2021 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2022 }
deadbeef1dcb1642017-03-29 21:08:16 -07002023 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2024 callee()->SetReceivedSdpMunger(nullptr);
2025 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2026 caller()->CreateAndSetAndSignalOffer();
2027 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2028
2029 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002030 {
2031 MediaExpectations media_expectations;
2032 media_expectations.ExpectBidirectionalAudioAndVideo();
2033 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2034 }
deadbeef1dcb1642017-03-29 21:08:16 -07002035}
2036
2037// Test CVO (Coordination of Video Orientation). If a video source is rotated
2038// and both peers support the CVO RTP header extension, the actual video frames
2039// don't need to be encoded in different resolutions, since the rotation is
2040// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002042 ASSERT_TRUE(CreatePeerConnectionWrappers());
2043 ConnectFakeSignaling();
2044 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002045 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002046 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002047 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002048 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2049
2050 // Wait for video frames to be received by both sides.
2051 caller()->CreateAndSetAndSignalOffer();
2052 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2053 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2054 callee()->min_video_frames_received_per_track() > 0,
2055 kMaxWaitForFramesMs);
2056
2057 // Ensure that the aspect ratio is unmodified.
2058 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2059 // not just assumed.
2060 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2061 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2062 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2063 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2064 // Ensure that the CVO bits were surfaced to the renderer.
2065 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2066 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2067}
2068
2069// Test that when the CVO extension isn't supported, video is rotated the
2070// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002071TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002072 ASSERT_TRUE(CreatePeerConnectionWrappers());
2073 ConnectFakeSignaling();
2074 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002075 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002076 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002077 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002078 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2079
2080 // Remove the CVO extension from the offered SDP.
2081 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2082 cricket::VideoContentDescription* video =
2083 GetFirstVideoContentDescription(desc);
2084 video->ClearRtpHeaderExtensions();
2085 });
2086 // Wait for video frames to be received by both sides.
2087 caller()->CreateAndSetAndSignalOffer();
2088 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2089 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2090 callee()->min_video_frames_received_per_track() > 0,
2091 kMaxWaitForFramesMs);
2092
2093 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2094 // rotation.
2095 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2096 // not just assumed.
2097 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2098 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2099 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2100 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2101 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2102 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2103 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2104}
2105
deadbeef1dcb1642017-03-29 21:08:16 -07002106// Test that if the answerer rejects the audio m= section, no audio is sent or
2107// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002108TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002109 ASSERT_TRUE(CreatePeerConnectionWrappers());
2110 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002111 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002112 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2113 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2114 // it will reject the audio m= section completely.
2115 PeerConnectionInterface::RTCOfferAnswerOptions options;
2116 options.offer_to_receive_audio = 0;
2117 callee()->SetOfferAnswerOptions(options);
2118 } else {
2119 // Stopping the audio RtpTransceiver will cause the media section to be
2120 // rejected in the answer.
2121 callee()->SetRemoteOfferHandler([this] {
2122 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2123 });
2124 }
Steve Anton15324772018-01-16 10:26:49 -08002125 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002126 // Do offer/answer and wait for successful end-to-end video frames.
2127 caller()->CreateAndSetAndSignalOffer();
2128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002129 MediaExpectations media_expectations;
2130 media_expectations.ExpectBidirectionalVideo();
2131 media_expectations.ExpectNoAudio();
2132 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2133
deadbeef1dcb1642017-03-29 21:08:16 -07002134 // Sanity check that the callee's description has a rejected audio section.
2135 ASSERT_NE(nullptr, callee()->pc()->local_description());
2136 const ContentInfo* callee_audio_content =
2137 GetFirstAudioContent(callee()->pc()->local_description()->description());
2138 ASSERT_NE(nullptr, callee_audio_content);
2139 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002140 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2141 // The caller's transceiver should have stopped after receiving the answer.
2142 EXPECT_TRUE(caller()
2143 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2144 ->stopped());
2145 }
deadbeef1dcb1642017-03-29 21:08:16 -07002146}
2147
2148// Test that if the answerer rejects the video m= section, no video is sent or
2149// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002150TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002151 ASSERT_TRUE(CreatePeerConnectionWrappers());
2152 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002153 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002154 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2155 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2156 // it will reject the video m= section completely.
2157 PeerConnectionInterface::RTCOfferAnswerOptions options;
2158 options.offer_to_receive_video = 0;
2159 callee()->SetOfferAnswerOptions(options);
2160 } else {
2161 // Stopping the video RtpTransceiver will cause the media section to be
2162 // rejected in the answer.
2163 callee()->SetRemoteOfferHandler([this] {
2164 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2165 });
2166 }
Steve Anton15324772018-01-16 10:26:49 -08002167 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002168 // Do offer/answer and wait for successful end-to-end audio frames.
2169 caller()->CreateAndSetAndSignalOffer();
2170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002171 MediaExpectations media_expectations;
2172 media_expectations.ExpectBidirectionalAudio();
2173 media_expectations.ExpectNoVideo();
2174 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2175
deadbeef1dcb1642017-03-29 21:08:16 -07002176 // Sanity check that the callee's description has a rejected video section.
2177 ASSERT_NE(nullptr, callee()->pc()->local_description());
2178 const ContentInfo* callee_video_content =
2179 GetFirstVideoContent(callee()->pc()->local_description()->description());
2180 ASSERT_NE(nullptr, callee_video_content);
2181 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002182 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2183 // The caller's transceiver should have stopped after receiving the answer.
2184 EXPECT_TRUE(caller()
2185 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2186 ->stopped());
2187 }
deadbeef1dcb1642017-03-29 21:08:16 -07002188}
2189
2190// Test that if the answerer rejects both audio and video m= sections, nothing
2191// bad happens.
2192// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2193// test anything but the fact that negotiation succeeds, which doesn't mean
2194// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002195TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002196 ASSERT_TRUE(CreatePeerConnectionWrappers());
2197 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002198 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002199 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2200 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2201 // will reject both audio and video m= sections.
2202 PeerConnectionInterface::RTCOfferAnswerOptions options;
2203 options.offer_to_receive_audio = 0;
2204 options.offer_to_receive_video = 0;
2205 callee()->SetOfferAnswerOptions(options);
2206 } else {
2207 callee()->SetRemoteOfferHandler([this] {
2208 // Stopping all transceivers will cause all media sections to be rejected.
2209 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2210 transceiver->Stop();
2211 }
2212 });
2213 }
deadbeef1dcb1642017-03-29 21:08:16 -07002214 // Do offer/answer and wait for stable signaling state.
2215 caller()->CreateAndSetAndSignalOffer();
2216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002217
deadbeef1dcb1642017-03-29 21:08:16 -07002218 // Sanity check that the callee's description has rejected m= sections.
2219 ASSERT_NE(nullptr, callee()->pc()->local_description());
2220 const ContentInfo* callee_audio_content =
2221 GetFirstAudioContent(callee()->pc()->local_description()->description());
2222 ASSERT_NE(nullptr, callee_audio_content);
2223 EXPECT_TRUE(callee_audio_content->rejected);
2224 const ContentInfo* callee_video_content =
2225 GetFirstVideoContent(callee()->pc()->local_description()->description());
2226 ASSERT_NE(nullptr, callee_video_content);
2227 EXPECT_TRUE(callee_video_content->rejected);
2228}
2229
2230// This test sets up an audio and video call between two parties. After the
2231// call runs for a while, the caller sends an updated offer with video being
2232// rejected. Once the re-negotiation is done, the video flow should stop and
2233// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002234TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002235 ASSERT_TRUE(CreatePeerConnectionWrappers());
2236 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002237 caller()->AddAudioVideoTracks();
2238 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002239 caller()->CreateAndSetAndSignalOffer();
2240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002241 {
2242 MediaExpectations media_expectations;
2243 media_expectations.ExpectBidirectionalAudioAndVideo();
2244 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2245 }
deadbeef1dcb1642017-03-29 21:08:16 -07002246 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002247 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2248 caller()->SetGeneratedSdpMunger(
2249 [](cricket::SessionDescription* description) {
2250 for (cricket::ContentInfo& content : description->contents()) {
2251 if (cricket::IsVideoContent(&content)) {
2252 content.rejected = true;
2253 }
2254 }
2255 });
2256 } else {
2257 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2258 }
deadbeef1dcb1642017-03-29 21:08:16 -07002259 caller()->CreateAndSetAndSignalOffer();
2260 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2261
2262 // Sanity check that the caller's description has a rejected video section.
2263 ASSERT_NE(nullptr, caller()->pc()->local_description());
2264 const ContentInfo* caller_video_content =
2265 GetFirstVideoContent(caller()->pc()->local_description()->description());
2266 ASSERT_NE(nullptr, caller_video_content);
2267 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002268 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002269 {
2270 MediaExpectations media_expectations;
2271 media_expectations.ExpectBidirectionalAudio();
2272 media_expectations.ExpectNoVideo();
2273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2274 }
deadbeef1dcb1642017-03-29 21:08:16 -07002275}
2276
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002277// Do one offer/answer with audio, another that disables it (rejecting the m=
2278// section), and another that re-enables it. Regression test for:
2279// bugs.webrtc.org/6023
2280TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2281 ASSERT_TRUE(CreatePeerConnectionWrappers());
2282 ConnectFakeSignaling();
2283
2284 // Add audio track, do normal offer/answer.
2285 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2286 caller()->CreateLocalAudioTrack();
2287 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2288 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2289 caller()->CreateAndSetAndSignalOffer();
2290 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2291
2292 // Remove audio track, and set offer_to_receive_audio to false to cause the
2293 // m= section to be completely disabled, not just "recvonly".
2294 caller()->pc()->RemoveTrack(sender);
2295 PeerConnectionInterface::RTCOfferAnswerOptions options;
2296 options.offer_to_receive_audio = 0;
2297 caller()->SetOfferAnswerOptions(options);
2298 caller()->CreateAndSetAndSignalOffer();
2299 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2300
2301 // Add the audio track again, expecting negotiation to succeed and frames to
2302 // flow.
2303 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2304 options.offer_to_receive_audio = 1;
2305 caller()->SetOfferAnswerOptions(options);
2306 caller()->CreateAndSetAndSignalOffer();
2307 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2308
2309 MediaExpectations media_expectations;
2310 media_expectations.CalleeExpectsSomeAudio();
2311 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2312}
2313
deadbeef1dcb1642017-03-29 21:08:16 -07002314// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2315// is needed to support legacy endpoints.
2316// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2317// add a test for an end-to-end test without MID signaling either (basically,
2318// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002319TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002320 ASSERT_TRUE(CreatePeerConnectionWrappers());
2321 ConnectFakeSignaling();
2322 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002323 caller()->AddAudioVideoTracks();
2324 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002325 // Remove SSRCs and MSIDs from the received offer SDP.
2326 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002327 caller()->CreateAndSetAndSignalOffer();
2328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002329 MediaExpectations media_expectations;
2330 media_expectations.ExpectBidirectionalAudioAndVideo();
2331 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002332}
2333
Seth Hampson5897a6e2018-04-03 11:16:33 -07002334// Basic end-to-end test, without SSRC signaling. This means that the track
2335// was created properly and frames are delivered when the MSIDs are communicated
2336// with a=msid lines and no a=ssrc lines.
2337TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2338 EndToEndCallWithoutSsrcSignaling) {
2339 const char kStreamId[] = "streamId";
2340 ASSERT_TRUE(CreatePeerConnectionWrappers());
2341 ConnectFakeSignaling();
2342 // Add just audio tracks.
2343 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2344 callee()->AddAudioTrack();
2345
2346 // Remove SSRCs from the received offer SDP.
2347 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2348 caller()->CreateAndSetAndSignalOffer();
2349 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2350 MediaExpectations media_expectations;
2351 media_expectations.ExpectBidirectionalAudio();
2352 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2353}
2354
Steve Antondf527fd2018-04-27 15:52:03 -07002355// Tests that video flows between multiple video tracks when SSRCs are not
2356// signaled. This exercises the MID RTP header extension which is needed to
2357// demux the incoming video tracks.
2358TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2359 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2360 ASSERT_TRUE(CreatePeerConnectionWrappers());
2361 ConnectFakeSignaling();
2362 caller()->AddVideoTrack();
2363 caller()->AddVideoTrack();
2364 callee()->AddVideoTrack();
2365 callee()->AddVideoTrack();
2366
2367 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2368 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2369 caller()->CreateAndSetAndSignalOffer();
2370 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2371 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2372 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2373
2374 // Expect video to be received in both directions on both tracks.
2375 MediaExpectations media_expectations;
2376 media_expectations.ExpectBidirectionalVideo();
2377 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2378}
2379
deadbeef1dcb1642017-03-29 21:08:16 -07002380// Test that if two video tracks are sent (from caller to callee, in this test),
2381// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002382TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002383 ASSERT_TRUE(CreatePeerConnectionWrappers());
2384 ConnectFakeSignaling();
2385 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002386 caller()->AddAudioVideoTracks();
2387 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002388 caller()->CreateAndSetAndSignalOffer();
2389 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002390 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002391
2392 MediaExpectations media_expectations;
2393 media_expectations.CalleeExpectsSomeAudioAndVideo();
2394 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002395}
2396
2397static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2398 bool first = true;
2399 for (cricket::ContentInfo& content : desc->contents()) {
2400 if (first) {
2401 first = false;
2402 continue;
2403 }
2404 content.bundle_only = true;
2405 }
2406 first = true;
2407 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2408 if (first) {
2409 first = false;
2410 continue;
2411 }
2412 transport.description.ice_ufrag.clear();
2413 transport.description.ice_pwd.clear();
2414 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2415 transport.description.identity_fingerprint.reset(nullptr);
2416 }
2417}
2418
2419// Test that if applying a true "max bundle" offer, which uses ports of 0,
2420// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2421// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2422// successfully and media flows.
2423// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2424// TODO(deadbeef): Won't need this test once we start generating actual
2425// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002426TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002427 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2428 ASSERT_TRUE(CreatePeerConnectionWrappers());
2429 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002430 caller()->AddAudioVideoTracks();
2431 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002432 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2433 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2434 // but the first m= section.
2435 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2436 caller()->CreateAndSetAndSignalOffer();
2437 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002438 MediaExpectations media_expectations;
2439 media_expectations.ExpectBidirectionalAudioAndVideo();
2440 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002441}
2442
2443// Test that we can receive the audio output level from a remote audio track.
2444// TODO(deadbeef): Use a fake audio source and verify that the output level is
2445// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002446TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002447 ASSERT_TRUE(CreatePeerConnectionWrappers());
2448 ConnectFakeSignaling();
2449 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002450 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002451 caller()->CreateAndSetAndSignalOffer();
2452 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2453
2454 // Get the audio output level stats. Note that the level is not available
2455 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002456 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002457 kMaxWaitForFramesMs);
2458}
2459
2460// Test that an audio input level is reported.
2461// TODO(deadbeef): Use a fake audio source and verify that the input level is
2462// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002463TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002464 ASSERT_TRUE(CreatePeerConnectionWrappers());
2465 ConnectFakeSignaling();
2466 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002467 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002468 caller()->CreateAndSetAndSignalOffer();
2469 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2470
2471 // Get the audio input level stats. The level should be available very
2472 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002473 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002474 kMaxWaitForStatsMs);
2475}
2476
2477// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002478TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002479 ASSERT_TRUE(CreatePeerConnectionWrappers());
2480 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002481 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002482 // Do offer/answer, wait for the callee to receive some frames.
2483 caller()->CreateAndSetAndSignalOffer();
2484 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002485
2486 MediaExpectations media_expectations;
2487 media_expectations.CalleeExpectsSomeAudioAndVideo();
2488 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002489
2490 // Get a handle to the remote tracks created, so they can be used as GetStats
2491 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002492 for (auto receiver : callee()->pc()->GetReceivers()) {
2493 // We received frames, so we definitely should have nonzero "received bytes"
2494 // stats at this point.
2495 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2496 0);
2497 }
deadbeef1dcb1642017-03-29 21:08:16 -07002498}
2499
2500// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002501TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002502 ASSERT_TRUE(CreatePeerConnectionWrappers());
2503 ConnectFakeSignaling();
2504 auto audio_track = caller()->CreateLocalAudioTrack();
2505 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002506 caller()->AddTrack(audio_track);
2507 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002508 // Do offer/answer, wait for the callee to receive some frames.
2509 caller()->CreateAndSetAndSignalOffer();
2510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002511 MediaExpectations media_expectations;
2512 media_expectations.CalleeExpectsSomeAudioAndVideo();
2513 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002514
2515 // The callee received frames, so we definitely should have nonzero "sent
2516 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002517 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2518 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2519}
2520
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002521// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002522TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002523 ASSERT_TRUE(CreatePeerConnectionWrappers());
2524 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002525 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002526
Steve Anton15324772018-01-16 10:26:49 -08002527 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002528
2529 // Do offer/answer, wait for the callee to receive some frames.
2530 caller()->CreateAndSetAndSignalOffer();
2531 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2532
2533 // Get the remote audio track created on the receiver, so they can be used as
2534 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002535 auto receivers = callee()->pc()->GetReceivers();
2536 ASSERT_EQ(1u, receivers.size());
2537 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002538
2539 // Get the audio output level stats. Note that the level is not available
2540 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002541 EXPECT_TRUE_WAIT(
2542 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2543 0,
2544 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002545}
2546
deadbeefd8ad7882017-04-18 16:01:17 -07002547// Test that we can get stats (using the new stats implemnetation) for
2548// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2549// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002550TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002551 GetStatsForUnsignaledStreamWithNewStatsApi) {
2552 ASSERT_TRUE(CreatePeerConnectionWrappers());
2553 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002554 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002555 // Remove SSRCs and MSIDs from the received offer SDP.
2556 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2557 caller()->CreateAndSetAndSignalOffer();
2558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559 MediaExpectations media_expectations;
2560 media_expectations.CalleeExpectsSomeAudio(1);
2561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002562
2563 // We received a frame, so we should have nonzero "bytes received" stats for
2564 // the unsignaled stream, if stats are working for it.
2565 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2566 callee()->NewGetStats();
2567 ASSERT_NE(nullptr, report);
2568 auto inbound_stream_stats =
2569 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2570 ASSERT_EQ(1U, inbound_stream_stats.size());
2571 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2572 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002573 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2574}
2575
Taylor Brandstettera4653442018-06-19 09:44:26 -07002576// Same as above but for the legacy stats implementation.
2577TEST_P(PeerConnectionIntegrationTest,
2578 GetStatsForUnsignaledStreamWithOldStatsApi) {
2579 ASSERT_TRUE(CreatePeerConnectionWrappers());
2580 ConnectFakeSignaling();
2581 caller()->AddAudioTrack();
2582 // Remove SSRCs and MSIDs from the received offer SDP.
2583 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2584 caller()->CreateAndSetAndSignalOffer();
2585 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2586
2587 // Note that, since the old stats implementation associates SSRCs with tracks
2588 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2589 // associated track ID. So we can't use the track "selector" argument.
2590 //
2591 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2592 // return cached stats if not enough time has passed since the last update.
2593 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0U,
2594 kDefaultTimeout);
2595}
2596
zhihuangf8164932017-05-19 13:09:47 -07002597// Test that we can successfully get the media related stats (audio level
2598// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002599TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002600 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2601 ASSERT_TRUE(CreatePeerConnectionWrappers());
2602 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002603 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002604 // Remove SSRCs and MSIDs from the received offer SDP.
2605 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2606 caller()->CreateAndSetAndSignalOffer();
2607 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608 MediaExpectations media_expectations;
2609 media_expectations.CalleeExpectsSomeAudio(1);
2610 media_expectations.CalleeExpectsSomeVideo(1);
2611 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002612
2613 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2614 callee()->NewGetStats();
2615 ASSERT_NE(nullptr, report);
2616
2617 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2618 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2619 ASSERT_GE(audio_index, 0);
2620 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002621}
2622
deadbeef4e2deab2017-09-20 13:56:21 -07002623// Helper for test below.
2624void ModifySsrcs(cricket::SessionDescription* desc) {
2625 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002626 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002627 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002628 for (uint32_t& ssrc : stream.ssrcs) {
2629 ssrc = rtc::CreateRandomId();
2630 }
2631 }
2632 }
2633}
2634
2635// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2636// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2637// This should result in two "RTCInboundRTPStreamStats", but only one
2638// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2639// being reset to 0 once the SSRC change occurs.
2640//
2641// Regression test for this bug:
2642// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2643//
2644// The bug causes the track stats to only represent one of the two streams:
2645// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2646// that the track stat counters would reset to 0 when the new stream is
2647// received, and a 50% chance that they'll stop updating (while
2648// "concealed_samples" continues increasing, due to silence being generated for
2649// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002650TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002651 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002652 ASSERT_TRUE(CreatePeerConnectionWrappers());
2653 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002654 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002655 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2656 // that doesn't signal SSRCs (from the callee's perspective).
2657 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2658 caller()->CreateAndSetAndSignalOffer();
2659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2660 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002661 {
2662 MediaExpectations media_expectations;
2663 media_expectations.CalleeExpectsSomeAudio(50);
2664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2665 }
deadbeef4e2deab2017-09-20 13:56:21 -07002666 // Some audio frames were received, so we should have nonzero "samples
2667 // received" for the track.
2668 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2669 callee()->NewGetStats();
2670 ASSERT_NE(nullptr, report);
2671 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2672 ASSERT_EQ(1U, track_stats.size());
2673 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2674 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2675 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2676
2677 // Create a new offer and munge it to cause the caller to use a new SSRC.
2678 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2679 caller()->CreateAndSetAndSignalOffer();
2680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2681 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2682 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002683 {
2684 MediaExpectations media_expectations;
2685 media_expectations.CalleeExpectsSomeAudio(25);
2686 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2687 }
deadbeef4e2deab2017-09-20 13:56:21 -07002688
2689 report = callee()->NewGetStats();
2690 ASSERT_NE(nullptr, report);
2691 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2692 ASSERT_EQ(1U, track_stats.size());
2693 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2694 // The "total samples received" stat should only be greater than it was
2695 // before.
2696 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2697 // Right now, the new SSRC will cause the counters to reset to 0.
2698 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2699
2700 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002701 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002702 // good sign that we're seeing stats from the old stream that's no longer
2703 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002704 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002705 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2706 EXPECT_LT(*track_stats[0]->concealed_samples,
2707 *track_stats[0]->total_samples_received *
2708 kAcceptableConcealedSamplesPercentage);
2709
2710 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2711 // sanity check that the SSRC really changed.
2712 // TODO(deadbeef): This isn't working right now, because we're not returning
2713 // *any* stats for the inactive stream. Uncomment when the bug is completely
2714 // fixed.
2715 // auto inbound_stream_stats =
2716 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2717 // ASSERT_EQ(2U, inbound_stream_stats.size());
2718}
2719
deadbeef1dcb1642017-03-29 21:08:16 -07002720// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002721TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002722 PeerConnectionFactory::Options dtls_10_options;
2723 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2724 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2725 dtls_10_options));
2726 ConnectFakeSignaling();
2727 // Do normal offer/answer and wait for some frames to be received in each
2728 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002729 caller()->AddAudioVideoTracks();
2730 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002731 caller()->CreateAndSetAndSignalOffer();
2732 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002733 MediaExpectations media_expectations;
2734 media_expectations.ExpectBidirectionalAudioAndVideo();
2735 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002736}
2737
2738// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002739TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002740 PeerConnectionFactory::Options dtls_10_options;
2741 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2742 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2743 dtls_10_options));
2744 ConnectFakeSignaling();
2745 // Register UMA observer before signaling begins.
2746 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2747 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2748 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002749 caller()->AddAudioVideoTracks();
2750 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002751 caller()->CreateAndSetAndSignalOffer();
2752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2753 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002754 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002755 kDefaultTimeout);
2756 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002757 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002758 EXPECT_EQ(1,
2759 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2760 kDefaultSrtpCryptoSuite));
2761}
2762
2763// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002764TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002765 PeerConnectionFactory::Options dtls_12_options;
2766 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2767 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2768 dtls_12_options));
2769 ConnectFakeSignaling();
2770 // Register UMA observer before signaling begins.
2771 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2772 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2773 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002774 caller()->AddAudioVideoTracks();
2775 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002776 caller()->CreateAndSetAndSignalOffer();
2777 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2778 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002779 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002780 kDefaultTimeout);
2781 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002782 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002783 EXPECT_EQ(1,
2784 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2785 kDefaultSrtpCryptoSuite));
2786}
2787
2788// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2789// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002790TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002791 PeerConnectionFactory::Options caller_options;
2792 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2793 PeerConnectionFactory::Options callee_options;
2794 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2795 ASSERT_TRUE(
2796 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2797 ConnectFakeSignaling();
2798 // Do normal offer/answer and wait for some frames to be received in each
2799 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002800 caller()->AddAudioVideoTracks();
2801 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002802 caller()->CreateAndSetAndSignalOffer();
2803 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002804 MediaExpectations media_expectations;
2805 media_expectations.ExpectBidirectionalAudioAndVideo();
2806 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002807}
2808
2809// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2810// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002811TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002812 PeerConnectionFactory::Options caller_options;
2813 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2814 PeerConnectionFactory::Options callee_options;
2815 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2816 ASSERT_TRUE(
2817 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2818 ConnectFakeSignaling();
2819 // Do normal offer/answer and wait for some frames to be received in each
2820 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002821 caller()->AddAudioVideoTracks();
2822 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002823 caller()->CreateAndSetAndSignalOffer();
2824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002825 MediaExpectations media_expectations;
2826 media_expectations.ExpectBidirectionalAudioAndVideo();
2827 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002828}
2829
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002830// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2831// works as expected; the cipher should only be used if enabled by both sides.
2832TEST_P(PeerConnectionIntegrationTest,
2833 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2834 PeerConnectionFactory::Options caller_options;
2835 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2836 PeerConnectionFactory::Options callee_options;
2837 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2838 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2839 TestNegotiatedCipherSuite(caller_options, callee_options,
2840 expected_cipher_suite);
2841}
2842
2843TEST_P(PeerConnectionIntegrationTest,
2844 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2845 PeerConnectionFactory::Options caller_options;
2846 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2847 PeerConnectionFactory::Options callee_options;
2848 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2849 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2850 TestNegotiatedCipherSuite(caller_options, callee_options,
2851 expected_cipher_suite);
2852}
2853
2854TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2855 PeerConnectionFactory::Options caller_options;
2856 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2857 PeerConnectionFactory::Options callee_options;
2858 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2859 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2860 TestNegotiatedCipherSuite(caller_options, callee_options,
2861 expected_cipher_suite);
2862}
2863
deadbeef1dcb1642017-03-29 21:08:16 -07002864// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002865TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002866 bool local_gcm_enabled = false;
2867 bool remote_gcm_enabled = false;
2868 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2869 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2870 expected_cipher_suite);
2871}
2872
2873// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002874TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002875 bool local_gcm_enabled = true;
2876 bool remote_gcm_enabled = true;
2877 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2878 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2879 expected_cipher_suite);
2880}
2881
2882// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002883TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002884 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2885 bool local_gcm_enabled = true;
2886 bool remote_gcm_enabled = false;
2887 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2888 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2889 expected_cipher_suite);
2890}
2891
2892// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002893TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002894 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2895 bool local_gcm_enabled = false;
2896 bool remote_gcm_enabled = true;
2897 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2898 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2899 expected_cipher_suite);
2900}
2901
deadbeef7914b8c2017-04-21 03:23:33 -07002902// Verify that media can be transmitted end-to-end when GCM crypto suites are
2903// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2904// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2905// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002906TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002907 PeerConnectionFactory::Options gcm_options;
2908 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2909 ASSERT_TRUE(
2910 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2911 ConnectFakeSignaling();
2912 // Do normal offer/answer and wait for some frames to be received in each
2913 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002914 caller()->AddAudioVideoTracks();
2915 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002916 caller()->CreateAndSetAndSignalOffer();
2917 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002918 MediaExpectations media_expectations;
2919 media_expectations.ExpectBidirectionalAudioAndVideo();
2920 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002921}
2922
deadbeef1dcb1642017-03-29 21:08:16 -07002923// This test sets up a call between two parties with audio, video and an RTP
2924// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002925TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002926 FakeConstraints setup_constraints;
2927 setup_constraints.SetAllowRtpDataChannels();
2928 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2929 &setup_constraints));
2930 ConnectFakeSignaling();
2931 // Expect that data channel created on caller side will show up for callee as
2932 // well.
2933 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002934 caller()->AddAudioVideoTracks();
2935 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002936 caller()->CreateAndSetAndSignalOffer();
2937 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2938 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002939 MediaExpectations media_expectations;
2940 media_expectations.ExpectBidirectionalAudioAndVideo();
2941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002942 ASSERT_NE(nullptr, caller()->data_channel());
2943 ASSERT_NE(nullptr, callee()->data_channel());
2944 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2945 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2946
2947 // Ensure data can be sent in both directions.
2948 std::string data = "hello world";
2949 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2950 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2951 kDefaultTimeout);
2952 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2953 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2954 kDefaultTimeout);
2955}
2956
2957// Ensure that an RTP data channel is signaled as closed for the caller when
2958// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002959TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002960 RtpDataChannelSignaledClosedInCalleeOffer) {
2961 // Same procedure as above test.
2962 FakeConstraints setup_constraints;
2963 setup_constraints.SetAllowRtpDataChannels();
2964 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2965 &setup_constraints));
2966 ConnectFakeSignaling();
2967 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002968 caller()->AddAudioVideoTracks();
2969 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002970 caller()->CreateAndSetAndSignalOffer();
2971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2972 ASSERT_NE(nullptr, caller()->data_channel());
2973 ASSERT_NE(nullptr, callee()->data_channel());
2974 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2975 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2976
2977 // Close the data channel on the callee, and do an updated offer/answer.
2978 callee()->data_channel()->Close();
2979 callee()->CreateAndSetAndSignalOffer();
2980 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2981 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2982 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2983}
2984
2985// Tests that data is buffered in an RTP data channel until an observer is
2986// registered for it.
2987//
2988// NOTE: RTP data channels can receive data before the underlying
2989// transport has detected that a channel is writable and thus data can be
2990// received before the data channel state changes to open. That is hard to test
2991// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002992TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002993 DataBufferedUntilRtpDataChannelObserverRegistered) {
2994 // Use fake clock and simulated network delay so that we predictably can wait
2995 // until an SCTP message has been delivered without "sleep()"ing.
2996 rtc::ScopedFakeClock fake_clock;
2997 // Some things use a time of "0" as a special value, so we need to start out
2998 // the fake clock at a nonzero time.
2999 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003000 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003001 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3002 virtual_socket_server()->UpdateDelayDistribution();
3003
3004 FakeConstraints constraints;
3005 constraints.SetAllowRtpDataChannels();
3006 ASSERT_TRUE(
3007 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
3008 ConnectFakeSignaling();
3009 caller()->CreateDataChannel();
3010 caller()->CreateAndSetAndSignalOffer();
3011 ASSERT_TRUE(caller()->data_channel() != nullptr);
3012 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3013 kDefaultTimeout, fake_clock);
3014 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3015 kDefaultTimeout, fake_clock);
3016 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3017 callee()->data_channel()->state(), kDefaultTimeout,
3018 fake_clock);
3019
3020 // Unregister the observer which is normally automatically registered.
3021 callee()->data_channel()->UnregisterObserver();
3022 // Send data and advance fake clock until it should have been received.
3023 std::string data = "hello world";
3024 caller()->data_channel()->Send(DataBuffer(data));
3025 SIMULATED_WAIT(false, 50, fake_clock);
3026
3027 // Attach data channel and expect data to be received immediately. Note that
3028 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3029 // further, but data can be received even if the callback is asynchronous.
3030 MockDataChannelObserver new_observer(callee()->data_channel());
3031 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3032 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003033 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3034 // If this is not done a DCHECK can be hit in ports.cc, because a large
3035 // negative number is calculated for the rtt due to the global clock changing.
3036 caller()->pc()->Close();
3037 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003038}
3039
3040// This test sets up a call between two parties with audio, video and but only
3041// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003042TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07003043 FakeConstraints setup_constraints_1;
3044 setup_constraints_1.SetAllowRtpDataChannels();
3045 // Must disable DTLS to make negotiation succeed.
3046 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3047 false);
3048 FakeConstraints setup_constraints_2;
3049 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3050 false);
3051 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
3052 &setup_constraints_1, &setup_constraints_2));
3053 ConnectFakeSignaling();
3054 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003055 caller()->AddAudioVideoTracks();
3056 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003057 caller()->CreateAndSetAndSignalOffer();
3058 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3059 // The caller should still have a data channel, but it should be closed, and
3060 // one should ever have been created for the callee.
3061 EXPECT_TRUE(caller()->data_channel() != nullptr);
3062 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3063 EXPECT_EQ(nullptr, callee()->data_channel());
3064}
3065
3066// This test sets up a call between two parties with audio, and video. When
3067// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003068TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003069 FakeConstraints setup_constraints;
3070 setup_constraints.SetAllowRtpDataChannels();
3071 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
3072 &setup_constraints));
3073 ConnectFakeSignaling();
3074 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003075 caller()->AddAudioVideoTracks();
3076 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003077 caller()->CreateAndSetAndSignalOffer();
3078 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3079 // Create data channel and do new offer and answer.
3080 caller()->CreateDataChannel();
3081 caller()->CreateAndSetAndSignalOffer();
3082 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3083 ASSERT_NE(nullptr, caller()->data_channel());
3084 ASSERT_NE(nullptr, callee()->data_channel());
3085 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3086 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3087 // Ensure data can be sent in both directions.
3088 std::string data = "hello world";
3089 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3090 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3091 kDefaultTimeout);
3092 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3093 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3094 kDefaultTimeout);
3095}
3096
3097#ifdef HAVE_SCTP
3098
3099// This test sets up a call between two parties with audio, video and an SCTP
3100// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003101TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003102 ASSERT_TRUE(CreatePeerConnectionWrappers());
3103 ConnectFakeSignaling();
3104 // Expect that data channel created on caller side will show up for callee as
3105 // well.
3106 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003107 caller()->AddAudioVideoTracks();
3108 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003109 caller()->CreateAndSetAndSignalOffer();
3110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3111 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003112 MediaExpectations media_expectations;
3113 media_expectations.ExpectBidirectionalAudioAndVideo();
3114 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003115 // Caller data channel should already exist (it created one). Callee data
3116 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3117 ASSERT_NE(nullptr, caller()->data_channel());
3118 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3119 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3120 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3121
3122 // Ensure data can be sent in both directions.
3123 std::string data = "hello world";
3124 caller()->data_channel()->Send(DataBuffer(data));
3125 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3126 kDefaultTimeout);
3127 callee()->data_channel()->Send(DataBuffer(data));
3128 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3129 kDefaultTimeout);
3130}
3131
3132// Ensure that when the callee closes an SCTP data channel, the closing
3133// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003134TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003135 // Same procedure as above test.
3136 ASSERT_TRUE(CreatePeerConnectionWrappers());
3137 ConnectFakeSignaling();
3138 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003139 caller()->AddAudioVideoTracks();
3140 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003141 caller()->CreateAndSetAndSignalOffer();
3142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3143 ASSERT_NE(nullptr, caller()->data_channel());
3144 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3145 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3146 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3147
3148 // Close the data channel on the callee side, and wait for it to reach the
3149 // "closed" state on both sides.
3150 callee()->data_channel()->Close();
3151 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3152 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3153}
3154
Seth Hampson2f0d7022018-02-20 11:54:42 -08003155TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003156 ASSERT_TRUE(CreatePeerConnectionWrappers());
3157 ConnectFakeSignaling();
3158 webrtc::DataChannelInit init;
3159 init.id = 53;
3160 init.maxRetransmits = 52;
3161 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003162 caller()->AddAudioVideoTracks();
3163 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003164 caller()->CreateAndSetAndSignalOffer();
3165 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003166 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3167 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003168 EXPECT_EQ(init.id, callee()->data_channel()->id());
3169 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3170 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3171 EXPECT_FALSE(callee()->data_channel()->negotiated());
3172}
3173
deadbeef1dcb1642017-03-29 21:08:16 -07003174// Test usrsctp's ability to process unordered data stream, where data actually
3175// arrives out of order using simulated delays. Previously there have been some
3176// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003177TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003178 // Introduce random network delays.
3179 // Otherwise it's not a true "unordered" test.
3180 virtual_socket_server()->set_delay_mean(20);
3181 virtual_socket_server()->set_delay_stddev(5);
3182 virtual_socket_server()->UpdateDelayDistribution();
3183 // Normal procedure, but with unordered data channel config.
3184 ASSERT_TRUE(CreatePeerConnectionWrappers());
3185 ConnectFakeSignaling();
3186 webrtc::DataChannelInit init;
3187 init.ordered = false;
3188 caller()->CreateDataChannel(&init);
3189 caller()->CreateAndSetAndSignalOffer();
3190 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3191 ASSERT_NE(nullptr, caller()->data_channel());
3192 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3193 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3194 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3195
3196 static constexpr int kNumMessages = 100;
3197 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3198 static constexpr size_t kMaxMessageSize = 4096;
3199 // Create and send random messages.
3200 std::vector<std::string> sent_messages;
3201 for (int i = 0; i < kNumMessages; ++i) {
3202 size_t length =
3203 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3204 std::string message;
3205 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3206 caller()->data_channel()->Send(DataBuffer(message));
3207 callee()->data_channel()->Send(DataBuffer(message));
3208 sent_messages.push_back(message);
3209 }
3210
3211 // Wait for all messages to be received.
3212 EXPECT_EQ_WAIT(kNumMessages,
3213 caller()->data_observer()->received_message_count(),
3214 kDefaultTimeout);
3215 EXPECT_EQ_WAIT(kNumMessages,
3216 callee()->data_observer()->received_message_count(),
3217 kDefaultTimeout);
3218
3219 // Sort and compare to make sure none of the messages were corrupted.
3220 std::vector<std::string> caller_received_messages =
3221 caller()->data_observer()->messages();
3222 std::vector<std::string> callee_received_messages =
3223 callee()->data_observer()->messages();
3224 std::sort(sent_messages.begin(), sent_messages.end());
3225 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3226 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3227 EXPECT_EQ(sent_messages, caller_received_messages);
3228 EXPECT_EQ(sent_messages, callee_received_messages);
3229}
3230
3231// This test sets up a call between two parties with audio, and video. When
3232// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003233TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003234 ASSERT_TRUE(CreatePeerConnectionWrappers());
3235 ConnectFakeSignaling();
3236 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003237 caller()->AddAudioVideoTracks();
3238 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003239 caller()->CreateAndSetAndSignalOffer();
3240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3241 // Create data channel and do new offer and answer.
3242 caller()->CreateDataChannel();
3243 caller()->CreateAndSetAndSignalOffer();
3244 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3245 // Caller data channel should already exist (it created one). Callee data
3246 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3247 ASSERT_NE(nullptr, caller()->data_channel());
3248 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3249 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3250 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3251 // Ensure data can be sent in both directions.
3252 std::string data = "hello world";
3253 caller()->data_channel()->Send(DataBuffer(data));
3254 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3255 kDefaultTimeout);
3256 callee()->data_channel()->Send(DataBuffer(data));
3257 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3258 kDefaultTimeout);
3259}
3260
deadbeef7914b8c2017-04-21 03:23:33 -07003261// Set up a connection initially just using SCTP data channels, later upgrading
3262// to audio/video, ensuring frames are received end-to-end. Effectively the
3263// inverse of the test above.
3264// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003265TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003266 ASSERT_TRUE(CreatePeerConnectionWrappers());
3267 ConnectFakeSignaling();
3268 // Do initial offer/answer with just data channel.
3269 caller()->CreateDataChannel();
3270 caller()->CreateAndSetAndSignalOffer();
3271 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3272 // Wait until data can be sent over the data channel.
3273 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3274 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3275 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3276
3277 // Do subsequent offer/answer with two-way audio and video. Audio and video
3278 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003279 caller()->AddAudioVideoTracks();
3280 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003281 caller()->CreateAndSetAndSignalOffer();
3282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003283 MediaExpectations media_expectations;
3284 media_expectations.ExpectBidirectionalAudioAndVideo();
3285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003286}
3287
deadbeef8b7e9ad2017-05-25 09:38:55 -07003288static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003289 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003290 GetFirstDataContentDescription(desc);
3291 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003292 dcd_offer->set_use_sctpmap(false);
3293 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3294}
3295
3296// Test that the data channel works when a spec-compliant SCTP m= section is
3297// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3298// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003299TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003300 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3301 ASSERT_TRUE(CreatePeerConnectionWrappers());
3302 ConnectFakeSignaling();
3303 caller()->CreateDataChannel();
3304 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3305 caller()->CreateAndSetAndSignalOffer();
3306 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3307 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3308 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3309 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3310
3311 // Ensure data can be sent in both directions.
3312 std::string data = "hello world";
3313 caller()->data_channel()->Send(DataBuffer(data));
3314 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3315 kDefaultTimeout);
3316 callee()->data_channel()->Send(DataBuffer(data));
3317 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3318 kDefaultTimeout);
3319}
3320
deadbeef1dcb1642017-03-29 21:08:16 -07003321#endif // HAVE_SCTP
3322
3323// Test that the ICE connection and gathering states eventually reach
3324// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003325TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003326 ASSERT_TRUE(CreatePeerConnectionWrappers());
3327 ConnectFakeSignaling();
3328 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003329 caller()->AddAudioVideoTracks();
3330 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003331 caller()->CreateAndSetAndSignalOffer();
3332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3333 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3334 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3335 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3336 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3337 // After the best candidate pair is selected and all candidates are signaled,
3338 // the ICE connection state should reach "complete".
3339 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3340 // answerer/"callee" by default) only reaches "connected". When this is
3341 // fixed, this test should be updated.
3342 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3343 caller()->ice_connection_state(), kDefaultTimeout);
3344 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3345 callee()->ice_connection_state(), kDefaultTimeout);
3346}
3347
Steve Antonede9ca52017-10-16 13:04:27 -07003348// Test that firewalling the ICE connection causes the clients to identify the
3349// disconnected state and then removing the firewall causes them to reconnect.
3350class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003351 : public PeerConnectionIntegrationBaseTest,
3352 public ::testing::WithParamInterface<
3353 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003354 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003355 PeerConnectionIntegrationIceStatesTest()
3356 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3357 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003358 }
3359
3360 void StartStunServer(const SocketAddress& server_address) {
3361 stun_server_.reset(
3362 cricket::TestStunServer::Create(network_thread(), server_address));
3363 }
3364
3365 bool TestIPv6() {
3366 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3367 }
3368
3369 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003370 network_thread()->Invoke<void>(
3371 RTC_FROM_HERE,
3372 rtc::Bind(&cricket::PortAllocator::set_flags,
3373 caller()->port_allocator(), port_allocator_flags_));
3374 network_thread()->Invoke<void>(
3375 RTC_FROM_HERE,
3376 rtc::Bind(&cricket::PortAllocator::set_flags,
3377 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003378 }
3379
3380 std::vector<SocketAddress> CallerAddresses() {
3381 std::vector<SocketAddress> addresses;
3382 addresses.push_back(SocketAddress("1.1.1.1", 0));
3383 if (TestIPv6()) {
3384 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3385 }
3386 return addresses;
3387 }
3388
3389 std::vector<SocketAddress> CalleeAddresses() {
3390 std::vector<SocketAddress> addresses;
3391 addresses.push_back(SocketAddress("2.2.2.2", 0));
3392 if (TestIPv6()) {
3393 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3394 }
3395 return addresses;
3396 }
3397
3398 void SetUpNetworkInterfaces() {
3399 // Remove the default interfaces added by the test infrastructure.
3400 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3401 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3402
3403 // Add network addresses for test.
3404 for (const auto& caller_address : CallerAddresses()) {
3405 caller()->network()->AddInterface(caller_address);
3406 }
3407 for (const auto& callee_address : CalleeAddresses()) {
3408 callee()->network()->AddInterface(callee_address);
3409 }
3410 }
3411
3412 private:
3413 uint32_t port_allocator_flags_;
3414 std::unique_ptr<cricket::TestStunServer> stun_server_;
3415};
3416
3417// Tests that the PeerConnection goes through all the ICE gathering/connection
3418// states over the duration of the call. This includes Disconnected and Failed
3419// states, induced by putting a firewall between the peers and waiting for them
3420// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003421TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3422 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3423 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3424 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003425
3426 const SocketAddress kStunServerAddress =
3427 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3428 StartStunServer(kStunServerAddress);
3429
3430 PeerConnectionInterface::RTCConfiguration config;
3431 PeerConnectionInterface::IceServer ice_stun_server;
3432 ice_stun_server.urls.push_back(
3433 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3434 kStunServerAddress.PortAsString());
3435 config.servers.push_back(ice_stun_server);
3436
3437 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3438 ConnectFakeSignaling();
3439 SetPortAllocatorFlags();
3440 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003441 caller()->AddAudioVideoTracks();
3442 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003443
3444 // Initial state before anything happens.
3445 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3446 caller()->ice_gathering_state());
3447 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3448 caller()->ice_connection_state());
3449
3450 // Start the call by creating the offer, setting it as the local description,
3451 // then sending it to the peer who will respond with an answer. This happens
3452 // asynchronously so that we can watch the states as it runs in the
3453 // background.
3454 caller()->CreateAndSetAndSignalOffer();
3455
Steve Anton83119dd2017-11-10 16:19:52 -08003456 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3457 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003458
3459 // Verify that the observer was notified of the intermediate transitions.
3460 EXPECT_THAT(caller()->ice_connection_state_history(),
3461 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3462 PeerConnectionInterface::kIceConnectionConnected,
3463 PeerConnectionInterface::kIceConnectionCompleted));
3464 EXPECT_THAT(caller()->ice_gathering_state_history(),
3465 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3466 PeerConnectionInterface::kIceGatheringComplete));
3467
3468 // Block connections to/from the caller and wait for ICE to become
3469 // disconnected.
3470 for (const auto& caller_address : CallerAddresses()) {
3471 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3472 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003473 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003474 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3475 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003476
3477 // Let ICE re-establish by removing the firewall rules.
3478 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003479 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003480 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3481 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003482
3483 // According to RFC7675, if there is no response within 30 seconds then the
3484 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003485 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003486 constexpr int kConsentTimeout = 30000;
3487 for (const auto& caller_address : CallerAddresses()) {
3488 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3489 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003490 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003491 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3492 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003493}
3494
3495// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3496// and that the statistics in the metric observers are updated correctly.
3497TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3498 ASSERT_TRUE(CreatePeerConnectionWrappers());
3499 ConnectFakeSignaling();
3500 SetPortAllocatorFlags();
3501 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003502 caller()->AddAudioVideoTracks();
3503 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003504
3505 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3506 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3507 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3508
3509 caller()->CreateAndSetAndSignalOffer();
3510
3511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3512
3513 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3514 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3515 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3516 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3517 if (TestIPv6()) {
3518 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3519 // connection.
3520 EXPECT_EQ(0u, num_best_ipv4);
3521 EXPECT_EQ(1u, num_best_ipv6);
3522 } else {
3523 EXPECT_EQ(1u, num_best_ipv4);
3524 EXPECT_EQ(0u, num_best_ipv6);
3525 }
3526
3527 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3528 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3529 webrtc::kIceCandidatePairHostHost));
3530 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3531 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3532 webrtc::kIceCandidatePairHostPublicHostPublic));
3533}
3534
3535constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3536 cricket::PORTALLOCATOR_DISABLE_STUN |
3537 cricket::PORTALLOCATOR_DISABLE_RELAY;
3538constexpr uint32_t kFlagsIPv6NoStun =
3539 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3540 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3541constexpr uint32_t kFlagsIPv4Stun =
3542 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3543
Seth Hampson2f0d7022018-02-20 11:54:42 -08003544INSTANTIATE_TEST_CASE_P(
3545 PeerConnectionIntegrationTest,
3546 PeerConnectionIntegrationIceStatesTest,
3547 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3548 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3549 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3550 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003551
deadbeef1dcb1642017-03-29 21:08:16 -07003552// This test sets up a call between two parties with audio and video.
3553// During the call, the caller restarts ICE and the test verifies that
3554// new ICE candidates are generated and audio and video still can flow, and the
3555// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003556TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003557 ASSERT_TRUE(CreatePeerConnectionWrappers());
3558 ConnectFakeSignaling();
3559 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003560 caller()->AddAudioVideoTracks();
3561 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003562 caller()->CreateAndSetAndSignalOffer();
3563 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3564 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3565 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3566 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3567 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3568
3569 // To verify that the ICE restart actually occurs, get
3570 // ufrag/password/candidates before and after restart.
3571 // Create an SDP string of the first audio candidate for both clients.
3572 const webrtc::IceCandidateCollection* audio_candidates_caller =
3573 caller()->pc()->local_description()->candidates(0);
3574 const webrtc::IceCandidateCollection* audio_candidates_callee =
3575 callee()->pc()->local_description()->candidates(0);
3576 ASSERT_GT(audio_candidates_caller->count(), 0u);
3577 ASSERT_GT(audio_candidates_callee->count(), 0u);
3578 std::string caller_candidate_pre_restart;
3579 ASSERT_TRUE(
3580 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3581 std::string callee_candidate_pre_restart;
3582 ASSERT_TRUE(
3583 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3584 const cricket::SessionDescription* desc =
3585 caller()->pc()->local_description()->description();
3586 std::string caller_ufrag_pre_restart =
3587 desc->transport_infos()[0].description.ice_ufrag;
3588 desc = callee()->pc()->local_description()->description();
3589 std::string callee_ufrag_pre_restart =
3590 desc->transport_infos()[0].description.ice_ufrag;
3591
3592 // Have the caller initiate an ICE restart.
3593 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3594 caller()->CreateAndSetAndSignalOffer();
3595 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3596 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3597 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3598 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3599 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3600
3601 // Grab the ufrags/candidates again.
3602 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3603 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3604 ASSERT_GT(audio_candidates_caller->count(), 0u);
3605 ASSERT_GT(audio_candidates_callee->count(), 0u);
3606 std::string caller_candidate_post_restart;
3607 ASSERT_TRUE(
3608 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3609 std::string callee_candidate_post_restart;
3610 ASSERT_TRUE(
3611 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3612 desc = caller()->pc()->local_description()->description();
3613 std::string caller_ufrag_post_restart =
3614 desc->transport_infos()[0].description.ice_ufrag;
3615 desc = callee()->pc()->local_description()->description();
3616 std::string callee_ufrag_post_restart =
3617 desc->transport_infos()[0].description.ice_ufrag;
3618 // Sanity check that an ICE restart was actually negotiated in SDP.
3619 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3620 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3621 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3622 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3623
3624 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003625 MediaExpectations media_expectations;
3626 media_expectations.ExpectBidirectionalAudioAndVideo();
3627 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003628}
3629
3630// Verify that audio/video can be received end-to-end when ICE renomination is
3631// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003632TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003633 PeerConnectionInterface::RTCConfiguration config;
3634 config.enable_ice_renomination = true;
3635 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3636 ConnectFakeSignaling();
3637 // Do normal offer/answer and wait for some frames to be received in each
3638 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003639 caller()->AddAudioVideoTracks();
3640 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003641 caller()->CreateAndSetAndSignalOffer();
3642 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3643 // Sanity check that ICE renomination was actually negotiated.
3644 const cricket::SessionDescription* desc =
3645 caller()->pc()->local_description()->description();
3646 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003647 ASSERT_NE(
3648 info.description.transport_options.end(),
3649 std::find(info.description.transport_options.begin(),
3650 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003651 }
3652 desc = callee()->pc()->local_description()->description();
3653 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003654 ASSERT_NE(
3655 info.description.transport_options.end(),
3656 std::find(info.description.transport_options.begin(),
3657 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003658 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003659 MediaExpectations media_expectations;
3660 media_expectations.ExpectBidirectionalAudioAndVideo();
3661 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003662}
3663
Steve Anton6f25b092017-10-23 09:39:20 -07003664// With a max bundle policy and RTCP muxing, adding a new media description to
3665// the connection should not affect ICE at all because the new media will use
3666// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003667TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003668 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003669 PeerConnectionInterface::RTCConfiguration config;
3670 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3671 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3672 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3673 config, PeerConnectionInterface::RTCConfiguration()));
3674 ConnectFakeSignaling();
3675
Steve Anton15324772018-01-16 10:26:49 -08003676 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003677 caller()->CreateAndSetAndSignalOffer();
3678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003679 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3680 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003681
3682 caller()->clear_ice_connection_state_history();
3683
Steve Anton15324772018-01-16 10:26:49 -08003684 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003685 caller()->CreateAndSetAndSignalOffer();
3686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3687
3688 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3689}
3690
deadbeef1dcb1642017-03-29 21:08:16 -07003691// This test sets up a call between two parties with audio and video. It then
3692// renegotiates setting the video m-line to "port 0", then later renegotiates
3693// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003694TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003695 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3696 ASSERT_TRUE(CreatePeerConnectionWrappers());
3697 ConnectFakeSignaling();
3698
3699 // Do initial negotiation, only sending media from the caller. Will result in
3700 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003701 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003702 caller()->CreateAndSetAndSignalOffer();
3703 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3704
3705 // Negotiate again, disabling the video "m=" section (the callee will set the
3706 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003707 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3708 PeerConnectionInterface::RTCOfferAnswerOptions options;
3709 options.offer_to_receive_video = 0;
3710 callee()->SetOfferAnswerOptions(options);
3711 } else {
3712 callee()->SetRemoteOfferHandler([this] {
3713 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3714 });
3715 }
deadbeef1dcb1642017-03-29 21:08:16 -07003716 caller()->CreateAndSetAndSignalOffer();
3717 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3718 // Sanity check that video "m=" section was actually rejected.
3719 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3720 callee()->pc()->local_description()->description());
3721 ASSERT_NE(nullptr, answer_video_content);
3722 ASSERT_TRUE(answer_video_content->rejected);
3723
3724 // Enable video and do negotiation again, making sure video is received
3725 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003726 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3727 PeerConnectionInterface::RTCOfferAnswerOptions options;
3728 options.offer_to_receive_video = 1;
3729 callee()->SetOfferAnswerOptions(options);
3730 } else {
3731 // The caller's transceiver is stopped, so we need to add another track.
3732 auto caller_transceiver =
3733 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3734 EXPECT_TRUE(caller_transceiver->stopped());
3735 caller()->AddVideoTrack();
3736 }
3737 callee()->AddVideoTrack();
3738 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003739 caller()->CreateAndSetAndSignalOffer();
3740 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003741
deadbeef1dcb1642017-03-29 21:08:16 -07003742 // Verify the caller receives frames from the newly added stream, and the
3743 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003744 MediaExpectations media_expectations;
3745 media_expectations.CalleeExpectsSomeAudio();
3746 media_expectations.ExpectBidirectionalVideo();
3747 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003748}
3749
deadbeef1dcb1642017-03-29 21:08:16 -07003750// This tests that if we negotiate after calling CreateSender but before we
3751// have a track, then set a track later, frames from the newly-set track are
3752// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003753TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003754 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3755 ASSERT_TRUE(CreatePeerConnectionWrappers());
3756 ConnectFakeSignaling();
3757 auto caller_audio_sender =
3758 caller()->pc()->CreateSender("audio", "caller_stream");
3759 auto caller_video_sender =
3760 caller()->pc()->CreateSender("video", "caller_stream");
3761 auto callee_audio_sender =
3762 callee()->pc()->CreateSender("audio", "callee_stream");
3763 auto callee_video_sender =
3764 callee()->pc()->CreateSender("video", "callee_stream");
3765 caller()->CreateAndSetAndSignalOffer();
3766 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3767 // Wait for ICE to complete, without any tracks being set.
3768 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3769 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3770 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3771 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3772 // Now set the tracks, and expect frames to immediately start flowing.
3773 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3774 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3775 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3776 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003777 MediaExpectations media_expectations;
3778 media_expectations.ExpectBidirectionalAudioAndVideo();
3779 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3780}
3781
3782// This tests that if we negotiate after calling AddTransceiver but before we
3783// have a track, then set a track later, frames from the newly-set tracks are
3784// received end-to-end.
3785TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3786 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3787 ASSERT_TRUE(CreatePeerConnectionWrappers());
3788 ConnectFakeSignaling();
3789 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3790 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3791 auto caller_audio_sender = audio_result.MoveValue()->sender();
3792 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3793 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3794 auto caller_video_sender = video_result.MoveValue()->sender();
3795 callee()->SetRemoteOfferHandler([this] {
3796 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3797 callee()->pc()->GetTransceivers()[0]->SetDirection(
3798 RtpTransceiverDirection::kSendRecv);
3799 callee()->pc()->GetTransceivers()[1]->SetDirection(
3800 RtpTransceiverDirection::kSendRecv);
3801 });
3802 caller()->CreateAndSetAndSignalOffer();
3803 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3804 // Wait for ICE to complete, without any tracks being set.
3805 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3806 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3807 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3808 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3809 // Now set the tracks, and expect frames to immediately start flowing.
3810 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3811 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3812 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3813 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3814 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3815 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3816 MediaExpectations media_expectations;
3817 media_expectations.ExpectBidirectionalAudioAndVideo();
3818 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003819}
3820
3821// This test verifies that a remote video track can be added via AddStream,
3822// and sent end-to-end. For this particular test, it's simply echoed back
3823// from the caller to the callee, rather than being forwarded to a third
3824// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003825TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003826 ASSERT_TRUE(CreatePeerConnectionWrappers());
3827 ConnectFakeSignaling();
3828 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003829 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003830 caller()->CreateAndSetAndSignalOffer();
3831 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3832 ASSERT_EQ(1, callee()->remote_streams()->count());
3833
3834 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3835 // time).
3836 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3837 callee()->CreateAndSetAndSignalOffer();
3838 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3839
Seth Hampson2f0d7022018-02-20 11:54:42 -08003840 MediaExpectations media_expectations;
3841 media_expectations.ExpectBidirectionalVideo();
3842 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003843}
3844
3845// Test that we achieve the expected end-to-end connection time, using a
3846// fake clock and simulated latency on the media and signaling paths.
3847// We use a TURN<->TURN connection because this is usually the quickest to
3848// set up initially, especially when we're confident the connection will work
3849// and can start sending media before we get a STUN response.
3850//
3851// With various optimizations enabled, here are the network delays we expect to
3852// be on the critical path:
3853// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3854// signaling answer (with DTLS fingerprint).
3855// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3856// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3857// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003858TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003859 rtc::ScopedFakeClock fake_clock;
3860 // Some things use a time of "0" as a special value, so we need to start out
3861 // the fake clock at a nonzero time.
3862 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003863 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003864
3865 static constexpr int media_hop_delay_ms = 50;
3866 static constexpr int signaling_trip_delay_ms = 500;
3867 // For explanation of these values, see comment above.
3868 static constexpr int required_media_hops = 9;
3869 static constexpr int required_signaling_trips = 2;
3870 // For internal delays (such as posting an event asychronously).
3871 static constexpr int allowed_internal_delay_ms = 20;
3872 static constexpr int total_connection_time_ms =
3873 media_hop_delay_ms * required_media_hops +
3874 signaling_trip_delay_ms * required_signaling_trips +
3875 allowed_internal_delay_ms;
3876
3877 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3878 3478};
3879 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3880 0};
3881 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3882 3478};
3883 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3884 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003885 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
3886 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003887
Seth Hampsonaed71642018-06-11 07:41:32 -07003888 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
3889 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07003890 // Bypass permission check on received packets so media can be sent before
3891 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07003892 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
3893 turn_server_1->set_enable_permission_checks(false);
3894 });
3895 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
3896 turn_server_2->set_enable_permission_checks(false);
3897 });
deadbeef1dcb1642017-03-29 21:08:16 -07003898
3899 PeerConnectionInterface::RTCConfiguration client_1_config;
3900 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3901 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3902 ice_server_1.username = "test";
3903 ice_server_1.password = "test";
3904 client_1_config.servers.push_back(ice_server_1);
3905 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3906 client_1_config.presume_writable_when_fully_relayed = true;
3907
3908 PeerConnectionInterface::RTCConfiguration client_2_config;
3909 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3910 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3911 ice_server_2.username = "test";
3912 ice_server_2.password = "test";
3913 client_2_config.servers.push_back(ice_server_2);
3914 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3915 client_2_config.presume_writable_when_fully_relayed = true;
3916
3917 ASSERT_TRUE(
3918 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3919 // Set up the simulated delays.
3920 SetSignalingDelayMs(signaling_trip_delay_ms);
3921 ConnectFakeSignaling();
3922 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3923 virtual_socket_server()->UpdateDelayDistribution();
3924
3925 // Set "offer to receive audio/video" without adding any tracks, so we just
3926 // set up ICE/DTLS with no media.
3927 PeerConnectionInterface::RTCOfferAnswerOptions options;
3928 options.offer_to_receive_audio = 1;
3929 options.offer_to_receive_video = 1;
3930 caller()->SetOfferAnswerOptions(options);
3931 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003932 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3933 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003934 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3935 // If this is not done a DCHECK can be hit in ports.cc, because a large
3936 // negative number is calculated for the rtt due to the global clock changing.
3937 caller()->pc()->Close();
3938 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003939}
3940
Jonas Orelandbdcee282017-10-10 14:01:40 +02003941// Verify that a TurnCustomizer passed in through RTCConfiguration
3942// is actually used by the underlying TURN candidate pair.
3943// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003944TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003945 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3946 3478};
3947 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3948 0};
3949 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3950 3478};
3951 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3952 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003953 CreateTurnServer(turn_server_1_internal_address,
3954 turn_server_1_external_address);
3955 CreateTurnServer(turn_server_2_internal_address,
3956 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003957
3958 PeerConnectionInterface::RTCConfiguration client_1_config;
3959 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3960 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3961 ice_server_1.username = "test";
3962 ice_server_1.password = "test";
3963 client_1_config.servers.push_back(ice_server_1);
3964 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003965 auto* customizer1 = CreateTurnCustomizer();
3966 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003967
3968 PeerConnectionInterface::RTCConfiguration client_2_config;
3969 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3970 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3971 ice_server_2.username = "test";
3972 ice_server_2.password = "test";
3973 client_2_config.servers.push_back(ice_server_2);
3974 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003975 auto* customizer2 = CreateTurnCustomizer();
3976 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003977
3978 ASSERT_TRUE(
3979 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3980 ConnectFakeSignaling();
3981
3982 // Set "offer to receive audio/video" without adding any tracks, so we just
3983 // set up ICE/DTLS with no media.
3984 PeerConnectionInterface::RTCOfferAnswerOptions options;
3985 options.offer_to_receive_audio = 1;
3986 options.offer_to_receive_video = 1;
3987 caller()->SetOfferAnswerOptions(options);
3988 caller()->CreateAndSetAndSignalOffer();
3989 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3990
Seth Hampsonaed71642018-06-11 07:41:32 -07003991 ExpectTurnCustomizerCountersIncremented(customizer1);
3992 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003993}
3994
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07003995// Verifies that you can use TCP instead of UDP to connect to a TURN server and
3996// send media between the caller and the callee.
3997TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
3998 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3999 3478};
4000 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4001
4002 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004003 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4004 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004005
4006 webrtc::PeerConnectionInterface::IceServer ice_server;
4007 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4008 ice_server.username = "test";
4009 ice_server.password = "test";
4010
4011 PeerConnectionInterface::RTCConfiguration client_1_config;
4012 client_1_config.servers.push_back(ice_server);
4013 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4014
4015 PeerConnectionInterface::RTCConfiguration client_2_config;
4016 client_2_config.servers.push_back(ice_server);
4017 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4018
4019 ASSERT_TRUE(
4020 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4021
4022 // Do normal offer/answer and wait for ICE to complete.
4023 ConnectFakeSignaling();
4024 caller()->AddAudioVideoTracks();
4025 callee()->AddAudioVideoTracks();
4026 caller()->CreateAndSetAndSignalOffer();
4027 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4028 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4029 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4030
4031 MediaExpectations media_expectations;
4032 media_expectations.ExpectBidirectionalAudioAndVideo();
4033 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4034}
4035
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004036// Verify that a SSLCertificateVerifier passed in through
4037// PeerConnectionDependencies is actually used by the underlying SSL
4038// implementation to determine whether a certificate presented by the TURN
4039// server is accepted by the client. Note that openssladapter_unittest.cc
4040// contains more detailed, lower-level tests.
4041TEST_P(PeerConnectionIntegrationTest,
4042 SSLCertificateVerifierUsedForTurnConnections) {
4043 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4044 3478};
4045 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4046
4047 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4048 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004049 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4050 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004051
4052 webrtc::PeerConnectionInterface::IceServer ice_server;
4053 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4054 ice_server.username = "test";
4055 ice_server.password = "test";
4056
4057 PeerConnectionInterface::RTCConfiguration client_1_config;
4058 client_1_config.servers.push_back(ice_server);
4059 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4060
4061 PeerConnectionInterface::RTCConfiguration client_2_config;
4062 client_2_config.servers.push_back(ice_server);
4063 // Setting the type to kRelay forces the connection to go through a TURN
4064 // server.
4065 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4066
4067 // Get a copy to the pointer so we can verify calls later.
4068 rtc::TestCertificateVerifier* client_1_cert_verifier =
4069 new rtc::TestCertificateVerifier();
4070 client_1_cert_verifier->verify_certificate_ = true;
4071 rtc::TestCertificateVerifier* client_2_cert_verifier =
4072 new rtc::TestCertificateVerifier();
4073 client_2_cert_verifier->verify_certificate_ = true;
4074
4075 // Create the dependencies with the test certificate verifier.
4076 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4077 client_1_deps.tls_cert_verifier =
4078 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4079 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4080 client_2_deps.tls_cert_verifier =
4081 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4082
4083 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4084 client_1_config, std::move(client_1_deps), client_2_config,
4085 std::move(client_2_deps)));
4086 ConnectFakeSignaling();
4087
4088 // Set "offer to receive audio/video" without adding any tracks, so we just
4089 // set up ICE/DTLS with no media.
4090 PeerConnectionInterface::RTCOfferAnswerOptions options;
4091 options.offer_to_receive_audio = 1;
4092 options.offer_to_receive_video = 1;
4093 caller()->SetOfferAnswerOptions(options);
4094 caller()->CreateAndSetAndSignalOffer();
4095 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4096
4097 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4098 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004099}
4100
4101TEST_P(PeerConnectionIntegrationTest,
4102 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4103 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4104 3478};
4105 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4106
4107 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4108 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004109 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4110 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004111
4112 webrtc::PeerConnectionInterface::IceServer ice_server;
4113 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4114 ice_server.username = "test";
4115 ice_server.password = "test";
4116
4117 PeerConnectionInterface::RTCConfiguration client_1_config;
4118 client_1_config.servers.push_back(ice_server);
4119 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4120
4121 PeerConnectionInterface::RTCConfiguration client_2_config;
4122 client_2_config.servers.push_back(ice_server);
4123 // Setting the type to kRelay forces the connection to go through a TURN
4124 // server.
4125 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4126
4127 // Get a copy to the pointer so we can verify calls later.
4128 rtc::TestCertificateVerifier* client_1_cert_verifier =
4129 new rtc::TestCertificateVerifier();
4130 client_1_cert_verifier->verify_certificate_ = false;
4131 rtc::TestCertificateVerifier* client_2_cert_verifier =
4132 new rtc::TestCertificateVerifier();
4133 client_2_cert_verifier->verify_certificate_ = false;
4134
4135 // Create the dependencies with the test certificate verifier.
4136 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4137 client_1_deps.tls_cert_verifier =
4138 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4139 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4140 client_2_deps.tls_cert_verifier =
4141 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4142
4143 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4144 client_1_config, std::move(client_1_deps), client_2_config,
4145 std::move(client_2_deps)));
4146 ConnectFakeSignaling();
4147
4148 // Set "offer to receive audio/video" without adding any tracks, so we just
4149 // set up ICE/DTLS with no media.
4150 PeerConnectionInterface::RTCOfferAnswerOptions options;
4151 options.offer_to_receive_audio = 1;
4152 options.offer_to_receive_video = 1;
4153 caller()->SetOfferAnswerOptions(options);
4154 caller()->CreateAndSetAndSignalOffer();
4155 bool wait_res = true;
4156 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4157 // properly, should be able to just wait for a state of "failed" instead of
4158 // waiting a fixed 10 seconds.
4159 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4160 ASSERT_FALSE(wait_res);
4161
4162 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4163 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004164}
4165
deadbeefc964d0b2017-04-03 10:03:35 -07004166// Test that audio and video flow end-to-end when codec names don't use the
4167// expected casing, given that they're supposed to be case insensitive. To test
4168// this, all but one codec is removed from each media description, and its
4169// casing is changed.
4170//
4171// In the past, this has regressed and caused crashes/black video, due to the
4172// fact that code at some layers was doing case-insensitive comparisons and
4173// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004174TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004175 ASSERT_TRUE(CreatePeerConnectionWrappers());
4176 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004177 caller()->AddAudioVideoTracks();
4178 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004179
4180 // Remove all but one audio/video codec (opus and VP8), and change the
4181 // casing of the caller's generated offer.
4182 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4183 cricket::AudioContentDescription* audio =
4184 GetFirstAudioContentDescription(description);
4185 ASSERT_NE(nullptr, audio);
4186 auto audio_codecs = audio->codecs();
4187 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4188 [](const cricket::AudioCodec& codec) {
4189 return codec.name != "opus";
4190 }),
4191 audio_codecs.end());
4192 ASSERT_EQ(1u, audio_codecs.size());
4193 audio_codecs[0].name = "OpUs";
4194 audio->set_codecs(audio_codecs);
4195
4196 cricket::VideoContentDescription* video =
4197 GetFirstVideoContentDescription(description);
4198 ASSERT_NE(nullptr, video);
4199 auto video_codecs = video->codecs();
4200 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4201 [](const cricket::VideoCodec& codec) {
4202 return codec.name != "VP8";
4203 }),
4204 video_codecs.end());
4205 ASSERT_EQ(1u, video_codecs.size());
4206 video_codecs[0].name = "vP8";
4207 video->set_codecs(video_codecs);
4208 });
4209
4210 caller()->CreateAndSetAndSignalOffer();
4211 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4212
4213 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004214 MediaExpectations media_expectations;
4215 media_expectations.ExpectBidirectionalAudioAndVideo();
4216 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004217}
4218
Seth Hampson2f0d7022018-02-20 11:54:42 -08004219TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004220 ASSERT_TRUE(CreatePeerConnectionWrappers());
4221 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004222 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004223 caller()->CreateAndSetAndSignalOffer();
4224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004225 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004226 MediaExpectations media_expectations;
4227 media_expectations.CalleeExpectsSomeAudio(1);
4228 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004229 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4230 auto receiver = callee()->pc()->GetReceivers()[0];
4231 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4232
4233 auto contributing_sources = receiver->GetSources();
4234 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4235 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4236 contributing_sources[0].source_id());
4237}
4238
deadbeef2f425aa2017-04-14 10:41:32 -07004239// Test that if a track is removed and added again with a different stream ID,
4240// the new stream ID is successfully communicated in SDP and media continues to
4241// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004242// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4243// it will not reuse a transceiver that has already been sending. After creating
4244// a new transceiver it tries to create an offer with two senders of the same
4245// track ids and it fails.
4246TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004247 ASSERT_TRUE(CreatePeerConnectionWrappers());
4248 ConnectFakeSignaling();
4249
4250 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4251 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4252 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4253 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4254
4255 // Add track using stream 1, do offer/answer.
4256 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4257 caller()->CreateLocalAudioTrack();
4258 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4259 caller()->pc()->AddTrack(track, {stream_1.get()});
4260 caller()->CreateAndSetAndSignalOffer();
4261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004262 {
4263 MediaExpectations media_expectations;
4264 media_expectations.CalleeExpectsSomeAudio(1);
4265 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4266 }
deadbeef2f425aa2017-04-14 10:41:32 -07004267 // Remove the sender, and create a new one with the new stream.
4268 caller()->pc()->RemoveTrack(sender);
4269 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4270 caller()->CreateAndSetAndSignalOffer();
4271 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4272 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004273 {
4274 MediaExpectations media_expectations;
4275 media_expectations.CalleeExpectsSomeAudio();
4276 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4277 }
deadbeef2f425aa2017-04-14 10:41:32 -07004278}
4279
Seth Hampson2f0d7022018-02-20 11:54:42 -08004280TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004281 ASSERT_TRUE(CreatePeerConnectionWrappers());
4282 ConnectFakeSignaling();
4283
4284 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4285 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4286 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4287 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004288 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4289 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004290
Steve Anton15324772018-01-16 10:26:49 -08004291 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004292 caller()->CreateAndSetAndSignalOffer();
4293 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4294}
4295
Steve Antonede9ca52017-10-16 13:04:27 -07004296// Test that if candidates are only signaled by applying full session
4297// descriptions (instead of using AddIceCandidate), the peers can connect to
4298// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004299TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004300 ASSERT_TRUE(CreatePeerConnectionWrappers());
4301 // Each side will signal the session descriptions but not candidates.
4302 ConnectFakeSignalingForSdpOnly();
4303
4304 // Add audio video track and exchange the initial offer/answer with media
4305 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004306 caller()->AddAudioVideoTracks();
4307 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004308 caller()->CreateAndSetAndSignalOffer();
4309
4310 // Wait for all candidates to be gathered on both the caller and callee.
4311 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4312 caller()->ice_gathering_state(), kDefaultTimeout);
4313 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4314 callee()->ice_gathering_state(), kDefaultTimeout);
4315
4316 // The candidates will now be included in the session description, so
4317 // signaling them will start the ICE connection.
4318 caller()->CreateAndSetAndSignalOffer();
4319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4320
4321 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004322 MediaExpectations media_expectations;
4323 media_expectations.ExpectBidirectionalAudioAndVideo();
4324 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004325}
4326
henrika5f6bf242017-11-01 11:06:56 +01004327// Test that SetAudioPlayout can be used to disable audio playout from the
4328// start, then later enable it. This may be useful, for example, if the caller
4329// needs to play a local ringtone until some event occurs, after which it
4330// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004331TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004332 ASSERT_TRUE(CreatePeerConnectionWrappers());
4333 ConnectFakeSignaling();
4334
4335 // Set up audio-only call where audio playout is disabled on caller's side.
4336 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004337 caller()->AddAudioTrack();
4338 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004339 caller()->CreateAndSetAndSignalOffer();
4340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4341
4342 // Pump messages for a second.
4343 WAIT(false, 1000);
4344 // Since audio playout is disabled, the caller shouldn't have received
4345 // anything (at the playout level, at least).
4346 EXPECT_EQ(0, caller()->audio_frames_received());
4347 // As a sanity check, make sure the callee (for which playout isn't disabled)
4348 // did still see frames on its audio level.
4349 ASSERT_GT(callee()->audio_frames_received(), 0);
4350
4351 // Enable playout again, and ensure audio starts flowing.
4352 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004353 MediaExpectations media_expectations;
4354 media_expectations.ExpectBidirectionalAudio();
4355 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004356}
4357
4358double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4359 auto report = pc->NewGetStats();
4360 auto track_stats_list =
4361 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4362 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4363 for (const auto* track_stats : track_stats_list) {
4364 if (track_stats->remote_source.is_defined() &&
4365 *track_stats->remote_source) {
4366 remote_track_stats = track_stats;
4367 break;
4368 }
4369 }
4370
4371 if (!remote_track_stats->total_audio_energy.is_defined()) {
4372 return 0.0;
4373 }
4374 return *remote_track_stats->total_audio_energy;
4375}
4376
4377// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4378// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004379TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004380 DisableAudioPlayoutStillGeneratesAudioStats) {
4381 ASSERT_TRUE(CreatePeerConnectionWrappers());
4382 ConnectFakeSignaling();
4383
4384 // Set up audio-only call where playout is disabled but audio-processing is
4385 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004386 caller()->AddAudioTrack();
4387 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004388 caller()->pc()->SetAudioPlayout(false);
4389
4390 caller()->CreateAndSetAndSignalOffer();
4391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4392
4393 // Wait for the callee to receive audio stats.
4394 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4395}
4396
henrika4f167df2017-11-01 14:45:55 +01004397// Test that SetAudioRecording can be used to disable audio recording from the
4398// start, then later enable it. This may be useful, for example, if the caller
4399// wants to ensure that no audio resources are active before a certain state
4400// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004401TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004402 ASSERT_TRUE(CreatePeerConnectionWrappers());
4403 ConnectFakeSignaling();
4404
4405 // Set up audio-only call where audio recording is disabled on caller's side.
4406 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004407 caller()->AddAudioTrack();
4408 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004409 caller()->CreateAndSetAndSignalOffer();
4410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4411
4412 // Pump messages for a second.
4413 WAIT(false, 1000);
4414 // Since caller has disabled audio recording, the callee shouldn't have
4415 // received anything.
4416 EXPECT_EQ(0, callee()->audio_frames_received());
4417 // As a sanity check, make sure the caller did still see frames on its
4418 // audio level since audio recording is enabled on the calle side.
4419 ASSERT_GT(caller()->audio_frames_received(), 0);
4420
4421 // Enable audio recording again, and ensure audio starts flowing.
4422 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004423 MediaExpectations media_expectations;
4424 media_expectations.ExpectBidirectionalAudio();
4425 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004426}
4427
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004428// Test that after closing PeerConnections, they stop sending any packets (ICE,
4429// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004430TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004431 // Set up audio/video/data, wait for some frames to be received.
4432 ASSERT_TRUE(CreatePeerConnectionWrappers());
4433 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004434 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004435#ifdef HAVE_SCTP
4436 caller()->CreateDataChannel();
4437#endif
4438 caller()->CreateAndSetAndSignalOffer();
4439 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004440 MediaExpectations media_expectations;
4441 media_expectations.CalleeExpectsSomeAudioAndVideo();
4442 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004443 // Close PeerConnections.
4444 caller()->pc()->Close();
4445 callee()->pc()->Close();
4446 // Pump messages for a second, and ensure no new packets end up sent.
4447 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4448 WAIT(false, 1000);
4449 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4450 EXPECT_EQ(sent_packets_a, sent_packets_b);
4451}
4452
Steve Anton7eca0932018-03-30 15:18:41 -07004453// Test that transport stats are generated by the RTCStatsCollector for a
4454// connection that only involves data channels. This is a regression test for
4455// crbug.com/826972.
4456#ifdef HAVE_SCTP
4457TEST_P(PeerConnectionIntegrationTest,
4458 TransportStatsReportedForDataChannelOnlyConnection) {
4459 ASSERT_TRUE(CreatePeerConnectionWrappers());
4460 ConnectFakeSignaling();
4461 caller()->CreateDataChannel();
4462
4463 caller()->CreateAndSetAndSignalOffer();
4464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4465 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4466
4467 auto caller_report = caller()->NewGetStats();
4468 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4469 auto callee_report = callee()->NewGetStats();
4470 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4471}
4472#endif // HAVE_SCTP
4473
Qingsi Wang7685e862018-06-11 20:15:46 -07004474TEST_P(PeerConnectionIntegrationTest,
4475 IceEventsGeneratedAndLoggedInRtcEventLog) {
4476 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4477 ConnectFakeSignaling();
4478 PeerConnectionInterface::RTCOfferAnswerOptions options;
4479 options.offer_to_receive_audio = 1;
4480 caller()->SetOfferAnswerOptions(options);
4481 caller()->CreateAndSetAndSignalOffer();
4482 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4483 ASSERT_NE(nullptr, caller()->event_log_factory());
4484 ASSERT_NE(nullptr, callee()->event_log_factory());
4485 webrtc::FakeRtcEventLog* caller_event_log =
4486 static_cast<webrtc::FakeRtcEventLog*>(
4487 caller()->event_log_factory()->last_log_created());
4488 webrtc::FakeRtcEventLog* callee_event_log =
4489 static_cast<webrtc::FakeRtcEventLog*>(
4490 callee()->event_log_factory()->last_log_created());
4491 ASSERT_NE(nullptr, caller_event_log);
4492 ASSERT_NE(nullptr, callee_event_log);
4493 int caller_ice_config_count = caller_event_log->GetEventCount(
4494 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4495 int caller_ice_event_count = caller_event_log->GetEventCount(
4496 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4497 int callee_ice_config_count = callee_event_log->GetEventCount(
4498 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4499 int callee_ice_event_count = callee_event_log->GetEventCount(
4500 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4501 EXPECT_LT(0, caller_ice_config_count);
4502 EXPECT_LT(0, caller_ice_event_count);
4503 EXPECT_LT(0, callee_ice_config_count);
4504 EXPECT_LT(0, callee_ice_event_count);
4505}
4506
Seth Hampson2f0d7022018-02-20 11:54:42 -08004507INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4508 PeerConnectionIntegrationTest,
4509 Values(SdpSemantics::kPlanB,
4510 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004511
Steve Anton74255ff2018-01-24 18:32:57 -08004512// Tests that verify interoperability between Plan B and Unified Plan
4513// PeerConnections.
4514class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004515 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004516 public ::testing::WithParamInterface<
4517 std::tuple<SdpSemantics, SdpSemantics>> {
4518 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004519 // Setting the SdpSemantics for the base test to kDefault does not matter
4520 // because we specify not to use the test semantics when creating
4521 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004522 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004523 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004524 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004525 callee_semantics_(std::get<1>(GetParam())) {}
4526
4527 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004528 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4529 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004530 }
4531
4532 const SdpSemantics caller_semantics_;
4533 const SdpSemantics callee_semantics_;
4534};
4535
4536TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4537 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4538 ConnectFakeSignaling();
4539
4540 caller()->CreateAndSetAndSignalOffer();
4541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4542}
4543
4544TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4545 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4546 ConnectFakeSignaling();
4547 auto audio_sender = caller()->AddAudioTrack();
4548
4549 caller()->CreateAndSetAndSignalOffer();
4550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4551
4552 // Verify that one audio receiver has been created on the remote and that it
4553 // has the same track ID as the sending track.
4554 auto receivers = callee()->pc()->GetReceivers();
4555 ASSERT_EQ(1u, receivers.size());
4556 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4557 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4558
Seth Hampson2f0d7022018-02-20 11:54:42 -08004559 MediaExpectations media_expectations;
4560 media_expectations.CalleeExpectsSomeAudio();
4561 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004562}
4563
4564TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4565 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4566 ConnectFakeSignaling();
4567 auto video_sender = caller()->AddVideoTrack();
4568 auto audio_sender = caller()->AddAudioTrack();
4569
4570 caller()->CreateAndSetAndSignalOffer();
4571 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4572
4573 // Verify that one audio and one video receiver have been created on the
4574 // remote and that they have the same track IDs as the sending tracks.
4575 auto audio_receivers =
4576 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4577 ASSERT_EQ(1u, audio_receivers.size());
4578 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4579 auto video_receivers =
4580 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4581 ASSERT_EQ(1u, video_receivers.size());
4582 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4583
Seth Hampson2f0d7022018-02-20 11:54:42 -08004584 MediaExpectations media_expectations;
4585 media_expectations.CalleeExpectsSomeAudioAndVideo();
4586 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004587}
4588
4589TEST_P(PeerConnectionIntegrationInteropTest,
4590 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4591 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4592 ConnectFakeSignaling();
4593 caller()->AddAudioVideoTracks();
4594 callee()->AddAudioVideoTracks();
4595
4596 caller()->CreateAndSetAndSignalOffer();
4597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4598
Seth Hampson2f0d7022018-02-20 11:54:42 -08004599 MediaExpectations media_expectations;
4600 media_expectations.ExpectBidirectionalAudioAndVideo();
4601 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004602}
4603
4604TEST_P(PeerConnectionIntegrationInteropTest,
4605 ReverseRolesOneAudioLocalToOneVideoRemote) {
4606 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4607 ConnectFakeSignaling();
4608 caller()->AddAudioTrack();
4609 callee()->AddVideoTrack();
4610
4611 caller()->CreateAndSetAndSignalOffer();
4612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4613
4614 // Verify that only the audio track has been negotiated.
4615 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4616 // Might also check that the callee's NegotiationNeeded flag is set.
4617
4618 // Reverse roles.
4619 callee()->CreateAndSetAndSignalOffer();
4620 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4621
Seth Hampson2f0d7022018-02-20 11:54:42 -08004622 MediaExpectations media_expectations;
4623 media_expectations.CallerExpectsSomeVideo();
4624 media_expectations.CalleeExpectsSomeAudio();
4625 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004626}
4627
Steve Antonba42e992018-04-09 14:10:01 -07004628INSTANTIATE_TEST_CASE_P(
4629 PeerConnectionIntegrationTest,
4630 PeerConnectionIntegrationInteropTest,
4631 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4632 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4633
4634// Test that if the Unified Plan side offers two video tracks then the Plan B
4635// side will only see the first one and ignore the second.
4636TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004637 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4638 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004639 ConnectFakeSignaling();
4640 auto first_sender = caller()->AddVideoTrack();
4641 caller()->AddVideoTrack();
4642
4643 caller()->CreateAndSetAndSignalOffer();
4644 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4645
4646 // Verify that there is only one receiver and it corresponds to the first
4647 // added track.
4648 auto receivers = callee()->pc()->GetReceivers();
4649 ASSERT_EQ(1u, receivers.size());
4650 EXPECT_TRUE(receivers[0]->track()->enabled());
4651 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4652
Seth Hampson2f0d7022018-02-20 11:54:42 -08004653 MediaExpectations media_expectations;
4654 media_expectations.CalleeExpectsSomeVideo();
4655 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004656}
4657
deadbeef1dcb1642017-03-29 21:08:16 -07004658} // namespace
4659
4660#endif // if !defined(THREAD_SANITIZER)