blob: 4091006656d5435fa26c559435a8a2590ba255eb [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
Qingsi Wang1dac6d82018-12-12 15:28:47 -080025#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020026#include "api/audio_codecs/builtin_audio_decoder_factory.h"
27#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
29#include "api/peer_connection_interface.h"
30#include "api/peer_connection_proxy.h"
31#include "api/rtp_receiver_interface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080032#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/builtin_video_decoder_factory.h"
35#include "api/video_codecs/builtin_video_encoder_factory.h"
36#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070037#include "call/call.h"
38#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010039#include "logging/rtc_event_log/rtc_event_log_factory.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/engine/fake_webrtc_video_engine.h"
42#include "media/engine/webrtc_media_engine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070043#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/mock_async_resolver.h"
45#include "p2p/base/p2p_constants.h"
46#include "p2p/base/port_interface.h"
47#include "p2p/base/test_stun_server.h"
48#include "p2p/base/test_turn_customizer.h"
49#include "p2p/base/test_turn_server.h"
50#include "p2p/client/basic_port_allocator.h"
51#include "pc/dtmf_sender.h"
52#include "pc/local_audio_source.h"
53#include "pc/media_session.h"
54#include "pc/peer_connection.h"
55#include "pc/peer_connection_factory.h"
56#include "pc/rtp_media_utils.h"
57#include "pc/session_description.h"
58#include "pc/test/fake_audio_capture_module.h"
59#include "pc/test/fake_periodic_video_track_source.h"
60#include "pc/test/fake_rtc_certificate_generator.h"
61#include "pc/test/fake_video_track_renderer.h"
62#include "pc/test/mock_peer_connection_observers.h"
63#include "rtc_base/fake_network.h"
64#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020066#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080067#include "rtc_base/test_certificate_verifier.h"
68#include "rtc_base/time_utils.h"
69#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020070#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020071#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070072
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010073namespace webrtc {
74namespace {
75
76using ::cricket::ContentInfo;
77using ::cricket::StreamParams;
78using ::rtc::SocketAddress;
79using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080080using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070081using ::testing::ElementsAre;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070082using ::testing::Return;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080083using ::testing::NiceMock;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070084using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080085using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010086using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080087using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070088
89static const int kDefaultTimeout = 10000;
90static const int kMaxWaitForStatsMs = 3000;
91static const int kMaxWaitForActivationMs = 5000;
92static const int kMaxWaitForFramesMs = 10000;
93// Default number of audio/video frames to wait for before considering a test
94// successful.
95static const int kDefaultExpectedAudioFrameCount = 3;
96static const int kDefaultExpectedVideoFrameCount = 3;
97
deadbeef1dcb1642017-03-29 21:08:16 -070098static const char kDataChannelLabel[] = "data_channel";
99
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700103static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
104
Steve Antonede9ca52017-10-16 13:04:27 -0700105static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
106
deadbeef1dcb1642017-03-29 21:08:16 -0700107// Helper function for constructing offer/answer options to initiate an ICE
108// restart.
109PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
110 PeerConnectionInterface::RTCOfferAnswerOptions options;
111 options.ice_restart = true;
112 return options;
113}
114
deadbeefd8ad7882017-04-18 16:01:17 -0700115// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
116// attribute from received SDP, simulating a legacy endpoint.
117void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
118 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800119 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700120 }
121 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100122 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700123}
124
Seth Hampson5897a6e2018-04-03 11:16:33 -0700125// Removes all stream information besides the stream ids, simulating an
126// endpoint that only signals a=msid lines to convey stream_ids.
127void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
128 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700129 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700130 std::vector<std::string> stream_ids;
131 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700132 const StreamParams& first_stream =
133 content.media_description()->streams()[0];
134 track_id = first_stream.id;
135 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700136 }
137 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700138 StreamParams new_stream;
139 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700140 new_stream.set_stream_ids(stream_ids);
141 content.media_description()->AddStream(new_stream);
142 }
143}
144
zhihuangf8164932017-05-19 13:09:47 -0700145int FindFirstMediaStatsIndexByKind(
146 const std::string& kind,
147 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
148 media_stats_vec) {
149 for (size_t i = 0; i < media_stats_vec.size(); i++) {
150 if (media_stats_vec[i]->kind.ValueToString() == kind) {
151 return i;
152 }
153 }
154 return -1;
155}
156
deadbeef1dcb1642017-03-29 21:08:16 -0700157class SignalingMessageReceiver {
158 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800159 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700160 virtual void ReceiveIceMessage(const std::string& sdp_mid,
161 int sdp_mline_index,
162 const std::string& msg) = 0;
163
164 protected:
165 SignalingMessageReceiver() {}
166 virtual ~SignalingMessageReceiver() {}
167};
168
169class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
170 public:
171 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
172 : expected_media_type_(media_type) {}
173
174 void OnFirstPacketReceived(cricket::MediaType media_type) override {
175 ASSERT_EQ(expected_media_type_, media_type);
176 first_packet_received_ = true;
177 }
178
179 bool first_packet_received() const { return first_packet_received_; }
180
181 virtual ~MockRtpReceiverObserver() {}
182
183 private:
184 bool first_packet_received_ = false;
185 cricket::MediaType expected_media_type_;
186};
187
188// Helper class that wraps a peer connection, observes it, and can accept
189// signaling messages from another wrapper.
190//
191// Uses a fake network, fake A/V capture, and optionally fake
192// encoders/decoders, though they aren't used by default since they don't
193// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700194// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800195// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700196class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800197 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700198 public:
199 // Different factory methods for convenience.
200 // TODO(deadbeef): Could use the pattern of:
201 //
202 // PeerConnectionWrapper =
203 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
204 //
205 // To reduce some code duplication.
206 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
207 const std::string& debug_name,
208 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
209 rtc::Thread* network_thread,
210 rtc::Thread* worker_thread) {
211 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700212 webrtc::PeerConnectionDependencies dependencies(nullptr);
213 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200214 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800215 worker_thread, nullptr,
216 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700217 delete client;
218 return nullptr;
219 }
220 return client;
221 }
222
deadbeef2f425aa2017-04-14 10:41:32 -0700223 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
224 return peer_connection_factory_.get();
225 }
226
deadbeef1dcb1642017-03-29 21:08:16 -0700227 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
228
229 // If a signaling message receiver is set (via ConnectFakeSignaling), this
230 // will set the whole offer/answer exchange in motion. Just need to wait for
231 // the signaling state to reach "stable".
232 void CreateAndSetAndSignalOffer() {
233 auto offer = CreateOffer();
234 ASSERT_NE(nullptr, offer);
235 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
236 }
237
238 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
239 // when a remote offer is received (via fake signaling) and an answer is
240 // generated. By default, uses default options.
241 void SetOfferAnswerOptions(
242 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
243 offer_answer_options_ = options;
244 }
245
246 // Set a callback to be invoked when SDP is received via the fake signaling
247 // channel, which provides an opportunity to munge (modify) the SDP. This is
248 // used to test SDP being applied that a PeerConnection would normally not
249 // generate, but a non-JSEP endpoint might.
250 void SetReceivedSdpMunger(
251 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100252 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700253 }
254
deadbeefc964d0b2017-04-03 10:03:35 -0700255 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700256 // generated.
257 void SetGeneratedSdpMunger(
258 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100259 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700260 }
261
Seth Hampson2f0d7022018-02-20 11:54:42 -0800262 // Set a callback to be invoked when a remote offer is received via the fake
263 // signaling channel. This provides an opportunity to change the
264 // PeerConnection state before an answer is created and sent to the caller.
265 void SetRemoteOfferHandler(std::function<void()> handler) {
266 remote_offer_handler_ = std::move(handler);
267 }
268
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800269 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
270 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700271 }
272
Steve Antonede9ca52017-10-16 13:04:27 -0700273 // Every ICE connection state in order that has been seen by the observer.
274 std::vector<PeerConnectionInterface::IceConnectionState>
275 ice_connection_state_history() const {
276 return ice_connection_state_history_;
277 }
Steve Anton6f25b092017-10-23 09:39:20 -0700278 void clear_ice_connection_state_history() {
279 ice_connection_state_history_.clear();
280 }
Steve Antonede9ca52017-10-16 13:04:27 -0700281
Jonas Olsson635474e2018-10-18 15:58:17 +0200282 // Every PeerConnection state in order that has been seen by the observer.
283 std::vector<PeerConnectionInterface::PeerConnectionState>
284 peer_connection_state_history() const {
285 return peer_connection_state_history_;
286 }
287
Steve Antonede9ca52017-10-16 13:04:27 -0700288 // Every ICE gathering state in order that has been seen by the observer.
289 std::vector<PeerConnectionInterface::IceGatheringState>
290 ice_gathering_state_history() const {
291 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700292 }
293
Steve Anton15324772018-01-16 10:26:49 -0800294 void AddAudioVideoTracks() {
295 AddAudioTrack();
296 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700297 }
298
Steve Anton74255ff2018-01-24 18:32:57 -0800299 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
300 return AddTrack(CreateLocalAudioTrack());
301 }
deadbeef1dcb1642017-03-29 21:08:16 -0700302
Steve Anton74255ff2018-01-24 18:32:57 -0800303 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
304 return AddTrack(CreateLocalVideoTrack());
305 }
deadbeef1dcb1642017-03-29 21:08:16 -0700306
307 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200308 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700309 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200310 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700311 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200312 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700313 // TODO(perkj): Test audio source when it is implemented. Currently audio
314 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700315 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700316 source);
317 }
318
319 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200320 webrtc::FakePeriodicVideoSource::Config config;
321 config.timestamp_offset_ms = rtc::TimeMillis();
322 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700323 }
324
325 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200326 CreateLocalVideoTrackWithConfig(
327 webrtc::FakePeriodicVideoSource::Config config) {
328 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700329 }
330
331 rtc::scoped_refptr<webrtc::VideoTrackInterface>
332 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200333 webrtc::FakePeriodicVideoSource::Config config;
334 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200335 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200336 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700337 }
338
Steve Anton74255ff2018-01-24 18:32:57 -0800339 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
340 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800341 const std::vector<std::string>& stream_ids = {}) {
342 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800343 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800344 return result.MoveValue();
345 }
346
347 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
348 cricket::MediaType media_type) {
349 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
350 for (auto receiver : pc()->GetReceivers()) {
351 if (receiver->media_type() == media_type) {
352 receivers.push_back(receiver);
353 }
354 }
355 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700356 }
357
Seth Hampson2f0d7022018-02-20 11:54:42 -0800358 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
359 cricket::MediaType media_type) {
360 for (auto transceiver : pc()->GetTransceivers()) {
361 if (transceiver->receiver()->media_type() == media_type) {
362 return transceiver;
363 }
364 }
365 return nullptr;
366 }
367
deadbeef1dcb1642017-03-29 21:08:16 -0700368 bool SignalingStateStable() {
369 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
370 }
371
372 void CreateDataChannel() { CreateDataChannel(nullptr); }
373
374 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700375 CreateDataChannel(kDataChannelLabel, init);
376 }
377
378 void CreateDataChannel(const std::string& label,
379 const webrtc::DataChannelInit* init) {
380 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700381 ASSERT_TRUE(data_channel_.get() != nullptr);
382 data_observer_.reset(new MockDataChannelObserver(data_channel_));
383 }
384
385 DataChannelInterface* data_channel() { return data_channel_; }
386 const MockDataChannelObserver* data_observer() const {
387 return data_observer_.get();
388 }
389
390 int audio_frames_received() const {
391 return fake_audio_capture_module_->frames_received();
392 }
393
394 // Takes minimum of video frames received for each track.
395 //
396 // Can be used like:
397 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
398 //
399 // To ensure that all video tracks received at least a certain number of
400 // frames.
401 int min_video_frames_received_per_track() const {
402 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200403 if (fake_video_renderers_.empty()) {
404 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700405 }
deadbeef1dcb1642017-03-29 21:08:16 -0700406
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200407 for (const auto& pair : fake_video_renderers_) {
408 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700409 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200410 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700411 }
412
413 // Returns a MockStatsObserver in a state after stats gathering finished,
414 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700415 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700416 webrtc::MediaStreamTrackInterface* track) {
417 rtc::scoped_refptr<MockStatsObserver> observer(
418 new rtc::RefCountedObject<MockStatsObserver>());
419 EXPECT_TRUE(peer_connection_->GetStats(
420 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
421 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
422 return observer;
423 }
424
425 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700426 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
427 return OldGetStatsForTrack(nullptr);
428 }
429
430 // Synchronously gets stats and returns them. If it times out, fails the test
431 // and returns null.
432 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
433 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
434 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
435 peer_connection_->GetStats(callback);
436 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
437 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700438 }
439
440 int rendered_width() {
441 EXPECT_FALSE(fake_video_renderers_.empty());
442 return fake_video_renderers_.empty()
443 ? 0
444 : fake_video_renderers_.begin()->second->width();
445 }
446
447 int rendered_height() {
448 EXPECT_FALSE(fake_video_renderers_.empty());
449 return fake_video_renderers_.empty()
450 ? 0
451 : fake_video_renderers_.begin()->second->height();
452 }
453
454 double rendered_aspect_ratio() {
455 if (rendered_height() == 0) {
456 return 0.0;
457 }
458 return static_cast<double>(rendered_width()) / rendered_height();
459 }
460
461 webrtc::VideoRotation rendered_rotation() {
462 EXPECT_FALSE(fake_video_renderers_.empty());
463 return fake_video_renderers_.empty()
464 ? webrtc::kVideoRotation_0
465 : fake_video_renderers_.begin()->second->rotation();
466 }
467
468 int local_rendered_width() {
469 return local_video_renderer_ ? local_video_renderer_->width() : 0;
470 }
471
472 int local_rendered_height() {
473 return local_video_renderer_ ? local_video_renderer_->height() : 0;
474 }
475
476 double local_rendered_aspect_ratio() {
477 if (local_rendered_height() == 0) {
478 return 0.0;
479 }
480 return static_cast<double>(local_rendered_width()) /
481 local_rendered_height();
482 }
483
484 size_t number_of_remote_streams() {
485 if (!pc()) {
486 return 0;
487 }
488 return pc()->remote_streams()->count();
489 }
490
491 StreamCollectionInterface* remote_streams() const {
492 if (!pc()) {
493 ADD_FAILURE();
494 return nullptr;
495 }
496 return pc()->remote_streams();
497 }
498
499 StreamCollectionInterface* local_streams() {
500 if (!pc()) {
501 ADD_FAILURE();
502 return nullptr;
503 }
504 return pc()->local_streams();
505 }
506
507 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
508 return pc()->signaling_state();
509 }
510
511 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
512 return pc()->ice_connection_state();
513 }
514
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100515 webrtc::PeerConnectionInterface::IceConnectionState
516 standardized_ice_connection_state() {
517 return pc()->standardized_ice_connection_state();
518 }
519
deadbeef1dcb1642017-03-29 21:08:16 -0700520 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
521 return pc()->ice_gathering_state();
522 }
523
524 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
525 // GetReceivers. They're updated automatically when a remote offer/answer
526 // from the fake signaling channel is applied, or when
527 // ResetRtpReceiverObservers below is called.
528 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
529 rtp_receiver_observers() {
530 return rtp_receiver_observers_;
531 }
532
533 void ResetRtpReceiverObservers() {
534 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100535 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
536 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700537 std::unique_ptr<MockRtpReceiverObserver> observer(
538 new MockRtpReceiverObserver(receiver->media_type()));
539 receiver->SetObserver(observer.get());
540 rtp_receiver_observers_.push_back(std::move(observer));
541 }
542 }
543
Steve Antonede9ca52017-10-16 13:04:27 -0700544 rtc::FakeNetworkManager* network() const {
545 return fake_network_manager_.get();
546 }
547 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
548
Qingsi Wang7685e862018-06-11 20:15:46 -0700549 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
550 return event_log_factory_;
551 }
552
deadbeef1dcb1642017-03-29 21:08:16 -0700553 private:
554 explicit PeerConnectionWrapper(const std::string& debug_name)
555 : debug_name_(debug_name) {}
556
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800557 bool Init(
558 const PeerConnectionFactory::Options* options,
559 const PeerConnectionInterface::RTCConfiguration* config,
560 webrtc::PeerConnectionDependencies dependencies,
561 rtc::Thread* network_thread,
562 rtc::Thread* worker_thread,
563 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
564 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700565 // There's an error in this test code if Init ends up being called twice.
566 RTC_DCHECK(!peer_connection_);
567 RTC_DCHECK(!peer_connection_factory_);
568
569 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700570 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700571
572 std::unique_ptr<cricket::PortAllocator> port_allocator(
573 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700574 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700575 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
576 if (!fake_audio_capture_module_) {
577 return false;
578 }
deadbeef1dcb1642017-03-29 21:08:16 -0700579 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700580
581 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
582 pc_factory_dependencies.network_thread = network_thread;
583 pc_factory_dependencies.worker_thread = worker_thread;
584 pc_factory_dependencies.signaling_thread = signaling_thread;
585 pc_factory_dependencies.media_engine =
586 cricket::WebRtcMediaEngineFactory::Create(
587 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
588 fake_audio_capture_module_),
589 webrtc::CreateBuiltinAudioEncoderFactory(),
590 webrtc::CreateBuiltinAudioDecoderFactory(),
591 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000592 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700593 webrtc::AudioProcessingBuilder().Create());
594 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
595 if (event_log_factory) {
596 event_log_factory_ = event_log_factory.get();
597 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
598 } else {
599 pc_factory_dependencies.event_log_factory =
600 webrtc::CreateRtcEventLogFactory();
601 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800602 if (media_transport_factory) {
603 pc_factory_dependencies.media_transport_factory =
604 std::move(media_transport_factory);
605 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700606 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
607 std::move(pc_factory_dependencies));
608
deadbeef1dcb1642017-03-29 21:08:16 -0700609 if (!peer_connection_factory_) {
610 return false;
611 }
612 if (options) {
613 peer_connection_factory_->SetOptions(*options);
614 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800615 if (config) {
616 sdp_semantics_ = config->sdp_semantics;
617 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700618
619 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200620 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700621 return peer_connection_.get() != nullptr;
622 }
623
624 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700625 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700626 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700627 PeerConnectionInterface::RTCConfiguration modified_config;
628 // If |config| is null, this will result in a default configuration being
629 // used.
630 if (config) {
631 modified_config = *config;
632 }
633 // Disable resolution adaptation; we don't want it interfering with the
634 // test results.
635 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
636 // ratios and not specific resolutions, is this even necessary?
637 modified_config.set_cpu_adaptation(false);
638
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700639 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700640 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700641 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700642 }
643
644 void set_signaling_message_receiver(
645 SignalingMessageReceiver* signaling_message_receiver) {
646 signaling_message_receiver_ = signaling_message_receiver;
647 }
648
649 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
650
Steve Antonede9ca52017-10-16 13:04:27 -0700651 void set_signal_ice_candidates(bool signal) {
652 signal_ice_candidates_ = signal;
653 }
654
deadbeef1dcb1642017-03-29 21:08:16 -0700655 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200656 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700657 // Set max frame rate to 10fps to reduce the risk of test flakiness.
658 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200659 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700660
Niels Möller5c7efe72018-05-11 10:34:46 +0200661 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200662 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
663 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700664 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200665 peer_connection_factory_->CreateVideoTrack(
666 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700667 if (!local_video_renderer_) {
668 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
669 }
670 return track;
671 }
672
673 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100674 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800675 std::unique_ptr<SessionDescriptionInterface> desc =
676 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700677 if (received_sdp_munger_) {
678 received_sdp_munger_(desc->description());
679 }
680
681 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
682 // Setting a remote description may have changed the number of receivers,
683 // so reset the receiver observers.
684 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800685 if (remote_offer_handler_) {
686 remote_offer_handler_();
687 }
deadbeef1dcb1642017-03-29 21:08:16 -0700688 auto answer = CreateAnswer();
689 ASSERT_NE(nullptr, answer);
690 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
691 }
692
693 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100694 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800695 std::unique_ptr<SessionDescriptionInterface> desc =
696 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700697 if (received_sdp_munger_) {
698 received_sdp_munger_(desc->description());
699 }
700
701 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
702 // Set the RtpReceiverObserver after receivers are created.
703 ResetRtpReceiverObservers();
704 }
705
706 // Returns null on failure.
707 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
708 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
709 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
710 pc()->CreateOffer(observer, offer_answer_options_);
711 return WaitForDescriptionFromObserver(observer);
712 }
713
714 // Returns null on failure.
715 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
716 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
717 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
718 pc()->CreateAnswer(observer, offer_answer_options_);
719 return WaitForDescriptionFromObserver(observer);
720 }
721
722 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100723 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700724 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
725 if (!observer->result()) {
726 return nullptr;
727 }
728 auto description = observer->MoveDescription();
729 if (generated_sdp_munger_) {
730 generated_sdp_munger_(description->description());
731 }
732 return description;
733 }
734
735 // Setting the local description and sending the SDP message over the fake
736 // signaling channel are combined into the same method because the SDP
737 // message needs to be sent as soon as SetLocalDescription finishes, without
738 // waiting for the observer to be called. This ensures that ICE candidates
739 // don't outrace the description.
740 bool SetLocalDescriptionAndSendSdpMessage(
741 std::unique_ptr<SessionDescriptionInterface> desc) {
742 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
743 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100744 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800745 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700746 std::string sdp;
747 EXPECT_TRUE(desc->ToString(&sdp));
748 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800749 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
750 RemoveUnusedVideoRenderers();
751 }
deadbeef1dcb1642017-03-29 21:08:16 -0700752 // As mentioned above, we need to send the message immediately after
753 // SetLocalDescription.
754 SendSdpMessage(type, sdp);
755 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
756 return true;
757 }
758
759 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
760 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
761 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100762 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700763 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
765 RemoveUnusedVideoRenderers();
766 }
deadbeef1dcb1642017-03-29 21:08:16 -0700767 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
768 return observer->result();
769 }
770
Seth Hampson2f0d7022018-02-20 11:54:42 -0800771 // This is a work around to remove unused fake_video_renderers from
772 // transceivers that have either stopped or are no longer receiving.
773 void RemoveUnusedVideoRenderers() {
774 auto transceivers = pc()->GetTransceivers();
775 for (auto& transceiver : transceivers) {
776 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
777 continue;
778 }
779 // Remove fake video renderers from any stopped transceivers.
780 if (transceiver->stopped()) {
781 auto it =
782 fake_video_renderers_.find(transceiver->receiver()->track()->id());
783 if (it != fake_video_renderers_.end()) {
784 fake_video_renderers_.erase(it);
785 }
786 }
787 // Remove fake video renderers from any transceivers that are no longer
788 // receiving.
789 if ((transceiver->current_direction() &&
790 !webrtc::RtpTransceiverDirectionHasRecv(
791 *transceiver->current_direction()))) {
792 auto it =
793 fake_video_renderers_.find(transceiver->receiver()->track()->id());
794 if (it != fake_video_renderers_.end()) {
795 fake_video_renderers_.erase(it);
796 }
797 }
798 }
799 }
800
deadbeef1dcb1642017-03-29 21:08:16 -0700801 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
802 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800803 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700804 if (signaling_delay_ms_ == 0) {
805 RelaySdpMessageIfReceiverExists(type, msg);
806 } else {
807 invoker_.AsyncInvokeDelayed<void>(
808 RTC_FROM_HERE, rtc::Thread::Current(),
809 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
810 this, type, msg),
811 signaling_delay_ms_);
812 }
813 }
814
Steve Antona3a92c22017-12-07 10:27:41 -0800815 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700816 if (signaling_message_receiver_) {
817 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
818 }
819 }
820
821 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
822 // default).
823 void SendIceMessage(const std::string& sdp_mid,
824 int sdp_mline_index,
825 const std::string& msg) {
826 if (signaling_delay_ms_ == 0) {
827 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
828 } else {
829 invoker_.AsyncInvokeDelayed<void>(
830 RTC_FROM_HERE, rtc::Thread::Current(),
831 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
832 this, sdp_mid, sdp_mline_index, msg),
833 signaling_delay_ms_);
834 }
835 }
836
837 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
838 int sdp_mline_index,
839 const std::string& msg) {
840 if (signaling_message_receiver_) {
841 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
842 msg);
843 }
844 }
845
846 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800847 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
848 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700849 HandleIncomingOffer(msg);
850 } else {
851 HandleIncomingAnswer(msg);
852 }
853 }
854
855 void ReceiveIceMessage(const std::string& sdp_mid,
856 int sdp_mline_index,
857 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100858 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700859 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
860 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
861 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
862 }
863
864 // PeerConnectionObserver callbacks.
865 void OnSignalingChange(
866 webrtc::PeerConnectionInterface::SignalingState new_state) override {
867 EXPECT_EQ(pc()->signaling_state(), new_state);
868 }
Steve Anton15324772018-01-16 10:26:49 -0800869 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
870 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
871 streams) override {
872 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
873 rtc::scoped_refptr<VideoTrackInterface> video_track(
874 static_cast<VideoTrackInterface*>(receiver->track().get()));
875 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700876 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800877 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200878 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700879 }
880 }
Steve Anton15324772018-01-16 10:26:49 -0800881 void OnRemoveTrack(
882 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
883 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
884 auto it = fake_video_renderers_.find(receiver->track()->id());
885 RTC_DCHECK(it != fake_video_renderers_.end());
886 fake_video_renderers_.erase(it);
887 }
888 }
deadbeef1dcb1642017-03-29 21:08:16 -0700889 void OnRenegotiationNeeded() override {}
890 void OnIceConnectionChange(
891 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
892 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700893 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700894 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200895 void OnConnectionChange(
896 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
897 peer_connection_state_history_.push_back(new_state);
898 }
899
deadbeef1dcb1642017-03-29 21:08:16 -0700900 void OnIceGatheringChange(
901 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700902 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700903 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 }
905 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700907
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800908 if (remote_async_resolver_) {
909 const auto& local_candidate = candidate->candidate();
910 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
911 if (local_candidate.address().IsUnresolvedIP()) {
912 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
913 rtc::SocketAddress resolved_addr(local_candidate.address());
914 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
915 local_candidate.address().hostname());
916 RTC_DCHECK(!resolved_ip.IsNil());
917 resolved_addr.SetResolvedIP(resolved_ip);
918 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
919 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
920 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700921 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700922 }
923
deadbeef1dcb1642017-03-29 21:08:16 -0700924 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800925 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700926 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700927 // Remote party may be deleted.
928 return;
929 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800930 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700931 }
932 void OnDataChannel(
933 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100934 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700935 data_channel_ = data_channel;
936 data_observer_.reset(new MockDataChannelObserver(data_channel));
937 }
938
deadbeef1dcb1642017-03-29 21:08:16 -0700939 std::string debug_name_;
940
941 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
942
943 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
944 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
945 peer_connection_factory_;
946
Steve Antonede9ca52017-10-16 13:04:27 -0700947 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700948 // Needed to keep track of number of frames sent.
949 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
950 // Needed to keep track of number of frames received.
951 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
952 fake_video_renderers_;
953 // Needed to ensure frames aren't received for removed tracks.
954 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
955 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700956
957 // For remote peer communication.
958 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
959 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700960 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700961
Niels Möller5c7efe72018-05-11 10:34:46 +0200962 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700963 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200964 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
965 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700966 // |local_video_renderer_| attached to the first created local video track.
967 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
968
Seth Hampson2f0d7022018-02-20 11:54:42 -0800969 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700970 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
971 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
972 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800973 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800974 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700975 rtc::scoped_refptr<DataChannelInterface> data_channel_;
976 std::unique_ptr<MockDataChannelObserver> data_observer_;
977
978 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
979
Steve Antonede9ca52017-10-16 13:04:27 -0700980 std::vector<PeerConnectionInterface::IceConnectionState>
981 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200982 std::vector<PeerConnectionInterface::PeerConnectionState>
983 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -0700984 std::vector<PeerConnectionInterface::IceGatheringState>
985 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700986
Qingsi Wang7685e862018-06-11 20:15:46 -0700987 webrtc::FakeRtcEventLogFactory* event_log_factory_;
988
deadbeef1dcb1642017-03-29 21:08:16 -0700989 rtc::AsyncInvoker invoker_;
990
Seth Hampson2f0d7022018-02-20 11:54:42 -0800991 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700992};
993
Elad Alon99c3fe52017-10-13 16:29:40 +0200994class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
995 public:
996 virtual ~MockRtcEventLogOutput() = default;
997 MOCK_CONST_METHOD0(IsActive, bool());
998 MOCK_METHOD1(Write, bool(const std::string&));
999};
1000
Seth Hampson2f0d7022018-02-20 11:54:42 -08001001// This helper object is used for both specifying how many audio/video frames
1002// are expected to be received for a caller/callee. It provides helper functions
1003// to specify these expectations. The object initially starts in a state of no
1004// expectations.
1005class MediaExpectations {
1006 public:
1007 enum ExpectFrames {
1008 kExpectSomeFrames,
1009 kExpectNoFrames,
1010 kNoExpectation,
1011 };
1012
1013 void ExpectBidirectionalAudioAndVideo() {
1014 ExpectBidirectionalAudio();
1015 ExpectBidirectionalVideo();
1016 }
1017
1018 void ExpectBidirectionalAudio() {
1019 CallerExpectsSomeAudio();
1020 CalleeExpectsSomeAudio();
1021 }
1022
1023 void ExpectNoAudio() {
1024 CallerExpectsNoAudio();
1025 CalleeExpectsNoAudio();
1026 }
1027
1028 void ExpectBidirectionalVideo() {
1029 CallerExpectsSomeVideo();
1030 CalleeExpectsSomeVideo();
1031 }
1032
1033 void ExpectNoVideo() {
1034 CallerExpectsNoVideo();
1035 CalleeExpectsNoVideo();
1036 }
1037
1038 void CallerExpectsSomeAudioAndVideo() {
1039 CallerExpectsSomeAudio();
1040 CallerExpectsSomeVideo();
1041 }
1042
1043 void CalleeExpectsSomeAudioAndVideo() {
1044 CalleeExpectsSomeAudio();
1045 CalleeExpectsSomeVideo();
1046 }
1047
1048 // Caller's audio functions.
1049 void CallerExpectsSomeAudio(
1050 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1051 caller_audio_expectation_ = kExpectSomeFrames;
1052 caller_audio_frames_expected_ = expected_audio_frames;
1053 }
1054
1055 void CallerExpectsNoAudio() {
1056 caller_audio_expectation_ = kExpectNoFrames;
1057 caller_audio_frames_expected_ = 0;
1058 }
1059
1060 // Caller's video functions.
1061 void CallerExpectsSomeVideo(
1062 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1063 caller_video_expectation_ = kExpectSomeFrames;
1064 caller_video_frames_expected_ = expected_video_frames;
1065 }
1066
1067 void CallerExpectsNoVideo() {
1068 caller_video_expectation_ = kExpectNoFrames;
1069 caller_video_frames_expected_ = 0;
1070 }
1071
1072 // Callee's audio functions.
1073 void CalleeExpectsSomeAudio(
1074 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1075 callee_audio_expectation_ = kExpectSomeFrames;
1076 callee_audio_frames_expected_ = expected_audio_frames;
1077 }
1078
1079 void CalleeExpectsNoAudio() {
1080 callee_audio_expectation_ = kExpectNoFrames;
1081 callee_audio_frames_expected_ = 0;
1082 }
1083
1084 // Callee's video functions.
1085 void CalleeExpectsSomeVideo(
1086 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1087 callee_video_expectation_ = kExpectSomeFrames;
1088 callee_video_frames_expected_ = expected_video_frames;
1089 }
1090
1091 void CalleeExpectsNoVideo() {
1092 callee_video_expectation_ = kExpectNoFrames;
1093 callee_video_frames_expected_ = 0;
1094 }
1095
1096 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1097 ExpectFrames caller_video_expectation_ = kNoExpectation;
1098 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1099 ExpectFrames callee_video_expectation_ = kNoExpectation;
1100 int caller_audio_frames_expected_ = 0;
1101 int caller_video_frames_expected_ = 0;
1102 int callee_audio_frames_expected_ = 0;
1103 int callee_video_frames_expected_ = 0;
1104};
1105
deadbeef1dcb1642017-03-29 21:08:16 -07001106// Tests two PeerConnections connecting to each other end-to-end, using a
1107// virtual network, fake A/V capture and fake encoder/decoders. The
1108// PeerConnections share the threads/socket servers, but use separate versions
1109// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001110class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001111 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001112 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1113 : sdp_semantics_(sdp_semantics),
1114 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001115 fss_(new rtc::FirewallSocketServer(ss_.get())),
1116 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001117 worker_thread_(rtc::Thread::Create()),
1118 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001119 network_thread_->SetName("PCNetworkThread", this);
1120 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001121 RTC_CHECK(network_thread_->Start());
1122 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001123 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001124 }
1125
Seth Hampson2f0d7022018-02-20 11:54:42 -08001126 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001127 // The PeerConnections should deleted before the TurnCustomizers.
1128 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1129 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1130 // that the TurnCustomizer outlives the life of the PeerConnection or else
1131 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001132 if (caller_) {
1133 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001134 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001135 }
1136 if (callee_) {
1137 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001138 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001139 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001140
1141 // If turn servers were created for the test they need to be destroyed on
1142 // the network thread.
1143 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1144 turn_servers_.clear();
1145 turn_customizers_.clear();
1146 });
deadbeef1dcb1642017-03-29 21:08:16 -07001147 }
1148
1149 bool SignalingStateStable() {
1150 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1151 }
1152
deadbeef71452802017-05-07 17:21:01 -07001153 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001154 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1155 // are connected. This is an important distinction. Once we have separate
1156 // ICE and DTLS state, this check needs to use the DTLS state.
1157 return (callee()->ice_connection_state() ==
1158 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1159 callee()->ice_connection_state() ==
1160 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1161 (caller()->ice_connection_state() ==
1162 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1163 caller()->ice_connection_state() ==
1164 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001165 }
1166
Qingsi Wang7685e862018-06-11 20:15:46 -07001167 // When |event_log_factory| is null, the default implementation of the event
1168 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001169 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1170 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001171 const PeerConnectionFactory::Options* options,
1172 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001173 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001174 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1175 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001176 RTCConfiguration modified_config;
1177 if (config) {
1178 modified_config = *config;
1179 }
Steve Anton3acffc32018-04-12 17:21:03 -07001180 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001181 if (!dependencies.cert_generator) {
1182 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001183 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001184 }
1185 std::unique_ptr<PeerConnectionWrapper> client(
1186 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001187
Niels Möllerf06f9232018-08-07 12:32:18 +02001188 if (!client->Init(options, &modified_config, std::move(dependencies),
1189 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001190 std::move(event_log_factory),
1191 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001192 return nullptr;
1193 }
1194 return client;
1195 }
1196
Qingsi Wang7685e862018-06-11 20:15:46 -07001197 std::unique_ptr<PeerConnectionWrapper>
1198 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1199 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001200 const PeerConnectionFactory::Options* options,
1201 const RTCConfiguration* config,
1202 webrtc::PeerConnectionDependencies dependencies) {
1203 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1204 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001205 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001206 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001207 std::move(event_log_factory),
1208 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001209 }
1210
deadbeef1dcb1642017-03-29 21:08:16 -07001211 bool CreatePeerConnectionWrappers() {
1212 return CreatePeerConnectionWrappersWithConfig(
1213 PeerConnectionInterface::RTCConfiguration(),
1214 PeerConnectionInterface::RTCConfiguration());
1215 }
1216
Steve Anton3acffc32018-04-12 17:21:03 -07001217 bool CreatePeerConnectionWrappersWithSdpSemantics(
1218 SdpSemantics caller_semantics,
1219 SdpSemantics callee_semantics) {
1220 // Can't specify the sdp_semantics in the passed-in configuration since it
1221 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1222 // stored in sdp_semantics_. So get around this by modifying the instance
1223 // variable before calling CreatePeerConnectionWrapper for the caller and
1224 // callee PeerConnections.
1225 SdpSemantics original_semantics = sdp_semantics_;
1226 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001227 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001228 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001229 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001230 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001231 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001232 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001233 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001234 sdp_semantics_ = original_semantics;
1235 return caller_ && callee_;
1236 }
1237
deadbeef1dcb1642017-03-29 21:08:16 -07001238 bool CreatePeerConnectionWrappersWithConfig(
1239 const PeerConnectionInterface::RTCConfiguration& caller_config,
1240 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001241 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001242 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001243 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1244 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001245 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001246 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001247 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1248 /*media_transport_factory=*/nullptr);
1249 return caller_ && callee_;
1250 }
1251
1252 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1253 const PeerConnectionInterface::RTCConfiguration& caller_config,
1254 const PeerConnectionInterface::RTCConfiguration& callee_config,
1255 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1256 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1257 caller_ =
1258 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1259 webrtc::PeerConnectionDependencies(nullptr),
1260 nullptr, std::move(caller_factory));
1261 callee_ =
1262 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1263 webrtc::PeerConnectionDependencies(nullptr),
1264 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001265 return caller_ && callee_;
1266 }
1267
1268 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1269 const PeerConnectionInterface::RTCConfiguration& caller_config,
1270 webrtc::PeerConnectionDependencies caller_dependencies,
1271 const PeerConnectionInterface::RTCConfiguration& callee_config,
1272 webrtc::PeerConnectionDependencies callee_dependencies) {
1273 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001274 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001275 std::move(caller_dependencies), nullptr,
1276 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001277 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001278 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001279 std::move(callee_dependencies), nullptr,
1280 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001281 return caller_ && callee_;
1282 }
1283
1284 bool CreatePeerConnectionWrappersWithOptions(
1285 const PeerConnectionFactory::Options& caller_options,
1286 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001287 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001288 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001289 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1290 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001291 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001292 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001293 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1294 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001295 return caller_ && callee_;
1296 }
1297
1298 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1299 PeerConnectionInterface::RTCConfiguration default_config;
1300 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001301 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001302 webrtc::PeerConnectionDependencies(nullptr));
1303 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001304 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001305 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001306 return caller_ && callee_;
1307 }
1308
Seth Hampson2f0d7022018-02-20 11:54:42 -08001309 std::unique_ptr<PeerConnectionWrapper>
1310 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001311 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1312 new FakeRTCCertificateGenerator());
1313 cert_generator->use_alternate_key();
1314
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001315 webrtc::PeerConnectionDependencies dependencies(nullptr);
1316 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001317 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001318 std::move(dependencies), nullptr,
1319 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001320 }
1321
Seth Hampsonaed71642018-06-11 07:41:32 -07001322 cricket::TestTurnServer* CreateTurnServer(
1323 rtc::SocketAddress internal_address,
1324 rtc::SocketAddress external_address,
1325 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1326 const std::string& common_name = "test turn server") {
1327 rtc::Thread* thread = network_thread();
1328 std::unique_ptr<cricket::TestTurnServer> turn_server =
1329 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1330 RTC_FROM_HERE,
1331 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001332 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001333 thread, internal_address, external_address, type,
1334 /*ignore_bad_certs=*/true, common_name);
1335 });
1336 turn_servers_.push_back(std::move(turn_server));
1337 // Interactions with the turn server should be done on the network thread.
1338 return turn_servers_.back().get();
1339 }
1340
1341 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1342 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1343 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1344 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001345 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001346 turn_customizers_.push_back(std::move(turn_customizer));
1347 // Interactions with the turn customizer should be done on the network
1348 // thread.
1349 return turn_customizers_.back().get();
1350 }
1351
1352 // Checks that the function counters for a TestTurnCustomizer are greater than
1353 // 0.
1354 void ExpectTurnCustomizerCountersIncremented(
1355 cricket::TestTurnCustomizer* turn_customizer) {
1356 unsigned int allow_channel_data_counter =
1357 network_thread()->Invoke<unsigned int>(
1358 RTC_FROM_HERE, [turn_customizer] {
1359 return turn_customizer->allow_channel_data_cnt_;
1360 });
1361 EXPECT_GT(allow_channel_data_counter, 0u);
1362 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1363 RTC_FROM_HERE,
1364 [turn_customizer] { return turn_customizer->modify_cnt_; });
1365 EXPECT_GT(modify_counter, 0u);
1366 }
1367
deadbeef1dcb1642017-03-29 21:08:16 -07001368 // Once called, SDP blobs and ICE candidates will be automatically signaled
1369 // between PeerConnections.
1370 void ConnectFakeSignaling() {
1371 caller_->set_signaling_message_receiver(callee_.get());
1372 callee_->set_signaling_message_receiver(caller_.get());
1373 }
1374
Steve Antonede9ca52017-10-16 13:04:27 -07001375 // Once called, SDP blobs will be automatically signaled between
1376 // PeerConnections. Note that ICE candidates will not be signaled unless they
1377 // are in the exchanged SDP blobs.
1378 void ConnectFakeSignalingForSdpOnly() {
1379 ConnectFakeSignaling();
1380 SetSignalIceCandidates(false);
1381 }
1382
deadbeef1dcb1642017-03-29 21:08:16 -07001383 void SetSignalingDelayMs(int delay_ms) {
1384 caller_->set_signaling_delay_ms(delay_ms);
1385 callee_->set_signaling_delay_ms(delay_ms);
1386 }
1387
Steve Antonede9ca52017-10-16 13:04:27 -07001388 void SetSignalIceCandidates(bool signal) {
1389 caller_->set_signal_ice_candidates(signal);
1390 callee_->set_signal_ice_candidates(signal);
1391 }
1392
deadbeef1dcb1642017-03-29 21:08:16 -07001393 // Messages may get lost on the unreliable DataChannel, so we send multiple
1394 // times to avoid test flakiness.
1395 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1396 const std::string& data,
1397 int retries) {
1398 for (int i = 0; i < retries; ++i) {
1399 dc->Send(DataBuffer(data));
1400 }
1401 }
1402
1403 rtc::Thread* network_thread() { return network_thread_.get(); }
1404
1405 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1406
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001407 webrtc::MediaTransportPair* loopback_media_transports() {
1408 return &loopback_media_transports_;
1409 }
1410
deadbeef1dcb1642017-03-29 21:08:16 -07001411 PeerConnectionWrapper* caller() { return caller_.get(); }
1412
1413 // Set the |caller_| to the |wrapper| passed in and return the
1414 // original |caller_|.
1415 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1416 PeerConnectionWrapper* wrapper) {
1417 PeerConnectionWrapper* old = caller_.release();
1418 caller_.reset(wrapper);
1419 return old;
1420 }
1421
1422 PeerConnectionWrapper* callee() { return callee_.get(); }
1423
1424 // Set the |callee_| to the |wrapper| passed in and return the
1425 // original |callee_|.
1426 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1427 PeerConnectionWrapper* wrapper) {
1428 PeerConnectionWrapper* old = callee_.release();
1429 callee_.reset(wrapper);
1430 return old;
1431 }
1432
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001433 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1434 network_thread()->Invoke<void>(
1435 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1436 caller()->port_allocator(), caller_flags));
1437 network_thread()->Invoke<void>(
1438 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1439 callee()->port_allocator(), callee_flags));
1440 }
1441
Steve Antonede9ca52017-10-16 13:04:27 -07001442 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1443
Seth Hampson2f0d7022018-02-20 11:54:42 -08001444 // Expects the provided number of new frames to be received within
1445 // kMaxWaitForFramesMs. The new expected frames are specified in
1446 // |media_expectations|. Returns false if any of the expectations were
1447 // not met.
1448 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1449 // First initialize the expected frame counts based upon the current
1450 // frame count.
1451 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1452 if (media_expectations.caller_audio_expectation_ ==
1453 MediaExpectations::kExpectSomeFrames) {
1454 total_caller_audio_frames_expected +=
1455 media_expectations.caller_audio_frames_expected_;
1456 }
1457 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001458 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001459 if (media_expectations.caller_video_expectation_ ==
1460 MediaExpectations::kExpectSomeFrames) {
1461 total_caller_video_frames_expected +=
1462 media_expectations.caller_video_frames_expected_;
1463 }
1464 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1465 if (media_expectations.callee_audio_expectation_ ==
1466 MediaExpectations::kExpectSomeFrames) {
1467 total_callee_audio_frames_expected +=
1468 media_expectations.callee_audio_frames_expected_;
1469 }
1470 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001471 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001472 if (media_expectations.callee_video_expectation_ ==
1473 MediaExpectations::kExpectSomeFrames) {
1474 total_callee_video_frames_expected +=
1475 media_expectations.callee_video_frames_expected_;
1476 }
deadbeef1dcb1642017-03-29 21:08:16 -07001477
Seth Hampson2f0d7022018-02-20 11:54:42 -08001478 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001479 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001480 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001481 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001482 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001483 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001484 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001485 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486 total_callee_video_frames_expected,
1487 kMaxWaitForFramesMs);
1488 bool expectations_correct =
1489 caller()->audio_frames_received() >=
1490 total_caller_audio_frames_expected &&
1491 caller()->min_video_frames_received_per_track() >=
1492 total_caller_video_frames_expected &&
1493 callee()->audio_frames_received() >=
1494 total_callee_audio_frames_expected &&
1495 callee()->min_video_frames_received_per_track() >=
1496 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001497
Seth Hampson2f0d7022018-02-20 11:54:42 -08001498 // After the combined wait, print out a more detailed message upon
1499 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001500 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001502 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001503 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001504 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001505 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001506 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 total_callee_video_frames_expected);
1508
1509 // We want to make sure nothing unexpected was received.
1510 if (media_expectations.caller_audio_expectation_ ==
1511 MediaExpectations::kExpectNoFrames) {
1512 EXPECT_EQ(caller()->audio_frames_received(),
1513 total_caller_audio_frames_expected);
1514 if (caller()->audio_frames_received() !=
1515 total_caller_audio_frames_expected) {
1516 expectations_correct = false;
1517 }
1518 }
1519 if (media_expectations.caller_video_expectation_ ==
1520 MediaExpectations::kExpectNoFrames) {
1521 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1522 total_caller_video_frames_expected);
1523 if (caller()->min_video_frames_received_per_track() !=
1524 total_caller_video_frames_expected) {
1525 expectations_correct = false;
1526 }
1527 }
1528 if (media_expectations.callee_audio_expectation_ ==
1529 MediaExpectations::kExpectNoFrames) {
1530 EXPECT_EQ(callee()->audio_frames_received(),
1531 total_callee_audio_frames_expected);
1532 if (callee()->audio_frames_received() !=
1533 total_callee_audio_frames_expected) {
1534 expectations_correct = false;
1535 }
1536 }
1537 if (media_expectations.callee_video_expectation_ ==
1538 MediaExpectations::kExpectNoFrames) {
1539 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1540 total_callee_video_frames_expected);
1541 if (callee()->min_video_frames_received_per_track() !=
1542 total_callee_video_frames_expected) {
1543 expectations_correct = false;
1544 }
1545 }
1546 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001547 }
1548
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001549 void TestNegotiatedCipherSuite(
1550 const PeerConnectionFactory::Options& caller_options,
1551 const PeerConnectionFactory::Options& callee_options,
1552 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001553 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1554 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001555 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001556 caller()->AddAudioVideoTracks();
1557 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001558 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001559 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001560 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001561 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001562 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001563 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001564 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1565 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001566 }
1567
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001568 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1569 bool remote_gcm_enabled,
1570 int expected_cipher_suite) {
1571 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001572 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1573 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001574 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001575 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1576 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001577 TestNegotiatedCipherSuite(caller_options, callee_options,
1578 expected_cipher_suite);
1579 }
1580
Seth Hampson2f0d7022018-02-20 11:54:42 -08001581 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001582 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001583
deadbeef1dcb1642017-03-29 21:08:16 -07001584 private:
1585 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001586 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001587 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001588 // |network_thread_| and |worker_thread_| are used by both
1589 // |caller_| and |callee_| so they must be destroyed
1590 // later.
1591 std::unique_ptr<rtc::Thread> network_thread_;
1592 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001593 // The turn servers and turn customizers should be accessed & deleted on the
1594 // network thread to avoid a race with the socket read/write that occurs
1595 // on the network thread.
1596 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1597 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001598 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001599 std::unique_ptr<PeerConnectionWrapper> caller_;
1600 std::unique_ptr<PeerConnectionWrapper> callee_;
1601};
1602
Seth Hampson2f0d7022018-02-20 11:54:42 -08001603class PeerConnectionIntegrationTest
1604 : public PeerConnectionIntegrationBaseTest,
1605 public ::testing::WithParamInterface<SdpSemantics> {
1606 protected:
1607 PeerConnectionIntegrationTest()
1608 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1609};
1610
1611class PeerConnectionIntegrationTestPlanB
1612 : public PeerConnectionIntegrationBaseTest {
1613 protected:
1614 PeerConnectionIntegrationTestPlanB()
1615 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1616};
1617
1618class PeerConnectionIntegrationTestUnifiedPlan
1619 : public PeerConnectionIntegrationBaseTest {
1620 protected:
1621 PeerConnectionIntegrationTestUnifiedPlan()
1622 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1623};
1624
deadbeef1dcb1642017-03-29 21:08:16 -07001625// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1626// includes testing that the callback is invoked if an observer is connected
1627// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001628TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001629 RtpReceiverObserverOnFirstPacketReceived) {
1630 ASSERT_TRUE(CreatePeerConnectionWrappers());
1631 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001632 caller()->AddAudioVideoTracks();
1633 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001634 // Start offer/answer exchange and wait for it to complete.
1635 caller()->CreateAndSetAndSignalOffer();
1636 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1637 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001638 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1639 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001640 // Wait for all "first packet received" callbacks to be fired.
1641 EXPECT_TRUE_WAIT(
1642 std::all_of(caller()->rtp_receiver_observers().begin(),
1643 caller()->rtp_receiver_observers().end(),
1644 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1645 return o->first_packet_received();
1646 }),
1647 kMaxWaitForFramesMs);
1648 EXPECT_TRUE_WAIT(
1649 std::all_of(callee()->rtp_receiver_observers().begin(),
1650 callee()->rtp_receiver_observers().end(),
1651 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1652 return o->first_packet_received();
1653 }),
1654 kMaxWaitForFramesMs);
1655 // If new observers are set after the first packet was already received, the
1656 // callback should still be invoked.
1657 caller()->ResetRtpReceiverObservers();
1658 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001659 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1660 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001661 EXPECT_TRUE(
1662 std::all_of(caller()->rtp_receiver_observers().begin(),
1663 caller()->rtp_receiver_observers().end(),
1664 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1665 return o->first_packet_received();
1666 }));
1667 EXPECT_TRUE(
1668 std::all_of(callee()->rtp_receiver_observers().begin(),
1669 callee()->rtp_receiver_observers().end(),
1670 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1671 return o->first_packet_received();
1672 }));
1673}
1674
1675class DummyDtmfObserver : public DtmfSenderObserverInterface {
1676 public:
1677 DummyDtmfObserver() : completed_(false) {}
1678
1679 // Implements DtmfSenderObserverInterface.
1680 void OnToneChange(const std::string& tone) override {
1681 tones_.push_back(tone);
1682 if (tone.empty()) {
1683 completed_ = true;
1684 }
1685 }
1686
1687 const std::vector<std::string>& tones() const { return tones_; }
1688 bool completed() const { return completed_; }
1689
1690 private:
1691 bool completed_;
1692 std::vector<std::string> tones_;
1693};
1694
1695// Assumes |sender| already has an audio track added and the offer/answer
1696// exchange is done.
1697void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1698 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001699 // We should be able to get a DTMF sender from the local sender.
1700 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1701 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1702 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001703 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001704 dtmf_sender->RegisterObserver(&observer);
1705
1706 // Test the DtmfSender object just created.
1707 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1708 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1709
1710 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1711 std::vector<std::string> tones = {"1", "a", ""};
1712 EXPECT_EQ(tones, observer.tones());
1713 dtmf_sender->UnregisterObserver();
1714 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1715}
1716
1717// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1718// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001719TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001720 ASSERT_TRUE(CreatePeerConnectionWrappers());
1721 ConnectFakeSignaling();
1722 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001723 caller()->AddAudioTrack();
1724 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001725 caller()->CreateAndSetAndSignalOffer();
1726 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001727 // DTLS must finish before the DTMF sender can be used reliably.
1728 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001729 TestDtmfFromSenderToReceiver(caller(), callee());
1730 TestDtmfFromSenderToReceiver(callee(), caller());
1731}
1732
1733// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1734// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001735TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001736 ASSERT_TRUE(CreatePeerConnectionWrappers());
1737 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001738
deadbeef1dcb1642017-03-29 21:08:16 -07001739 // Do normal offer/answer and wait for some frames to be received in each
1740 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001741 caller()->AddAudioVideoTracks();
1742 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001743 caller()->CreateAndSetAndSignalOffer();
1744 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001745 MediaExpectations media_expectations;
1746 media_expectations.ExpectBidirectionalAudioAndVideo();
1747 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001748 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1749 webrtc::kEnumCounterKeyProtocolDtls));
1750 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1751 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001752}
1753
1754// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001755TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001756 PeerConnectionInterface::RTCConfiguration sdes_config;
1757 sdes_config.enable_dtls_srtp.emplace(false);
1758 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1759 ConnectFakeSignaling();
1760
1761 // Do normal offer/answer and wait for some frames to be received in each
1762 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001763 caller()->AddAudioVideoTracks();
1764 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001765 caller()->CreateAndSetAndSignalOffer();
1766 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001767 MediaExpectations media_expectations;
1768 media_expectations.ExpectBidirectionalAudioAndVideo();
1769 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001770 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1771 webrtc::kEnumCounterKeyProtocolSdes));
1772 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1773 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001774}
1775
Steve Anton8c0f7a72017-10-03 10:03:10 -07001776// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1777// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001779 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1780 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1781 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1782 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1783 return pc->GetRemoteAudioSSLCertificate();
1784 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001785 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1786 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1787 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1788 return pc->GetRemoteAudioSSLCertChain();
1789 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001790
1791 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1792 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1793
1794 // Configure each side with a known certificate so they can be compared later.
1795 PeerConnectionInterface::RTCConfiguration caller_config;
1796 caller_config.enable_dtls_srtp.emplace(true);
1797 caller_config.certificates.push_back(caller_cert);
1798 PeerConnectionInterface::RTCConfiguration callee_config;
1799 callee_config.enable_dtls_srtp.emplace(true);
1800 callee_config.certificates.push_back(callee_cert);
1801 ASSERT_TRUE(
1802 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1803 ConnectFakeSignaling();
1804
1805 // When first initialized, there should not be a remote SSL certificate (and
1806 // calling this method should not crash).
1807 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1808 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001809 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1810 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001811
Steve Anton15324772018-01-16 10:26:49 -08001812 caller()->AddAudioTrack();
1813 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001814 caller()->CreateAndSetAndSignalOffer();
1815 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1816 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1817
1818 // Once DTLS has been connected, each side should return the other's SSL
1819 // certificate when calling GetRemoteAudioSSLCertificate.
1820
1821 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1822 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001823 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001824 caller_remote_cert->ToPEMString());
1825
1826 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1827 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001828 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001829 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001830
1831 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1832 ASSERT_TRUE(caller_remote_cert_chain);
1833 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1834 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001835 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001836 remote_cert->ToPEMString());
1837
1838 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1839 ASSERT_TRUE(callee_remote_cert_chain);
1840 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1841 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001842 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001843 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001844}
1845
deadbeef1dcb1642017-03-29 21:08:16 -07001846// This test sets up a call between two parties with a source resolution of
1847// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001848TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001849 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1850 ASSERT_TRUE(CreatePeerConnectionWrappers());
1851 ConnectFakeSignaling();
1852
Niels Möller5c7efe72018-05-11 10:34:46 +02001853 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1854 webrtc::FakePeriodicVideoSource::Config config;
1855 config.width = 1280;
1856 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001857 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001858 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1859 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001860
1861 // Do normal offer/answer and wait for at least one frame to be received in
1862 // each direction.
1863 caller()->CreateAndSetAndSignalOffer();
1864 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1865 callee()->min_video_frames_received_per_track() > 0,
1866 kMaxWaitForFramesMs);
1867
1868 // Check rendered aspect ratio.
1869 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1870 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1871 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1872 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1873}
1874
1875// This test sets up an one-way call, with media only from caller to
1876// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001877TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001878 ASSERT_TRUE(CreatePeerConnectionWrappers());
1879 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001880 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001881 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001882 MediaExpectations media_expectations;
1883 media_expectations.CalleeExpectsSomeAudioAndVideo();
1884 media_expectations.CallerExpectsNoAudio();
1885 media_expectations.CallerExpectsNoVideo();
1886 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001887}
1888
1889// This test sets up a audio call initially, with the callee rejecting video
1890// initially. Then later the callee decides to upgrade to audio/video, and
1891// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001892TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001893 ASSERT_TRUE(CreatePeerConnectionWrappers());
1894 ConnectFakeSignaling();
1895 // Initially, offer an audio/video stream from the caller, but refuse to
1896 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001897 caller()->AddAudioVideoTracks();
1898 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001899 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1900 PeerConnectionInterface::RTCOfferAnswerOptions options;
1901 options.offer_to_receive_video = 0;
1902 callee()->SetOfferAnswerOptions(options);
1903 } else {
1904 callee()->SetRemoteOfferHandler([this] {
1905 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1906 });
1907 }
deadbeef1dcb1642017-03-29 21:08:16 -07001908 // Do offer/answer and make sure audio is still received end-to-end.
1909 caller()->CreateAndSetAndSignalOffer();
1910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001911 {
1912 MediaExpectations media_expectations;
1913 media_expectations.ExpectBidirectionalAudio();
1914 media_expectations.ExpectNoVideo();
1915 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1916 }
deadbeef1dcb1642017-03-29 21:08:16 -07001917 // Sanity check that the callee's description has a rejected video section.
1918 ASSERT_NE(nullptr, callee()->pc()->local_description());
1919 const ContentInfo* callee_video_content =
1920 GetFirstVideoContent(callee()->pc()->local_description()->description());
1921 ASSERT_NE(nullptr, callee_video_content);
1922 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923
deadbeef1dcb1642017-03-29 21:08:16 -07001924 // Now negotiate with video and ensure negotiation succeeds, with video
1925 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001926 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001927 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1928 PeerConnectionInterface::RTCOfferAnswerOptions options;
1929 options.offer_to_receive_video = 1;
1930 callee()->SetOfferAnswerOptions(options);
1931 } else {
1932 callee()->SetRemoteOfferHandler(nullptr);
1933 caller()->SetRemoteOfferHandler([this] {
1934 // The caller creates a new transceiver to receive video on when receiving
1935 // the offer, but by default it is send only.
1936 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001937 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001938 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1939 transceivers[2]->receiver()->media_type());
1940 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1941 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1942 });
1943 }
deadbeef1dcb1642017-03-29 21:08:16 -07001944 callee()->CreateAndSetAndSignalOffer();
1945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001946 {
1947 // Expect additional audio frames to be received after the upgrade.
1948 MediaExpectations media_expectations;
1949 media_expectations.ExpectBidirectionalAudioAndVideo();
1950 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1951 }
deadbeef1dcb1642017-03-29 21:08:16 -07001952}
1953
deadbeef4389b4d2017-09-07 09:07:36 -07001954// Simpler than the above test; just add an audio track to an established
1955// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001956TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001957 ASSERT_TRUE(CreatePeerConnectionWrappers());
1958 ConnectFakeSignaling();
1959 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001960 caller()->AddVideoTrack();
1961 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001962 caller()->CreateAndSetAndSignalOffer();
1963 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1964 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001965 caller()->AddAudioTrack();
1966 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001967 caller()->CreateAndSetAndSignalOffer();
1968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1969 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001970 MediaExpectations media_expectations;
1971 media_expectations.ExpectBidirectionalAudioAndVideo();
1972 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001973}
1974
deadbeef1dcb1642017-03-29 21:08:16 -07001975// This test sets up a call that's transferred to a new caller with a different
1976// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001977TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001978 ASSERT_TRUE(CreatePeerConnectionWrappers());
1979 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001980 caller()->AddAudioVideoTracks();
1981 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001982 caller()->CreateAndSetAndSignalOffer();
1983 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1984
1985 // Keep the original peer around which will still send packets to the
1986 // receiving client. These SRTP packets will be dropped.
1987 std::unique_ptr<PeerConnectionWrapper> original_peer(
1988 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001989 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001990 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1991 // directly above.
1992 original_peer->pc()->Close();
1993
1994 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001995 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001996 caller()->CreateAndSetAndSignalOffer();
1997 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1998 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001999 MediaExpectations media_expectations;
2000 media_expectations.ExpectBidirectionalAudioAndVideo();
2001 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002002}
2003
2004// This test sets up a call that's transferred to a new callee with a different
2005// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002006TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002007 ASSERT_TRUE(CreatePeerConnectionWrappers());
2008 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002009 caller()->AddAudioVideoTracks();
2010 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002011 caller()->CreateAndSetAndSignalOffer();
2012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2013
2014 // Keep the original peer around which will still send packets to the
2015 // receiving client. These SRTP packets will be dropped.
2016 std::unique_ptr<PeerConnectionWrapper> original_peer(
2017 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002018 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002019 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2020 // directly above.
2021 original_peer->pc()->Close();
2022
2023 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002024 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002025 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2026 caller()->CreateAndSetAndSignalOffer();
2027 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2028 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002029 MediaExpectations media_expectations;
2030 media_expectations.ExpectBidirectionalAudioAndVideo();
2031 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002032}
2033
2034// This test sets up a non-bundled call and negotiates bundling at the same
2035// time as starting an ICE restart. When bundling is in effect in the restart,
2036// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002037TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002038 ASSERT_TRUE(CreatePeerConnectionWrappers());
2039 ConnectFakeSignaling();
2040
Steve Anton15324772018-01-16 10:26:49 -08002041 caller()->AddAudioVideoTracks();
2042 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002043 // Remove the bundle group from the SDP received by the callee.
2044 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2045 desc->RemoveGroupByName("BUNDLE");
2046 });
2047 caller()->CreateAndSetAndSignalOffer();
2048 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002049 {
2050 MediaExpectations media_expectations;
2051 media_expectations.ExpectBidirectionalAudioAndVideo();
2052 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2053 }
deadbeef1dcb1642017-03-29 21:08:16 -07002054 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2055 callee()->SetReceivedSdpMunger(nullptr);
2056 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2057 caller()->CreateAndSetAndSignalOffer();
2058 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2059
2060 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002061 {
2062 MediaExpectations media_expectations;
2063 media_expectations.ExpectBidirectionalAudioAndVideo();
2064 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2065 }
deadbeef1dcb1642017-03-29 21:08:16 -07002066}
2067
2068// Test CVO (Coordination of Video Orientation). If a video source is rotated
2069// and both peers support the CVO RTP header extension, the actual video frames
2070// don't need to be encoded in different resolutions, since the rotation is
2071// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002072TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002073 ASSERT_TRUE(CreatePeerConnectionWrappers());
2074 ConnectFakeSignaling();
2075 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002076 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002077 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002078 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002079 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2080
2081 // Wait for video frames to be received by both sides.
2082 caller()->CreateAndSetAndSignalOffer();
2083 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2084 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2085 callee()->min_video_frames_received_per_track() > 0,
2086 kMaxWaitForFramesMs);
2087
2088 // Ensure that the aspect ratio is unmodified.
2089 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2090 // not just assumed.
2091 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2092 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2093 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2094 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2095 // Ensure that the CVO bits were surfaced to the renderer.
2096 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2097 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2098}
2099
2100// Test that when the CVO extension isn't supported, video is rotated the
2101// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002102TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002103 ASSERT_TRUE(CreatePeerConnectionWrappers());
2104 ConnectFakeSignaling();
2105 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002106 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002107 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002108 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002109 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2110
2111 // Remove the CVO extension from the offered SDP.
2112 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2113 cricket::VideoContentDescription* video =
2114 GetFirstVideoContentDescription(desc);
2115 video->ClearRtpHeaderExtensions();
2116 });
2117 // Wait for video frames to be received by both sides.
2118 caller()->CreateAndSetAndSignalOffer();
2119 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2120 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2121 callee()->min_video_frames_received_per_track() > 0,
2122 kMaxWaitForFramesMs);
2123
2124 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2125 // rotation.
2126 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2127 // not just assumed.
2128 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2129 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2130 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2131 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2132 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2133 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2134 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2135}
2136
deadbeef1dcb1642017-03-29 21:08:16 -07002137// Test that if the answerer rejects the audio m= section, no audio is sent or
2138// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002139TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002140 ASSERT_TRUE(CreatePeerConnectionWrappers());
2141 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002142 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002143 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2144 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2145 // it will reject the audio m= section completely.
2146 PeerConnectionInterface::RTCOfferAnswerOptions options;
2147 options.offer_to_receive_audio = 0;
2148 callee()->SetOfferAnswerOptions(options);
2149 } else {
2150 // Stopping the audio RtpTransceiver will cause the media section to be
2151 // rejected in the answer.
2152 callee()->SetRemoteOfferHandler([this] {
2153 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2154 });
2155 }
Steve Anton15324772018-01-16 10:26:49 -08002156 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002157 // Do offer/answer and wait for successful end-to-end video frames.
2158 caller()->CreateAndSetAndSignalOffer();
2159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002160 MediaExpectations media_expectations;
2161 media_expectations.ExpectBidirectionalVideo();
2162 media_expectations.ExpectNoAudio();
2163 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2164
deadbeef1dcb1642017-03-29 21:08:16 -07002165 // Sanity check that the callee's description has a rejected audio section.
2166 ASSERT_NE(nullptr, callee()->pc()->local_description());
2167 const ContentInfo* callee_audio_content =
2168 GetFirstAudioContent(callee()->pc()->local_description()->description());
2169 ASSERT_NE(nullptr, callee_audio_content);
2170 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002171 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2172 // The caller's transceiver should have stopped after receiving the answer.
2173 EXPECT_TRUE(caller()
2174 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2175 ->stopped());
2176 }
deadbeef1dcb1642017-03-29 21:08:16 -07002177}
2178
2179// Test that if the answerer rejects the video m= section, no video is sent or
2180// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002181TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002182 ASSERT_TRUE(CreatePeerConnectionWrappers());
2183 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002184 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002185 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2186 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2187 // it will reject the video m= section completely.
2188 PeerConnectionInterface::RTCOfferAnswerOptions options;
2189 options.offer_to_receive_video = 0;
2190 callee()->SetOfferAnswerOptions(options);
2191 } else {
2192 // Stopping the video RtpTransceiver will cause the media section to be
2193 // rejected in the answer.
2194 callee()->SetRemoteOfferHandler([this] {
2195 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2196 });
2197 }
Steve Anton15324772018-01-16 10:26:49 -08002198 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002199 // Do offer/answer and wait for successful end-to-end audio frames.
2200 caller()->CreateAndSetAndSignalOffer();
2201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002202 MediaExpectations media_expectations;
2203 media_expectations.ExpectBidirectionalAudio();
2204 media_expectations.ExpectNoVideo();
2205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2206
deadbeef1dcb1642017-03-29 21:08:16 -07002207 // Sanity check that the callee's description has a rejected video section.
2208 ASSERT_NE(nullptr, callee()->pc()->local_description());
2209 const ContentInfo* callee_video_content =
2210 GetFirstVideoContent(callee()->pc()->local_description()->description());
2211 ASSERT_NE(nullptr, callee_video_content);
2212 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002213 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2214 // The caller's transceiver should have stopped after receiving the answer.
2215 EXPECT_TRUE(caller()
2216 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2217 ->stopped());
2218 }
deadbeef1dcb1642017-03-29 21:08:16 -07002219}
2220
2221// Test that if the answerer rejects both audio and video m= sections, nothing
2222// bad happens.
2223// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2224// test anything but the fact that negotiation succeeds, which doesn't mean
2225// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002226TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002227 ASSERT_TRUE(CreatePeerConnectionWrappers());
2228 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002229 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002230 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2231 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2232 // will reject both audio and video m= sections.
2233 PeerConnectionInterface::RTCOfferAnswerOptions options;
2234 options.offer_to_receive_audio = 0;
2235 options.offer_to_receive_video = 0;
2236 callee()->SetOfferAnswerOptions(options);
2237 } else {
2238 callee()->SetRemoteOfferHandler([this] {
2239 // Stopping all transceivers will cause all media sections to be rejected.
2240 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2241 transceiver->Stop();
2242 }
2243 });
2244 }
deadbeef1dcb1642017-03-29 21:08:16 -07002245 // Do offer/answer and wait for stable signaling state.
2246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002248
deadbeef1dcb1642017-03-29 21:08:16 -07002249 // Sanity check that the callee's description has rejected m= sections.
2250 ASSERT_NE(nullptr, callee()->pc()->local_description());
2251 const ContentInfo* callee_audio_content =
2252 GetFirstAudioContent(callee()->pc()->local_description()->description());
2253 ASSERT_NE(nullptr, callee_audio_content);
2254 EXPECT_TRUE(callee_audio_content->rejected);
2255 const ContentInfo* callee_video_content =
2256 GetFirstVideoContent(callee()->pc()->local_description()->description());
2257 ASSERT_NE(nullptr, callee_video_content);
2258 EXPECT_TRUE(callee_video_content->rejected);
2259}
2260
2261// This test sets up an audio and video call between two parties. After the
2262// call runs for a while, the caller sends an updated offer with video being
2263// rejected. Once the re-negotiation is done, the video flow should stop and
2264// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002265TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002266 ASSERT_TRUE(CreatePeerConnectionWrappers());
2267 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002268 caller()->AddAudioVideoTracks();
2269 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002270 caller()->CreateAndSetAndSignalOffer();
2271 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002272 {
2273 MediaExpectations media_expectations;
2274 media_expectations.ExpectBidirectionalAudioAndVideo();
2275 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2276 }
deadbeef1dcb1642017-03-29 21:08:16 -07002277 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2279 caller()->SetGeneratedSdpMunger(
2280 [](cricket::SessionDescription* description) {
2281 for (cricket::ContentInfo& content : description->contents()) {
2282 if (cricket::IsVideoContent(&content)) {
2283 content.rejected = true;
2284 }
2285 }
2286 });
2287 } else {
2288 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2289 }
deadbeef1dcb1642017-03-29 21:08:16 -07002290 caller()->CreateAndSetAndSignalOffer();
2291 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2292
2293 // Sanity check that the caller's description has a rejected video section.
2294 ASSERT_NE(nullptr, caller()->pc()->local_description());
2295 const ContentInfo* caller_video_content =
2296 GetFirstVideoContent(caller()->pc()->local_description()->description());
2297 ASSERT_NE(nullptr, caller_video_content);
2298 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002299 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002300 {
2301 MediaExpectations media_expectations;
2302 media_expectations.ExpectBidirectionalAudio();
2303 media_expectations.ExpectNoVideo();
2304 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2305 }
deadbeef1dcb1642017-03-29 21:08:16 -07002306}
2307
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002308// Do one offer/answer with audio, another that disables it (rejecting the m=
2309// section), and another that re-enables it. Regression test for:
2310// bugs.webrtc.org/6023
2311TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2312 ASSERT_TRUE(CreatePeerConnectionWrappers());
2313 ConnectFakeSignaling();
2314
2315 // Add audio track, do normal offer/answer.
2316 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2317 caller()->CreateLocalAudioTrack();
2318 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2319 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2320 caller()->CreateAndSetAndSignalOffer();
2321 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2322
2323 // Remove audio track, and set offer_to_receive_audio to false to cause the
2324 // m= section to be completely disabled, not just "recvonly".
2325 caller()->pc()->RemoveTrack(sender);
2326 PeerConnectionInterface::RTCOfferAnswerOptions options;
2327 options.offer_to_receive_audio = 0;
2328 caller()->SetOfferAnswerOptions(options);
2329 caller()->CreateAndSetAndSignalOffer();
2330 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2331
2332 // Add the audio track again, expecting negotiation to succeed and frames to
2333 // flow.
2334 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2335 options.offer_to_receive_audio = 1;
2336 caller()->SetOfferAnswerOptions(options);
2337 caller()->CreateAndSetAndSignalOffer();
2338 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2339
2340 MediaExpectations media_expectations;
2341 media_expectations.CalleeExpectsSomeAudio();
2342 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2343}
2344
deadbeef1dcb1642017-03-29 21:08:16 -07002345// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2346// is needed to support legacy endpoints.
2347// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2348// add a test for an end-to-end test without MID signaling either (basically,
2349// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002350TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002351 ASSERT_TRUE(CreatePeerConnectionWrappers());
2352 ConnectFakeSignaling();
2353 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002354 caller()->AddAudioVideoTracks();
2355 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002356 // Remove SSRCs and MSIDs from the received offer SDP.
2357 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002358 caller()->CreateAndSetAndSignalOffer();
2359 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002360 MediaExpectations media_expectations;
2361 media_expectations.ExpectBidirectionalAudioAndVideo();
2362 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002363}
2364
Seth Hampson5897a6e2018-04-03 11:16:33 -07002365// Basic end-to-end test, without SSRC signaling. This means that the track
2366// was created properly and frames are delivered when the MSIDs are communicated
2367// with a=msid lines and no a=ssrc lines.
2368TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2369 EndToEndCallWithoutSsrcSignaling) {
2370 const char kStreamId[] = "streamId";
2371 ASSERT_TRUE(CreatePeerConnectionWrappers());
2372 ConnectFakeSignaling();
2373 // Add just audio tracks.
2374 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2375 callee()->AddAudioTrack();
2376
2377 // Remove SSRCs from the received offer SDP.
2378 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2381 MediaExpectations media_expectations;
2382 media_expectations.ExpectBidirectionalAudio();
2383 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2384}
2385
Steve Antondf527fd2018-04-27 15:52:03 -07002386// Tests that video flows between multiple video tracks when SSRCs are not
2387// signaled. This exercises the MID RTP header extension which is needed to
2388// demux the incoming video tracks.
2389TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2390 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2391 ASSERT_TRUE(CreatePeerConnectionWrappers());
2392 ConnectFakeSignaling();
2393 caller()->AddVideoTrack();
2394 caller()->AddVideoTrack();
2395 callee()->AddVideoTrack();
2396 callee()->AddVideoTrack();
2397
2398 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2399 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2400 caller()->CreateAndSetAndSignalOffer();
2401 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2402 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2403 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2404
2405 // Expect video to be received in both directions on both tracks.
2406 MediaExpectations media_expectations;
2407 media_expectations.ExpectBidirectionalVideo();
2408 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2409}
2410
Henrik Boström5b147782018-12-04 11:25:05 +01002411TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2412 ASSERT_TRUE(CreatePeerConnectionWrappers());
2413 ConnectFakeSignaling();
2414 caller()->AddAudioTrack();
2415 caller()->AddVideoTrack();
2416 caller()->CreateAndSetAndSignalOffer();
2417 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2418 auto callee_receivers = callee()->pc()->GetReceivers();
2419 ASSERT_EQ(2u, callee_receivers.size());
2420 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2421 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2422}
2423
2424TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2425 ASSERT_TRUE(CreatePeerConnectionWrappers());
2426 ConnectFakeSignaling();
2427 caller()->AddAudioTrack();
2428 caller()->AddVideoTrack();
2429 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2430 caller()->CreateAndSetAndSignalOffer();
2431 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2432 auto callee_receivers = callee()->pc()->GetReceivers();
2433 ASSERT_EQ(2u, callee_receivers.size());
2434 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2435 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2436 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2437 callee_receivers[1]->stream_ids()[0]);
2438 EXPECT_EQ(callee_receivers[0]->streams()[0],
2439 callee_receivers[1]->streams()[0]);
2440}
2441
deadbeef1dcb1642017-03-29 21:08:16 -07002442// Test that if two video tracks are sent (from caller to callee, in this test),
2443// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002444TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002445 ASSERT_TRUE(CreatePeerConnectionWrappers());
2446 ConnectFakeSignaling();
2447 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002448 caller()->AddAudioVideoTracks();
2449 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002450 caller()->CreateAndSetAndSignalOffer();
2451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002452 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002453
2454 MediaExpectations media_expectations;
2455 media_expectations.CalleeExpectsSomeAudioAndVideo();
2456 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002457}
2458
2459static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2460 bool first = true;
2461 for (cricket::ContentInfo& content : desc->contents()) {
2462 if (first) {
2463 first = false;
2464 continue;
2465 }
2466 content.bundle_only = true;
2467 }
2468 first = true;
2469 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2470 if (first) {
2471 first = false;
2472 continue;
2473 }
2474 transport.description.ice_ufrag.clear();
2475 transport.description.ice_pwd.clear();
2476 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2477 transport.description.identity_fingerprint.reset(nullptr);
2478 }
2479}
2480
2481// Test that if applying a true "max bundle" offer, which uses ports of 0,
2482// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2483// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2484// successfully and media flows.
2485// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2486// TODO(deadbeef): Won't need this test once we start generating actual
2487// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002488TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002489 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2490 ASSERT_TRUE(CreatePeerConnectionWrappers());
2491 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002492 caller()->AddAudioVideoTracks();
2493 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002494 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2495 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2496 // but the first m= section.
2497 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2498 caller()->CreateAndSetAndSignalOffer();
2499 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002500 MediaExpectations media_expectations;
2501 media_expectations.ExpectBidirectionalAudioAndVideo();
2502 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002503}
2504
2505// Test that we can receive the audio output level from a remote audio track.
2506// TODO(deadbeef): Use a fake audio source and verify that the output level is
2507// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002508TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002509 ASSERT_TRUE(CreatePeerConnectionWrappers());
2510 ConnectFakeSignaling();
2511 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002512 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002513 caller()->CreateAndSetAndSignalOffer();
2514 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2515
2516 // Get the audio output level stats. Note that the level is not available
2517 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002518 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002519 kMaxWaitForFramesMs);
2520}
2521
2522// Test that an audio input level is reported.
2523// TODO(deadbeef): Use a fake audio source and verify that the input level is
2524// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002525TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002526 ASSERT_TRUE(CreatePeerConnectionWrappers());
2527 ConnectFakeSignaling();
2528 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002529 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002530 caller()->CreateAndSetAndSignalOffer();
2531 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2532
2533 // Get the audio input level stats. The level should be available very
2534 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002535 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002536 kMaxWaitForStatsMs);
2537}
2538
2539// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002540TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002541 ASSERT_TRUE(CreatePeerConnectionWrappers());
2542 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002543 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002544 // Do offer/answer, wait for the callee to receive some frames.
2545 caller()->CreateAndSetAndSignalOffer();
2546 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002547
2548 MediaExpectations media_expectations;
2549 media_expectations.CalleeExpectsSomeAudioAndVideo();
2550 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002551
2552 // Get a handle to the remote tracks created, so they can be used as GetStats
2553 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002554 for (auto receiver : callee()->pc()->GetReceivers()) {
2555 // We received frames, so we definitely should have nonzero "received bytes"
2556 // stats at this point.
2557 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2558 0);
2559 }
deadbeef1dcb1642017-03-29 21:08:16 -07002560}
2561
2562// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002563TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002564 ASSERT_TRUE(CreatePeerConnectionWrappers());
2565 ConnectFakeSignaling();
2566 auto audio_track = caller()->CreateLocalAudioTrack();
2567 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002568 caller()->AddTrack(audio_track);
2569 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002570 // Do offer/answer, wait for the callee to receive some frames.
2571 caller()->CreateAndSetAndSignalOffer();
2572 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002573 MediaExpectations media_expectations;
2574 media_expectations.CalleeExpectsSomeAudioAndVideo();
2575 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002576
2577 // The callee received frames, so we definitely should have nonzero "sent
2578 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002579 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2580 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2581}
2582
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002583// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002585 ASSERT_TRUE(CreatePeerConnectionWrappers());
2586 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002587 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002588
Steve Anton15324772018-01-16 10:26:49 -08002589 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002590
2591 // Do offer/answer, wait for the callee to receive some frames.
2592 caller()->CreateAndSetAndSignalOffer();
2593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2594
2595 // Get the remote audio track created on the receiver, so they can be used as
2596 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002597 auto receivers = callee()->pc()->GetReceivers();
2598 ASSERT_EQ(1u, receivers.size());
2599 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002600
2601 // Get the audio output level stats. Note that the level is not available
2602 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002603 EXPECT_TRUE_WAIT(
2604 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2605 0,
2606 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002607}
2608
Steve Antona41959e2018-11-28 11:15:33 -08002609// Test that the track ID is associated with all local and remote SSRC stats
2610// using the old GetStats() and more than 1 audio and more than 1 video track.
2611// This is a regression test for crbug.com/906988
2612TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2613 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2614 ASSERT_TRUE(CreatePeerConnectionWrappers());
2615 ConnectFakeSignaling();
2616 auto audio_sender_1 = caller()->AddAudioTrack();
2617 auto video_sender_1 = caller()->AddVideoTrack();
2618 auto audio_sender_2 = caller()->AddAudioTrack();
2619 auto video_sender_2 = caller()->AddVideoTrack();
2620 caller()->CreateAndSetAndSignalOffer();
2621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2622
2623 MediaExpectations media_expectations;
2624 media_expectations.CalleeExpectsSomeAudioAndVideo();
2625 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2626
2627 std::vector<std::string> track_ids = {
2628 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2629 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2630
2631 auto caller_stats = caller()->OldGetStats();
2632 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2633 auto callee_stats = callee()->OldGetStats();
2634 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2635}
2636
Steve Antonffa6ce42018-11-30 09:26:08 -08002637// Test that the new GetStats() returns stats for all outgoing/incoming streams
2638// with the correct track IDs if there are more than one audio and more than one
2639// video senders/receivers.
2640TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2641 ASSERT_TRUE(CreatePeerConnectionWrappers());
2642 ConnectFakeSignaling();
2643 auto audio_sender_1 = caller()->AddAudioTrack();
2644 auto video_sender_1 = caller()->AddVideoTrack();
2645 auto audio_sender_2 = caller()->AddAudioTrack();
2646 auto video_sender_2 = caller()->AddVideoTrack();
2647 caller()->CreateAndSetAndSignalOffer();
2648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2649
2650 MediaExpectations media_expectations;
2651 media_expectations.CalleeExpectsSomeAudioAndVideo();
2652 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2653
2654 std::vector<std::string> track_ids = {
2655 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2656 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2657
2658 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2659 caller()->NewGetStats();
2660 ASSERT_TRUE(caller_report);
2661 auto outbound_stream_stats =
2662 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2663 ASSERT_EQ(4u, outbound_stream_stats.size());
2664 std::vector<std::string> outbound_track_ids;
2665 for (const auto& stat : outbound_stream_stats) {
2666 ASSERT_TRUE(stat->bytes_sent.is_defined());
2667 EXPECT_LT(0u, *stat->bytes_sent);
2668 ASSERT_TRUE(stat->track_id.is_defined());
2669 const auto* track_stat =
2670 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2671 ASSERT_TRUE(track_stat);
2672 outbound_track_ids.push_back(*track_stat->track_identifier);
2673 }
2674 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2675
2676 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2677 callee()->NewGetStats();
2678 ASSERT_TRUE(callee_report);
2679 auto inbound_stream_stats =
2680 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2681 ASSERT_EQ(4u, inbound_stream_stats.size());
2682 std::vector<std::string> inbound_track_ids;
2683 for (const auto& stat : inbound_stream_stats) {
2684 ASSERT_TRUE(stat->bytes_received.is_defined());
2685 EXPECT_LT(0u, *stat->bytes_received);
2686 ASSERT_TRUE(stat->track_id.is_defined());
2687 const auto* track_stat =
2688 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2689 ASSERT_TRUE(track_stat);
2690 inbound_track_ids.push_back(*track_stat->track_identifier);
2691 }
2692 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2693}
2694
2695// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002696// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2697// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002698TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002699 GetStatsForUnsignaledStreamWithNewStatsApi) {
2700 ASSERT_TRUE(CreatePeerConnectionWrappers());
2701 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002702 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002703 // Remove SSRCs and MSIDs from the received offer SDP.
2704 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2705 caller()->CreateAndSetAndSignalOffer();
2706 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002707 MediaExpectations media_expectations;
2708 media_expectations.CalleeExpectsSomeAudio(1);
2709 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002710
2711 // We received a frame, so we should have nonzero "bytes received" stats for
2712 // the unsignaled stream, if stats are working for it.
2713 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2714 callee()->NewGetStats();
2715 ASSERT_NE(nullptr, report);
2716 auto inbound_stream_stats =
2717 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2718 ASSERT_EQ(1U, inbound_stream_stats.size());
2719 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2720 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002721 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2722}
2723
Taylor Brandstettera4653442018-06-19 09:44:26 -07002724// Same as above but for the legacy stats implementation.
2725TEST_P(PeerConnectionIntegrationTest,
2726 GetStatsForUnsignaledStreamWithOldStatsApi) {
2727 ASSERT_TRUE(CreatePeerConnectionWrappers());
2728 ConnectFakeSignaling();
2729 caller()->AddAudioTrack();
2730 // Remove SSRCs and MSIDs from the received offer SDP.
2731 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2732 caller()->CreateAndSetAndSignalOffer();
2733 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2734
2735 // Note that, since the old stats implementation associates SSRCs with tracks
2736 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2737 // associated track ID. So we can't use the track "selector" argument.
2738 //
2739 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2740 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002741 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002742 kDefaultTimeout);
2743}
2744
zhihuangf8164932017-05-19 13:09:47 -07002745// Test that we can successfully get the media related stats (audio level
2746// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002747TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002748 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2749 ASSERT_TRUE(CreatePeerConnectionWrappers());
2750 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002751 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002752 // Remove SSRCs and MSIDs from the received offer SDP.
2753 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2754 caller()->CreateAndSetAndSignalOffer();
2755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002756 MediaExpectations media_expectations;
2757 media_expectations.CalleeExpectsSomeAudio(1);
2758 media_expectations.CalleeExpectsSomeVideo(1);
2759 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002760
2761 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2762 callee()->NewGetStats();
2763 ASSERT_NE(nullptr, report);
2764
2765 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2766 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2767 ASSERT_GE(audio_index, 0);
2768 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002769}
2770
deadbeef4e2deab2017-09-20 13:56:21 -07002771// Helper for test below.
2772void ModifySsrcs(cricket::SessionDescription* desc) {
2773 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002774 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002775 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002776 for (uint32_t& ssrc : stream.ssrcs) {
2777 ssrc = rtc::CreateRandomId();
2778 }
2779 }
2780 }
2781}
2782
2783// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2784// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2785// This should result in two "RTCInboundRTPStreamStats", but only one
2786// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2787// being reset to 0 once the SSRC change occurs.
2788//
2789// Regression test for this bug:
2790// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2791//
2792// The bug causes the track stats to only represent one of the two streams:
2793// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2794// that the track stat counters would reset to 0 when the new stream is
2795// received, and a 50% chance that they'll stop updating (while
2796// "concealed_samples" continues increasing, due to silence being generated for
2797// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002798TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002799 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002800 ASSERT_TRUE(CreatePeerConnectionWrappers());
2801 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002802 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002803 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2804 // that doesn't signal SSRCs (from the callee's perspective).
2805 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2806 caller()->CreateAndSetAndSignalOffer();
2807 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2808 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002809 {
2810 MediaExpectations media_expectations;
2811 media_expectations.CalleeExpectsSomeAudio(50);
2812 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2813 }
deadbeef4e2deab2017-09-20 13:56:21 -07002814 // Some audio frames were received, so we should have nonzero "samples
2815 // received" for the track.
2816 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2817 callee()->NewGetStats();
2818 ASSERT_NE(nullptr, report);
2819 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2820 ASSERT_EQ(1U, track_stats.size());
2821 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2822 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2823 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2824
2825 // Create a new offer and munge it to cause the caller to use a new SSRC.
2826 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2827 caller()->CreateAndSetAndSignalOffer();
2828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2829 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2830 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002831 {
2832 MediaExpectations media_expectations;
2833 media_expectations.CalleeExpectsSomeAudio(25);
2834 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2835 }
deadbeef4e2deab2017-09-20 13:56:21 -07002836
2837 report = callee()->NewGetStats();
2838 ASSERT_NE(nullptr, report);
2839 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2840 ASSERT_EQ(1U, track_stats.size());
2841 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2842 // The "total samples received" stat should only be greater than it was
2843 // before.
2844 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2845 // Right now, the new SSRC will cause the counters to reset to 0.
2846 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2847
2848 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002849 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002850 // good sign that we're seeing stats from the old stream that's no longer
2851 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002852 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002853 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2854 EXPECT_LT(*track_stats[0]->concealed_samples,
2855 *track_stats[0]->total_samples_received *
2856 kAcceptableConcealedSamplesPercentage);
2857
2858 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2859 // sanity check that the SSRC really changed.
2860 // TODO(deadbeef): This isn't working right now, because we're not returning
2861 // *any* stats for the inactive stream. Uncomment when the bug is completely
2862 // fixed.
2863 // auto inbound_stream_stats =
2864 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2865 // ASSERT_EQ(2U, inbound_stream_stats.size());
2866}
2867
deadbeef1dcb1642017-03-29 21:08:16 -07002868// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002869TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002870 PeerConnectionFactory::Options dtls_10_options;
2871 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2872 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2873 dtls_10_options));
2874 ConnectFakeSignaling();
2875 // Do normal offer/answer and wait for some frames to be received in each
2876 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002877 caller()->AddAudioVideoTracks();
2878 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002879 caller()->CreateAndSetAndSignalOffer();
2880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002881 MediaExpectations media_expectations;
2882 media_expectations.ExpectBidirectionalAudioAndVideo();
2883 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002884}
2885
2886// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002887TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002888 PeerConnectionFactory::Options dtls_10_options;
2889 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2890 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2891 dtls_10_options));
2892 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002893 caller()->AddAudioVideoTracks();
2894 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002895 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002896 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002897 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002898 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002899 kDefaultTimeout);
2900 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002901 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002902 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002903 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002904 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2905 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002906}
2907
2908// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002909TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002910 PeerConnectionFactory::Options dtls_12_options;
2911 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2912 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2913 dtls_12_options));
2914 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002915 caller()->AddAudioVideoTracks();
2916 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002917 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002918 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002919 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002920 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002921 kDefaultTimeout);
2922 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002923 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002924 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002925 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002926 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2927 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002928}
2929
2930// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2931// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002932TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002933 PeerConnectionFactory::Options caller_options;
2934 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2935 PeerConnectionFactory::Options callee_options;
2936 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2937 ASSERT_TRUE(
2938 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2939 ConnectFakeSignaling();
2940 // Do normal offer/answer and wait for some frames to be received in each
2941 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002942 caller()->AddAudioVideoTracks();
2943 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002944 caller()->CreateAndSetAndSignalOffer();
2945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002946 MediaExpectations media_expectations;
2947 media_expectations.ExpectBidirectionalAudioAndVideo();
2948 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002949}
2950
2951// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2952// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002953TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002954 PeerConnectionFactory::Options caller_options;
2955 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2956 PeerConnectionFactory::Options callee_options;
2957 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2958 ASSERT_TRUE(
2959 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2960 ConnectFakeSignaling();
2961 // Do normal offer/answer and wait for some frames to be received in each
2962 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002963 caller()->AddAudioVideoTracks();
2964 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002965 caller()->CreateAndSetAndSignalOffer();
2966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967 MediaExpectations media_expectations;
2968 media_expectations.ExpectBidirectionalAudioAndVideo();
2969 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002970}
2971
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002972// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2973// works as expected; the cipher should only be used if enabled by both sides.
2974TEST_P(PeerConnectionIntegrationTest,
2975 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2976 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002977 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002978 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002979 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2980 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002981 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2982 TestNegotiatedCipherSuite(caller_options, callee_options,
2983 expected_cipher_suite);
2984}
2985
2986TEST_P(PeerConnectionIntegrationTest,
2987 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2988 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002989 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2990 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002991 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002992 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002993 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2994 TestNegotiatedCipherSuite(caller_options, callee_options,
2995 expected_cipher_suite);
2996}
2997
2998TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2999 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003000 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003001 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003002 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003003 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3004 TestNegotiatedCipherSuite(caller_options, callee_options,
3005 expected_cipher_suite);
3006}
3007
deadbeef1dcb1642017-03-29 21:08:16 -07003008// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003009TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003010 bool local_gcm_enabled = false;
3011 bool remote_gcm_enabled = false;
3012 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3013 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3014 expected_cipher_suite);
3015}
3016
3017// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003018TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003019 bool local_gcm_enabled = true;
3020 bool remote_gcm_enabled = true;
3021 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3022 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3023 expected_cipher_suite);
3024}
3025
3026// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003027TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003028 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3029 bool local_gcm_enabled = true;
3030 bool remote_gcm_enabled = false;
3031 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3032 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3033 expected_cipher_suite);
3034}
3035
3036// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003037TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003038 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3039 bool local_gcm_enabled = false;
3040 bool remote_gcm_enabled = true;
3041 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3042 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3043 expected_cipher_suite);
3044}
3045
deadbeef7914b8c2017-04-21 03:23:33 -07003046// Verify that media can be transmitted end-to-end when GCM crypto suites are
3047// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3048// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3049// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003050TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003051 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003052 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003053 ASSERT_TRUE(
3054 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3055 ConnectFakeSignaling();
3056 // Do normal offer/answer and wait for some frames to be received in each
3057 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003058 caller()->AddAudioVideoTracks();
3059 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003060 caller()->CreateAndSetAndSignalOffer();
3061 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003062 MediaExpectations media_expectations;
3063 media_expectations.ExpectBidirectionalAudioAndVideo();
3064 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003065}
3066
deadbeef1dcb1642017-03-29 21:08:16 -07003067// This test sets up a call between two parties with audio, video and an RTP
3068// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003069TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003070 PeerConnectionInterface::RTCConfiguration rtc_config;
3071 rtc_config.enable_rtp_data_channel = true;
3072 rtc_config.enable_dtls_srtp = false;
3073 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003074 ConnectFakeSignaling();
3075 // Expect that data channel created on caller side will show up for callee as
3076 // well.
3077 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003078 caller()->AddAudioVideoTracks();
3079 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003080 caller()->CreateAndSetAndSignalOffer();
3081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3082 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003083 MediaExpectations media_expectations;
3084 media_expectations.ExpectBidirectionalAudioAndVideo();
3085 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003086 ASSERT_NE(nullptr, caller()->data_channel());
3087 ASSERT_NE(nullptr, callee()->data_channel());
3088 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3089 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3090
3091 // Ensure data can be sent in both directions.
3092 std::string data = "hello world";
3093 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3094 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3095 kDefaultTimeout);
3096 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3097 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3098 kDefaultTimeout);
3099}
3100
3101// Ensure that an RTP data channel is signaled as closed for the caller when
3102// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003103TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003104 RtpDataChannelSignaledClosedInCalleeOffer) {
3105 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003106 PeerConnectionInterface::RTCConfiguration rtc_config;
3107 rtc_config.enable_rtp_data_channel = true;
3108 rtc_config.enable_dtls_srtp = false;
3109 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003110 ConnectFakeSignaling();
3111 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003112 caller()->AddAudioVideoTracks();
3113 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003114 caller()->CreateAndSetAndSignalOffer();
3115 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3116 ASSERT_NE(nullptr, caller()->data_channel());
3117 ASSERT_NE(nullptr, callee()->data_channel());
3118 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3119 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3120
3121 // Close the data channel on the callee, and do an updated offer/answer.
3122 callee()->data_channel()->Close();
3123 callee()->CreateAndSetAndSignalOffer();
3124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3125 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3126 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3127}
3128
3129// Tests that data is buffered in an RTP data channel until an observer is
3130// registered for it.
3131//
3132// NOTE: RTP data channels can receive data before the underlying
3133// transport has detected that a channel is writable and thus data can be
3134// received before the data channel state changes to open. That is hard to test
3135// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003136TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003137 DataBufferedUntilRtpDataChannelObserverRegistered) {
3138 // Use fake clock and simulated network delay so that we predictably can wait
3139 // until an SCTP message has been delivered without "sleep()"ing.
3140 rtc::ScopedFakeClock fake_clock;
3141 // Some things use a time of "0" as a special value, so we need to start out
3142 // the fake clock at a nonzero time.
3143 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003144 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003145 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3146 virtual_socket_server()->UpdateDelayDistribution();
3147
Niels Möllerf06f9232018-08-07 12:32:18 +02003148 PeerConnectionInterface::RTCConfiguration rtc_config;
3149 rtc_config.enable_rtp_data_channel = true;
3150 rtc_config.enable_dtls_srtp = false;
3151 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003152 ConnectFakeSignaling();
3153 caller()->CreateDataChannel();
3154 caller()->CreateAndSetAndSignalOffer();
3155 ASSERT_TRUE(caller()->data_channel() != nullptr);
3156 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3157 kDefaultTimeout, fake_clock);
3158 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3159 kDefaultTimeout, fake_clock);
3160 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3161 callee()->data_channel()->state(), kDefaultTimeout,
3162 fake_clock);
3163
3164 // Unregister the observer which is normally automatically registered.
3165 callee()->data_channel()->UnregisterObserver();
3166 // Send data and advance fake clock until it should have been received.
3167 std::string data = "hello world";
3168 caller()->data_channel()->Send(DataBuffer(data));
3169 SIMULATED_WAIT(false, 50, fake_clock);
3170
3171 // Attach data channel and expect data to be received immediately. Note that
3172 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3173 // further, but data can be received even if the callback is asynchronous.
3174 MockDataChannelObserver new_observer(callee()->data_channel());
3175 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3176 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003177 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3178 // If this is not done a DCHECK can be hit in ports.cc, because a large
3179 // negative number is calculated for the rtt due to the global clock changing.
3180 caller()->pc()->Close();
3181 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003182}
3183
3184// This test sets up a call between two parties with audio, video and but only
3185// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003186TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003187 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3188 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003189 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003190 rtc_config_1.enable_dtls_srtp = false;
3191 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3192 rtc_config_2.enable_dtls_srtp = false;
3193 rtc_config_2.enable_dtls_srtp = false;
3194 ASSERT_TRUE(
3195 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003196 ConnectFakeSignaling();
3197 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003198 caller()->AddAudioVideoTracks();
3199 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003200 caller()->CreateAndSetAndSignalOffer();
3201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3202 // The caller should still have a data channel, but it should be closed, and
3203 // one should ever have been created for the callee.
3204 EXPECT_TRUE(caller()->data_channel() != nullptr);
3205 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3206 EXPECT_EQ(nullptr, callee()->data_channel());
3207}
3208
3209// This test sets up a call between two parties with audio, and video. When
3210// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003211TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003212 PeerConnectionInterface::RTCConfiguration rtc_config;
3213 rtc_config.enable_rtp_data_channel = true;
3214 rtc_config.enable_dtls_srtp = false;
3215 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003216 ConnectFakeSignaling();
3217 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003218 caller()->AddAudioVideoTracks();
3219 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003220 caller()->CreateAndSetAndSignalOffer();
3221 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3222 // Create data channel and do new offer and answer.
3223 caller()->CreateDataChannel();
3224 caller()->CreateAndSetAndSignalOffer();
3225 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3226 ASSERT_NE(nullptr, caller()->data_channel());
3227 ASSERT_NE(nullptr, callee()->data_channel());
3228 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3229 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3230 // Ensure data can be sent in both directions.
3231 std::string data = "hello world";
3232 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3233 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3234 kDefaultTimeout);
3235 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3236 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3237 kDefaultTimeout);
3238}
3239
3240#ifdef HAVE_SCTP
3241
3242// This test sets up a call between two parties with audio, video and an SCTP
3243// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003244TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003245 ASSERT_TRUE(CreatePeerConnectionWrappers());
3246 ConnectFakeSignaling();
3247 // Expect that data channel created on caller side will show up for callee as
3248 // well.
3249 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003250 caller()->AddAudioVideoTracks();
3251 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003252 caller()->CreateAndSetAndSignalOffer();
3253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3254 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003255 MediaExpectations media_expectations;
3256 media_expectations.ExpectBidirectionalAudioAndVideo();
3257 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003258 // Caller data channel should already exist (it created one). Callee data
3259 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3260 ASSERT_NE(nullptr, caller()->data_channel());
3261 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3262 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3263 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3264
3265 // Ensure data can be sent in both directions.
3266 std::string data = "hello world";
3267 caller()->data_channel()->Send(DataBuffer(data));
3268 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3269 kDefaultTimeout);
3270 callee()->data_channel()->Send(DataBuffer(data));
3271 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3272 kDefaultTimeout);
3273}
3274
3275// Ensure that when the callee closes an SCTP data channel, the closing
3276// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003277TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003278 // Same procedure as above test.
3279 ASSERT_TRUE(CreatePeerConnectionWrappers());
3280 ConnectFakeSignaling();
3281 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003282 caller()->AddAudioVideoTracks();
3283 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003284 caller()->CreateAndSetAndSignalOffer();
3285 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3286 ASSERT_NE(nullptr, caller()->data_channel());
3287 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3288 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3289 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3290
3291 // Close the data channel on the callee side, and wait for it to reach the
3292 // "closed" state on both sides.
3293 callee()->data_channel()->Close();
3294 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3295 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3296}
3297
Seth Hampson2f0d7022018-02-20 11:54:42 -08003298TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003299 ASSERT_TRUE(CreatePeerConnectionWrappers());
3300 ConnectFakeSignaling();
3301 webrtc::DataChannelInit init;
3302 init.id = 53;
3303 init.maxRetransmits = 52;
3304 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003305 caller()->AddAudioVideoTracks();
3306 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003307 caller()->CreateAndSetAndSignalOffer();
3308 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003309 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3310 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003311 EXPECT_EQ(init.id, callee()->data_channel()->id());
3312 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3313 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3314 EXPECT_FALSE(callee()->data_channel()->negotiated());
3315}
3316
deadbeef1dcb1642017-03-29 21:08:16 -07003317// Test usrsctp's ability to process unordered data stream, where data actually
3318// arrives out of order using simulated delays. Previously there have been some
3319// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003320TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003321 // Introduce random network delays.
3322 // Otherwise it's not a true "unordered" test.
3323 virtual_socket_server()->set_delay_mean(20);
3324 virtual_socket_server()->set_delay_stddev(5);
3325 virtual_socket_server()->UpdateDelayDistribution();
3326 // Normal procedure, but with unordered data channel config.
3327 ASSERT_TRUE(CreatePeerConnectionWrappers());
3328 ConnectFakeSignaling();
3329 webrtc::DataChannelInit init;
3330 init.ordered = false;
3331 caller()->CreateDataChannel(&init);
3332 caller()->CreateAndSetAndSignalOffer();
3333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3334 ASSERT_NE(nullptr, caller()->data_channel());
3335 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3336 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3337 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3338
3339 static constexpr int kNumMessages = 100;
3340 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3341 static constexpr size_t kMaxMessageSize = 4096;
3342 // Create and send random messages.
3343 std::vector<std::string> sent_messages;
3344 for (int i = 0; i < kNumMessages; ++i) {
3345 size_t length =
3346 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3347 std::string message;
3348 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3349 caller()->data_channel()->Send(DataBuffer(message));
3350 callee()->data_channel()->Send(DataBuffer(message));
3351 sent_messages.push_back(message);
3352 }
3353
3354 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003355 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003356 caller()->data_observer()->received_message_count(),
3357 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003358 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003359 callee()->data_observer()->received_message_count(),
3360 kDefaultTimeout);
3361
3362 // Sort and compare to make sure none of the messages were corrupted.
3363 std::vector<std::string> caller_received_messages =
3364 caller()->data_observer()->messages();
3365 std::vector<std::string> callee_received_messages =
3366 callee()->data_observer()->messages();
3367 std::sort(sent_messages.begin(), sent_messages.end());
3368 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3369 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3370 EXPECT_EQ(sent_messages, caller_received_messages);
3371 EXPECT_EQ(sent_messages, callee_received_messages);
3372}
3373
3374// This test sets up a call between two parties with audio, and video. When
3375// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003376TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003377 ASSERT_TRUE(CreatePeerConnectionWrappers());
3378 ConnectFakeSignaling();
3379 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003380 caller()->AddAudioVideoTracks();
3381 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003382 caller()->CreateAndSetAndSignalOffer();
3383 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3384 // Create data channel and do new offer and answer.
3385 caller()->CreateDataChannel();
3386 caller()->CreateAndSetAndSignalOffer();
3387 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3388 // Caller data channel should already exist (it created one). Callee data
3389 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3390 ASSERT_NE(nullptr, caller()->data_channel());
3391 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3392 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3393 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3394 // Ensure data can be sent in both directions.
3395 std::string data = "hello world";
3396 caller()->data_channel()->Send(DataBuffer(data));
3397 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3398 kDefaultTimeout);
3399 callee()->data_channel()->Send(DataBuffer(data));
3400 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3401 kDefaultTimeout);
3402}
3403
deadbeef7914b8c2017-04-21 03:23:33 -07003404// Set up a connection initially just using SCTP data channels, later upgrading
3405// to audio/video, ensuring frames are received end-to-end. Effectively the
3406// inverse of the test above.
3407// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003408TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003409 ASSERT_TRUE(CreatePeerConnectionWrappers());
3410 ConnectFakeSignaling();
3411 // Do initial offer/answer with just data channel.
3412 caller()->CreateDataChannel();
3413 caller()->CreateAndSetAndSignalOffer();
3414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3415 // Wait until data can be sent over the data channel.
3416 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3417 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3418 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3419
3420 // Do subsequent offer/answer with two-way audio and video. Audio and video
3421 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003422 caller()->AddAudioVideoTracks();
3423 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003424 caller()->CreateAndSetAndSignalOffer();
3425 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003426 MediaExpectations media_expectations;
3427 media_expectations.ExpectBidirectionalAudioAndVideo();
3428 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003429}
3430
deadbeef8b7e9ad2017-05-25 09:38:55 -07003431static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003432 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003433 GetFirstDataContentDescription(desc);
3434 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003435 dcd_offer->set_use_sctpmap(false);
3436 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3437}
3438
3439// Test that the data channel works when a spec-compliant SCTP m= section is
3440// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3441// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003442TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003443 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3444 ASSERT_TRUE(CreatePeerConnectionWrappers());
3445 ConnectFakeSignaling();
3446 caller()->CreateDataChannel();
3447 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3448 caller()->CreateAndSetAndSignalOffer();
3449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3451 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3452 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3453
3454 // Ensure data can be sent in both directions.
3455 std::string data = "hello world";
3456 caller()->data_channel()->Send(DataBuffer(data));
3457 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3458 kDefaultTimeout);
3459 callee()->data_channel()->Send(DataBuffer(data));
3460 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3461 kDefaultTimeout);
3462}
3463
deadbeef1dcb1642017-03-29 21:08:16 -07003464#endif // HAVE_SCTP
3465
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003466// This test sets up a call between two parties with a media transport data
3467// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003468TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3469 PeerConnectionInterface::RTCConfiguration rtc_config;
3470 rtc_config.use_media_transport_for_data_channels = true;
3471 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3472 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3473 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3474 loopback_media_transports()->second_factory()));
3475 ConnectFakeSignaling();
3476
3477 // Expect that data channel created on caller side will show up for callee as
3478 // well.
3479 caller()->CreateDataChannel();
3480 caller()->CreateAndSetAndSignalOffer();
3481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3482
3483 // Ensure that the media transport is ready.
3484 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3485 loopback_media_transports()->FlushAsyncInvokes();
3486
3487 // Caller data channel should already exist (it created one). Callee data
3488 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3489 ASSERT_NE(nullptr, caller()->data_channel());
3490 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3491 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3492 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3493
3494 // Ensure data can be sent in both directions.
3495 std::string data = "hello world";
3496 caller()->data_channel()->Send(DataBuffer(data));
3497 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3498 kDefaultTimeout);
3499 callee()->data_channel()->Send(DataBuffer(data));
3500 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3501 kDefaultTimeout);
3502}
3503
3504// Ensure that when the callee closes a media transport data channel, the
3505// closing procedure results in the data channel being closed for the caller
3506// as well.
3507TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3508 PeerConnectionInterface::RTCConfiguration rtc_config;
3509 rtc_config.use_media_transport_for_data_channels = true;
3510 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3511 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3512 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3513 loopback_media_transports()->second_factory()));
3514 ConnectFakeSignaling();
3515
3516 // Create a data channel on the caller and signal it to the callee.
3517 caller()->CreateDataChannel();
3518 caller()->CreateAndSetAndSignalOffer();
3519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3520
3521 // Ensure that the media transport is ready.
3522 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3523 loopback_media_transports()->FlushAsyncInvokes();
3524
3525 // Data channels exist and open on both ends of the connection.
3526 ASSERT_NE(nullptr, caller()->data_channel());
3527 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3528 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3529 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3530
3531 // Close the data channel on the callee side, and wait for it to reach the
3532 // "closed" state on both sides.
3533 callee()->data_channel()->Close();
3534 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3535 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3536}
3537
3538TEST_P(PeerConnectionIntegrationTest,
3539 MediaTransportDataChannelConfigSentToOtherSide) {
3540 PeerConnectionInterface::RTCConfiguration rtc_config;
3541 rtc_config.use_media_transport_for_data_channels = true;
3542 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3543 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3544 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3545 loopback_media_transports()->second_factory()));
3546 ConnectFakeSignaling();
3547
3548 // Create a data channel with a non-default configuration and signal it to the
3549 // callee.
3550 webrtc::DataChannelInit init;
3551 init.id = 53;
3552 init.maxRetransmits = 52;
3553 caller()->CreateDataChannel("data-channel", &init);
3554 caller()->CreateAndSetAndSignalOffer();
3555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3556
3557 // Ensure that the media transport is ready.
3558 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3559 loopback_media_transports()->FlushAsyncInvokes();
3560
3561 // Ensure that the data channel exists on the callee with the correct
3562 // configuration.
3563 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3564 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3565 EXPECT_EQ(init.id, callee()->data_channel()->id());
3566 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3567 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3568 EXPECT_FALSE(callee()->data_channel()->negotiated());
3569}
3570
Niels Möllerc68d2822018-11-20 14:52:05 +01003571TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3572 PeerConnectionInterface::RTCConfiguration rtc_config;
3573 rtc_config.use_media_transport = true;
3574 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3575 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3576 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3577 loopback_media_transports()->second_factory()));
3578 ConnectFakeSignaling();
3579
3580 caller()->AddAudioTrack();
3581 callee()->AddAudioTrack();
3582 // Start offer/answer exchange and wait for it to complete.
3583 caller()->CreateAndSetAndSignalOffer();
3584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3585
3586 // Ensure that the media transport is ready.
3587 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3588 loopback_media_transports()->FlushAsyncInvokes();
3589
3590 MediaExpectations media_expectations;
3591 media_expectations.ExpectBidirectionalAudio();
3592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3593
3594 webrtc::MediaTransportPair::Stats first_stats =
3595 loopback_media_transports()->FirstStats();
3596 webrtc::MediaTransportPair::Stats second_stats =
3597 loopback_media_transports()->SecondStats();
3598
3599 EXPECT_GT(first_stats.received_audio_frames, 0);
3600 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3601
3602 EXPECT_GT(second_stats.received_audio_frames, 0);
3603 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3604}
3605
Niels Möller46879152019-01-07 15:54:47 +01003606TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3607 PeerConnectionInterface::RTCConfiguration rtc_config;
3608 rtc_config.use_media_transport = true;
3609 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3610 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3611 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3612 loopback_media_transports()->second_factory()));
3613 ConnectFakeSignaling();
3614
3615 caller()->AddVideoTrack();
3616 callee()->AddVideoTrack();
3617 // Start offer/answer exchange and wait for it to complete.
3618 caller()->CreateAndSetAndSignalOffer();
3619 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3620
3621 // Ensure that the media transport is ready.
3622 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3623 loopback_media_transports()->FlushAsyncInvokes();
3624
3625 MediaExpectations media_expectations;
3626 media_expectations.ExpectBidirectionalVideo();
3627 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3628
3629 webrtc::MediaTransportPair::Stats first_stats =
3630 loopback_media_transports()->FirstStats();
3631 webrtc::MediaTransportPair::Stats second_stats =
3632 loopback_media_transports()->SecondStats();
3633
3634 EXPECT_GT(first_stats.received_video_frames, 0);
3635 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3636
3637 EXPECT_GT(second_stats.received_video_frames, 0);
3638 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3639}
3640
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003641TEST_P(PeerConnectionIntegrationTest,
3642 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3643 PeerConnectionInterface::RTCConfiguration rtc_config;
3644 rtc_config.use_media_transport = false;
3645 rtc_config.use_media_transport_for_data_channels = true;
3646 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3647 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3648 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3649 loopback_media_transports()->second_factory()));
3650 ConnectFakeSignaling();
3651
3652 caller()->AddVideoTrack();
3653 callee()->AddVideoTrack();
3654 // Start offer/answer exchange and wait for it to complete.
3655 caller()->CreateAndSetAndSignalOffer();
3656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3657
3658 MediaExpectations media_expectations;
3659 media_expectations.ExpectBidirectionalVideo();
3660 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3661}
3662
deadbeef1dcb1642017-03-29 21:08:16 -07003663// Test that the ICE connection and gathering states eventually reach
3664// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003665TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003666 ASSERT_TRUE(CreatePeerConnectionWrappers());
3667 ConnectFakeSignaling();
3668 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003669 caller()->AddAudioVideoTracks();
3670 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003671 caller()->CreateAndSetAndSignalOffer();
3672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3673 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3674 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3675 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3676 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3677 // After the best candidate pair is selected and all candidates are signaled,
3678 // the ICE connection state should reach "complete".
3679 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3680 // answerer/"callee" by default) only reaches "connected". When this is
3681 // fixed, this test should be updated.
3682 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3683 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003684 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3685 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003686}
3687
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003688constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3689 cricket::PORTALLOCATOR_DISABLE_RELAY |
3690 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003691
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003692// Use a mock resolver to resolve the hostname back to the original IP on both
3693// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003694TEST_P(PeerConnectionIntegrationTest,
3695 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003696 auto caller_resolver_factory =
3697 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3698 auto callee_resolver_factory =
3699 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3700 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3701 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003702
3703 // This also verifies that the injected AsyncResolverFactory is used by
3704 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003705 EXPECT_CALL(*caller_resolver_factory, Create())
3706 .WillOnce(Return(&caller_async_resolver));
3707 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3708 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3709
3710 EXPECT_CALL(*callee_resolver_factory, Create())
3711 .WillOnce(Return(&callee_async_resolver));
3712 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3713 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3714
3715 PeerConnectionInterface::RTCConfiguration config;
3716 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3717 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3718
3719 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3720 config, std::move(caller_deps), config, std::move(callee_deps)));
3721
3722 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3723 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3724
3725 // Enable hostname candidates with mDNS names.
3726 caller()->network()->CreateMdnsResponder(network_thread());
3727 callee()->network()->CreateMdnsResponder(network_thread());
3728
3729 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003730
3731 ConnectFakeSignaling();
3732 caller()->AddAudioVideoTracks();
3733 callee()->AddAudioVideoTracks();
3734 caller()->CreateAndSetAndSignalOffer();
3735 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3736 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3737 caller()->ice_connection_state(), kDefaultTimeout);
3738 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3739 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003740
3741 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3742 "WebRTC.PeerConnection.CandidatePairType_UDP",
3743 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003744}
3745
Steve Antonede9ca52017-10-16 13:04:27 -07003746// Test that firewalling the ICE connection causes the clients to identify the
3747// disconnected state and then removing the firewall causes them to reconnect.
3748class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003749 : public PeerConnectionIntegrationBaseTest,
3750 public ::testing::WithParamInterface<
3751 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003752 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003753 PeerConnectionIntegrationIceStatesTest()
3754 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3755 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003756 }
3757
3758 void StartStunServer(const SocketAddress& server_address) {
3759 stun_server_.reset(
3760 cricket::TestStunServer::Create(network_thread(), server_address));
3761 }
3762
3763 bool TestIPv6() {
3764 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3765 }
3766
3767 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003768 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3769 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003770 }
3771
3772 std::vector<SocketAddress> CallerAddresses() {
3773 std::vector<SocketAddress> addresses;
3774 addresses.push_back(SocketAddress("1.1.1.1", 0));
3775 if (TestIPv6()) {
3776 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3777 }
3778 return addresses;
3779 }
3780
3781 std::vector<SocketAddress> CalleeAddresses() {
3782 std::vector<SocketAddress> addresses;
3783 addresses.push_back(SocketAddress("2.2.2.2", 0));
3784 if (TestIPv6()) {
3785 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3786 }
3787 return addresses;
3788 }
3789
3790 void SetUpNetworkInterfaces() {
3791 // Remove the default interfaces added by the test infrastructure.
3792 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3793 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3794
3795 // Add network addresses for test.
3796 for (const auto& caller_address : CallerAddresses()) {
3797 caller()->network()->AddInterface(caller_address);
3798 }
3799 for (const auto& callee_address : CalleeAddresses()) {
3800 callee()->network()->AddInterface(callee_address);
3801 }
3802 }
3803
3804 private:
3805 uint32_t port_allocator_flags_;
3806 std::unique_ptr<cricket::TestStunServer> stun_server_;
3807};
3808
3809// Tests that the PeerConnection goes through all the ICE gathering/connection
3810// states over the duration of the call. This includes Disconnected and Failed
3811// states, induced by putting a firewall between the peers and waiting for them
3812// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003813TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3814 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3815 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3816 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003817
3818 const SocketAddress kStunServerAddress =
3819 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3820 StartStunServer(kStunServerAddress);
3821
3822 PeerConnectionInterface::RTCConfiguration config;
3823 PeerConnectionInterface::IceServer ice_stun_server;
3824 ice_stun_server.urls.push_back(
3825 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3826 kStunServerAddress.PortAsString());
3827 config.servers.push_back(ice_stun_server);
3828
3829 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3830 ConnectFakeSignaling();
3831 SetPortAllocatorFlags();
3832 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003833 caller()->AddAudioVideoTracks();
3834 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003835
3836 // Initial state before anything happens.
3837 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3838 caller()->ice_gathering_state());
3839 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3840 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003841 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3842 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003843
3844 // Start the call by creating the offer, setting it as the local description,
3845 // then sending it to the peer who will respond with an answer. This happens
3846 // asynchronously so that we can watch the states as it runs in the
3847 // background.
3848 caller()->CreateAndSetAndSignalOffer();
3849
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003850 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3851 caller()->ice_connection_state());
Jonas Olssondf919fb2019-01-22 11:21:00 +01003852 ASSERT_EQ(PeerConnectionInterface::kIceConnectionConnected,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003853 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003854
3855 // Verify that the observer was notified of the intermediate transitions.
3856 EXPECT_THAT(caller()->ice_connection_state_history(),
3857 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3858 PeerConnectionInterface::kIceConnectionConnected,
3859 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003860 EXPECT_THAT(
3861 caller()->peer_connection_state_history(),
3862 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003863 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003864 EXPECT_THAT(caller()->ice_gathering_state_history(),
3865 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3866 PeerConnectionInterface::kIceGatheringComplete));
3867
3868 // Block connections to/from the caller and wait for ICE to become
3869 // disconnected.
3870 for (const auto& caller_address : CallerAddresses()) {
3871 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3872 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003873 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003874 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3875 caller()->ice_connection_state(), kDefaultTimeout);
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003876 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3877 caller()->standardized_ice_connection_state(),
3878 kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003879
3880 // Let ICE re-establish by removing the firewall rules.
3881 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003882 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003883 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3884 caller()->ice_connection_state(), kDefaultTimeout);
Jonas Olssondf919fb2019-01-22 11:21:00 +01003885 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionConnected,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003886 caller()->standardized_ice_connection_state(),
3887 kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003888
3889 // According to RFC7675, if there is no response within 30 seconds then the
3890 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003891 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003892 constexpr int kConsentTimeout = 30000;
3893 for (const auto& caller_address : CallerAddresses()) {
3894 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3895 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003896 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003897 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3898 caller()->ice_connection_state(), kConsentTimeout);
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003899 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3900 caller()->standardized_ice_connection_state(),
3901 kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003902}
3903
3904// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3905// and that the statistics in the metric observers are updated correctly.
3906TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3907 ASSERT_TRUE(CreatePeerConnectionWrappers());
3908 ConnectFakeSignaling();
3909 SetPortAllocatorFlags();
3910 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003911 caller()->AddAudioVideoTracks();
3912 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003913 caller()->CreateAndSetAndSignalOffer();
3914
3915 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3916
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003917 // TODO(bugs.webrtc.org/9456): Fix it.
3918 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3919 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3920 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3921 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003922 if (TestIPv6()) {
3923 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3924 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003925 EXPECT_EQ(0, num_best_ipv4);
3926 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003927 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003928 EXPECT_EQ(1, num_best_ipv4);
3929 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003930 }
3931
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003932 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3933 "WebRTC.PeerConnection.CandidatePairType_UDP",
3934 webrtc::kIceCandidatePairHostHost));
3935 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3936 "WebRTC.PeerConnection.CandidatePairType_UDP",
3937 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003938}
3939
3940constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3941 cricket::PORTALLOCATOR_DISABLE_STUN |
3942 cricket::PORTALLOCATOR_DISABLE_RELAY;
3943constexpr uint32_t kFlagsIPv6NoStun =
3944 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3945 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3946constexpr uint32_t kFlagsIPv4Stun =
3947 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3948
Seth Hampson2f0d7022018-02-20 11:54:42 -08003949INSTANTIATE_TEST_CASE_P(
3950 PeerConnectionIntegrationTest,
3951 PeerConnectionIntegrationIceStatesTest,
3952 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3953 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3954 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3955 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003956
deadbeef1dcb1642017-03-29 21:08:16 -07003957// This test sets up a call between two parties with audio and video.
3958// During the call, the caller restarts ICE and the test verifies that
3959// new ICE candidates are generated and audio and video still can flow, and the
3960// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003961TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003962 ASSERT_TRUE(CreatePeerConnectionWrappers());
3963 ConnectFakeSignaling();
3964 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003965 caller()->AddAudioVideoTracks();
3966 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003967 caller()->CreateAndSetAndSignalOffer();
3968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3969 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3970 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003971 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3972 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07003973
3974 // To verify that the ICE restart actually occurs, get
3975 // ufrag/password/candidates before and after restart.
3976 // Create an SDP string of the first audio candidate for both clients.
3977 const webrtc::IceCandidateCollection* audio_candidates_caller =
3978 caller()->pc()->local_description()->candidates(0);
3979 const webrtc::IceCandidateCollection* audio_candidates_callee =
3980 callee()->pc()->local_description()->candidates(0);
3981 ASSERT_GT(audio_candidates_caller->count(), 0u);
3982 ASSERT_GT(audio_candidates_callee->count(), 0u);
3983 std::string caller_candidate_pre_restart;
3984 ASSERT_TRUE(
3985 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3986 std::string callee_candidate_pre_restart;
3987 ASSERT_TRUE(
3988 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3989 const cricket::SessionDescription* desc =
3990 caller()->pc()->local_description()->description();
3991 std::string caller_ufrag_pre_restart =
3992 desc->transport_infos()[0].description.ice_ufrag;
3993 desc = callee()->pc()->local_description()->description();
3994 std::string callee_ufrag_pre_restart =
3995 desc->transport_infos()[0].description.ice_ufrag;
3996
3997 // Have the caller initiate an ICE restart.
3998 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3999 caller()->CreateAndSetAndSignalOffer();
4000 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4001 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4002 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004003 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004004 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4005
4006 // Grab the ufrags/candidates again.
4007 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4008 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4009 ASSERT_GT(audio_candidates_caller->count(), 0u);
4010 ASSERT_GT(audio_candidates_callee->count(), 0u);
4011 std::string caller_candidate_post_restart;
4012 ASSERT_TRUE(
4013 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4014 std::string callee_candidate_post_restart;
4015 ASSERT_TRUE(
4016 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4017 desc = caller()->pc()->local_description()->description();
4018 std::string caller_ufrag_post_restart =
4019 desc->transport_infos()[0].description.ice_ufrag;
4020 desc = callee()->pc()->local_description()->description();
4021 std::string callee_ufrag_post_restart =
4022 desc->transport_infos()[0].description.ice_ufrag;
4023 // Sanity check that an ICE restart was actually negotiated in SDP.
4024 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4025 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4026 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4027 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4028
4029 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004030 MediaExpectations media_expectations;
4031 media_expectations.ExpectBidirectionalAudioAndVideo();
4032 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004033}
4034
4035// Verify that audio/video can be received end-to-end when ICE renomination is
4036// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004037TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004038 PeerConnectionInterface::RTCConfiguration config;
4039 config.enable_ice_renomination = true;
4040 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4041 ConnectFakeSignaling();
4042 // Do normal offer/answer and wait for some frames to be received in each
4043 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004044 caller()->AddAudioVideoTracks();
4045 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004046 caller()->CreateAndSetAndSignalOffer();
4047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4048 // Sanity check that ICE renomination was actually negotiated.
4049 const cricket::SessionDescription* desc =
4050 caller()->pc()->local_description()->description();
4051 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004052 ASSERT_NE(
4053 info.description.transport_options.end(),
4054 std::find(info.description.transport_options.begin(),
4055 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004056 }
4057 desc = callee()->pc()->local_description()->description();
4058 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004059 ASSERT_NE(
4060 info.description.transport_options.end(),
4061 std::find(info.description.transport_options.begin(),
4062 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004063 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004064 MediaExpectations media_expectations;
4065 media_expectations.ExpectBidirectionalAudioAndVideo();
4066 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004067}
4068
Steve Anton6f25b092017-10-23 09:39:20 -07004069// With a max bundle policy and RTCP muxing, adding a new media description to
4070// the connection should not affect ICE at all because the new media will use
4071// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004072TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004073 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004074 PeerConnectionInterface::RTCConfiguration config;
4075 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4076 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4077 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4078 config, PeerConnectionInterface::RTCConfiguration()));
4079 ConnectFakeSignaling();
4080
Steve Anton15324772018-01-16 10:26:49 -08004081 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004082 caller()->CreateAndSetAndSignalOffer();
4083 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004084 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4085 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004086
4087 caller()->clear_ice_connection_state_history();
4088
Steve Anton15324772018-01-16 10:26:49 -08004089 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004090 caller()->CreateAndSetAndSignalOffer();
4091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4092
4093 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4094}
4095
deadbeef1dcb1642017-03-29 21:08:16 -07004096// This test sets up a call between two parties with audio and video. It then
4097// renegotiates setting the video m-line to "port 0", then later renegotiates
4098// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004099TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004100 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4101 ASSERT_TRUE(CreatePeerConnectionWrappers());
4102 ConnectFakeSignaling();
4103
4104 // Do initial negotiation, only sending media from the caller. Will result in
4105 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004106 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004107 caller()->CreateAndSetAndSignalOffer();
4108 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4109
4110 // Negotiate again, disabling the video "m=" section (the callee will set the
4111 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004112 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4113 PeerConnectionInterface::RTCOfferAnswerOptions options;
4114 options.offer_to_receive_video = 0;
4115 callee()->SetOfferAnswerOptions(options);
4116 } else {
4117 callee()->SetRemoteOfferHandler([this] {
4118 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4119 });
4120 }
deadbeef1dcb1642017-03-29 21:08:16 -07004121 caller()->CreateAndSetAndSignalOffer();
4122 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4123 // Sanity check that video "m=" section was actually rejected.
4124 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4125 callee()->pc()->local_description()->description());
4126 ASSERT_NE(nullptr, answer_video_content);
4127 ASSERT_TRUE(answer_video_content->rejected);
4128
4129 // Enable video and do negotiation again, making sure video is received
4130 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004131 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4132 PeerConnectionInterface::RTCOfferAnswerOptions options;
4133 options.offer_to_receive_video = 1;
4134 callee()->SetOfferAnswerOptions(options);
4135 } else {
4136 // The caller's transceiver is stopped, so we need to add another track.
4137 auto caller_transceiver =
4138 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4139 EXPECT_TRUE(caller_transceiver->stopped());
4140 caller()->AddVideoTrack();
4141 }
4142 callee()->AddVideoTrack();
4143 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004144 caller()->CreateAndSetAndSignalOffer();
4145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004146
deadbeef1dcb1642017-03-29 21:08:16 -07004147 // Verify the caller receives frames from the newly added stream, and the
4148 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004149 MediaExpectations media_expectations;
4150 media_expectations.CalleeExpectsSomeAudio();
4151 media_expectations.ExpectBidirectionalVideo();
4152 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004153}
4154
deadbeef1dcb1642017-03-29 21:08:16 -07004155// This tests that if we negotiate after calling CreateSender but before we
4156// have a track, then set a track later, frames from the newly-set track are
4157// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004158TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004159 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4160 ASSERT_TRUE(CreatePeerConnectionWrappers());
4161 ConnectFakeSignaling();
4162 auto caller_audio_sender =
4163 caller()->pc()->CreateSender("audio", "caller_stream");
4164 auto caller_video_sender =
4165 caller()->pc()->CreateSender("video", "caller_stream");
4166 auto callee_audio_sender =
4167 callee()->pc()->CreateSender("audio", "callee_stream");
4168 auto callee_video_sender =
4169 callee()->pc()->CreateSender("video", "callee_stream");
4170 caller()->CreateAndSetAndSignalOffer();
4171 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4172 // Wait for ICE to complete, without any tracks being set.
4173 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4174 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4175 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4176 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4177 // Now set the tracks, and expect frames to immediately start flowing.
4178 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4179 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4180 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4181 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182 MediaExpectations media_expectations;
4183 media_expectations.ExpectBidirectionalAudioAndVideo();
4184 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4185}
4186
4187// This tests that if we negotiate after calling AddTransceiver but before we
4188// have a track, then set a track later, frames from the newly-set tracks are
4189// received end-to-end.
4190TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4191 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4192 ASSERT_TRUE(CreatePeerConnectionWrappers());
4193 ConnectFakeSignaling();
4194 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4195 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4196 auto caller_audio_sender = audio_result.MoveValue()->sender();
4197 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4198 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4199 auto caller_video_sender = video_result.MoveValue()->sender();
4200 callee()->SetRemoteOfferHandler([this] {
4201 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4202 callee()->pc()->GetTransceivers()[0]->SetDirection(
4203 RtpTransceiverDirection::kSendRecv);
4204 callee()->pc()->GetTransceivers()[1]->SetDirection(
4205 RtpTransceiverDirection::kSendRecv);
4206 });
4207 caller()->CreateAndSetAndSignalOffer();
4208 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4209 // Wait for ICE to complete, without any tracks being set.
4210 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4211 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4212 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4213 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4214 // Now set the tracks, and expect frames to immediately start flowing.
4215 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4216 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4217 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4218 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4219 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4220 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4221 MediaExpectations media_expectations;
4222 media_expectations.ExpectBidirectionalAudioAndVideo();
4223 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004224}
4225
4226// This test verifies that a remote video track can be added via AddStream,
4227// and sent end-to-end. For this particular test, it's simply echoed back
4228// from the caller to the callee, rather than being forwarded to a third
4229// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004230TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004231 ASSERT_TRUE(CreatePeerConnectionWrappers());
4232 ConnectFakeSignaling();
4233 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004234 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004235 caller()->CreateAndSetAndSignalOffer();
4236 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004237 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004238
4239 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4240 // time).
4241 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4242 callee()->CreateAndSetAndSignalOffer();
4243 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4244
Seth Hampson2f0d7022018-02-20 11:54:42 -08004245 MediaExpectations media_expectations;
4246 media_expectations.ExpectBidirectionalVideo();
4247 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004248}
4249
4250// Test that we achieve the expected end-to-end connection time, using a
4251// fake clock and simulated latency on the media and signaling paths.
4252// We use a TURN<->TURN connection because this is usually the quickest to
4253// set up initially, especially when we're confident the connection will work
4254// and can start sending media before we get a STUN response.
4255//
4256// With various optimizations enabled, here are the network delays we expect to
4257// be on the critical path:
4258// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4259// signaling answer (with DTLS fingerprint).
4260// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4261// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4262// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004263TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004264 rtc::ScopedFakeClock fake_clock;
4265 // Some things use a time of "0" as a special value, so we need to start out
4266 // the fake clock at a nonzero time.
4267 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004268 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004269
4270 static constexpr int media_hop_delay_ms = 50;
4271 static constexpr int signaling_trip_delay_ms = 500;
4272 // For explanation of these values, see comment above.
4273 static constexpr int required_media_hops = 9;
4274 static constexpr int required_signaling_trips = 2;
4275 // For internal delays (such as posting an event asychronously).
4276 static constexpr int allowed_internal_delay_ms = 20;
4277 static constexpr int total_connection_time_ms =
4278 media_hop_delay_ms * required_media_hops +
4279 signaling_trip_delay_ms * required_signaling_trips +
4280 allowed_internal_delay_ms;
4281
4282 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4283 3478};
4284 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4285 0};
4286 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4287 3478};
4288 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4289 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004290 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4291 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004292
Seth Hampsonaed71642018-06-11 07:41:32 -07004293 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4294 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004295 // Bypass permission check on received packets so media can be sent before
4296 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004297 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4298 turn_server_1->set_enable_permission_checks(false);
4299 });
4300 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4301 turn_server_2->set_enable_permission_checks(false);
4302 });
deadbeef1dcb1642017-03-29 21:08:16 -07004303
4304 PeerConnectionInterface::RTCConfiguration client_1_config;
4305 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4306 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4307 ice_server_1.username = "test";
4308 ice_server_1.password = "test";
4309 client_1_config.servers.push_back(ice_server_1);
4310 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4311 client_1_config.presume_writable_when_fully_relayed = true;
4312
4313 PeerConnectionInterface::RTCConfiguration client_2_config;
4314 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4315 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4316 ice_server_2.username = "test";
4317 ice_server_2.password = "test";
4318 client_2_config.servers.push_back(ice_server_2);
4319 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4320 client_2_config.presume_writable_when_fully_relayed = true;
4321
4322 ASSERT_TRUE(
4323 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4324 // Set up the simulated delays.
4325 SetSignalingDelayMs(signaling_trip_delay_ms);
4326 ConnectFakeSignaling();
4327 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4328 virtual_socket_server()->UpdateDelayDistribution();
4329
4330 // Set "offer to receive audio/video" without adding any tracks, so we just
4331 // set up ICE/DTLS with no media.
4332 PeerConnectionInterface::RTCOfferAnswerOptions options;
4333 options.offer_to_receive_audio = 1;
4334 options.offer_to_receive_video = 1;
4335 caller()->SetOfferAnswerOptions(options);
4336 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004337 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4338 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004339 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4340 // If this is not done a DCHECK can be hit in ports.cc, because a large
4341 // negative number is calculated for the rtt due to the global clock changing.
4342 caller()->pc()->Close();
4343 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004344}
4345
Jonas Orelandbdcee282017-10-10 14:01:40 +02004346// Verify that a TurnCustomizer passed in through RTCConfiguration
4347// is actually used by the underlying TURN candidate pair.
4348// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004349TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004350 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4351 3478};
4352 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4353 0};
4354 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4355 3478};
4356 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4357 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004358 CreateTurnServer(turn_server_1_internal_address,
4359 turn_server_1_external_address);
4360 CreateTurnServer(turn_server_2_internal_address,
4361 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004362
4363 PeerConnectionInterface::RTCConfiguration client_1_config;
4364 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4365 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4366 ice_server_1.username = "test";
4367 ice_server_1.password = "test";
4368 client_1_config.servers.push_back(ice_server_1);
4369 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004370 auto* customizer1 = CreateTurnCustomizer();
4371 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004372
4373 PeerConnectionInterface::RTCConfiguration client_2_config;
4374 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4375 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4376 ice_server_2.username = "test";
4377 ice_server_2.password = "test";
4378 client_2_config.servers.push_back(ice_server_2);
4379 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004380 auto* customizer2 = CreateTurnCustomizer();
4381 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004382
4383 ASSERT_TRUE(
4384 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4385 ConnectFakeSignaling();
4386
4387 // Set "offer to receive audio/video" without adding any tracks, so we just
4388 // set up ICE/DTLS with no media.
4389 PeerConnectionInterface::RTCOfferAnswerOptions options;
4390 options.offer_to_receive_audio = 1;
4391 options.offer_to_receive_video = 1;
4392 caller()->SetOfferAnswerOptions(options);
4393 caller()->CreateAndSetAndSignalOffer();
4394 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4395
Seth Hampsonaed71642018-06-11 07:41:32 -07004396 ExpectTurnCustomizerCountersIncremented(customizer1);
4397 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004398}
4399
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004400// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4401// send media between the caller and the callee.
4402TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4403 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4404 3478};
4405 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4406
4407 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004408 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4409 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004410
4411 webrtc::PeerConnectionInterface::IceServer ice_server;
4412 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4413 ice_server.username = "test";
4414 ice_server.password = "test";
4415
4416 PeerConnectionInterface::RTCConfiguration client_1_config;
4417 client_1_config.servers.push_back(ice_server);
4418 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4419
4420 PeerConnectionInterface::RTCConfiguration client_2_config;
4421 client_2_config.servers.push_back(ice_server);
4422 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4423
4424 ASSERT_TRUE(
4425 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4426
4427 // Do normal offer/answer and wait for ICE to complete.
4428 ConnectFakeSignaling();
4429 caller()->AddAudioVideoTracks();
4430 callee()->AddAudioVideoTracks();
4431 caller()->CreateAndSetAndSignalOffer();
4432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4433 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4434 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4435
4436 MediaExpectations media_expectations;
4437 media_expectations.ExpectBidirectionalAudioAndVideo();
4438 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4439}
4440
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004441// Verify that a SSLCertificateVerifier passed in through
4442// PeerConnectionDependencies is actually used by the underlying SSL
4443// implementation to determine whether a certificate presented by the TURN
4444// server is accepted by the client. Note that openssladapter_unittest.cc
4445// contains more detailed, lower-level tests.
4446TEST_P(PeerConnectionIntegrationTest,
4447 SSLCertificateVerifierUsedForTurnConnections) {
4448 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4449 3478};
4450 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4451
4452 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4453 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004454 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4455 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004456
4457 webrtc::PeerConnectionInterface::IceServer ice_server;
4458 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4459 ice_server.username = "test";
4460 ice_server.password = "test";
4461
4462 PeerConnectionInterface::RTCConfiguration client_1_config;
4463 client_1_config.servers.push_back(ice_server);
4464 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4465
4466 PeerConnectionInterface::RTCConfiguration client_2_config;
4467 client_2_config.servers.push_back(ice_server);
4468 // Setting the type to kRelay forces the connection to go through a TURN
4469 // server.
4470 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4471
4472 // Get a copy to the pointer so we can verify calls later.
4473 rtc::TestCertificateVerifier* client_1_cert_verifier =
4474 new rtc::TestCertificateVerifier();
4475 client_1_cert_verifier->verify_certificate_ = true;
4476 rtc::TestCertificateVerifier* client_2_cert_verifier =
4477 new rtc::TestCertificateVerifier();
4478 client_2_cert_verifier->verify_certificate_ = true;
4479
4480 // Create the dependencies with the test certificate verifier.
4481 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4482 client_1_deps.tls_cert_verifier =
4483 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4484 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4485 client_2_deps.tls_cert_verifier =
4486 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4487
4488 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4489 client_1_config, std::move(client_1_deps), client_2_config,
4490 std::move(client_2_deps)));
4491 ConnectFakeSignaling();
4492
4493 // Set "offer to receive audio/video" without adding any tracks, so we just
4494 // set up ICE/DTLS with no media.
4495 PeerConnectionInterface::RTCOfferAnswerOptions options;
4496 options.offer_to_receive_audio = 1;
4497 options.offer_to_receive_video = 1;
4498 caller()->SetOfferAnswerOptions(options);
4499 caller()->CreateAndSetAndSignalOffer();
4500 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4501
4502 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4503 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004504}
4505
4506TEST_P(PeerConnectionIntegrationTest,
4507 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4508 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4509 3478};
4510 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4511
4512 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4513 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004514 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4515 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004516
4517 webrtc::PeerConnectionInterface::IceServer ice_server;
4518 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4519 ice_server.username = "test";
4520 ice_server.password = "test";
4521
4522 PeerConnectionInterface::RTCConfiguration client_1_config;
4523 client_1_config.servers.push_back(ice_server);
4524 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4525
4526 PeerConnectionInterface::RTCConfiguration client_2_config;
4527 client_2_config.servers.push_back(ice_server);
4528 // Setting the type to kRelay forces the connection to go through a TURN
4529 // server.
4530 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4531
4532 // Get a copy to the pointer so we can verify calls later.
4533 rtc::TestCertificateVerifier* client_1_cert_verifier =
4534 new rtc::TestCertificateVerifier();
4535 client_1_cert_verifier->verify_certificate_ = false;
4536 rtc::TestCertificateVerifier* client_2_cert_verifier =
4537 new rtc::TestCertificateVerifier();
4538 client_2_cert_verifier->verify_certificate_ = false;
4539
4540 // Create the dependencies with the test certificate verifier.
4541 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4542 client_1_deps.tls_cert_verifier =
4543 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4544 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4545 client_2_deps.tls_cert_verifier =
4546 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4547
4548 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4549 client_1_config, std::move(client_1_deps), client_2_config,
4550 std::move(client_2_deps)));
4551 ConnectFakeSignaling();
4552
4553 // Set "offer to receive audio/video" without adding any tracks, so we just
4554 // set up ICE/DTLS with no media.
4555 PeerConnectionInterface::RTCOfferAnswerOptions options;
4556 options.offer_to_receive_audio = 1;
4557 options.offer_to_receive_video = 1;
4558 caller()->SetOfferAnswerOptions(options);
4559 caller()->CreateAndSetAndSignalOffer();
4560 bool wait_res = true;
4561 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4562 // properly, should be able to just wait for a state of "failed" instead of
4563 // waiting a fixed 10 seconds.
4564 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4565 ASSERT_FALSE(wait_res);
4566
4567 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4568 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004569}
4570
deadbeefc964d0b2017-04-03 10:03:35 -07004571// Test that audio and video flow end-to-end when codec names don't use the
4572// expected casing, given that they're supposed to be case insensitive. To test
4573// this, all but one codec is removed from each media description, and its
4574// casing is changed.
4575//
4576// In the past, this has regressed and caused crashes/black video, due to the
4577// fact that code at some layers was doing case-insensitive comparisons and
4578// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004579TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004580 ASSERT_TRUE(CreatePeerConnectionWrappers());
4581 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004582 caller()->AddAudioVideoTracks();
4583 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004584
4585 // Remove all but one audio/video codec (opus and VP8), and change the
4586 // casing of the caller's generated offer.
4587 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4588 cricket::AudioContentDescription* audio =
4589 GetFirstAudioContentDescription(description);
4590 ASSERT_NE(nullptr, audio);
4591 auto audio_codecs = audio->codecs();
4592 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4593 [](const cricket::AudioCodec& codec) {
4594 return codec.name != "opus";
4595 }),
4596 audio_codecs.end());
4597 ASSERT_EQ(1u, audio_codecs.size());
4598 audio_codecs[0].name = "OpUs";
4599 audio->set_codecs(audio_codecs);
4600
4601 cricket::VideoContentDescription* video =
4602 GetFirstVideoContentDescription(description);
4603 ASSERT_NE(nullptr, video);
4604 auto video_codecs = video->codecs();
4605 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4606 [](const cricket::VideoCodec& codec) {
4607 return codec.name != "VP8";
4608 }),
4609 video_codecs.end());
4610 ASSERT_EQ(1u, video_codecs.size());
4611 video_codecs[0].name = "vP8";
4612 video->set_codecs(video_codecs);
4613 });
4614
4615 caller()->CreateAndSetAndSignalOffer();
4616 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4617
4618 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004619 MediaExpectations media_expectations;
4620 media_expectations.ExpectBidirectionalAudioAndVideo();
4621 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004622}
4623
Jonas Oreland49ac5952018-09-26 16:04:32 +02004624TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004625 ASSERT_TRUE(CreatePeerConnectionWrappers());
4626 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004627 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004628 caller()->CreateAndSetAndSignalOffer();
4629 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004630 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004631 MediaExpectations media_expectations;
4632 media_expectations.CalleeExpectsSomeAudio(1);
4633 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004634 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004635 auto receiver = callee()->pc()->GetReceivers()[0];
4636 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004637 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004638 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4639 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004640 sources[0].source_id());
4641 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4642}
4643
4644TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4645 ASSERT_TRUE(CreatePeerConnectionWrappers());
4646 ConnectFakeSignaling();
4647 caller()->AddVideoTrack();
4648 caller()->CreateAndSetAndSignalOffer();
4649 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4650 // Wait for one video frame to be received by the callee.
4651 MediaExpectations media_expectations;
4652 media_expectations.CalleeExpectsSomeVideo(1);
4653 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4654 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4655 auto receiver = callee()->pc()->GetReceivers()[0];
4656 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4657 auto sources = receiver->GetSources();
4658 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4659 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4660 sources[0].source_id());
4661 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004662}
4663
deadbeef2f425aa2017-04-14 10:41:32 -07004664// Test that if a track is removed and added again with a different stream ID,
4665// the new stream ID is successfully communicated in SDP and media continues to
4666// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004667// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4668// it will not reuse a transceiver that has already been sending. After creating
4669// a new transceiver it tries to create an offer with two senders of the same
4670// track ids and it fails.
4671TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004672 ASSERT_TRUE(CreatePeerConnectionWrappers());
4673 ConnectFakeSignaling();
4674
deadbeef2f425aa2017-04-14 10:41:32 -07004675 // Add track using stream 1, do offer/answer.
4676 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4677 caller()->CreateLocalAudioTrack();
4678 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004679 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004680 caller()->CreateAndSetAndSignalOffer();
4681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004682 {
4683 MediaExpectations media_expectations;
4684 media_expectations.CalleeExpectsSomeAudio(1);
4685 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4686 }
deadbeef2f425aa2017-04-14 10:41:32 -07004687 // Remove the sender, and create a new one with the new stream.
4688 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004689 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004690 caller()->CreateAndSetAndSignalOffer();
4691 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4692 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004693 {
4694 MediaExpectations media_expectations;
4695 media_expectations.CalleeExpectsSomeAudio();
4696 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4697 }
deadbeef2f425aa2017-04-14 10:41:32 -07004698}
4699
Seth Hampson2f0d7022018-02-20 11:54:42 -08004700TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004701 ASSERT_TRUE(CreatePeerConnectionWrappers());
4702 ConnectFakeSignaling();
4703
Karl Wiberg918f50c2018-07-05 11:40:33 +02004704 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004705 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4706 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4707 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004708 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4709 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004710
Steve Anton15324772018-01-16 10:26:49 -08004711 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004712 caller()->CreateAndSetAndSignalOffer();
4713 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4714}
4715
Steve Antonede9ca52017-10-16 13:04:27 -07004716// Test that if candidates are only signaled by applying full session
4717// descriptions (instead of using AddIceCandidate), the peers can connect to
4718// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004719TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004720 ASSERT_TRUE(CreatePeerConnectionWrappers());
4721 // Each side will signal the session descriptions but not candidates.
4722 ConnectFakeSignalingForSdpOnly();
4723
4724 // Add audio video track and exchange the initial offer/answer with media
4725 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004726 caller()->AddAudioVideoTracks();
4727 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004728 caller()->CreateAndSetAndSignalOffer();
4729
4730 // Wait for all candidates to be gathered on both the caller and callee.
4731 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4732 caller()->ice_gathering_state(), kDefaultTimeout);
4733 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4734 callee()->ice_gathering_state(), kDefaultTimeout);
4735
4736 // The candidates will now be included in the session description, so
4737 // signaling them will start the ICE connection.
4738 caller()->CreateAndSetAndSignalOffer();
4739 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4740
4741 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004742 MediaExpectations media_expectations;
4743 media_expectations.ExpectBidirectionalAudioAndVideo();
4744 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004745}
4746
henrika5f6bf242017-11-01 11:06:56 +01004747// Test that SetAudioPlayout can be used to disable audio playout from the
4748// start, then later enable it. This may be useful, for example, if the caller
4749// needs to play a local ringtone until some event occurs, after which it
4750// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004751TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004752 ASSERT_TRUE(CreatePeerConnectionWrappers());
4753 ConnectFakeSignaling();
4754
4755 // Set up audio-only call where audio playout is disabled on caller's side.
4756 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004757 caller()->AddAudioTrack();
4758 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004759 caller()->CreateAndSetAndSignalOffer();
4760 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4761
4762 // Pump messages for a second.
4763 WAIT(false, 1000);
4764 // Since audio playout is disabled, the caller shouldn't have received
4765 // anything (at the playout level, at least).
4766 EXPECT_EQ(0, caller()->audio_frames_received());
4767 // As a sanity check, make sure the callee (for which playout isn't disabled)
4768 // did still see frames on its audio level.
4769 ASSERT_GT(callee()->audio_frames_received(), 0);
4770
4771 // Enable playout again, and ensure audio starts flowing.
4772 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004773 MediaExpectations media_expectations;
4774 media_expectations.ExpectBidirectionalAudio();
4775 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004776}
4777
4778double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4779 auto report = pc->NewGetStats();
4780 auto track_stats_list =
4781 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4782 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4783 for (const auto* track_stats : track_stats_list) {
4784 if (track_stats->remote_source.is_defined() &&
4785 *track_stats->remote_source) {
4786 remote_track_stats = track_stats;
4787 break;
4788 }
4789 }
4790
4791 if (!remote_track_stats->total_audio_energy.is_defined()) {
4792 return 0.0;
4793 }
4794 return *remote_track_stats->total_audio_energy;
4795}
4796
4797// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4798// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004799TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004800 DisableAudioPlayoutStillGeneratesAudioStats) {
4801 ASSERT_TRUE(CreatePeerConnectionWrappers());
4802 ConnectFakeSignaling();
4803
4804 // Set up audio-only call where playout is disabled but audio-processing is
4805 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004806 caller()->AddAudioTrack();
4807 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004808 caller()->pc()->SetAudioPlayout(false);
4809
4810 caller()->CreateAndSetAndSignalOffer();
4811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4812
4813 // Wait for the callee to receive audio stats.
4814 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4815}
4816
henrika4f167df2017-11-01 14:45:55 +01004817// Test that SetAudioRecording can be used to disable audio recording from the
4818// start, then later enable it. This may be useful, for example, if the caller
4819// wants to ensure that no audio resources are active before a certain state
4820// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004821TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004822 ASSERT_TRUE(CreatePeerConnectionWrappers());
4823 ConnectFakeSignaling();
4824
4825 // Set up audio-only call where audio recording is disabled on caller's side.
4826 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004827 caller()->AddAudioTrack();
4828 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004829 caller()->CreateAndSetAndSignalOffer();
4830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4831
4832 // Pump messages for a second.
4833 WAIT(false, 1000);
4834 // Since caller has disabled audio recording, the callee shouldn't have
4835 // received anything.
4836 EXPECT_EQ(0, callee()->audio_frames_received());
4837 // As a sanity check, make sure the caller did still see frames on its
4838 // audio level since audio recording is enabled on the calle side.
4839 ASSERT_GT(caller()->audio_frames_received(), 0);
4840
4841 // Enable audio recording again, and ensure audio starts flowing.
4842 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004843 MediaExpectations media_expectations;
4844 media_expectations.ExpectBidirectionalAudio();
4845 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004846}
4847
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004848// Test that after closing PeerConnections, they stop sending any packets (ICE,
4849// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004850TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004851 // Set up audio/video/data, wait for some frames to be received.
4852 ASSERT_TRUE(CreatePeerConnectionWrappers());
4853 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004854 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004855#ifdef HAVE_SCTP
4856 caller()->CreateDataChannel();
4857#endif
4858 caller()->CreateAndSetAndSignalOffer();
4859 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004860 MediaExpectations media_expectations;
4861 media_expectations.CalleeExpectsSomeAudioAndVideo();
4862 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004863 // Close PeerConnections.
4864 caller()->pc()->Close();
4865 callee()->pc()->Close();
4866 // Pump messages for a second, and ensure no new packets end up sent.
4867 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4868 WAIT(false, 1000);
4869 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4870 EXPECT_EQ(sent_packets_a, sent_packets_b);
4871}
4872
Steve Anton7eca0932018-03-30 15:18:41 -07004873// Test that transport stats are generated by the RTCStatsCollector for a
4874// connection that only involves data channels. This is a regression test for
4875// crbug.com/826972.
4876#ifdef HAVE_SCTP
4877TEST_P(PeerConnectionIntegrationTest,
4878 TransportStatsReportedForDataChannelOnlyConnection) {
4879 ASSERT_TRUE(CreatePeerConnectionWrappers());
4880 ConnectFakeSignaling();
4881 caller()->CreateDataChannel();
4882
4883 caller()->CreateAndSetAndSignalOffer();
4884 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4885 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4886
4887 auto caller_report = caller()->NewGetStats();
4888 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4889 auto callee_report = callee()->NewGetStats();
4890 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4891}
4892#endif // HAVE_SCTP
4893
Qingsi Wang7685e862018-06-11 20:15:46 -07004894TEST_P(PeerConnectionIntegrationTest,
4895 IceEventsGeneratedAndLoggedInRtcEventLog) {
4896 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4897 ConnectFakeSignaling();
4898 PeerConnectionInterface::RTCOfferAnswerOptions options;
4899 options.offer_to_receive_audio = 1;
4900 caller()->SetOfferAnswerOptions(options);
4901 caller()->CreateAndSetAndSignalOffer();
4902 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4903 ASSERT_NE(nullptr, caller()->event_log_factory());
4904 ASSERT_NE(nullptr, callee()->event_log_factory());
4905 webrtc::FakeRtcEventLog* caller_event_log =
4906 static_cast<webrtc::FakeRtcEventLog*>(
4907 caller()->event_log_factory()->last_log_created());
4908 webrtc::FakeRtcEventLog* callee_event_log =
4909 static_cast<webrtc::FakeRtcEventLog*>(
4910 callee()->event_log_factory()->last_log_created());
4911 ASSERT_NE(nullptr, caller_event_log);
4912 ASSERT_NE(nullptr, callee_event_log);
4913 int caller_ice_config_count = caller_event_log->GetEventCount(
4914 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4915 int caller_ice_event_count = caller_event_log->GetEventCount(
4916 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4917 int callee_ice_config_count = callee_event_log->GetEventCount(
4918 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4919 int callee_ice_event_count = callee_event_log->GetEventCount(
4920 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4921 EXPECT_LT(0, caller_ice_config_count);
4922 EXPECT_LT(0, caller_ice_event_count);
4923 EXPECT_LT(0, callee_ice_config_count);
4924 EXPECT_LT(0, callee_ice_event_count);
4925}
4926
Seth Hampson2f0d7022018-02-20 11:54:42 -08004927INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4928 PeerConnectionIntegrationTest,
4929 Values(SdpSemantics::kPlanB,
4930 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004931
Steve Anton74255ff2018-01-24 18:32:57 -08004932// Tests that verify interoperability between Plan B and Unified Plan
4933// PeerConnections.
4934class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004935 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004936 public ::testing::WithParamInterface<
4937 std::tuple<SdpSemantics, SdpSemantics>> {
4938 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004939 // Setting the SdpSemantics for the base test to kDefault does not matter
4940 // because we specify not to use the test semantics when creating
4941 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004942 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004943 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004944 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004945 callee_semantics_(std::get<1>(GetParam())) {}
4946
4947 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004948 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4949 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004950 }
4951
4952 const SdpSemantics caller_semantics_;
4953 const SdpSemantics callee_semantics_;
4954};
4955
4956TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4957 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4958 ConnectFakeSignaling();
4959
4960 caller()->CreateAndSetAndSignalOffer();
4961 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4962}
4963
4964TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4965 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4966 ConnectFakeSignaling();
4967 auto audio_sender = caller()->AddAudioTrack();
4968
4969 caller()->CreateAndSetAndSignalOffer();
4970 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4971
4972 // Verify that one audio receiver has been created on the remote and that it
4973 // has the same track ID as the sending track.
4974 auto receivers = callee()->pc()->GetReceivers();
4975 ASSERT_EQ(1u, receivers.size());
4976 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4977 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4978
Seth Hampson2f0d7022018-02-20 11:54:42 -08004979 MediaExpectations media_expectations;
4980 media_expectations.CalleeExpectsSomeAudio();
4981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004982}
4983
4984TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4985 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4986 ConnectFakeSignaling();
4987 auto video_sender = caller()->AddVideoTrack();
4988 auto audio_sender = caller()->AddAudioTrack();
4989
4990 caller()->CreateAndSetAndSignalOffer();
4991 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4992
4993 // Verify that one audio and one video receiver have been created on the
4994 // remote and that they have the same track IDs as the sending tracks.
4995 auto audio_receivers =
4996 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4997 ASSERT_EQ(1u, audio_receivers.size());
4998 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4999 auto video_receivers =
5000 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5001 ASSERT_EQ(1u, video_receivers.size());
5002 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5003
Seth Hampson2f0d7022018-02-20 11:54:42 -08005004 MediaExpectations media_expectations;
5005 media_expectations.CalleeExpectsSomeAudioAndVideo();
5006 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005007}
5008
5009TEST_P(PeerConnectionIntegrationInteropTest,
5010 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5011 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5012 ConnectFakeSignaling();
5013 caller()->AddAudioVideoTracks();
5014 callee()->AddAudioVideoTracks();
5015
5016 caller()->CreateAndSetAndSignalOffer();
5017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5018
Seth Hampson2f0d7022018-02-20 11:54:42 -08005019 MediaExpectations media_expectations;
5020 media_expectations.ExpectBidirectionalAudioAndVideo();
5021 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005022}
5023
5024TEST_P(PeerConnectionIntegrationInteropTest,
5025 ReverseRolesOneAudioLocalToOneVideoRemote) {
5026 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5027 ConnectFakeSignaling();
5028 caller()->AddAudioTrack();
5029 callee()->AddVideoTrack();
5030
5031 caller()->CreateAndSetAndSignalOffer();
5032 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5033
5034 // Verify that only the audio track has been negotiated.
5035 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5036 // Might also check that the callee's NegotiationNeeded flag is set.
5037
5038 // Reverse roles.
5039 callee()->CreateAndSetAndSignalOffer();
5040 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5041
Seth Hampson2f0d7022018-02-20 11:54:42 -08005042 MediaExpectations media_expectations;
5043 media_expectations.CallerExpectsSomeVideo();
5044 media_expectations.CalleeExpectsSomeAudio();
5045 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005046}
5047
Steve Antonba42e992018-04-09 14:10:01 -07005048INSTANTIATE_TEST_CASE_P(
5049 PeerConnectionIntegrationTest,
5050 PeerConnectionIntegrationInteropTest,
5051 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5052 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5053
5054// Test that if the Unified Plan side offers two video tracks then the Plan B
5055// side will only see the first one and ignore the second.
5056TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005057 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5058 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005059 ConnectFakeSignaling();
5060 auto first_sender = caller()->AddVideoTrack();
5061 caller()->AddVideoTrack();
5062
5063 caller()->CreateAndSetAndSignalOffer();
5064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5065
5066 // Verify that there is only one receiver and it corresponds to the first
5067 // added track.
5068 auto receivers = callee()->pc()->GetReceivers();
5069 ASSERT_EQ(1u, receivers.size());
5070 EXPECT_TRUE(receivers[0]->track()->enabled());
5071 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5072
Seth Hampson2f0d7022018-02-20 11:54:42 -08005073 MediaExpectations media_expectations;
5074 media_expectations.CalleeExpectsSomeVideo();
5075 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005076}
5077
Steve Anton2bed3972019-01-04 17:04:30 -08005078// Test that if the initial offer tagged BUNDLE section is rejected due to its
5079// associated RtpTransceiver being stopped and another transceiver is added,
5080// then renegotiation causes the callee to receive the new video track without
5081// error.
5082// This is a regression test for bugs.webrtc.org/9954
5083TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5084 ReOfferWithStoppedBundleTaggedTransceiver) {
5085 RTCConfiguration config;
5086 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5087 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5088 ConnectFakeSignaling();
5089 auto audio_transceiver_or_error =
5090 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5091 ASSERT_TRUE(audio_transceiver_or_error.ok());
5092 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5093
5094 caller()->CreateAndSetAndSignalOffer();
5095 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5096 {
5097 MediaExpectations media_expectations;
5098 media_expectations.CalleeExpectsSomeAudio();
5099 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5100 }
5101
5102 audio_transceiver->Stop();
5103 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5104
5105 caller()->CreateAndSetAndSignalOffer();
5106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5107 {
5108 MediaExpectations media_expectations;
5109 media_expectations.CalleeExpectsSomeVideo();
5110 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5111 }
5112}
5113
deadbeef1dcb1642017-03-29 21:08:16 -07005114} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005115} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005116
5117#endif // if !defined(THREAD_SANITIZER)