blob: 8ce09d25466639aaede1d307fe652de8398f50e3 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/mediastreaminterface.h"
28#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070029#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010030#include "api/rtpreceiverinterface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080031#include "api/test/loopback_media_transport.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020032#include "api/umametrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/builtin_video_decoder_factory.h"
34#include "api/video_codecs/builtin_video_encoder_factory.h"
35#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070036#include "call/call.h"
37#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
38#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "media/engine/fakewebrtcvideoengine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "media/engine/webrtcmediaengine.h"
41#include "modules/audio_processing/include/audio_processing.h"
Zach Stein6fcdc2f2018-08-23 16:25:55 -070042#include "p2p/base/mockasyncresolver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "p2p/base/p2pconstants.h"
44#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070045#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020046#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "p2p/base/testturnserver.h"
48#include "p2p/client/basicportallocator.h"
49#include "pc/dtmfsender.h"
50#include "pc/localaudiosource.h"
51#include "pc/mediasession.h"
52#include "pc/peerconnection.h"
53#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080054#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080055#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020057#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "pc/test/fakertccertificategenerator.h"
59#include "pc/test/fakevideotrackrenderer.h"
60#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070062#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020064#include "rtc_base/numerics/safe_conversions.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070065#include "rtc_base/testcertificateverifier.h"
Johannes Kron965e7942018-09-13 15:36:20 +020066#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#include "rtc_base/virtualsocketserver.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020068#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020069#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070070
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010071namespace webrtc {
72namespace {
73
74using ::cricket::ContentInfo;
75using ::cricket::StreamParams;
76using ::rtc::SocketAddress;
77using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080078using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070079using ::testing::ElementsAre;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070080using ::testing::Return;
81using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080082using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010083using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080084using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070085
86static const int kDefaultTimeout = 10000;
87static const int kMaxWaitForStatsMs = 3000;
88static const int kMaxWaitForActivationMs = 5000;
89static const int kMaxWaitForFramesMs = 10000;
90// Default number of audio/video frames to wait for before considering a test
91// successful.
92static const int kDefaultExpectedAudioFrameCount = 3;
93static const int kDefaultExpectedVideoFrameCount = 3;
94
deadbeef1dcb1642017-03-29 21:08:16 -070095static const char kDataChannelLabel[] = "data_channel";
96
97// SRTP cipher name negotiated by the tests. This must be updated if the
98// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -070099static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700100static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
101
Steve Antonede9ca52017-10-16 13:04:27 -0700102static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
103
deadbeef1dcb1642017-03-29 21:08:16 -0700104// Helper function for constructing offer/answer options to initiate an ICE
105// restart.
106PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
107 PeerConnectionInterface::RTCOfferAnswerOptions options;
108 options.ice_restart = true;
109 return options;
110}
111
deadbeefd8ad7882017-04-18 16:01:17 -0700112// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
113// attribute from received SDP, simulating a legacy endpoint.
114void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
115 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800116 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700117 }
118 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100119 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700120}
121
Seth Hampson5897a6e2018-04-03 11:16:33 -0700122// Removes all stream information besides the stream ids, simulating an
123// endpoint that only signals a=msid lines to convey stream_ids.
124void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
125 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700126 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700127 std::vector<std::string> stream_ids;
128 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700129 const StreamParams& first_stream =
130 content.media_description()->streams()[0];
131 track_id = first_stream.id;
132 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700133 }
134 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700135 StreamParams new_stream;
136 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700137 new_stream.set_stream_ids(stream_ids);
138 content.media_description()->AddStream(new_stream);
139 }
140}
141
zhihuangf8164932017-05-19 13:09:47 -0700142int FindFirstMediaStatsIndexByKind(
143 const std::string& kind,
144 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
145 media_stats_vec) {
146 for (size_t i = 0; i < media_stats_vec.size(); i++) {
147 if (media_stats_vec[i]->kind.ValueToString() == kind) {
148 return i;
149 }
150 }
151 return -1;
152}
153
deadbeef1dcb1642017-03-29 21:08:16 -0700154class SignalingMessageReceiver {
155 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800156 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700157 virtual void ReceiveIceMessage(const std::string& sdp_mid,
158 int sdp_mline_index,
159 const std::string& msg) = 0;
160
161 protected:
162 SignalingMessageReceiver() {}
163 virtual ~SignalingMessageReceiver() {}
164};
165
166class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
167 public:
168 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
169 : expected_media_type_(media_type) {}
170
171 void OnFirstPacketReceived(cricket::MediaType media_type) override {
172 ASSERT_EQ(expected_media_type_, media_type);
173 first_packet_received_ = true;
174 }
175
176 bool first_packet_received() const { return first_packet_received_; }
177
178 virtual ~MockRtpReceiverObserver() {}
179
180 private:
181 bool first_packet_received_ = false;
182 cricket::MediaType expected_media_type_;
183};
184
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700185// Used by PeerConnectionWrapper::OnIceCandidate to allow a test to modify an
186// ICE candidate before it is signaled.
187class IceCandidateReplacerInterface {
188 public:
189 virtual ~IceCandidateReplacerInterface() = default;
190 // Return nullptr to drop the candidate (it won't be signaled to the other
191 // side).
192 virtual std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
193 const webrtc::IceCandidateInterface*) = 0;
194};
195
deadbeef1dcb1642017-03-29 21:08:16 -0700196// Helper class that wraps a peer connection, observes it, and can accept
197// signaling messages from another wrapper.
198//
199// Uses a fake network, fake A/V capture, and optionally fake
200// encoders/decoders, though they aren't used by default since they don't
201// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700202// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800203// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700204class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800205 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700206 public:
207 // Different factory methods for convenience.
208 // TODO(deadbeef): Could use the pattern of:
209 //
210 // PeerConnectionWrapper =
211 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
212 //
213 // To reduce some code duplication.
214 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
215 const std::string& debug_name,
216 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
217 rtc::Thread* network_thread,
218 rtc::Thread* worker_thread) {
219 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700220 webrtc::PeerConnectionDependencies dependencies(nullptr);
221 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200222 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800223 worker_thread, nullptr,
224 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700225 delete client;
226 return nullptr;
227 }
228 return client;
229 }
230
deadbeef2f425aa2017-04-14 10:41:32 -0700231 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
232 return peer_connection_factory_.get();
233 }
234
deadbeef1dcb1642017-03-29 21:08:16 -0700235 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
236
237 // If a signaling message receiver is set (via ConnectFakeSignaling), this
238 // will set the whole offer/answer exchange in motion. Just need to wait for
239 // the signaling state to reach "stable".
240 void CreateAndSetAndSignalOffer() {
241 auto offer = CreateOffer();
242 ASSERT_NE(nullptr, offer);
243 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
244 }
245
246 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
247 // when a remote offer is received (via fake signaling) and an answer is
248 // generated. By default, uses default options.
249 void SetOfferAnswerOptions(
250 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
251 offer_answer_options_ = options;
252 }
253
254 // Set a callback to be invoked when SDP is received via the fake signaling
255 // channel, which provides an opportunity to munge (modify) the SDP. This is
256 // used to test SDP being applied that a PeerConnection would normally not
257 // generate, but a non-JSEP endpoint might.
258 void SetReceivedSdpMunger(
259 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100260 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700261 }
262
deadbeefc964d0b2017-04-03 10:03:35 -0700263 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700264 // generated.
265 void SetGeneratedSdpMunger(
266 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100267 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700268 }
269
Seth Hampson2f0d7022018-02-20 11:54:42 -0800270 // Set a callback to be invoked when a remote offer is received via the fake
271 // signaling channel. This provides an opportunity to change the
272 // PeerConnection state before an answer is created and sent to the caller.
273 void SetRemoteOfferHandler(std::function<void()> handler) {
274 remote_offer_handler_ = std::move(handler);
275 }
276
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700277 void SetLocalIceCandidateReplacer(
278 std::unique_ptr<IceCandidateReplacerInterface> replacer) {
279 local_ice_candidate_replacer_ = std::move(replacer);
280 }
281
Steve Antonede9ca52017-10-16 13:04:27 -0700282 // Every ICE connection state in order that has been seen by the observer.
283 std::vector<PeerConnectionInterface::IceConnectionState>
284 ice_connection_state_history() const {
285 return ice_connection_state_history_;
286 }
Steve Anton6f25b092017-10-23 09:39:20 -0700287 void clear_ice_connection_state_history() {
288 ice_connection_state_history_.clear();
289 }
Steve Antonede9ca52017-10-16 13:04:27 -0700290
Jonas Olsson635474e2018-10-18 15:58:17 +0200291 // Every PeerConnection state in order that has been seen by the observer.
292 std::vector<PeerConnectionInterface::PeerConnectionState>
293 peer_connection_state_history() const {
294 return peer_connection_state_history_;
295 }
296
Steve Antonede9ca52017-10-16 13:04:27 -0700297 // Every ICE gathering state in order that has been seen by the observer.
298 std::vector<PeerConnectionInterface::IceGatheringState>
299 ice_gathering_state_history() const {
300 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700301 }
302
Steve Anton15324772018-01-16 10:26:49 -0800303 void AddAudioVideoTracks() {
304 AddAudioTrack();
305 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700306 }
307
Steve Anton74255ff2018-01-24 18:32:57 -0800308 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
309 return AddTrack(CreateLocalAudioTrack());
310 }
deadbeef1dcb1642017-03-29 21:08:16 -0700311
Steve Anton74255ff2018-01-24 18:32:57 -0800312 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
313 return AddTrack(CreateLocalVideoTrack());
314 }
deadbeef1dcb1642017-03-29 21:08:16 -0700315
316 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200317 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700318 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200319 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700320 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200321 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700322 // TODO(perkj): Test audio source when it is implemented. Currently audio
323 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700324 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700325 source);
326 }
327
328 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200329 webrtc::FakePeriodicVideoSource::Config config;
330 config.timestamp_offset_ms = rtc::TimeMillis();
331 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700332 }
333
334 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200335 CreateLocalVideoTrackWithConfig(
336 webrtc::FakePeriodicVideoSource::Config config) {
337 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface>
341 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200342 webrtc::FakePeriodicVideoSource::Config config;
343 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200344 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200345 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700346 }
347
Steve Anton74255ff2018-01-24 18:32:57 -0800348 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
349 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800350 const std::vector<std::string>& stream_ids = {}) {
351 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800352 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800353 return result.MoveValue();
354 }
355
356 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
357 cricket::MediaType media_type) {
358 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
359 for (auto receiver : pc()->GetReceivers()) {
360 if (receiver->media_type() == media_type) {
361 receivers.push_back(receiver);
362 }
363 }
364 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700365 }
366
Seth Hampson2f0d7022018-02-20 11:54:42 -0800367 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
368 cricket::MediaType media_type) {
369 for (auto transceiver : pc()->GetTransceivers()) {
370 if (transceiver->receiver()->media_type() == media_type) {
371 return transceiver;
372 }
373 }
374 return nullptr;
375 }
376
deadbeef1dcb1642017-03-29 21:08:16 -0700377 bool SignalingStateStable() {
378 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
379 }
380
381 void CreateDataChannel() { CreateDataChannel(nullptr); }
382
383 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700384 CreateDataChannel(kDataChannelLabel, init);
385 }
386
387 void CreateDataChannel(const std::string& label,
388 const webrtc::DataChannelInit* init) {
389 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700390 ASSERT_TRUE(data_channel_.get() != nullptr);
391 data_observer_.reset(new MockDataChannelObserver(data_channel_));
392 }
393
394 DataChannelInterface* data_channel() { return data_channel_; }
395 const MockDataChannelObserver* data_observer() const {
396 return data_observer_.get();
397 }
398
399 int audio_frames_received() const {
400 return fake_audio_capture_module_->frames_received();
401 }
402
403 // Takes minimum of video frames received for each track.
404 //
405 // Can be used like:
406 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
407 //
408 // To ensure that all video tracks received at least a certain number of
409 // frames.
410 int min_video_frames_received_per_track() const {
411 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200412 if (fake_video_renderers_.empty()) {
413 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700414 }
deadbeef1dcb1642017-03-29 21:08:16 -0700415
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200416 for (const auto& pair : fake_video_renderers_) {
417 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700418 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200419 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700420 }
421
422 // Returns a MockStatsObserver in a state after stats gathering finished,
423 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700424 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700425 webrtc::MediaStreamTrackInterface* track) {
426 rtc::scoped_refptr<MockStatsObserver> observer(
427 new rtc::RefCountedObject<MockStatsObserver>());
428 EXPECT_TRUE(peer_connection_->GetStats(
429 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
430 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
431 return observer;
432 }
433
434 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
436 return OldGetStatsForTrack(nullptr);
437 }
438
439 // Synchronously gets stats and returns them. If it times out, fails the test
440 // and returns null.
441 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
442 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
443 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
444 peer_connection_->GetStats(callback);
445 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
446 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
448
449 int rendered_width() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->width();
454 }
455
456 int rendered_height() {
457 EXPECT_FALSE(fake_video_renderers_.empty());
458 return fake_video_renderers_.empty()
459 ? 0
460 : fake_video_renderers_.begin()->second->height();
461 }
462
463 double rendered_aspect_ratio() {
464 if (rendered_height() == 0) {
465 return 0.0;
466 }
467 return static_cast<double>(rendered_width()) / rendered_height();
468 }
469
470 webrtc::VideoRotation rendered_rotation() {
471 EXPECT_FALSE(fake_video_renderers_.empty());
472 return fake_video_renderers_.empty()
473 ? webrtc::kVideoRotation_0
474 : fake_video_renderers_.begin()->second->rotation();
475 }
476
477 int local_rendered_width() {
478 return local_video_renderer_ ? local_video_renderer_->width() : 0;
479 }
480
481 int local_rendered_height() {
482 return local_video_renderer_ ? local_video_renderer_->height() : 0;
483 }
484
485 double local_rendered_aspect_ratio() {
486 if (local_rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(local_rendered_width()) /
490 local_rendered_height();
491 }
492
493 size_t number_of_remote_streams() {
494 if (!pc()) {
495 return 0;
496 }
497 return pc()->remote_streams()->count();
498 }
499
500 StreamCollectionInterface* remote_streams() const {
501 if (!pc()) {
502 ADD_FAILURE();
503 return nullptr;
504 }
505 return pc()->remote_streams();
506 }
507
508 StreamCollectionInterface* local_streams() {
509 if (!pc()) {
510 ADD_FAILURE();
511 return nullptr;
512 }
513 return pc()->local_streams();
514 }
515
516 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
517 return pc()->signaling_state();
518 }
519
520 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
521 return pc()->ice_connection_state();
522 }
523
524 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
525 return pc()->ice_gathering_state();
526 }
527
528 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
529 // GetReceivers. They're updated automatically when a remote offer/answer
530 // from the fake signaling channel is applied, or when
531 // ResetRtpReceiverObservers below is called.
532 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
533 rtp_receiver_observers() {
534 return rtp_receiver_observers_;
535 }
536
537 void ResetRtpReceiverObservers() {
538 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100539 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
540 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700541 std::unique_ptr<MockRtpReceiverObserver> observer(
542 new MockRtpReceiverObserver(receiver->media_type()));
543 receiver->SetObserver(observer.get());
544 rtp_receiver_observers_.push_back(std::move(observer));
545 }
546 }
547
Steve Antonede9ca52017-10-16 13:04:27 -0700548 rtc::FakeNetworkManager* network() const {
549 return fake_network_manager_.get();
550 }
551 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
552
Qingsi Wang7685e862018-06-11 20:15:46 -0700553 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
554 return event_log_factory_;
555 }
556
deadbeef1dcb1642017-03-29 21:08:16 -0700557 private:
558 explicit PeerConnectionWrapper(const std::string& debug_name)
559 : debug_name_(debug_name) {}
560
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800561 bool Init(
562 const PeerConnectionFactory::Options* options,
563 const PeerConnectionInterface::RTCConfiguration* config,
564 webrtc::PeerConnectionDependencies dependencies,
565 rtc::Thread* network_thread,
566 rtc::Thread* worker_thread,
567 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
568 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700569 // There's an error in this test code if Init ends up being called twice.
570 RTC_DCHECK(!peer_connection_);
571 RTC_DCHECK(!peer_connection_factory_);
572
573 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700574 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700575
576 std::unique_ptr<cricket::PortAllocator> port_allocator(
577 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700578 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700579 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
580 if (!fake_audio_capture_module_) {
581 return false;
582 }
deadbeef1dcb1642017-03-29 21:08:16 -0700583 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700584
585 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
586 pc_factory_dependencies.network_thread = network_thread;
587 pc_factory_dependencies.worker_thread = worker_thread;
588 pc_factory_dependencies.signaling_thread = signaling_thread;
589 pc_factory_dependencies.media_engine =
590 cricket::WebRtcMediaEngineFactory::Create(
591 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
592 fake_audio_capture_module_),
593 webrtc::CreateBuiltinAudioEncoderFactory(),
594 webrtc::CreateBuiltinAudioDecoderFactory(),
595 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000596 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700597 webrtc::AudioProcessingBuilder().Create());
598 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
599 if (event_log_factory) {
600 event_log_factory_ = event_log_factory.get();
601 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
602 } else {
603 pc_factory_dependencies.event_log_factory =
604 webrtc::CreateRtcEventLogFactory();
605 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800606 if (media_transport_factory) {
607 pc_factory_dependencies.media_transport_factory =
608 std::move(media_transport_factory);
609 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700610 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
611 std::move(pc_factory_dependencies));
612
deadbeef1dcb1642017-03-29 21:08:16 -0700613 if (!peer_connection_factory_) {
614 return false;
615 }
616 if (options) {
617 peer_connection_factory_->SetOptions(*options);
618 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800619 if (config) {
620 sdp_semantics_ = config->sdp_semantics;
621 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700622
623 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200624 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700625 return peer_connection_.get() != nullptr;
626 }
627
628 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700629 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700630 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700631 PeerConnectionInterface::RTCConfiguration modified_config;
632 // If |config| is null, this will result in a default configuration being
633 // used.
634 if (config) {
635 modified_config = *config;
636 }
637 // Disable resolution adaptation; we don't want it interfering with the
638 // test results.
639 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
640 // ratios and not specific resolutions, is this even necessary?
641 modified_config.set_cpu_adaptation(false);
642
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700644 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700645 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700646 }
647
648 void set_signaling_message_receiver(
649 SignalingMessageReceiver* signaling_message_receiver) {
650 signaling_message_receiver_ = signaling_message_receiver;
651 }
652
653 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
654
Steve Antonede9ca52017-10-16 13:04:27 -0700655 void set_signal_ice_candidates(bool signal) {
656 signal_ice_candidates_ = signal;
657 }
658
deadbeef1dcb1642017-03-29 21:08:16 -0700659 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200660 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700661 // Set max frame rate to 10fps to reduce the risk of test flakiness.
662 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200663 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700664
Niels Möller5c7efe72018-05-11 10:34:46 +0200665 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200666 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
667 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700668 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200669 peer_connection_factory_->CreateVideoTrack(
670 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700671 if (!local_video_renderer_) {
672 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
673 }
674 return track;
675 }
676
677 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100678 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800679 std::unique_ptr<SessionDescriptionInterface> desc =
680 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700681 if (received_sdp_munger_) {
682 received_sdp_munger_(desc->description());
683 }
684
685 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
686 // Setting a remote description may have changed the number of receivers,
687 // so reset the receiver observers.
688 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800689 if (remote_offer_handler_) {
690 remote_offer_handler_();
691 }
deadbeef1dcb1642017-03-29 21:08:16 -0700692 auto answer = CreateAnswer();
693 ASSERT_NE(nullptr, answer);
694 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
695 }
696
697 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100698 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800699 std::unique_ptr<SessionDescriptionInterface> desc =
700 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700701 if (received_sdp_munger_) {
702 received_sdp_munger_(desc->description());
703 }
704
705 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
706 // Set the RtpReceiverObserver after receivers are created.
707 ResetRtpReceiverObservers();
708 }
709
710 // Returns null on failure.
711 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
712 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
713 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
714 pc()->CreateOffer(observer, offer_answer_options_);
715 return WaitForDescriptionFromObserver(observer);
716 }
717
718 // Returns null on failure.
719 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
720 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
721 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
722 pc()->CreateAnswer(observer, offer_answer_options_);
723 return WaitForDescriptionFromObserver(observer);
724 }
725
726 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100727 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700728 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
729 if (!observer->result()) {
730 return nullptr;
731 }
732 auto description = observer->MoveDescription();
733 if (generated_sdp_munger_) {
734 generated_sdp_munger_(description->description());
735 }
736 return description;
737 }
738
739 // Setting the local description and sending the SDP message over the fake
740 // signaling channel are combined into the same method because the SDP
741 // message needs to be sent as soon as SetLocalDescription finishes, without
742 // waiting for the observer to be called. This ensures that ICE candidates
743 // don't outrace the description.
744 bool SetLocalDescriptionAndSendSdpMessage(
745 std::unique_ptr<SessionDescriptionInterface> desc) {
746 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
747 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100748 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800749 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700750 std::string sdp;
751 EXPECT_TRUE(desc->ToString(&sdp));
752 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800753 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
754 RemoveUnusedVideoRenderers();
755 }
deadbeef1dcb1642017-03-29 21:08:16 -0700756 // As mentioned above, we need to send the message immediately after
757 // SetLocalDescription.
758 SendSdpMessage(type, sdp);
759 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
760 return true;
761 }
762
763 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
764 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
765 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100766 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700767 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800768 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
769 RemoveUnusedVideoRenderers();
770 }
deadbeef1dcb1642017-03-29 21:08:16 -0700771 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
772 return observer->result();
773 }
774
Seth Hampson2f0d7022018-02-20 11:54:42 -0800775 // This is a work around to remove unused fake_video_renderers from
776 // transceivers that have either stopped or are no longer receiving.
777 void RemoveUnusedVideoRenderers() {
778 auto transceivers = pc()->GetTransceivers();
779 for (auto& transceiver : transceivers) {
780 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
781 continue;
782 }
783 // Remove fake video renderers from any stopped transceivers.
784 if (transceiver->stopped()) {
785 auto it =
786 fake_video_renderers_.find(transceiver->receiver()->track()->id());
787 if (it != fake_video_renderers_.end()) {
788 fake_video_renderers_.erase(it);
789 }
790 }
791 // Remove fake video renderers from any transceivers that are no longer
792 // receiving.
793 if ((transceiver->current_direction() &&
794 !webrtc::RtpTransceiverDirectionHasRecv(
795 *transceiver->current_direction()))) {
796 auto it =
797 fake_video_renderers_.find(transceiver->receiver()->track()->id());
798 if (it != fake_video_renderers_.end()) {
799 fake_video_renderers_.erase(it);
800 }
801 }
802 }
803 }
804
deadbeef1dcb1642017-03-29 21:08:16 -0700805 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
806 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800807 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700808 if (signaling_delay_ms_ == 0) {
809 RelaySdpMessageIfReceiverExists(type, msg);
810 } else {
811 invoker_.AsyncInvokeDelayed<void>(
812 RTC_FROM_HERE, rtc::Thread::Current(),
813 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
814 this, type, msg),
815 signaling_delay_ms_);
816 }
817 }
818
Steve Antona3a92c22017-12-07 10:27:41 -0800819 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700820 if (signaling_message_receiver_) {
821 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
822 }
823 }
824
825 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
826 // default).
827 void SendIceMessage(const std::string& sdp_mid,
828 int sdp_mline_index,
829 const std::string& msg) {
830 if (signaling_delay_ms_ == 0) {
831 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
832 } else {
833 invoker_.AsyncInvokeDelayed<void>(
834 RTC_FROM_HERE, rtc::Thread::Current(),
835 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
836 this, sdp_mid, sdp_mline_index, msg),
837 signaling_delay_ms_);
838 }
839 }
840
841 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
842 int sdp_mline_index,
843 const std::string& msg) {
844 if (signaling_message_receiver_) {
845 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
846 msg);
847 }
848 }
849
850 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800851 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
852 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700853 HandleIncomingOffer(msg);
854 } else {
855 HandleIncomingAnswer(msg);
856 }
857 }
858
859 void ReceiveIceMessage(const std::string& sdp_mid,
860 int sdp_mline_index,
861 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100862 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700863 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
864 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
865 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
866 }
867
868 // PeerConnectionObserver callbacks.
869 void OnSignalingChange(
870 webrtc::PeerConnectionInterface::SignalingState new_state) override {
871 EXPECT_EQ(pc()->signaling_state(), new_state);
872 }
Steve Anton15324772018-01-16 10:26:49 -0800873 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
874 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
875 streams) override {
876 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
877 rtc::scoped_refptr<VideoTrackInterface> video_track(
878 static_cast<VideoTrackInterface*>(receiver->track().get()));
879 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700880 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800881 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200882 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700883 }
884 }
Steve Anton15324772018-01-16 10:26:49 -0800885 void OnRemoveTrack(
886 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
887 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
888 auto it = fake_video_renderers_.find(receiver->track()->id());
889 RTC_DCHECK(it != fake_video_renderers_.end());
890 fake_video_renderers_.erase(it);
891 }
892 }
deadbeef1dcb1642017-03-29 21:08:16 -0700893 void OnRenegotiationNeeded() override {}
894 void OnIceConnectionChange(
895 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
896 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700897 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700898 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200899 void OnConnectionChange(
900 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
901 peer_connection_state_history_.push_back(new_state);
902 }
903
deadbeef1dcb1642017-03-29 21:08:16 -0700904 void OnIceGatheringChange(
905 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700906 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700907 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700908 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700909 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceIceCandidate(
910 const webrtc::IceCandidateInterface* candidate) {
911 std::string candidate_string;
912 candidate->ToString(&candidate_string);
913
914 auto owned_candidate =
915 local_ice_candidate_replacer_->ReplaceCandidate(candidate);
916 if (!owned_candidate) {
917 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer dropped \""
918 << candidate_string << "\"";
919 return nullptr;
920 }
921 std::string owned_candidate_string;
922 owned_candidate->ToString(&owned_candidate_string);
923 RTC_LOG(LS_INFO) << "LocalIceCandidateReplacer changed \""
924 << candidate_string << "\" to \"" << owned_candidate_string
925 << "\"";
926 return owned_candidate;
927 }
deadbeef1dcb1642017-03-29 21:08:16 -0700928 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100929 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700930
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700931 const webrtc::IceCandidateInterface* new_candidate = candidate;
932 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate;
933 if (local_ice_candidate_replacer_) {
934 owned_candidate = ReplaceIceCandidate(candidate);
935 if (!owned_candidate) {
936 return; // The candidate was dropped.
937 }
938 new_candidate = owned_candidate.get();
939 }
940
deadbeef1dcb1642017-03-29 21:08:16 -0700941 std::string ice_sdp;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700942 EXPECT_TRUE(new_candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700943 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700944 // Remote party may be deleted.
945 return;
946 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700947 SendIceMessage(new_candidate->sdp_mid(), new_candidate->sdp_mline_index(),
948 ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700949 }
950 void OnDataChannel(
951 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100952 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700953 data_channel_ = data_channel;
954 data_observer_.reset(new MockDataChannelObserver(data_channel));
955 }
956
deadbeef1dcb1642017-03-29 21:08:16 -0700957 std::string debug_name_;
958
959 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
960
961 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
962 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
963 peer_connection_factory_;
964
Steve Antonede9ca52017-10-16 13:04:27 -0700965 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700966 // Needed to keep track of number of frames sent.
967 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
968 // Needed to keep track of number of frames received.
969 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
970 fake_video_renderers_;
971 // Needed to ensure frames aren't received for removed tracks.
972 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
973 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700974
975 // For remote peer communication.
976 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
977 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700978 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700979
Niels Möller5c7efe72018-05-11 10:34:46 +0200980 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700981 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200982 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
983 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700984 // |local_video_renderer_| attached to the first created local video track.
985 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
986
Seth Hampson2f0d7022018-02-20 11:54:42 -0800987 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700988 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
989 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
990 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800991 std::function<void()> remote_offer_handler_;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700992 std::unique_ptr<IceCandidateReplacerInterface> local_ice_candidate_replacer_;
deadbeef1dcb1642017-03-29 21:08:16 -0700993 rtc::scoped_refptr<DataChannelInterface> data_channel_;
994 std::unique_ptr<MockDataChannelObserver> data_observer_;
995
996 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
997
Steve Antonede9ca52017-10-16 13:04:27 -0700998 std::vector<PeerConnectionInterface::IceConnectionState>
999 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001000 std::vector<PeerConnectionInterface::PeerConnectionState>
1001 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001002 std::vector<PeerConnectionInterface::IceGatheringState>
1003 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001004
Qingsi Wang7685e862018-06-11 20:15:46 -07001005 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1006
deadbeef1dcb1642017-03-29 21:08:16 -07001007 rtc::AsyncInvoker invoker_;
1008
Seth Hampson2f0d7022018-02-20 11:54:42 -08001009 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001010};
1011
Elad Alon99c3fe52017-10-13 16:29:40 +02001012class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1013 public:
1014 virtual ~MockRtcEventLogOutput() = default;
1015 MOCK_CONST_METHOD0(IsActive, bool());
1016 MOCK_METHOD1(Write, bool(const std::string&));
1017};
1018
Seth Hampson2f0d7022018-02-20 11:54:42 -08001019// This helper object is used for both specifying how many audio/video frames
1020// are expected to be received for a caller/callee. It provides helper functions
1021// to specify these expectations. The object initially starts in a state of no
1022// expectations.
1023class MediaExpectations {
1024 public:
1025 enum ExpectFrames {
1026 kExpectSomeFrames,
1027 kExpectNoFrames,
1028 kNoExpectation,
1029 };
1030
1031 void ExpectBidirectionalAudioAndVideo() {
1032 ExpectBidirectionalAudio();
1033 ExpectBidirectionalVideo();
1034 }
1035
1036 void ExpectBidirectionalAudio() {
1037 CallerExpectsSomeAudio();
1038 CalleeExpectsSomeAudio();
1039 }
1040
1041 void ExpectNoAudio() {
1042 CallerExpectsNoAudio();
1043 CalleeExpectsNoAudio();
1044 }
1045
1046 void ExpectBidirectionalVideo() {
1047 CallerExpectsSomeVideo();
1048 CalleeExpectsSomeVideo();
1049 }
1050
1051 void ExpectNoVideo() {
1052 CallerExpectsNoVideo();
1053 CalleeExpectsNoVideo();
1054 }
1055
1056 void CallerExpectsSomeAudioAndVideo() {
1057 CallerExpectsSomeAudio();
1058 CallerExpectsSomeVideo();
1059 }
1060
1061 void CalleeExpectsSomeAudioAndVideo() {
1062 CalleeExpectsSomeAudio();
1063 CalleeExpectsSomeVideo();
1064 }
1065
1066 // Caller's audio functions.
1067 void CallerExpectsSomeAudio(
1068 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1069 caller_audio_expectation_ = kExpectSomeFrames;
1070 caller_audio_frames_expected_ = expected_audio_frames;
1071 }
1072
1073 void CallerExpectsNoAudio() {
1074 caller_audio_expectation_ = kExpectNoFrames;
1075 caller_audio_frames_expected_ = 0;
1076 }
1077
1078 // Caller's video functions.
1079 void CallerExpectsSomeVideo(
1080 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1081 caller_video_expectation_ = kExpectSomeFrames;
1082 caller_video_frames_expected_ = expected_video_frames;
1083 }
1084
1085 void CallerExpectsNoVideo() {
1086 caller_video_expectation_ = kExpectNoFrames;
1087 caller_video_frames_expected_ = 0;
1088 }
1089
1090 // Callee's audio functions.
1091 void CalleeExpectsSomeAudio(
1092 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1093 callee_audio_expectation_ = kExpectSomeFrames;
1094 callee_audio_frames_expected_ = expected_audio_frames;
1095 }
1096
1097 void CalleeExpectsNoAudio() {
1098 callee_audio_expectation_ = kExpectNoFrames;
1099 callee_audio_frames_expected_ = 0;
1100 }
1101
1102 // Callee's video functions.
1103 void CalleeExpectsSomeVideo(
1104 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1105 callee_video_expectation_ = kExpectSomeFrames;
1106 callee_video_frames_expected_ = expected_video_frames;
1107 }
1108
1109 void CalleeExpectsNoVideo() {
1110 callee_video_expectation_ = kExpectNoFrames;
1111 callee_video_frames_expected_ = 0;
1112 }
1113
1114 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1115 ExpectFrames caller_video_expectation_ = kNoExpectation;
1116 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1117 ExpectFrames callee_video_expectation_ = kNoExpectation;
1118 int caller_audio_frames_expected_ = 0;
1119 int caller_video_frames_expected_ = 0;
1120 int callee_audio_frames_expected_ = 0;
1121 int callee_video_frames_expected_ = 0;
1122};
1123
deadbeef1dcb1642017-03-29 21:08:16 -07001124// Tests two PeerConnections connecting to each other end-to-end, using a
1125// virtual network, fake A/V capture and fake encoder/decoders. The
1126// PeerConnections share the threads/socket servers, but use separate versions
1127// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001128class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001129 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001130 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1131 : sdp_semantics_(sdp_semantics),
1132 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001133 fss_(new rtc::FirewallSocketServer(ss_.get())),
1134 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001135 worker_thread_(rtc::Thread::Create()),
1136 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001137 network_thread_->SetName("PCNetworkThread", this);
1138 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001139 RTC_CHECK(network_thread_->Start());
1140 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001141 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001142 }
1143
Seth Hampson2f0d7022018-02-20 11:54:42 -08001144 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001145 // The PeerConnections should deleted before the TurnCustomizers.
1146 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1147 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1148 // that the TurnCustomizer outlives the life of the PeerConnection or else
1149 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001150 if (caller_) {
1151 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001152 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001153 }
1154 if (callee_) {
1155 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001156 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001157 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001158
1159 // If turn servers were created for the test they need to be destroyed on
1160 // the network thread.
1161 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1162 turn_servers_.clear();
1163 turn_customizers_.clear();
1164 });
deadbeef1dcb1642017-03-29 21:08:16 -07001165 }
1166
1167 bool SignalingStateStable() {
1168 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1169 }
1170
deadbeef71452802017-05-07 17:21:01 -07001171 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001172 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1173 // are connected. This is an important distinction. Once we have separate
1174 // ICE and DTLS state, this check needs to use the DTLS state.
1175 return (callee()->ice_connection_state() ==
1176 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1177 callee()->ice_connection_state() ==
1178 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1179 (caller()->ice_connection_state() ==
1180 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1181 caller()->ice_connection_state() ==
1182 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001183 }
1184
Qingsi Wang7685e862018-06-11 20:15:46 -07001185 // When |event_log_factory| is null, the default implementation of the event
1186 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001187 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1188 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001189 const PeerConnectionFactory::Options* options,
1190 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001191 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001192 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1193 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001194 RTCConfiguration modified_config;
1195 if (config) {
1196 modified_config = *config;
1197 }
Steve Anton3acffc32018-04-12 17:21:03 -07001198 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001199 if (!dependencies.cert_generator) {
1200 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001201 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001202 }
1203 std::unique_ptr<PeerConnectionWrapper> client(
1204 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001205
Niels Möllerf06f9232018-08-07 12:32:18 +02001206 if (!client->Init(options, &modified_config, std::move(dependencies),
1207 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001208 std::move(event_log_factory),
1209 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001210 return nullptr;
1211 }
1212 return client;
1213 }
1214
Qingsi Wang7685e862018-06-11 20:15:46 -07001215 std::unique_ptr<PeerConnectionWrapper>
1216 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1217 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001218 const PeerConnectionFactory::Options* options,
1219 const RTCConfiguration* config,
1220 webrtc::PeerConnectionDependencies dependencies) {
1221 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1222 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001223 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001224 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001225 std::move(event_log_factory),
1226 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001227 }
1228
deadbeef1dcb1642017-03-29 21:08:16 -07001229 bool CreatePeerConnectionWrappers() {
1230 return CreatePeerConnectionWrappersWithConfig(
1231 PeerConnectionInterface::RTCConfiguration(),
1232 PeerConnectionInterface::RTCConfiguration());
1233 }
1234
Steve Anton3acffc32018-04-12 17:21:03 -07001235 bool CreatePeerConnectionWrappersWithSdpSemantics(
1236 SdpSemantics caller_semantics,
1237 SdpSemantics callee_semantics) {
1238 // Can't specify the sdp_semantics in the passed-in configuration since it
1239 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1240 // stored in sdp_semantics_. So get around this by modifying the instance
1241 // variable before calling CreatePeerConnectionWrapper for the caller and
1242 // callee PeerConnections.
1243 SdpSemantics original_semantics = sdp_semantics_;
1244 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001245 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001246 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001247 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001248 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001249 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001250 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001251 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001252 sdp_semantics_ = original_semantics;
1253 return caller_ && callee_;
1254 }
1255
deadbeef1dcb1642017-03-29 21:08:16 -07001256 bool CreatePeerConnectionWrappersWithConfig(
1257 const PeerConnectionInterface::RTCConfiguration& caller_config,
1258 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001259 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001260 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001261 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1262 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001263 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001264 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001265 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1266 /*media_transport_factory=*/nullptr);
1267 return caller_ && callee_;
1268 }
1269
1270 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1271 const PeerConnectionInterface::RTCConfiguration& caller_config,
1272 const PeerConnectionInterface::RTCConfiguration& callee_config,
1273 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1274 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1275 caller_ =
1276 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1277 webrtc::PeerConnectionDependencies(nullptr),
1278 nullptr, std::move(caller_factory));
1279 callee_ =
1280 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1281 webrtc::PeerConnectionDependencies(nullptr),
1282 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001283 return caller_ && callee_;
1284 }
1285
1286 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1287 const PeerConnectionInterface::RTCConfiguration& caller_config,
1288 webrtc::PeerConnectionDependencies caller_dependencies,
1289 const PeerConnectionInterface::RTCConfiguration& callee_config,
1290 webrtc::PeerConnectionDependencies callee_dependencies) {
1291 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001292 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001293 std::move(caller_dependencies), nullptr,
1294 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001295 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001296 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001297 std::move(callee_dependencies), nullptr,
1298 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001299 return caller_ && callee_;
1300 }
1301
1302 bool CreatePeerConnectionWrappersWithOptions(
1303 const PeerConnectionFactory::Options& caller_options,
1304 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001305 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001306 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001307 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1308 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001309 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001310 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001311 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1312 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001313 return caller_ && callee_;
1314 }
1315
1316 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1317 PeerConnectionInterface::RTCConfiguration default_config;
1318 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001319 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001320 webrtc::PeerConnectionDependencies(nullptr));
1321 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001322 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001323 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001324 return caller_ && callee_;
1325 }
1326
Seth Hampson2f0d7022018-02-20 11:54:42 -08001327 std::unique_ptr<PeerConnectionWrapper>
1328 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001329 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1330 new FakeRTCCertificateGenerator());
1331 cert_generator->use_alternate_key();
1332
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001333 webrtc::PeerConnectionDependencies dependencies(nullptr);
1334 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001335 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001336 std::move(dependencies), nullptr,
1337 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001338 }
1339
Seth Hampsonaed71642018-06-11 07:41:32 -07001340 cricket::TestTurnServer* CreateTurnServer(
1341 rtc::SocketAddress internal_address,
1342 rtc::SocketAddress external_address,
1343 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1344 const std::string& common_name = "test turn server") {
1345 rtc::Thread* thread = network_thread();
1346 std::unique_ptr<cricket::TestTurnServer> turn_server =
1347 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1348 RTC_FROM_HERE,
1349 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001350 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001351 thread, internal_address, external_address, type,
1352 /*ignore_bad_certs=*/true, common_name);
1353 });
1354 turn_servers_.push_back(std::move(turn_server));
1355 // Interactions with the turn server should be done on the network thread.
1356 return turn_servers_.back().get();
1357 }
1358
1359 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1360 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1361 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1362 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001363 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001364 turn_customizers_.push_back(std::move(turn_customizer));
1365 // Interactions with the turn customizer should be done on the network
1366 // thread.
1367 return turn_customizers_.back().get();
1368 }
1369
1370 // Checks that the function counters for a TestTurnCustomizer are greater than
1371 // 0.
1372 void ExpectTurnCustomizerCountersIncremented(
1373 cricket::TestTurnCustomizer* turn_customizer) {
1374 unsigned int allow_channel_data_counter =
1375 network_thread()->Invoke<unsigned int>(
1376 RTC_FROM_HERE, [turn_customizer] {
1377 return turn_customizer->allow_channel_data_cnt_;
1378 });
1379 EXPECT_GT(allow_channel_data_counter, 0u);
1380 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1381 RTC_FROM_HERE,
1382 [turn_customizer] { return turn_customizer->modify_cnt_; });
1383 EXPECT_GT(modify_counter, 0u);
1384 }
1385
deadbeef1dcb1642017-03-29 21:08:16 -07001386 // Once called, SDP blobs and ICE candidates will be automatically signaled
1387 // between PeerConnections.
1388 void ConnectFakeSignaling() {
1389 caller_->set_signaling_message_receiver(callee_.get());
1390 callee_->set_signaling_message_receiver(caller_.get());
1391 }
1392
Steve Antonede9ca52017-10-16 13:04:27 -07001393 // Once called, SDP blobs will be automatically signaled between
1394 // PeerConnections. Note that ICE candidates will not be signaled unless they
1395 // are in the exchanged SDP blobs.
1396 void ConnectFakeSignalingForSdpOnly() {
1397 ConnectFakeSignaling();
1398 SetSignalIceCandidates(false);
1399 }
1400
deadbeef1dcb1642017-03-29 21:08:16 -07001401 void SetSignalingDelayMs(int delay_ms) {
1402 caller_->set_signaling_delay_ms(delay_ms);
1403 callee_->set_signaling_delay_ms(delay_ms);
1404 }
1405
Steve Antonede9ca52017-10-16 13:04:27 -07001406 void SetSignalIceCandidates(bool signal) {
1407 caller_->set_signal_ice_candidates(signal);
1408 callee_->set_signal_ice_candidates(signal);
1409 }
1410
deadbeef1dcb1642017-03-29 21:08:16 -07001411 // Messages may get lost on the unreliable DataChannel, so we send multiple
1412 // times to avoid test flakiness.
1413 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1414 const std::string& data,
1415 int retries) {
1416 for (int i = 0; i < retries; ++i) {
1417 dc->Send(DataBuffer(data));
1418 }
1419 }
1420
1421 rtc::Thread* network_thread() { return network_thread_.get(); }
1422
1423 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1424
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001425 webrtc::MediaTransportPair* loopback_media_transports() {
1426 return &loopback_media_transports_;
1427 }
1428
deadbeef1dcb1642017-03-29 21:08:16 -07001429 PeerConnectionWrapper* caller() { return caller_.get(); }
1430
1431 // Set the |caller_| to the |wrapper| passed in and return the
1432 // original |caller_|.
1433 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1434 PeerConnectionWrapper* wrapper) {
1435 PeerConnectionWrapper* old = caller_.release();
1436 caller_.reset(wrapper);
1437 return old;
1438 }
1439
1440 PeerConnectionWrapper* callee() { return callee_.get(); }
1441
1442 // Set the |callee_| to the |wrapper| passed in and return the
1443 // original |callee_|.
1444 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1445 PeerConnectionWrapper* wrapper) {
1446 PeerConnectionWrapper* old = callee_.release();
1447 callee_.reset(wrapper);
1448 return old;
1449 }
1450
Steve Antonede9ca52017-10-16 13:04:27 -07001451 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1452
Seth Hampson2f0d7022018-02-20 11:54:42 -08001453 // Expects the provided number of new frames to be received within
1454 // kMaxWaitForFramesMs. The new expected frames are specified in
1455 // |media_expectations|. Returns false if any of the expectations were
1456 // not met.
1457 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1458 // First initialize the expected frame counts based upon the current
1459 // frame count.
1460 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1461 if (media_expectations.caller_audio_expectation_ ==
1462 MediaExpectations::kExpectSomeFrames) {
1463 total_caller_audio_frames_expected +=
1464 media_expectations.caller_audio_frames_expected_;
1465 }
1466 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001467 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001468 if (media_expectations.caller_video_expectation_ ==
1469 MediaExpectations::kExpectSomeFrames) {
1470 total_caller_video_frames_expected +=
1471 media_expectations.caller_video_frames_expected_;
1472 }
1473 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1474 if (media_expectations.callee_audio_expectation_ ==
1475 MediaExpectations::kExpectSomeFrames) {
1476 total_callee_audio_frames_expected +=
1477 media_expectations.callee_audio_frames_expected_;
1478 }
1479 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001480 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001481 if (media_expectations.callee_video_expectation_ ==
1482 MediaExpectations::kExpectSomeFrames) {
1483 total_callee_video_frames_expected +=
1484 media_expectations.callee_video_frames_expected_;
1485 }
deadbeef1dcb1642017-03-29 21:08:16 -07001486
Seth Hampson2f0d7022018-02-20 11:54:42 -08001487 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001488 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001489 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001490 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001491 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001492 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001493 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001494 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001495 total_callee_video_frames_expected,
1496 kMaxWaitForFramesMs);
1497 bool expectations_correct =
1498 caller()->audio_frames_received() >=
1499 total_caller_audio_frames_expected &&
1500 caller()->min_video_frames_received_per_track() >=
1501 total_caller_video_frames_expected &&
1502 callee()->audio_frames_received() >=
1503 total_callee_audio_frames_expected &&
1504 callee()->min_video_frames_received_per_track() >=
1505 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001506
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 // After the combined wait, print out a more detailed message upon
1508 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001509 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001510 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001511 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001512 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001513 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001515 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001516 total_callee_video_frames_expected);
1517
1518 // We want to make sure nothing unexpected was received.
1519 if (media_expectations.caller_audio_expectation_ ==
1520 MediaExpectations::kExpectNoFrames) {
1521 EXPECT_EQ(caller()->audio_frames_received(),
1522 total_caller_audio_frames_expected);
1523 if (caller()->audio_frames_received() !=
1524 total_caller_audio_frames_expected) {
1525 expectations_correct = false;
1526 }
1527 }
1528 if (media_expectations.caller_video_expectation_ ==
1529 MediaExpectations::kExpectNoFrames) {
1530 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1531 total_caller_video_frames_expected);
1532 if (caller()->min_video_frames_received_per_track() !=
1533 total_caller_video_frames_expected) {
1534 expectations_correct = false;
1535 }
1536 }
1537 if (media_expectations.callee_audio_expectation_ ==
1538 MediaExpectations::kExpectNoFrames) {
1539 EXPECT_EQ(callee()->audio_frames_received(),
1540 total_callee_audio_frames_expected);
1541 if (callee()->audio_frames_received() !=
1542 total_callee_audio_frames_expected) {
1543 expectations_correct = false;
1544 }
1545 }
1546 if (media_expectations.callee_video_expectation_ ==
1547 MediaExpectations::kExpectNoFrames) {
1548 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1549 total_callee_video_frames_expected);
1550 if (callee()->min_video_frames_received_per_track() !=
1551 total_callee_video_frames_expected) {
1552 expectations_correct = false;
1553 }
1554 }
1555 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001556 }
1557
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001558 void TestNegotiatedCipherSuite(
1559 const PeerConnectionFactory::Options& caller_options,
1560 const PeerConnectionFactory::Options& callee_options,
1561 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001562 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1563 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001564 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001565 caller()->AddAudioVideoTracks();
1566 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001567 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001568 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001569 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001570 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001571 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001572 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001573 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1574 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001575 }
1576
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001577 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1578 bool remote_gcm_enabled,
1579 int expected_cipher_suite) {
1580 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001581 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1582 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001583 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001584 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1585 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001586 TestNegotiatedCipherSuite(caller_options, callee_options,
1587 expected_cipher_suite);
1588 }
1589
Seth Hampson2f0d7022018-02-20 11:54:42 -08001590 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001591 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001592
deadbeef1dcb1642017-03-29 21:08:16 -07001593 private:
1594 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001595 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001596 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001597 // |network_thread_| and |worker_thread_| are used by both
1598 // |caller_| and |callee_| so they must be destroyed
1599 // later.
1600 std::unique_ptr<rtc::Thread> network_thread_;
1601 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001602 // The turn servers and turn customizers should be accessed & deleted on the
1603 // network thread to avoid a race with the socket read/write that occurs
1604 // on the network thread.
1605 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1606 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001607 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001608 std::unique_ptr<PeerConnectionWrapper> caller_;
1609 std::unique_ptr<PeerConnectionWrapper> callee_;
1610};
1611
Seth Hampson2f0d7022018-02-20 11:54:42 -08001612class PeerConnectionIntegrationTest
1613 : public PeerConnectionIntegrationBaseTest,
1614 public ::testing::WithParamInterface<SdpSemantics> {
1615 protected:
1616 PeerConnectionIntegrationTest()
1617 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1618};
1619
1620class PeerConnectionIntegrationTestPlanB
1621 : public PeerConnectionIntegrationBaseTest {
1622 protected:
1623 PeerConnectionIntegrationTestPlanB()
1624 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1625};
1626
1627class PeerConnectionIntegrationTestUnifiedPlan
1628 : public PeerConnectionIntegrationBaseTest {
1629 protected:
1630 PeerConnectionIntegrationTestUnifiedPlan()
1631 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1632};
1633
deadbeef1dcb1642017-03-29 21:08:16 -07001634// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1635// includes testing that the callback is invoked if an observer is connected
1636// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001637TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001638 RtpReceiverObserverOnFirstPacketReceived) {
1639 ASSERT_TRUE(CreatePeerConnectionWrappers());
1640 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001641 caller()->AddAudioVideoTracks();
1642 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001643 // Start offer/answer exchange and wait for it to complete.
1644 caller()->CreateAndSetAndSignalOffer();
1645 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1646 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001647 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1648 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001649 // Wait for all "first packet received" callbacks to be fired.
1650 EXPECT_TRUE_WAIT(
1651 std::all_of(caller()->rtp_receiver_observers().begin(),
1652 caller()->rtp_receiver_observers().end(),
1653 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1654 return o->first_packet_received();
1655 }),
1656 kMaxWaitForFramesMs);
1657 EXPECT_TRUE_WAIT(
1658 std::all_of(callee()->rtp_receiver_observers().begin(),
1659 callee()->rtp_receiver_observers().end(),
1660 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1661 return o->first_packet_received();
1662 }),
1663 kMaxWaitForFramesMs);
1664 // If new observers are set after the first packet was already received, the
1665 // callback should still be invoked.
1666 caller()->ResetRtpReceiverObservers();
1667 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001668 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1669 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001670 EXPECT_TRUE(
1671 std::all_of(caller()->rtp_receiver_observers().begin(),
1672 caller()->rtp_receiver_observers().end(),
1673 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1674 return o->first_packet_received();
1675 }));
1676 EXPECT_TRUE(
1677 std::all_of(callee()->rtp_receiver_observers().begin(),
1678 callee()->rtp_receiver_observers().end(),
1679 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1680 return o->first_packet_received();
1681 }));
1682}
1683
1684class DummyDtmfObserver : public DtmfSenderObserverInterface {
1685 public:
1686 DummyDtmfObserver() : completed_(false) {}
1687
1688 // Implements DtmfSenderObserverInterface.
1689 void OnToneChange(const std::string& tone) override {
1690 tones_.push_back(tone);
1691 if (tone.empty()) {
1692 completed_ = true;
1693 }
1694 }
1695
1696 const std::vector<std::string>& tones() const { return tones_; }
1697 bool completed() const { return completed_; }
1698
1699 private:
1700 bool completed_;
1701 std::vector<std::string> tones_;
1702};
1703
1704// Assumes |sender| already has an audio track added and the offer/answer
1705// exchange is done.
1706void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1707 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001708 // We should be able to get a DTMF sender from the local sender.
1709 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1710 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1711 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001712 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001713 dtmf_sender->RegisterObserver(&observer);
1714
1715 // Test the DtmfSender object just created.
1716 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1717 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1718
1719 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1720 std::vector<std::string> tones = {"1", "a", ""};
1721 EXPECT_EQ(tones, observer.tones());
1722 dtmf_sender->UnregisterObserver();
1723 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1724}
1725
1726// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1727// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001728TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001729 ASSERT_TRUE(CreatePeerConnectionWrappers());
1730 ConnectFakeSignaling();
1731 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001732 caller()->AddAudioTrack();
1733 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001734 caller()->CreateAndSetAndSignalOffer();
1735 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001736 // DTLS must finish before the DTMF sender can be used reliably.
1737 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001738 TestDtmfFromSenderToReceiver(caller(), callee());
1739 TestDtmfFromSenderToReceiver(callee(), caller());
1740}
1741
1742// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1743// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001744TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001745 ASSERT_TRUE(CreatePeerConnectionWrappers());
1746 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001747
deadbeef1dcb1642017-03-29 21:08:16 -07001748 // Do normal offer/answer and wait for some frames to be received in each
1749 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001750 caller()->AddAudioVideoTracks();
1751 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001752 caller()->CreateAndSetAndSignalOffer();
1753 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001754 MediaExpectations media_expectations;
1755 media_expectations.ExpectBidirectionalAudioAndVideo();
1756 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001757 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1758 webrtc::kEnumCounterKeyProtocolDtls));
1759 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1760 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001761}
1762
1763// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001764TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001765 PeerConnectionInterface::RTCConfiguration sdes_config;
1766 sdes_config.enable_dtls_srtp.emplace(false);
1767 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1768 ConnectFakeSignaling();
1769
1770 // Do normal offer/answer and wait for some frames to be received in each
1771 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001772 caller()->AddAudioVideoTracks();
1773 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001774 caller()->CreateAndSetAndSignalOffer();
1775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776 MediaExpectations media_expectations;
1777 media_expectations.ExpectBidirectionalAudioAndVideo();
1778 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001779 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1780 webrtc::kEnumCounterKeyProtocolSdes));
1781 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1782 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001783}
1784
Steve Anton8c0f7a72017-10-03 10:03:10 -07001785// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1786// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001787TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001788 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1789 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1790 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1791 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1792 return pc->GetRemoteAudioSSLCertificate();
1793 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001794 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1795 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1796 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1797 return pc->GetRemoteAudioSSLCertChain();
1798 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001799
1800 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1801 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1802
1803 // Configure each side with a known certificate so they can be compared later.
1804 PeerConnectionInterface::RTCConfiguration caller_config;
1805 caller_config.enable_dtls_srtp.emplace(true);
1806 caller_config.certificates.push_back(caller_cert);
1807 PeerConnectionInterface::RTCConfiguration callee_config;
1808 callee_config.enable_dtls_srtp.emplace(true);
1809 callee_config.certificates.push_back(callee_cert);
1810 ASSERT_TRUE(
1811 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1812 ConnectFakeSignaling();
1813
1814 // When first initialized, there should not be a remote SSL certificate (and
1815 // calling this method should not crash).
1816 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1817 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001818 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1819 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001820
Steve Anton15324772018-01-16 10:26:49 -08001821 caller()->AddAudioTrack();
1822 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001823 caller()->CreateAndSetAndSignalOffer();
1824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1825 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1826
1827 // Once DTLS has been connected, each side should return the other's SSL
1828 // certificate when calling GetRemoteAudioSSLCertificate.
1829
1830 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1831 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001832 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001833 caller_remote_cert->ToPEMString());
1834
1835 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1836 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001837 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001838 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001839
1840 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1841 ASSERT_TRUE(caller_remote_cert_chain);
1842 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1843 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001844 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001845 remote_cert->ToPEMString());
1846
1847 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1848 ASSERT_TRUE(callee_remote_cert_chain);
1849 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1850 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001851 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001852 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001853}
1854
deadbeef1dcb1642017-03-29 21:08:16 -07001855// This test sets up a call between two parties with a source resolution of
1856// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001857TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001858 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1859 ASSERT_TRUE(CreatePeerConnectionWrappers());
1860 ConnectFakeSignaling();
1861
Niels Möller5c7efe72018-05-11 10:34:46 +02001862 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1863 webrtc::FakePeriodicVideoSource::Config config;
1864 config.width = 1280;
1865 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001866 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001867 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1868 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001869
1870 // Do normal offer/answer and wait for at least one frame to be received in
1871 // each direction.
1872 caller()->CreateAndSetAndSignalOffer();
1873 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1874 callee()->min_video_frames_received_per_track() > 0,
1875 kMaxWaitForFramesMs);
1876
1877 // Check rendered aspect ratio.
1878 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1879 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1880 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1881 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1882}
1883
1884// This test sets up an one-way call, with media only from caller to
1885// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001886TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001887 ASSERT_TRUE(CreatePeerConnectionWrappers());
1888 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001889 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001890 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001891 MediaExpectations media_expectations;
1892 media_expectations.CalleeExpectsSomeAudioAndVideo();
1893 media_expectations.CallerExpectsNoAudio();
1894 media_expectations.CallerExpectsNoVideo();
1895 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001896}
1897
1898// This test sets up a audio call initially, with the callee rejecting video
1899// initially. Then later the callee decides to upgrade to audio/video, and
1900// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001901TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001902 ASSERT_TRUE(CreatePeerConnectionWrappers());
1903 ConnectFakeSignaling();
1904 // Initially, offer an audio/video stream from the caller, but refuse to
1905 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001906 caller()->AddAudioVideoTracks();
1907 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001908 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1909 PeerConnectionInterface::RTCOfferAnswerOptions options;
1910 options.offer_to_receive_video = 0;
1911 callee()->SetOfferAnswerOptions(options);
1912 } else {
1913 callee()->SetRemoteOfferHandler([this] {
1914 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1915 });
1916 }
deadbeef1dcb1642017-03-29 21:08:16 -07001917 // Do offer/answer and make sure audio is still received end-to-end.
1918 caller()->CreateAndSetAndSignalOffer();
1919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001920 {
1921 MediaExpectations media_expectations;
1922 media_expectations.ExpectBidirectionalAudio();
1923 media_expectations.ExpectNoVideo();
1924 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1925 }
deadbeef1dcb1642017-03-29 21:08:16 -07001926 // Sanity check that the callee's description has a rejected video section.
1927 ASSERT_NE(nullptr, callee()->pc()->local_description());
1928 const ContentInfo* callee_video_content =
1929 GetFirstVideoContent(callee()->pc()->local_description()->description());
1930 ASSERT_NE(nullptr, callee_video_content);
1931 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001932
deadbeef1dcb1642017-03-29 21:08:16 -07001933 // Now negotiate with video and ensure negotiation succeeds, with video
1934 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001935 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001936 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1937 PeerConnectionInterface::RTCOfferAnswerOptions options;
1938 options.offer_to_receive_video = 1;
1939 callee()->SetOfferAnswerOptions(options);
1940 } else {
1941 callee()->SetRemoteOfferHandler(nullptr);
1942 caller()->SetRemoteOfferHandler([this] {
1943 // The caller creates a new transceiver to receive video on when receiving
1944 // the offer, but by default it is send only.
1945 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001946 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001947 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1948 transceivers[2]->receiver()->media_type());
1949 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1950 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1951 });
1952 }
deadbeef1dcb1642017-03-29 21:08:16 -07001953 callee()->CreateAndSetAndSignalOffer();
1954 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001955 {
1956 // Expect additional audio frames to be received after the upgrade.
1957 MediaExpectations media_expectations;
1958 media_expectations.ExpectBidirectionalAudioAndVideo();
1959 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1960 }
deadbeef1dcb1642017-03-29 21:08:16 -07001961}
1962
deadbeef4389b4d2017-09-07 09:07:36 -07001963// Simpler than the above test; just add an audio track to an established
1964// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001965TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001966 ASSERT_TRUE(CreatePeerConnectionWrappers());
1967 ConnectFakeSignaling();
1968 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001969 caller()->AddVideoTrack();
1970 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001971 caller()->CreateAndSetAndSignalOffer();
1972 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1973 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001974 caller()->AddAudioTrack();
1975 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001976 caller()->CreateAndSetAndSignalOffer();
1977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1978 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001979 MediaExpectations media_expectations;
1980 media_expectations.ExpectBidirectionalAudioAndVideo();
1981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001982}
1983
deadbeef1dcb1642017-03-29 21:08:16 -07001984// This test sets up a call that's transferred to a new caller with a different
1985// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001986TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001987 ASSERT_TRUE(CreatePeerConnectionWrappers());
1988 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001989 caller()->AddAudioVideoTracks();
1990 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001991 caller()->CreateAndSetAndSignalOffer();
1992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1993
1994 // Keep the original peer around which will still send packets to the
1995 // receiving client. These SRTP packets will be dropped.
1996 std::unique_ptr<PeerConnectionWrapper> original_peer(
1997 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001999 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2000 // directly above.
2001 original_peer->pc()->Close();
2002
2003 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002004 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002005 caller()->CreateAndSetAndSignalOffer();
2006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2007 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002008 MediaExpectations media_expectations;
2009 media_expectations.ExpectBidirectionalAudioAndVideo();
2010 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002011}
2012
2013// This test sets up a call that's transferred to a new callee with a different
2014// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002015TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002016 ASSERT_TRUE(CreatePeerConnectionWrappers());
2017 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002018 caller()->AddAudioVideoTracks();
2019 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002020 caller()->CreateAndSetAndSignalOffer();
2021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2022
2023 // Keep the original peer around which will still send packets to the
2024 // receiving client. These SRTP packets will be dropped.
2025 std::unique_ptr<PeerConnectionWrapper> original_peer(
2026 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002028 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2029 // directly above.
2030 original_peer->pc()->Close();
2031
2032 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002033 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002034 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2035 caller()->CreateAndSetAndSignalOffer();
2036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2037 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002038 MediaExpectations media_expectations;
2039 media_expectations.ExpectBidirectionalAudioAndVideo();
2040 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002041}
2042
2043// This test sets up a non-bundled call and negotiates bundling at the same
2044// time as starting an ICE restart. When bundling is in effect in the restart,
2045// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002046TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002047 ASSERT_TRUE(CreatePeerConnectionWrappers());
2048 ConnectFakeSignaling();
2049
Steve Anton15324772018-01-16 10:26:49 -08002050 caller()->AddAudioVideoTracks();
2051 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002052 // Remove the bundle group from the SDP received by the callee.
2053 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2054 desc->RemoveGroupByName("BUNDLE");
2055 });
2056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002058 {
2059 MediaExpectations media_expectations;
2060 media_expectations.ExpectBidirectionalAudioAndVideo();
2061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2062 }
deadbeef1dcb1642017-03-29 21:08:16 -07002063 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2064 callee()->SetReceivedSdpMunger(nullptr);
2065 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2066 caller()->CreateAndSetAndSignalOffer();
2067 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2068
2069 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002070 {
2071 MediaExpectations media_expectations;
2072 media_expectations.ExpectBidirectionalAudioAndVideo();
2073 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2074 }
deadbeef1dcb1642017-03-29 21:08:16 -07002075}
2076
2077// Test CVO (Coordination of Video Orientation). If a video source is rotated
2078// and both peers support the CVO RTP header extension, the actual video frames
2079// don't need to be encoded in different resolutions, since the rotation is
2080// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002081TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002082 ASSERT_TRUE(CreatePeerConnectionWrappers());
2083 ConnectFakeSignaling();
2084 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002085 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002086 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002087 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002088 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2089
2090 // Wait for video frames to be received by both sides.
2091 caller()->CreateAndSetAndSignalOffer();
2092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2093 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2094 callee()->min_video_frames_received_per_track() > 0,
2095 kMaxWaitForFramesMs);
2096
2097 // Ensure that the aspect ratio is unmodified.
2098 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2099 // not just assumed.
2100 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2101 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2102 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2103 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2104 // Ensure that the CVO bits were surfaced to the renderer.
2105 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2106 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2107}
2108
2109// Test that when the CVO extension isn't supported, video is rotated the
2110// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002111TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002112 ASSERT_TRUE(CreatePeerConnectionWrappers());
2113 ConnectFakeSignaling();
2114 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002115 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002116 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002117 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002118 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2119
2120 // Remove the CVO extension from the offered SDP.
2121 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2122 cricket::VideoContentDescription* video =
2123 GetFirstVideoContentDescription(desc);
2124 video->ClearRtpHeaderExtensions();
2125 });
2126 // Wait for video frames to be received by both sides.
2127 caller()->CreateAndSetAndSignalOffer();
2128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2129 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2130 callee()->min_video_frames_received_per_track() > 0,
2131 kMaxWaitForFramesMs);
2132
2133 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2134 // rotation.
2135 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2136 // not just assumed.
2137 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2138 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2139 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2140 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2141 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2142 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2143 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2144}
2145
deadbeef1dcb1642017-03-29 21:08:16 -07002146// Test that if the answerer rejects the audio m= section, no audio is sent or
2147// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002148TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002149 ASSERT_TRUE(CreatePeerConnectionWrappers());
2150 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002151 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002152 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2153 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2154 // it will reject the audio m= section completely.
2155 PeerConnectionInterface::RTCOfferAnswerOptions options;
2156 options.offer_to_receive_audio = 0;
2157 callee()->SetOfferAnswerOptions(options);
2158 } else {
2159 // Stopping the audio RtpTransceiver will cause the media section to be
2160 // rejected in the answer.
2161 callee()->SetRemoteOfferHandler([this] {
2162 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2163 });
2164 }
Steve Anton15324772018-01-16 10:26:49 -08002165 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002166 // Do offer/answer and wait for successful end-to-end video frames.
2167 caller()->CreateAndSetAndSignalOffer();
2168 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002169 MediaExpectations media_expectations;
2170 media_expectations.ExpectBidirectionalVideo();
2171 media_expectations.ExpectNoAudio();
2172 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2173
deadbeef1dcb1642017-03-29 21:08:16 -07002174 // Sanity check that the callee's description has a rejected audio section.
2175 ASSERT_NE(nullptr, callee()->pc()->local_description());
2176 const ContentInfo* callee_audio_content =
2177 GetFirstAudioContent(callee()->pc()->local_description()->description());
2178 ASSERT_NE(nullptr, callee_audio_content);
2179 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002180 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2181 // The caller's transceiver should have stopped after receiving the answer.
2182 EXPECT_TRUE(caller()
2183 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2184 ->stopped());
2185 }
deadbeef1dcb1642017-03-29 21:08:16 -07002186}
2187
2188// Test that if the answerer rejects the video m= section, no video is sent or
2189// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002190TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002191 ASSERT_TRUE(CreatePeerConnectionWrappers());
2192 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002193 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002194 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2195 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2196 // it will reject the video m= section completely.
2197 PeerConnectionInterface::RTCOfferAnswerOptions options;
2198 options.offer_to_receive_video = 0;
2199 callee()->SetOfferAnswerOptions(options);
2200 } else {
2201 // Stopping the video RtpTransceiver will cause the media section to be
2202 // rejected in the answer.
2203 callee()->SetRemoteOfferHandler([this] {
2204 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2205 });
2206 }
Steve Anton15324772018-01-16 10:26:49 -08002207 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002208 // Do offer/answer and wait for successful end-to-end audio frames.
2209 caller()->CreateAndSetAndSignalOffer();
2210 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211 MediaExpectations media_expectations;
2212 media_expectations.ExpectBidirectionalAudio();
2213 media_expectations.ExpectNoVideo();
2214 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2215
deadbeef1dcb1642017-03-29 21:08:16 -07002216 // Sanity check that the callee's description has a rejected video section.
2217 ASSERT_NE(nullptr, callee()->pc()->local_description());
2218 const ContentInfo* callee_video_content =
2219 GetFirstVideoContent(callee()->pc()->local_description()->description());
2220 ASSERT_NE(nullptr, callee_video_content);
2221 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002222 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2223 // The caller's transceiver should have stopped after receiving the answer.
2224 EXPECT_TRUE(caller()
2225 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2226 ->stopped());
2227 }
deadbeef1dcb1642017-03-29 21:08:16 -07002228}
2229
2230// Test that if the answerer rejects both audio and video m= sections, nothing
2231// bad happens.
2232// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2233// test anything but the fact that negotiation succeeds, which doesn't mean
2234// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002235TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002236 ASSERT_TRUE(CreatePeerConnectionWrappers());
2237 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002238 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002239 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2240 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2241 // will reject both audio and video m= sections.
2242 PeerConnectionInterface::RTCOfferAnswerOptions options;
2243 options.offer_to_receive_audio = 0;
2244 options.offer_to_receive_video = 0;
2245 callee()->SetOfferAnswerOptions(options);
2246 } else {
2247 callee()->SetRemoteOfferHandler([this] {
2248 // Stopping all transceivers will cause all media sections to be rejected.
2249 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2250 transceiver->Stop();
2251 }
2252 });
2253 }
deadbeef1dcb1642017-03-29 21:08:16 -07002254 // Do offer/answer and wait for stable signaling state.
2255 caller()->CreateAndSetAndSignalOffer();
2256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002257
deadbeef1dcb1642017-03-29 21:08:16 -07002258 // Sanity check that the callee's description has rejected m= sections.
2259 ASSERT_NE(nullptr, callee()->pc()->local_description());
2260 const ContentInfo* callee_audio_content =
2261 GetFirstAudioContent(callee()->pc()->local_description()->description());
2262 ASSERT_NE(nullptr, callee_audio_content);
2263 EXPECT_TRUE(callee_audio_content->rejected);
2264 const ContentInfo* callee_video_content =
2265 GetFirstVideoContent(callee()->pc()->local_description()->description());
2266 ASSERT_NE(nullptr, callee_video_content);
2267 EXPECT_TRUE(callee_video_content->rejected);
2268}
2269
2270// This test sets up an audio and video call between two parties. After the
2271// call runs for a while, the caller sends an updated offer with video being
2272// rejected. Once the re-negotiation is done, the video flow should stop and
2273// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002274TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002275 ASSERT_TRUE(CreatePeerConnectionWrappers());
2276 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002277 caller()->AddAudioVideoTracks();
2278 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002279 caller()->CreateAndSetAndSignalOffer();
2280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002281 {
2282 MediaExpectations media_expectations;
2283 media_expectations.ExpectBidirectionalAudioAndVideo();
2284 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2285 }
deadbeef1dcb1642017-03-29 21:08:16 -07002286 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002287 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2288 caller()->SetGeneratedSdpMunger(
2289 [](cricket::SessionDescription* description) {
2290 for (cricket::ContentInfo& content : description->contents()) {
2291 if (cricket::IsVideoContent(&content)) {
2292 content.rejected = true;
2293 }
2294 }
2295 });
2296 } else {
2297 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2298 }
deadbeef1dcb1642017-03-29 21:08:16 -07002299 caller()->CreateAndSetAndSignalOffer();
2300 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2301
2302 // Sanity check that the caller's description has a rejected video section.
2303 ASSERT_NE(nullptr, caller()->pc()->local_description());
2304 const ContentInfo* caller_video_content =
2305 GetFirstVideoContent(caller()->pc()->local_description()->description());
2306 ASSERT_NE(nullptr, caller_video_content);
2307 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002308 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002309 {
2310 MediaExpectations media_expectations;
2311 media_expectations.ExpectBidirectionalAudio();
2312 media_expectations.ExpectNoVideo();
2313 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2314 }
deadbeef1dcb1642017-03-29 21:08:16 -07002315}
2316
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002317// Do one offer/answer with audio, another that disables it (rejecting the m=
2318// section), and another that re-enables it. Regression test for:
2319// bugs.webrtc.org/6023
2320TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2321 ASSERT_TRUE(CreatePeerConnectionWrappers());
2322 ConnectFakeSignaling();
2323
2324 // Add audio track, do normal offer/answer.
2325 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2326 caller()->CreateLocalAudioTrack();
2327 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2328 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2329 caller()->CreateAndSetAndSignalOffer();
2330 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2331
2332 // Remove audio track, and set offer_to_receive_audio to false to cause the
2333 // m= section to be completely disabled, not just "recvonly".
2334 caller()->pc()->RemoveTrack(sender);
2335 PeerConnectionInterface::RTCOfferAnswerOptions options;
2336 options.offer_to_receive_audio = 0;
2337 caller()->SetOfferAnswerOptions(options);
2338 caller()->CreateAndSetAndSignalOffer();
2339 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2340
2341 // Add the audio track again, expecting negotiation to succeed and frames to
2342 // flow.
2343 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2344 options.offer_to_receive_audio = 1;
2345 caller()->SetOfferAnswerOptions(options);
2346 caller()->CreateAndSetAndSignalOffer();
2347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2348
2349 MediaExpectations media_expectations;
2350 media_expectations.CalleeExpectsSomeAudio();
2351 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2352}
2353
deadbeef1dcb1642017-03-29 21:08:16 -07002354// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2355// is needed to support legacy endpoints.
2356// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2357// add a test for an end-to-end test without MID signaling either (basically,
2358// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002359TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002360 ASSERT_TRUE(CreatePeerConnectionWrappers());
2361 ConnectFakeSignaling();
2362 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002363 caller()->AddAudioVideoTracks();
2364 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002365 // Remove SSRCs and MSIDs from the received offer SDP.
2366 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002367 caller()->CreateAndSetAndSignalOffer();
2368 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002369 MediaExpectations media_expectations;
2370 media_expectations.ExpectBidirectionalAudioAndVideo();
2371 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002372}
2373
Seth Hampson5897a6e2018-04-03 11:16:33 -07002374// Basic end-to-end test, without SSRC signaling. This means that the track
2375// was created properly and frames are delivered when the MSIDs are communicated
2376// with a=msid lines and no a=ssrc lines.
2377TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2378 EndToEndCallWithoutSsrcSignaling) {
2379 const char kStreamId[] = "streamId";
2380 ASSERT_TRUE(CreatePeerConnectionWrappers());
2381 ConnectFakeSignaling();
2382 // Add just audio tracks.
2383 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2384 callee()->AddAudioTrack();
2385
2386 // Remove SSRCs from the received offer SDP.
2387 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2388 caller()->CreateAndSetAndSignalOffer();
2389 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2390 MediaExpectations media_expectations;
2391 media_expectations.ExpectBidirectionalAudio();
2392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2393}
2394
Steve Antondf527fd2018-04-27 15:52:03 -07002395// Tests that video flows between multiple video tracks when SSRCs are not
2396// signaled. This exercises the MID RTP header extension which is needed to
2397// demux the incoming video tracks.
2398TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2399 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2400 ASSERT_TRUE(CreatePeerConnectionWrappers());
2401 ConnectFakeSignaling();
2402 caller()->AddVideoTrack();
2403 caller()->AddVideoTrack();
2404 callee()->AddVideoTrack();
2405 callee()->AddVideoTrack();
2406
2407 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2408 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2409 caller()->CreateAndSetAndSignalOffer();
2410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2411 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2412 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2413
2414 // Expect video to be received in both directions on both tracks.
2415 MediaExpectations media_expectations;
2416 media_expectations.ExpectBidirectionalVideo();
2417 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2418}
2419
Henrik Boström5b147782018-12-04 11:25:05 +01002420TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2421 ASSERT_TRUE(CreatePeerConnectionWrappers());
2422 ConnectFakeSignaling();
2423 caller()->AddAudioTrack();
2424 caller()->AddVideoTrack();
2425 caller()->CreateAndSetAndSignalOffer();
2426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2427 auto callee_receivers = callee()->pc()->GetReceivers();
2428 ASSERT_EQ(2u, callee_receivers.size());
2429 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2430 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2431}
2432
2433TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2434 ASSERT_TRUE(CreatePeerConnectionWrappers());
2435 ConnectFakeSignaling();
2436 caller()->AddAudioTrack();
2437 caller()->AddVideoTrack();
2438 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2439 caller()->CreateAndSetAndSignalOffer();
2440 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2441 auto callee_receivers = callee()->pc()->GetReceivers();
2442 ASSERT_EQ(2u, callee_receivers.size());
2443 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2444 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2445 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2446 callee_receivers[1]->stream_ids()[0]);
2447 EXPECT_EQ(callee_receivers[0]->streams()[0],
2448 callee_receivers[1]->streams()[0]);
2449}
2450
deadbeef1dcb1642017-03-29 21:08:16 -07002451// Test that if two video tracks are sent (from caller to callee, in this test),
2452// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002453TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002454 ASSERT_TRUE(CreatePeerConnectionWrappers());
2455 ConnectFakeSignaling();
2456 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002457 caller()->AddAudioVideoTracks();
2458 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002459 caller()->CreateAndSetAndSignalOffer();
2460 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002461 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002462
2463 MediaExpectations media_expectations;
2464 media_expectations.CalleeExpectsSomeAudioAndVideo();
2465 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002466}
2467
2468static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2469 bool first = true;
2470 for (cricket::ContentInfo& content : desc->contents()) {
2471 if (first) {
2472 first = false;
2473 continue;
2474 }
2475 content.bundle_only = true;
2476 }
2477 first = true;
2478 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2479 if (first) {
2480 first = false;
2481 continue;
2482 }
2483 transport.description.ice_ufrag.clear();
2484 transport.description.ice_pwd.clear();
2485 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2486 transport.description.identity_fingerprint.reset(nullptr);
2487 }
2488}
2489
2490// Test that if applying a true "max bundle" offer, which uses ports of 0,
2491// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2492// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2493// successfully and media flows.
2494// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2495// TODO(deadbeef): Won't need this test once we start generating actual
2496// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002497TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002498 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2499 ASSERT_TRUE(CreatePeerConnectionWrappers());
2500 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002501 caller()->AddAudioVideoTracks();
2502 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002503 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2504 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2505 // but the first m= section.
2506 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2507 caller()->CreateAndSetAndSignalOffer();
2508 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002509 MediaExpectations media_expectations;
2510 media_expectations.ExpectBidirectionalAudioAndVideo();
2511 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002512}
2513
2514// Test that we can receive the audio output level from a remote audio track.
2515// TODO(deadbeef): Use a fake audio source and verify that the output level is
2516// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002517TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002518 ASSERT_TRUE(CreatePeerConnectionWrappers());
2519 ConnectFakeSignaling();
2520 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002521 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002522 caller()->CreateAndSetAndSignalOffer();
2523 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2524
2525 // Get the audio output level stats. Note that the level is not available
2526 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002527 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002528 kMaxWaitForFramesMs);
2529}
2530
2531// Test that an audio input level is reported.
2532// TODO(deadbeef): Use a fake audio source and verify that the input level is
2533// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002534TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002535 ASSERT_TRUE(CreatePeerConnectionWrappers());
2536 ConnectFakeSignaling();
2537 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002538 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002539 caller()->CreateAndSetAndSignalOffer();
2540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2541
2542 // Get the audio input level stats. The level should be available very
2543 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002544 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002545 kMaxWaitForStatsMs);
2546}
2547
2548// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002549TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002550 ASSERT_TRUE(CreatePeerConnectionWrappers());
2551 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002552 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002553 // Do offer/answer, wait for the callee to receive some frames.
2554 caller()->CreateAndSetAndSignalOffer();
2555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002556
2557 MediaExpectations media_expectations;
2558 media_expectations.CalleeExpectsSomeAudioAndVideo();
2559 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002560
2561 // Get a handle to the remote tracks created, so they can be used as GetStats
2562 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002563 for (auto receiver : callee()->pc()->GetReceivers()) {
2564 // We received frames, so we definitely should have nonzero "received bytes"
2565 // stats at this point.
2566 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2567 0);
2568 }
deadbeef1dcb1642017-03-29 21:08:16 -07002569}
2570
2571// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002572TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002573 ASSERT_TRUE(CreatePeerConnectionWrappers());
2574 ConnectFakeSignaling();
2575 auto audio_track = caller()->CreateLocalAudioTrack();
2576 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002577 caller()->AddTrack(audio_track);
2578 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002579 // Do offer/answer, wait for the callee to receive some frames.
2580 caller()->CreateAndSetAndSignalOffer();
2581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582 MediaExpectations media_expectations;
2583 media_expectations.CalleeExpectsSomeAudioAndVideo();
2584 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002585
2586 // The callee received frames, so we definitely should have nonzero "sent
2587 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002588 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2589 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2590}
2591
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002592// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002593TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002594 ASSERT_TRUE(CreatePeerConnectionWrappers());
2595 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002596 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002597
Steve Anton15324772018-01-16 10:26:49 -08002598 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002599
2600 // Do offer/answer, wait for the callee to receive some frames.
2601 caller()->CreateAndSetAndSignalOffer();
2602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2603
2604 // Get the remote audio track created on the receiver, so they can be used as
2605 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002606 auto receivers = callee()->pc()->GetReceivers();
2607 ASSERT_EQ(1u, receivers.size());
2608 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002609
2610 // Get the audio output level stats. Note that the level is not available
2611 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002612 EXPECT_TRUE_WAIT(
2613 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2614 0,
2615 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002616}
2617
Steve Antona41959e2018-11-28 11:15:33 -08002618// Test that the track ID is associated with all local and remote SSRC stats
2619// using the old GetStats() and more than 1 audio and more than 1 video track.
2620// This is a regression test for crbug.com/906988
2621TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2622 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2623 ASSERT_TRUE(CreatePeerConnectionWrappers());
2624 ConnectFakeSignaling();
2625 auto audio_sender_1 = caller()->AddAudioTrack();
2626 auto video_sender_1 = caller()->AddVideoTrack();
2627 auto audio_sender_2 = caller()->AddAudioTrack();
2628 auto video_sender_2 = caller()->AddVideoTrack();
2629 caller()->CreateAndSetAndSignalOffer();
2630 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2631
2632 MediaExpectations media_expectations;
2633 media_expectations.CalleeExpectsSomeAudioAndVideo();
2634 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2635
2636 std::vector<std::string> track_ids = {
2637 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2638 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2639
2640 auto caller_stats = caller()->OldGetStats();
2641 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2642 auto callee_stats = callee()->OldGetStats();
2643 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2644}
2645
Steve Antonffa6ce42018-11-30 09:26:08 -08002646// Test that the new GetStats() returns stats for all outgoing/incoming streams
2647// with the correct track IDs if there are more than one audio and more than one
2648// video senders/receivers.
2649TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2650 ASSERT_TRUE(CreatePeerConnectionWrappers());
2651 ConnectFakeSignaling();
2652 auto audio_sender_1 = caller()->AddAudioTrack();
2653 auto video_sender_1 = caller()->AddVideoTrack();
2654 auto audio_sender_2 = caller()->AddAudioTrack();
2655 auto video_sender_2 = caller()->AddVideoTrack();
2656 caller()->CreateAndSetAndSignalOffer();
2657 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2658
2659 MediaExpectations media_expectations;
2660 media_expectations.CalleeExpectsSomeAudioAndVideo();
2661 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2662
2663 std::vector<std::string> track_ids = {
2664 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2665 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2666
2667 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2668 caller()->NewGetStats();
2669 ASSERT_TRUE(caller_report);
2670 auto outbound_stream_stats =
2671 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2672 ASSERT_EQ(4u, outbound_stream_stats.size());
2673 std::vector<std::string> outbound_track_ids;
2674 for (const auto& stat : outbound_stream_stats) {
2675 ASSERT_TRUE(stat->bytes_sent.is_defined());
2676 EXPECT_LT(0u, *stat->bytes_sent);
2677 ASSERT_TRUE(stat->track_id.is_defined());
2678 const auto* track_stat =
2679 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2680 ASSERT_TRUE(track_stat);
2681 outbound_track_ids.push_back(*track_stat->track_identifier);
2682 }
2683 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2684
2685 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2686 callee()->NewGetStats();
2687 ASSERT_TRUE(callee_report);
2688 auto inbound_stream_stats =
2689 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2690 ASSERT_EQ(4u, inbound_stream_stats.size());
2691 std::vector<std::string> inbound_track_ids;
2692 for (const auto& stat : inbound_stream_stats) {
2693 ASSERT_TRUE(stat->bytes_received.is_defined());
2694 EXPECT_LT(0u, *stat->bytes_received);
2695 ASSERT_TRUE(stat->track_id.is_defined());
2696 const auto* track_stat =
2697 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2698 ASSERT_TRUE(track_stat);
2699 inbound_track_ids.push_back(*track_stat->track_identifier);
2700 }
2701 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2702}
2703
2704// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002705// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2706// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002707TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002708 GetStatsForUnsignaledStreamWithNewStatsApi) {
2709 ASSERT_TRUE(CreatePeerConnectionWrappers());
2710 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002711 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002712 // Remove SSRCs and MSIDs from the received offer SDP.
2713 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2714 caller()->CreateAndSetAndSignalOffer();
2715 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002716 MediaExpectations media_expectations;
2717 media_expectations.CalleeExpectsSomeAudio(1);
2718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002719
2720 // We received a frame, so we should have nonzero "bytes received" stats for
2721 // the unsignaled stream, if stats are working for it.
2722 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2723 callee()->NewGetStats();
2724 ASSERT_NE(nullptr, report);
2725 auto inbound_stream_stats =
2726 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2727 ASSERT_EQ(1U, inbound_stream_stats.size());
2728 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2729 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002730 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2731}
2732
Taylor Brandstettera4653442018-06-19 09:44:26 -07002733// Same as above but for the legacy stats implementation.
2734TEST_P(PeerConnectionIntegrationTest,
2735 GetStatsForUnsignaledStreamWithOldStatsApi) {
2736 ASSERT_TRUE(CreatePeerConnectionWrappers());
2737 ConnectFakeSignaling();
2738 caller()->AddAudioTrack();
2739 // Remove SSRCs and MSIDs from the received offer SDP.
2740 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2741 caller()->CreateAndSetAndSignalOffer();
2742 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2743
2744 // Note that, since the old stats implementation associates SSRCs with tracks
2745 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2746 // associated track ID. So we can't use the track "selector" argument.
2747 //
2748 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2749 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002750 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002751 kDefaultTimeout);
2752}
2753
zhihuangf8164932017-05-19 13:09:47 -07002754// Test that we can successfully get the media related stats (audio level
2755// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002756TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002757 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2758 ASSERT_TRUE(CreatePeerConnectionWrappers());
2759 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002760 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002761 // Remove SSRCs and MSIDs from the received offer SDP.
2762 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2763 caller()->CreateAndSetAndSignalOffer();
2764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002765 MediaExpectations media_expectations;
2766 media_expectations.CalleeExpectsSomeAudio(1);
2767 media_expectations.CalleeExpectsSomeVideo(1);
2768 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002769
2770 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2771 callee()->NewGetStats();
2772 ASSERT_NE(nullptr, report);
2773
2774 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2775 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2776 ASSERT_GE(audio_index, 0);
2777 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002778}
2779
deadbeef4e2deab2017-09-20 13:56:21 -07002780// Helper for test below.
2781void ModifySsrcs(cricket::SessionDescription* desc) {
2782 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002783 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002784 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002785 for (uint32_t& ssrc : stream.ssrcs) {
2786 ssrc = rtc::CreateRandomId();
2787 }
2788 }
2789 }
2790}
2791
2792// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2793// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2794// This should result in two "RTCInboundRTPStreamStats", but only one
2795// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2796// being reset to 0 once the SSRC change occurs.
2797//
2798// Regression test for this bug:
2799// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2800//
2801// The bug causes the track stats to only represent one of the two streams:
2802// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2803// that the track stat counters would reset to 0 when the new stream is
2804// received, and a 50% chance that they'll stop updating (while
2805// "concealed_samples" continues increasing, due to silence being generated for
2806// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002807TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002808 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002809 ASSERT_TRUE(CreatePeerConnectionWrappers());
2810 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002811 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002812 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2813 // that doesn't signal SSRCs (from the callee's perspective).
2814 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2815 caller()->CreateAndSetAndSignalOffer();
2816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2817 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002818 {
2819 MediaExpectations media_expectations;
2820 media_expectations.CalleeExpectsSomeAudio(50);
2821 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2822 }
deadbeef4e2deab2017-09-20 13:56:21 -07002823 // Some audio frames were received, so we should have nonzero "samples
2824 // received" for the track.
2825 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2826 callee()->NewGetStats();
2827 ASSERT_NE(nullptr, report);
2828 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2829 ASSERT_EQ(1U, track_stats.size());
2830 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2831 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2832 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2833
2834 // Create a new offer and munge it to cause the caller to use a new SSRC.
2835 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2836 caller()->CreateAndSetAndSignalOffer();
2837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2838 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2839 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002840 {
2841 MediaExpectations media_expectations;
2842 media_expectations.CalleeExpectsSomeAudio(25);
2843 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2844 }
deadbeef4e2deab2017-09-20 13:56:21 -07002845
2846 report = callee()->NewGetStats();
2847 ASSERT_NE(nullptr, report);
2848 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2849 ASSERT_EQ(1U, track_stats.size());
2850 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2851 // The "total samples received" stat should only be greater than it was
2852 // before.
2853 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2854 // Right now, the new SSRC will cause the counters to reset to 0.
2855 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2856
2857 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002858 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002859 // good sign that we're seeing stats from the old stream that's no longer
2860 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002861 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002862 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2863 EXPECT_LT(*track_stats[0]->concealed_samples,
2864 *track_stats[0]->total_samples_received *
2865 kAcceptableConcealedSamplesPercentage);
2866
2867 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2868 // sanity check that the SSRC really changed.
2869 // TODO(deadbeef): This isn't working right now, because we're not returning
2870 // *any* stats for the inactive stream. Uncomment when the bug is completely
2871 // fixed.
2872 // auto inbound_stream_stats =
2873 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2874 // ASSERT_EQ(2U, inbound_stream_stats.size());
2875}
2876
deadbeef1dcb1642017-03-29 21:08:16 -07002877// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002878TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002879 PeerConnectionFactory::Options dtls_10_options;
2880 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2881 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2882 dtls_10_options));
2883 ConnectFakeSignaling();
2884 // Do normal offer/answer and wait for some frames to be received in each
2885 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002886 caller()->AddAudioVideoTracks();
2887 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002888 caller()->CreateAndSetAndSignalOffer();
2889 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002890 MediaExpectations media_expectations;
2891 media_expectations.ExpectBidirectionalAudioAndVideo();
2892 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002893}
2894
2895// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002896TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002897 PeerConnectionFactory::Options dtls_10_options;
2898 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2899 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2900 dtls_10_options));
2901 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002902 caller()->AddAudioVideoTracks();
2903 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002904 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002905 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002906 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002907 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002908 kDefaultTimeout);
2909 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002910 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002911 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002912 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002913 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2914 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002915}
2916
2917// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002918TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002919 PeerConnectionFactory::Options dtls_12_options;
2920 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2921 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2922 dtls_12_options));
2923 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002924 caller()->AddAudioVideoTracks();
2925 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002926 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002927 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002928 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002929 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002930 kDefaultTimeout);
2931 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002932 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002933 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002934 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002935 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2936 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002937}
2938
2939// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2940// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002941TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002942 PeerConnectionFactory::Options caller_options;
2943 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2944 PeerConnectionFactory::Options callee_options;
2945 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2946 ASSERT_TRUE(
2947 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2948 ConnectFakeSignaling();
2949 // Do normal offer/answer and wait for some frames to be received in each
2950 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002951 caller()->AddAudioVideoTracks();
2952 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002953 caller()->CreateAndSetAndSignalOffer();
2954 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002955 MediaExpectations media_expectations;
2956 media_expectations.ExpectBidirectionalAudioAndVideo();
2957 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002958}
2959
2960// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2961// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002963 PeerConnectionFactory::Options caller_options;
2964 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2965 PeerConnectionFactory::Options callee_options;
2966 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2967 ASSERT_TRUE(
2968 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2969 ConnectFakeSignaling();
2970 // Do normal offer/answer and wait for some frames to be received in each
2971 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002972 caller()->AddAudioVideoTracks();
2973 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002974 caller()->CreateAndSetAndSignalOffer();
2975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002976 MediaExpectations media_expectations;
2977 media_expectations.ExpectBidirectionalAudioAndVideo();
2978 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002979}
2980
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002981// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2982// works as expected; the cipher should only be used if enabled by both sides.
2983TEST_P(PeerConnectionIntegrationTest,
2984 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2985 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002986 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002987 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002988 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2989 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002990 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2991 TestNegotiatedCipherSuite(caller_options, callee_options,
2992 expected_cipher_suite);
2993}
2994
2995TEST_P(PeerConnectionIntegrationTest,
2996 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2997 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002998 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2999 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003000 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003001 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003002 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3003 TestNegotiatedCipherSuite(caller_options, callee_options,
3004 expected_cipher_suite);
3005}
3006
3007TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3008 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003009 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003010 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003011 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003012 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3013 TestNegotiatedCipherSuite(caller_options, callee_options,
3014 expected_cipher_suite);
3015}
3016
deadbeef1dcb1642017-03-29 21:08:16 -07003017// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003018TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003019 bool local_gcm_enabled = false;
3020 bool remote_gcm_enabled = false;
3021 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3022 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3023 expected_cipher_suite);
3024}
3025
3026// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003027TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003028 bool local_gcm_enabled = true;
3029 bool remote_gcm_enabled = true;
3030 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3031 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3032 expected_cipher_suite);
3033}
3034
3035// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003036TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003037 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3038 bool local_gcm_enabled = true;
3039 bool remote_gcm_enabled = false;
3040 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3041 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3042 expected_cipher_suite);
3043}
3044
3045// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003046TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003047 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3048 bool local_gcm_enabled = false;
3049 bool remote_gcm_enabled = true;
3050 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3051 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3052 expected_cipher_suite);
3053}
3054
deadbeef7914b8c2017-04-21 03:23:33 -07003055// Verify that media can be transmitted end-to-end when GCM crypto suites are
3056// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3057// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3058// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003059TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003060 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003061 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003062 ASSERT_TRUE(
3063 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3064 ConnectFakeSignaling();
3065 // Do normal offer/answer and wait for some frames to be received in each
3066 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003067 caller()->AddAudioVideoTracks();
3068 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003069 caller()->CreateAndSetAndSignalOffer();
3070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003071 MediaExpectations media_expectations;
3072 media_expectations.ExpectBidirectionalAudioAndVideo();
3073 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003074}
3075
deadbeef1dcb1642017-03-29 21:08:16 -07003076// This test sets up a call between two parties with audio, video and an RTP
3077// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003078TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003079 PeerConnectionInterface::RTCConfiguration rtc_config;
3080 rtc_config.enable_rtp_data_channel = true;
3081 rtc_config.enable_dtls_srtp = false;
3082 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003083 ConnectFakeSignaling();
3084 // Expect that data channel created on caller side will show up for callee as
3085 // well.
3086 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003087 caller()->AddAudioVideoTracks();
3088 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003089 caller()->CreateAndSetAndSignalOffer();
3090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3091 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003092 MediaExpectations media_expectations;
3093 media_expectations.ExpectBidirectionalAudioAndVideo();
3094 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003095 ASSERT_NE(nullptr, caller()->data_channel());
3096 ASSERT_NE(nullptr, callee()->data_channel());
3097 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3098 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3099
3100 // Ensure data can be sent in both directions.
3101 std::string data = "hello world";
3102 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3103 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3104 kDefaultTimeout);
3105 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3106 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3107 kDefaultTimeout);
3108}
3109
3110// Ensure that an RTP data channel is signaled as closed for the caller when
3111// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003112TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003113 RtpDataChannelSignaledClosedInCalleeOffer) {
3114 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003115 PeerConnectionInterface::RTCConfiguration rtc_config;
3116 rtc_config.enable_rtp_data_channel = true;
3117 rtc_config.enable_dtls_srtp = false;
3118 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003119 ConnectFakeSignaling();
3120 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003121 caller()->AddAudioVideoTracks();
3122 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003123 caller()->CreateAndSetAndSignalOffer();
3124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3125 ASSERT_NE(nullptr, caller()->data_channel());
3126 ASSERT_NE(nullptr, callee()->data_channel());
3127 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3128 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3129
3130 // Close the data channel on the callee, and do an updated offer/answer.
3131 callee()->data_channel()->Close();
3132 callee()->CreateAndSetAndSignalOffer();
3133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3134 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3135 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3136}
3137
3138// Tests that data is buffered in an RTP data channel until an observer is
3139// registered for it.
3140//
3141// NOTE: RTP data channels can receive data before the underlying
3142// transport has detected that a channel is writable and thus data can be
3143// received before the data channel state changes to open. That is hard to test
3144// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003145TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003146 DataBufferedUntilRtpDataChannelObserverRegistered) {
3147 // Use fake clock and simulated network delay so that we predictably can wait
3148 // until an SCTP message has been delivered without "sleep()"ing.
3149 rtc::ScopedFakeClock fake_clock;
3150 // Some things use a time of "0" as a special value, so we need to start out
3151 // the fake clock at a nonzero time.
3152 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003153 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003154 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3155 virtual_socket_server()->UpdateDelayDistribution();
3156
Niels Möllerf06f9232018-08-07 12:32:18 +02003157 PeerConnectionInterface::RTCConfiguration rtc_config;
3158 rtc_config.enable_rtp_data_channel = true;
3159 rtc_config.enable_dtls_srtp = false;
3160 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003161 ConnectFakeSignaling();
3162 caller()->CreateDataChannel();
3163 caller()->CreateAndSetAndSignalOffer();
3164 ASSERT_TRUE(caller()->data_channel() != nullptr);
3165 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3166 kDefaultTimeout, fake_clock);
3167 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3168 kDefaultTimeout, fake_clock);
3169 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3170 callee()->data_channel()->state(), kDefaultTimeout,
3171 fake_clock);
3172
3173 // Unregister the observer which is normally automatically registered.
3174 callee()->data_channel()->UnregisterObserver();
3175 // Send data and advance fake clock until it should have been received.
3176 std::string data = "hello world";
3177 caller()->data_channel()->Send(DataBuffer(data));
3178 SIMULATED_WAIT(false, 50, fake_clock);
3179
3180 // Attach data channel and expect data to be received immediately. Note that
3181 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3182 // further, but data can be received even if the callback is asynchronous.
3183 MockDataChannelObserver new_observer(callee()->data_channel());
3184 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3185 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003186 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3187 // If this is not done a DCHECK can be hit in ports.cc, because a large
3188 // negative number is calculated for the rtt due to the global clock changing.
3189 caller()->pc()->Close();
3190 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003191}
3192
3193// This test sets up a call between two parties with audio, video and but only
3194// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003195TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003196 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3197 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003198 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003199 rtc_config_1.enable_dtls_srtp = false;
3200 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3201 rtc_config_2.enable_dtls_srtp = false;
3202 rtc_config_2.enable_dtls_srtp = false;
3203 ASSERT_TRUE(
3204 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003205 ConnectFakeSignaling();
3206 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003207 caller()->AddAudioVideoTracks();
3208 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003209 caller()->CreateAndSetAndSignalOffer();
3210 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3211 // The caller should still have a data channel, but it should be closed, and
3212 // one should ever have been created for the callee.
3213 EXPECT_TRUE(caller()->data_channel() != nullptr);
3214 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3215 EXPECT_EQ(nullptr, callee()->data_channel());
3216}
3217
3218// This test sets up a call between two parties with audio, and video. When
3219// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003220TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003221 PeerConnectionInterface::RTCConfiguration rtc_config;
3222 rtc_config.enable_rtp_data_channel = true;
3223 rtc_config.enable_dtls_srtp = false;
3224 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003225 ConnectFakeSignaling();
3226 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003227 caller()->AddAudioVideoTracks();
3228 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003229 caller()->CreateAndSetAndSignalOffer();
3230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3231 // Create data channel and do new offer and answer.
3232 caller()->CreateDataChannel();
3233 caller()->CreateAndSetAndSignalOffer();
3234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3235 ASSERT_NE(nullptr, caller()->data_channel());
3236 ASSERT_NE(nullptr, callee()->data_channel());
3237 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3238 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3239 // Ensure data can be sent in both directions.
3240 std::string data = "hello world";
3241 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3242 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3243 kDefaultTimeout);
3244 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3245 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3246 kDefaultTimeout);
3247}
3248
3249#ifdef HAVE_SCTP
3250
3251// This test sets up a call between two parties with audio, video and an SCTP
3252// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003253TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003254 ASSERT_TRUE(CreatePeerConnectionWrappers());
3255 ConnectFakeSignaling();
3256 // Expect that data channel created on caller side will show up for callee as
3257 // well.
3258 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003259 caller()->AddAudioVideoTracks();
3260 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003261 caller()->CreateAndSetAndSignalOffer();
3262 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3263 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003264 MediaExpectations media_expectations;
3265 media_expectations.ExpectBidirectionalAudioAndVideo();
3266 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003267 // Caller data channel should already exist (it created one). Callee data
3268 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3269 ASSERT_NE(nullptr, caller()->data_channel());
3270 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3271 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3272 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3273
3274 // Ensure data can be sent in both directions.
3275 std::string data = "hello world";
3276 caller()->data_channel()->Send(DataBuffer(data));
3277 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3278 kDefaultTimeout);
3279 callee()->data_channel()->Send(DataBuffer(data));
3280 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3281 kDefaultTimeout);
3282}
3283
3284// Ensure that when the callee closes an SCTP data channel, the closing
3285// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003286TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003287 // Same procedure as above test.
3288 ASSERT_TRUE(CreatePeerConnectionWrappers());
3289 ConnectFakeSignaling();
3290 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003291 caller()->AddAudioVideoTracks();
3292 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003293 caller()->CreateAndSetAndSignalOffer();
3294 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3295 ASSERT_NE(nullptr, caller()->data_channel());
3296 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3297 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3298 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3299
3300 // Close the data channel on the callee side, and wait for it to reach the
3301 // "closed" state on both sides.
3302 callee()->data_channel()->Close();
3303 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3304 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3305}
3306
Seth Hampson2f0d7022018-02-20 11:54:42 -08003307TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003308 ASSERT_TRUE(CreatePeerConnectionWrappers());
3309 ConnectFakeSignaling();
3310 webrtc::DataChannelInit init;
3311 init.id = 53;
3312 init.maxRetransmits = 52;
3313 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003314 caller()->AddAudioVideoTracks();
3315 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003316 caller()->CreateAndSetAndSignalOffer();
3317 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003318 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3319 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003320 EXPECT_EQ(init.id, callee()->data_channel()->id());
3321 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3322 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3323 EXPECT_FALSE(callee()->data_channel()->negotiated());
3324}
3325
deadbeef1dcb1642017-03-29 21:08:16 -07003326// Test usrsctp's ability to process unordered data stream, where data actually
3327// arrives out of order using simulated delays. Previously there have been some
3328// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003329TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003330 // Introduce random network delays.
3331 // Otherwise it's not a true "unordered" test.
3332 virtual_socket_server()->set_delay_mean(20);
3333 virtual_socket_server()->set_delay_stddev(5);
3334 virtual_socket_server()->UpdateDelayDistribution();
3335 // Normal procedure, but with unordered data channel config.
3336 ASSERT_TRUE(CreatePeerConnectionWrappers());
3337 ConnectFakeSignaling();
3338 webrtc::DataChannelInit init;
3339 init.ordered = false;
3340 caller()->CreateDataChannel(&init);
3341 caller()->CreateAndSetAndSignalOffer();
3342 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3343 ASSERT_NE(nullptr, caller()->data_channel());
3344 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3345 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3346 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3347
3348 static constexpr int kNumMessages = 100;
3349 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3350 static constexpr size_t kMaxMessageSize = 4096;
3351 // Create and send random messages.
3352 std::vector<std::string> sent_messages;
3353 for (int i = 0; i < kNumMessages; ++i) {
3354 size_t length =
3355 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3356 std::string message;
3357 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3358 caller()->data_channel()->Send(DataBuffer(message));
3359 callee()->data_channel()->Send(DataBuffer(message));
3360 sent_messages.push_back(message);
3361 }
3362
3363 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003364 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003365 caller()->data_observer()->received_message_count(),
3366 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003367 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003368 callee()->data_observer()->received_message_count(),
3369 kDefaultTimeout);
3370
3371 // Sort and compare to make sure none of the messages were corrupted.
3372 std::vector<std::string> caller_received_messages =
3373 caller()->data_observer()->messages();
3374 std::vector<std::string> callee_received_messages =
3375 callee()->data_observer()->messages();
3376 std::sort(sent_messages.begin(), sent_messages.end());
3377 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3378 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3379 EXPECT_EQ(sent_messages, caller_received_messages);
3380 EXPECT_EQ(sent_messages, callee_received_messages);
3381}
3382
3383// This test sets up a call between two parties with audio, and video. When
3384// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003385TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003386 ASSERT_TRUE(CreatePeerConnectionWrappers());
3387 ConnectFakeSignaling();
3388 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003389 caller()->AddAudioVideoTracks();
3390 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003391 caller()->CreateAndSetAndSignalOffer();
3392 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3393 // Create data channel and do new offer and answer.
3394 caller()->CreateDataChannel();
3395 caller()->CreateAndSetAndSignalOffer();
3396 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3397 // Caller data channel should already exist (it created one). Callee data
3398 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3399 ASSERT_NE(nullptr, caller()->data_channel());
3400 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3401 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3402 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3403 // Ensure data can be sent in both directions.
3404 std::string data = "hello world";
3405 caller()->data_channel()->Send(DataBuffer(data));
3406 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3407 kDefaultTimeout);
3408 callee()->data_channel()->Send(DataBuffer(data));
3409 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3410 kDefaultTimeout);
3411}
3412
deadbeef7914b8c2017-04-21 03:23:33 -07003413// Set up a connection initially just using SCTP data channels, later upgrading
3414// to audio/video, ensuring frames are received end-to-end. Effectively the
3415// inverse of the test above.
3416// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003417TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003418 ASSERT_TRUE(CreatePeerConnectionWrappers());
3419 ConnectFakeSignaling();
3420 // Do initial offer/answer with just data channel.
3421 caller()->CreateDataChannel();
3422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3424 // Wait until data can be sent over the data channel.
3425 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3426 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3427 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3428
3429 // Do subsequent offer/answer with two-way audio and video. Audio and video
3430 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003431 caller()->AddAudioVideoTracks();
3432 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003433 caller()->CreateAndSetAndSignalOffer();
3434 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003435 MediaExpectations media_expectations;
3436 media_expectations.ExpectBidirectionalAudioAndVideo();
3437 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003438}
3439
deadbeef8b7e9ad2017-05-25 09:38:55 -07003440static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003441 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003442 GetFirstDataContentDescription(desc);
3443 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003444 dcd_offer->set_use_sctpmap(false);
3445 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3446}
3447
3448// Test that the data channel works when a spec-compliant SCTP m= section is
3449// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3450// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003451TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003452 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3453 ASSERT_TRUE(CreatePeerConnectionWrappers());
3454 ConnectFakeSignaling();
3455 caller()->CreateDataChannel();
3456 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3457 caller()->CreateAndSetAndSignalOffer();
3458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3459 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3460 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3461 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3462
3463 // Ensure data can be sent in both directions.
3464 std::string data = "hello world";
3465 caller()->data_channel()->Send(DataBuffer(data));
3466 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3467 kDefaultTimeout);
3468 callee()->data_channel()->Send(DataBuffer(data));
3469 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3470 kDefaultTimeout);
3471}
3472
deadbeef1dcb1642017-03-29 21:08:16 -07003473#endif // HAVE_SCTP
3474
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003475// This test sets up a call between two parties with a media transport data
3476// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003477TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3478 PeerConnectionInterface::RTCConfiguration rtc_config;
3479 rtc_config.use_media_transport_for_data_channels = true;
3480 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3481 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3482 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3483 loopback_media_transports()->second_factory()));
3484 ConnectFakeSignaling();
3485
3486 // Expect that data channel created on caller side will show up for callee as
3487 // well.
3488 caller()->CreateDataChannel();
3489 caller()->CreateAndSetAndSignalOffer();
3490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3491
3492 // Ensure that the media transport is ready.
3493 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3494 loopback_media_transports()->FlushAsyncInvokes();
3495
3496 // Caller data channel should already exist (it created one). Callee data
3497 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3498 ASSERT_NE(nullptr, caller()->data_channel());
3499 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3500 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3501 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3502
3503 // Ensure data can be sent in both directions.
3504 std::string data = "hello world";
3505 caller()->data_channel()->Send(DataBuffer(data));
3506 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3507 kDefaultTimeout);
3508 callee()->data_channel()->Send(DataBuffer(data));
3509 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3510 kDefaultTimeout);
3511}
3512
3513// Ensure that when the callee closes a media transport data channel, the
3514// closing procedure results in the data channel being closed for the caller
3515// as well.
3516TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3517 PeerConnectionInterface::RTCConfiguration rtc_config;
3518 rtc_config.use_media_transport_for_data_channels = true;
3519 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3520 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3521 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3522 loopback_media_transports()->second_factory()));
3523 ConnectFakeSignaling();
3524
3525 // Create a data channel on the caller and signal it to the callee.
3526 caller()->CreateDataChannel();
3527 caller()->CreateAndSetAndSignalOffer();
3528 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3529
3530 // Ensure that the media transport is ready.
3531 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3532 loopback_media_transports()->FlushAsyncInvokes();
3533
3534 // Data channels exist and open on both ends of the connection.
3535 ASSERT_NE(nullptr, caller()->data_channel());
3536 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3537 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3538 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3539
3540 // Close the data channel on the callee side, and wait for it to reach the
3541 // "closed" state on both sides.
3542 callee()->data_channel()->Close();
3543 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3544 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3545}
3546
3547TEST_P(PeerConnectionIntegrationTest,
3548 MediaTransportDataChannelConfigSentToOtherSide) {
3549 PeerConnectionInterface::RTCConfiguration rtc_config;
3550 rtc_config.use_media_transport_for_data_channels = true;
3551 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3552 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3553 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3554 loopback_media_transports()->second_factory()));
3555 ConnectFakeSignaling();
3556
3557 // Create a data channel with a non-default configuration and signal it to the
3558 // callee.
3559 webrtc::DataChannelInit init;
3560 init.id = 53;
3561 init.maxRetransmits = 52;
3562 caller()->CreateDataChannel("data-channel", &init);
3563 caller()->CreateAndSetAndSignalOffer();
3564 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3565
3566 // Ensure that the media transport is ready.
3567 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3568 loopback_media_transports()->FlushAsyncInvokes();
3569
3570 // Ensure that the data channel exists on the callee with the correct
3571 // configuration.
3572 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3573 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3574 EXPECT_EQ(init.id, callee()->data_channel()->id());
3575 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3576 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3577 EXPECT_FALSE(callee()->data_channel()->negotiated());
3578}
3579
Niels Möllerc68d2822018-11-20 14:52:05 +01003580TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3581 PeerConnectionInterface::RTCConfiguration rtc_config;
3582 rtc_config.use_media_transport = true;
3583 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3584 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3585 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3586 loopback_media_transports()->second_factory()));
3587 ConnectFakeSignaling();
3588
3589 caller()->AddAudioTrack();
3590 callee()->AddAudioTrack();
3591 // Start offer/answer exchange and wait for it to complete.
3592 caller()->CreateAndSetAndSignalOffer();
3593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3594
3595 // Ensure that the media transport is ready.
3596 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3597 loopback_media_transports()->FlushAsyncInvokes();
3598
3599 MediaExpectations media_expectations;
3600 media_expectations.ExpectBidirectionalAudio();
3601 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3602
3603 webrtc::MediaTransportPair::Stats first_stats =
3604 loopback_media_transports()->FirstStats();
3605 webrtc::MediaTransportPair::Stats second_stats =
3606 loopback_media_transports()->SecondStats();
3607
3608 EXPECT_GT(first_stats.received_audio_frames, 0);
3609 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3610
3611 EXPECT_GT(second_stats.received_audio_frames, 0);
3612 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3613}
3614
deadbeef1dcb1642017-03-29 21:08:16 -07003615// Test that the ICE connection and gathering states eventually reach
3616// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003617TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003618 ASSERT_TRUE(CreatePeerConnectionWrappers());
3619 ConnectFakeSignaling();
3620 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003621 caller()->AddAudioVideoTracks();
3622 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003623 caller()->CreateAndSetAndSignalOffer();
3624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3625 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3626 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3627 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3628 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3629 // After the best candidate pair is selected and all candidates are signaled,
3630 // the ICE connection state should reach "complete".
3631 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3632 // answerer/"callee" by default) only reaches "connected". When this is
3633 // fixed, this test should be updated.
3634 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3635 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003636 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3637 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003638}
3639
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003640// Replaces the first candidate with a static address and configures a
3641// MockAsyncResolver to return the replaced address the first time the static
3642// address is resolved. Candidates past the first will not be signaled.
3643class ReplaceFirstCandidateAddressDropOthers final
3644 : public IceCandidateReplacerInterface {
3645 public:
3646 ReplaceFirstCandidateAddressDropOthers(
3647 const SocketAddress& new_address,
3648 rtc::MockAsyncResolver* mock_async_resolver)
3649 : mock_async_resolver_(mock_async_resolver), new_address_(new_address) {
3650 RTC_DCHECK(mock_async_resolver);
3651 }
3652
3653 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3654 const webrtc::IceCandidateInterface* candidate) override {
3655 if (replaced_candidate_) {
3656 return nullptr;
3657 }
3658
3659 replaced_candidate_ = true;
3660 cricket::Candidate new_candidate(candidate->candidate());
3661 new_candidate.set_address(new_address_);
3662 EXPECT_CALL(*mock_async_resolver_, GetResolvedAddress(_, _))
3663 .WillOnce(DoAll(SetArgPointee<1>(candidate->candidate().address()),
3664 Return(true)));
3665 EXPECT_CALL(*mock_async_resolver_, Destroy(_));
3666 return webrtc::CreateIceCandidate(
3667 candidate->sdp_mid(), candidate->sdp_mline_index(), new_candidate);
3668 }
3669
3670 private:
3671 rtc::MockAsyncResolver* mock_async_resolver_;
3672 SocketAddress new_address_;
3673 bool replaced_candidate_ = false;
3674};
3675
3676// Drops all candidates before they are signaled.
3677class DropAllCandidates final : public IceCandidateReplacerInterface {
3678 public:
3679 std::unique_ptr<webrtc::IceCandidateInterface> ReplaceCandidate(
3680 const webrtc::IceCandidateInterface*) override {
3681 return nullptr;
3682 }
3683};
3684
3685// Replace the first caller ICE candidate IP with a fake hostname and drop the
3686// other candidates. Drop all candidates on the callee side (to avoid a prflx
3687// connection). Use a mock resolver to resolve the hostname back to the original
3688// IP on the callee side and check that the ice connection connects.
3689TEST_P(PeerConnectionIntegrationTest,
3690 IceStatesReachCompletionWithRemoteHostname) {
3691 webrtc::MockAsyncResolverFactory* callee_mock_async_resolver_factory;
3692 {
3693 auto resolver_factory =
3694 absl::make_unique<webrtc::MockAsyncResolverFactory>();
3695 callee_mock_async_resolver_factory = resolver_factory.get();
3696 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3697 callee_deps.async_resolver_factory = std::move(resolver_factory);
3698
3699 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3700 RTCConfiguration(), webrtc::PeerConnectionDependencies(nullptr),
3701 RTCConfiguration(), std::move(callee_deps)));
3702 }
3703
3704 rtc::MockAsyncResolver mock_async_resolver;
3705
3706 // This also verifies that the injected AsyncResolverFactory is used by
3707 // P2PTransportChannel.
3708 EXPECT_CALL(*callee_mock_async_resolver_factory, Create())
3709 .WillOnce(Return(&mock_async_resolver));
3710 caller()->SetLocalIceCandidateReplacer(
3711 absl::make_unique<ReplaceFirstCandidateAddressDropOthers>(
3712 SocketAddress("a.b", 10000), &mock_async_resolver));
3713 callee()->SetLocalIceCandidateReplacer(
3714 absl::make_unique<DropAllCandidates>());
3715
3716 ConnectFakeSignaling();
3717 caller()->AddAudioVideoTracks();
3718 callee()->AddAudioVideoTracks();
3719 caller()->CreateAndSetAndSignalOffer();
3720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3721 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3722 caller()->ice_connection_state(), kDefaultTimeout);
3723 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3724 callee()->ice_connection_state(), kDefaultTimeout);
3725}
3726
Steve Antonede9ca52017-10-16 13:04:27 -07003727// Test that firewalling the ICE connection causes the clients to identify the
3728// disconnected state and then removing the firewall causes them to reconnect.
3729class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003730 : public PeerConnectionIntegrationBaseTest,
3731 public ::testing::WithParamInterface<
3732 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003733 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003734 PeerConnectionIntegrationIceStatesTest()
3735 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3736 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003737 }
3738
3739 void StartStunServer(const SocketAddress& server_address) {
3740 stun_server_.reset(
3741 cricket::TestStunServer::Create(network_thread(), server_address));
3742 }
3743
3744 bool TestIPv6() {
3745 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3746 }
3747
3748 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003749 network_thread()->Invoke<void>(
3750 RTC_FROM_HERE,
3751 rtc::Bind(&cricket::PortAllocator::set_flags,
3752 caller()->port_allocator(), port_allocator_flags_));
3753 network_thread()->Invoke<void>(
3754 RTC_FROM_HERE,
3755 rtc::Bind(&cricket::PortAllocator::set_flags,
3756 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003757 }
3758
3759 std::vector<SocketAddress> CallerAddresses() {
3760 std::vector<SocketAddress> addresses;
3761 addresses.push_back(SocketAddress("1.1.1.1", 0));
3762 if (TestIPv6()) {
3763 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3764 }
3765 return addresses;
3766 }
3767
3768 std::vector<SocketAddress> CalleeAddresses() {
3769 std::vector<SocketAddress> addresses;
3770 addresses.push_back(SocketAddress("2.2.2.2", 0));
3771 if (TestIPv6()) {
3772 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3773 }
3774 return addresses;
3775 }
3776
3777 void SetUpNetworkInterfaces() {
3778 // Remove the default interfaces added by the test infrastructure.
3779 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3780 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3781
3782 // Add network addresses for test.
3783 for (const auto& caller_address : CallerAddresses()) {
3784 caller()->network()->AddInterface(caller_address);
3785 }
3786 for (const auto& callee_address : CalleeAddresses()) {
3787 callee()->network()->AddInterface(callee_address);
3788 }
3789 }
3790
3791 private:
3792 uint32_t port_allocator_flags_;
3793 std::unique_ptr<cricket::TestStunServer> stun_server_;
3794};
3795
3796// Tests that the PeerConnection goes through all the ICE gathering/connection
3797// states over the duration of the call. This includes Disconnected and Failed
3798// states, induced by putting a firewall between the peers and waiting for them
3799// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003800TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3801 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3802 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3803 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003804
3805 const SocketAddress kStunServerAddress =
3806 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3807 StartStunServer(kStunServerAddress);
3808
3809 PeerConnectionInterface::RTCConfiguration config;
3810 PeerConnectionInterface::IceServer ice_stun_server;
3811 ice_stun_server.urls.push_back(
3812 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3813 kStunServerAddress.PortAsString());
3814 config.servers.push_back(ice_stun_server);
3815
3816 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3817 ConnectFakeSignaling();
3818 SetPortAllocatorFlags();
3819 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003820 caller()->AddAudioVideoTracks();
3821 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003822
3823 // Initial state before anything happens.
3824 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3825 caller()->ice_gathering_state());
3826 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3827 caller()->ice_connection_state());
3828
3829 // Start the call by creating the offer, setting it as the local description,
3830 // then sending it to the peer who will respond with an answer. This happens
3831 // asynchronously so that we can watch the states as it runs in the
3832 // background.
3833 caller()->CreateAndSetAndSignalOffer();
3834
Steve Anton83119dd2017-11-10 16:19:52 -08003835 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3836 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003837
3838 // Verify that the observer was notified of the intermediate transitions.
3839 EXPECT_THAT(caller()->ice_connection_state_history(),
3840 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3841 PeerConnectionInterface::kIceConnectionConnected,
3842 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003843 // After the ice transport transitions from checking to connected we revert
3844 // back to new as the standard requires, as at that point the DTLS transport
3845 // is in the "new" state while no transports are "connecting", "checking",
3846 // "failed" or disconnected. This is pretty unintuitive, and we might want to
3847 // amend the spec to handle this case more gracefully.
3848 EXPECT_THAT(
3849 caller()->peer_connection_state_history(),
3850 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003851 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003852 EXPECT_THAT(caller()->ice_gathering_state_history(),
3853 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3854 PeerConnectionInterface::kIceGatheringComplete));
3855
3856 // Block connections to/from the caller and wait for ICE to become
3857 // disconnected.
3858 for (const auto& caller_address : CallerAddresses()) {
3859 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3860 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003861 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003862 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3863 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003864
3865 // Let ICE re-establish by removing the firewall rules.
3866 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003867 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003868 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3869 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003870
3871 // According to RFC7675, if there is no response within 30 seconds then the
3872 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003873 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003874 constexpr int kConsentTimeout = 30000;
3875 for (const auto& caller_address : CallerAddresses()) {
3876 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3877 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003878 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003879 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3880 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003881}
3882
3883// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3884// and that the statistics in the metric observers are updated correctly.
3885TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3886 ASSERT_TRUE(CreatePeerConnectionWrappers());
3887 ConnectFakeSignaling();
3888 SetPortAllocatorFlags();
3889 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003890 caller()->AddAudioVideoTracks();
3891 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003892 caller()->CreateAndSetAndSignalOffer();
3893
3894 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3895
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003896 // TODO(bugs.webrtc.org/9456): Fix it.
3897 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3898 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3899 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3900 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003901 if (TestIPv6()) {
3902 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3903 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003904 EXPECT_EQ(0, num_best_ipv4);
3905 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003906 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003907 EXPECT_EQ(1, num_best_ipv4);
3908 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003909 }
3910
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003911 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3912 "WebRTC.PeerConnection.CandidatePairType_UDP",
3913 webrtc::kIceCandidatePairHostHost));
3914 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3915 "WebRTC.PeerConnection.CandidatePairType_UDP",
3916 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003917}
3918
3919constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3920 cricket::PORTALLOCATOR_DISABLE_STUN |
3921 cricket::PORTALLOCATOR_DISABLE_RELAY;
3922constexpr uint32_t kFlagsIPv6NoStun =
3923 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3924 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3925constexpr uint32_t kFlagsIPv4Stun =
3926 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3927
Seth Hampson2f0d7022018-02-20 11:54:42 -08003928INSTANTIATE_TEST_CASE_P(
3929 PeerConnectionIntegrationTest,
3930 PeerConnectionIntegrationIceStatesTest,
3931 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3932 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3933 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3934 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003935
deadbeef1dcb1642017-03-29 21:08:16 -07003936// This test sets up a call between two parties with audio and video.
3937// During the call, the caller restarts ICE and the test verifies that
3938// new ICE candidates are generated and audio and video still can flow, and the
3939// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003940TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003941 ASSERT_TRUE(CreatePeerConnectionWrappers());
3942 ConnectFakeSignaling();
3943 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003944 caller()->AddAudioVideoTracks();
3945 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003946 caller()->CreateAndSetAndSignalOffer();
3947 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3948 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3949 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003950 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3951 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07003952
3953 // To verify that the ICE restart actually occurs, get
3954 // ufrag/password/candidates before and after restart.
3955 // Create an SDP string of the first audio candidate for both clients.
3956 const webrtc::IceCandidateCollection* audio_candidates_caller =
3957 caller()->pc()->local_description()->candidates(0);
3958 const webrtc::IceCandidateCollection* audio_candidates_callee =
3959 callee()->pc()->local_description()->candidates(0);
3960 ASSERT_GT(audio_candidates_caller->count(), 0u);
3961 ASSERT_GT(audio_candidates_callee->count(), 0u);
3962 std::string caller_candidate_pre_restart;
3963 ASSERT_TRUE(
3964 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3965 std::string callee_candidate_pre_restart;
3966 ASSERT_TRUE(
3967 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3968 const cricket::SessionDescription* desc =
3969 caller()->pc()->local_description()->description();
3970 std::string caller_ufrag_pre_restart =
3971 desc->transport_infos()[0].description.ice_ufrag;
3972 desc = callee()->pc()->local_description()->description();
3973 std::string callee_ufrag_pre_restart =
3974 desc->transport_infos()[0].description.ice_ufrag;
3975
3976 // Have the caller initiate an ICE restart.
3977 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3978 caller()->CreateAndSetAndSignalOffer();
3979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3980 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3981 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003982 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07003983 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3984
3985 // Grab the ufrags/candidates again.
3986 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3987 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3988 ASSERT_GT(audio_candidates_caller->count(), 0u);
3989 ASSERT_GT(audio_candidates_callee->count(), 0u);
3990 std::string caller_candidate_post_restart;
3991 ASSERT_TRUE(
3992 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3993 std::string callee_candidate_post_restart;
3994 ASSERT_TRUE(
3995 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3996 desc = caller()->pc()->local_description()->description();
3997 std::string caller_ufrag_post_restart =
3998 desc->transport_infos()[0].description.ice_ufrag;
3999 desc = callee()->pc()->local_description()->description();
4000 std::string callee_ufrag_post_restart =
4001 desc->transport_infos()[0].description.ice_ufrag;
4002 // Sanity check that an ICE restart was actually negotiated in SDP.
4003 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4004 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4005 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4006 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4007
4008 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004009 MediaExpectations media_expectations;
4010 media_expectations.ExpectBidirectionalAudioAndVideo();
4011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004012}
4013
4014// Verify that audio/video can be received end-to-end when ICE renomination is
4015// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004016TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004017 PeerConnectionInterface::RTCConfiguration config;
4018 config.enable_ice_renomination = true;
4019 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4020 ConnectFakeSignaling();
4021 // Do normal offer/answer and wait for some frames to be received in each
4022 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004023 caller()->AddAudioVideoTracks();
4024 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004025 caller()->CreateAndSetAndSignalOffer();
4026 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4027 // Sanity check that ICE renomination was actually negotiated.
4028 const cricket::SessionDescription* desc =
4029 caller()->pc()->local_description()->description();
4030 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004031 ASSERT_NE(
4032 info.description.transport_options.end(),
4033 std::find(info.description.transport_options.begin(),
4034 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004035 }
4036 desc = callee()->pc()->local_description()->description();
4037 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004038 ASSERT_NE(
4039 info.description.transport_options.end(),
4040 std::find(info.description.transport_options.begin(),
4041 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004042 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004043 MediaExpectations media_expectations;
4044 media_expectations.ExpectBidirectionalAudioAndVideo();
4045 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004046}
4047
Steve Anton6f25b092017-10-23 09:39:20 -07004048// With a max bundle policy and RTCP muxing, adding a new media description to
4049// the connection should not affect ICE at all because the new media will use
4050// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004051TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004052 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004053 PeerConnectionInterface::RTCConfiguration config;
4054 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4055 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4056 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4057 config, PeerConnectionInterface::RTCConfiguration()));
4058 ConnectFakeSignaling();
4059
Steve Anton15324772018-01-16 10:26:49 -08004060 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004061 caller()->CreateAndSetAndSignalOffer();
4062 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004063 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4064 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004065
4066 caller()->clear_ice_connection_state_history();
4067
Steve Anton15324772018-01-16 10:26:49 -08004068 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004069 caller()->CreateAndSetAndSignalOffer();
4070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4071
4072 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4073}
4074
deadbeef1dcb1642017-03-29 21:08:16 -07004075// This test sets up a call between two parties with audio and video. It then
4076// renegotiates setting the video m-line to "port 0", then later renegotiates
4077// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004078TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004079 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4080 ASSERT_TRUE(CreatePeerConnectionWrappers());
4081 ConnectFakeSignaling();
4082
4083 // Do initial negotiation, only sending media from the caller. Will result in
4084 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004085 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004086 caller()->CreateAndSetAndSignalOffer();
4087 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4088
4089 // Negotiate again, disabling the video "m=" section (the callee will set the
4090 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004091 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4092 PeerConnectionInterface::RTCOfferAnswerOptions options;
4093 options.offer_to_receive_video = 0;
4094 callee()->SetOfferAnswerOptions(options);
4095 } else {
4096 callee()->SetRemoteOfferHandler([this] {
4097 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4098 });
4099 }
deadbeef1dcb1642017-03-29 21:08:16 -07004100 caller()->CreateAndSetAndSignalOffer();
4101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4102 // Sanity check that video "m=" section was actually rejected.
4103 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4104 callee()->pc()->local_description()->description());
4105 ASSERT_NE(nullptr, answer_video_content);
4106 ASSERT_TRUE(answer_video_content->rejected);
4107
4108 // Enable video and do negotiation again, making sure video is received
4109 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004110 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4111 PeerConnectionInterface::RTCOfferAnswerOptions options;
4112 options.offer_to_receive_video = 1;
4113 callee()->SetOfferAnswerOptions(options);
4114 } else {
4115 // The caller's transceiver is stopped, so we need to add another track.
4116 auto caller_transceiver =
4117 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4118 EXPECT_TRUE(caller_transceiver->stopped());
4119 caller()->AddVideoTrack();
4120 }
4121 callee()->AddVideoTrack();
4122 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004123 caller()->CreateAndSetAndSignalOffer();
4124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004125
deadbeef1dcb1642017-03-29 21:08:16 -07004126 // Verify the caller receives frames from the newly added stream, and the
4127 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004128 MediaExpectations media_expectations;
4129 media_expectations.CalleeExpectsSomeAudio();
4130 media_expectations.ExpectBidirectionalVideo();
4131 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004132}
4133
deadbeef1dcb1642017-03-29 21:08:16 -07004134// This tests that if we negotiate after calling CreateSender but before we
4135// have a track, then set a track later, frames from the newly-set track are
4136// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004137TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004138 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4139 ASSERT_TRUE(CreatePeerConnectionWrappers());
4140 ConnectFakeSignaling();
4141 auto caller_audio_sender =
4142 caller()->pc()->CreateSender("audio", "caller_stream");
4143 auto caller_video_sender =
4144 caller()->pc()->CreateSender("video", "caller_stream");
4145 auto callee_audio_sender =
4146 callee()->pc()->CreateSender("audio", "callee_stream");
4147 auto callee_video_sender =
4148 callee()->pc()->CreateSender("video", "callee_stream");
4149 caller()->CreateAndSetAndSignalOffer();
4150 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4151 // Wait for ICE to complete, without any tracks being set.
4152 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4153 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4154 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4155 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4156 // Now set the tracks, and expect frames to immediately start flowing.
4157 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4158 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4159 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4160 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004161 MediaExpectations media_expectations;
4162 media_expectations.ExpectBidirectionalAudioAndVideo();
4163 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4164}
4165
4166// This tests that if we negotiate after calling AddTransceiver but before we
4167// have a track, then set a track later, frames from the newly-set tracks are
4168// received end-to-end.
4169TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4170 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4171 ASSERT_TRUE(CreatePeerConnectionWrappers());
4172 ConnectFakeSignaling();
4173 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4174 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4175 auto caller_audio_sender = audio_result.MoveValue()->sender();
4176 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4177 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4178 auto caller_video_sender = video_result.MoveValue()->sender();
4179 callee()->SetRemoteOfferHandler([this] {
4180 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4181 callee()->pc()->GetTransceivers()[0]->SetDirection(
4182 RtpTransceiverDirection::kSendRecv);
4183 callee()->pc()->GetTransceivers()[1]->SetDirection(
4184 RtpTransceiverDirection::kSendRecv);
4185 });
4186 caller()->CreateAndSetAndSignalOffer();
4187 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4188 // Wait for ICE to complete, without any tracks being set.
4189 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4190 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4191 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4192 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4193 // Now set the tracks, and expect frames to immediately start flowing.
4194 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4195 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4196 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4197 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4198 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4199 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4200 MediaExpectations media_expectations;
4201 media_expectations.ExpectBidirectionalAudioAndVideo();
4202 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004203}
4204
4205// This test verifies that a remote video track can be added via AddStream,
4206// and sent end-to-end. For this particular test, it's simply echoed back
4207// from the caller to the callee, rather than being forwarded to a third
4208// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004209TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004210 ASSERT_TRUE(CreatePeerConnectionWrappers());
4211 ConnectFakeSignaling();
4212 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004213 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004214 caller()->CreateAndSetAndSignalOffer();
4215 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004216 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004217
4218 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4219 // time).
4220 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4221 callee()->CreateAndSetAndSignalOffer();
4222 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4223
Seth Hampson2f0d7022018-02-20 11:54:42 -08004224 MediaExpectations media_expectations;
4225 media_expectations.ExpectBidirectionalVideo();
4226 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004227}
4228
4229// Test that we achieve the expected end-to-end connection time, using a
4230// fake clock and simulated latency on the media and signaling paths.
4231// We use a TURN<->TURN connection because this is usually the quickest to
4232// set up initially, especially when we're confident the connection will work
4233// and can start sending media before we get a STUN response.
4234//
4235// With various optimizations enabled, here are the network delays we expect to
4236// be on the critical path:
4237// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4238// signaling answer (with DTLS fingerprint).
4239// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4240// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4241// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004242TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004243 rtc::ScopedFakeClock fake_clock;
4244 // Some things use a time of "0" as a special value, so we need to start out
4245 // the fake clock at a nonzero time.
4246 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004247 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004248
4249 static constexpr int media_hop_delay_ms = 50;
4250 static constexpr int signaling_trip_delay_ms = 500;
4251 // For explanation of these values, see comment above.
4252 static constexpr int required_media_hops = 9;
4253 static constexpr int required_signaling_trips = 2;
4254 // For internal delays (such as posting an event asychronously).
4255 static constexpr int allowed_internal_delay_ms = 20;
4256 static constexpr int total_connection_time_ms =
4257 media_hop_delay_ms * required_media_hops +
4258 signaling_trip_delay_ms * required_signaling_trips +
4259 allowed_internal_delay_ms;
4260
4261 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4262 3478};
4263 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4264 0};
4265 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4266 3478};
4267 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4268 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004269 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4270 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004271
Seth Hampsonaed71642018-06-11 07:41:32 -07004272 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4273 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004274 // Bypass permission check on received packets so media can be sent before
4275 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004276 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4277 turn_server_1->set_enable_permission_checks(false);
4278 });
4279 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4280 turn_server_2->set_enable_permission_checks(false);
4281 });
deadbeef1dcb1642017-03-29 21:08:16 -07004282
4283 PeerConnectionInterface::RTCConfiguration client_1_config;
4284 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4285 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4286 ice_server_1.username = "test";
4287 ice_server_1.password = "test";
4288 client_1_config.servers.push_back(ice_server_1);
4289 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4290 client_1_config.presume_writable_when_fully_relayed = true;
4291
4292 PeerConnectionInterface::RTCConfiguration client_2_config;
4293 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4294 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4295 ice_server_2.username = "test";
4296 ice_server_2.password = "test";
4297 client_2_config.servers.push_back(ice_server_2);
4298 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4299 client_2_config.presume_writable_when_fully_relayed = true;
4300
4301 ASSERT_TRUE(
4302 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4303 // Set up the simulated delays.
4304 SetSignalingDelayMs(signaling_trip_delay_ms);
4305 ConnectFakeSignaling();
4306 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4307 virtual_socket_server()->UpdateDelayDistribution();
4308
4309 // Set "offer to receive audio/video" without adding any tracks, so we just
4310 // set up ICE/DTLS with no media.
4311 PeerConnectionInterface::RTCOfferAnswerOptions options;
4312 options.offer_to_receive_audio = 1;
4313 options.offer_to_receive_video = 1;
4314 caller()->SetOfferAnswerOptions(options);
4315 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004316 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4317 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004318 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4319 // If this is not done a DCHECK can be hit in ports.cc, because a large
4320 // negative number is calculated for the rtt due to the global clock changing.
4321 caller()->pc()->Close();
4322 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004323}
4324
Jonas Orelandbdcee282017-10-10 14:01:40 +02004325// Verify that a TurnCustomizer passed in through RTCConfiguration
4326// is actually used by the underlying TURN candidate pair.
4327// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004328TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004329 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4330 3478};
4331 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4332 0};
4333 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4334 3478};
4335 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4336 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004337 CreateTurnServer(turn_server_1_internal_address,
4338 turn_server_1_external_address);
4339 CreateTurnServer(turn_server_2_internal_address,
4340 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004341
4342 PeerConnectionInterface::RTCConfiguration client_1_config;
4343 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4344 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4345 ice_server_1.username = "test";
4346 ice_server_1.password = "test";
4347 client_1_config.servers.push_back(ice_server_1);
4348 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004349 auto* customizer1 = CreateTurnCustomizer();
4350 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004351
4352 PeerConnectionInterface::RTCConfiguration client_2_config;
4353 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4354 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4355 ice_server_2.username = "test";
4356 ice_server_2.password = "test";
4357 client_2_config.servers.push_back(ice_server_2);
4358 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004359 auto* customizer2 = CreateTurnCustomizer();
4360 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004361
4362 ASSERT_TRUE(
4363 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4364 ConnectFakeSignaling();
4365
4366 // Set "offer to receive audio/video" without adding any tracks, so we just
4367 // set up ICE/DTLS with no media.
4368 PeerConnectionInterface::RTCOfferAnswerOptions options;
4369 options.offer_to_receive_audio = 1;
4370 options.offer_to_receive_video = 1;
4371 caller()->SetOfferAnswerOptions(options);
4372 caller()->CreateAndSetAndSignalOffer();
4373 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4374
Seth Hampsonaed71642018-06-11 07:41:32 -07004375 ExpectTurnCustomizerCountersIncremented(customizer1);
4376 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004377}
4378
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004379// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4380// send media between the caller and the callee.
4381TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4382 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4383 3478};
4384 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4385
4386 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004387 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4388 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004389
4390 webrtc::PeerConnectionInterface::IceServer ice_server;
4391 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4392 ice_server.username = "test";
4393 ice_server.password = "test";
4394
4395 PeerConnectionInterface::RTCConfiguration client_1_config;
4396 client_1_config.servers.push_back(ice_server);
4397 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4398
4399 PeerConnectionInterface::RTCConfiguration client_2_config;
4400 client_2_config.servers.push_back(ice_server);
4401 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4402
4403 ASSERT_TRUE(
4404 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4405
4406 // Do normal offer/answer and wait for ICE to complete.
4407 ConnectFakeSignaling();
4408 caller()->AddAudioVideoTracks();
4409 callee()->AddAudioVideoTracks();
4410 caller()->CreateAndSetAndSignalOffer();
4411 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4412 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4413 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4414
4415 MediaExpectations media_expectations;
4416 media_expectations.ExpectBidirectionalAudioAndVideo();
4417 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4418}
4419
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004420// Verify that a SSLCertificateVerifier passed in through
4421// PeerConnectionDependencies is actually used by the underlying SSL
4422// implementation to determine whether a certificate presented by the TURN
4423// server is accepted by the client. Note that openssladapter_unittest.cc
4424// contains more detailed, lower-level tests.
4425TEST_P(PeerConnectionIntegrationTest,
4426 SSLCertificateVerifierUsedForTurnConnections) {
4427 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4428 3478};
4429 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4430
4431 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4432 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004433 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4434 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004435
4436 webrtc::PeerConnectionInterface::IceServer ice_server;
4437 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4438 ice_server.username = "test";
4439 ice_server.password = "test";
4440
4441 PeerConnectionInterface::RTCConfiguration client_1_config;
4442 client_1_config.servers.push_back(ice_server);
4443 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4444
4445 PeerConnectionInterface::RTCConfiguration client_2_config;
4446 client_2_config.servers.push_back(ice_server);
4447 // Setting the type to kRelay forces the connection to go through a TURN
4448 // server.
4449 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4450
4451 // Get a copy to the pointer so we can verify calls later.
4452 rtc::TestCertificateVerifier* client_1_cert_verifier =
4453 new rtc::TestCertificateVerifier();
4454 client_1_cert_verifier->verify_certificate_ = true;
4455 rtc::TestCertificateVerifier* client_2_cert_verifier =
4456 new rtc::TestCertificateVerifier();
4457 client_2_cert_verifier->verify_certificate_ = true;
4458
4459 // Create the dependencies with the test certificate verifier.
4460 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4461 client_1_deps.tls_cert_verifier =
4462 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4463 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4464 client_2_deps.tls_cert_verifier =
4465 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4466
4467 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4468 client_1_config, std::move(client_1_deps), client_2_config,
4469 std::move(client_2_deps)));
4470 ConnectFakeSignaling();
4471
4472 // Set "offer to receive audio/video" without adding any tracks, so we just
4473 // set up ICE/DTLS with no media.
4474 PeerConnectionInterface::RTCOfferAnswerOptions options;
4475 options.offer_to_receive_audio = 1;
4476 options.offer_to_receive_video = 1;
4477 caller()->SetOfferAnswerOptions(options);
4478 caller()->CreateAndSetAndSignalOffer();
4479 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4480
4481 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4482 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004483}
4484
4485TEST_P(PeerConnectionIntegrationTest,
4486 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4487 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4488 3478};
4489 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4490
4491 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4492 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004493 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4494 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004495
4496 webrtc::PeerConnectionInterface::IceServer ice_server;
4497 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4498 ice_server.username = "test";
4499 ice_server.password = "test";
4500
4501 PeerConnectionInterface::RTCConfiguration client_1_config;
4502 client_1_config.servers.push_back(ice_server);
4503 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4504
4505 PeerConnectionInterface::RTCConfiguration client_2_config;
4506 client_2_config.servers.push_back(ice_server);
4507 // Setting the type to kRelay forces the connection to go through a TURN
4508 // server.
4509 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4510
4511 // Get a copy to the pointer so we can verify calls later.
4512 rtc::TestCertificateVerifier* client_1_cert_verifier =
4513 new rtc::TestCertificateVerifier();
4514 client_1_cert_verifier->verify_certificate_ = false;
4515 rtc::TestCertificateVerifier* client_2_cert_verifier =
4516 new rtc::TestCertificateVerifier();
4517 client_2_cert_verifier->verify_certificate_ = false;
4518
4519 // Create the dependencies with the test certificate verifier.
4520 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4521 client_1_deps.tls_cert_verifier =
4522 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4523 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4524 client_2_deps.tls_cert_verifier =
4525 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4526
4527 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4528 client_1_config, std::move(client_1_deps), client_2_config,
4529 std::move(client_2_deps)));
4530 ConnectFakeSignaling();
4531
4532 // Set "offer to receive audio/video" without adding any tracks, so we just
4533 // set up ICE/DTLS with no media.
4534 PeerConnectionInterface::RTCOfferAnswerOptions options;
4535 options.offer_to_receive_audio = 1;
4536 options.offer_to_receive_video = 1;
4537 caller()->SetOfferAnswerOptions(options);
4538 caller()->CreateAndSetAndSignalOffer();
4539 bool wait_res = true;
4540 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4541 // properly, should be able to just wait for a state of "failed" instead of
4542 // waiting a fixed 10 seconds.
4543 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4544 ASSERT_FALSE(wait_res);
4545
4546 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4547 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004548}
4549
deadbeefc964d0b2017-04-03 10:03:35 -07004550// Test that audio and video flow end-to-end when codec names don't use the
4551// expected casing, given that they're supposed to be case insensitive. To test
4552// this, all but one codec is removed from each media description, and its
4553// casing is changed.
4554//
4555// In the past, this has regressed and caused crashes/black video, due to the
4556// fact that code at some layers was doing case-insensitive comparisons and
4557// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004558TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004559 ASSERT_TRUE(CreatePeerConnectionWrappers());
4560 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004561 caller()->AddAudioVideoTracks();
4562 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004563
4564 // Remove all but one audio/video codec (opus and VP8), and change the
4565 // casing of the caller's generated offer.
4566 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4567 cricket::AudioContentDescription* audio =
4568 GetFirstAudioContentDescription(description);
4569 ASSERT_NE(nullptr, audio);
4570 auto audio_codecs = audio->codecs();
4571 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4572 [](const cricket::AudioCodec& codec) {
4573 return codec.name != "opus";
4574 }),
4575 audio_codecs.end());
4576 ASSERT_EQ(1u, audio_codecs.size());
4577 audio_codecs[0].name = "OpUs";
4578 audio->set_codecs(audio_codecs);
4579
4580 cricket::VideoContentDescription* video =
4581 GetFirstVideoContentDescription(description);
4582 ASSERT_NE(nullptr, video);
4583 auto video_codecs = video->codecs();
4584 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4585 [](const cricket::VideoCodec& codec) {
4586 return codec.name != "VP8";
4587 }),
4588 video_codecs.end());
4589 ASSERT_EQ(1u, video_codecs.size());
4590 video_codecs[0].name = "vP8";
4591 video->set_codecs(video_codecs);
4592 });
4593
4594 caller()->CreateAndSetAndSignalOffer();
4595 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4596
4597 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004598 MediaExpectations media_expectations;
4599 media_expectations.ExpectBidirectionalAudioAndVideo();
4600 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004601}
4602
Jonas Oreland49ac5952018-09-26 16:04:32 +02004603TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004604 ASSERT_TRUE(CreatePeerConnectionWrappers());
4605 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004606 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004607 caller()->CreateAndSetAndSignalOffer();
4608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004609 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004610 MediaExpectations media_expectations;
4611 media_expectations.CalleeExpectsSomeAudio(1);
4612 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004613 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004614 auto receiver = callee()->pc()->GetReceivers()[0];
4615 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004616 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004617 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4618 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004619 sources[0].source_id());
4620 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4621}
4622
4623TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4624 ASSERT_TRUE(CreatePeerConnectionWrappers());
4625 ConnectFakeSignaling();
4626 caller()->AddVideoTrack();
4627 caller()->CreateAndSetAndSignalOffer();
4628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4629 // Wait for one video frame to be received by the callee.
4630 MediaExpectations media_expectations;
4631 media_expectations.CalleeExpectsSomeVideo(1);
4632 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4633 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4634 auto receiver = callee()->pc()->GetReceivers()[0];
4635 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4636 auto sources = receiver->GetSources();
4637 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4638 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4639 sources[0].source_id());
4640 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004641}
4642
deadbeef2f425aa2017-04-14 10:41:32 -07004643// Test that if a track is removed and added again with a different stream ID,
4644// the new stream ID is successfully communicated in SDP and media continues to
4645// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004646// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4647// it will not reuse a transceiver that has already been sending. After creating
4648// a new transceiver it tries to create an offer with two senders of the same
4649// track ids and it fails.
4650TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004651 ASSERT_TRUE(CreatePeerConnectionWrappers());
4652 ConnectFakeSignaling();
4653
deadbeef2f425aa2017-04-14 10:41:32 -07004654 // Add track using stream 1, do offer/answer.
4655 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4656 caller()->CreateLocalAudioTrack();
4657 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004658 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004659 caller()->CreateAndSetAndSignalOffer();
4660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004661 {
4662 MediaExpectations media_expectations;
4663 media_expectations.CalleeExpectsSomeAudio(1);
4664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4665 }
deadbeef2f425aa2017-04-14 10:41:32 -07004666 // Remove the sender, and create a new one with the new stream.
4667 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004668 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004669 caller()->CreateAndSetAndSignalOffer();
4670 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4671 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004672 {
4673 MediaExpectations media_expectations;
4674 media_expectations.CalleeExpectsSomeAudio();
4675 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4676 }
deadbeef2f425aa2017-04-14 10:41:32 -07004677}
4678
Seth Hampson2f0d7022018-02-20 11:54:42 -08004679TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004680 ASSERT_TRUE(CreatePeerConnectionWrappers());
4681 ConnectFakeSignaling();
4682
Karl Wiberg918f50c2018-07-05 11:40:33 +02004683 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004684 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4685 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4686 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004687 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4688 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004689
Steve Anton15324772018-01-16 10:26:49 -08004690 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004691 caller()->CreateAndSetAndSignalOffer();
4692 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4693}
4694
Steve Antonede9ca52017-10-16 13:04:27 -07004695// Test that if candidates are only signaled by applying full session
4696// descriptions (instead of using AddIceCandidate), the peers can connect to
4697// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004698TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004699 ASSERT_TRUE(CreatePeerConnectionWrappers());
4700 // Each side will signal the session descriptions but not candidates.
4701 ConnectFakeSignalingForSdpOnly();
4702
4703 // Add audio video track and exchange the initial offer/answer with media
4704 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004705 caller()->AddAudioVideoTracks();
4706 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004707 caller()->CreateAndSetAndSignalOffer();
4708
4709 // Wait for all candidates to be gathered on both the caller and callee.
4710 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4711 caller()->ice_gathering_state(), kDefaultTimeout);
4712 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4713 callee()->ice_gathering_state(), kDefaultTimeout);
4714
4715 // The candidates will now be included in the session description, so
4716 // signaling them will start the ICE connection.
4717 caller()->CreateAndSetAndSignalOffer();
4718 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4719
4720 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004721 MediaExpectations media_expectations;
4722 media_expectations.ExpectBidirectionalAudioAndVideo();
4723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004724}
4725
henrika5f6bf242017-11-01 11:06:56 +01004726// Test that SetAudioPlayout can be used to disable audio playout from the
4727// start, then later enable it. This may be useful, for example, if the caller
4728// needs to play a local ringtone until some event occurs, after which it
4729// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004730TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004731 ASSERT_TRUE(CreatePeerConnectionWrappers());
4732 ConnectFakeSignaling();
4733
4734 // Set up audio-only call where audio playout is disabled on caller's side.
4735 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004736 caller()->AddAudioTrack();
4737 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004738 caller()->CreateAndSetAndSignalOffer();
4739 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4740
4741 // Pump messages for a second.
4742 WAIT(false, 1000);
4743 // Since audio playout is disabled, the caller shouldn't have received
4744 // anything (at the playout level, at least).
4745 EXPECT_EQ(0, caller()->audio_frames_received());
4746 // As a sanity check, make sure the callee (for which playout isn't disabled)
4747 // did still see frames on its audio level.
4748 ASSERT_GT(callee()->audio_frames_received(), 0);
4749
4750 // Enable playout again, and ensure audio starts flowing.
4751 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004752 MediaExpectations media_expectations;
4753 media_expectations.ExpectBidirectionalAudio();
4754 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004755}
4756
4757double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4758 auto report = pc->NewGetStats();
4759 auto track_stats_list =
4760 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4761 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4762 for (const auto* track_stats : track_stats_list) {
4763 if (track_stats->remote_source.is_defined() &&
4764 *track_stats->remote_source) {
4765 remote_track_stats = track_stats;
4766 break;
4767 }
4768 }
4769
4770 if (!remote_track_stats->total_audio_energy.is_defined()) {
4771 return 0.0;
4772 }
4773 return *remote_track_stats->total_audio_energy;
4774}
4775
4776// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4777// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004778TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004779 DisableAudioPlayoutStillGeneratesAudioStats) {
4780 ASSERT_TRUE(CreatePeerConnectionWrappers());
4781 ConnectFakeSignaling();
4782
4783 // Set up audio-only call where playout is disabled but audio-processing is
4784 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004785 caller()->AddAudioTrack();
4786 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004787 caller()->pc()->SetAudioPlayout(false);
4788
4789 caller()->CreateAndSetAndSignalOffer();
4790 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4791
4792 // Wait for the callee to receive audio stats.
4793 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4794}
4795
henrika4f167df2017-11-01 14:45:55 +01004796// Test that SetAudioRecording can be used to disable audio recording from the
4797// start, then later enable it. This may be useful, for example, if the caller
4798// wants to ensure that no audio resources are active before a certain state
4799// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004800TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004801 ASSERT_TRUE(CreatePeerConnectionWrappers());
4802 ConnectFakeSignaling();
4803
4804 // Set up audio-only call where audio recording is disabled on caller's side.
4805 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004806 caller()->AddAudioTrack();
4807 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004808 caller()->CreateAndSetAndSignalOffer();
4809 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4810
4811 // Pump messages for a second.
4812 WAIT(false, 1000);
4813 // Since caller has disabled audio recording, the callee shouldn't have
4814 // received anything.
4815 EXPECT_EQ(0, callee()->audio_frames_received());
4816 // As a sanity check, make sure the caller did still see frames on its
4817 // audio level since audio recording is enabled on the calle side.
4818 ASSERT_GT(caller()->audio_frames_received(), 0);
4819
4820 // Enable audio recording again, and ensure audio starts flowing.
4821 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004822 MediaExpectations media_expectations;
4823 media_expectations.ExpectBidirectionalAudio();
4824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004825}
4826
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004827// Test that after closing PeerConnections, they stop sending any packets (ICE,
4828// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004829TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004830 // Set up audio/video/data, wait for some frames to be received.
4831 ASSERT_TRUE(CreatePeerConnectionWrappers());
4832 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004833 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004834#ifdef HAVE_SCTP
4835 caller()->CreateDataChannel();
4836#endif
4837 caller()->CreateAndSetAndSignalOffer();
4838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004839 MediaExpectations media_expectations;
4840 media_expectations.CalleeExpectsSomeAudioAndVideo();
4841 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004842 // Close PeerConnections.
4843 caller()->pc()->Close();
4844 callee()->pc()->Close();
4845 // Pump messages for a second, and ensure no new packets end up sent.
4846 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4847 WAIT(false, 1000);
4848 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4849 EXPECT_EQ(sent_packets_a, sent_packets_b);
4850}
4851
Steve Anton7eca0932018-03-30 15:18:41 -07004852// Test that transport stats are generated by the RTCStatsCollector for a
4853// connection that only involves data channels. This is a regression test for
4854// crbug.com/826972.
4855#ifdef HAVE_SCTP
4856TEST_P(PeerConnectionIntegrationTest,
4857 TransportStatsReportedForDataChannelOnlyConnection) {
4858 ASSERT_TRUE(CreatePeerConnectionWrappers());
4859 ConnectFakeSignaling();
4860 caller()->CreateDataChannel();
4861
4862 caller()->CreateAndSetAndSignalOffer();
4863 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4864 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4865
4866 auto caller_report = caller()->NewGetStats();
4867 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4868 auto callee_report = callee()->NewGetStats();
4869 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4870}
4871#endif // HAVE_SCTP
4872
Qingsi Wang7685e862018-06-11 20:15:46 -07004873TEST_P(PeerConnectionIntegrationTest,
4874 IceEventsGeneratedAndLoggedInRtcEventLog) {
4875 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4876 ConnectFakeSignaling();
4877 PeerConnectionInterface::RTCOfferAnswerOptions options;
4878 options.offer_to_receive_audio = 1;
4879 caller()->SetOfferAnswerOptions(options);
4880 caller()->CreateAndSetAndSignalOffer();
4881 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4882 ASSERT_NE(nullptr, caller()->event_log_factory());
4883 ASSERT_NE(nullptr, callee()->event_log_factory());
4884 webrtc::FakeRtcEventLog* caller_event_log =
4885 static_cast<webrtc::FakeRtcEventLog*>(
4886 caller()->event_log_factory()->last_log_created());
4887 webrtc::FakeRtcEventLog* callee_event_log =
4888 static_cast<webrtc::FakeRtcEventLog*>(
4889 callee()->event_log_factory()->last_log_created());
4890 ASSERT_NE(nullptr, caller_event_log);
4891 ASSERT_NE(nullptr, callee_event_log);
4892 int caller_ice_config_count = caller_event_log->GetEventCount(
4893 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4894 int caller_ice_event_count = caller_event_log->GetEventCount(
4895 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4896 int callee_ice_config_count = callee_event_log->GetEventCount(
4897 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4898 int callee_ice_event_count = callee_event_log->GetEventCount(
4899 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4900 EXPECT_LT(0, caller_ice_config_count);
4901 EXPECT_LT(0, caller_ice_event_count);
4902 EXPECT_LT(0, callee_ice_config_count);
4903 EXPECT_LT(0, callee_ice_event_count);
4904}
4905
Seth Hampson2f0d7022018-02-20 11:54:42 -08004906INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4907 PeerConnectionIntegrationTest,
4908 Values(SdpSemantics::kPlanB,
4909 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004910
Steve Anton74255ff2018-01-24 18:32:57 -08004911// Tests that verify interoperability between Plan B and Unified Plan
4912// PeerConnections.
4913class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004914 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004915 public ::testing::WithParamInterface<
4916 std::tuple<SdpSemantics, SdpSemantics>> {
4917 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004918 // Setting the SdpSemantics for the base test to kDefault does not matter
4919 // because we specify not to use the test semantics when creating
4920 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004921 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004922 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004923 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004924 callee_semantics_(std::get<1>(GetParam())) {}
4925
4926 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004927 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4928 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004929 }
4930
4931 const SdpSemantics caller_semantics_;
4932 const SdpSemantics callee_semantics_;
4933};
4934
4935TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4936 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4937 ConnectFakeSignaling();
4938
4939 caller()->CreateAndSetAndSignalOffer();
4940 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4941}
4942
4943TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4944 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4945 ConnectFakeSignaling();
4946 auto audio_sender = caller()->AddAudioTrack();
4947
4948 caller()->CreateAndSetAndSignalOffer();
4949 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4950
4951 // Verify that one audio receiver has been created on the remote and that it
4952 // has the same track ID as the sending track.
4953 auto receivers = callee()->pc()->GetReceivers();
4954 ASSERT_EQ(1u, receivers.size());
4955 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4956 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4957
Seth Hampson2f0d7022018-02-20 11:54:42 -08004958 MediaExpectations media_expectations;
4959 media_expectations.CalleeExpectsSomeAudio();
4960 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004961}
4962
4963TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4964 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4965 ConnectFakeSignaling();
4966 auto video_sender = caller()->AddVideoTrack();
4967 auto audio_sender = caller()->AddAudioTrack();
4968
4969 caller()->CreateAndSetAndSignalOffer();
4970 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4971
4972 // Verify that one audio and one video receiver have been created on the
4973 // remote and that they have the same track IDs as the sending tracks.
4974 auto audio_receivers =
4975 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4976 ASSERT_EQ(1u, audio_receivers.size());
4977 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4978 auto video_receivers =
4979 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4980 ASSERT_EQ(1u, video_receivers.size());
4981 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4982
Seth Hampson2f0d7022018-02-20 11:54:42 -08004983 MediaExpectations media_expectations;
4984 media_expectations.CalleeExpectsSomeAudioAndVideo();
4985 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004986}
4987
4988TEST_P(PeerConnectionIntegrationInteropTest,
4989 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4990 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4991 ConnectFakeSignaling();
4992 caller()->AddAudioVideoTracks();
4993 callee()->AddAudioVideoTracks();
4994
4995 caller()->CreateAndSetAndSignalOffer();
4996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4997
Seth Hampson2f0d7022018-02-20 11:54:42 -08004998 MediaExpectations media_expectations;
4999 media_expectations.ExpectBidirectionalAudioAndVideo();
5000 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005001}
5002
5003TEST_P(PeerConnectionIntegrationInteropTest,
5004 ReverseRolesOneAudioLocalToOneVideoRemote) {
5005 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5006 ConnectFakeSignaling();
5007 caller()->AddAudioTrack();
5008 callee()->AddVideoTrack();
5009
5010 caller()->CreateAndSetAndSignalOffer();
5011 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5012
5013 // Verify that only the audio track has been negotiated.
5014 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5015 // Might also check that the callee's NegotiationNeeded flag is set.
5016
5017 // Reverse roles.
5018 callee()->CreateAndSetAndSignalOffer();
5019 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5020
Seth Hampson2f0d7022018-02-20 11:54:42 -08005021 MediaExpectations media_expectations;
5022 media_expectations.CallerExpectsSomeVideo();
5023 media_expectations.CalleeExpectsSomeAudio();
5024 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005025}
5026
Steve Antonba42e992018-04-09 14:10:01 -07005027INSTANTIATE_TEST_CASE_P(
5028 PeerConnectionIntegrationTest,
5029 PeerConnectionIntegrationInteropTest,
5030 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5031 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5032
5033// Test that if the Unified Plan side offers two video tracks then the Plan B
5034// side will only see the first one and ignore the second.
5035TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005036 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5037 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005038 ConnectFakeSignaling();
5039 auto first_sender = caller()->AddVideoTrack();
5040 caller()->AddVideoTrack();
5041
5042 caller()->CreateAndSetAndSignalOffer();
5043 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5044
5045 // Verify that there is only one receiver and it corresponds to the first
5046 // added track.
5047 auto receivers = callee()->pc()->GetReceivers();
5048 ASSERT_EQ(1u, receivers.size());
5049 EXPECT_TRUE(receivers[0]->track()->enabled());
5050 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5051
Seth Hampson2f0d7022018-02-20 11:54:42 -08005052 MediaExpectations media_expectations;
5053 media_expectations.CalleeExpectsSomeVideo();
5054 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005055}
5056
deadbeef1dcb1642017-03-29 21:08:16 -07005057} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005058} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005059
5060#endif // if !defined(THREAD_SANITIZER)