blob: ec7f5c1b949c29f8e04bdfb4ea0bf6d75314b80f [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/builtin_video_decoder_factory.h"
34#include "api/video_codecs/builtin_video_encoder_factory.h"
35#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "media/engine/fakewebrtcvideoengine.h"
37#include "p2p/base/p2pconstants.h"
38#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070039#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020040#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "p2p/base/testturnserver.h"
42#include "p2p/client/basicportallocator.h"
43#include "pc/dtmfsender.h"
44#include "pc/localaudiosource.h"
45#include "pc/mediasession.h"
46#include "pc/peerconnection.h"
47#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080048#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080049#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020051#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "pc/test/fakertccertificategenerator.h"
53#include "pc/test/fakevideotrackrenderer.h"
54#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070056#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/gunit.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070058#include "rtc_base/testcertificateverifier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020060#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070061
62using cricket::ContentInfo;
63using cricket::FakeWebRtcVideoDecoder;
64using cricket::FakeWebRtcVideoDecoderFactory;
65using cricket::FakeWebRtcVideoEncoder;
66using cricket::FakeWebRtcVideoEncoderFactory;
67using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070068using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070069using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080070using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070071using ::testing::ElementsAre;
72using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070073using webrtc::DataBuffer;
74using webrtc::DataChannelInterface;
75using webrtc::DtmfSender;
76using webrtc::DtmfSenderInterface;
77using webrtc::DtmfSenderObserverInterface;
78using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080079using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070080using webrtc::MediaConstraintsInterface;
81using webrtc::MediaStreamInterface;
82using webrtc::MediaStreamTrackInterface;
83using webrtc::MockCreateSessionDescriptionObserver;
84using webrtc::MockDataChannelObserver;
85using webrtc::MockSetSessionDescriptionObserver;
86using webrtc::MockStatsObserver;
87using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070088using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070089using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080090using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070091using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070092using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080093using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070094using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -080095using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010096using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080097using webrtc::RtpSenderInterface;
98using webrtc::RtpTransceiverDirection;
99using webrtc::RtpTransceiverInit;
100using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800101using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800102using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700103using webrtc::SessionDescriptionInterface;
104using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800105using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700106
107namespace {
108
109static const int kDefaultTimeout = 10000;
110static const int kMaxWaitForStatsMs = 3000;
111static const int kMaxWaitForActivationMs = 5000;
112static const int kMaxWaitForFramesMs = 10000;
113// Default number of audio/video frames to wait for before considering a test
114// successful.
115static const int kDefaultExpectedAudioFrameCount = 3;
116static const int kDefaultExpectedVideoFrameCount = 3;
117
deadbeef1dcb1642017-03-29 21:08:16 -0700118static const char kDataChannelLabel[] = "data_channel";
119
120// SRTP cipher name negotiated by the tests. This must be updated if the
121// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700122static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700123static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
124
Steve Antonede9ca52017-10-16 13:04:27 -0700125static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
126
deadbeef1dcb1642017-03-29 21:08:16 -0700127// Helper function for constructing offer/answer options to initiate an ICE
128// restart.
129PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
130 PeerConnectionInterface::RTCOfferAnswerOptions options;
131 options.ice_restart = true;
132 return options;
133}
134
deadbeefd8ad7882017-04-18 16:01:17 -0700135// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
136// attribute from received SDP, simulating a legacy endpoint.
137void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
138 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800139 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700140 }
141 desc->set_msid_supported(false);
142}
143
Seth Hampson5897a6e2018-04-03 11:16:33 -0700144// Removes all stream information besides the stream ids, simulating an
145// endpoint that only signals a=msid lines to convey stream_ids.
146void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
147 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700148 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700149 std::vector<std::string> stream_ids;
150 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700151 const StreamParams& first_stream =
152 content.media_description()->streams()[0];
153 track_id = first_stream.id;
154 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700155 }
156 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700157 StreamParams new_stream;
158 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700159 new_stream.set_stream_ids(stream_ids);
160 content.media_description()->AddStream(new_stream);
161 }
162}
163
zhihuangf8164932017-05-19 13:09:47 -0700164int FindFirstMediaStatsIndexByKind(
165 const std::string& kind,
166 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
167 media_stats_vec) {
168 for (size_t i = 0; i < media_stats_vec.size(); i++) {
169 if (media_stats_vec[i]->kind.ValueToString() == kind) {
170 return i;
171 }
172 }
173 return -1;
174}
175
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200176int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) {
177 int signature = 0;
178 for (const auto it : events) {
179 signature |= static_cast<int>(it);
180 }
181 return signature;
182}
183
deadbeef1dcb1642017-03-29 21:08:16 -0700184class SignalingMessageReceiver {
185 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800186 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700187 virtual void ReceiveIceMessage(const std::string& sdp_mid,
188 int sdp_mline_index,
189 const std::string& msg) = 0;
190
191 protected:
192 SignalingMessageReceiver() {}
193 virtual ~SignalingMessageReceiver() {}
194};
195
196class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
197 public:
198 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
199 : expected_media_type_(media_type) {}
200
201 void OnFirstPacketReceived(cricket::MediaType media_type) override {
202 ASSERT_EQ(expected_media_type_, media_type);
203 first_packet_received_ = true;
204 }
205
206 bool first_packet_received() const { return first_packet_received_; }
207
208 virtual ~MockRtpReceiverObserver() {}
209
210 private:
211 bool first_packet_received_ = false;
212 cricket::MediaType expected_media_type_;
213};
214
215// Helper class that wraps a peer connection, observes it, and can accept
216// signaling messages from another wrapper.
217//
218// Uses a fake network, fake A/V capture, and optionally fake
219// encoders/decoders, though they aren't used by default since they don't
220// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700221// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800222// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700223class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800224 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700225 public:
226 // Different factory methods for convenience.
227 // TODO(deadbeef): Could use the pattern of:
228 //
229 // PeerConnectionWrapper =
230 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
231 //
232 // To reduce some code duplication.
233 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
234 const std::string& debug_name,
235 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
236 rtc::Thread* network_thread,
237 rtc::Thread* worker_thread) {
238 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700239 webrtc::PeerConnectionDependencies dependencies(nullptr);
240 dependencies.cert_generator = std::move(cert_generator);
241 if (!client->Init(nullptr, nullptr, nullptr, std::move(dependencies),
deadbeef1dcb1642017-03-29 21:08:16 -0700242 network_thread, worker_thread)) {
243 delete client;
244 return nullptr;
245 }
246 return client;
247 }
248
deadbeef2f425aa2017-04-14 10:41:32 -0700249 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
250 return peer_connection_factory_.get();
251 }
252
deadbeef1dcb1642017-03-29 21:08:16 -0700253 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
254
255 // If a signaling message receiver is set (via ConnectFakeSignaling), this
256 // will set the whole offer/answer exchange in motion. Just need to wait for
257 // the signaling state to reach "stable".
258 void CreateAndSetAndSignalOffer() {
259 auto offer = CreateOffer();
260 ASSERT_NE(nullptr, offer);
261 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
262 }
263
264 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
265 // when a remote offer is received (via fake signaling) and an answer is
266 // generated. By default, uses default options.
267 void SetOfferAnswerOptions(
268 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
269 offer_answer_options_ = options;
270 }
271
272 // Set a callback to be invoked when SDP is received via the fake signaling
273 // channel, which provides an opportunity to munge (modify) the SDP. This is
274 // used to test SDP being applied that a PeerConnection would normally not
275 // generate, but a non-JSEP endpoint might.
276 void SetReceivedSdpMunger(
277 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100278 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700279 }
280
deadbeefc964d0b2017-04-03 10:03:35 -0700281 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700282 // generated.
283 void SetGeneratedSdpMunger(
284 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100285 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700286 }
287
Seth Hampson2f0d7022018-02-20 11:54:42 -0800288 // Set a callback to be invoked when a remote offer is received via the fake
289 // signaling channel. This provides an opportunity to change the
290 // PeerConnection state before an answer is created and sent to the caller.
291 void SetRemoteOfferHandler(std::function<void()> handler) {
292 remote_offer_handler_ = std::move(handler);
293 }
294
Steve Antonede9ca52017-10-16 13:04:27 -0700295 // Every ICE connection state in order that has been seen by the observer.
296 std::vector<PeerConnectionInterface::IceConnectionState>
297 ice_connection_state_history() const {
298 return ice_connection_state_history_;
299 }
Steve Anton6f25b092017-10-23 09:39:20 -0700300 void clear_ice_connection_state_history() {
301 ice_connection_state_history_.clear();
302 }
Steve Antonede9ca52017-10-16 13:04:27 -0700303
304 // Every ICE gathering state in order that has been seen by the observer.
305 std::vector<PeerConnectionInterface::IceGatheringState>
306 ice_gathering_state_history() const {
307 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700308 }
309
Steve Anton15324772018-01-16 10:26:49 -0800310 void AddAudioVideoTracks() {
311 AddAudioTrack();
312 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700313 }
314
Steve Anton74255ff2018-01-24 18:32:57 -0800315 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
316 return AddTrack(CreateLocalAudioTrack());
317 }
deadbeef1dcb1642017-03-29 21:08:16 -0700318
Steve Anton74255ff2018-01-24 18:32:57 -0800319 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
320 return AddTrack(CreateLocalVideoTrack());
321 }
deadbeef1dcb1642017-03-29 21:08:16 -0700322
323 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
324 FakeConstraints constraints;
325 // Disable highpass filter so that we can get all the test audio frames.
326 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
327 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
328 peer_connection_factory_->CreateAudioSource(&constraints);
329 // TODO(perkj): Test audio source when it is implemented. Currently audio
330 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700331 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700332 source);
333 }
334
335 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200336 return CreateLocalVideoTrackInternal(
337 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200341 CreateLocalVideoTrackWithConfig(
342 webrtc::FakePeriodicVideoSource::Config config) {
343 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700344 }
345
346 rtc::scoped_refptr<webrtc::VideoTrackInterface>
347 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200348 webrtc::FakePeriodicVideoSource::Config config;
349 config.rotation = rotation;
350 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700351 }
352
Steve Anton74255ff2018-01-24 18:32:57 -0800353 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
354 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800355 const std::vector<std::string>& stream_ids = {}) {
356 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800357 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800358 return result.MoveValue();
359 }
360
361 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
362 cricket::MediaType media_type) {
363 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
364 for (auto receiver : pc()->GetReceivers()) {
365 if (receiver->media_type() == media_type) {
366 receivers.push_back(receiver);
367 }
368 }
369 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700370 }
371
Seth Hampson2f0d7022018-02-20 11:54:42 -0800372 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
373 cricket::MediaType media_type) {
374 for (auto transceiver : pc()->GetTransceivers()) {
375 if (transceiver->receiver()->media_type() == media_type) {
376 return transceiver;
377 }
378 }
379 return nullptr;
380 }
381
deadbeef1dcb1642017-03-29 21:08:16 -0700382 bool SignalingStateStable() {
383 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
384 }
385
386 void CreateDataChannel() { CreateDataChannel(nullptr); }
387
388 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700389 CreateDataChannel(kDataChannelLabel, init);
390 }
391
392 void CreateDataChannel(const std::string& label,
393 const webrtc::DataChannelInit* init) {
394 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700395 ASSERT_TRUE(data_channel_.get() != nullptr);
396 data_observer_.reset(new MockDataChannelObserver(data_channel_));
397 }
398
399 DataChannelInterface* data_channel() { return data_channel_; }
400 const MockDataChannelObserver* data_observer() const {
401 return data_observer_.get();
402 }
403
404 int audio_frames_received() const {
405 return fake_audio_capture_module_->frames_received();
406 }
407
408 // Takes minimum of video frames received for each track.
409 //
410 // Can be used like:
411 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
412 //
413 // To ensure that all video tracks received at least a certain number of
414 // frames.
415 int min_video_frames_received_per_track() const {
416 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200417 if (fake_video_renderers_.empty()) {
418 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700419 }
deadbeef1dcb1642017-03-29 21:08:16 -0700420
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200421 for (const auto& pair : fake_video_renderers_) {
422 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700423 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200424 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700425 }
426
427 // Returns a MockStatsObserver in a state after stats gathering finished,
428 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700429 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700430 webrtc::MediaStreamTrackInterface* track) {
431 rtc::scoped_refptr<MockStatsObserver> observer(
432 new rtc::RefCountedObject<MockStatsObserver>());
433 EXPECT_TRUE(peer_connection_->GetStats(
434 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
435 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
436 return observer;
437 }
438
439 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700440 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
441 return OldGetStatsForTrack(nullptr);
442 }
443
444 // Synchronously gets stats and returns them. If it times out, fails the test
445 // and returns null.
446 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
447 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
448 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
449 peer_connection_->GetStats(callback);
450 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
451 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700452 }
453
454 int rendered_width() {
455 EXPECT_FALSE(fake_video_renderers_.empty());
456 return fake_video_renderers_.empty()
457 ? 0
458 : fake_video_renderers_.begin()->second->width();
459 }
460
461 int rendered_height() {
462 EXPECT_FALSE(fake_video_renderers_.empty());
463 return fake_video_renderers_.empty()
464 ? 0
465 : fake_video_renderers_.begin()->second->height();
466 }
467
468 double rendered_aspect_ratio() {
469 if (rendered_height() == 0) {
470 return 0.0;
471 }
472 return static_cast<double>(rendered_width()) / rendered_height();
473 }
474
475 webrtc::VideoRotation rendered_rotation() {
476 EXPECT_FALSE(fake_video_renderers_.empty());
477 return fake_video_renderers_.empty()
478 ? webrtc::kVideoRotation_0
479 : fake_video_renderers_.begin()->second->rotation();
480 }
481
482 int local_rendered_width() {
483 return local_video_renderer_ ? local_video_renderer_->width() : 0;
484 }
485
486 int local_rendered_height() {
487 return local_video_renderer_ ? local_video_renderer_->height() : 0;
488 }
489
490 double local_rendered_aspect_ratio() {
491 if (local_rendered_height() == 0) {
492 return 0.0;
493 }
494 return static_cast<double>(local_rendered_width()) /
495 local_rendered_height();
496 }
497
498 size_t number_of_remote_streams() {
499 if (!pc()) {
500 return 0;
501 }
502 return pc()->remote_streams()->count();
503 }
504
505 StreamCollectionInterface* remote_streams() const {
506 if (!pc()) {
507 ADD_FAILURE();
508 return nullptr;
509 }
510 return pc()->remote_streams();
511 }
512
513 StreamCollectionInterface* local_streams() {
514 if (!pc()) {
515 ADD_FAILURE();
516 return nullptr;
517 }
518 return pc()->local_streams();
519 }
520
521 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
522 return pc()->signaling_state();
523 }
524
525 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
526 return pc()->ice_connection_state();
527 }
528
529 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
530 return pc()->ice_gathering_state();
531 }
532
533 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
534 // GetReceivers. They're updated automatically when a remote offer/answer
535 // from the fake signaling channel is applied, or when
536 // ResetRtpReceiverObservers below is called.
537 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
538 rtp_receiver_observers() {
539 return rtp_receiver_observers_;
540 }
541
542 void ResetRtpReceiverObservers() {
543 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100544 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
545 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700546 std::unique_ptr<MockRtpReceiverObserver> observer(
547 new MockRtpReceiverObserver(receiver->media_type()));
548 receiver->SetObserver(observer.get());
549 rtp_receiver_observers_.push_back(std::move(observer));
550 }
551 }
552
Steve Antonede9ca52017-10-16 13:04:27 -0700553 rtc::FakeNetworkManager* network() const {
554 return fake_network_manager_.get();
555 }
556 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
557
deadbeef1dcb1642017-03-29 21:08:16 -0700558 private:
559 explicit PeerConnectionWrapper(const std::string& debug_name)
560 : debug_name_(debug_name) {}
561
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700562 bool Init(const MediaConstraintsInterface* constraints,
563 const PeerConnectionFactory::Options* options,
564 const PeerConnectionInterface::RTCConfiguration* config,
565 webrtc::PeerConnectionDependencies dependencies,
566 rtc::Thread* network_thread,
567 rtc::Thread* worker_thread) {
deadbeef1dcb1642017-03-29 21:08:16 -0700568 // There's an error in this test code if Init ends up being called twice.
569 RTC_DCHECK(!peer_connection_);
570 RTC_DCHECK(!peer_connection_factory_);
571
572 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700573 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700574
575 std::unique_ptr<cricket::PortAllocator> port_allocator(
576 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700577 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700578 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
579 if (!fake_audio_capture_module_) {
580 return false;
581 }
deadbeef1dcb1642017-03-29 21:08:16 -0700582 rtc::Thread* const signaling_thread = rtc::Thread::Current();
583 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
584 network_thread, worker_thread, signaling_thread,
Anders Carlsson67537952018-05-03 11:28:29 +0200585 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
586 fake_audio_capture_module_),
587 webrtc::CreateBuiltinAudioEncoderFactory(),
588 webrtc::CreateBuiltinAudioDecoderFactory(),
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200589 webrtc::CreateBuiltinVideoEncoderFactory(),
590 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
591 nullptr /* audio_processing */);
deadbeef1dcb1642017-03-29 21:08:16 -0700592 if (!peer_connection_factory_) {
593 return false;
594 }
595 if (options) {
596 peer_connection_factory_->SetOptions(*options);
597 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800598 if (config) {
599 sdp_semantics_ = config->sdp_semantics;
600 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700601
602 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700603 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700604 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700605 return peer_connection_.get() != nullptr;
606 }
607
608 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700609 const MediaConstraintsInterface* constraints,
610 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700611 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700612 PeerConnectionInterface::RTCConfiguration modified_config;
613 // If |config| is null, this will result in a default configuration being
614 // used.
615 if (config) {
616 modified_config = *config;
617 }
618 // Disable resolution adaptation; we don't want it interfering with the
619 // test results.
620 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
621 // ratios and not specific resolutions, is this even necessary?
622 modified_config.set_cpu_adaptation(false);
623
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700624 // Use the legacy interface.
625 if (constraints != nullptr) {
626 return peer_connection_factory_->CreatePeerConnection(
627 modified_config, constraints, std::move(dependencies.allocator),
628 std::move(dependencies.cert_generator), this);
629 }
630 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700631 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700632 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700633 }
634
635 void set_signaling_message_receiver(
636 SignalingMessageReceiver* signaling_message_receiver) {
637 signaling_message_receiver_ = signaling_message_receiver;
638 }
639
640 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
641
Steve Antonede9ca52017-10-16 13:04:27 -0700642 void set_signal_ice_candidates(bool signal) {
643 signal_ice_candidates_ = signal;
644 }
645
deadbeef1dcb1642017-03-29 21:08:16 -0700646 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200647 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700648 // Set max frame rate to 10fps to reduce the risk of test flakiness.
649 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200650 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700651
Niels Möller5c7efe72018-05-11 10:34:46 +0200652 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200653 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
654 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700655 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200656 peer_connection_factory_->CreateVideoTrack(
657 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700658 if (!local_video_renderer_) {
659 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
660 }
661 return track;
662 }
663
664 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100665 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800666 std::unique_ptr<SessionDescriptionInterface> desc =
667 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700668 if (received_sdp_munger_) {
669 received_sdp_munger_(desc->description());
670 }
671
672 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
673 // Setting a remote description may have changed the number of receivers,
674 // so reset the receiver observers.
675 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800676 if (remote_offer_handler_) {
677 remote_offer_handler_();
678 }
deadbeef1dcb1642017-03-29 21:08:16 -0700679 auto answer = CreateAnswer();
680 ASSERT_NE(nullptr, answer);
681 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
682 }
683
684 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100685 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800686 std::unique_ptr<SessionDescriptionInterface> desc =
687 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700688 if (received_sdp_munger_) {
689 received_sdp_munger_(desc->description());
690 }
691
692 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
693 // Set the RtpReceiverObserver after receivers are created.
694 ResetRtpReceiverObservers();
695 }
696
697 // Returns null on failure.
698 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
699 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
700 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
701 pc()->CreateOffer(observer, offer_answer_options_);
702 return WaitForDescriptionFromObserver(observer);
703 }
704
705 // Returns null on failure.
706 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
707 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
708 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
709 pc()->CreateAnswer(observer, offer_answer_options_);
710 return WaitForDescriptionFromObserver(observer);
711 }
712
713 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100714 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700715 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
716 if (!observer->result()) {
717 return nullptr;
718 }
719 auto description = observer->MoveDescription();
720 if (generated_sdp_munger_) {
721 generated_sdp_munger_(description->description());
722 }
723 return description;
724 }
725
726 // Setting the local description and sending the SDP message over the fake
727 // signaling channel are combined into the same method because the SDP
728 // message needs to be sent as soon as SetLocalDescription finishes, without
729 // waiting for the observer to be called. This ensures that ICE candidates
730 // don't outrace the description.
731 bool SetLocalDescriptionAndSendSdpMessage(
732 std::unique_ptr<SessionDescriptionInterface> desc) {
733 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
734 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100735 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800736 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700737 std::string sdp;
738 EXPECT_TRUE(desc->ToString(&sdp));
739 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800740 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
741 RemoveUnusedVideoRenderers();
742 }
deadbeef1dcb1642017-03-29 21:08:16 -0700743 // As mentioned above, we need to send the message immediately after
744 // SetLocalDescription.
745 SendSdpMessage(type, sdp);
746 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
747 return true;
748 }
749
750 bool SetRemoteDescription(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_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700754 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800755 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
756 RemoveUnusedVideoRenderers();
757 }
deadbeef1dcb1642017-03-29 21:08:16 -0700758 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
759 return observer->result();
760 }
761
Seth Hampson2f0d7022018-02-20 11:54:42 -0800762 // This is a work around to remove unused fake_video_renderers from
763 // transceivers that have either stopped or are no longer receiving.
764 void RemoveUnusedVideoRenderers() {
765 auto transceivers = pc()->GetTransceivers();
766 for (auto& transceiver : transceivers) {
767 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
768 continue;
769 }
770 // Remove fake video renderers from any stopped transceivers.
771 if (transceiver->stopped()) {
772 auto it =
773 fake_video_renderers_.find(transceiver->receiver()->track()->id());
774 if (it != fake_video_renderers_.end()) {
775 fake_video_renderers_.erase(it);
776 }
777 }
778 // Remove fake video renderers from any transceivers that are no longer
779 // receiving.
780 if ((transceiver->current_direction() &&
781 !webrtc::RtpTransceiverDirectionHasRecv(
782 *transceiver->current_direction()))) {
783 auto it =
784 fake_video_renderers_.find(transceiver->receiver()->track()->id());
785 if (it != fake_video_renderers_.end()) {
786 fake_video_renderers_.erase(it);
787 }
788 }
789 }
790 }
791
deadbeef1dcb1642017-03-29 21:08:16 -0700792 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
793 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800794 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700795 if (signaling_delay_ms_ == 0) {
796 RelaySdpMessageIfReceiverExists(type, msg);
797 } else {
798 invoker_.AsyncInvokeDelayed<void>(
799 RTC_FROM_HERE, rtc::Thread::Current(),
800 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
801 this, type, msg),
802 signaling_delay_ms_);
803 }
804 }
805
Steve Antona3a92c22017-12-07 10:27:41 -0800806 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700807 if (signaling_message_receiver_) {
808 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
809 }
810 }
811
812 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
813 // default).
814 void SendIceMessage(const std::string& sdp_mid,
815 int sdp_mline_index,
816 const std::string& msg) {
817 if (signaling_delay_ms_ == 0) {
818 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
819 } else {
820 invoker_.AsyncInvokeDelayed<void>(
821 RTC_FROM_HERE, rtc::Thread::Current(),
822 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
823 this, sdp_mid, sdp_mline_index, msg),
824 signaling_delay_ms_);
825 }
826 }
827
828 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
829 int sdp_mline_index,
830 const std::string& msg) {
831 if (signaling_message_receiver_) {
832 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
833 msg);
834 }
835 }
836
837 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800838 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
839 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700840 HandleIncomingOffer(msg);
841 } else {
842 HandleIncomingAnswer(msg);
843 }
844 }
845
846 void ReceiveIceMessage(const std::string& sdp_mid,
847 int sdp_mline_index,
848 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100849 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700850 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
851 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
852 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
853 }
854
855 // PeerConnectionObserver callbacks.
856 void OnSignalingChange(
857 webrtc::PeerConnectionInterface::SignalingState new_state) override {
858 EXPECT_EQ(pc()->signaling_state(), new_state);
859 }
Steve Anton15324772018-01-16 10:26:49 -0800860 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
861 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
862 streams) override {
863 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
864 rtc::scoped_refptr<VideoTrackInterface> video_track(
865 static_cast<VideoTrackInterface*>(receiver->track().get()));
866 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700867 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800868 fake_video_renderers_[video_track->id()] =
869 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700870 }
871 }
Steve Anton15324772018-01-16 10:26:49 -0800872 void OnRemoveTrack(
873 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
874 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
875 auto it = fake_video_renderers_.find(receiver->track()->id());
876 RTC_DCHECK(it != fake_video_renderers_.end());
877 fake_video_renderers_.erase(it);
878 }
879 }
deadbeef1dcb1642017-03-29 21:08:16 -0700880 void OnRenegotiationNeeded() override {}
881 void OnIceConnectionChange(
882 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
883 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700884 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700885 }
886 void OnIceGatheringChange(
887 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700888 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700889 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700890 }
891 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100892 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700893
894 std::string ice_sdp;
895 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700896 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700897 // Remote party may be deleted.
898 return;
899 }
900 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
901 }
902 void OnDataChannel(
903 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100904 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700905 data_channel_ = data_channel;
906 data_observer_.reset(new MockDataChannelObserver(data_channel));
907 }
908
deadbeef1dcb1642017-03-29 21:08:16 -0700909 std::string debug_name_;
910
911 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
912
913 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
914 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
915 peer_connection_factory_;
916
Steve Antonede9ca52017-10-16 13:04:27 -0700917 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700918 // Needed to keep track of number of frames sent.
919 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
920 // Needed to keep track of number of frames received.
921 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
922 fake_video_renderers_;
923 // Needed to ensure frames aren't received for removed tracks.
924 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
925 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700926
927 // For remote peer communication.
928 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
929 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700930 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700931
Niels Möller5c7efe72018-05-11 10:34:46 +0200932 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700933 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200934 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
935 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700936 // |local_video_renderer_| attached to the first created local video track.
937 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
938
Seth Hampson2f0d7022018-02-20 11:54:42 -0800939 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700940 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
941 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
942 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800943 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700944
945 rtc::scoped_refptr<DataChannelInterface> data_channel_;
946 std::unique_ptr<MockDataChannelObserver> data_observer_;
947
948 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
949
Steve Antonede9ca52017-10-16 13:04:27 -0700950 std::vector<PeerConnectionInterface::IceConnectionState>
951 ice_connection_state_history_;
952 std::vector<PeerConnectionInterface::IceGatheringState>
953 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700954
955 rtc::AsyncInvoker invoker_;
956
Seth Hampson2f0d7022018-02-20 11:54:42 -0800957 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700958};
959
Elad Alon99c3fe52017-10-13 16:29:40 +0200960class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
961 public:
962 virtual ~MockRtcEventLogOutput() = default;
963 MOCK_CONST_METHOD0(IsActive, bool());
964 MOCK_METHOD1(Write, bool(const std::string&));
965};
966
Seth Hampson2f0d7022018-02-20 11:54:42 -0800967// This helper object is used for both specifying how many audio/video frames
968// are expected to be received for a caller/callee. It provides helper functions
969// to specify these expectations. The object initially starts in a state of no
970// expectations.
971class MediaExpectations {
972 public:
973 enum ExpectFrames {
974 kExpectSomeFrames,
975 kExpectNoFrames,
976 kNoExpectation,
977 };
978
979 void ExpectBidirectionalAudioAndVideo() {
980 ExpectBidirectionalAudio();
981 ExpectBidirectionalVideo();
982 }
983
984 void ExpectBidirectionalAudio() {
985 CallerExpectsSomeAudio();
986 CalleeExpectsSomeAudio();
987 }
988
989 void ExpectNoAudio() {
990 CallerExpectsNoAudio();
991 CalleeExpectsNoAudio();
992 }
993
994 void ExpectBidirectionalVideo() {
995 CallerExpectsSomeVideo();
996 CalleeExpectsSomeVideo();
997 }
998
999 void ExpectNoVideo() {
1000 CallerExpectsNoVideo();
1001 CalleeExpectsNoVideo();
1002 }
1003
1004 void CallerExpectsSomeAudioAndVideo() {
1005 CallerExpectsSomeAudio();
1006 CallerExpectsSomeVideo();
1007 }
1008
1009 void CalleeExpectsSomeAudioAndVideo() {
1010 CalleeExpectsSomeAudio();
1011 CalleeExpectsSomeVideo();
1012 }
1013
1014 // Caller's audio functions.
1015 void CallerExpectsSomeAudio(
1016 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1017 caller_audio_expectation_ = kExpectSomeFrames;
1018 caller_audio_frames_expected_ = expected_audio_frames;
1019 }
1020
1021 void CallerExpectsNoAudio() {
1022 caller_audio_expectation_ = kExpectNoFrames;
1023 caller_audio_frames_expected_ = 0;
1024 }
1025
1026 // Caller's video functions.
1027 void CallerExpectsSomeVideo(
1028 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1029 caller_video_expectation_ = kExpectSomeFrames;
1030 caller_video_frames_expected_ = expected_video_frames;
1031 }
1032
1033 void CallerExpectsNoVideo() {
1034 caller_video_expectation_ = kExpectNoFrames;
1035 caller_video_frames_expected_ = 0;
1036 }
1037
1038 // Callee's audio functions.
1039 void CalleeExpectsSomeAudio(
1040 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1041 callee_audio_expectation_ = kExpectSomeFrames;
1042 callee_audio_frames_expected_ = expected_audio_frames;
1043 }
1044
1045 void CalleeExpectsNoAudio() {
1046 callee_audio_expectation_ = kExpectNoFrames;
1047 callee_audio_frames_expected_ = 0;
1048 }
1049
1050 // Callee's video functions.
1051 void CalleeExpectsSomeVideo(
1052 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1053 callee_video_expectation_ = kExpectSomeFrames;
1054 callee_video_frames_expected_ = expected_video_frames;
1055 }
1056
1057 void CalleeExpectsNoVideo() {
1058 callee_video_expectation_ = kExpectNoFrames;
1059 callee_video_frames_expected_ = 0;
1060 }
1061
1062 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1063 ExpectFrames caller_video_expectation_ = kNoExpectation;
1064 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1065 ExpectFrames callee_video_expectation_ = kNoExpectation;
1066 int caller_audio_frames_expected_ = 0;
1067 int caller_video_frames_expected_ = 0;
1068 int callee_audio_frames_expected_ = 0;
1069 int callee_video_frames_expected_ = 0;
1070};
1071
deadbeef1dcb1642017-03-29 21:08:16 -07001072// Tests two PeerConnections connecting to each other end-to-end, using a
1073// virtual network, fake A/V capture and fake encoder/decoders. The
1074// PeerConnections share the threads/socket servers, but use separate versions
1075// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001076class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001077 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001078 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1079 : sdp_semantics_(sdp_semantics),
1080 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001081 fss_(new rtc::FirewallSocketServer(ss_.get())),
1082 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001083 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001084 network_thread_->SetName("PCNetworkThread", this);
1085 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001086 RTC_CHECK(network_thread_->Start());
1087 RTC_CHECK(worker_thread_->Start());
1088 }
1089
Seth Hampson2f0d7022018-02-20 11:54:42 -08001090 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001091 if (caller_) {
1092 caller_->set_signaling_message_receiver(nullptr);
1093 }
1094 if (callee_) {
1095 callee_->set_signaling_message_receiver(nullptr);
1096 }
1097 }
1098
1099 bool SignalingStateStable() {
1100 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1101 }
1102
deadbeef71452802017-05-07 17:21:01 -07001103 bool DtlsConnected() {
1104 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1105 // are connected. This is an important distinction. Once we have separate
1106 // ICE and DTLS state, this check needs to use the DTLS state.
1107 return (callee()->ice_connection_state() ==
1108 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1109 callee()->ice_connection_state() ==
1110 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1111 (caller()->ice_connection_state() ==
1112 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1113 caller()->ice_connection_state() ==
1114 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1115 }
1116
Seth Hampson2f0d7022018-02-20 11:54:42 -08001117 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1118 const std::string& debug_name,
1119 const MediaConstraintsInterface* constraints,
1120 const PeerConnectionFactory::Options* options,
1121 const RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001122 webrtc::PeerConnectionDependencies dependencies) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001123 RTCConfiguration modified_config;
1124 if (config) {
1125 modified_config = *config;
1126 }
Steve Anton3acffc32018-04-12 17:21:03 -07001127 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001128 if (!dependencies.cert_generator) {
1129 dependencies.cert_generator =
1130 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001131 }
1132 std::unique_ptr<PeerConnectionWrapper> client(
1133 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001134
Seth Hampson2f0d7022018-02-20 11:54:42 -08001135 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001136 std::move(dependencies), network_thread_.get(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001137 worker_thread_.get())) {
1138 return nullptr;
1139 }
1140 return client;
1141 }
1142
deadbeef1dcb1642017-03-29 21:08:16 -07001143 bool CreatePeerConnectionWrappers() {
1144 return CreatePeerConnectionWrappersWithConfig(
1145 PeerConnectionInterface::RTCConfiguration(),
1146 PeerConnectionInterface::RTCConfiguration());
1147 }
1148
Steve Anton3acffc32018-04-12 17:21:03 -07001149 bool CreatePeerConnectionWrappersWithSdpSemantics(
1150 SdpSemantics caller_semantics,
1151 SdpSemantics callee_semantics) {
1152 // Can't specify the sdp_semantics in the passed-in configuration since it
1153 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1154 // stored in sdp_semantics_. So get around this by modifying the instance
1155 // variable before calling CreatePeerConnectionWrapper for the caller and
1156 // callee PeerConnections.
1157 SdpSemantics original_semantics = sdp_semantics_;
1158 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001159 caller_ = CreatePeerConnectionWrapper(
1160 "Caller", nullptr, nullptr, nullptr,
1161 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001162 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001163 callee_ = CreatePeerConnectionWrapper(
1164 "Callee", nullptr, nullptr, nullptr,
1165 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001166 sdp_semantics_ = original_semantics;
1167 return caller_ && callee_;
1168 }
1169
deadbeef1dcb1642017-03-29 21:08:16 -07001170 bool CreatePeerConnectionWrappersWithConstraints(
1171 MediaConstraintsInterface* caller_constraints,
1172 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001173 caller_ = CreatePeerConnectionWrapper(
1174 "Caller", caller_constraints, nullptr, nullptr,
1175 webrtc::PeerConnectionDependencies(nullptr));
1176 callee_ = CreatePeerConnectionWrapper(
1177 "Callee", callee_constraints, nullptr, nullptr,
1178 webrtc::PeerConnectionDependencies(nullptr));
1179
deadbeef1dcb1642017-03-29 21:08:16 -07001180 return caller_ && callee_;
1181 }
1182
1183 bool CreatePeerConnectionWrappersWithConfig(
1184 const PeerConnectionInterface::RTCConfiguration& caller_config,
1185 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001186 caller_ = CreatePeerConnectionWrapper(
1187 "Caller", nullptr, nullptr, &caller_config,
1188 webrtc::PeerConnectionDependencies(nullptr));
1189 callee_ = CreatePeerConnectionWrapper(
1190 "Callee", nullptr, nullptr, &callee_config,
1191 webrtc::PeerConnectionDependencies(nullptr));
1192 return caller_ && callee_;
1193 }
1194
1195 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1196 const PeerConnectionInterface::RTCConfiguration& caller_config,
1197 webrtc::PeerConnectionDependencies caller_dependencies,
1198 const PeerConnectionInterface::RTCConfiguration& callee_config,
1199 webrtc::PeerConnectionDependencies callee_dependencies) {
1200 caller_ =
1201 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
1202 std::move(caller_dependencies));
1203 callee_ =
1204 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
1205 std::move(callee_dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001206 return caller_ && callee_;
1207 }
1208
1209 bool CreatePeerConnectionWrappersWithOptions(
1210 const PeerConnectionFactory::Options& caller_options,
1211 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001212 caller_ = CreatePeerConnectionWrapper(
1213 "Caller", nullptr, &caller_options, nullptr,
1214 webrtc::PeerConnectionDependencies(nullptr));
1215 callee_ = CreatePeerConnectionWrapper(
1216 "Callee", nullptr, &callee_options, nullptr,
1217 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001218 return caller_ && callee_;
1219 }
1220
Seth Hampson2f0d7022018-02-20 11:54:42 -08001221 std::unique_ptr<PeerConnectionWrapper>
1222 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001223 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1224 new FakeRTCCertificateGenerator());
1225 cert_generator->use_alternate_key();
1226
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001227 webrtc::PeerConnectionDependencies dependencies(nullptr);
1228 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001229 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001230 std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001231 }
1232
1233 // Once called, SDP blobs and ICE candidates will be automatically signaled
1234 // between PeerConnections.
1235 void ConnectFakeSignaling() {
1236 caller_->set_signaling_message_receiver(callee_.get());
1237 callee_->set_signaling_message_receiver(caller_.get());
1238 }
1239
Steve Antonede9ca52017-10-16 13:04:27 -07001240 // Once called, SDP blobs will be automatically signaled between
1241 // PeerConnections. Note that ICE candidates will not be signaled unless they
1242 // are in the exchanged SDP blobs.
1243 void ConnectFakeSignalingForSdpOnly() {
1244 ConnectFakeSignaling();
1245 SetSignalIceCandidates(false);
1246 }
1247
deadbeef1dcb1642017-03-29 21:08:16 -07001248 void SetSignalingDelayMs(int delay_ms) {
1249 caller_->set_signaling_delay_ms(delay_ms);
1250 callee_->set_signaling_delay_ms(delay_ms);
1251 }
1252
Steve Antonede9ca52017-10-16 13:04:27 -07001253 void SetSignalIceCandidates(bool signal) {
1254 caller_->set_signal_ice_candidates(signal);
1255 callee_->set_signal_ice_candidates(signal);
1256 }
1257
deadbeef1dcb1642017-03-29 21:08:16 -07001258 // Messages may get lost on the unreliable DataChannel, so we send multiple
1259 // times to avoid test flakiness.
1260 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1261 const std::string& data,
1262 int retries) {
1263 for (int i = 0; i < retries; ++i) {
1264 dc->Send(DataBuffer(data));
1265 }
1266 }
1267
1268 rtc::Thread* network_thread() { return network_thread_.get(); }
1269
1270 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1271
1272 PeerConnectionWrapper* caller() { return caller_.get(); }
1273
1274 // Set the |caller_| to the |wrapper| passed in and return the
1275 // original |caller_|.
1276 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1277 PeerConnectionWrapper* wrapper) {
1278 PeerConnectionWrapper* old = caller_.release();
1279 caller_.reset(wrapper);
1280 return old;
1281 }
1282
1283 PeerConnectionWrapper* callee() { return callee_.get(); }
1284
1285 // Set the |callee_| to the |wrapper| passed in and return the
1286 // original |callee_|.
1287 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1288 PeerConnectionWrapper* wrapper) {
1289 PeerConnectionWrapper* old = callee_.release();
1290 callee_.reset(wrapper);
1291 return old;
1292 }
1293
Steve Antonede9ca52017-10-16 13:04:27 -07001294 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1295
Seth Hampson2f0d7022018-02-20 11:54:42 -08001296 // Expects the provided number of new frames to be received within
1297 // kMaxWaitForFramesMs. The new expected frames are specified in
1298 // |media_expectations|. Returns false if any of the expectations were
1299 // not met.
1300 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1301 // First initialize the expected frame counts based upon the current
1302 // frame count.
1303 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1304 if (media_expectations.caller_audio_expectation_ ==
1305 MediaExpectations::kExpectSomeFrames) {
1306 total_caller_audio_frames_expected +=
1307 media_expectations.caller_audio_frames_expected_;
1308 }
1309 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001310 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001311 if (media_expectations.caller_video_expectation_ ==
1312 MediaExpectations::kExpectSomeFrames) {
1313 total_caller_video_frames_expected +=
1314 media_expectations.caller_video_frames_expected_;
1315 }
1316 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1317 if (media_expectations.callee_audio_expectation_ ==
1318 MediaExpectations::kExpectSomeFrames) {
1319 total_callee_audio_frames_expected +=
1320 media_expectations.callee_audio_frames_expected_;
1321 }
1322 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001323 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 if (media_expectations.callee_video_expectation_ ==
1325 MediaExpectations::kExpectSomeFrames) {
1326 total_callee_video_frames_expected +=
1327 media_expectations.callee_video_frames_expected_;
1328 }
deadbeef1dcb1642017-03-29 21:08:16 -07001329
Seth Hampson2f0d7022018-02-20 11:54:42 -08001330 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001331 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001332 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001333 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001334 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001335 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001336 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001337 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001338 total_callee_video_frames_expected,
1339 kMaxWaitForFramesMs);
1340 bool expectations_correct =
1341 caller()->audio_frames_received() >=
1342 total_caller_audio_frames_expected &&
1343 caller()->min_video_frames_received_per_track() >=
1344 total_caller_video_frames_expected &&
1345 callee()->audio_frames_received() >=
1346 total_callee_audio_frames_expected &&
1347 callee()->min_video_frames_received_per_track() >=
1348 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001349
Seth Hampson2f0d7022018-02-20 11:54:42 -08001350 // After the combined wait, print out a more detailed message upon
1351 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001352 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001353 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001354 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001355 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001356 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001357 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001358 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001359 total_callee_video_frames_expected);
1360
1361 // We want to make sure nothing unexpected was received.
1362 if (media_expectations.caller_audio_expectation_ ==
1363 MediaExpectations::kExpectNoFrames) {
1364 EXPECT_EQ(caller()->audio_frames_received(),
1365 total_caller_audio_frames_expected);
1366 if (caller()->audio_frames_received() !=
1367 total_caller_audio_frames_expected) {
1368 expectations_correct = false;
1369 }
1370 }
1371 if (media_expectations.caller_video_expectation_ ==
1372 MediaExpectations::kExpectNoFrames) {
1373 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1374 total_caller_video_frames_expected);
1375 if (caller()->min_video_frames_received_per_track() !=
1376 total_caller_video_frames_expected) {
1377 expectations_correct = false;
1378 }
1379 }
1380 if (media_expectations.callee_audio_expectation_ ==
1381 MediaExpectations::kExpectNoFrames) {
1382 EXPECT_EQ(callee()->audio_frames_received(),
1383 total_callee_audio_frames_expected);
1384 if (callee()->audio_frames_received() !=
1385 total_callee_audio_frames_expected) {
1386 expectations_correct = false;
1387 }
1388 }
1389 if (media_expectations.callee_video_expectation_ ==
1390 MediaExpectations::kExpectNoFrames) {
1391 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1392 total_callee_video_frames_expected);
1393 if (callee()->min_video_frames_received_per_track() !=
1394 total_callee_video_frames_expected) {
1395 expectations_correct = false;
1396 }
1397 }
1398 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001399 }
1400
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001401 void TestNegotiatedCipherSuite(
1402 const PeerConnectionFactory::Options& caller_options,
1403 const PeerConnectionFactory::Options& callee_options,
1404 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001405 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1406 callee_options));
1407 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1408 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1409 caller()->pc()->RegisterUMAObserver(caller_observer);
1410 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001411 caller()->AddAudioVideoTracks();
1412 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001413 caller()->CreateAndSetAndSignalOffer();
1414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1415 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001416 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001417 EXPECT_EQ(
1418 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1419 expected_cipher_suite));
1420 caller()->pc()->RegisterUMAObserver(nullptr);
1421 }
1422
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001423 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1424 bool remote_gcm_enabled,
1425 int expected_cipher_suite) {
1426 PeerConnectionFactory::Options caller_options;
1427 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1428 PeerConnectionFactory::Options callee_options;
1429 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1430 TestNegotiatedCipherSuite(caller_options, callee_options,
1431 expected_cipher_suite);
1432 }
1433
Seth Hampson2f0d7022018-02-20 11:54:42 -08001434 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001435 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001436
deadbeef1dcb1642017-03-29 21:08:16 -07001437 private:
1438 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001439 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001440 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001441 // |network_thread_| and |worker_thread_| are used by both
1442 // |caller_| and |callee_| so they must be destroyed
1443 // later.
1444 std::unique_ptr<rtc::Thread> network_thread_;
1445 std::unique_ptr<rtc::Thread> worker_thread_;
1446 std::unique_ptr<PeerConnectionWrapper> caller_;
1447 std::unique_ptr<PeerConnectionWrapper> callee_;
1448};
1449
Seth Hampson2f0d7022018-02-20 11:54:42 -08001450class PeerConnectionIntegrationTest
1451 : public PeerConnectionIntegrationBaseTest,
1452 public ::testing::WithParamInterface<SdpSemantics> {
1453 protected:
1454 PeerConnectionIntegrationTest()
1455 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1456};
1457
1458class PeerConnectionIntegrationTestPlanB
1459 : public PeerConnectionIntegrationBaseTest {
1460 protected:
1461 PeerConnectionIntegrationTestPlanB()
1462 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1463};
1464
1465class PeerConnectionIntegrationTestUnifiedPlan
1466 : public PeerConnectionIntegrationBaseTest {
1467 protected:
1468 PeerConnectionIntegrationTestUnifiedPlan()
1469 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1470};
1471
deadbeef1dcb1642017-03-29 21:08:16 -07001472// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1473// includes testing that the callback is invoked if an observer is connected
1474// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001475TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001476 RtpReceiverObserverOnFirstPacketReceived) {
1477 ASSERT_TRUE(CreatePeerConnectionWrappers());
1478 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001479 caller()->AddAudioVideoTracks();
1480 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001481 // Start offer/answer exchange and wait for it to complete.
1482 caller()->CreateAndSetAndSignalOffer();
1483 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1484 // Should be one receiver each for audio/video.
1485 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1486 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1487 // Wait for all "first packet received" callbacks to be fired.
1488 EXPECT_TRUE_WAIT(
1489 std::all_of(caller()->rtp_receiver_observers().begin(),
1490 caller()->rtp_receiver_observers().end(),
1491 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1492 return o->first_packet_received();
1493 }),
1494 kMaxWaitForFramesMs);
1495 EXPECT_TRUE_WAIT(
1496 std::all_of(callee()->rtp_receiver_observers().begin(),
1497 callee()->rtp_receiver_observers().end(),
1498 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1499 return o->first_packet_received();
1500 }),
1501 kMaxWaitForFramesMs);
1502 // If new observers are set after the first packet was already received, the
1503 // callback should still be invoked.
1504 caller()->ResetRtpReceiverObservers();
1505 callee()->ResetRtpReceiverObservers();
1506 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1507 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1508 EXPECT_TRUE(
1509 std::all_of(caller()->rtp_receiver_observers().begin(),
1510 caller()->rtp_receiver_observers().end(),
1511 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1512 return o->first_packet_received();
1513 }));
1514 EXPECT_TRUE(
1515 std::all_of(callee()->rtp_receiver_observers().begin(),
1516 callee()->rtp_receiver_observers().end(),
1517 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1518 return o->first_packet_received();
1519 }));
1520}
1521
1522class DummyDtmfObserver : public DtmfSenderObserverInterface {
1523 public:
1524 DummyDtmfObserver() : completed_(false) {}
1525
1526 // Implements DtmfSenderObserverInterface.
1527 void OnToneChange(const std::string& tone) override {
1528 tones_.push_back(tone);
1529 if (tone.empty()) {
1530 completed_ = true;
1531 }
1532 }
1533
1534 const std::vector<std::string>& tones() const { return tones_; }
1535 bool completed() const { return completed_; }
1536
1537 private:
1538 bool completed_;
1539 std::vector<std::string> tones_;
1540};
1541
1542// Assumes |sender| already has an audio track added and the offer/answer
1543// exchange is done.
1544void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1545 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001546 // We should be able to get a DTMF sender from the local sender.
1547 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1548 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1549 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001550 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001551 dtmf_sender->RegisterObserver(&observer);
1552
1553 // Test the DtmfSender object just created.
1554 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1555 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1556
1557 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1558 std::vector<std::string> tones = {"1", "a", ""};
1559 EXPECT_EQ(tones, observer.tones());
1560 dtmf_sender->UnregisterObserver();
1561 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1562}
1563
1564// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1565// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001566TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001567 ASSERT_TRUE(CreatePeerConnectionWrappers());
1568 ConnectFakeSignaling();
1569 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001570 caller()->AddAudioTrack();
1571 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001572 caller()->CreateAndSetAndSignalOffer();
1573 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001574 // DTLS must finish before the DTMF sender can be used reliably.
1575 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001576 TestDtmfFromSenderToReceiver(caller(), callee());
1577 TestDtmfFromSenderToReceiver(callee(), caller());
1578}
1579
1580// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1581// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001582TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001583 ASSERT_TRUE(CreatePeerConnectionWrappers());
1584 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001585 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1586 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1587 caller()->pc()->RegisterUMAObserver(caller_observer);
1588
deadbeef1dcb1642017-03-29 21:08:16 -07001589 // Do normal offer/answer and wait for some frames to be received in each
1590 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001591 caller()->AddAudioVideoTracks();
1592 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001593 caller()->CreateAndSetAndSignalOffer();
1594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001595 MediaExpectations media_expectations;
1596 media_expectations.ExpectBidirectionalAudioAndVideo();
1597 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001598 EXPECT_LE(
1599 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1600 webrtc::kEnumCounterKeyProtocolDtls));
1601 EXPECT_EQ(
1602 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1603 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001604}
1605
1606// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001607TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001608 PeerConnectionInterface::RTCConfiguration sdes_config;
1609 sdes_config.enable_dtls_srtp.emplace(false);
1610 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1611 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001612 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1613 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1614 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001615
1616 // Do normal offer/answer and wait for some frames to be received in each
1617 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001618 caller()->AddAudioVideoTracks();
1619 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001620 caller()->CreateAndSetAndSignalOffer();
1621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 MediaExpectations media_expectations;
1623 media_expectations.ExpectBidirectionalAudioAndVideo();
1624 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001625 EXPECT_LE(
1626 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1627 webrtc::kEnumCounterKeyProtocolSdes));
1628 EXPECT_EQ(
1629 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1630 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001631}
1632
Steve Anton8c0f7a72017-10-03 10:03:10 -07001633// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1634// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001635TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001636 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1637 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1638 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1639 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1640 return pc->GetRemoteAudioSSLCertificate();
1641 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001642 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1643 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1644 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1645 return pc->GetRemoteAudioSSLCertChain();
1646 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001647
1648 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1649 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1650
1651 // Configure each side with a known certificate so they can be compared later.
1652 PeerConnectionInterface::RTCConfiguration caller_config;
1653 caller_config.enable_dtls_srtp.emplace(true);
1654 caller_config.certificates.push_back(caller_cert);
1655 PeerConnectionInterface::RTCConfiguration callee_config;
1656 callee_config.enable_dtls_srtp.emplace(true);
1657 callee_config.certificates.push_back(callee_cert);
1658 ASSERT_TRUE(
1659 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1660 ConnectFakeSignaling();
1661
1662 // When first initialized, there should not be a remote SSL certificate (and
1663 // calling this method should not crash).
1664 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1665 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001666 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1667 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001668
Steve Anton15324772018-01-16 10:26:49 -08001669 caller()->AddAudioTrack();
1670 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001671 caller()->CreateAndSetAndSignalOffer();
1672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1673 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1674
1675 // Once DTLS has been connected, each side should return the other's SSL
1676 // certificate when calling GetRemoteAudioSSLCertificate.
1677
1678 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1679 ASSERT_TRUE(caller_remote_cert);
1680 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1681 caller_remote_cert->ToPEMString());
1682
1683 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1684 ASSERT_TRUE(callee_remote_cert);
1685 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1686 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001687
1688 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1689 ASSERT_TRUE(caller_remote_cert_chain);
1690 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1691 auto remote_cert = &caller_remote_cert_chain->Get(0);
1692 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1693 remote_cert->ToPEMString());
1694
1695 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1696 ASSERT_TRUE(callee_remote_cert_chain);
1697 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1698 remote_cert = &callee_remote_cert_chain->Get(0);
1699 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1700 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001701}
1702
deadbeef1dcb1642017-03-29 21:08:16 -07001703// This test sets up a call between two parties with a source resolution of
1704// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001705TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001706 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1707 ASSERT_TRUE(CreatePeerConnectionWrappers());
1708 ConnectFakeSignaling();
1709
Niels Möller5c7efe72018-05-11 10:34:46 +02001710 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1711 webrtc::FakePeriodicVideoSource::Config config;
1712 config.width = 1280;
1713 config.height = 720;
1714 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1715 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001716
1717 // Do normal offer/answer and wait for at least one frame to be received in
1718 // each direction.
1719 caller()->CreateAndSetAndSignalOffer();
1720 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1721 callee()->min_video_frames_received_per_track() > 0,
1722 kMaxWaitForFramesMs);
1723
1724 // Check rendered aspect ratio.
1725 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1726 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1727 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1728 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1729}
1730
1731// This test sets up an one-way call, with media only from caller to
1732// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001733TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001734 ASSERT_TRUE(CreatePeerConnectionWrappers());
1735 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001736 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001737 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001738 MediaExpectations media_expectations;
1739 media_expectations.CalleeExpectsSomeAudioAndVideo();
1740 media_expectations.CallerExpectsNoAudio();
1741 media_expectations.CallerExpectsNoVideo();
1742 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001743}
1744
1745// This test sets up a audio call initially, with the callee rejecting video
1746// initially. Then later the callee decides to upgrade to audio/video, and
1747// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001748TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001749 ASSERT_TRUE(CreatePeerConnectionWrappers());
1750 ConnectFakeSignaling();
1751 // Initially, offer an audio/video stream from the caller, but refuse to
1752 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001753 caller()->AddAudioVideoTracks();
1754 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001755 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1756 PeerConnectionInterface::RTCOfferAnswerOptions options;
1757 options.offer_to_receive_video = 0;
1758 callee()->SetOfferAnswerOptions(options);
1759 } else {
1760 callee()->SetRemoteOfferHandler([this] {
1761 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1762 });
1763 }
deadbeef1dcb1642017-03-29 21:08:16 -07001764 // Do offer/answer and make sure audio is still received end-to-end.
1765 caller()->CreateAndSetAndSignalOffer();
1766 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001767 {
1768 MediaExpectations media_expectations;
1769 media_expectations.ExpectBidirectionalAudio();
1770 media_expectations.ExpectNoVideo();
1771 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1772 }
deadbeef1dcb1642017-03-29 21:08:16 -07001773 // Sanity check that the callee's description has a rejected video section.
1774 ASSERT_NE(nullptr, callee()->pc()->local_description());
1775 const ContentInfo* callee_video_content =
1776 GetFirstVideoContent(callee()->pc()->local_description()->description());
1777 ASSERT_NE(nullptr, callee_video_content);
1778 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001779
deadbeef1dcb1642017-03-29 21:08:16 -07001780 // Now negotiate with video and ensure negotiation succeeds, with video
1781 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001782 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001783 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1784 PeerConnectionInterface::RTCOfferAnswerOptions options;
1785 options.offer_to_receive_video = 1;
1786 callee()->SetOfferAnswerOptions(options);
1787 } else {
1788 callee()->SetRemoteOfferHandler(nullptr);
1789 caller()->SetRemoteOfferHandler([this] {
1790 // The caller creates a new transceiver to receive video on when receiving
1791 // the offer, but by default it is send only.
1792 auto transceivers = caller()->pc()->GetTransceivers();
1793 ASSERT_EQ(3, transceivers.size());
1794 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1795 transceivers[2]->receiver()->media_type());
1796 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1797 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1798 });
1799 }
deadbeef1dcb1642017-03-29 21:08:16 -07001800 callee()->CreateAndSetAndSignalOffer();
1801 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001802 {
1803 // Expect additional audio frames to be received after the upgrade.
1804 MediaExpectations media_expectations;
1805 media_expectations.ExpectBidirectionalAudioAndVideo();
1806 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1807 }
deadbeef1dcb1642017-03-29 21:08:16 -07001808}
1809
deadbeef4389b4d2017-09-07 09:07:36 -07001810// Simpler than the above test; just add an audio track to an established
1811// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001812TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001813 ASSERT_TRUE(CreatePeerConnectionWrappers());
1814 ConnectFakeSignaling();
1815 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001816 caller()->AddVideoTrack();
1817 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001818 caller()->CreateAndSetAndSignalOffer();
1819 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1820 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001821 caller()->AddAudioTrack();
1822 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001823 caller()->CreateAndSetAndSignalOffer();
1824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1825 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001826 MediaExpectations media_expectations;
1827 media_expectations.ExpectBidirectionalAudioAndVideo();
1828 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001829}
1830
deadbeef1dcb1642017-03-29 21:08:16 -07001831// This test sets up a call that's transferred to a new caller with a different
1832// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001833TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001834 ASSERT_TRUE(CreatePeerConnectionWrappers());
1835 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001836 caller()->AddAudioVideoTracks();
1837 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001838 caller()->CreateAndSetAndSignalOffer();
1839 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1840
1841 // Keep the original peer around which will still send packets to the
1842 // receiving client. These SRTP packets will be dropped.
1843 std::unique_ptr<PeerConnectionWrapper> original_peer(
1844 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001845 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001846 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1847 // directly above.
1848 original_peer->pc()->Close();
1849
1850 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001851 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001852 caller()->CreateAndSetAndSignalOffer();
1853 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1854 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001855 MediaExpectations media_expectations;
1856 media_expectations.ExpectBidirectionalAudioAndVideo();
1857 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001858}
1859
1860// This test sets up a call that's transferred to a new callee with a different
1861// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001862TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001863 ASSERT_TRUE(CreatePeerConnectionWrappers());
1864 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001865 caller()->AddAudioVideoTracks();
1866 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001867 caller()->CreateAndSetAndSignalOffer();
1868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1869
1870 // Keep the original peer around which will still send packets to the
1871 // receiving client. These SRTP packets will be dropped.
1872 std::unique_ptr<PeerConnectionWrapper> original_peer(
1873 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001874 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001875 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1876 // directly above.
1877 original_peer->pc()->Close();
1878
1879 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001880 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001881 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1882 caller()->CreateAndSetAndSignalOffer();
1883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1884 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001885 MediaExpectations media_expectations;
1886 media_expectations.ExpectBidirectionalAudioAndVideo();
1887 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001888}
1889
1890// This test sets up a non-bundled call and negotiates bundling at the same
1891// time as starting an ICE restart. When bundling is in effect in the restart,
1892// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001893TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001894 ASSERT_TRUE(CreatePeerConnectionWrappers());
1895 ConnectFakeSignaling();
1896
Steve Anton15324772018-01-16 10:26:49 -08001897 caller()->AddAudioVideoTracks();
1898 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001899 // Remove the bundle group from the SDP received by the callee.
1900 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1901 desc->RemoveGroupByName("BUNDLE");
1902 });
1903 caller()->CreateAndSetAndSignalOffer();
1904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001905 {
1906 MediaExpectations media_expectations;
1907 media_expectations.ExpectBidirectionalAudioAndVideo();
1908 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1909 }
deadbeef1dcb1642017-03-29 21:08:16 -07001910 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1911 callee()->SetReceivedSdpMunger(nullptr);
1912 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1913 caller()->CreateAndSetAndSignalOffer();
1914 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1915
1916 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001917 {
1918 MediaExpectations media_expectations;
1919 media_expectations.ExpectBidirectionalAudioAndVideo();
1920 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1921 }
deadbeef1dcb1642017-03-29 21:08:16 -07001922}
1923
1924// Test CVO (Coordination of Video Orientation). If a video source is rotated
1925// and both peers support the CVO RTP header extension, the actual video frames
1926// don't need to be encoded in different resolutions, since the rotation is
1927// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001928TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001929 ASSERT_TRUE(CreatePeerConnectionWrappers());
1930 ConnectFakeSignaling();
1931 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001932 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001933 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001934 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001935 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1936
1937 // Wait for video frames to be received by both sides.
1938 caller()->CreateAndSetAndSignalOffer();
1939 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1940 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1941 callee()->min_video_frames_received_per_track() > 0,
1942 kMaxWaitForFramesMs);
1943
1944 // Ensure that the aspect ratio is unmodified.
1945 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1946 // not just assumed.
1947 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1948 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1949 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1950 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1951 // Ensure that the CVO bits were surfaced to the renderer.
1952 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1953 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1954}
1955
1956// Test that when the CVO extension isn't supported, video is rotated the
1957// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001958TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001959 ASSERT_TRUE(CreatePeerConnectionWrappers());
1960 ConnectFakeSignaling();
1961 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001962 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001963 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001964 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001965 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1966
1967 // Remove the CVO extension from the offered SDP.
1968 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1969 cricket::VideoContentDescription* video =
1970 GetFirstVideoContentDescription(desc);
1971 video->ClearRtpHeaderExtensions();
1972 });
1973 // Wait for video frames to be received by both sides.
1974 caller()->CreateAndSetAndSignalOffer();
1975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1976 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1977 callee()->min_video_frames_received_per_track() > 0,
1978 kMaxWaitForFramesMs);
1979
1980 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1981 // rotation.
1982 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1983 // not just assumed.
1984 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1985 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1986 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1987 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1988 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1989 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1990 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1991}
1992
deadbeef1dcb1642017-03-29 21:08:16 -07001993// Test that if the answerer rejects the audio m= section, no audio is sent or
1994// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001995TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001996 ASSERT_TRUE(CreatePeerConnectionWrappers());
1997 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001998 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001999 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2000 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2001 // it will reject the audio m= section completely.
2002 PeerConnectionInterface::RTCOfferAnswerOptions options;
2003 options.offer_to_receive_audio = 0;
2004 callee()->SetOfferAnswerOptions(options);
2005 } else {
2006 // Stopping the audio RtpTransceiver will cause the media section to be
2007 // rejected in the answer.
2008 callee()->SetRemoteOfferHandler([this] {
2009 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2010 });
2011 }
Steve Anton15324772018-01-16 10:26:49 -08002012 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002013 // Do offer/answer and wait for successful end-to-end video frames.
2014 caller()->CreateAndSetAndSignalOffer();
2015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002016 MediaExpectations media_expectations;
2017 media_expectations.ExpectBidirectionalVideo();
2018 media_expectations.ExpectNoAudio();
2019 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2020
deadbeef1dcb1642017-03-29 21:08:16 -07002021 // Sanity check that the callee's description has a rejected audio section.
2022 ASSERT_NE(nullptr, callee()->pc()->local_description());
2023 const ContentInfo* callee_audio_content =
2024 GetFirstAudioContent(callee()->pc()->local_description()->description());
2025 ASSERT_NE(nullptr, callee_audio_content);
2026 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2028 // The caller's transceiver should have stopped after receiving the answer.
2029 EXPECT_TRUE(caller()
2030 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2031 ->stopped());
2032 }
deadbeef1dcb1642017-03-29 21:08:16 -07002033}
2034
2035// Test that if the answerer rejects the video m= section, no video is sent or
2036// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002037TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002038 ASSERT_TRUE(CreatePeerConnectionWrappers());
2039 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002040 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002041 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2042 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2043 // it will reject the video m= section completely.
2044 PeerConnectionInterface::RTCOfferAnswerOptions options;
2045 options.offer_to_receive_video = 0;
2046 callee()->SetOfferAnswerOptions(options);
2047 } else {
2048 // Stopping the video RtpTransceiver will cause the media section to be
2049 // rejected in the answer.
2050 callee()->SetRemoteOfferHandler([this] {
2051 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2052 });
2053 }
Steve Anton15324772018-01-16 10:26:49 -08002054 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002055 // Do offer/answer and wait for successful end-to-end audio frames.
2056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002058 MediaExpectations media_expectations;
2059 media_expectations.ExpectBidirectionalAudio();
2060 media_expectations.ExpectNoVideo();
2061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2062
deadbeef1dcb1642017-03-29 21:08:16 -07002063 // Sanity check that the callee's description has a rejected video section.
2064 ASSERT_NE(nullptr, callee()->pc()->local_description());
2065 const ContentInfo* callee_video_content =
2066 GetFirstVideoContent(callee()->pc()->local_description()->description());
2067 ASSERT_NE(nullptr, callee_video_content);
2068 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002069 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2070 // The caller's transceiver should have stopped after receiving the answer.
2071 EXPECT_TRUE(caller()
2072 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2073 ->stopped());
2074 }
deadbeef1dcb1642017-03-29 21:08:16 -07002075}
2076
2077// Test that if the answerer rejects both audio and video m= sections, nothing
2078// bad happens.
2079// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2080// test anything but the fact that negotiation succeeds, which doesn't mean
2081// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002082TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002083 ASSERT_TRUE(CreatePeerConnectionWrappers());
2084 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002085 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002086 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2087 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2088 // will reject both audio and video m= sections.
2089 PeerConnectionInterface::RTCOfferAnswerOptions options;
2090 options.offer_to_receive_audio = 0;
2091 options.offer_to_receive_video = 0;
2092 callee()->SetOfferAnswerOptions(options);
2093 } else {
2094 callee()->SetRemoteOfferHandler([this] {
2095 // Stopping all transceivers will cause all media sections to be rejected.
2096 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2097 transceiver->Stop();
2098 }
2099 });
2100 }
deadbeef1dcb1642017-03-29 21:08:16 -07002101 // Do offer/answer and wait for stable signaling state.
2102 caller()->CreateAndSetAndSignalOffer();
2103 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002104
deadbeef1dcb1642017-03-29 21:08:16 -07002105 // Sanity check that the callee's description has rejected m= sections.
2106 ASSERT_NE(nullptr, callee()->pc()->local_description());
2107 const ContentInfo* callee_audio_content =
2108 GetFirstAudioContent(callee()->pc()->local_description()->description());
2109 ASSERT_NE(nullptr, callee_audio_content);
2110 EXPECT_TRUE(callee_audio_content->rejected);
2111 const ContentInfo* callee_video_content =
2112 GetFirstVideoContent(callee()->pc()->local_description()->description());
2113 ASSERT_NE(nullptr, callee_video_content);
2114 EXPECT_TRUE(callee_video_content->rejected);
2115}
2116
2117// This test sets up an audio and video call between two parties. After the
2118// call runs for a while, the caller sends an updated offer with video being
2119// rejected. Once the re-negotiation is done, the video flow should stop and
2120// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002121TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002122 ASSERT_TRUE(CreatePeerConnectionWrappers());
2123 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002124 caller()->AddAudioVideoTracks();
2125 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002126 caller()->CreateAndSetAndSignalOffer();
2127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002128 {
2129 MediaExpectations media_expectations;
2130 media_expectations.ExpectBidirectionalAudioAndVideo();
2131 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2132 }
deadbeef1dcb1642017-03-29 21:08:16 -07002133 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002134 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2135 caller()->SetGeneratedSdpMunger(
2136 [](cricket::SessionDescription* description) {
2137 for (cricket::ContentInfo& content : description->contents()) {
2138 if (cricket::IsVideoContent(&content)) {
2139 content.rejected = true;
2140 }
2141 }
2142 });
2143 } else {
2144 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2145 }
deadbeef1dcb1642017-03-29 21:08:16 -07002146 caller()->CreateAndSetAndSignalOffer();
2147 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2148
2149 // Sanity check that the caller's description has a rejected video section.
2150 ASSERT_NE(nullptr, caller()->pc()->local_description());
2151 const ContentInfo* caller_video_content =
2152 GetFirstVideoContent(caller()->pc()->local_description()->description());
2153 ASSERT_NE(nullptr, caller_video_content);
2154 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002155 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002156 {
2157 MediaExpectations media_expectations;
2158 media_expectations.ExpectBidirectionalAudio();
2159 media_expectations.ExpectNoVideo();
2160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2161 }
deadbeef1dcb1642017-03-29 21:08:16 -07002162}
2163
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002164// Do one offer/answer with audio, another that disables it (rejecting the m=
2165// section), and another that re-enables it. Regression test for:
2166// bugs.webrtc.org/6023
2167TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2168 ASSERT_TRUE(CreatePeerConnectionWrappers());
2169 ConnectFakeSignaling();
2170
2171 // Add audio track, do normal offer/answer.
2172 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2173 caller()->CreateLocalAudioTrack();
2174 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2175 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2176 caller()->CreateAndSetAndSignalOffer();
2177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2178
2179 // Remove audio track, and set offer_to_receive_audio to false to cause the
2180 // m= section to be completely disabled, not just "recvonly".
2181 caller()->pc()->RemoveTrack(sender);
2182 PeerConnectionInterface::RTCOfferAnswerOptions options;
2183 options.offer_to_receive_audio = 0;
2184 caller()->SetOfferAnswerOptions(options);
2185 caller()->CreateAndSetAndSignalOffer();
2186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2187
2188 // Add the audio track again, expecting negotiation to succeed and frames to
2189 // flow.
2190 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2191 options.offer_to_receive_audio = 1;
2192 caller()->SetOfferAnswerOptions(options);
2193 caller()->CreateAndSetAndSignalOffer();
2194 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2195
2196 MediaExpectations media_expectations;
2197 media_expectations.CalleeExpectsSomeAudio();
2198 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2199}
2200
deadbeef1dcb1642017-03-29 21:08:16 -07002201// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2202// is needed to support legacy endpoints.
2203// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2204// add a test for an end-to-end test without MID signaling either (basically,
2205// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002206TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002207 ASSERT_TRUE(CreatePeerConnectionWrappers());
2208 ConnectFakeSignaling();
2209 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002210 caller()->AddAudioVideoTracks();
2211 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002212 // Remove SSRCs and MSIDs from the received offer SDP.
2213 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002214 caller()->CreateAndSetAndSignalOffer();
2215 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002216 MediaExpectations media_expectations;
2217 media_expectations.ExpectBidirectionalAudioAndVideo();
2218 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002219}
2220
Seth Hampson5897a6e2018-04-03 11:16:33 -07002221// Basic end-to-end test, without SSRC signaling. This means that the track
2222// was created properly and frames are delivered when the MSIDs are communicated
2223// with a=msid lines and no a=ssrc lines.
2224TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2225 EndToEndCallWithoutSsrcSignaling) {
2226 const char kStreamId[] = "streamId";
2227 ASSERT_TRUE(CreatePeerConnectionWrappers());
2228 ConnectFakeSignaling();
2229 // Add just audio tracks.
2230 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2231 callee()->AddAudioTrack();
2232
2233 // Remove SSRCs from the received offer SDP.
2234 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2235 caller()->CreateAndSetAndSignalOffer();
2236 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2237 MediaExpectations media_expectations;
2238 media_expectations.ExpectBidirectionalAudio();
2239 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2240}
2241
Steve Antondf527fd2018-04-27 15:52:03 -07002242// Tests that video flows between multiple video tracks when SSRCs are not
2243// signaled. This exercises the MID RTP header extension which is needed to
2244// demux the incoming video tracks.
2245TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2246 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2247 ASSERT_TRUE(CreatePeerConnectionWrappers());
2248 ConnectFakeSignaling();
2249 caller()->AddVideoTrack();
2250 caller()->AddVideoTrack();
2251 callee()->AddVideoTrack();
2252 callee()->AddVideoTrack();
2253
2254 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2255 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2256 caller()->CreateAndSetAndSignalOffer();
2257 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2258 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2259 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2260
2261 // Expect video to be received in both directions on both tracks.
2262 MediaExpectations media_expectations;
2263 media_expectations.ExpectBidirectionalVideo();
2264 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2265}
2266
deadbeef1dcb1642017-03-29 21:08:16 -07002267// Test that if two video tracks are sent (from caller to callee, in this test),
2268// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002269TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002270 ASSERT_TRUE(CreatePeerConnectionWrappers());
2271 ConnectFakeSignaling();
2272 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002273 caller()->AddAudioVideoTracks();
2274 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002275 caller()->CreateAndSetAndSignalOffer();
2276 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002277 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278
2279 MediaExpectations media_expectations;
2280 media_expectations.CalleeExpectsSomeAudioAndVideo();
2281 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002282}
2283
2284static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2285 bool first = true;
2286 for (cricket::ContentInfo& content : desc->contents()) {
2287 if (first) {
2288 first = false;
2289 continue;
2290 }
2291 content.bundle_only = true;
2292 }
2293 first = true;
2294 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2295 if (first) {
2296 first = false;
2297 continue;
2298 }
2299 transport.description.ice_ufrag.clear();
2300 transport.description.ice_pwd.clear();
2301 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2302 transport.description.identity_fingerprint.reset(nullptr);
2303 }
2304}
2305
2306// Test that if applying a true "max bundle" offer, which uses ports of 0,
2307// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2308// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2309// successfully and media flows.
2310// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2311// TODO(deadbeef): Won't need this test once we start generating actual
2312// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002313TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002314 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2315 ASSERT_TRUE(CreatePeerConnectionWrappers());
2316 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002317 caller()->AddAudioVideoTracks();
2318 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002319 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2320 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2321 // but the first m= section.
2322 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2323 caller()->CreateAndSetAndSignalOffer();
2324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002325 MediaExpectations media_expectations;
2326 media_expectations.ExpectBidirectionalAudioAndVideo();
2327 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002328}
2329
2330// Test that we can receive the audio output level from a remote audio track.
2331// TODO(deadbeef): Use a fake audio source and verify that the output level is
2332// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002333TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002334 ASSERT_TRUE(CreatePeerConnectionWrappers());
2335 ConnectFakeSignaling();
2336 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002337 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002338 caller()->CreateAndSetAndSignalOffer();
2339 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2340
2341 // Get the audio output level stats. Note that the level is not available
2342 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002343 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002344 kMaxWaitForFramesMs);
2345}
2346
2347// Test that an audio input level is reported.
2348// TODO(deadbeef): Use a fake audio source and verify that the input level is
2349// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002350TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002351 ASSERT_TRUE(CreatePeerConnectionWrappers());
2352 ConnectFakeSignaling();
2353 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002354 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002355 caller()->CreateAndSetAndSignalOffer();
2356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2357
2358 // Get the audio input level stats. The level should be available very
2359 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002360 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002361 kMaxWaitForStatsMs);
2362}
2363
2364// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002365TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002366 ASSERT_TRUE(CreatePeerConnectionWrappers());
2367 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002368 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002369 // Do offer/answer, wait for the callee to receive some frames.
2370 caller()->CreateAndSetAndSignalOffer();
2371 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372
2373 MediaExpectations media_expectations;
2374 media_expectations.CalleeExpectsSomeAudioAndVideo();
2375 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002376
2377 // Get a handle to the remote tracks created, so they can be used as GetStats
2378 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002379 for (auto receiver : callee()->pc()->GetReceivers()) {
2380 // We received frames, so we definitely should have nonzero "received bytes"
2381 // stats at this point.
2382 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2383 0);
2384 }
deadbeef1dcb1642017-03-29 21:08:16 -07002385}
2386
2387// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002388TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002389 ASSERT_TRUE(CreatePeerConnectionWrappers());
2390 ConnectFakeSignaling();
2391 auto audio_track = caller()->CreateLocalAudioTrack();
2392 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002393 caller()->AddTrack(audio_track);
2394 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002395 // Do offer/answer, wait for the callee to receive some frames.
2396 caller()->CreateAndSetAndSignalOffer();
2397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002398 MediaExpectations media_expectations;
2399 media_expectations.CalleeExpectsSomeAudioAndVideo();
2400 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002401
2402 // The callee received frames, so we definitely should have nonzero "sent
2403 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002404 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2405 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2406}
2407
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002408// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002409TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002410 ASSERT_TRUE(CreatePeerConnectionWrappers());
2411 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002412 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002413
Steve Anton15324772018-01-16 10:26:49 -08002414 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002415
2416 // Do offer/answer, wait for the callee to receive some frames.
2417 caller()->CreateAndSetAndSignalOffer();
2418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2419
2420 // Get the remote audio track created on the receiver, so they can be used as
2421 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002422 auto receivers = callee()->pc()->GetReceivers();
2423 ASSERT_EQ(1u, receivers.size());
2424 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002425
2426 // Get the audio output level stats. Note that the level is not available
2427 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002428 EXPECT_TRUE_WAIT(
2429 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2430 0,
2431 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002432}
2433
deadbeefd8ad7882017-04-18 16:01:17 -07002434// Test that we can get stats (using the new stats implemnetation) for
2435// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2436// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002437TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002438 GetStatsForUnsignaledStreamWithNewStatsApi) {
2439 ASSERT_TRUE(CreatePeerConnectionWrappers());
2440 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002441 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002442 // Remove SSRCs and MSIDs from the received offer SDP.
2443 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2444 caller()->CreateAndSetAndSignalOffer();
2445 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002446 MediaExpectations media_expectations;
2447 media_expectations.CalleeExpectsSomeAudio(1);
2448 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002449
2450 // We received a frame, so we should have nonzero "bytes received" stats for
2451 // the unsignaled stream, if stats are working for it.
2452 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2453 callee()->NewGetStats();
2454 ASSERT_NE(nullptr, report);
2455 auto inbound_stream_stats =
2456 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2457 ASSERT_EQ(1U, inbound_stream_stats.size());
2458 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2459 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002460 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2461}
2462
2463// Test that we can successfully get the media related stats (audio level
2464// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002465TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002466 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2467 ASSERT_TRUE(CreatePeerConnectionWrappers());
2468 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002469 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002470 // Remove SSRCs and MSIDs from the received offer SDP.
2471 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2472 caller()->CreateAndSetAndSignalOffer();
2473 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002474 MediaExpectations media_expectations;
2475 media_expectations.CalleeExpectsSomeAudio(1);
2476 media_expectations.CalleeExpectsSomeVideo(1);
2477 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002478
2479 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2480 callee()->NewGetStats();
2481 ASSERT_NE(nullptr, report);
2482
2483 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2484 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2485 ASSERT_GE(audio_index, 0);
2486 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002487}
2488
deadbeef4e2deab2017-09-20 13:56:21 -07002489// Helper for test below.
2490void ModifySsrcs(cricket::SessionDescription* desc) {
2491 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002492 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002493 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002494 for (uint32_t& ssrc : stream.ssrcs) {
2495 ssrc = rtc::CreateRandomId();
2496 }
2497 }
2498 }
2499}
2500
2501// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2502// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2503// This should result in two "RTCInboundRTPStreamStats", but only one
2504// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2505// being reset to 0 once the SSRC change occurs.
2506//
2507// Regression test for this bug:
2508// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2509//
2510// The bug causes the track stats to only represent one of the two streams:
2511// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2512// that the track stat counters would reset to 0 when the new stream is
2513// received, and a 50% chance that they'll stop updating (while
2514// "concealed_samples" continues increasing, due to silence being generated for
2515// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002516TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002517 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002518 ASSERT_TRUE(CreatePeerConnectionWrappers());
2519 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002520 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002521 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2522 // that doesn't signal SSRCs (from the callee's perspective).
2523 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002527 {
2528 MediaExpectations media_expectations;
2529 media_expectations.CalleeExpectsSomeAudio(50);
2530 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2531 }
deadbeef4e2deab2017-09-20 13:56:21 -07002532 // Some audio frames were received, so we should have nonzero "samples
2533 // received" for the track.
2534 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2535 callee()->NewGetStats();
2536 ASSERT_NE(nullptr, report);
2537 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2538 ASSERT_EQ(1U, track_stats.size());
2539 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2540 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2541 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2542
2543 // Create a new offer and munge it to cause the caller to use a new SSRC.
2544 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2545 caller()->CreateAndSetAndSignalOffer();
2546 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2547 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2548 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002549 {
2550 MediaExpectations media_expectations;
2551 media_expectations.CalleeExpectsSomeAudio(25);
2552 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2553 }
deadbeef4e2deab2017-09-20 13:56:21 -07002554
2555 report = callee()->NewGetStats();
2556 ASSERT_NE(nullptr, report);
2557 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2558 ASSERT_EQ(1U, track_stats.size());
2559 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2560 // The "total samples received" stat should only be greater than it was
2561 // before.
2562 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2563 // Right now, the new SSRC will cause the counters to reset to 0.
2564 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2565
2566 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002567 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002568 // good sign that we're seeing stats from the old stream that's no longer
2569 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002570 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002571 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2572 EXPECT_LT(*track_stats[0]->concealed_samples,
2573 *track_stats[0]->total_samples_received *
2574 kAcceptableConcealedSamplesPercentage);
2575
2576 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2577 // sanity check that the SSRC really changed.
2578 // TODO(deadbeef): This isn't working right now, because we're not returning
2579 // *any* stats for the inactive stream. Uncomment when the bug is completely
2580 // fixed.
2581 // auto inbound_stream_stats =
2582 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2583 // ASSERT_EQ(2U, inbound_stream_stats.size());
2584}
2585
deadbeef1dcb1642017-03-29 21:08:16 -07002586// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002587TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002588 PeerConnectionFactory::Options dtls_10_options;
2589 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2590 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2591 dtls_10_options));
2592 ConnectFakeSignaling();
2593 // Do normal offer/answer and wait for some frames to be received in each
2594 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002595 caller()->AddAudioVideoTracks();
2596 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002597 caller()->CreateAndSetAndSignalOffer();
2598 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002599 MediaExpectations media_expectations;
2600 media_expectations.ExpectBidirectionalAudioAndVideo();
2601 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002602}
2603
2604// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002605TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002606 PeerConnectionFactory::Options dtls_10_options;
2607 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2608 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2609 dtls_10_options));
2610 ConnectFakeSignaling();
2611 // Register UMA observer before signaling begins.
2612 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2613 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2614 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002615 caller()->AddAudioVideoTracks();
2616 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002617 caller()->CreateAndSetAndSignalOffer();
2618 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2619 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002620 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002621 kDefaultTimeout);
2622 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002623 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002624 EXPECT_EQ(1,
2625 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2626 kDefaultSrtpCryptoSuite));
2627}
2628
2629// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002630TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002631 PeerConnectionFactory::Options dtls_12_options;
2632 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2633 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2634 dtls_12_options));
2635 ConnectFakeSignaling();
2636 // Register UMA observer before signaling begins.
2637 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2638 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2639 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002640 caller()->AddAudioVideoTracks();
2641 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002642 caller()->CreateAndSetAndSignalOffer();
2643 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2644 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002645 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002646 kDefaultTimeout);
2647 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002648 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002649 EXPECT_EQ(1,
2650 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2651 kDefaultSrtpCryptoSuite));
2652}
2653
2654// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2655// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002656TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002657 PeerConnectionFactory::Options caller_options;
2658 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2659 PeerConnectionFactory::Options callee_options;
2660 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2661 ASSERT_TRUE(
2662 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2663 ConnectFakeSignaling();
2664 // Do normal offer/answer and wait for some frames to be received in each
2665 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002666 caller()->AddAudioVideoTracks();
2667 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002668 caller()->CreateAndSetAndSignalOffer();
2669 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002670 MediaExpectations media_expectations;
2671 media_expectations.ExpectBidirectionalAudioAndVideo();
2672 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002673}
2674
2675// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2676// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002677TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002678 PeerConnectionFactory::Options caller_options;
2679 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2680 PeerConnectionFactory::Options callee_options;
2681 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2682 ASSERT_TRUE(
2683 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2684 ConnectFakeSignaling();
2685 // Do normal offer/answer and wait for some frames to be received in each
2686 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002687 caller()->AddAudioVideoTracks();
2688 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002689 caller()->CreateAndSetAndSignalOffer();
2690 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002691 MediaExpectations media_expectations;
2692 media_expectations.ExpectBidirectionalAudioAndVideo();
2693 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002694}
2695
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002696// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2697// works as expected; the cipher should only be used if enabled by both sides.
2698TEST_P(PeerConnectionIntegrationTest,
2699 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2700 PeerConnectionFactory::Options caller_options;
2701 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2702 PeerConnectionFactory::Options callee_options;
2703 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2704 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2705 TestNegotiatedCipherSuite(caller_options, callee_options,
2706 expected_cipher_suite);
2707}
2708
2709TEST_P(PeerConnectionIntegrationTest,
2710 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2711 PeerConnectionFactory::Options caller_options;
2712 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2713 PeerConnectionFactory::Options callee_options;
2714 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2715 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2716 TestNegotiatedCipherSuite(caller_options, callee_options,
2717 expected_cipher_suite);
2718}
2719
2720TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2721 PeerConnectionFactory::Options caller_options;
2722 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2723 PeerConnectionFactory::Options callee_options;
2724 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2725 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2726 TestNegotiatedCipherSuite(caller_options, callee_options,
2727 expected_cipher_suite);
2728}
2729
deadbeef1dcb1642017-03-29 21:08:16 -07002730// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002731TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002732 bool local_gcm_enabled = false;
2733 bool remote_gcm_enabled = false;
2734 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2735 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2736 expected_cipher_suite);
2737}
2738
2739// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002740TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002741 bool local_gcm_enabled = true;
2742 bool remote_gcm_enabled = true;
2743 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2744 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2745 expected_cipher_suite);
2746}
2747
2748// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002749TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002750 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2751 bool local_gcm_enabled = true;
2752 bool remote_gcm_enabled = false;
2753 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2754 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2755 expected_cipher_suite);
2756}
2757
2758// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002759TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002760 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2761 bool local_gcm_enabled = false;
2762 bool remote_gcm_enabled = true;
2763 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2764 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2765 expected_cipher_suite);
2766}
2767
deadbeef7914b8c2017-04-21 03:23:33 -07002768// Verify that media can be transmitted end-to-end when GCM crypto suites are
2769// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2770// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2771// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002772TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002773 PeerConnectionFactory::Options gcm_options;
2774 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2775 ASSERT_TRUE(
2776 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2777 ConnectFakeSignaling();
2778 // Do normal offer/answer and wait for some frames to be received in each
2779 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002780 caller()->AddAudioVideoTracks();
2781 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002782 caller()->CreateAndSetAndSignalOffer();
2783 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002784 MediaExpectations media_expectations;
2785 media_expectations.ExpectBidirectionalAudioAndVideo();
2786 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002787}
2788
deadbeef1dcb1642017-03-29 21:08:16 -07002789// This test sets up a call between two parties with audio, video and an RTP
2790// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002791TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002792 FakeConstraints setup_constraints;
2793 setup_constraints.SetAllowRtpDataChannels();
2794 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2795 &setup_constraints));
2796 ConnectFakeSignaling();
2797 // Expect that data channel created on caller side will show up for callee as
2798 // well.
2799 caller()->CreateDataChannel();
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);
2804 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002805 MediaExpectations media_expectations;
2806 media_expectations.ExpectBidirectionalAudioAndVideo();
2807 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002808 ASSERT_NE(nullptr, caller()->data_channel());
2809 ASSERT_NE(nullptr, callee()->data_channel());
2810 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2811 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2812
2813 // Ensure data can be sent in both directions.
2814 std::string data = "hello world";
2815 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2816 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2817 kDefaultTimeout);
2818 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2819 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2820 kDefaultTimeout);
2821}
2822
2823// Ensure that an RTP data channel is signaled as closed for the caller when
2824// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002825TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002826 RtpDataChannelSignaledClosedInCalleeOffer) {
2827 // Same procedure as above test.
2828 FakeConstraints setup_constraints;
2829 setup_constraints.SetAllowRtpDataChannels();
2830 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2831 &setup_constraints));
2832 ConnectFakeSignaling();
2833 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002834 caller()->AddAudioVideoTracks();
2835 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002836 caller()->CreateAndSetAndSignalOffer();
2837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2838 ASSERT_NE(nullptr, caller()->data_channel());
2839 ASSERT_NE(nullptr, callee()->data_channel());
2840 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2841 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2842
2843 // Close the data channel on the callee, and do an updated offer/answer.
2844 callee()->data_channel()->Close();
2845 callee()->CreateAndSetAndSignalOffer();
2846 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2847 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2848 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2849}
2850
2851// Tests that data is buffered in an RTP data channel until an observer is
2852// registered for it.
2853//
2854// NOTE: RTP data channels can receive data before the underlying
2855// transport has detected that a channel is writable and thus data can be
2856// received before the data channel state changes to open. That is hard to test
2857// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002858TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002859 DataBufferedUntilRtpDataChannelObserverRegistered) {
2860 // Use fake clock and simulated network delay so that we predictably can wait
2861 // until an SCTP message has been delivered without "sleep()"ing.
2862 rtc::ScopedFakeClock fake_clock;
2863 // Some things use a time of "0" as a special value, so we need to start out
2864 // the fake clock at a nonzero time.
2865 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02002866 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07002867 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2868 virtual_socket_server()->UpdateDelayDistribution();
2869
2870 FakeConstraints constraints;
2871 constraints.SetAllowRtpDataChannels();
2872 ASSERT_TRUE(
2873 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2874 ConnectFakeSignaling();
2875 caller()->CreateDataChannel();
2876 caller()->CreateAndSetAndSignalOffer();
2877 ASSERT_TRUE(caller()->data_channel() != nullptr);
2878 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2879 kDefaultTimeout, fake_clock);
2880 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2881 kDefaultTimeout, fake_clock);
2882 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2883 callee()->data_channel()->state(), kDefaultTimeout,
2884 fake_clock);
2885
2886 // Unregister the observer which is normally automatically registered.
2887 callee()->data_channel()->UnregisterObserver();
2888 // Send data and advance fake clock until it should have been received.
2889 std::string data = "hello world";
2890 caller()->data_channel()->Send(DataBuffer(data));
2891 SIMULATED_WAIT(false, 50, fake_clock);
2892
2893 // Attach data channel and expect data to be received immediately. Note that
2894 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2895 // further, but data can be received even if the callback is asynchronous.
2896 MockDataChannelObserver new_observer(callee()->data_channel());
2897 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2898 fake_clock);
2899}
2900
2901// This test sets up a call between two parties with audio, video and but only
2902// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002903TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002904 FakeConstraints setup_constraints_1;
2905 setup_constraints_1.SetAllowRtpDataChannels();
2906 // Must disable DTLS to make negotiation succeed.
2907 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2908 false);
2909 FakeConstraints setup_constraints_2;
2910 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2911 false);
2912 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2913 &setup_constraints_1, &setup_constraints_2));
2914 ConnectFakeSignaling();
2915 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002916 caller()->AddAudioVideoTracks();
2917 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002918 caller()->CreateAndSetAndSignalOffer();
2919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2920 // The caller should still have a data channel, but it should be closed, and
2921 // one should ever have been created for the callee.
2922 EXPECT_TRUE(caller()->data_channel() != nullptr);
2923 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2924 EXPECT_EQ(nullptr, callee()->data_channel());
2925}
2926
2927// This test sets up a call between two parties with audio, and video. When
2928// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002929TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002930 FakeConstraints setup_constraints;
2931 setup_constraints.SetAllowRtpDataChannels();
2932 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2933 &setup_constraints));
2934 ConnectFakeSignaling();
2935 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002936 caller()->AddAudioVideoTracks();
2937 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002938 caller()->CreateAndSetAndSignalOffer();
2939 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2940 // Create data channel and do new offer and answer.
2941 caller()->CreateDataChannel();
2942 caller()->CreateAndSetAndSignalOffer();
2943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2944 ASSERT_NE(nullptr, caller()->data_channel());
2945 ASSERT_NE(nullptr, callee()->data_channel());
2946 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2947 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2948 // Ensure data can be sent in both directions.
2949 std::string data = "hello world";
2950 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2951 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2952 kDefaultTimeout);
2953 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2954 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2955 kDefaultTimeout);
2956}
2957
2958#ifdef HAVE_SCTP
2959
2960// This test sets up a call between two parties with audio, video and an SCTP
2961// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002963 ASSERT_TRUE(CreatePeerConnectionWrappers());
2964 ConnectFakeSignaling();
2965 // Expect that data channel created on caller side will show up for callee as
2966 // well.
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 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002973 MediaExpectations media_expectations;
2974 media_expectations.ExpectBidirectionalAudioAndVideo();
2975 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002976 // Caller data channel should already exist (it created one). Callee data
2977 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2978 ASSERT_NE(nullptr, caller()->data_channel());
2979 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2980 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2981 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2982
2983 // Ensure data can be sent in both directions.
2984 std::string data = "hello world";
2985 caller()->data_channel()->Send(DataBuffer(data));
2986 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2987 kDefaultTimeout);
2988 callee()->data_channel()->Send(DataBuffer(data));
2989 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2990 kDefaultTimeout);
2991}
2992
2993// Ensure that when the callee closes an SCTP data channel, the closing
2994// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002995TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002996 // Same procedure as above test.
2997 ASSERT_TRUE(CreatePeerConnectionWrappers());
2998 ConnectFakeSignaling();
2999 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003000 caller()->AddAudioVideoTracks();
3001 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003002 caller()->CreateAndSetAndSignalOffer();
3003 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3004 ASSERT_NE(nullptr, caller()->data_channel());
3005 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3006 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3007 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3008
3009 // Close the data channel on the callee side, and wait for it to reach the
3010 // "closed" state on both sides.
3011 callee()->data_channel()->Close();
3012 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3013 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3014}
3015
Seth Hampson2f0d7022018-02-20 11:54:42 -08003016TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003017 ASSERT_TRUE(CreatePeerConnectionWrappers());
3018 ConnectFakeSignaling();
3019 webrtc::DataChannelInit init;
3020 init.id = 53;
3021 init.maxRetransmits = 52;
3022 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003023 caller()->AddAudioVideoTracks();
3024 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003025 caller()->CreateAndSetAndSignalOffer();
3026 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003027 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3028 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003029 EXPECT_EQ(init.id, callee()->data_channel()->id());
3030 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3031 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3032 EXPECT_FALSE(callee()->data_channel()->negotiated());
3033}
3034
deadbeef1dcb1642017-03-29 21:08:16 -07003035// Test usrsctp's ability to process unordered data stream, where data actually
3036// arrives out of order using simulated delays. Previously there have been some
3037// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003038TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003039 // Introduce random network delays.
3040 // Otherwise it's not a true "unordered" test.
3041 virtual_socket_server()->set_delay_mean(20);
3042 virtual_socket_server()->set_delay_stddev(5);
3043 virtual_socket_server()->UpdateDelayDistribution();
3044 // Normal procedure, but with unordered data channel config.
3045 ASSERT_TRUE(CreatePeerConnectionWrappers());
3046 ConnectFakeSignaling();
3047 webrtc::DataChannelInit init;
3048 init.ordered = false;
3049 caller()->CreateDataChannel(&init);
3050 caller()->CreateAndSetAndSignalOffer();
3051 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3052 ASSERT_NE(nullptr, caller()->data_channel());
3053 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3054 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3055 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3056
3057 static constexpr int kNumMessages = 100;
3058 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3059 static constexpr size_t kMaxMessageSize = 4096;
3060 // Create and send random messages.
3061 std::vector<std::string> sent_messages;
3062 for (int i = 0; i < kNumMessages; ++i) {
3063 size_t length =
3064 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3065 std::string message;
3066 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3067 caller()->data_channel()->Send(DataBuffer(message));
3068 callee()->data_channel()->Send(DataBuffer(message));
3069 sent_messages.push_back(message);
3070 }
3071
3072 // Wait for all messages to be received.
3073 EXPECT_EQ_WAIT(kNumMessages,
3074 caller()->data_observer()->received_message_count(),
3075 kDefaultTimeout);
3076 EXPECT_EQ_WAIT(kNumMessages,
3077 callee()->data_observer()->received_message_count(),
3078 kDefaultTimeout);
3079
3080 // Sort and compare to make sure none of the messages were corrupted.
3081 std::vector<std::string> caller_received_messages =
3082 caller()->data_observer()->messages();
3083 std::vector<std::string> callee_received_messages =
3084 callee()->data_observer()->messages();
3085 std::sort(sent_messages.begin(), sent_messages.end());
3086 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3087 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3088 EXPECT_EQ(sent_messages, caller_received_messages);
3089 EXPECT_EQ(sent_messages, callee_received_messages);
3090}
3091
3092// This test sets up a call between two parties with audio, and video. When
3093// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003094TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003095 ASSERT_TRUE(CreatePeerConnectionWrappers());
3096 ConnectFakeSignaling();
3097 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003098 caller()->AddAudioVideoTracks();
3099 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003100 caller()->CreateAndSetAndSignalOffer();
3101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3102 // Create data channel and do new offer and answer.
3103 caller()->CreateDataChannel();
3104 caller()->CreateAndSetAndSignalOffer();
3105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3106 // Caller data channel should already exist (it created one). Callee data
3107 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3108 ASSERT_NE(nullptr, caller()->data_channel());
3109 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3110 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3111 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3112 // Ensure data can be sent in both directions.
3113 std::string data = "hello world";
3114 caller()->data_channel()->Send(DataBuffer(data));
3115 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3116 kDefaultTimeout);
3117 callee()->data_channel()->Send(DataBuffer(data));
3118 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3119 kDefaultTimeout);
3120}
3121
deadbeef7914b8c2017-04-21 03:23:33 -07003122// Set up a connection initially just using SCTP data channels, later upgrading
3123// to audio/video, ensuring frames are received end-to-end. Effectively the
3124// inverse of the test above.
3125// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003126TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003127 ASSERT_TRUE(CreatePeerConnectionWrappers());
3128 ConnectFakeSignaling();
3129 // Do initial offer/answer with just data channel.
3130 caller()->CreateDataChannel();
3131 caller()->CreateAndSetAndSignalOffer();
3132 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3133 // Wait until data can be sent over the data channel.
3134 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3135 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3136 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3137
3138 // Do subsequent offer/answer with two-way audio and video. Audio and video
3139 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003140 caller()->AddAudioVideoTracks();
3141 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003142 caller()->CreateAndSetAndSignalOffer();
3143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003144 MediaExpectations media_expectations;
3145 media_expectations.ExpectBidirectionalAudioAndVideo();
3146 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003147}
3148
deadbeef8b7e9ad2017-05-25 09:38:55 -07003149static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003150 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003151 GetFirstDataContentDescription(desc);
3152 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003153 dcd_offer->set_use_sctpmap(false);
3154 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3155}
3156
3157// Test that the data channel works when a spec-compliant SCTP m= section is
3158// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3159// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003160TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003161 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3162 ASSERT_TRUE(CreatePeerConnectionWrappers());
3163 ConnectFakeSignaling();
3164 caller()->CreateDataChannel();
3165 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3166 caller()->CreateAndSetAndSignalOffer();
3167 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3168 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3169 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3170 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3171
3172 // Ensure data can be sent in both directions.
3173 std::string data = "hello world";
3174 caller()->data_channel()->Send(DataBuffer(data));
3175 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3176 kDefaultTimeout);
3177 callee()->data_channel()->Send(DataBuffer(data));
3178 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3179 kDefaultTimeout);
3180}
3181
deadbeef1dcb1642017-03-29 21:08:16 -07003182#endif // HAVE_SCTP
3183
3184// Test that the ICE connection and gathering states eventually reach
3185// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003186TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003187 ASSERT_TRUE(CreatePeerConnectionWrappers());
3188 ConnectFakeSignaling();
3189 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003190 caller()->AddAudioVideoTracks();
3191 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003192 caller()->CreateAndSetAndSignalOffer();
3193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3194 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3195 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3196 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3197 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3198 // After the best candidate pair is selected and all candidates are signaled,
3199 // the ICE connection state should reach "complete".
3200 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3201 // answerer/"callee" by default) only reaches "connected". When this is
3202 // fixed, this test should be updated.
3203 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3204 caller()->ice_connection_state(), kDefaultTimeout);
3205 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3206 callee()->ice_connection_state(), kDefaultTimeout);
3207}
3208
Steve Antonede9ca52017-10-16 13:04:27 -07003209// Test that firewalling the ICE connection causes the clients to identify the
3210// disconnected state and then removing the firewall causes them to reconnect.
3211class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003212 : public PeerConnectionIntegrationBaseTest,
3213 public ::testing::WithParamInterface<
3214 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003215 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003216 PeerConnectionIntegrationIceStatesTest()
3217 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3218 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003219 }
3220
3221 void StartStunServer(const SocketAddress& server_address) {
3222 stun_server_.reset(
3223 cricket::TestStunServer::Create(network_thread(), server_address));
3224 }
3225
3226 bool TestIPv6() {
3227 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3228 }
3229
3230 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003231 network_thread()->Invoke<void>(
3232 RTC_FROM_HERE,
3233 rtc::Bind(&cricket::PortAllocator::set_flags,
3234 caller()->port_allocator(), port_allocator_flags_));
3235 network_thread()->Invoke<void>(
3236 RTC_FROM_HERE,
3237 rtc::Bind(&cricket::PortAllocator::set_flags,
3238 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003239 }
3240
3241 std::vector<SocketAddress> CallerAddresses() {
3242 std::vector<SocketAddress> addresses;
3243 addresses.push_back(SocketAddress("1.1.1.1", 0));
3244 if (TestIPv6()) {
3245 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3246 }
3247 return addresses;
3248 }
3249
3250 std::vector<SocketAddress> CalleeAddresses() {
3251 std::vector<SocketAddress> addresses;
3252 addresses.push_back(SocketAddress("2.2.2.2", 0));
3253 if (TestIPv6()) {
3254 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3255 }
3256 return addresses;
3257 }
3258
3259 void SetUpNetworkInterfaces() {
3260 // Remove the default interfaces added by the test infrastructure.
3261 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3262 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3263
3264 // Add network addresses for test.
3265 for (const auto& caller_address : CallerAddresses()) {
3266 caller()->network()->AddInterface(caller_address);
3267 }
3268 for (const auto& callee_address : CalleeAddresses()) {
3269 callee()->network()->AddInterface(callee_address);
3270 }
3271 }
3272
3273 private:
3274 uint32_t port_allocator_flags_;
3275 std::unique_ptr<cricket::TestStunServer> stun_server_;
3276};
3277
3278// Tests that the PeerConnection goes through all the ICE gathering/connection
3279// states over the duration of the call. This includes Disconnected and Failed
3280// states, induced by putting a firewall between the peers and waiting for them
3281// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003282TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3283 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3284 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3285 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003286
3287 const SocketAddress kStunServerAddress =
3288 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3289 StartStunServer(kStunServerAddress);
3290
3291 PeerConnectionInterface::RTCConfiguration config;
3292 PeerConnectionInterface::IceServer ice_stun_server;
3293 ice_stun_server.urls.push_back(
3294 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3295 kStunServerAddress.PortAsString());
3296 config.servers.push_back(ice_stun_server);
3297
3298 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3299 ConnectFakeSignaling();
3300 SetPortAllocatorFlags();
3301 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003302 caller()->AddAudioVideoTracks();
3303 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003304
3305 // Initial state before anything happens.
3306 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3307 caller()->ice_gathering_state());
3308 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3309 caller()->ice_connection_state());
3310
3311 // Start the call by creating the offer, setting it as the local description,
3312 // then sending it to the peer who will respond with an answer. This happens
3313 // asynchronously so that we can watch the states as it runs in the
3314 // background.
3315 caller()->CreateAndSetAndSignalOffer();
3316
Steve Anton83119dd2017-11-10 16:19:52 -08003317 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3318 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003319
3320 // Verify that the observer was notified of the intermediate transitions.
3321 EXPECT_THAT(caller()->ice_connection_state_history(),
3322 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3323 PeerConnectionInterface::kIceConnectionConnected,
3324 PeerConnectionInterface::kIceConnectionCompleted));
3325 EXPECT_THAT(caller()->ice_gathering_state_history(),
3326 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3327 PeerConnectionInterface::kIceGatheringComplete));
3328
3329 // Block connections to/from the caller and wait for ICE to become
3330 // disconnected.
3331 for (const auto& caller_address : CallerAddresses()) {
3332 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3333 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003334 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003335 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3336 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003337
3338 // Let ICE re-establish by removing the firewall rules.
3339 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003340 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003341 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3342 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003343
3344 // According to RFC7675, if there is no response within 30 seconds then the
3345 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003346 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003347 constexpr int kConsentTimeout = 30000;
3348 for (const auto& caller_address : CallerAddresses()) {
3349 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3350 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003351 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003352 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3353 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003354}
3355
3356// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3357// and that the statistics in the metric observers are updated correctly.
3358TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3359 ASSERT_TRUE(CreatePeerConnectionWrappers());
3360 ConnectFakeSignaling();
3361 SetPortAllocatorFlags();
3362 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003363 caller()->AddAudioVideoTracks();
3364 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003365
3366 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3367 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3368 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3369
3370 caller()->CreateAndSetAndSignalOffer();
3371
3372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3373
3374 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3375 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3376 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3377 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3378 if (TestIPv6()) {
3379 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3380 // connection.
3381 EXPECT_EQ(0u, num_best_ipv4);
3382 EXPECT_EQ(1u, num_best_ipv6);
3383 } else {
3384 EXPECT_EQ(1u, num_best_ipv4);
3385 EXPECT_EQ(0u, num_best_ipv6);
3386 }
3387
3388 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3389 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3390 webrtc::kIceCandidatePairHostHost));
3391 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3392 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3393 webrtc::kIceCandidatePairHostPublicHostPublic));
3394}
3395
3396constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3397 cricket::PORTALLOCATOR_DISABLE_STUN |
3398 cricket::PORTALLOCATOR_DISABLE_RELAY;
3399constexpr uint32_t kFlagsIPv6NoStun =
3400 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3401 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3402constexpr uint32_t kFlagsIPv4Stun =
3403 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3404
Seth Hampson2f0d7022018-02-20 11:54:42 -08003405INSTANTIATE_TEST_CASE_P(
3406 PeerConnectionIntegrationTest,
3407 PeerConnectionIntegrationIceStatesTest,
3408 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3409 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3410 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3411 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003412
deadbeef1dcb1642017-03-29 21:08:16 -07003413// This test sets up a call between two parties with audio and video.
3414// During the call, the caller restarts ICE and the test verifies that
3415// new ICE candidates are generated and audio and video still can flow, and the
3416// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003417TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003418 ASSERT_TRUE(CreatePeerConnectionWrappers());
3419 ConnectFakeSignaling();
3420 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003421 caller()->AddAudioVideoTracks();
3422 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003423 caller()->CreateAndSetAndSignalOffer();
3424 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3425 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3426 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3427 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3428 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3429
3430 // To verify that the ICE restart actually occurs, get
3431 // ufrag/password/candidates before and after restart.
3432 // Create an SDP string of the first audio candidate for both clients.
3433 const webrtc::IceCandidateCollection* audio_candidates_caller =
3434 caller()->pc()->local_description()->candidates(0);
3435 const webrtc::IceCandidateCollection* audio_candidates_callee =
3436 callee()->pc()->local_description()->candidates(0);
3437 ASSERT_GT(audio_candidates_caller->count(), 0u);
3438 ASSERT_GT(audio_candidates_callee->count(), 0u);
3439 std::string caller_candidate_pre_restart;
3440 ASSERT_TRUE(
3441 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3442 std::string callee_candidate_pre_restart;
3443 ASSERT_TRUE(
3444 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3445 const cricket::SessionDescription* desc =
3446 caller()->pc()->local_description()->description();
3447 std::string caller_ufrag_pre_restart =
3448 desc->transport_infos()[0].description.ice_ufrag;
3449 desc = callee()->pc()->local_description()->description();
3450 std::string callee_ufrag_pre_restart =
3451 desc->transport_infos()[0].description.ice_ufrag;
3452
3453 // Have the caller initiate an ICE restart.
3454 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3455 caller()->CreateAndSetAndSignalOffer();
3456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3457 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3458 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3459 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3460 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3461
3462 // Grab the ufrags/candidates again.
3463 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3464 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3465 ASSERT_GT(audio_candidates_caller->count(), 0u);
3466 ASSERT_GT(audio_candidates_callee->count(), 0u);
3467 std::string caller_candidate_post_restart;
3468 ASSERT_TRUE(
3469 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3470 std::string callee_candidate_post_restart;
3471 ASSERT_TRUE(
3472 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3473 desc = caller()->pc()->local_description()->description();
3474 std::string caller_ufrag_post_restart =
3475 desc->transport_infos()[0].description.ice_ufrag;
3476 desc = callee()->pc()->local_description()->description();
3477 std::string callee_ufrag_post_restart =
3478 desc->transport_infos()[0].description.ice_ufrag;
3479 // Sanity check that an ICE restart was actually negotiated in SDP.
3480 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3481 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3482 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3483 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3484
3485 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003486 MediaExpectations media_expectations;
3487 media_expectations.ExpectBidirectionalAudioAndVideo();
3488 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003489}
3490
3491// Verify that audio/video can be received end-to-end when ICE renomination is
3492// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003493TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003494 PeerConnectionInterface::RTCConfiguration config;
3495 config.enable_ice_renomination = true;
3496 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3497 ConnectFakeSignaling();
3498 // Do normal offer/answer and wait for some frames to be received in each
3499 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003500 caller()->AddAudioVideoTracks();
3501 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003502 caller()->CreateAndSetAndSignalOffer();
3503 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3504 // Sanity check that ICE renomination was actually negotiated.
3505 const cricket::SessionDescription* desc =
3506 caller()->pc()->local_description()->description();
3507 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003508 ASSERT_NE(
3509 info.description.transport_options.end(),
3510 std::find(info.description.transport_options.begin(),
3511 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003512 }
3513 desc = callee()->pc()->local_description()->description();
3514 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003515 ASSERT_NE(
3516 info.description.transport_options.end(),
3517 std::find(info.description.transport_options.begin(),
3518 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003519 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003520 MediaExpectations media_expectations;
3521 media_expectations.ExpectBidirectionalAudioAndVideo();
3522 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003523}
3524
Steve Anton6f25b092017-10-23 09:39:20 -07003525// With a max bundle policy and RTCP muxing, adding a new media description to
3526// the connection should not affect ICE at all because the new media will use
3527// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003528TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003529 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003530 PeerConnectionInterface::RTCConfiguration config;
3531 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3532 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3533 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3534 config, PeerConnectionInterface::RTCConfiguration()));
3535 ConnectFakeSignaling();
3536
Steve Anton15324772018-01-16 10:26:49 -08003537 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003538 caller()->CreateAndSetAndSignalOffer();
3539 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003540 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3541 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003542
3543 caller()->clear_ice_connection_state_history();
3544
Steve Anton15324772018-01-16 10:26:49 -08003545 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003546 caller()->CreateAndSetAndSignalOffer();
3547 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3548
3549 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3550}
3551
deadbeef1dcb1642017-03-29 21:08:16 -07003552// This test sets up a call between two parties with audio and video. It then
3553// renegotiates setting the video m-line to "port 0", then later renegotiates
3554// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003555TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003556 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3557 ASSERT_TRUE(CreatePeerConnectionWrappers());
3558 ConnectFakeSignaling();
3559
3560 // Do initial negotiation, only sending media from the caller. Will result in
3561 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003562 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003563 caller()->CreateAndSetAndSignalOffer();
3564 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3565
3566 // Negotiate again, disabling the video "m=" section (the callee will set the
3567 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003568 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3569 PeerConnectionInterface::RTCOfferAnswerOptions options;
3570 options.offer_to_receive_video = 0;
3571 callee()->SetOfferAnswerOptions(options);
3572 } else {
3573 callee()->SetRemoteOfferHandler([this] {
3574 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3575 });
3576 }
deadbeef1dcb1642017-03-29 21:08:16 -07003577 caller()->CreateAndSetAndSignalOffer();
3578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3579 // Sanity check that video "m=" section was actually rejected.
3580 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3581 callee()->pc()->local_description()->description());
3582 ASSERT_NE(nullptr, answer_video_content);
3583 ASSERT_TRUE(answer_video_content->rejected);
3584
3585 // Enable video and do negotiation again, making sure video is received
3586 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003587 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3588 PeerConnectionInterface::RTCOfferAnswerOptions options;
3589 options.offer_to_receive_video = 1;
3590 callee()->SetOfferAnswerOptions(options);
3591 } else {
3592 // The caller's transceiver is stopped, so we need to add another track.
3593 auto caller_transceiver =
3594 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3595 EXPECT_TRUE(caller_transceiver->stopped());
3596 caller()->AddVideoTrack();
3597 }
3598 callee()->AddVideoTrack();
3599 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003600 caller()->CreateAndSetAndSignalOffer();
3601 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003602
deadbeef1dcb1642017-03-29 21:08:16 -07003603 // Verify the caller receives frames from the newly added stream, and the
3604 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003605 MediaExpectations media_expectations;
3606 media_expectations.CalleeExpectsSomeAudio();
3607 media_expectations.ExpectBidirectionalVideo();
3608 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003609}
3610
deadbeef1dcb1642017-03-29 21:08:16 -07003611// This tests that if we negotiate after calling CreateSender but before we
3612// have a track, then set a track later, frames from the newly-set track are
3613// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003614TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003615 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3616 ASSERT_TRUE(CreatePeerConnectionWrappers());
3617 ConnectFakeSignaling();
3618 auto caller_audio_sender =
3619 caller()->pc()->CreateSender("audio", "caller_stream");
3620 auto caller_video_sender =
3621 caller()->pc()->CreateSender("video", "caller_stream");
3622 auto callee_audio_sender =
3623 callee()->pc()->CreateSender("audio", "callee_stream");
3624 auto callee_video_sender =
3625 callee()->pc()->CreateSender("video", "callee_stream");
3626 caller()->CreateAndSetAndSignalOffer();
3627 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3628 // Wait for ICE to complete, without any tracks being set.
3629 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3630 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3631 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3632 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3633 // Now set the tracks, and expect frames to immediately start flowing.
3634 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3635 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3636 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3637 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003638 MediaExpectations media_expectations;
3639 media_expectations.ExpectBidirectionalAudioAndVideo();
3640 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3641}
3642
3643// This tests that if we negotiate after calling AddTransceiver but before we
3644// have a track, then set a track later, frames from the newly-set tracks are
3645// received end-to-end.
3646TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3647 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3648 ASSERT_TRUE(CreatePeerConnectionWrappers());
3649 ConnectFakeSignaling();
3650 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3651 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3652 auto caller_audio_sender = audio_result.MoveValue()->sender();
3653 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3654 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3655 auto caller_video_sender = video_result.MoveValue()->sender();
3656 callee()->SetRemoteOfferHandler([this] {
3657 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3658 callee()->pc()->GetTransceivers()[0]->SetDirection(
3659 RtpTransceiverDirection::kSendRecv);
3660 callee()->pc()->GetTransceivers()[1]->SetDirection(
3661 RtpTransceiverDirection::kSendRecv);
3662 });
3663 caller()->CreateAndSetAndSignalOffer();
3664 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3665 // Wait for ICE to complete, without any tracks being set.
3666 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3667 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3668 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3669 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3670 // Now set the tracks, and expect frames to immediately start flowing.
3671 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3672 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3673 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3674 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3675 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3676 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3677 MediaExpectations media_expectations;
3678 media_expectations.ExpectBidirectionalAudioAndVideo();
3679 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003680}
3681
3682// This test verifies that a remote video track can be added via AddStream,
3683// and sent end-to-end. For this particular test, it's simply echoed back
3684// from the caller to the callee, rather than being forwarded to a third
3685// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003686TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003687 ASSERT_TRUE(CreatePeerConnectionWrappers());
3688 ConnectFakeSignaling();
3689 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003690 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003691 caller()->CreateAndSetAndSignalOffer();
3692 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3693 ASSERT_EQ(1, callee()->remote_streams()->count());
3694
3695 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3696 // time).
3697 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3698 callee()->CreateAndSetAndSignalOffer();
3699 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3700
Seth Hampson2f0d7022018-02-20 11:54:42 -08003701 MediaExpectations media_expectations;
3702 media_expectations.ExpectBidirectionalVideo();
3703 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003704}
3705
3706// Test that we achieve the expected end-to-end connection time, using a
3707// fake clock and simulated latency on the media and signaling paths.
3708// We use a TURN<->TURN connection because this is usually the quickest to
3709// set up initially, especially when we're confident the connection will work
3710// and can start sending media before we get a STUN response.
3711//
3712// With various optimizations enabled, here are the network delays we expect to
3713// be on the critical path:
3714// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3715// signaling answer (with DTLS fingerprint).
3716// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3717// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3718// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003719TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003720 rtc::ScopedFakeClock fake_clock;
3721 // Some things use a time of "0" as a special value, so we need to start out
3722 // the fake clock at a nonzero time.
3723 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003724 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003725
3726 static constexpr int media_hop_delay_ms = 50;
3727 static constexpr int signaling_trip_delay_ms = 500;
3728 // For explanation of these values, see comment above.
3729 static constexpr int required_media_hops = 9;
3730 static constexpr int required_signaling_trips = 2;
3731 // For internal delays (such as posting an event asychronously).
3732 static constexpr int allowed_internal_delay_ms = 20;
3733 static constexpr int total_connection_time_ms =
3734 media_hop_delay_ms * required_media_hops +
3735 signaling_trip_delay_ms * required_signaling_trips +
3736 allowed_internal_delay_ms;
3737
3738 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3739 3478};
3740 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3741 0};
3742 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3743 3478};
3744 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3745 0};
3746 cricket::TestTurnServer turn_server_1(network_thread(),
3747 turn_server_1_internal_address,
3748 turn_server_1_external_address);
3749 cricket::TestTurnServer turn_server_2(network_thread(),
3750 turn_server_2_internal_address,
3751 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003752
deadbeef1dcb1642017-03-29 21:08:16 -07003753 // Bypass permission check on received packets so media can be sent before
3754 // the candidate is signaled.
3755 turn_server_1.set_enable_permission_checks(false);
3756 turn_server_2.set_enable_permission_checks(false);
3757
3758 PeerConnectionInterface::RTCConfiguration client_1_config;
3759 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3760 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3761 ice_server_1.username = "test";
3762 ice_server_1.password = "test";
3763 client_1_config.servers.push_back(ice_server_1);
3764 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3765 client_1_config.presume_writable_when_fully_relayed = true;
3766
3767 PeerConnectionInterface::RTCConfiguration client_2_config;
3768 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3769 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3770 ice_server_2.username = "test";
3771 ice_server_2.password = "test";
3772 client_2_config.servers.push_back(ice_server_2);
3773 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3774 client_2_config.presume_writable_when_fully_relayed = true;
3775
3776 ASSERT_TRUE(
3777 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3778 // Set up the simulated delays.
3779 SetSignalingDelayMs(signaling_trip_delay_ms);
3780 ConnectFakeSignaling();
3781 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3782 virtual_socket_server()->UpdateDelayDistribution();
3783
3784 // Set "offer to receive audio/video" without adding any tracks, so we just
3785 // set up ICE/DTLS with no media.
3786 PeerConnectionInterface::RTCOfferAnswerOptions options;
3787 options.offer_to_receive_audio = 1;
3788 options.offer_to_receive_video = 1;
3789 caller()->SetOfferAnswerOptions(options);
3790 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003791 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3792 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003793 // Need to free the clients here since they're using things we created on
3794 // the stack.
3795 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3796 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3797}
3798
Jonas Orelandbdcee282017-10-10 14:01:40 +02003799// Verify that a TurnCustomizer passed in through RTCConfiguration
3800// is actually used by the underlying TURN candidate pair.
3801// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003802TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003803 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3804 3478};
3805 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3806 0};
3807 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3808 3478};
3809 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3810 0};
3811 cricket::TestTurnServer turn_server_1(network_thread(),
3812 turn_server_1_internal_address,
3813 turn_server_1_external_address);
3814 cricket::TestTurnServer turn_server_2(network_thread(),
3815 turn_server_2_internal_address,
3816 turn_server_2_external_address);
3817
3818 PeerConnectionInterface::RTCConfiguration client_1_config;
3819 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3820 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3821 ice_server_1.username = "test";
3822 ice_server_1.password = "test";
3823 client_1_config.servers.push_back(ice_server_1);
3824 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3825 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3826 client_1_config.turn_customizer = customizer1.get();
3827
3828 PeerConnectionInterface::RTCConfiguration client_2_config;
3829 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3830 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3831 ice_server_2.username = "test";
3832 ice_server_2.password = "test";
3833 client_2_config.servers.push_back(ice_server_2);
3834 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3835 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3836 client_2_config.turn_customizer = customizer2.get();
3837
3838 ASSERT_TRUE(
3839 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3840 ConnectFakeSignaling();
3841
3842 // Set "offer to receive audio/video" without adding any tracks, so we just
3843 // set up ICE/DTLS with no media.
3844 PeerConnectionInterface::RTCOfferAnswerOptions options;
3845 options.offer_to_receive_audio = 1;
3846 options.offer_to_receive_video = 1;
3847 caller()->SetOfferAnswerOptions(options);
3848 caller()->CreateAndSetAndSignalOffer();
3849 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3850
3851 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3852 EXPECT_GT(customizer1->modify_cnt_, 0u);
3853
3854 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3855 EXPECT_GT(customizer2->modify_cnt_, 0u);
3856
3857 // Need to free the clients here since they're using things we created on
3858 // the stack.
3859 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3860 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3861}
3862
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07003863// Verifies that you can use TCP instead of UDP to connect to a TURN server and
3864// send media between the caller and the callee.
3865TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
3866 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3867 3478};
3868 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3869
3870 // Enable TCP for the fake turn server.
3871 cricket::TestTurnServer turn_server(
3872 network_thread(), turn_server_internal_address,
3873 turn_server_external_address, cricket::PROTO_TCP);
3874
3875 webrtc::PeerConnectionInterface::IceServer ice_server;
3876 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
3877 ice_server.username = "test";
3878 ice_server.password = "test";
3879
3880 PeerConnectionInterface::RTCConfiguration client_1_config;
3881 client_1_config.servers.push_back(ice_server);
3882 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3883
3884 PeerConnectionInterface::RTCConfiguration client_2_config;
3885 client_2_config.servers.push_back(ice_server);
3886 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3887
3888 ASSERT_TRUE(
3889 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3890
3891 // Do normal offer/answer and wait for ICE to complete.
3892 ConnectFakeSignaling();
3893 caller()->AddAudioVideoTracks();
3894 callee()->AddAudioVideoTracks();
3895 caller()->CreateAndSetAndSignalOffer();
3896 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3897 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3898 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3899
3900 MediaExpectations media_expectations;
3901 media_expectations.ExpectBidirectionalAudioAndVideo();
3902 EXPECT_TRUE(ExpectNewFrames(media_expectations));
3903}
3904
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07003905// Verify that a SSLCertificateVerifier passed in through
3906// PeerConnectionDependencies is actually used by the underlying SSL
3907// implementation to determine whether a certificate presented by the TURN
3908// server is accepted by the client. Note that openssladapter_unittest.cc
3909// contains more detailed, lower-level tests.
3910TEST_P(PeerConnectionIntegrationTest,
3911 SSLCertificateVerifierUsedForTurnConnections) {
3912 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3913 3478};
3914 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3915
3916 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3917 // that host name verification passes on the fake certificate.
3918 cricket::TestTurnServer turn_server(
3919 network_thread(), turn_server_internal_address,
3920 turn_server_external_address, cricket::PROTO_TLS,
3921 /*ignore_bad_certs=*/true, "88.88.88.0");
3922
3923 webrtc::PeerConnectionInterface::IceServer ice_server;
3924 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3925 ice_server.username = "test";
3926 ice_server.password = "test";
3927
3928 PeerConnectionInterface::RTCConfiguration client_1_config;
3929 client_1_config.servers.push_back(ice_server);
3930 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3931
3932 PeerConnectionInterface::RTCConfiguration client_2_config;
3933 client_2_config.servers.push_back(ice_server);
3934 // Setting the type to kRelay forces the connection to go through a TURN
3935 // server.
3936 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3937
3938 // Get a copy to the pointer so we can verify calls later.
3939 rtc::TestCertificateVerifier* client_1_cert_verifier =
3940 new rtc::TestCertificateVerifier();
3941 client_1_cert_verifier->verify_certificate_ = true;
3942 rtc::TestCertificateVerifier* client_2_cert_verifier =
3943 new rtc::TestCertificateVerifier();
3944 client_2_cert_verifier->verify_certificate_ = true;
3945
3946 // Create the dependencies with the test certificate verifier.
3947 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3948 client_1_deps.tls_cert_verifier =
3949 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3950 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3951 client_2_deps.tls_cert_verifier =
3952 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3953
3954 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3955 client_1_config, std::move(client_1_deps), client_2_config,
3956 std::move(client_2_deps)));
3957 ConnectFakeSignaling();
3958
3959 // Set "offer to receive audio/video" without adding any tracks, so we just
3960 // set up ICE/DTLS with no media.
3961 PeerConnectionInterface::RTCOfferAnswerOptions options;
3962 options.offer_to_receive_audio = 1;
3963 options.offer_to_receive_video = 1;
3964 caller()->SetOfferAnswerOptions(options);
3965 caller()->CreateAndSetAndSignalOffer();
3966 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3967
3968 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
3969 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
3970
3971 // Need to free the clients here since they're using things we created on
3972 // the stack.
3973 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3974 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3975}
3976
3977TEST_P(PeerConnectionIntegrationTest,
3978 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
3979 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3980 3478};
3981 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3982
3983 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3984 // that host name verification passes on the fake certificate.
3985 cricket::TestTurnServer turn_server(
3986 network_thread(), turn_server_internal_address,
3987 turn_server_external_address, cricket::PROTO_TLS,
3988 /*ignore_bad_certs=*/true, "88.88.88.0");
3989
3990 webrtc::PeerConnectionInterface::IceServer ice_server;
3991 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3992 ice_server.username = "test";
3993 ice_server.password = "test";
3994
3995 PeerConnectionInterface::RTCConfiguration client_1_config;
3996 client_1_config.servers.push_back(ice_server);
3997 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3998
3999 PeerConnectionInterface::RTCConfiguration client_2_config;
4000 client_2_config.servers.push_back(ice_server);
4001 // Setting the type to kRelay forces the connection to go through a TURN
4002 // server.
4003 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4004
4005 // Get a copy to the pointer so we can verify calls later.
4006 rtc::TestCertificateVerifier* client_1_cert_verifier =
4007 new rtc::TestCertificateVerifier();
4008 client_1_cert_verifier->verify_certificate_ = false;
4009 rtc::TestCertificateVerifier* client_2_cert_verifier =
4010 new rtc::TestCertificateVerifier();
4011 client_2_cert_verifier->verify_certificate_ = false;
4012
4013 // Create the dependencies with the test certificate verifier.
4014 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4015 client_1_deps.tls_cert_verifier =
4016 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4017 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4018 client_2_deps.tls_cert_verifier =
4019 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4020
4021 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4022 client_1_config, std::move(client_1_deps), client_2_config,
4023 std::move(client_2_deps)));
4024 ConnectFakeSignaling();
4025
4026 // Set "offer to receive audio/video" without adding any tracks, so we just
4027 // set up ICE/DTLS with no media.
4028 PeerConnectionInterface::RTCOfferAnswerOptions options;
4029 options.offer_to_receive_audio = 1;
4030 options.offer_to_receive_video = 1;
4031 caller()->SetOfferAnswerOptions(options);
4032 caller()->CreateAndSetAndSignalOffer();
4033 bool wait_res = true;
4034 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4035 // properly, should be able to just wait for a state of "failed" instead of
4036 // waiting a fixed 10 seconds.
4037 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4038 ASSERT_FALSE(wait_res);
4039
4040 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4041 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4042
4043 // Need to free the clients here since they're using things we created on
4044 // the stack.
4045 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4046 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4047}
4048
deadbeefc964d0b2017-04-03 10:03:35 -07004049// Test that audio and video flow end-to-end when codec names don't use the
4050// expected casing, given that they're supposed to be case insensitive. To test
4051// this, all but one codec is removed from each media description, and its
4052// casing is changed.
4053//
4054// In the past, this has regressed and caused crashes/black video, due to the
4055// fact that code at some layers was doing case-insensitive comparisons and
4056// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004057TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004058 ASSERT_TRUE(CreatePeerConnectionWrappers());
4059 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004060 caller()->AddAudioVideoTracks();
4061 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004062
4063 // Remove all but one audio/video codec (opus and VP8), and change the
4064 // casing of the caller's generated offer.
4065 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4066 cricket::AudioContentDescription* audio =
4067 GetFirstAudioContentDescription(description);
4068 ASSERT_NE(nullptr, audio);
4069 auto audio_codecs = audio->codecs();
4070 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4071 [](const cricket::AudioCodec& codec) {
4072 return codec.name != "opus";
4073 }),
4074 audio_codecs.end());
4075 ASSERT_EQ(1u, audio_codecs.size());
4076 audio_codecs[0].name = "OpUs";
4077 audio->set_codecs(audio_codecs);
4078
4079 cricket::VideoContentDescription* video =
4080 GetFirstVideoContentDescription(description);
4081 ASSERT_NE(nullptr, video);
4082 auto video_codecs = video->codecs();
4083 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4084 [](const cricket::VideoCodec& codec) {
4085 return codec.name != "VP8";
4086 }),
4087 video_codecs.end());
4088 ASSERT_EQ(1u, video_codecs.size());
4089 video_codecs[0].name = "vP8";
4090 video->set_codecs(video_codecs);
4091 });
4092
4093 caller()->CreateAndSetAndSignalOffer();
4094 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4095
4096 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004097 MediaExpectations media_expectations;
4098 media_expectations.ExpectBidirectionalAudioAndVideo();
4099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004100}
4101
Seth Hampson2f0d7022018-02-20 11:54:42 -08004102TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004103 ASSERT_TRUE(CreatePeerConnectionWrappers());
4104 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004105 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004106 caller()->CreateAndSetAndSignalOffer();
4107 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004108 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004109 MediaExpectations media_expectations;
4110 media_expectations.CalleeExpectsSomeAudio(1);
4111 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004112 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4113 auto receiver = callee()->pc()->GetReceivers()[0];
4114 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4115
4116 auto contributing_sources = receiver->GetSources();
4117 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4118 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4119 contributing_sources[0].source_id());
4120}
4121
deadbeef2f425aa2017-04-14 10:41:32 -07004122// Test that if a track is removed and added again with a different stream ID,
4123// the new stream ID is successfully communicated in SDP and media continues to
4124// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004125// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4126// it will not reuse a transceiver that has already been sending. After creating
4127// a new transceiver it tries to create an offer with two senders of the same
4128// track ids and it fails.
4129TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004130 ASSERT_TRUE(CreatePeerConnectionWrappers());
4131 ConnectFakeSignaling();
4132
4133 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4134 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4135 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4136 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4137
4138 // Add track using stream 1, do offer/answer.
4139 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4140 caller()->CreateLocalAudioTrack();
4141 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4142 caller()->pc()->AddTrack(track, {stream_1.get()});
4143 caller()->CreateAndSetAndSignalOffer();
4144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004145 {
4146 MediaExpectations media_expectations;
4147 media_expectations.CalleeExpectsSomeAudio(1);
4148 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4149 }
deadbeef2f425aa2017-04-14 10:41:32 -07004150 // Remove the sender, and create a new one with the new stream.
4151 caller()->pc()->RemoveTrack(sender);
4152 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4153 caller()->CreateAndSetAndSignalOffer();
4154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4155 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004156 {
4157 MediaExpectations media_expectations;
4158 media_expectations.CalleeExpectsSomeAudio();
4159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4160 }
deadbeef2f425aa2017-04-14 10:41:32 -07004161}
4162
Seth Hampson2f0d7022018-02-20 11:54:42 -08004163TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004164 ASSERT_TRUE(CreatePeerConnectionWrappers());
4165 ConnectFakeSignaling();
4166
4167 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4168 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4169 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4170 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004171 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4172 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004173
Steve Anton15324772018-01-16 10:26:49 -08004174 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004175 caller()->CreateAndSetAndSignalOffer();
4176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4177}
4178
Steve Antonede9ca52017-10-16 13:04:27 -07004179// Test that if candidates are only signaled by applying full session
4180// descriptions (instead of using AddIceCandidate), the peers can connect to
4181// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004183 ASSERT_TRUE(CreatePeerConnectionWrappers());
4184 // Each side will signal the session descriptions but not candidates.
4185 ConnectFakeSignalingForSdpOnly();
4186
4187 // Add audio video track and exchange the initial offer/answer with media
4188 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004189 caller()->AddAudioVideoTracks();
4190 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004191 caller()->CreateAndSetAndSignalOffer();
4192
4193 // Wait for all candidates to be gathered on both the caller and callee.
4194 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4195 caller()->ice_gathering_state(), kDefaultTimeout);
4196 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4197 callee()->ice_gathering_state(), kDefaultTimeout);
4198
4199 // The candidates will now be included in the session description, so
4200 // signaling them will start the ICE connection.
4201 caller()->CreateAndSetAndSignalOffer();
4202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4203
4204 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004205 MediaExpectations media_expectations;
4206 media_expectations.ExpectBidirectionalAudioAndVideo();
4207 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004208}
4209
henrika5f6bf242017-11-01 11:06:56 +01004210// Test that SetAudioPlayout can be used to disable audio playout from the
4211// start, then later enable it. This may be useful, for example, if the caller
4212// needs to play a local ringtone until some event occurs, after which it
4213// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004214TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004215 ASSERT_TRUE(CreatePeerConnectionWrappers());
4216 ConnectFakeSignaling();
4217
4218 // Set up audio-only call where audio playout is disabled on caller's side.
4219 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004220 caller()->AddAudioTrack();
4221 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004222 caller()->CreateAndSetAndSignalOffer();
4223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4224
4225 // Pump messages for a second.
4226 WAIT(false, 1000);
4227 // Since audio playout is disabled, the caller shouldn't have received
4228 // anything (at the playout level, at least).
4229 EXPECT_EQ(0, caller()->audio_frames_received());
4230 // As a sanity check, make sure the callee (for which playout isn't disabled)
4231 // did still see frames on its audio level.
4232 ASSERT_GT(callee()->audio_frames_received(), 0);
4233
4234 // Enable playout again, and ensure audio starts flowing.
4235 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004236 MediaExpectations media_expectations;
4237 media_expectations.ExpectBidirectionalAudio();
4238 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004239}
4240
4241double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4242 auto report = pc->NewGetStats();
4243 auto track_stats_list =
4244 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4245 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4246 for (const auto* track_stats : track_stats_list) {
4247 if (track_stats->remote_source.is_defined() &&
4248 *track_stats->remote_source) {
4249 remote_track_stats = track_stats;
4250 break;
4251 }
4252 }
4253
4254 if (!remote_track_stats->total_audio_energy.is_defined()) {
4255 return 0.0;
4256 }
4257 return *remote_track_stats->total_audio_energy;
4258}
4259
4260// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4261// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004262TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004263 DisableAudioPlayoutStillGeneratesAudioStats) {
4264 ASSERT_TRUE(CreatePeerConnectionWrappers());
4265 ConnectFakeSignaling();
4266
4267 // Set up audio-only call where playout is disabled but audio-processing is
4268 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004269 caller()->AddAudioTrack();
4270 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004271 caller()->pc()->SetAudioPlayout(false);
4272
4273 caller()->CreateAndSetAndSignalOffer();
4274 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4275
4276 // Wait for the callee to receive audio stats.
4277 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4278}
4279
henrika4f167df2017-11-01 14:45:55 +01004280// Test that SetAudioRecording can be used to disable audio recording from the
4281// start, then later enable it. This may be useful, for example, if the caller
4282// wants to ensure that no audio resources are active before a certain state
4283// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004284TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004285 ASSERT_TRUE(CreatePeerConnectionWrappers());
4286 ConnectFakeSignaling();
4287
4288 // Set up audio-only call where audio recording is disabled on caller's side.
4289 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004290 caller()->AddAudioTrack();
4291 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004292 caller()->CreateAndSetAndSignalOffer();
4293 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4294
4295 // Pump messages for a second.
4296 WAIT(false, 1000);
4297 // Since caller has disabled audio recording, the callee shouldn't have
4298 // received anything.
4299 EXPECT_EQ(0, callee()->audio_frames_received());
4300 // As a sanity check, make sure the caller did still see frames on its
4301 // audio level since audio recording is enabled on the calle side.
4302 ASSERT_GT(caller()->audio_frames_received(), 0);
4303
4304 // Enable audio recording again, and ensure audio starts flowing.
4305 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004306 MediaExpectations media_expectations;
4307 media_expectations.ExpectBidirectionalAudio();
4308 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004309}
4310
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004311// Test that after closing PeerConnections, they stop sending any packets (ICE,
4312// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004313TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004314 // Set up audio/video/data, wait for some frames to be received.
4315 ASSERT_TRUE(CreatePeerConnectionWrappers());
4316 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004317 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004318#ifdef HAVE_SCTP
4319 caller()->CreateDataChannel();
4320#endif
4321 caller()->CreateAndSetAndSignalOffer();
4322 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004323 MediaExpectations media_expectations;
4324 media_expectations.CalleeExpectsSomeAudioAndVideo();
4325 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004326 // Close PeerConnections.
4327 caller()->pc()->Close();
4328 callee()->pc()->Close();
4329 // Pump messages for a second, and ensure no new packets end up sent.
4330 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4331 WAIT(false, 1000);
4332 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4333 EXPECT_EQ(sent_packets_a, sent_packets_b);
4334}
4335
Steve Anton7eca0932018-03-30 15:18:41 -07004336// Test that transport stats are generated by the RTCStatsCollector for a
4337// connection that only involves data channels. This is a regression test for
4338// crbug.com/826972.
4339#ifdef HAVE_SCTP
4340TEST_P(PeerConnectionIntegrationTest,
4341 TransportStatsReportedForDataChannelOnlyConnection) {
4342 ASSERT_TRUE(CreatePeerConnectionWrappers());
4343 ConnectFakeSignaling();
4344 caller()->CreateDataChannel();
4345
4346 caller()->CreateAndSetAndSignalOffer();
4347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4348 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4349
4350 auto caller_report = caller()->NewGetStats();
4351 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4352 auto callee_report = callee()->NewGetStats();
4353 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4354}
4355#endif // HAVE_SCTP
4356
Seth Hampson2f0d7022018-02-20 11:54:42 -08004357INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4358 PeerConnectionIntegrationTest,
4359 Values(SdpSemantics::kPlanB,
4360 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004361
Steve Anton74255ff2018-01-24 18:32:57 -08004362// Tests that verify interoperability between Plan B and Unified Plan
4363// PeerConnections.
4364class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004365 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004366 public ::testing::WithParamInterface<
4367 std::tuple<SdpSemantics, SdpSemantics>> {
4368 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004369 // Setting the SdpSemantics for the base test to kDefault does not matter
4370 // because we specify not to use the test semantics when creating
4371 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004372 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004373 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004374 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004375 callee_semantics_(std::get<1>(GetParam())) {}
4376
4377 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004378 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4379 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004380 }
4381
4382 const SdpSemantics caller_semantics_;
4383 const SdpSemantics callee_semantics_;
4384};
4385
4386TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4387 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4388 ConnectFakeSignaling();
4389
4390 caller()->CreateAndSetAndSignalOffer();
4391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4392}
4393
4394TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4395 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4396 ConnectFakeSignaling();
4397 auto audio_sender = caller()->AddAudioTrack();
4398
4399 caller()->CreateAndSetAndSignalOffer();
4400 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4401
4402 // Verify that one audio receiver has been created on the remote and that it
4403 // has the same track ID as the sending track.
4404 auto receivers = callee()->pc()->GetReceivers();
4405 ASSERT_EQ(1u, receivers.size());
4406 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4407 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4408
Seth Hampson2f0d7022018-02-20 11:54:42 -08004409 MediaExpectations media_expectations;
4410 media_expectations.CalleeExpectsSomeAudio();
4411 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004412}
4413
4414TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4415 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4416 ConnectFakeSignaling();
4417 auto video_sender = caller()->AddVideoTrack();
4418 auto audio_sender = caller()->AddAudioTrack();
4419
4420 caller()->CreateAndSetAndSignalOffer();
4421 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4422
4423 // Verify that one audio and one video receiver have been created on the
4424 // remote and that they have the same track IDs as the sending tracks.
4425 auto audio_receivers =
4426 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4427 ASSERT_EQ(1u, audio_receivers.size());
4428 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4429 auto video_receivers =
4430 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4431 ASSERT_EQ(1u, video_receivers.size());
4432 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4433
Seth Hampson2f0d7022018-02-20 11:54:42 -08004434 MediaExpectations media_expectations;
4435 media_expectations.CalleeExpectsSomeAudioAndVideo();
4436 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004437}
4438
4439TEST_P(PeerConnectionIntegrationInteropTest,
4440 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4441 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4442 ConnectFakeSignaling();
4443 caller()->AddAudioVideoTracks();
4444 callee()->AddAudioVideoTracks();
4445
4446 caller()->CreateAndSetAndSignalOffer();
4447 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4448
Seth Hampson2f0d7022018-02-20 11:54:42 -08004449 MediaExpectations media_expectations;
4450 media_expectations.ExpectBidirectionalAudioAndVideo();
4451 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004452}
4453
4454TEST_P(PeerConnectionIntegrationInteropTest,
4455 ReverseRolesOneAudioLocalToOneVideoRemote) {
4456 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4457 ConnectFakeSignaling();
4458 caller()->AddAudioTrack();
4459 callee()->AddVideoTrack();
4460
4461 caller()->CreateAndSetAndSignalOffer();
4462 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4463
4464 // Verify that only the audio track has been negotiated.
4465 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4466 // Might also check that the callee's NegotiationNeeded flag is set.
4467
4468 // Reverse roles.
4469 callee()->CreateAndSetAndSignalOffer();
4470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4471
Seth Hampson2f0d7022018-02-20 11:54:42 -08004472 MediaExpectations media_expectations;
4473 media_expectations.CallerExpectsSomeVideo();
4474 media_expectations.CalleeExpectsSomeAudio();
4475 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004476}
4477
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004478// Test getting the usage fingerprint for a simple test case.
4479TEST_P(PeerConnectionIntegrationTest, UsageFingerprintHistogram) {
4480 ASSERT_TRUE(CreatePeerConnectionWrappers());
4481 ConnectFakeSignaling();
4482 // Register UMA observer before signaling begins.
4483 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
4484 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4485 caller()->pc()->RegisterUMAObserver(caller_observer);
4486 rtc::scoped_refptr<webrtc::FakeMetricsObserver> callee_observer =
4487 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4488 callee()->pc()->RegisterUMAObserver(callee_observer);
4489 caller()->AddAudioTrack();
4490 caller()->AddVideoTrack();
4491 caller()->CreateAndSetAndSignalOffer();
4492 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4493 caller()->pc()->Close();
4494 callee()->pc()->Close();
4495 int expected_fingerprint = MakeUsageFingerprint(
4496 {PeerConnection::UsageEvent::AUDIO_ADDED,
4497 PeerConnection::UsageEvent::VIDEO_ADDED,
4498 PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED,
4499 PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED,
4500 PeerConnection::UsageEvent::CANDIDATE_COLLECTED,
4501 PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED,
4502 PeerConnection::UsageEvent::ICE_STATE_CONNECTED,
4503 PeerConnection::UsageEvent::CLOSE_CALLED});
4504 EXPECT_TRUE(caller_observer->ExpectOnlySingleEnumCount(
4505 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4506 EXPECT_TRUE(callee_observer->ExpectOnlySingleEnumCount(
4507 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4508}
4509
Steve Antonba42e992018-04-09 14:10:01 -07004510INSTANTIATE_TEST_CASE_P(
4511 PeerConnectionIntegrationTest,
4512 PeerConnectionIntegrationInteropTest,
4513 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4514 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4515
4516// Test that if the Unified Plan side offers two video tracks then the Plan B
4517// side will only see the first one and ignore the second.
4518TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004519 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4520 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004521 ConnectFakeSignaling();
4522 auto first_sender = caller()->AddVideoTrack();
4523 caller()->AddVideoTrack();
4524
4525 caller()->CreateAndSetAndSignalOffer();
4526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4527
4528 // Verify that there is only one receiver and it corresponds to the first
4529 // added track.
4530 auto receivers = callee()->pc()->GetReceivers();
4531 ASSERT_EQ(1u, receivers.size());
4532 EXPECT_TRUE(receivers[0]->track()->enabled());
4533 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4534
Seth Hampson2f0d7022018-02-20 11:54:42 -08004535 MediaExpectations media_expectations;
4536 media_expectations.CalleeExpectsSomeVideo();
4537 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004538}
4539
deadbeef1dcb1642017-03-29 21:08:16 -07004540} // namespace
4541
4542#endif // if !defined(THREAD_SANITIZER)