blob: 7927c1ee6e4cb9a5657f7b57ce981190a0bb489a [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
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <functional>
18#include <list>
19#include <map>
20#include <memory>
21#include <utility>
22#include <vector>
23
Steve Anton64b626b2019-01-28 17:25:26 -080024#include "absl/algorithm/container.h"
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"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010063#include "rtc_base/fake_clock.h"
Steve Anton10542f22019-01-11 09:11:00 -080064#include "rtc_base/fake_network.h"
65#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020067#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080068#include "rtc_base/test_certificate_verifier.h"
69#include "rtc_base/time_utils.h"
70#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020071#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020072#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070073
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010074namespace webrtc {
75namespace {
76
77using ::cricket::ContentInfo;
78using ::cricket::StreamParams;
79using ::rtc::SocketAddress;
80using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080081using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080082using ::testing::Contains;
Steve Antonede9ca52017-10-16 13:04:27 -070083using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080084using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080085using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070086using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080087using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010088using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080089using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070090
91static const int kDefaultTimeout = 10000;
92static const int kMaxWaitForStatsMs = 3000;
93static const int kMaxWaitForActivationMs = 5000;
94static const int kMaxWaitForFramesMs = 10000;
95// Default number of audio/video frames to wait for before considering a test
96// successful.
97static const int kDefaultExpectedAudioFrameCount = 3;
98static const int kDefaultExpectedVideoFrameCount = 3;
99
deadbeef1dcb1642017-03-29 21:08:16 -0700100static const char kDataChannelLabel[] = "data_channel";
101
102// SRTP cipher name negotiated by the tests. This must be updated if the
103// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700104static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700105static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
106
Steve Antonede9ca52017-10-16 13:04:27 -0700107static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
108
deadbeef1dcb1642017-03-29 21:08:16 -0700109// Helper function for constructing offer/answer options to initiate an ICE
110// restart.
111PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
112 PeerConnectionInterface::RTCOfferAnswerOptions options;
113 options.ice_restart = true;
114 return options;
115}
116
deadbeefd8ad7882017-04-18 16:01:17 -0700117// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
118// attribute from received SDP, simulating a legacy endpoint.
119void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
120 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800121 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700122 }
123 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100124 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700125}
126
Seth Hampson5897a6e2018-04-03 11:16:33 -0700127// Removes all stream information besides the stream ids, simulating an
128// endpoint that only signals a=msid lines to convey stream_ids.
129void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
130 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700131 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700132 std::vector<std::string> stream_ids;
133 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 const StreamParams& first_stream =
135 content.media_description()->streams()[0];
136 track_id = first_stream.id;
137 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700138 }
139 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700140 StreamParams new_stream;
141 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700142 new_stream.set_stream_ids(stream_ids);
143 content.media_description()->AddStream(new_stream);
144 }
145}
146
zhihuangf8164932017-05-19 13:09:47 -0700147int FindFirstMediaStatsIndexByKind(
148 const std::string& kind,
149 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
150 media_stats_vec) {
151 for (size_t i = 0; i < media_stats_vec.size(); i++) {
152 if (media_stats_vec[i]->kind.ValueToString() == kind) {
153 return i;
154 }
155 }
156 return -1;
157}
158
deadbeef1dcb1642017-03-29 21:08:16 -0700159class SignalingMessageReceiver {
160 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800161 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700162 virtual void ReceiveIceMessage(const std::string& sdp_mid,
163 int sdp_mline_index,
164 const std::string& msg) = 0;
165
166 protected:
167 SignalingMessageReceiver() {}
168 virtual ~SignalingMessageReceiver() {}
169};
170
171class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
172 public:
173 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
174 : expected_media_type_(media_type) {}
175
176 void OnFirstPacketReceived(cricket::MediaType media_type) override {
177 ASSERT_EQ(expected_media_type_, media_type);
178 first_packet_received_ = true;
179 }
180
181 bool first_packet_received() const { return first_packet_received_; }
182
183 virtual ~MockRtpReceiverObserver() {}
184
185 private:
186 bool first_packet_received_ = false;
187 cricket::MediaType expected_media_type_;
188};
189
190// Helper class that wraps a peer connection, observes it, and can accept
191// signaling messages from another wrapper.
192//
193// Uses a fake network, fake A/V capture, and optionally fake
194// encoders/decoders, though they aren't used by default since they don't
195// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700196// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800197// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700198class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800199 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700200 public:
201 // Different factory methods for convenience.
202 // TODO(deadbeef): Could use the pattern of:
203 //
204 // PeerConnectionWrapper =
205 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
206 //
207 // To reduce some code duplication.
208 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
209 const std::string& debug_name,
210 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
211 rtc::Thread* network_thread,
212 rtc::Thread* worker_thread) {
213 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700214 webrtc::PeerConnectionDependencies dependencies(nullptr);
215 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200216 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800217 worker_thread, nullptr,
218 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700219 delete client;
220 return nullptr;
221 }
222 return client;
223 }
224
deadbeef2f425aa2017-04-14 10:41:32 -0700225 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
226 return peer_connection_factory_.get();
227 }
228
deadbeef1dcb1642017-03-29 21:08:16 -0700229 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
230
231 // If a signaling message receiver is set (via ConnectFakeSignaling), this
232 // will set the whole offer/answer exchange in motion. Just need to wait for
233 // the signaling state to reach "stable".
234 void CreateAndSetAndSignalOffer() {
235 auto offer = CreateOffer();
236 ASSERT_NE(nullptr, offer);
237 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
238 }
239
240 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
241 // when a remote offer is received (via fake signaling) and an answer is
242 // generated. By default, uses default options.
243 void SetOfferAnswerOptions(
244 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
245 offer_answer_options_ = options;
246 }
247
248 // Set a callback to be invoked when SDP is received via the fake signaling
249 // channel, which provides an opportunity to munge (modify) the SDP. This is
250 // used to test SDP being applied that a PeerConnection would normally not
251 // generate, but a non-JSEP endpoint might.
252 void SetReceivedSdpMunger(
253 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100254 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700255 }
256
deadbeefc964d0b2017-04-03 10:03:35 -0700257 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700258 // generated.
259 void SetGeneratedSdpMunger(
260 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100261 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700262 }
263
Seth Hampson2f0d7022018-02-20 11:54:42 -0800264 // Set a callback to be invoked when a remote offer is received via the fake
265 // signaling channel. This provides an opportunity to change the
266 // PeerConnection state before an answer is created and sent to the caller.
267 void SetRemoteOfferHandler(std::function<void()> handler) {
268 remote_offer_handler_ = std::move(handler);
269 }
270
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800271 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
272 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700273 }
274
Steve Antonede9ca52017-10-16 13:04:27 -0700275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
Steve Anton6f25b092017-10-23 09:39:20 -0700280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
Steve Antonede9ca52017-10-16 13:04:27 -0700283
Jonas Olsson635474e2018-10-18 15:58:17 +0200284 // Every PeerConnection state in order that has been seen by the observer.
285 std::vector<PeerConnectionInterface::PeerConnectionState>
286 peer_connection_state_history() const {
287 return peer_connection_state_history_;
288 }
289
Steve Antonede9ca52017-10-16 13:04:27 -0700290 // Every ICE gathering state in order that has been seen by the observer.
291 std::vector<PeerConnectionInterface::IceGatheringState>
292 ice_gathering_state_history() const {
293 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700294 }
295
Steve Anton15324772018-01-16 10:26:49 -0800296 void AddAudioVideoTracks() {
297 AddAudioTrack();
298 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700299 }
300
Steve Anton74255ff2018-01-24 18:32:57 -0800301 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
302 return AddTrack(CreateLocalAudioTrack());
303 }
deadbeef1dcb1642017-03-29 21:08:16 -0700304
Steve Anton74255ff2018-01-24 18:32:57 -0800305 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
306 return AddTrack(CreateLocalVideoTrack());
307 }
deadbeef1dcb1642017-03-29 21:08:16 -0700308
309 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200310 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700311 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200312 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700313 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200314 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700315 // TODO(perkj): Test audio source when it is implemented. Currently audio
316 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700317 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700318 source);
319 }
320
321 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200322 webrtc::FakePeriodicVideoSource::Config config;
323 config.timestamp_offset_ms = rtc::TimeMillis();
324 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700325 }
326
327 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200328 CreateLocalVideoTrackWithConfig(
329 webrtc::FakePeriodicVideoSource::Config config) {
330 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
333 rtc::scoped_refptr<webrtc::VideoTrackInterface>
334 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200335 webrtc::FakePeriodicVideoSource::Config config;
336 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200337 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200338 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700339 }
340
Steve Anton74255ff2018-01-24 18:32:57 -0800341 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
342 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800343 const std::vector<std::string>& stream_ids = {}) {
344 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800345 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800346 return result.MoveValue();
347 }
348
349 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
350 cricket::MediaType media_type) {
351 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100352 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800353 if (receiver->media_type() == media_type) {
354 receivers.push_back(receiver);
355 }
356 }
357 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700358 }
359
Seth Hampson2f0d7022018-02-20 11:54:42 -0800360 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
361 cricket::MediaType media_type) {
362 for (auto transceiver : pc()->GetTransceivers()) {
363 if (transceiver->receiver()->media_type() == media_type) {
364 return transceiver;
365 }
366 }
367 return nullptr;
368 }
369
deadbeef1dcb1642017-03-29 21:08:16 -0700370 bool SignalingStateStable() {
371 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
372 }
373
374 void CreateDataChannel() { CreateDataChannel(nullptr); }
375
376 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700377 CreateDataChannel(kDataChannelLabel, init);
378 }
379
380 void CreateDataChannel(const std::string& label,
381 const webrtc::DataChannelInit* init) {
382 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700383 ASSERT_TRUE(data_channel_.get() != nullptr);
384 data_observer_.reset(new MockDataChannelObserver(data_channel_));
385 }
386
387 DataChannelInterface* data_channel() { return data_channel_; }
388 const MockDataChannelObserver* data_observer() const {
389 return data_observer_.get();
390 }
391
392 int audio_frames_received() const {
393 return fake_audio_capture_module_->frames_received();
394 }
395
396 // Takes minimum of video frames received for each track.
397 //
398 // Can be used like:
399 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
400 //
401 // To ensure that all video tracks received at least a certain number of
402 // frames.
403 int min_video_frames_received_per_track() const {
404 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200405 if (fake_video_renderers_.empty()) {
406 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700407 }
deadbeef1dcb1642017-03-29 21:08:16 -0700408
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200409 for (const auto& pair : fake_video_renderers_) {
410 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700411 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200412 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700413 }
414
415 // Returns a MockStatsObserver in a state after stats gathering finished,
416 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700417 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700418 webrtc::MediaStreamTrackInterface* track) {
419 rtc::scoped_refptr<MockStatsObserver> observer(
420 new rtc::RefCountedObject<MockStatsObserver>());
421 EXPECT_TRUE(peer_connection_->GetStats(
422 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
423 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
424 return observer;
425 }
426
427 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700428 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
429 return OldGetStatsForTrack(nullptr);
430 }
431
432 // Synchronously gets stats and returns them. If it times out, fails the test
433 // and returns null.
434 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
435 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
436 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
437 peer_connection_->GetStats(callback);
438 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
439 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700440 }
441
442 int rendered_width() {
443 EXPECT_FALSE(fake_video_renderers_.empty());
444 return fake_video_renderers_.empty()
445 ? 0
446 : fake_video_renderers_.begin()->second->width();
447 }
448
449 int rendered_height() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->height();
454 }
455
456 double rendered_aspect_ratio() {
457 if (rendered_height() == 0) {
458 return 0.0;
459 }
460 return static_cast<double>(rendered_width()) / rendered_height();
461 }
462
463 webrtc::VideoRotation rendered_rotation() {
464 EXPECT_FALSE(fake_video_renderers_.empty());
465 return fake_video_renderers_.empty()
466 ? webrtc::kVideoRotation_0
467 : fake_video_renderers_.begin()->second->rotation();
468 }
469
470 int local_rendered_width() {
471 return local_video_renderer_ ? local_video_renderer_->width() : 0;
472 }
473
474 int local_rendered_height() {
475 return local_video_renderer_ ? local_video_renderer_->height() : 0;
476 }
477
478 double local_rendered_aspect_ratio() {
479 if (local_rendered_height() == 0) {
480 return 0.0;
481 }
482 return static_cast<double>(local_rendered_width()) /
483 local_rendered_height();
484 }
485
486 size_t number_of_remote_streams() {
487 if (!pc()) {
488 return 0;
489 }
490 return pc()->remote_streams()->count();
491 }
492
493 StreamCollectionInterface* remote_streams() const {
494 if (!pc()) {
495 ADD_FAILURE();
496 return nullptr;
497 }
498 return pc()->remote_streams();
499 }
500
501 StreamCollectionInterface* local_streams() {
502 if (!pc()) {
503 ADD_FAILURE();
504 return nullptr;
505 }
506 return pc()->local_streams();
507 }
508
509 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
510 return pc()->signaling_state();
511 }
512
513 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
514 return pc()->ice_connection_state();
515 }
516
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100517 webrtc::PeerConnectionInterface::IceConnectionState
518 standardized_ice_connection_state() {
519 return pc()->standardized_ice_connection_state();
520 }
521
deadbeef1dcb1642017-03-29 21:08:16 -0700522 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
523 return pc()->ice_gathering_state();
524 }
525
526 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
527 // GetReceivers. They're updated automatically when a remote offer/answer
528 // from the fake signaling channel is applied, or when
529 // ResetRtpReceiverObservers below is called.
530 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
531 rtp_receiver_observers() {
532 return rtp_receiver_observers_;
533 }
534
535 void ResetRtpReceiverObservers() {
536 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100537 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
538 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700539 std::unique_ptr<MockRtpReceiverObserver> observer(
540 new MockRtpReceiverObserver(receiver->media_type()));
541 receiver->SetObserver(observer.get());
542 rtp_receiver_observers_.push_back(std::move(observer));
543 }
544 }
545
Steve Antonede9ca52017-10-16 13:04:27 -0700546 rtc::FakeNetworkManager* network() const {
547 return fake_network_manager_.get();
548 }
549 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
550
Qingsi Wang7685e862018-06-11 20:15:46 -0700551 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
552 return event_log_factory_;
553 }
554
deadbeef1dcb1642017-03-29 21:08:16 -0700555 private:
556 explicit PeerConnectionWrapper(const std::string& debug_name)
557 : debug_name_(debug_name) {}
558
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800559 bool Init(
560 const PeerConnectionFactory::Options* options,
561 const PeerConnectionInterface::RTCConfiguration* config,
562 webrtc::PeerConnectionDependencies dependencies,
563 rtc::Thread* network_thread,
564 rtc::Thread* worker_thread,
565 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
566 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700567 // There's an error in this test code if Init ends up being called twice.
568 RTC_DCHECK(!peer_connection_);
569 RTC_DCHECK(!peer_connection_factory_);
570
571 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700572 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700573
574 std::unique_ptr<cricket::PortAllocator> port_allocator(
575 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700576 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700577 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
578 if (!fake_audio_capture_module_) {
579 return false;
580 }
deadbeef1dcb1642017-03-29 21:08:16 -0700581 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700582
583 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
584 pc_factory_dependencies.network_thread = network_thread;
585 pc_factory_dependencies.worker_thread = worker_thread;
586 pc_factory_dependencies.signaling_thread = signaling_thread;
587 pc_factory_dependencies.media_engine =
588 cricket::WebRtcMediaEngineFactory::Create(
589 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
590 fake_audio_capture_module_),
591 webrtc::CreateBuiltinAudioEncoderFactory(),
592 webrtc::CreateBuiltinAudioDecoderFactory(),
593 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000594 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700595 webrtc::AudioProcessingBuilder().Create());
596 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
597 if (event_log_factory) {
598 event_log_factory_ = event_log_factory.get();
599 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
600 } else {
601 pc_factory_dependencies.event_log_factory =
602 webrtc::CreateRtcEventLogFactory();
603 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800604 if (media_transport_factory) {
605 pc_factory_dependencies.media_transport_factory =
606 std::move(media_transport_factory);
607 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700608 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
609 std::move(pc_factory_dependencies));
610
deadbeef1dcb1642017-03-29 21:08:16 -0700611 if (!peer_connection_factory_) {
612 return false;
613 }
614 if (options) {
615 peer_connection_factory_->SetOptions(*options);
616 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800617 if (config) {
618 sdp_semantics_ = config->sdp_semantics;
619 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700620
621 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200622 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700623 return peer_connection_.get() != nullptr;
624 }
625
626 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700627 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700628 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700629 PeerConnectionInterface::RTCConfiguration modified_config;
630 // If |config| is null, this will result in a default configuration being
631 // used.
632 if (config) {
633 modified_config = *config;
634 }
635 // Disable resolution adaptation; we don't want it interfering with the
636 // test results.
637 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
638 // ratios and not specific resolutions, is this even necessary?
639 modified_config.set_cpu_adaptation(false);
640
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700641 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700642 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700644 }
645
646 void set_signaling_message_receiver(
647 SignalingMessageReceiver* signaling_message_receiver) {
648 signaling_message_receiver_ = signaling_message_receiver;
649 }
650
651 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
652
Steve Antonede9ca52017-10-16 13:04:27 -0700653 void set_signal_ice_candidates(bool signal) {
654 signal_ice_candidates_ = signal;
655 }
656
deadbeef1dcb1642017-03-29 21:08:16 -0700657 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200658 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700659 // Set max frame rate to 10fps to reduce the risk of test flakiness.
660 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200661 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700662
Niels Möller5c7efe72018-05-11 10:34:46 +0200663 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200664 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
665 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700666 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200667 peer_connection_factory_->CreateVideoTrack(
668 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700669 if (!local_video_renderer_) {
670 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
671 }
672 return track;
673 }
674
675 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100676 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800677 std::unique_ptr<SessionDescriptionInterface> desc =
678 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700679 if (received_sdp_munger_) {
680 received_sdp_munger_(desc->description());
681 }
682
683 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
684 // Setting a remote description may have changed the number of receivers,
685 // so reset the receiver observers.
686 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800687 if (remote_offer_handler_) {
688 remote_offer_handler_();
689 }
deadbeef1dcb1642017-03-29 21:08:16 -0700690 auto answer = CreateAnswer();
691 ASSERT_NE(nullptr, answer);
692 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
693 }
694
695 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100696 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800697 std::unique_ptr<SessionDescriptionInterface> desc =
698 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700699 if (received_sdp_munger_) {
700 received_sdp_munger_(desc->description());
701 }
702
703 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
704 // Set the RtpReceiverObserver after receivers are created.
705 ResetRtpReceiverObservers();
706 }
707
708 // Returns null on failure.
709 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
710 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
711 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
712 pc()->CreateOffer(observer, offer_answer_options_);
713 return WaitForDescriptionFromObserver(observer);
714 }
715
716 // Returns null on failure.
717 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
718 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
719 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
720 pc()->CreateAnswer(observer, offer_answer_options_);
721 return WaitForDescriptionFromObserver(observer);
722 }
723
724 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100725 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700726 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
727 if (!observer->result()) {
728 return nullptr;
729 }
730 auto description = observer->MoveDescription();
731 if (generated_sdp_munger_) {
732 generated_sdp_munger_(description->description());
733 }
734 return description;
735 }
736
737 // Setting the local description and sending the SDP message over the fake
738 // signaling channel are combined into the same method because the SDP
739 // message needs to be sent as soon as SetLocalDescription finishes, without
740 // waiting for the observer to be called. This ensures that ICE candidates
741 // don't outrace the description.
742 bool SetLocalDescriptionAndSendSdpMessage(
743 std::unique_ptr<SessionDescriptionInterface> desc) {
744 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
745 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100746 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800747 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700748 std::string sdp;
749 EXPECT_TRUE(desc->ToString(&sdp));
750 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800751 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
752 RemoveUnusedVideoRenderers();
753 }
deadbeef1dcb1642017-03-29 21:08:16 -0700754 // As mentioned above, we need to send the message immediately after
755 // SetLocalDescription.
756 SendSdpMessage(type, sdp);
757 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
758 return true;
759 }
760
761 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
762 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
763 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100764 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700765 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800766 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
767 RemoveUnusedVideoRenderers();
768 }
deadbeef1dcb1642017-03-29 21:08:16 -0700769 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
770 return observer->result();
771 }
772
Seth Hampson2f0d7022018-02-20 11:54:42 -0800773 // This is a work around to remove unused fake_video_renderers from
774 // transceivers that have either stopped or are no longer receiving.
775 void RemoveUnusedVideoRenderers() {
776 auto transceivers = pc()->GetTransceivers();
777 for (auto& transceiver : transceivers) {
778 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
779 continue;
780 }
781 // Remove fake video renderers from any stopped transceivers.
782 if (transceiver->stopped()) {
783 auto it =
784 fake_video_renderers_.find(transceiver->receiver()->track()->id());
785 if (it != fake_video_renderers_.end()) {
786 fake_video_renderers_.erase(it);
787 }
788 }
789 // Remove fake video renderers from any transceivers that are no longer
790 // receiving.
791 if ((transceiver->current_direction() &&
792 !webrtc::RtpTransceiverDirectionHasRecv(
793 *transceiver->current_direction()))) {
794 auto it =
795 fake_video_renderers_.find(transceiver->receiver()->track()->id());
796 if (it != fake_video_renderers_.end()) {
797 fake_video_renderers_.erase(it);
798 }
799 }
800 }
801 }
802
deadbeef1dcb1642017-03-29 21:08:16 -0700803 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
804 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800805 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700806 if (signaling_delay_ms_ == 0) {
807 RelaySdpMessageIfReceiverExists(type, msg);
808 } else {
809 invoker_.AsyncInvokeDelayed<void>(
810 RTC_FROM_HERE, rtc::Thread::Current(),
811 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
812 this, type, msg),
813 signaling_delay_ms_);
814 }
815 }
816
Steve Antona3a92c22017-12-07 10:27:41 -0800817 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700818 if (signaling_message_receiver_) {
819 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
820 }
821 }
822
823 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
824 // default).
825 void SendIceMessage(const std::string& sdp_mid,
826 int sdp_mline_index,
827 const std::string& msg) {
828 if (signaling_delay_ms_ == 0) {
829 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
830 } else {
831 invoker_.AsyncInvokeDelayed<void>(
832 RTC_FROM_HERE, rtc::Thread::Current(),
833 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
834 this, sdp_mid, sdp_mline_index, msg),
835 signaling_delay_ms_);
836 }
837 }
838
839 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
840 int sdp_mline_index,
841 const std::string& msg) {
842 if (signaling_message_receiver_) {
843 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
844 msg);
845 }
846 }
847
848 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800849 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
850 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700851 HandleIncomingOffer(msg);
852 } else {
853 HandleIncomingAnswer(msg);
854 }
855 }
856
857 void ReceiveIceMessage(const std::string& sdp_mid,
858 int sdp_mline_index,
859 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100860 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700861 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
862 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
863 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
864 }
865
866 // PeerConnectionObserver callbacks.
867 void OnSignalingChange(
868 webrtc::PeerConnectionInterface::SignalingState new_state) override {
869 EXPECT_EQ(pc()->signaling_state(), new_state);
870 }
Steve Anton15324772018-01-16 10:26:49 -0800871 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
872 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
873 streams) override {
874 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
875 rtc::scoped_refptr<VideoTrackInterface> video_track(
876 static_cast<VideoTrackInterface*>(receiver->track().get()));
877 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700878 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800879 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200880 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700881 }
882 }
Steve Anton15324772018-01-16 10:26:49 -0800883 void OnRemoveTrack(
884 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
885 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
886 auto it = fake_video_renderers_.find(receiver->track()->id());
887 RTC_DCHECK(it != fake_video_renderers_.end());
888 fake_video_renderers_.erase(it);
889 }
890 }
deadbeef1dcb1642017-03-29 21:08:16 -0700891 void OnRenegotiationNeeded() override {}
892 void OnIceConnectionChange(
893 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
894 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700895 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700896 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200897 void OnConnectionChange(
898 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
899 peer_connection_state_history_.push_back(new_state);
900 }
901
deadbeef1dcb1642017-03-29 21:08:16 -0700902 void OnIceGatheringChange(
903 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700904 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700905 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700906 }
907 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100908 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700909
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800910 if (remote_async_resolver_) {
911 const auto& local_candidate = candidate->candidate();
912 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
913 if (local_candidate.address().IsUnresolvedIP()) {
914 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
915 rtc::SocketAddress resolved_addr(local_candidate.address());
916 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
917 local_candidate.address().hostname());
918 RTC_DCHECK(!resolved_ip.IsNil());
919 resolved_addr.SetResolvedIP(resolved_ip);
920 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
921 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
922 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700923 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700924 }
925
deadbeef1dcb1642017-03-29 21:08:16 -0700926 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800927 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700928 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700929 // Remote party may be deleted.
930 return;
931 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800932 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 void OnDataChannel(
935 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100936 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700937 data_channel_ = data_channel;
938 data_observer_.reset(new MockDataChannelObserver(data_channel));
939 }
940
deadbeef1dcb1642017-03-29 21:08:16 -0700941 std::string debug_name_;
942
943 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
944
945 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
946 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
947 peer_connection_factory_;
948
Steve Antonede9ca52017-10-16 13:04:27 -0700949 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700950 // Needed to keep track of number of frames sent.
951 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
952 // Needed to keep track of number of frames received.
953 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
954 fake_video_renderers_;
955 // Needed to ensure frames aren't received for removed tracks.
956 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
957 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700958
959 // For remote peer communication.
960 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
961 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700962 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700963
Niels Möller5c7efe72018-05-11 10:34:46 +0200964 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700965 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200966 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
967 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700968 // |local_video_renderer_| attached to the first created local video track.
969 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
970
Seth Hampson2f0d7022018-02-20 11:54:42 -0800971 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700972 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
973 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
974 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800975 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800976 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700977 rtc::scoped_refptr<DataChannelInterface> data_channel_;
978 std::unique_ptr<MockDataChannelObserver> data_observer_;
979
980 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
981
Steve Antonede9ca52017-10-16 13:04:27 -0700982 std::vector<PeerConnectionInterface::IceConnectionState>
983 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200984 std::vector<PeerConnectionInterface::PeerConnectionState>
985 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -0700986 std::vector<PeerConnectionInterface::IceGatheringState>
987 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700988
Qingsi Wang7685e862018-06-11 20:15:46 -0700989 webrtc::FakeRtcEventLogFactory* event_log_factory_;
990
deadbeef1dcb1642017-03-29 21:08:16 -0700991 rtc::AsyncInvoker invoker_;
992
Seth Hampson2f0d7022018-02-20 11:54:42 -0800993 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700994};
995
Elad Alon99c3fe52017-10-13 16:29:40 +0200996class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
997 public:
998 virtual ~MockRtcEventLogOutput() = default;
999 MOCK_CONST_METHOD0(IsActive, bool());
1000 MOCK_METHOD1(Write, bool(const std::string&));
1001};
1002
Seth Hampson2f0d7022018-02-20 11:54:42 -08001003// This helper object is used for both specifying how many audio/video frames
1004// are expected to be received for a caller/callee. It provides helper functions
1005// to specify these expectations. The object initially starts in a state of no
1006// expectations.
1007class MediaExpectations {
1008 public:
1009 enum ExpectFrames {
1010 kExpectSomeFrames,
1011 kExpectNoFrames,
1012 kNoExpectation,
1013 };
1014
1015 void ExpectBidirectionalAudioAndVideo() {
1016 ExpectBidirectionalAudio();
1017 ExpectBidirectionalVideo();
1018 }
1019
1020 void ExpectBidirectionalAudio() {
1021 CallerExpectsSomeAudio();
1022 CalleeExpectsSomeAudio();
1023 }
1024
1025 void ExpectNoAudio() {
1026 CallerExpectsNoAudio();
1027 CalleeExpectsNoAudio();
1028 }
1029
1030 void ExpectBidirectionalVideo() {
1031 CallerExpectsSomeVideo();
1032 CalleeExpectsSomeVideo();
1033 }
1034
1035 void ExpectNoVideo() {
1036 CallerExpectsNoVideo();
1037 CalleeExpectsNoVideo();
1038 }
1039
1040 void CallerExpectsSomeAudioAndVideo() {
1041 CallerExpectsSomeAudio();
1042 CallerExpectsSomeVideo();
1043 }
1044
1045 void CalleeExpectsSomeAudioAndVideo() {
1046 CalleeExpectsSomeAudio();
1047 CalleeExpectsSomeVideo();
1048 }
1049
1050 // Caller's audio functions.
1051 void CallerExpectsSomeAudio(
1052 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1053 caller_audio_expectation_ = kExpectSomeFrames;
1054 caller_audio_frames_expected_ = expected_audio_frames;
1055 }
1056
1057 void CallerExpectsNoAudio() {
1058 caller_audio_expectation_ = kExpectNoFrames;
1059 caller_audio_frames_expected_ = 0;
1060 }
1061
1062 // Caller's video functions.
1063 void CallerExpectsSomeVideo(
1064 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1065 caller_video_expectation_ = kExpectSomeFrames;
1066 caller_video_frames_expected_ = expected_video_frames;
1067 }
1068
1069 void CallerExpectsNoVideo() {
1070 caller_video_expectation_ = kExpectNoFrames;
1071 caller_video_frames_expected_ = 0;
1072 }
1073
1074 // Callee's audio functions.
1075 void CalleeExpectsSomeAudio(
1076 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1077 callee_audio_expectation_ = kExpectSomeFrames;
1078 callee_audio_frames_expected_ = expected_audio_frames;
1079 }
1080
1081 void CalleeExpectsNoAudio() {
1082 callee_audio_expectation_ = kExpectNoFrames;
1083 callee_audio_frames_expected_ = 0;
1084 }
1085
1086 // Callee's video functions.
1087 void CalleeExpectsSomeVideo(
1088 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1089 callee_video_expectation_ = kExpectSomeFrames;
1090 callee_video_frames_expected_ = expected_video_frames;
1091 }
1092
1093 void CalleeExpectsNoVideo() {
1094 callee_video_expectation_ = kExpectNoFrames;
1095 callee_video_frames_expected_ = 0;
1096 }
1097
1098 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1099 ExpectFrames caller_video_expectation_ = kNoExpectation;
1100 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1101 ExpectFrames callee_video_expectation_ = kNoExpectation;
1102 int caller_audio_frames_expected_ = 0;
1103 int caller_video_frames_expected_ = 0;
1104 int callee_audio_frames_expected_ = 0;
1105 int callee_video_frames_expected_ = 0;
1106};
1107
deadbeef1dcb1642017-03-29 21:08:16 -07001108// Tests two PeerConnections connecting to each other end-to-end, using a
1109// virtual network, fake A/V capture and fake encoder/decoders. The
1110// PeerConnections share the threads/socket servers, but use separate versions
1111// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001112class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001113 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001114 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1115 : sdp_semantics_(sdp_semantics),
1116 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001117 fss_(new rtc::FirewallSocketServer(ss_.get())),
1118 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001119 worker_thread_(rtc::Thread::Create()),
1120 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001121 network_thread_->SetName("PCNetworkThread", this);
1122 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001123 RTC_CHECK(network_thread_->Start());
1124 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001125 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001126 }
1127
Seth Hampson2f0d7022018-02-20 11:54:42 -08001128 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001129 // The PeerConnections should deleted before the TurnCustomizers.
1130 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1131 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1132 // that the TurnCustomizer outlives the life of the PeerConnection or else
1133 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001134 if (caller_) {
1135 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001136 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001137 }
1138 if (callee_) {
1139 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001140 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001141 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001142
1143 // If turn servers were created for the test they need to be destroyed on
1144 // the network thread.
1145 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1146 turn_servers_.clear();
1147 turn_customizers_.clear();
1148 });
deadbeef1dcb1642017-03-29 21:08:16 -07001149 }
1150
1151 bool SignalingStateStable() {
1152 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1153 }
1154
deadbeef71452802017-05-07 17:21:01 -07001155 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001156 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1157 // are connected. This is an important distinction. Once we have separate
1158 // ICE and DTLS state, this check needs to use the DTLS state.
1159 return (callee()->ice_connection_state() ==
1160 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1161 callee()->ice_connection_state() ==
1162 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1163 (caller()->ice_connection_state() ==
1164 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1165 caller()->ice_connection_state() ==
1166 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001167 }
1168
Qingsi Wang7685e862018-06-11 20:15:46 -07001169 // When |event_log_factory| is null, the default implementation of the event
1170 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001171 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1172 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001173 const PeerConnectionFactory::Options* options,
1174 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001175 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001176 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1177 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001178 RTCConfiguration modified_config;
1179 if (config) {
1180 modified_config = *config;
1181 }
Steve Anton3acffc32018-04-12 17:21:03 -07001182 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001183 if (!dependencies.cert_generator) {
1184 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001185 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001186 }
1187 std::unique_ptr<PeerConnectionWrapper> client(
1188 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001189
Niels Möllerf06f9232018-08-07 12:32:18 +02001190 if (!client->Init(options, &modified_config, std::move(dependencies),
1191 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001192 std::move(event_log_factory),
1193 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001194 return nullptr;
1195 }
1196 return client;
1197 }
1198
Qingsi Wang7685e862018-06-11 20:15:46 -07001199 std::unique_ptr<PeerConnectionWrapper>
1200 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1201 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001202 const PeerConnectionFactory::Options* options,
1203 const RTCConfiguration* config,
1204 webrtc::PeerConnectionDependencies dependencies) {
1205 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1206 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001207 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001208 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001209 std::move(event_log_factory),
1210 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001211 }
1212
deadbeef1dcb1642017-03-29 21:08:16 -07001213 bool CreatePeerConnectionWrappers() {
1214 return CreatePeerConnectionWrappersWithConfig(
1215 PeerConnectionInterface::RTCConfiguration(),
1216 PeerConnectionInterface::RTCConfiguration());
1217 }
1218
Steve Anton3acffc32018-04-12 17:21:03 -07001219 bool CreatePeerConnectionWrappersWithSdpSemantics(
1220 SdpSemantics caller_semantics,
1221 SdpSemantics callee_semantics) {
1222 // Can't specify the sdp_semantics in the passed-in configuration since it
1223 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1224 // stored in sdp_semantics_. So get around this by modifying the instance
1225 // variable before calling CreatePeerConnectionWrapper for the caller and
1226 // callee PeerConnections.
1227 SdpSemantics original_semantics = sdp_semantics_;
1228 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001229 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001230 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001231 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001232 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001233 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001234 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001235 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001236 sdp_semantics_ = original_semantics;
1237 return caller_ && callee_;
1238 }
1239
deadbeef1dcb1642017-03-29 21:08:16 -07001240 bool CreatePeerConnectionWrappersWithConfig(
1241 const PeerConnectionInterface::RTCConfiguration& caller_config,
1242 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001243 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001244 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001245 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1246 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001247 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001248 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001249 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1250 /*media_transport_factory=*/nullptr);
1251 return caller_ && callee_;
1252 }
1253
1254 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1255 const PeerConnectionInterface::RTCConfiguration& caller_config,
1256 const PeerConnectionInterface::RTCConfiguration& callee_config,
1257 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1258 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1259 caller_ =
1260 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1261 webrtc::PeerConnectionDependencies(nullptr),
1262 nullptr, std::move(caller_factory));
1263 callee_ =
1264 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1265 webrtc::PeerConnectionDependencies(nullptr),
1266 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001267 return caller_ && callee_;
1268 }
1269
1270 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1271 const PeerConnectionInterface::RTCConfiguration& caller_config,
1272 webrtc::PeerConnectionDependencies caller_dependencies,
1273 const PeerConnectionInterface::RTCConfiguration& callee_config,
1274 webrtc::PeerConnectionDependencies callee_dependencies) {
1275 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001276 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001277 std::move(caller_dependencies), nullptr,
1278 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001280 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001281 std::move(callee_dependencies), nullptr,
1282 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001283 return caller_ && callee_;
1284 }
1285
1286 bool CreatePeerConnectionWrappersWithOptions(
1287 const PeerConnectionFactory::Options& caller_options,
1288 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001289 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001290 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001291 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1292 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001293 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001294 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001295 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1296 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001297 return caller_ && callee_;
1298 }
1299
1300 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1301 PeerConnectionInterface::RTCConfiguration default_config;
1302 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001303 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001304 webrtc::PeerConnectionDependencies(nullptr));
1305 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001306 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001307 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001308 return caller_ && callee_;
1309 }
1310
Seth Hampson2f0d7022018-02-20 11:54:42 -08001311 std::unique_ptr<PeerConnectionWrapper>
1312 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001313 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1314 new FakeRTCCertificateGenerator());
1315 cert_generator->use_alternate_key();
1316
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001317 webrtc::PeerConnectionDependencies dependencies(nullptr);
1318 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001319 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001320 std::move(dependencies), nullptr,
1321 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001322 }
1323
Seth Hampsonaed71642018-06-11 07:41:32 -07001324 cricket::TestTurnServer* CreateTurnServer(
1325 rtc::SocketAddress internal_address,
1326 rtc::SocketAddress external_address,
1327 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1328 const std::string& common_name = "test turn server") {
1329 rtc::Thread* thread = network_thread();
1330 std::unique_ptr<cricket::TestTurnServer> turn_server =
1331 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1332 RTC_FROM_HERE,
1333 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001334 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001335 thread, internal_address, external_address, type,
1336 /*ignore_bad_certs=*/true, common_name);
1337 });
1338 turn_servers_.push_back(std::move(turn_server));
1339 // Interactions with the turn server should be done on the network thread.
1340 return turn_servers_.back().get();
1341 }
1342
1343 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1344 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1345 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1346 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001347 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001348 turn_customizers_.push_back(std::move(turn_customizer));
1349 // Interactions with the turn customizer should be done on the network
1350 // thread.
1351 return turn_customizers_.back().get();
1352 }
1353
1354 // Checks that the function counters for a TestTurnCustomizer are greater than
1355 // 0.
1356 void ExpectTurnCustomizerCountersIncremented(
1357 cricket::TestTurnCustomizer* turn_customizer) {
1358 unsigned int allow_channel_data_counter =
1359 network_thread()->Invoke<unsigned int>(
1360 RTC_FROM_HERE, [turn_customizer] {
1361 return turn_customizer->allow_channel_data_cnt_;
1362 });
1363 EXPECT_GT(allow_channel_data_counter, 0u);
1364 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1365 RTC_FROM_HERE,
1366 [turn_customizer] { return turn_customizer->modify_cnt_; });
1367 EXPECT_GT(modify_counter, 0u);
1368 }
1369
deadbeef1dcb1642017-03-29 21:08:16 -07001370 // Once called, SDP blobs and ICE candidates will be automatically signaled
1371 // between PeerConnections.
1372 void ConnectFakeSignaling() {
1373 caller_->set_signaling_message_receiver(callee_.get());
1374 callee_->set_signaling_message_receiver(caller_.get());
1375 }
1376
Steve Antonede9ca52017-10-16 13:04:27 -07001377 // Once called, SDP blobs will be automatically signaled between
1378 // PeerConnections. Note that ICE candidates will not be signaled unless they
1379 // are in the exchanged SDP blobs.
1380 void ConnectFakeSignalingForSdpOnly() {
1381 ConnectFakeSignaling();
1382 SetSignalIceCandidates(false);
1383 }
1384
deadbeef1dcb1642017-03-29 21:08:16 -07001385 void SetSignalingDelayMs(int delay_ms) {
1386 caller_->set_signaling_delay_ms(delay_ms);
1387 callee_->set_signaling_delay_ms(delay_ms);
1388 }
1389
Steve Antonede9ca52017-10-16 13:04:27 -07001390 void SetSignalIceCandidates(bool signal) {
1391 caller_->set_signal_ice_candidates(signal);
1392 callee_->set_signal_ice_candidates(signal);
1393 }
1394
deadbeef1dcb1642017-03-29 21:08:16 -07001395 // Messages may get lost on the unreliable DataChannel, so we send multiple
1396 // times to avoid test flakiness.
1397 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1398 const std::string& data,
1399 int retries) {
1400 for (int i = 0; i < retries; ++i) {
1401 dc->Send(DataBuffer(data));
1402 }
1403 }
1404
1405 rtc::Thread* network_thread() { return network_thread_.get(); }
1406
1407 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1408
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001409 webrtc::MediaTransportPair* loopback_media_transports() {
1410 return &loopback_media_transports_;
1411 }
1412
deadbeef1dcb1642017-03-29 21:08:16 -07001413 PeerConnectionWrapper* caller() { return caller_.get(); }
1414
1415 // Set the |caller_| to the |wrapper| passed in and return the
1416 // original |caller_|.
1417 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1418 PeerConnectionWrapper* wrapper) {
1419 PeerConnectionWrapper* old = caller_.release();
1420 caller_.reset(wrapper);
1421 return old;
1422 }
1423
1424 PeerConnectionWrapper* callee() { return callee_.get(); }
1425
1426 // Set the |callee_| to the |wrapper| passed in and return the
1427 // original |callee_|.
1428 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1429 PeerConnectionWrapper* wrapper) {
1430 PeerConnectionWrapper* old = callee_.release();
1431 callee_.reset(wrapper);
1432 return old;
1433 }
1434
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001435 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1436 network_thread()->Invoke<void>(
1437 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1438 caller()->port_allocator(), caller_flags));
1439 network_thread()->Invoke<void>(
1440 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1441 callee()->port_allocator(), callee_flags));
1442 }
1443
Steve Antonede9ca52017-10-16 13:04:27 -07001444 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1445
Seth Hampson2f0d7022018-02-20 11:54:42 -08001446 // Expects the provided number of new frames to be received within
1447 // kMaxWaitForFramesMs. The new expected frames are specified in
1448 // |media_expectations|. Returns false if any of the expectations were
1449 // not met.
1450 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1451 // First initialize the expected frame counts based upon the current
1452 // frame count.
1453 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1454 if (media_expectations.caller_audio_expectation_ ==
1455 MediaExpectations::kExpectSomeFrames) {
1456 total_caller_audio_frames_expected +=
1457 media_expectations.caller_audio_frames_expected_;
1458 }
1459 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001460 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001461 if (media_expectations.caller_video_expectation_ ==
1462 MediaExpectations::kExpectSomeFrames) {
1463 total_caller_video_frames_expected +=
1464 media_expectations.caller_video_frames_expected_;
1465 }
1466 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1467 if (media_expectations.callee_audio_expectation_ ==
1468 MediaExpectations::kExpectSomeFrames) {
1469 total_callee_audio_frames_expected +=
1470 media_expectations.callee_audio_frames_expected_;
1471 }
1472 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001473 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001474 if (media_expectations.callee_video_expectation_ ==
1475 MediaExpectations::kExpectSomeFrames) {
1476 total_callee_video_frames_expected +=
1477 media_expectations.callee_video_frames_expected_;
1478 }
deadbeef1dcb1642017-03-29 21:08:16 -07001479
Seth Hampson2f0d7022018-02-20 11:54:42 -08001480 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001481 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001482 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001483 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001484 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001485 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001486 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001487 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001488 total_callee_video_frames_expected,
1489 kMaxWaitForFramesMs);
1490 bool expectations_correct =
1491 caller()->audio_frames_received() >=
1492 total_caller_audio_frames_expected &&
1493 caller()->min_video_frames_received_per_track() >=
1494 total_caller_video_frames_expected &&
1495 callee()->audio_frames_received() >=
1496 total_callee_audio_frames_expected &&
1497 callee()->min_video_frames_received_per_track() >=
1498 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001499
Seth Hampson2f0d7022018-02-20 11:54:42 -08001500 // After the combined wait, print out a more detailed message upon
1501 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001502 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001503 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001504 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001505 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001506 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001507 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001508 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001509 total_callee_video_frames_expected);
1510
1511 // We want to make sure nothing unexpected was received.
1512 if (media_expectations.caller_audio_expectation_ ==
1513 MediaExpectations::kExpectNoFrames) {
1514 EXPECT_EQ(caller()->audio_frames_received(),
1515 total_caller_audio_frames_expected);
1516 if (caller()->audio_frames_received() !=
1517 total_caller_audio_frames_expected) {
1518 expectations_correct = false;
1519 }
1520 }
1521 if (media_expectations.caller_video_expectation_ ==
1522 MediaExpectations::kExpectNoFrames) {
1523 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1524 total_caller_video_frames_expected);
1525 if (caller()->min_video_frames_received_per_track() !=
1526 total_caller_video_frames_expected) {
1527 expectations_correct = false;
1528 }
1529 }
1530 if (media_expectations.callee_audio_expectation_ ==
1531 MediaExpectations::kExpectNoFrames) {
1532 EXPECT_EQ(callee()->audio_frames_received(),
1533 total_callee_audio_frames_expected);
1534 if (callee()->audio_frames_received() !=
1535 total_callee_audio_frames_expected) {
1536 expectations_correct = false;
1537 }
1538 }
1539 if (media_expectations.callee_video_expectation_ ==
1540 MediaExpectations::kExpectNoFrames) {
1541 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1542 total_callee_video_frames_expected);
1543 if (callee()->min_video_frames_received_per_track() !=
1544 total_callee_video_frames_expected) {
1545 expectations_correct = false;
1546 }
1547 }
1548 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001549 }
1550
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001551 void TestNegotiatedCipherSuite(
1552 const PeerConnectionFactory::Options& caller_options,
1553 const PeerConnectionFactory::Options& callee_options,
1554 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001555 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1556 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001557 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001558 caller()->AddAudioVideoTracks();
1559 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001560 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001561 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001562 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001563 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001564 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001565 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001566 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1567 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001568 }
1569
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001570 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1571 bool remote_gcm_enabled,
1572 int expected_cipher_suite) {
1573 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001574 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1575 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001576 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001577 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1578 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001579 TestNegotiatedCipherSuite(caller_options, callee_options,
1580 expected_cipher_suite);
1581 }
1582
Seth Hampson2f0d7022018-02-20 11:54:42 -08001583 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001584 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001585
deadbeef1dcb1642017-03-29 21:08:16 -07001586 private:
1587 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001588 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001589 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001590 // |network_thread_| and |worker_thread_| are used by both
1591 // |caller_| and |callee_| so they must be destroyed
1592 // later.
1593 std::unique_ptr<rtc::Thread> network_thread_;
1594 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001595 // The turn servers and turn customizers should be accessed & deleted on the
1596 // network thread to avoid a race with the socket read/write that occurs
1597 // on the network thread.
1598 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1599 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001600 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001601 std::unique_ptr<PeerConnectionWrapper> caller_;
1602 std::unique_ptr<PeerConnectionWrapper> callee_;
1603};
1604
Seth Hampson2f0d7022018-02-20 11:54:42 -08001605class PeerConnectionIntegrationTest
1606 : public PeerConnectionIntegrationBaseTest,
1607 public ::testing::WithParamInterface<SdpSemantics> {
1608 protected:
1609 PeerConnectionIntegrationTest()
1610 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1611};
1612
1613class PeerConnectionIntegrationTestPlanB
1614 : public PeerConnectionIntegrationBaseTest {
1615 protected:
1616 PeerConnectionIntegrationTestPlanB()
1617 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1618};
1619
1620class PeerConnectionIntegrationTestUnifiedPlan
1621 : public PeerConnectionIntegrationBaseTest {
1622 protected:
1623 PeerConnectionIntegrationTestUnifiedPlan()
1624 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1625};
1626
deadbeef1dcb1642017-03-29 21:08:16 -07001627// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1628// includes testing that the callback is invoked if an observer is connected
1629// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001630TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001631 RtpReceiverObserverOnFirstPacketReceived) {
1632 ASSERT_TRUE(CreatePeerConnectionWrappers());
1633 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001634 caller()->AddAudioVideoTracks();
1635 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001636 // Start offer/answer exchange and wait for it to complete.
1637 caller()->CreateAndSetAndSignalOffer();
1638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1639 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001640 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1641 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001642 // Wait for all "first packet received" callbacks to be fired.
1643 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001644 absl::c_all_of(caller()->rtp_receiver_observers(),
1645 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1646 return o->first_packet_received();
1647 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001648 kMaxWaitForFramesMs);
1649 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001650 absl::c_all_of(callee()->rtp_receiver_observers(),
1651 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1652 return o->first_packet_received();
1653 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001654 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(
Steve Anton64b626b2019-01-28 17:25:26 -08001662 absl::c_all_of(caller()->rtp_receiver_observers(),
1663 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1664 return o->first_packet_received();
1665 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001666 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001667 absl::c_all_of(callee()->rtp_receiver_observers(),
1668 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1669 return o->first_packet_received();
1670 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001671}
1672
1673class DummyDtmfObserver : public DtmfSenderObserverInterface {
1674 public:
1675 DummyDtmfObserver() : completed_(false) {}
1676
1677 // Implements DtmfSenderObserverInterface.
1678 void OnToneChange(const std::string& tone) override {
1679 tones_.push_back(tone);
1680 if (tone.empty()) {
1681 completed_ = true;
1682 }
1683 }
1684
1685 const std::vector<std::string>& tones() const { return tones_; }
1686 bool completed() const { return completed_; }
1687
1688 private:
1689 bool completed_;
1690 std::vector<std::string> tones_;
1691};
1692
1693// Assumes |sender| already has an audio track added and the offer/answer
1694// exchange is done.
1695void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1696 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001697 // We should be able to get a DTMF sender from the local sender.
1698 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1699 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1700 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001701 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001702 dtmf_sender->RegisterObserver(&observer);
1703
1704 // Test the DtmfSender object just created.
1705 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1706 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1707
1708 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1709 std::vector<std::string> tones = {"1", "a", ""};
1710 EXPECT_EQ(tones, observer.tones());
1711 dtmf_sender->UnregisterObserver();
1712 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1713}
1714
1715// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1716// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001717TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001718 ASSERT_TRUE(CreatePeerConnectionWrappers());
1719 ConnectFakeSignaling();
1720 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001721 caller()->AddAudioTrack();
1722 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001723 caller()->CreateAndSetAndSignalOffer();
1724 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001725 // DTLS must finish before the DTMF sender can be used reliably.
1726 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001727 TestDtmfFromSenderToReceiver(caller(), callee());
1728 TestDtmfFromSenderToReceiver(callee(), caller());
1729}
1730
1731// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1732// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001733TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001734 ASSERT_TRUE(CreatePeerConnectionWrappers());
1735 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001736
deadbeef1dcb1642017-03-29 21:08:16 -07001737 // Do normal offer/answer and wait for some frames to be received in each
1738 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001739 caller()->AddAudioVideoTracks();
1740 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001741 caller()->CreateAndSetAndSignalOffer();
1742 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001743 MediaExpectations media_expectations;
1744 media_expectations.ExpectBidirectionalAudioAndVideo();
1745 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001746 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1747 webrtc::kEnumCounterKeyProtocolDtls));
1748 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1749 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001750}
1751
1752// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001753TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001754 PeerConnectionInterface::RTCConfiguration sdes_config;
1755 sdes_config.enable_dtls_srtp.emplace(false);
1756 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1757 ConnectFakeSignaling();
1758
1759 // Do normal offer/answer and wait for some frames to be received in each
1760 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001761 caller()->AddAudioVideoTracks();
1762 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001763 caller()->CreateAndSetAndSignalOffer();
1764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001765 MediaExpectations media_expectations;
1766 media_expectations.ExpectBidirectionalAudioAndVideo();
1767 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001768 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1769 webrtc::kEnumCounterKeyProtocolSdes));
1770 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1771 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001772}
1773
Steve Anton8c0f7a72017-10-03 10:03:10 -07001774// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1775// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001777 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1778 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1779 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1780 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1781 return pc->GetRemoteAudioSSLCertificate();
1782 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001783 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1784 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1785 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1786 return pc->GetRemoteAudioSSLCertChain();
1787 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001788
1789 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1790 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1791
1792 // Configure each side with a known certificate so they can be compared later.
1793 PeerConnectionInterface::RTCConfiguration caller_config;
1794 caller_config.enable_dtls_srtp.emplace(true);
1795 caller_config.certificates.push_back(caller_cert);
1796 PeerConnectionInterface::RTCConfiguration callee_config;
1797 callee_config.enable_dtls_srtp.emplace(true);
1798 callee_config.certificates.push_back(callee_cert);
1799 ASSERT_TRUE(
1800 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1801 ConnectFakeSignaling();
1802
1803 // When first initialized, there should not be a remote SSL certificate (and
1804 // calling this method should not crash).
1805 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1806 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001807 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1808 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001809
Steve Anton15324772018-01-16 10:26:49 -08001810 caller()->AddAudioTrack();
1811 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001812 caller()->CreateAndSetAndSignalOffer();
1813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1814 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1815
1816 // Once DTLS has been connected, each side should return the other's SSL
1817 // certificate when calling GetRemoteAudioSSLCertificate.
1818
1819 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1820 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001821 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001822 caller_remote_cert->ToPEMString());
1823
1824 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1825 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001826 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001827 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001828
1829 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1830 ASSERT_TRUE(caller_remote_cert_chain);
1831 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1832 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001833 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001834 remote_cert->ToPEMString());
1835
1836 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1837 ASSERT_TRUE(callee_remote_cert_chain);
1838 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1839 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001840 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001841 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001842}
1843
deadbeef1dcb1642017-03-29 21:08:16 -07001844// This test sets up a call between two parties with a source resolution of
1845// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001846TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001847 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1848 ASSERT_TRUE(CreatePeerConnectionWrappers());
1849 ConnectFakeSignaling();
1850
Niels Möller5c7efe72018-05-11 10:34:46 +02001851 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1852 webrtc::FakePeriodicVideoSource::Config config;
1853 config.width = 1280;
1854 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001855 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001856 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1857 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001858
1859 // Do normal offer/answer and wait for at least one frame to be received in
1860 // each direction.
1861 caller()->CreateAndSetAndSignalOffer();
1862 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1863 callee()->min_video_frames_received_per_track() > 0,
1864 kMaxWaitForFramesMs);
1865
1866 // Check rendered aspect ratio.
1867 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1868 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1869 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1870 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1871}
1872
1873// This test sets up an one-way call, with media only from caller to
1874// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001875TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001876 ASSERT_TRUE(CreatePeerConnectionWrappers());
1877 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001878 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001879 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001880 MediaExpectations media_expectations;
1881 media_expectations.CalleeExpectsSomeAudioAndVideo();
1882 media_expectations.CallerExpectsNoAudio();
1883 media_expectations.CallerExpectsNoVideo();
1884 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001885}
1886
1887// This test sets up a audio call initially, with the callee rejecting video
1888// initially. Then later the callee decides to upgrade to audio/video, and
1889// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001890TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001891 ASSERT_TRUE(CreatePeerConnectionWrappers());
1892 ConnectFakeSignaling();
1893 // Initially, offer an audio/video stream from the caller, but refuse to
1894 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001895 caller()->AddAudioVideoTracks();
1896 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001897 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1898 PeerConnectionInterface::RTCOfferAnswerOptions options;
1899 options.offer_to_receive_video = 0;
1900 callee()->SetOfferAnswerOptions(options);
1901 } else {
1902 callee()->SetRemoteOfferHandler([this] {
1903 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1904 });
1905 }
deadbeef1dcb1642017-03-29 21:08:16 -07001906 // Do offer/answer and make sure audio is still received end-to-end.
1907 caller()->CreateAndSetAndSignalOffer();
1908 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001909 {
1910 MediaExpectations media_expectations;
1911 media_expectations.ExpectBidirectionalAudio();
1912 media_expectations.ExpectNoVideo();
1913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1914 }
deadbeef1dcb1642017-03-29 21:08:16 -07001915 // Sanity check that the callee's description has a rejected video section.
1916 ASSERT_NE(nullptr, callee()->pc()->local_description());
1917 const ContentInfo* callee_video_content =
1918 GetFirstVideoContent(callee()->pc()->local_description()->description());
1919 ASSERT_NE(nullptr, callee_video_content);
1920 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001921
deadbeef1dcb1642017-03-29 21:08:16 -07001922 // Now negotiate with video and ensure negotiation succeeds, with video
1923 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001924 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001925 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1926 PeerConnectionInterface::RTCOfferAnswerOptions options;
1927 options.offer_to_receive_video = 1;
1928 callee()->SetOfferAnswerOptions(options);
1929 } else {
1930 callee()->SetRemoteOfferHandler(nullptr);
1931 caller()->SetRemoteOfferHandler([this] {
1932 // The caller creates a new transceiver to receive video on when receiving
1933 // the offer, but by default it is send only.
1934 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001935 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001936 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1937 transceivers[2]->receiver()->media_type());
1938 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1939 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1940 });
1941 }
deadbeef1dcb1642017-03-29 21:08:16 -07001942 callee()->CreateAndSetAndSignalOffer();
1943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001944 {
1945 // Expect additional audio frames to be received after the upgrade.
1946 MediaExpectations media_expectations;
1947 media_expectations.ExpectBidirectionalAudioAndVideo();
1948 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1949 }
deadbeef1dcb1642017-03-29 21:08:16 -07001950}
1951
deadbeef4389b4d2017-09-07 09:07:36 -07001952// Simpler than the above test; just add an audio track to an established
1953// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001954TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001955 ASSERT_TRUE(CreatePeerConnectionWrappers());
1956 ConnectFakeSignaling();
1957 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001958 caller()->AddVideoTrack();
1959 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001960 caller()->CreateAndSetAndSignalOffer();
1961 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1962 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001963 caller()->AddAudioTrack();
1964 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001965 caller()->CreateAndSetAndSignalOffer();
1966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1967 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968 MediaExpectations media_expectations;
1969 media_expectations.ExpectBidirectionalAudioAndVideo();
1970 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001971}
1972
deadbeef1dcb1642017-03-29 21:08:16 -07001973// This test sets up a call that's transferred to a new caller with a different
1974// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001975TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001976 ASSERT_TRUE(CreatePeerConnectionWrappers());
1977 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001978 caller()->AddAudioVideoTracks();
1979 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001980 caller()->CreateAndSetAndSignalOffer();
1981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1982
1983 // Keep the original peer around which will still send packets to the
1984 // receiving client. These SRTP packets will be dropped.
1985 std::unique_ptr<PeerConnectionWrapper> original_peer(
1986 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001987 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001988 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1989 // directly above.
1990 original_peer->pc()->Close();
1991
1992 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001993 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001994 caller()->CreateAndSetAndSignalOffer();
1995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1996 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001997 MediaExpectations media_expectations;
1998 media_expectations.ExpectBidirectionalAudioAndVideo();
1999 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002000}
2001
2002// This test sets up a call that's transferred to a new callee with a different
2003// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002004TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002005 ASSERT_TRUE(CreatePeerConnectionWrappers());
2006 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002007 caller()->AddAudioVideoTracks();
2008 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011
2012 // Keep the original peer around which will still send packets to the
2013 // receiving client. These SRTP packets will be dropped.
2014 std::unique_ptr<PeerConnectionWrapper> original_peer(
2015 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002016 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002017 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2018 // directly above.
2019 original_peer->pc()->Close();
2020
2021 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002022 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002023 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2024 caller()->CreateAndSetAndSignalOffer();
2025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2026 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002027 MediaExpectations media_expectations;
2028 media_expectations.ExpectBidirectionalAudioAndVideo();
2029 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002030}
2031
2032// This test sets up a non-bundled call and negotiates bundling at the same
2033// time as starting an ICE restart. When bundling is in effect in the restart,
2034// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002035TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002036 ASSERT_TRUE(CreatePeerConnectionWrappers());
2037 ConnectFakeSignaling();
2038
Steve Anton15324772018-01-16 10:26:49 -08002039 caller()->AddAudioVideoTracks();
2040 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002041 // Remove the bundle group from the SDP received by the callee.
2042 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2043 desc->RemoveGroupByName("BUNDLE");
2044 });
2045 caller()->CreateAndSetAndSignalOffer();
2046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002047 {
2048 MediaExpectations media_expectations;
2049 media_expectations.ExpectBidirectionalAudioAndVideo();
2050 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2051 }
deadbeef1dcb1642017-03-29 21:08:16 -07002052 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2053 callee()->SetReceivedSdpMunger(nullptr);
2054 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2055 caller()->CreateAndSetAndSignalOffer();
2056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2057
2058 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 {
2060 MediaExpectations media_expectations;
2061 media_expectations.ExpectBidirectionalAudioAndVideo();
2062 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2063 }
deadbeef1dcb1642017-03-29 21:08:16 -07002064}
2065
2066// Test CVO (Coordination of Video Orientation). If a video source is rotated
2067// and both peers support the CVO RTP header extension, the actual video frames
2068// don't need to be encoded in different resolutions, since the rotation is
2069// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002070TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002071 ASSERT_TRUE(CreatePeerConnectionWrappers());
2072 ConnectFakeSignaling();
2073 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002074 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002075 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002076 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002077 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2078
2079 // Wait for video frames to be received by both sides.
2080 caller()->CreateAndSetAndSignalOffer();
2081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2082 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2083 callee()->min_video_frames_received_per_track() > 0,
2084 kMaxWaitForFramesMs);
2085
2086 // Ensure that the aspect ratio is unmodified.
2087 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2088 // not just assumed.
2089 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2090 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2091 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2092 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2093 // Ensure that the CVO bits were surfaced to the renderer.
2094 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2095 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2096}
2097
2098// Test that when the CVO extension isn't supported, video is rotated the
2099// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002100TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002101 ASSERT_TRUE(CreatePeerConnectionWrappers());
2102 ConnectFakeSignaling();
2103 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002104 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002105 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002106 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002107 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2108
2109 // Remove the CVO extension from the offered SDP.
2110 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2111 cricket::VideoContentDescription* video =
2112 GetFirstVideoContentDescription(desc);
2113 video->ClearRtpHeaderExtensions();
2114 });
2115 // Wait for video frames to be received by both sides.
2116 caller()->CreateAndSetAndSignalOffer();
2117 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2118 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2119 callee()->min_video_frames_received_per_track() > 0,
2120 kMaxWaitForFramesMs);
2121
2122 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2123 // rotation.
2124 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2125 // not just assumed.
2126 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2127 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2128 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2129 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2130 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2131 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2132 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2133}
2134
deadbeef1dcb1642017-03-29 21:08:16 -07002135// Test that if the answerer rejects the audio m= section, no audio is sent or
2136// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002137TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002140 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002141 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2142 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2143 // it will reject the audio m= section completely.
2144 PeerConnectionInterface::RTCOfferAnswerOptions options;
2145 options.offer_to_receive_audio = 0;
2146 callee()->SetOfferAnswerOptions(options);
2147 } else {
2148 // Stopping the audio RtpTransceiver will cause the media section to be
2149 // rejected in the answer.
2150 callee()->SetRemoteOfferHandler([this] {
2151 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2152 });
2153 }
Steve Anton15324772018-01-16 10:26:49 -08002154 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002155 // Do offer/answer and wait for successful end-to-end video frames.
2156 caller()->CreateAndSetAndSignalOffer();
2157 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002158 MediaExpectations media_expectations;
2159 media_expectations.ExpectBidirectionalVideo();
2160 media_expectations.ExpectNoAudio();
2161 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2162
deadbeef1dcb1642017-03-29 21:08:16 -07002163 // Sanity check that the callee's description has a rejected audio section.
2164 ASSERT_NE(nullptr, callee()->pc()->local_description());
2165 const ContentInfo* callee_audio_content =
2166 GetFirstAudioContent(callee()->pc()->local_description()->description());
2167 ASSERT_NE(nullptr, callee_audio_content);
2168 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002169 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2170 // The caller's transceiver should have stopped after receiving the answer.
2171 EXPECT_TRUE(caller()
2172 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2173 ->stopped());
2174 }
deadbeef1dcb1642017-03-29 21:08:16 -07002175}
2176
2177// Test that if the answerer rejects the video m= section, no video is sent or
2178// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002179TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002180 ASSERT_TRUE(CreatePeerConnectionWrappers());
2181 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002182 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002183 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2184 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2185 // it will reject the video m= section completely.
2186 PeerConnectionInterface::RTCOfferAnswerOptions options;
2187 options.offer_to_receive_video = 0;
2188 callee()->SetOfferAnswerOptions(options);
2189 } else {
2190 // Stopping the video RtpTransceiver will cause the media section to be
2191 // rejected in the answer.
2192 callee()->SetRemoteOfferHandler([this] {
2193 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2194 });
2195 }
Steve Anton15324772018-01-16 10:26:49 -08002196 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002197 // Do offer/answer and wait for successful end-to-end audio frames.
2198 caller()->CreateAndSetAndSignalOffer();
2199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002200 MediaExpectations media_expectations;
2201 media_expectations.ExpectBidirectionalAudio();
2202 media_expectations.ExpectNoVideo();
2203 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2204
deadbeef1dcb1642017-03-29 21:08:16 -07002205 // Sanity check that the callee's description has a rejected video section.
2206 ASSERT_NE(nullptr, callee()->pc()->local_description());
2207 const ContentInfo* callee_video_content =
2208 GetFirstVideoContent(callee()->pc()->local_description()->description());
2209 ASSERT_NE(nullptr, callee_video_content);
2210 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2212 // The caller's transceiver should have stopped after receiving the answer.
2213 EXPECT_TRUE(caller()
2214 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2215 ->stopped());
2216 }
deadbeef1dcb1642017-03-29 21:08:16 -07002217}
2218
2219// Test that if the answerer rejects both audio and video m= sections, nothing
2220// bad happens.
2221// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2222// test anything but the fact that negotiation succeeds, which doesn't mean
2223// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002224TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002225 ASSERT_TRUE(CreatePeerConnectionWrappers());
2226 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002227 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002228 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2229 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2230 // will reject both audio and video m= sections.
2231 PeerConnectionInterface::RTCOfferAnswerOptions options;
2232 options.offer_to_receive_audio = 0;
2233 options.offer_to_receive_video = 0;
2234 callee()->SetOfferAnswerOptions(options);
2235 } else {
2236 callee()->SetRemoteOfferHandler([this] {
2237 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002238 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002239 transceiver->Stop();
2240 }
2241 });
2242 }
deadbeef1dcb1642017-03-29 21:08:16 -07002243 // Do offer/answer and wait for stable signaling state.
2244 caller()->CreateAndSetAndSignalOffer();
2245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002246
deadbeef1dcb1642017-03-29 21:08:16 -07002247 // Sanity check that the callee's description has rejected m= sections.
2248 ASSERT_NE(nullptr, callee()->pc()->local_description());
2249 const ContentInfo* callee_audio_content =
2250 GetFirstAudioContent(callee()->pc()->local_description()->description());
2251 ASSERT_NE(nullptr, callee_audio_content);
2252 EXPECT_TRUE(callee_audio_content->rejected);
2253 const ContentInfo* callee_video_content =
2254 GetFirstVideoContent(callee()->pc()->local_description()->description());
2255 ASSERT_NE(nullptr, callee_video_content);
2256 EXPECT_TRUE(callee_video_content->rejected);
2257}
2258
2259// This test sets up an audio and video call between two parties. After the
2260// call runs for a while, the caller sends an updated offer with video being
2261// rejected. Once the re-negotiation is done, the video flow should stop and
2262// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002263TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002264 ASSERT_TRUE(CreatePeerConnectionWrappers());
2265 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002266 caller()->AddAudioVideoTracks();
2267 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002268 caller()->CreateAndSetAndSignalOffer();
2269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002270 {
2271 MediaExpectations media_expectations;
2272 media_expectations.ExpectBidirectionalAudioAndVideo();
2273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2274 }
deadbeef1dcb1642017-03-29 21:08:16 -07002275 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002276 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2277 caller()->SetGeneratedSdpMunger(
2278 [](cricket::SessionDescription* description) {
2279 for (cricket::ContentInfo& content : description->contents()) {
2280 if (cricket::IsVideoContent(&content)) {
2281 content.rejected = true;
2282 }
2283 }
2284 });
2285 } else {
2286 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2287 }
deadbeef1dcb1642017-03-29 21:08:16 -07002288 caller()->CreateAndSetAndSignalOffer();
2289 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2290
2291 // Sanity check that the caller's description has a rejected video section.
2292 ASSERT_NE(nullptr, caller()->pc()->local_description());
2293 const ContentInfo* caller_video_content =
2294 GetFirstVideoContent(caller()->pc()->local_description()->description());
2295 ASSERT_NE(nullptr, caller_video_content);
2296 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002297 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002298 {
2299 MediaExpectations media_expectations;
2300 media_expectations.ExpectBidirectionalAudio();
2301 media_expectations.ExpectNoVideo();
2302 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2303 }
deadbeef1dcb1642017-03-29 21:08:16 -07002304}
2305
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002306// Do one offer/answer with audio, another that disables it (rejecting the m=
2307// section), and another that re-enables it. Regression test for:
2308// bugs.webrtc.org/6023
2309TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2310 ASSERT_TRUE(CreatePeerConnectionWrappers());
2311 ConnectFakeSignaling();
2312
2313 // Add audio track, do normal offer/answer.
2314 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2315 caller()->CreateLocalAudioTrack();
2316 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2317 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2318 caller()->CreateAndSetAndSignalOffer();
2319 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2320
2321 // Remove audio track, and set offer_to_receive_audio to false to cause the
2322 // m= section to be completely disabled, not just "recvonly".
2323 caller()->pc()->RemoveTrack(sender);
2324 PeerConnectionInterface::RTCOfferAnswerOptions options;
2325 options.offer_to_receive_audio = 0;
2326 caller()->SetOfferAnswerOptions(options);
2327 caller()->CreateAndSetAndSignalOffer();
2328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2329
2330 // Add the audio track again, expecting negotiation to succeed and frames to
2331 // flow.
2332 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2333 options.offer_to_receive_audio = 1;
2334 caller()->SetOfferAnswerOptions(options);
2335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2337
2338 MediaExpectations media_expectations;
2339 media_expectations.CalleeExpectsSomeAudio();
2340 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2341}
2342
deadbeef1dcb1642017-03-29 21:08:16 -07002343// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2344// is needed to support legacy endpoints.
2345// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2346// add a test for an end-to-end test without MID signaling either (basically,
2347// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002348TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002349 ASSERT_TRUE(CreatePeerConnectionWrappers());
2350 ConnectFakeSignaling();
2351 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002352 caller()->AddAudioVideoTracks();
2353 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002354 // Remove SSRCs and MSIDs from the received offer SDP.
2355 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002356 caller()->CreateAndSetAndSignalOffer();
2357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002358 MediaExpectations media_expectations;
2359 media_expectations.ExpectBidirectionalAudioAndVideo();
2360 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002361}
2362
Seth Hampson5897a6e2018-04-03 11:16:33 -07002363// Basic end-to-end test, without SSRC signaling. This means that the track
2364// was created properly and frames are delivered when the MSIDs are communicated
2365// with a=msid lines and no a=ssrc lines.
2366TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2367 EndToEndCallWithoutSsrcSignaling) {
2368 const char kStreamId[] = "streamId";
2369 ASSERT_TRUE(CreatePeerConnectionWrappers());
2370 ConnectFakeSignaling();
2371 // Add just audio tracks.
2372 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2373 callee()->AddAudioTrack();
2374
2375 // Remove SSRCs from the received offer SDP.
2376 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2377 caller()->CreateAndSetAndSignalOffer();
2378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2379 MediaExpectations media_expectations;
2380 media_expectations.ExpectBidirectionalAudio();
2381 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2382}
2383
Steve Antondf527fd2018-04-27 15:52:03 -07002384// Tests that video flows between multiple video tracks when SSRCs are not
2385// signaled. This exercises the MID RTP header extension which is needed to
2386// demux the incoming video tracks.
2387TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2388 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2389 ASSERT_TRUE(CreatePeerConnectionWrappers());
2390 ConnectFakeSignaling();
2391 caller()->AddVideoTrack();
2392 caller()->AddVideoTrack();
2393 callee()->AddVideoTrack();
2394 callee()->AddVideoTrack();
2395
2396 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2397 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2398 caller()->CreateAndSetAndSignalOffer();
2399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2400 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2401 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2402
2403 // Expect video to be received in both directions on both tracks.
2404 MediaExpectations media_expectations;
2405 media_expectations.ExpectBidirectionalVideo();
2406 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2407}
2408
Henrik Boström5b147782018-12-04 11:25:05 +01002409TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2410 ASSERT_TRUE(CreatePeerConnectionWrappers());
2411 ConnectFakeSignaling();
2412 caller()->AddAudioTrack();
2413 caller()->AddVideoTrack();
2414 caller()->CreateAndSetAndSignalOffer();
2415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2416 auto callee_receivers = callee()->pc()->GetReceivers();
2417 ASSERT_EQ(2u, callee_receivers.size());
2418 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2419 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2420}
2421
2422TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2423 ASSERT_TRUE(CreatePeerConnectionWrappers());
2424 ConnectFakeSignaling();
2425 caller()->AddAudioTrack();
2426 caller()->AddVideoTrack();
2427 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2428 caller()->CreateAndSetAndSignalOffer();
2429 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2430 auto callee_receivers = callee()->pc()->GetReceivers();
2431 ASSERT_EQ(2u, callee_receivers.size());
2432 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2433 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2434 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2435 callee_receivers[1]->stream_ids()[0]);
2436 EXPECT_EQ(callee_receivers[0]->streams()[0],
2437 callee_receivers[1]->streams()[0]);
2438}
2439
deadbeef1dcb1642017-03-29 21:08:16 -07002440// Test that if two video tracks are sent (from caller to callee, in this test),
2441// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002442TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002443 ASSERT_TRUE(CreatePeerConnectionWrappers());
2444 ConnectFakeSignaling();
2445 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002446 caller()->AddAudioVideoTracks();
2447 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002448 caller()->CreateAndSetAndSignalOffer();
2449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002450 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002451
2452 MediaExpectations media_expectations;
2453 media_expectations.CalleeExpectsSomeAudioAndVideo();
2454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002455}
2456
2457static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2458 bool first = true;
2459 for (cricket::ContentInfo& content : desc->contents()) {
2460 if (first) {
2461 first = false;
2462 continue;
2463 }
2464 content.bundle_only = true;
2465 }
2466 first = true;
2467 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2468 if (first) {
2469 first = false;
2470 continue;
2471 }
2472 transport.description.ice_ufrag.clear();
2473 transport.description.ice_pwd.clear();
2474 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2475 transport.description.identity_fingerprint.reset(nullptr);
2476 }
2477}
2478
2479// Test that if applying a true "max bundle" offer, which uses ports of 0,
2480// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2481// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2482// successfully and media flows.
2483// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2484// TODO(deadbeef): Won't need this test once we start generating actual
2485// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002486TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002487 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2488 ASSERT_TRUE(CreatePeerConnectionWrappers());
2489 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002490 caller()->AddAudioVideoTracks();
2491 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002492 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2493 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2494 // but the first m= section.
2495 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2496 caller()->CreateAndSetAndSignalOffer();
2497 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002498 MediaExpectations media_expectations;
2499 media_expectations.ExpectBidirectionalAudioAndVideo();
2500 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002501}
2502
2503// Test that we can receive the audio output level from a remote audio track.
2504// TODO(deadbeef): Use a fake audio source and verify that the output level is
2505// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002506TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002507 ASSERT_TRUE(CreatePeerConnectionWrappers());
2508 ConnectFakeSignaling();
2509 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002510 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002511 caller()->CreateAndSetAndSignalOffer();
2512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2513
2514 // Get the audio output level stats. Note that the level is not available
2515 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002516 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002517 kMaxWaitForFramesMs);
2518}
2519
2520// Test that an audio input level is reported.
2521// TODO(deadbeef): Use a fake audio source and verify that the input level is
2522// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002523TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002524 ASSERT_TRUE(CreatePeerConnectionWrappers());
2525 ConnectFakeSignaling();
2526 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002527 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002528 caller()->CreateAndSetAndSignalOffer();
2529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2530
2531 // Get the audio input level stats. The level should be available very
2532 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002533 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002534 kMaxWaitForStatsMs);
2535}
2536
2537// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002538TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002539 ASSERT_TRUE(CreatePeerConnectionWrappers());
2540 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002541 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002542 // Do offer/answer, wait for the callee to receive some frames.
2543 caller()->CreateAndSetAndSignalOffer();
2544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002545
2546 MediaExpectations media_expectations;
2547 media_expectations.CalleeExpectsSomeAudioAndVideo();
2548 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002549
2550 // Get a handle to the remote tracks created, so they can be used as GetStats
2551 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002552 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002553 // We received frames, so we definitely should have nonzero "received bytes"
2554 // stats at this point.
2555 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2556 0);
2557 }
deadbeef1dcb1642017-03-29 21:08:16 -07002558}
2559
2560// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002561TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002562 ASSERT_TRUE(CreatePeerConnectionWrappers());
2563 ConnectFakeSignaling();
2564 auto audio_track = caller()->CreateLocalAudioTrack();
2565 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002566 caller()->AddTrack(audio_track);
2567 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002568 // Do offer/answer, wait for the callee to receive some frames.
2569 caller()->CreateAndSetAndSignalOffer();
2570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002571 MediaExpectations media_expectations;
2572 media_expectations.CalleeExpectsSomeAudioAndVideo();
2573 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002574
2575 // The callee received frames, so we definitely should have nonzero "sent
2576 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002577 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2578 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2579}
2580
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002581// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002582TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002583 ASSERT_TRUE(CreatePeerConnectionWrappers());
2584 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002585 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002586
Steve Anton15324772018-01-16 10:26:49 -08002587 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002588
2589 // Do offer/answer, wait for the callee to receive some frames.
2590 caller()->CreateAndSetAndSignalOffer();
2591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2592
2593 // Get the remote audio track created on the receiver, so they can be used as
2594 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002595 auto receivers = callee()->pc()->GetReceivers();
2596 ASSERT_EQ(1u, receivers.size());
2597 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002598
2599 // Get the audio output level stats. Note that the level is not available
2600 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002601 EXPECT_TRUE_WAIT(
2602 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2603 0,
2604 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002605}
2606
Steve Antona41959e2018-11-28 11:15:33 -08002607// Test that the track ID is associated with all local and remote SSRC stats
2608// using the old GetStats() and more than 1 audio and more than 1 video track.
2609// This is a regression test for crbug.com/906988
2610TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2611 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2612 ASSERT_TRUE(CreatePeerConnectionWrappers());
2613 ConnectFakeSignaling();
2614 auto audio_sender_1 = caller()->AddAudioTrack();
2615 auto video_sender_1 = caller()->AddVideoTrack();
2616 auto audio_sender_2 = caller()->AddAudioTrack();
2617 auto video_sender_2 = caller()->AddVideoTrack();
2618 caller()->CreateAndSetAndSignalOffer();
2619 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2620
2621 MediaExpectations media_expectations;
2622 media_expectations.CalleeExpectsSomeAudioAndVideo();
2623 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2624
2625 std::vector<std::string> track_ids = {
2626 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2627 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2628
2629 auto caller_stats = caller()->OldGetStats();
2630 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2631 auto callee_stats = callee()->OldGetStats();
2632 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2633}
2634
Steve Antonffa6ce42018-11-30 09:26:08 -08002635// Test that the new GetStats() returns stats for all outgoing/incoming streams
2636// with the correct track IDs if there are more than one audio and more than one
2637// video senders/receivers.
2638TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2639 ASSERT_TRUE(CreatePeerConnectionWrappers());
2640 ConnectFakeSignaling();
2641 auto audio_sender_1 = caller()->AddAudioTrack();
2642 auto video_sender_1 = caller()->AddVideoTrack();
2643 auto audio_sender_2 = caller()->AddAudioTrack();
2644 auto video_sender_2 = caller()->AddVideoTrack();
2645 caller()->CreateAndSetAndSignalOffer();
2646 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2647
2648 MediaExpectations media_expectations;
2649 media_expectations.CalleeExpectsSomeAudioAndVideo();
2650 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2651
2652 std::vector<std::string> track_ids = {
2653 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2654 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2655
2656 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2657 caller()->NewGetStats();
2658 ASSERT_TRUE(caller_report);
2659 auto outbound_stream_stats =
2660 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2661 ASSERT_EQ(4u, outbound_stream_stats.size());
2662 std::vector<std::string> outbound_track_ids;
2663 for (const auto& stat : outbound_stream_stats) {
2664 ASSERT_TRUE(stat->bytes_sent.is_defined());
2665 EXPECT_LT(0u, *stat->bytes_sent);
2666 ASSERT_TRUE(stat->track_id.is_defined());
2667 const auto* track_stat =
2668 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2669 ASSERT_TRUE(track_stat);
2670 outbound_track_ids.push_back(*track_stat->track_identifier);
2671 }
2672 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2673
2674 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2675 callee()->NewGetStats();
2676 ASSERT_TRUE(callee_report);
2677 auto inbound_stream_stats =
2678 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2679 ASSERT_EQ(4u, inbound_stream_stats.size());
2680 std::vector<std::string> inbound_track_ids;
2681 for (const auto& stat : inbound_stream_stats) {
2682 ASSERT_TRUE(stat->bytes_received.is_defined());
2683 EXPECT_LT(0u, *stat->bytes_received);
2684 ASSERT_TRUE(stat->track_id.is_defined());
2685 const auto* track_stat =
2686 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2687 ASSERT_TRUE(track_stat);
2688 inbound_track_ids.push_back(*track_stat->track_identifier);
2689 }
2690 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2691}
2692
2693// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002694// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2695// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002696TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002697 GetStatsForUnsignaledStreamWithNewStatsApi) {
2698 ASSERT_TRUE(CreatePeerConnectionWrappers());
2699 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002700 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002701 // Remove SSRCs and MSIDs from the received offer SDP.
2702 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2703 caller()->CreateAndSetAndSignalOffer();
2704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002705 MediaExpectations media_expectations;
2706 media_expectations.CalleeExpectsSomeAudio(1);
2707 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002708
2709 // We received a frame, so we should have nonzero "bytes received" stats for
2710 // the unsignaled stream, if stats are working for it.
2711 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2712 callee()->NewGetStats();
2713 ASSERT_NE(nullptr, report);
2714 auto inbound_stream_stats =
2715 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2716 ASSERT_EQ(1U, inbound_stream_stats.size());
2717 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2718 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002719 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2720}
2721
Taylor Brandstettera4653442018-06-19 09:44:26 -07002722// Same as above but for the legacy stats implementation.
2723TEST_P(PeerConnectionIntegrationTest,
2724 GetStatsForUnsignaledStreamWithOldStatsApi) {
2725 ASSERT_TRUE(CreatePeerConnectionWrappers());
2726 ConnectFakeSignaling();
2727 caller()->AddAudioTrack();
2728 // Remove SSRCs and MSIDs from the received offer SDP.
2729 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2730 caller()->CreateAndSetAndSignalOffer();
2731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2732
2733 // Note that, since the old stats implementation associates SSRCs with tracks
2734 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2735 // associated track ID. So we can't use the track "selector" argument.
2736 //
2737 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2738 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002739 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002740 kDefaultTimeout);
2741}
2742
zhihuangf8164932017-05-19 13:09:47 -07002743// Test that we can successfully get the media related stats (audio level
2744// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002745TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002746 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2747 ASSERT_TRUE(CreatePeerConnectionWrappers());
2748 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002749 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002750 // Remove SSRCs and MSIDs from the received offer SDP.
2751 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2752 caller()->CreateAndSetAndSignalOffer();
2753 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002754 MediaExpectations media_expectations;
2755 media_expectations.CalleeExpectsSomeAudio(1);
2756 media_expectations.CalleeExpectsSomeVideo(1);
2757 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002758
2759 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2760 callee()->NewGetStats();
2761 ASSERT_NE(nullptr, report);
2762
2763 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2764 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2765 ASSERT_GE(audio_index, 0);
2766 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002767}
2768
deadbeef4e2deab2017-09-20 13:56:21 -07002769// Helper for test below.
2770void ModifySsrcs(cricket::SessionDescription* desc) {
2771 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002772 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002773 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002774 for (uint32_t& ssrc : stream.ssrcs) {
2775 ssrc = rtc::CreateRandomId();
2776 }
2777 }
2778 }
2779}
2780
2781// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2782// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2783// This should result in two "RTCInboundRTPStreamStats", but only one
2784// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2785// being reset to 0 once the SSRC change occurs.
2786//
2787// Regression test for this bug:
2788// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2789//
2790// The bug causes the track stats to only represent one of the two streams:
2791// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2792// that the track stat counters would reset to 0 when the new stream is
2793// received, and a 50% chance that they'll stop updating (while
2794// "concealed_samples" continues increasing, due to silence being generated for
2795// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002796TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002797 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002798 ASSERT_TRUE(CreatePeerConnectionWrappers());
2799 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002800 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002801 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2802 // that doesn't signal SSRCs (from the callee's perspective).
2803 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2804 caller()->CreateAndSetAndSignalOffer();
2805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2806 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002807 {
2808 MediaExpectations media_expectations;
2809 media_expectations.CalleeExpectsSomeAudio(50);
2810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2811 }
deadbeef4e2deab2017-09-20 13:56:21 -07002812 // Some audio frames were received, so we should have nonzero "samples
2813 // received" for the track.
2814 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2815 callee()->NewGetStats();
2816 ASSERT_NE(nullptr, report);
2817 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2818 ASSERT_EQ(1U, track_stats.size());
2819 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2820 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2821 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2822
2823 // Create a new offer and munge it to cause the caller to use a new SSRC.
2824 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2825 caller()->CreateAndSetAndSignalOffer();
2826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2827 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2828 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002829 {
2830 MediaExpectations media_expectations;
2831 media_expectations.CalleeExpectsSomeAudio(25);
2832 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2833 }
deadbeef4e2deab2017-09-20 13:56:21 -07002834
2835 report = callee()->NewGetStats();
2836 ASSERT_NE(nullptr, report);
2837 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2838 ASSERT_EQ(1U, track_stats.size());
2839 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2840 // The "total samples received" stat should only be greater than it was
2841 // before.
2842 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2843 // Right now, the new SSRC will cause the counters to reset to 0.
2844 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2845
2846 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002847 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002848 // good sign that we're seeing stats from the old stream that's no longer
2849 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002850 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002851 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2852 EXPECT_LT(*track_stats[0]->concealed_samples,
2853 *track_stats[0]->total_samples_received *
2854 kAcceptableConcealedSamplesPercentage);
2855
2856 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2857 // sanity check that the SSRC really changed.
2858 // TODO(deadbeef): This isn't working right now, because we're not returning
2859 // *any* stats for the inactive stream. Uncomment when the bug is completely
2860 // fixed.
2861 // auto inbound_stream_stats =
2862 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2863 // ASSERT_EQ(2U, inbound_stream_stats.size());
2864}
2865
deadbeef1dcb1642017-03-29 21:08:16 -07002866// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002867TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002868 PeerConnectionFactory::Options dtls_10_options;
2869 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2870 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2871 dtls_10_options));
2872 ConnectFakeSignaling();
2873 // Do normal offer/answer and wait for some frames to be received in each
2874 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002875 caller()->AddAudioVideoTracks();
2876 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002877 caller()->CreateAndSetAndSignalOffer();
2878 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002879 MediaExpectations media_expectations;
2880 media_expectations.ExpectBidirectionalAudioAndVideo();
2881 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002882}
2883
2884// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002885TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002886 PeerConnectionFactory::Options dtls_10_options;
2887 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2888 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2889 dtls_10_options));
2890 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002891 caller()->AddAudioVideoTracks();
2892 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002893 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002894 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002895 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002896 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002897 kDefaultTimeout);
2898 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002899 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002900 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002901 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002902 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2903 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002904}
2905
2906// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002907TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002908 PeerConnectionFactory::Options dtls_12_options;
2909 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2910 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2911 dtls_12_options));
2912 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002913 caller()->AddAudioVideoTracks();
2914 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002915 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002916 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002917 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002918 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002919 kDefaultTimeout);
2920 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002921 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002922 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002923 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002924 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2925 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002926}
2927
2928// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2929// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002930TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002931 PeerConnectionFactory::Options caller_options;
2932 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2933 PeerConnectionFactory::Options callee_options;
2934 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2935 ASSERT_TRUE(
2936 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2937 ConnectFakeSignaling();
2938 // Do normal offer/answer and wait for some frames to be received in each
2939 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002940 caller()->AddAudioVideoTracks();
2941 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002942 caller()->CreateAndSetAndSignalOffer();
2943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002944 MediaExpectations media_expectations;
2945 media_expectations.ExpectBidirectionalAudioAndVideo();
2946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002947}
2948
2949// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2950// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002951TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002952 PeerConnectionFactory::Options caller_options;
2953 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2954 PeerConnectionFactory::Options callee_options;
2955 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2956 ASSERT_TRUE(
2957 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2958 ConnectFakeSignaling();
2959 // Do normal offer/answer and wait for some frames to be received in each
2960 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002961 caller()->AddAudioVideoTracks();
2962 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002963 caller()->CreateAndSetAndSignalOffer();
2964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002965 MediaExpectations media_expectations;
2966 media_expectations.ExpectBidirectionalAudioAndVideo();
2967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002968}
2969
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002970// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2971// works as expected; the cipher should only be used if enabled by both sides.
2972TEST_P(PeerConnectionIntegrationTest,
2973 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2974 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002975 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002976 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002977 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2978 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002979 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2980 TestNegotiatedCipherSuite(caller_options, callee_options,
2981 expected_cipher_suite);
2982}
2983
2984TEST_P(PeerConnectionIntegrationTest,
2985 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2986 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002987 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2988 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002989 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002990 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002991 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2992 TestNegotiatedCipherSuite(caller_options, callee_options,
2993 expected_cipher_suite);
2994}
2995
2996TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2997 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002998 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002999 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003000 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003001 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3002 TestNegotiatedCipherSuite(caller_options, callee_options,
3003 expected_cipher_suite);
3004}
3005
deadbeef1dcb1642017-03-29 21:08:16 -07003006// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003007TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003008 bool local_gcm_enabled = false;
3009 bool remote_gcm_enabled = false;
3010 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3011 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3012 expected_cipher_suite);
3013}
3014
3015// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003016TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003017 bool local_gcm_enabled = true;
3018 bool remote_gcm_enabled = true;
3019 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3020 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3021 expected_cipher_suite);
3022}
3023
3024// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003025TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003026 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3027 bool local_gcm_enabled = true;
3028 bool remote_gcm_enabled = false;
3029 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3030 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3031 expected_cipher_suite);
3032}
3033
3034// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003035TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003036 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3037 bool local_gcm_enabled = false;
3038 bool remote_gcm_enabled = true;
3039 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3040 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3041 expected_cipher_suite);
3042}
3043
deadbeef7914b8c2017-04-21 03:23:33 -07003044// Verify that media can be transmitted end-to-end when GCM crypto suites are
3045// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3046// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3047// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003048TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003049 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003050 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003051 ASSERT_TRUE(
3052 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3053 ConnectFakeSignaling();
3054 // Do normal offer/answer and wait for some frames to be received in each
3055 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003056 caller()->AddAudioVideoTracks();
3057 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003058 caller()->CreateAndSetAndSignalOffer();
3059 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003060 MediaExpectations media_expectations;
3061 media_expectations.ExpectBidirectionalAudioAndVideo();
3062 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003063}
3064
deadbeef1dcb1642017-03-29 21:08:16 -07003065// This test sets up a call between two parties with audio, video and an RTP
3066// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003067TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003068 PeerConnectionInterface::RTCConfiguration rtc_config;
3069 rtc_config.enable_rtp_data_channel = true;
3070 rtc_config.enable_dtls_srtp = false;
3071 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003072 ConnectFakeSignaling();
3073 // Expect that data channel created on caller side will show up for callee as
3074 // well.
3075 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003076 caller()->AddAudioVideoTracks();
3077 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003078 caller()->CreateAndSetAndSignalOffer();
3079 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3080 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003081 MediaExpectations media_expectations;
3082 media_expectations.ExpectBidirectionalAudioAndVideo();
3083 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003084 ASSERT_NE(nullptr, caller()->data_channel());
3085 ASSERT_NE(nullptr, callee()->data_channel());
3086 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3087 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3088
3089 // Ensure data can be sent in both directions.
3090 std::string data = "hello world";
3091 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3092 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3093 kDefaultTimeout);
3094 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3095 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3096 kDefaultTimeout);
3097}
3098
3099// Ensure that an RTP data channel is signaled as closed for the caller when
3100// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003101TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003102 RtpDataChannelSignaledClosedInCalleeOffer) {
3103 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003104 PeerConnectionInterface::RTCConfiguration rtc_config;
3105 rtc_config.enable_rtp_data_channel = true;
3106 rtc_config.enable_dtls_srtp = false;
3107 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003108 ConnectFakeSignaling();
3109 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003110 caller()->AddAudioVideoTracks();
3111 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003112 caller()->CreateAndSetAndSignalOffer();
3113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3114 ASSERT_NE(nullptr, caller()->data_channel());
3115 ASSERT_NE(nullptr, callee()->data_channel());
3116 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3117 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3118
3119 // Close the data channel on the callee, and do an updated offer/answer.
3120 callee()->data_channel()->Close();
3121 callee()->CreateAndSetAndSignalOffer();
3122 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3123 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3124 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3125}
3126
3127// Tests that data is buffered in an RTP data channel until an observer is
3128// registered for it.
3129//
3130// NOTE: RTP data channels can receive data before the underlying
3131// transport has detected that a channel is writable and thus data can be
3132// received before the data channel state changes to open. That is hard to test
3133// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003134TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003135 DataBufferedUntilRtpDataChannelObserverRegistered) {
3136 // Use fake clock and simulated network delay so that we predictably can wait
3137 // until an SCTP message has been delivered without "sleep()"ing.
3138 rtc::ScopedFakeClock fake_clock;
3139 // Some things use a time of "0" as a special value, so we need to start out
3140 // the fake clock at a nonzero time.
3141 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003142 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003143 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3144 virtual_socket_server()->UpdateDelayDistribution();
3145
Niels Möllerf06f9232018-08-07 12:32:18 +02003146 PeerConnectionInterface::RTCConfiguration rtc_config;
3147 rtc_config.enable_rtp_data_channel = true;
3148 rtc_config.enable_dtls_srtp = false;
3149 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003150 ConnectFakeSignaling();
3151 caller()->CreateDataChannel();
3152 caller()->CreateAndSetAndSignalOffer();
3153 ASSERT_TRUE(caller()->data_channel() != nullptr);
3154 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3155 kDefaultTimeout, fake_clock);
3156 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3157 kDefaultTimeout, fake_clock);
3158 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3159 callee()->data_channel()->state(), kDefaultTimeout,
3160 fake_clock);
3161
3162 // Unregister the observer which is normally automatically registered.
3163 callee()->data_channel()->UnregisterObserver();
3164 // Send data and advance fake clock until it should have been received.
3165 std::string data = "hello world";
3166 caller()->data_channel()->Send(DataBuffer(data));
3167 SIMULATED_WAIT(false, 50, fake_clock);
3168
3169 // Attach data channel and expect data to be received immediately. Note that
3170 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3171 // further, but data can be received even if the callback is asynchronous.
3172 MockDataChannelObserver new_observer(callee()->data_channel());
3173 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3174 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003175 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3176 // If this is not done a DCHECK can be hit in ports.cc, because a large
3177 // negative number is calculated for the rtt due to the global clock changing.
3178 caller()->pc()->Close();
3179 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003180}
3181
3182// This test sets up a call between two parties with audio, video and but only
3183// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003184TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003185 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3186 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003187 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003188 rtc_config_1.enable_dtls_srtp = false;
3189 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3190 rtc_config_2.enable_dtls_srtp = false;
3191 rtc_config_2.enable_dtls_srtp = false;
3192 ASSERT_TRUE(
3193 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003194 ConnectFakeSignaling();
3195 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003196 caller()->AddAudioVideoTracks();
3197 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003198 caller()->CreateAndSetAndSignalOffer();
3199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3200 // The caller should still have a data channel, but it should be closed, and
3201 // one should ever have been created for the callee.
3202 EXPECT_TRUE(caller()->data_channel() != nullptr);
3203 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3204 EXPECT_EQ(nullptr, callee()->data_channel());
3205}
3206
3207// This test sets up a call between two parties with audio, and video. When
3208// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003209TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003210 PeerConnectionInterface::RTCConfiguration rtc_config;
3211 rtc_config.enable_rtp_data_channel = true;
3212 rtc_config.enable_dtls_srtp = false;
3213 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003214 ConnectFakeSignaling();
3215 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003216 caller()->AddAudioVideoTracks();
3217 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003218 caller()->CreateAndSetAndSignalOffer();
3219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3220 // Create data channel and do new offer and answer.
3221 caller()->CreateDataChannel();
3222 caller()->CreateAndSetAndSignalOffer();
3223 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3224 ASSERT_NE(nullptr, caller()->data_channel());
3225 ASSERT_NE(nullptr, callee()->data_channel());
3226 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3227 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3228 // Ensure data can be sent in both directions.
3229 std::string data = "hello world";
3230 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3231 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3232 kDefaultTimeout);
3233 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3234 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3235 kDefaultTimeout);
3236}
3237
3238#ifdef HAVE_SCTP
3239
3240// This test sets up a call between two parties with audio, video and an SCTP
3241// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003242TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003243 ASSERT_TRUE(CreatePeerConnectionWrappers());
3244 ConnectFakeSignaling();
3245 // Expect that data channel created on caller side will show up for callee as
3246 // well.
3247 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003248 caller()->AddAudioVideoTracks();
3249 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003250 caller()->CreateAndSetAndSignalOffer();
3251 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3252 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003253 MediaExpectations media_expectations;
3254 media_expectations.ExpectBidirectionalAudioAndVideo();
3255 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003256 // Caller data channel should already exist (it created one). Callee data
3257 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3258 ASSERT_NE(nullptr, caller()->data_channel());
3259 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3260 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3261 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3262
3263 // Ensure data can be sent in both directions.
3264 std::string data = "hello world";
3265 caller()->data_channel()->Send(DataBuffer(data));
3266 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3267 kDefaultTimeout);
3268 callee()->data_channel()->Send(DataBuffer(data));
3269 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3270 kDefaultTimeout);
3271}
3272
3273// Ensure that when the callee closes an SCTP data channel, the closing
3274// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003275TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003276 // Same procedure as above test.
3277 ASSERT_TRUE(CreatePeerConnectionWrappers());
3278 ConnectFakeSignaling();
3279 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003280 caller()->AddAudioVideoTracks();
3281 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003282 caller()->CreateAndSetAndSignalOffer();
3283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3284 ASSERT_NE(nullptr, caller()->data_channel());
3285 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3286 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3287 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3288
3289 // Close the data channel on the callee side, and wait for it to reach the
3290 // "closed" state on both sides.
3291 callee()->data_channel()->Close();
3292 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3293 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3294}
3295
Seth Hampson2f0d7022018-02-20 11:54:42 -08003296TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003297 ASSERT_TRUE(CreatePeerConnectionWrappers());
3298 ConnectFakeSignaling();
3299 webrtc::DataChannelInit init;
3300 init.id = 53;
3301 init.maxRetransmits = 52;
3302 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003303 caller()->AddAudioVideoTracks();
3304 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003305 caller()->CreateAndSetAndSignalOffer();
3306 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003307 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3308 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003309 EXPECT_EQ(init.id, callee()->data_channel()->id());
3310 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3311 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3312 EXPECT_FALSE(callee()->data_channel()->negotiated());
3313}
3314
deadbeef1dcb1642017-03-29 21:08:16 -07003315// Test usrsctp's ability to process unordered data stream, where data actually
3316// arrives out of order using simulated delays. Previously there have been some
3317// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003318TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003319 // Introduce random network delays.
3320 // Otherwise it's not a true "unordered" test.
3321 virtual_socket_server()->set_delay_mean(20);
3322 virtual_socket_server()->set_delay_stddev(5);
3323 virtual_socket_server()->UpdateDelayDistribution();
3324 // Normal procedure, but with unordered data channel config.
3325 ASSERT_TRUE(CreatePeerConnectionWrappers());
3326 ConnectFakeSignaling();
3327 webrtc::DataChannelInit init;
3328 init.ordered = false;
3329 caller()->CreateDataChannel(&init);
3330 caller()->CreateAndSetAndSignalOffer();
3331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3332 ASSERT_NE(nullptr, caller()->data_channel());
3333 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3334 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3335 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3336
3337 static constexpr int kNumMessages = 100;
3338 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3339 static constexpr size_t kMaxMessageSize = 4096;
3340 // Create and send random messages.
3341 std::vector<std::string> sent_messages;
3342 for (int i = 0; i < kNumMessages; ++i) {
3343 size_t length =
3344 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3345 std::string message;
3346 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3347 caller()->data_channel()->Send(DataBuffer(message));
3348 callee()->data_channel()->Send(DataBuffer(message));
3349 sent_messages.push_back(message);
3350 }
3351
3352 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003353 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003354 caller()->data_observer()->received_message_count(),
3355 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003356 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003357 callee()->data_observer()->received_message_count(),
3358 kDefaultTimeout);
3359
3360 // Sort and compare to make sure none of the messages were corrupted.
3361 std::vector<std::string> caller_received_messages =
3362 caller()->data_observer()->messages();
3363 std::vector<std::string> callee_received_messages =
3364 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003365 absl::c_sort(sent_messages);
3366 absl::c_sort(caller_received_messages);
3367 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003368 EXPECT_EQ(sent_messages, caller_received_messages);
3369 EXPECT_EQ(sent_messages, callee_received_messages);
3370}
3371
3372// This test sets up a call between two parties with audio, and video. When
3373// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003374TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003375 ASSERT_TRUE(CreatePeerConnectionWrappers());
3376 ConnectFakeSignaling();
3377 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003378 caller()->AddAudioVideoTracks();
3379 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003380 caller()->CreateAndSetAndSignalOffer();
3381 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3382 // Create data channel and do new offer and answer.
3383 caller()->CreateDataChannel();
3384 caller()->CreateAndSetAndSignalOffer();
3385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3386 // Caller data channel should already exist (it created one). Callee data
3387 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3388 ASSERT_NE(nullptr, caller()->data_channel());
3389 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3390 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3391 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3392 // Ensure data can be sent in both directions.
3393 std::string data = "hello world";
3394 caller()->data_channel()->Send(DataBuffer(data));
3395 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3396 kDefaultTimeout);
3397 callee()->data_channel()->Send(DataBuffer(data));
3398 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3399 kDefaultTimeout);
3400}
3401
deadbeef7914b8c2017-04-21 03:23:33 -07003402// Set up a connection initially just using SCTP data channels, later upgrading
3403// to audio/video, ensuring frames are received end-to-end. Effectively the
3404// inverse of the test above.
3405// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003406TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003407 ASSERT_TRUE(CreatePeerConnectionWrappers());
3408 ConnectFakeSignaling();
3409 // Do initial offer/answer with just data channel.
3410 caller()->CreateDataChannel();
3411 caller()->CreateAndSetAndSignalOffer();
3412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3413 // Wait until data can be sent over the data channel.
3414 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3415 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3416 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3417
3418 // Do subsequent offer/answer with two-way audio and video. Audio and video
3419 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003420 caller()->AddAudioVideoTracks();
3421 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003424 MediaExpectations media_expectations;
3425 media_expectations.ExpectBidirectionalAudioAndVideo();
3426 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003427}
3428
deadbeef8b7e9ad2017-05-25 09:38:55 -07003429static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003430 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003431 GetFirstDataContentDescription(desc);
3432 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003433 dcd_offer->set_use_sctpmap(false);
3434 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3435}
3436
3437// Test that the data channel works when a spec-compliant SCTP m= section is
3438// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3439// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003440TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003441 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3442 ASSERT_TRUE(CreatePeerConnectionWrappers());
3443 ConnectFakeSignaling();
3444 caller()->CreateDataChannel();
3445 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3446 caller()->CreateAndSetAndSignalOffer();
3447 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3448 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3449 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3450 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3451
3452 // Ensure data can be sent in both directions.
3453 std::string data = "hello world";
3454 caller()->data_channel()->Send(DataBuffer(data));
3455 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3456 kDefaultTimeout);
3457 callee()->data_channel()->Send(DataBuffer(data));
3458 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3459 kDefaultTimeout);
3460}
3461
deadbeef1dcb1642017-03-29 21:08:16 -07003462#endif // HAVE_SCTP
3463
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003464// This test sets up a call between two parties with a media transport data
3465// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003466TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3467 PeerConnectionInterface::RTCConfiguration rtc_config;
3468 rtc_config.use_media_transport_for_data_channels = true;
3469 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3470 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3471 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3472 loopback_media_transports()->second_factory()));
3473 ConnectFakeSignaling();
3474
3475 // Expect that data channel created on caller side will show up for callee as
3476 // well.
3477 caller()->CreateDataChannel();
3478 caller()->CreateAndSetAndSignalOffer();
3479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3480
3481 // Ensure that the media transport is ready.
3482 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3483 loopback_media_transports()->FlushAsyncInvokes();
3484
3485 // Caller data channel should already exist (it created one). Callee data
3486 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3487 ASSERT_NE(nullptr, caller()->data_channel());
3488 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3489 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3490 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3491
3492 // Ensure data can be sent in both directions.
3493 std::string data = "hello world";
3494 caller()->data_channel()->Send(DataBuffer(data));
3495 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3496 kDefaultTimeout);
3497 callee()->data_channel()->Send(DataBuffer(data));
3498 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3499 kDefaultTimeout);
3500}
3501
3502// Ensure that when the callee closes a media transport data channel, the
3503// closing procedure results in the data channel being closed for the caller
3504// as well.
3505TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3506 PeerConnectionInterface::RTCConfiguration rtc_config;
3507 rtc_config.use_media_transport_for_data_channels = true;
3508 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3509 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3510 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3511 loopback_media_transports()->second_factory()));
3512 ConnectFakeSignaling();
3513
3514 // Create a data channel on the caller and signal it to the callee.
3515 caller()->CreateDataChannel();
3516 caller()->CreateAndSetAndSignalOffer();
3517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3518
3519 // Ensure that the media transport is ready.
3520 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3521 loopback_media_transports()->FlushAsyncInvokes();
3522
3523 // Data channels exist and open on both ends of the connection.
3524 ASSERT_NE(nullptr, caller()->data_channel());
3525 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3526 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3527 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3528
3529 // Close the data channel on the callee side, and wait for it to reach the
3530 // "closed" state on both sides.
3531 callee()->data_channel()->Close();
3532 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3533 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3534}
3535
3536TEST_P(PeerConnectionIntegrationTest,
3537 MediaTransportDataChannelConfigSentToOtherSide) {
3538 PeerConnectionInterface::RTCConfiguration rtc_config;
3539 rtc_config.use_media_transport_for_data_channels = true;
3540 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3541 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3542 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3543 loopback_media_transports()->second_factory()));
3544 ConnectFakeSignaling();
3545
3546 // Create a data channel with a non-default configuration and signal it to the
3547 // callee.
3548 webrtc::DataChannelInit init;
3549 init.id = 53;
3550 init.maxRetransmits = 52;
3551 caller()->CreateDataChannel("data-channel", &init);
3552 caller()->CreateAndSetAndSignalOffer();
3553 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3554
3555 // Ensure that the media transport is ready.
3556 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3557 loopback_media_transports()->FlushAsyncInvokes();
3558
3559 // Ensure that the data channel exists on the callee with the correct
3560 // configuration.
3561 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3562 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3563 EXPECT_EQ(init.id, callee()->data_channel()->id());
3564 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3565 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3566 EXPECT_FALSE(callee()->data_channel()->negotiated());
3567}
3568
Niels Möllerc68d2822018-11-20 14:52:05 +01003569TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3570 PeerConnectionInterface::RTCConfiguration rtc_config;
3571 rtc_config.use_media_transport = true;
3572 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3573 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3574 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3575 loopback_media_transports()->second_factory()));
3576 ConnectFakeSignaling();
3577
3578 caller()->AddAudioTrack();
3579 callee()->AddAudioTrack();
3580 // Start offer/answer exchange and wait for it to complete.
3581 caller()->CreateAndSetAndSignalOffer();
3582 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3583
3584 // Ensure that the media transport is ready.
3585 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3586 loopback_media_transports()->FlushAsyncInvokes();
3587
3588 MediaExpectations media_expectations;
3589 media_expectations.ExpectBidirectionalAudio();
3590 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3591
3592 webrtc::MediaTransportPair::Stats first_stats =
3593 loopback_media_transports()->FirstStats();
3594 webrtc::MediaTransportPair::Stats second_stats =
3595 loopback_media_transports()->SecondStats();
3596
3597 EXPECT_GT(first_stats.received_audio_frames, 0);
3598 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3599
3600 EXPECT_GT(second_stats.received_audio_frames, 0);
3601 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3602}
3603
Niels Möller46879152019-01-07 15:54:47 +01003604TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3605 PeerConnectionInterface::RTCConfiguration rtc_config;
3606 rtc_config.use_media_transport = true;
3607 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3608 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3609 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3610 loopback_media_transports()->second_factory()));
3611 ConnectFakeSignaling();
3612
3613 caller()->AddVideoTrack();
3614 callee()->AddVideoTrack();
3615 // Start offer/answer exchange and wait for it to complete.
3616 caller()->CreateAndSetAndSignalOffer();
3617 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3618
3619 // Ensure that the media transport is ready.
3620 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3621 loopback_media_transports()->FlushAsyncInvokes();
3622
3623 MediaExpectations media_expectations;
3624 media_expectations.ExpectBidirectionalVideo();
3625 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3626
3627 webrtc::MediaTransportPair::Stats first_stats =
3628 loopback_media_transports()->FirstStats();
3629 webrtc::MediaTransportPair::Stats second_stats =
3630 loopback_media_transports()->SecondStats();
3631
3632 EXPECT_GT(first_stats.received_video_frames, 0);
3633 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3634
3635 EXPECT_GT(second_stats.received_video_frames, 0);
3636 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3637}
3638
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003639TEST_P(PeerConnectionIntegrationTest,
3640 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3641 PeerConnectionInterface::RTCConfiguration rtc_config;
3642 rtc_config.use_media_transport = false;
3643 rtc_config.use_media_transport_for_data_channels = true;
3644 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3645 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3646 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3647 loopback_media_transports()->second_factory()));
3648 ConnectFakeSignaling();
3649
3650 caller()->AddVideoTrack();
3651 callee()->AddVideoTrack();
3652 // Start offer/answer exchange and wait for it to complete.
3653 caller()->CreateAndSetAndSignalOffer();
3654 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3655
3656 MediaExpectations media_expectations;
3657 media_expectations.ExpectBidirectionalVideo();
3658 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3659}
3660
deadbeef1dcb1642017-03-29 21:08:16 -07003661// Test that the ICE connection and gathering states eventually reach
3662// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003663TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003664 ASSERT_TRUE(CreatePeerConnectionWrappers());
3665 ConnectFakeSignaling();
3666 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003667 caller()->AddAudioVideoTracks();
3668 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003669 caller()->CreateAndSetAndSignalOffer();
3670 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3671 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3672 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3673 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3674 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3675 // After the best candidate pair is selected and all candidates are signaled,
3676 // the ICE connection state should reach "complete".
3677 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3678 // answerer/"callee" by default) only reaches "connected". When this is
3679 // fixed, this test should be updated.
3680 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3681 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003682 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3683 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003684}
3685
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003686constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3687 cricket::PORTALLOCATOR_DISABLE_RELAY |
3688 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003689
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003690// Use a mock resolver to resolve the hostname back to the original IP on both
3691// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003692TEST_P(PeerConnectionIntegrationTest,
3693 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003694 auto caller_resolver_factory =
3695 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3696 auto callee_resolver_factory =
3697 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3698 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3699 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003700
3701 // This also verifies that the injected AsyncResolverFactory is used by
3702 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003703 EXPECT_CALL(*caller_resolver_factory, Create())
3704 .WillOnce(Return(&caller_async_resolver));
3705 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3706 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3707
3708 EXPECT_CALL(*callee_resolver_factory, Create())
3709 .WillOnce(Return(&callee_async_resolver));
3710 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3711 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3712
3713 PeerConnectionInterface::RTCConfiguration config;
3714 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3715 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3716
3717 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3718 config, std::move(caller_deps), config, std::move(callee_deps)));
3719
3720 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3721 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3722
3723 // Enable hostname candidates with mDNS names.
3724 caller()->network()->CreateMdnsResponder(network_thread());
3725 callee()->network()->CreateMdnsResponder(network_thread());
3726
3727 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003728
3729 ConnectFakeSignaling();
3730 caller()->AddAudioVideoTracks();
3731 callee()->AddAudioVideoTracks();
3732 caller()->CreateAndSetAndSignalOffer();
3733 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3734 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3735 caller()->ice_connection_state(), kDefaultTimeout);
3736 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3737 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003738
3739 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3740 "WebRTC.PeerConnection.CandidatePairType_UDP",
3741 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003742}
3743
Steve Antonede9ca52017-10-16 13:04:27 -07003744// Test that firewalling the ICE connection causes the clients to identify the
3745// disconnected state and then removing the firewall causes them to reconnect.
3746class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003747 : public PeerConnectionIntegrationBaseTest,
3748 public ::testing::WithParamInterface<
3749 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003750 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003751 PeerConnectionIntegrationIceStatesTest()
3752 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3753 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003754 }
3755
3756 void StartStunServer(const SocketAddress& server_address) {
3757 stun_server_.reset(
3758 cricket::TestStunServer::Create(network_thread(), server_address));
3759 }
3760
3761 bool TestIPv6() {
3762 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3763 }
3764
3765 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003766 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3767 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003768 }
3769
3770 std::vector<SocketAddress> CallerAddresses() {
3771 std::vector<SocketAddress> addresses;
3772 addresses.push_back(SocketAddress("1.1.1.1", 0));
3773 if (TestIPv6()) {
3774 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3775 }
3776 return addresses;
3777 }
3778
3779 std::vector<SocketAddress> CalleeAddresses() {
3780 std::vector<SocketAddress> addresses;
3781 addresses.push_back(SocketAddress("2.2.2.2", 0));
3782 if (TestIPv6()) {
3783 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3784 }
3785 return addresses;
3786 }
3787
3788 void SetUpNetworkInterfaces() {
3789 // Remove the default interfaces added by the test infrastructure.
3790 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3791 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3792
3793 // Add network addresses for test.
3794 for (const auto& caller_address : CallerAddresses()) {
3795 caller()->network()->AddInterface(caller_address);
3796 }
3797 for (const auto& callee_address : CalleeAddresses()) {
3798 callee()->network()->AddInterface(callee_address);
3799 }
3800 }
3801
3802 private:
3803 uint32_t port_allocator_flags_;
3804 std::unique_ptr<cricket::TestStunServer> stun_server_;
3805};
3806
3807// Tests that the PeerConnection goes through all the ICE gathering/connection
3808// states over the duration of the call. This includes Disconnected and Failed
3809// states, induced by putting a firewall between the peers and waiting for them
3810// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003811TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003812 rtc::ScopedFakeClock fake_clock;
3813 // Some things use a time of "0" as a special value, so we need to start out
3814 // the fake clock at a nonzero time.
3815 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003816
3817 const SocketAddress kStunServerAddress =
3818 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3819 StartStunServer(kStunServerAddress);
3820
3821 PeerConnectionInterface::RTCConfiguration config;
3822 PeerConnectionInterface::IceServer ice_stun_server;
3823 ice_stun_server.urls.push_back(
3824 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3825 kStunServerAddress.PortAsString());
3826 config.servers.push_back(ice_stun_server);
3827
3828 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3829 ConnectFakeSignaling();
3830 SetPortAllocatorFlags();
3831 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003832 caller()->AddAudioVideoTracks();
3833 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003834
3835 // Initial state before anything happens.
3836 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3837 caller()->ice_gathering_state());
3838 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3839 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003840 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3841 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003842
3843 // Start the call by creating the offer, setting it as the local description,
3844 // then sending it to the peer who will respond with an answer. This happens
3845 // asynchronously so that we can watch the states as it runs in the
3846 // background.
3847 caller()->CreateAndSetAndSignalOffer();
3848
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003849 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3850 caller()->ice_connection_state());
Jonas Olssondf919fb2019-01-22 11:21:00 +01003851 ASSERT_EQ(PeerConnectionInterface::kIceConnectionConnected,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003852 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003853
3854 // Verify that the observer was notified of the intermediate transitions.
3855 EXPECT_THAT(caller()->ice_connection_state_history(),
3856 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3857 PeerConnectionInterface::kIceConnectionConnected,
3858 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003859 EXPECT_THAT(
3860 caller()->peer_connection_state_history(),
3861 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003862 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003863 EXPECT_THAT(caller()->ice_gathering_state_history(),
3864 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3865 PeerConnectionInterface::kIceGatheringComplete));
3866
3867 // Block connections to/from the caller and wait for ICE to become
3868 // disconnected.
3869 for (const auto& caller_address : CallerAddresses()) {
3870 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3871 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003872 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003873 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3874 caller()->ice_connection_state(), kDefaultTimeout,
3875 fake_clock);
3876 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3877 caller()->standardized_ice_connection_state(),
3878 kDefaultTimeout, fake_clock);
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";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003883 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3884 caller()->ice_connection_state(), kDefaultTimeout,
3885 fake_clock);
3886 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionConnected,
3887 caller()->standardized_ice_connection_state(),
3888 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07003889
3890 // According to RFC7675, if there is no response within 30 seconds then the
3891 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003892 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003893 constexpr int kConsentTimeout = 30000;
3894 for (const auto& caller_address : CallerAddresses()) {
3895 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3896 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003897 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003898 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3899 caller()->ice_connection_state(), kConsentTimeout,
3900 fake_clock);
3901 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3902 caller()->standardized_ice_connection_state(),
3903 kConsentTimeout, fake_clock);
3904
3905 // We need to manually close the peerconnections before the fake clock goes
3906 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
3907 // return to using non-faked time.
3908 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3909 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3910}
3911
3912// Tests that if the connection doesn't get set up properly we eventually reach
3913// the "failed" iceConnectionState.
3914TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
3915 rtc::ScopedFakeClock fake_clock;
3916 // Some things use a time of "0" as a special value, so we need to start out
3917 // the fake clock at a nonzero time.
3918 fake_clock.AdvanceTime(TimeDelta::seconds(1));
3919
3920 // Block connections to/from the caller and wait for ICE to become
3921 // disconnected.
3922 for (const auto& caller_address : CallerAddresses()) {
3923 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3924 }
3925
3926 ASSERT_TRUE(CreatePeerConnectionWrappers());
3927 ConnectFakeSignaling();
3928 SetPortAllocatorFlags();
3929 SetUpNetworkInterfaces();
3930 caller()->AddAudioVideoTracks();
3931 caller()->CreateAndSetAndSignalOffer();
3932
3933 // According to RFC7675, if there is no response within 30 seconds then the
3934 // peer should consider the other side to have rejected the connection. This
3935 // is signaled by the state transitioning to "failed".
3936 constexpr int kConsentTimeout = 30000;
3937 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3938 caller()->standardized_ice_connection_state(),
3939 kConsentTimeout, fake_clock);
3940
3941 // We need to manually close the peerconnections before the fake clock goes
3942 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
3943 // return to using non-faked time.
3944 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3945 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07003946}
3947
3948// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3949// and that the statistics in the metric observers are updated correctly.
3950TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3951 ASSERT_TRUE(CreatePeerConnectionWrappers());
3952 ConnectFakeSignaling();
3953 SetPortAllocatorFlags();
3954 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003955 caller()->AddAudioVideoTracks();
3956 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003957 caller()->CreateAndSetAndSignalOffer();
3958
3959 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3960
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003961 // TODO(bugs.webrtc.org/9456): Fix it.
3962 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3963 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3964 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3965 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003966 if (TestIPv6()) {
3967 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3968 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003969 EXPECT_EQ(0, num_best_ipv4);
3970 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003971 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003972 EXPECT_EQ(1, num_best_ipv4);
3973 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003974 }
3975
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003976 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3977 "WebRTC.PeerConnection.CandidatePairType_UDP",
3978 webrtc::kIceCandidatePairHostHost));
3979 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3980 "WebRTC.PeerConnection.CandidatePairType_UDP",
3981 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003982}
3983
3984constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3985 cricket::PORTALLOCATOR_DISABLE_STUN |
3986 cricket::PORTALLOCATOR_DISABLE_RELAY;
3987constexpr uint32_t kFlagsIPv6NoStun =
3988 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3989 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3990constexpr uint32_t kFlagsIPv4Stun =
3991 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3992
Mirko Bonadeic84f6612019-01-31 12:20:57 +01003993INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08003994 PeerConnectionIntegrationTest,
3995 PeerConnectionIntegrationIceStatesTest,
3996 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3997 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3998 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3999 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004000
deadbeef1dcb1642017-03-29 21:08:16 -07004001// This test sets up a call between two parties with audio and video.
4002// During the call, the caller restarts ICE and the test verifies that
4003// new ICE candidates are generated and audio and video still can flow, and the
4004// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004005TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004006 ASSERT_TRUE(CreatePeerConnectionWrappers());
4007 ConnectFakeSignaling();
4008 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004009 caller()->AddAudioVideoTracks();
4010 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004011 caller()->CreateAndSetAndSignalOffer();
4012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4013 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4014 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004015 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4016 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004017
4018 // To verify that the ICE restart actually occurs, get
4019 // ufrag/password/candidates before and after restart.
4020 // Create an SDP string of the first audio candidate for both clients.
4021 const webrtc::IceCandidateCollection* audio_candidates_caller =
4022 caller()->pc()->local_description()->candidates(0);
4023 const webrtc::IceCandidateCollection* audio_candidates_callee =
4024 callee()->pc()->local_description()->candidates(0);
4025 ASSERT_GT(audio_candidates_caller->count(), 0u);
4026 ASSERT_GT(audio_candidates_callee->count(), 0u);
4027 std::string caller_candidate_pre_restart;
4028 ASSERT_TRUE(
4029 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4030 std::string callee_candidate_pre_restart;
4031 ASSERT_TRUE(
4032 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4033 const cricket::SessionDescription* desc =
4034 caller()->pc()->local_description()->description();
4035 std::string caller_ufrag_pre_restart =
4036 desc->transport_infos()[0].description.ice_ufrag;
4037 desc = callee()->pc()->local_description()->description();
4038 std::string callee_ufrag_pre_restart =
4039 desc->transport_infos()[0].description.ice_ufrag;
4040
4041 // Have the caller initiate an ICE restart.
4042 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4043 caller()->CreateAndSetAndSignalOffer();
4044 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4045 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4046 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004047 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004048 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4049
4050 // Grab the ufrags/candidates again.
4051 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4052 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4053 ASSERT_GT(audio_candidates_caller->count(), 0u);
4054 ASSERT_GT(audio_candidates_callee->count(), 0u);
4055 std::string caller_candidate_post_restart;
4056 ASSERT_TRUE(
4057 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4058 std::string callee_candidate_post_restart;
4059 ASSERT_TRUE(
4060 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4061 desc = caller()->pc()->local_description()->description();
4062 std::string caller_ufrag_post_restart =
4063 desc->transport_infos()[0].description.ice_ufrag;
4064 desc = callee()->pc()->local_description()->description();
4065 std::string callee_ufrag_post_restart =
4066 desc->transport_infos()[0].description.ice_ufrag;
4067 // Sanity check that an ICE restart was actually negotiated in SDP.
4068 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4069 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4070 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4071 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4072
4073 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004074 MediaExpectations media_expectations;
4075 media_expectations.ExpectBidirectionalAudioAndVideo();
4076 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004077}
4078
4079// Verify that audio/video can be received end-to-end when ICE renomination is
4080// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004081TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004082 PeerConnectionInterface::RTCConfiguration config;
4083 config.enable_ice_renomination = true;
4084 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4085 ConnectFakeSignaling();
4086 // Do normal offer/answer and wait for some frames to be received in each
4087 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004088 caller()->AddAudioVideoTracks();
4089 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004090 caller()->CreateAndSetAndSignalOffer();
4091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4092 // Sanity check that ICE renomination was actually negotiated.
4093 const cricket::SessionDescription* desc =
4094 caller()->pc()->local_description()->description();
4095 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004096 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004097 }
4098 desc = callee()->pc()->local_description()->description();
4099 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004100 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004101 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004102 MediaExpectations media_expectations;
4103 media_expectations.ExpectBidirectionalAudioAndVideo();
4104 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004105}
4106
Steve Anton6f25b092017-10-23 09:39:20 -07004107// With a max bundle policy and RTCP muxing, adding a new media description to
4108// the connection should not affect ICE at all because the new media will use
4109// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004110TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004111 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004112 PeerConnectionInterface::RTCConfiguration config;
4113 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4114 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4115 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4116 config, PeerConnectionInterface::RTCConfiguration()));
4117 ConnectFakeSignaling();
4118
Steve Anton15324772018-01-16 10:26:49 -08004119 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004120 caller()->CreateAndSetAndSignalOffer();
4121 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004122 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4123 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004124
4125 caller()->clear_ice_connection_state_history();
4126
Steve Anton15324772018-01-16 10:26:49 -08004127 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004128 caller()->CreateAndSetAndSignalOffer();
4129 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4130
4131 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4132}
4133
deadbeef1dcb1642017-03-29 21:08:16 -07004134// This test sets up a call between two parties with audio and video. It then
4135// renegotiates setting the video m-line to "port 0", then later renegotiates
4136// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004137TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004138 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4139 ASSERT_TRUE(CreatePeerConnectionWrappers());
4140 ConnectFakeSignaling();
4141
4142 // Do initial negotiation, only sending media from the caller. Will result in
4143 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004144 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004145 caller()->CreateAndSetAndSignalOffer();
4146 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4147
4148 // Negotiate again, disabling the video "m=" section (the callee will set the
4149 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004150 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4151 PeerConnectionInterface::RTCOfferAnswerOptions options;
4152 options.offer_to_receive_video = 0;
4153 callee()->SetOfferAnswerOptions(options);
4154 } else {
4155 callee()->SetRemoteOfferHandler([this] {
4156 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4157 });
4158 }
deadbeef1dcb1642017-03-29 21:08:16 -07004159 caller()->CreateAndSetAndSignalOffer();
4160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4161 // Sanity check that video "m=" section was actually rejected.
4162 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4163 callee()->pc()->local_description()->description());
4164 ASSERT_NE(nullptr, answer_video_content);
4165 ASSERT_TRUE(answer_video_content->rejected);
4166
4167 // Enable video and do negotiation again, making sure video is received
4168 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004169 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4170 PeerConnectionInterface::RTCOfferAnswerOptions options;
4171 options.offer_to_receive_video = 1;
4172 callee()->SetOfferAnswerOptions(options);
4173 } else {
4174 // The caller's transceiver is stopped, so we need to add another track.
4175 auto caller_transceiver =
4176 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4177 EXPECT_TRUE(caller_transceiver->stopped());
4178 caller()->AddVideoTrack();
4179 }
4180 callee()->AddVideoTrack();
4181 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004182 caller()->CreateAndSetAndSignalOffer();
4183 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004184
deadbeef1dcb1642017-03-29 21:08:16 -07004185 // Verify the caller receives frames from the newly added stream, and the
4186 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004187 MediaExpectations media_expectations;
4188 media_expectations.CalleeExpectsSomeAudio();
4189 media_expectations.ExpectBidirectionalVideo();
4190 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004191}
4192
deadbeef1dcb1642017-03-29 21:08:16 -07004193// This tests that if we negotiate after calling CreateSender but before we
4194// have a track, then set a track later, frames from the newly-set track are
4195// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004196TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004197 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4198 ASSERT_TRUE(CreatePeerConnectionWrappers());
4199 ConnectFakeSignaling();
4200 auto caller_audio_sender =
4201 caller()->pc()->CreateSender("audio", "caller_stream");
4202 auto caller_video_sender =
4203 caller()->pc()->CreateSender("video", "caller_stream");
4204 auto callee_audio_sender =
4205 callee()->pc()->CreateSender("audio", "callee_stream");
4206 auto callee_video_sender =
4207 callee()->pc()->CreateSender("video", "callee_stream");
4208 caller()->CreateAndSetAndSignalOffer();
4209 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4210 // Wait for ICE to complete, without any tracks being set.
4211 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4212 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4213 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4214 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4215 // Now set the tracks, and expect frames to immediately start flowing.
4216 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4217 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4218 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4219 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004220 MediaExpectations media_expectations;
4221 media_expectations.ExpectBidirectionalAudioAndVideo();
4222 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4223}
4224
4225// This tests that if we negotiate after calling AddTransceiver but before we
4226// have a track, then set a track later, frames from the newly-set tracks are
4227// received end-to-end.
4228TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4229 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4230 ASSERT_TRUE(CreatePeerConnectionWrappers());
4231 ConnectFakeSignaling();
4232 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4233 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4234 auto caller_audio_sender = audio_result.MoveValue()->sender();
4235 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4236 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4237 auto caller_video_sender = video_result.MoveValue()->sender();
4238 callee()->SetRemoteOfferHandler([this] {
4239 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4240 callee()->pc()->GetTransceivers()[0]->SetDirection(
4241 RtpTransceiverDirection::kSendRecv);
4242 callee()->pc()->GetTransceivers()[1]->SetDirection(
4243 RtpTransceiverDirection::kSendRecv);
4244 });
4245 caller()->CreateAndSetAndSignalOffer();
4246 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4247 // Wait for ICE to complete, without any tracks being set.
4248 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4249 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4250 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4251 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4252 // Now set the tracks, and expect frames to immediately start flowing.
4253 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4254 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4255 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4256 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4257 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4258 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4259 MediaExpectations media_expectations;
4260 media_expectations.ExpectBidirectionalAudioAndVideo();
4261 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004262}
4263
4264// This test verifies that a remote video track can be added via AddStream,
4265// and sent end-to-end. For this particular test, it's simply echoed back
4266// from the caller to the callee, rather than being forwarded to a third
4267// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004268TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004269 ASSERT_TRUE(CreatePeerConnectionWrappers());
4270 ConnectFakeSignaling();
4271 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004272 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004273 caller()->CreateAndSetAndSignalOffer();
4274 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004275 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004276
4277 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4278 // time).
4279 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4280 callee()->CreateAndSetAndSignalOffer();
4281 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4282
Seth Hampson2f0d7022018-02-20 11:54:42 -08004283 MediaExpectations media_expectations;
4284 media_expectations.ExpectBidirectionalVideo();
4285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004286}
4287
4288// Test that we achieve the expected end-to-end connection time, using a
4289// fake clock and simulated latency on the media and signaling paths.
4290// We use a TURN<->TURN connection because this is usually the quickest to
4291// set up initially, especially when we're confident the connection will work
4292// and can start sending media before we get a STUN response.
4293//
4294// With various optimizations enabled, here are the network delays we expect to
4295// be on the critical path:
4296// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4297// signaling answer (with DTLS fingerprint).
4298// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4299// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4300// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004301TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004302 rtc::ScopedFakeClock fake_clock;
4303 // Some things use a time of "0" as a special value, so we need to start out
4304 // the fake clock at a nonzero time.
4305 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004306 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004307
4308 static constexpr int media_hop_delay_ms = 50;
4309 static constexpr int signaling_trip_delay_ms = 500;
4310 // For explanation of these values, see comment above.
4311 static constexpr int required_media_hops = 9;
4312 static constexpr int required_signaling_trips = 2;
4313 // For internal delays (such as posting an event asychronously).
4314 static constexpr int allowed_internal_delay_ms = 20;
4315 static constexpr int total_connection_time_ms =
4316 media_hop_delay_ms * required_media_hops +
4317 signaling_trip_delay_ms * required_signaling_trips +
4318 allowed_internal_delay_ms;
4319
4320 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4321 3478};
4322 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4323 0};
4324 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4325 3478};
4326 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4327 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004328 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4329 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004330
Seth Hampsonaed71642018-06-11 07:41:32 -07004331 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4332 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004333 // Bypass permission check on received packets so media can be sent before
4334 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004335 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4336 turn_server_1->set_enable_permission_checks(false);
4337 });
4338 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4339 turn_server_2->set_enable_permission_checks(false);
4340 });
deadbeef1dcb1642017-03-29 21:08:16 -07004341
4342 PeerConnectionInterface::RTCConfiguration client_1_config;
4343 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4344 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4345 ice_server_1.username = "test";
4346 ice_server_1.password = "test";
4347 client_1_config.servers.push_back(ice_server_1);
4348 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4349 client_1_config.presume_writable_when_fully_relayed = true;
4350
4351 PeerConnectionInterface::RTCConfiguration client_2_config;
4352 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4353 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4354 ice_server_2.username = "test";
4355 ice_server_2.password = "test";
4356 client_2_config.servers.push_back(ice_server_2);
4357 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4358 client_2_config.presume_writable_when_fully_relayed = true;
4359
4360 ASSERT_TRUE(
4361 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4362 // Set up the simulated delays.
4363 SetSignalingDelayMs(signaling_trip_delay_ms);
4364 ConnectFakeSignaling();
4365 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4366 virtual_socket_server()->UpdateDelayDistribution();
4367
4368 // Set "offer to receive audio/video" without adding any tracks, so we just
4369 // set up ICE/DTLS with no media.
4370 PeerConnectionInterface::RTCOfferAnswerOptions options;
4371 options.offer_to_receive_audio = 1;
4372 options.offer_to_receive_video = 1;
4373 caller()->SetOfferAnswerOptions(options);
4374 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004375 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4376 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004377 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4378 // If this is not done a DCHECK can be hit in ports.cc, because a large
4379 // negative number is calculated for the rtt due to the global clock changing.
4380 caller()->pc()->Close();
4381 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004382}
4383
Jonas Orelandbdcee282017-10-10 14:01:40 +02004384// Verify that a TurnCustomizer passed in through RTCConfiguration
4385// is actually used by the underlying TURN candidate pair.
4386// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004387TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004388 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4389 3478};
4390 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4391 0};
4392 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4393 3478};
4394 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4395 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004396 CreateTurnServer(turn_server_1_internal_address,
4397 turn_server_1_external_address);
4398 CreateTurnServer(turn_server_2_internal_address,
4399 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004400
4401 PeerConnectionInterface::RTCConfiguration client_1_config;
4402 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4403 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4404 ice_server_1.username = "test";
4405 ice_server_1.password = "test";
4406 client_1_config.servers.push_back(ice_server_1);
4407 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004408 auto* customizer1 = CreateTurnCustomizer();
4409 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004410
4411 PeerConnectionInterface::RTCConfiguration client_2_config;
4412 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4413 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4414 ice_server_2.username = "test";
4415 ice_server_2.password = "test";
4416 client_2_config.servers.push_back(ice_server_2);
4417 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004418 auto* customizer2 = CreateTurnCustomizer();
4419 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004420
4421 ASSERT_TRUE(
4422 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4423 ConnectFakeSignaling();
4424
4425 // Set "offer to receive audio/video" without adding any tracks, so we just
4426 // set up ICE/DTLS with no media.
4427 PeerConnectionInterface::RTCOfferAnswerOptions options;
4428 options.offer_to_receive_audio = 1;
4429 options.offer_to_receive_video = 1;
4430 caller()->SetOfferAnswerOptions(options);
4431 caller()->CreateAndSetAndSignalOffer();
4432 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4433
Seth Hampsonaed71642018-06-11 07:41:32 -07004434 ExpectTurnCustomizerCountersIncremented(customizer1);
4435 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004436}
4437
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004438// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4439// send media between the caller and the callee.
4440TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4441 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4442 3478};
4443 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4444
4445 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004446 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4447 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004448
4449 webrtc::PeerConnectionInterface::IceServer ice_server;
4450 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4451 ice_server.username = "test";
4452 ice_server.password = "test";
4453
4454 PeerConnectionInterface::RTCConfiguration client_1_config;
4455 client_1_config.servers.push_back(ice_server);
4456 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4457
4458 PeerConnectionInterface::RTCConfiguration client_2_config;
4459 client_2_config.servers.push_back(ice_server);
4460 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4461
4462 ASSERT_TRUE(
4463 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4464
4465 // Do normal offer/answer and wait for ICE to complete.
4466 ConnectFakeSignaling();
4467 caller()->AddAudioVideoTracks();
4468 callee()->AddAudioVideoTracks();
4469 caller()->CreateAndSetAndSignalOffer();
4470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4471 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4472 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4473
4474 MediaExpectations media_expectations;
4475 media_expectations.ExpectBidirectionalAudioAndVideo();
4476 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4477}
4478
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004479// Verify that a SSLCertificateVerifier passed in through
4480// PeerConnectionDependencies is actually used by the underlying SSL
4481// implementation to determine whether a certificate presented by the TURN
4482// server is accepted by the client. Note that openssladapter_unittest.cc
4483// contains more detailed, lower-level tests.
4484TEST_P(PeerConnectionIntegrationTest,
4485 SSLCertificateVerifierUsedForTurnConnections) {
4486 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4487 3478};
4488 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4489
4490 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4491 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004492 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4493 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004494
4495 webrtc::PeerConnectionInterface::IceServer ice_server;
4496 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4497 ice_server.username = "test";
4498 ice_server.password = "test";
4499
4500 PeerConnectionInterface::RTCConfiguration client_1_config;
4501 client_1_config.servers.push_back(ice_server);
4502 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4503
4504 PeerConnectionInterface::RTCConfiguration client_2_config;
4505 client_2_config.servers.push_back(ice_server);
4506 // Setting the type to kRelay forces the connection to go through a TURN
4507 // server.
4508 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4509
4510 // Get a copy to the pointer so we can verify calls later.
4511 rtc::TestCertificateVerifier* client_1_cert_verifier =
4512 new rtc::TestCertificateVerifier();
4513 client_1_cert_verifier->verify_certificate_ = true;
4514 rtc::TestCertificateVerifier* client_2_cert_verifier =
4515 new rtc::TestCertificateVerifier();
4516 client_2_cert_verifier->verify_certificate_ = true;
4517
4518 // Create the dependencies with the test certificate verifier.
4519 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4520 client_1_deps.tls_cert_verifier =
4521 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4522 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4523 client_2_deps.tls_cert_verifier =
4524 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4525
4526 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4527 client_1_config, std::move(client_1_deps), client_2_config,
4528 std::move(client_2_deps)));
4529 ConnectFakeSignaling();
4530
4531 // Set "offer to receive audio/video" without adding any tracks, so we just
4532 // set up ICE/DTLS with no media.
4533 PeerConnectionInterface::RTCOfferAnswerOptions options;
4534 options.offer_to_receive_audio = 1;
4535 options.offer_to_receive_video = 1;
4536 caller()->SetOfferAnswerOptions(options);
4537 caller()->CreateAndSetAndSignalOffer();
4538 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4539
4540 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4541 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004542}
4543
4544TEST_P(PeerConnectionIntegrationTest,
4545 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4546 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4547 3478};
4548 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4549
4550 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4551 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004552 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4553 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004554
4555 webrtc::PeerConnectionInterface::IceServer ice_server;
4556 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4557 ice_server.username = "test";
4558 ice_server.password = "test";
4559
4560 PeerConnectionInterface::RTCConfiguration client_1_config;
4561 client_1_config.servers.push_back(ice_server);
4562 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4563
4564 PeerConnectionInterface::RTCConfiguration client_2_config;
4565 client_2_config.servers.push_back(ice_server);
4566 // Setting the type to kRelay forces the connection to go through a TURN
4567 // server.
4568 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4569
4570 // Get a copy to the pointer so we can verify calls later.
4571 rtc::TestCertificateVerifier* client_1_cert_verifier =
4572 new rtc::TestCertificateVerifier();
4573 client_1_cert_verifier->verify_certificate_ = false;
4574 rtc::TestCertificateVerifier* client_2_cert_verifier =
4575 new rtc::TestCertificateVerifier();
4576 client_2_cert_verifier->verify_certificate_ = false;
4577
4578 // Create the dependencies with the test certificate verifier.
4579 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4580 client_1_deps.tls_cert_verifier =
4581 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4582 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4583 client_2_deps.tls_cert_verifier =
4584 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4585
4586 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4587 client_1_config, std::move(client_1_deps), client_2_config,
4588 std::move(client_2_deps)));
4589 ConnectFakeSignaling();
4590
4591 // Set "offer to receive audio/video" without adding any tracks, so we just
4592 // set up ICE/DTLS with no media.
4593 PeerConnectionInterface::RTCOfferAnswerOptions options;
4594 options.offer_to_receive_audio = 1;
4595 options.offer_to_receive_video = 1;
4596 caller()->SetOfferAnswerOptions(options);
4597 caller()->CreateAndSetAndSignalOffer();
4598 bool wait_res = true;
4599 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4600 // properly, should be able to just wait for a state of "failed" instead of
4601 // waiting a fixed 10 seconds.
4602 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4603 ASSERT_FALSE(wait_res);
4604
4605 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4606 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004607}
4608
deadbeefc964d0b2017-04-03 10:03:35 -07004609// Test that audio and video flow end-to-end when codec names don't use the
4610// expected casing, given that they're supposed to be case insensitive. To test
4611// this, all but one codec is removed from each media description, and its
4612// casing is changed.
4613//
4614// In the past, this has regressed and caused crashes/black video, due to the
4615// fact that code at some layers was doing case-insensitive comparisons and
4616// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004617TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004618 ASSERT_TRUE(CreatePeerConnectionWrappers());
4619 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004620 caller()->AddAudioVideoTracks();
4621 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004622
4623 // Remove all but one audio/video codec (opus and VP8), and change the
4624 // casing of the caller's generated offer.
4625 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4626 cricket::AudioContentDescription* audio =
4627 GetFirstAudioContentDescription(description);
4628 ASSERT_NE(nullptr, audio);
4629 auto audio_codecs = audio->codecs();
4630 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4631 [](const cricket::AudioCodec& codec) {
4632 return codec.name != "opus";
4633 }),
4634 audio_codecs.end());
4635 ASSERT_EQ(1u, audio_codecs.size());
4636 audio_codecs[0].name = "OpUs";
4637 audio->set_codecs(audio_codecs);
4638
4639 cricket::VideoContentDescription* video =
4640 GetFirstVideoContentDescription(description);
4641 ASSERT_NE(nullptr, video);
4642 auto video_codecs = video->codecs();
4643 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4644 [](const cricket::VideoCodec& codec) {
4645 return codec.name != "VP8";
4646 }),
4647 video_codecs.end());
4648 ASSERT_EQ(1u, video_codecs.size());
4649 video_codecs[0].name = "vP8";
4650 video->set_codecs(video_codecs);
4651 });
4652
4653 caller()->CreateAndSetAndSignalOffer();
4654 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4655
4656 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004657 MediaExpectations media_expectations;
4658 media_expectations.ExpectBidirectionalAudioAndVideo();
4659 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004660}
4661
Jonas Oreland49ac5952018-09-26 16:04:32 +02004662TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004663 ASSERT_TRUE(CreatePeerConnectionWrappers());
4664 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004665 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004666 caller()->CreateAndSetAndSignalOffer();
4667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004668 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004669 MediaExpectations media_expectations;
4670 media_expectations.CalleeExpectsSomeAudio(1);
4671 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004672 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004673 auto receiver = callee()->pc()->GetReceivers()[0];
4674 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004675 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004676 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4677 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004678 sources[0].source_id());
4679 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4680}
4681
4682TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4683 ASSERT_TRUE(CreatePeerConnectionWrappers());
4684 ConnectFakeSignaling();
4685 caller()->AddVideoTrack();
4686 caller()->CreateAndSetAndSignalOffer();
4687 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4688 // Wait for one video frame to be received by the callee.
4689 MediaExpectations media_expectations;
4690 media_expectations.CalleeExpectsSomeVideo(1);
4691 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4692 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4693 auto receiver = callee()->pc()->GetReceivers()[0];
4694 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4695 auto sources = receiver->GetSources();
4696 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4697 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4698 sources[0].source_id());
4699 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004700}
4701
deadbeef2f425aa2017-04-14 10:41:32 -07004702// Test that if a track is removed and added again with a different stream ID,
4703// the new stream ID is successfully communicated in SDP and media continues to
4704// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004705// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4706// it will not reuse a transceiver that has already been sending. After creating
4707// a new transceiver it tries to create an offer with two senders of the same
4708// track ids and it fails.
4709TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004710 ASSERT_TRUE(CreatePeerConnectionWrappers());
4711 ConnectFakeSignaling();
4712
deadbeef2f425aa2017-04-14 10:41:32 -07004713 // Add track using stream 1, do offer/answer.
4714 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4715 caller()->CreateLocalAudioTrack();
4716 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004717 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004718 caller()->CreateAndSetAndSignalOffer();
4719 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004720 {
4721 MediaExpectations media_expectations;
4722 media_expectations.CalleeExpectsSomeAudio(1);
4723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4724 }
deadbeef2f425aa2017-04-14 10:41:32 -07004725 // Remove the sender, and create a new one with the new stream.
4726 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004727 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004728 caller()->CreateAndSetAndSignalOffer();
4729 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4730 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004731 {
4732 MediaExpectations media_expectations;
4733 media_expectations.CalleeExpectsSomeAudio();
4734 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4735 }
deadbeef2f425aa2017-04-14 10:41:32 -07004736}
4737
Seth Hampson2f0d7022018-02-20 11:54:42 -08004738TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004739 ASSERT_TRUE(CreatePeerConnectionWrappers());
4740 ConnectFakeSignaling();
4741
Karl Wiberg918f50c2018-07-05 11:40:33 +02004742 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004743 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4744 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4745 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004746 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4747 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004748
Steve Anton15324772018-01-16 10:26:49 -08004749 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004750 caller()->CreateAndSetAndSignalOffer();
4751 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4752}
4753
Steve Antonede9ca52017-10-16 13:04:27 -07004754// Test that if candidates are only signaled by applying full session
4755// descriptions (instead of using AddIceCandidate), the peers can connect to
4756// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004757TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004758 ASSERT_TRUE(CreatePeerConnectionWrappers());
4759 // Each side will signal the session descriptions but not candidates.
4760 ConnectFakeSignalingForSdpOnly();
4761
4762 // Add audio video track and exchange the initial offer/answer with media
4763 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004764 caller()->AddAudioVideoTracks();
4765 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004766 caller()->CreateAndSetAndSignalOffer();
4767
4768 // Wait for all candidates to be gathered on both the caller and callee.
4769 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4770 caller()->ice_gathering_state(), kDefaultTimeout);
4771 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4772 callee()->ice_gathering_state(), kDefaultTimeout);
4773
4774 // The candidates will now be included in the session description, so
4775 // signaling them will start the ICE connection.
4776 caller()->CreateAndSetAndSignalOffer();
4777 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4778
4779 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004780 MediaExpectations media_expectations;
4781 media_expectations.ExpectBidirectionalAudioAndVideo();
4782 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004783}
4784
henrika5f6bf242017-11-01 11:06:56 +01004785// Test that SetAudioPlayout can be used to disable audio playout from the
4786// start, then later enable it. This may be useful, for example, if the caller
4787// needs to play a local ringtone until some event occurs, after which it
4788// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004789TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004790 ASSERT_TRUE(CreatePeerConnectionWrappers());
4791 ConnectFakeSignaling();
4792
4793 // Set up audio-only call where audio playout is disabled on caller's side.
4794 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004795 caller()->AddAudioTrack();
4796 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004797 caller()->CreateAndSetAndSignalOffer();
4798 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4799
4800 // Pump messages for a second.
4801 WAIT(false, 1000);
4802 // Since audio playout is disabled, the caller shouldn't have received
4803 // anything (at the playout level, at least).
4804 EXPECT_EQ(0, caller()->audio_frames_received());
4805 // As a sanity check, make sure the callee (for which playout isn't disabled)
4806 // did still see frames on its audio level.
4807 ASSERT_GT(callee()->audio_frames_received(), 0);
4808
4809 // Enable playout again, and ensure audio starts flowing.
4810 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004811 MediaExpectations media_expectations;
4812 media_expectations.ExpectBidirectionalAudio();
4813 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004814}
4815
4816double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4817 auto report = pc->NewGetStats();
4818 auto track_stats_list =
4819 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4820 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4821 for (const auto* track_stats : track_stats_list) {
4822 if (track_stats->remote_source.is_defined() &&
4823 *track_stats->remote_source) {
4824 remote_track_stats = track_stats;
4825 break;
4826 }
4827 }
4828
4829 if (!remote_track_stats->total_audio_energy.is_defined()) {
4830 return 0.0;
4831 }
4832 return *remote_track_stats->total_audio_energy;
4833}
4834
4835// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4836// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004837TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004838 DisableAudioPlayoutStillGeneratesAudioStats) {
4839 ASSERT_TRUE(CreatePeerConnectionWrappers());
4840 ConnectFakeSignaling();
4841
4842 // Set up audio-only call where playout is disabled but audio-processing is
4843 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004844 caller()->AddAudioTrack();
4845 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004846 caller()->pc()->SetAudioPlayout(false);
4847
4848 caller()->CreateAndSetAndSignalOffer();
4849 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4850
4851 // Wait for the callee to receive audio stats.
4852 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4853}
4854
henrika4f167df2017-11-01 14:45:55 +01004855// Test that SetAudioRecording can be used to disable audio recording from the
4856// start, then later enable it. This may be useful, for example, if the caller
4857// wants to ensure that no audio resources are active before a certain state
4858// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004859TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004860 ASSERT_TRUE(CreatePeerConnectionWrappers());
4861 ConnectFakeSignaling();
4862
4863 // Set up audio-only call where audio recording is disabled on caller's side.
4864 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004865 caller()->AddAudioTrack();
4866 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004867 caller()->CreateAndSetAndSignalOffer();
4868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4869
4870 // Pump messages for a second.
4871 WAIT(false, 1000);
4872 // Since caller has disabled audio recording, the callee shouldn't have
4873 // received anything.
4874 EXPECT_EQ(0, callee()->audio_frames_received());
4875 // As a sanity check, make sure the caller did still see frames on its
4876 // audio level since audio recording is enabled on the calle side.
4877 ASSERT_GT(caller()->audio_frames_received(), 0);
4878
4879 // Enable audio recording again, and ensure audio starts flowing.
4880 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004881 MediaExpectations media_expectations;
4882 media_expectations.ExpectBidirectionalAudio();
4883 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004884}
4885
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004886// Test that after closing PeerConnections, they stop sending any packets (ICE,
4887// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004888TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004889 // Set up audio/video/data, wait for some frames to be received.
4890 ASSERT_TRUE(CreatePeerConnectionWrappers());
4891 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004892 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004893#ifdef HAVE_SCTP
4894 caller()->CreateDataChannel();
4895#endif
4896 caller()->CreateAndSetAndSignalOffer();
4897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004898 MediaExpectations media_expectations;
4899 media_expectations.CalleeExpectsSomeAudioAndVideo();
4900 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004901 // Close PeerConnections.
4902 caller()->pc()->Close();
4903 callee()->pc()->Close();
4904 // Pump messages for a second, and ensure no new packets end up sent.
4905 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4906 WAIT(false, 1000);
4907 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4908 EXPECT_EQ(sent_packets_a, sent_packets_b);
4909}
4910
Steve Anton7eca0932018-03-30 15:18:41 -07004911// Test that transport stats are generated by the RTCStatsCollector for a
4912// connection that only involves data channels. This is a regression test for
4913// crbug.com/826972.
4914#ifdef HAVE_SCTP
4915TEST_P(PeerConnectionIntegrationTest,
4916 TransportStatsReportedForDataChannelOnlyConnection) {
4917 ASSERT_TRUE(CreatePeerConnectionWrappers());
4918 ConnectFakeSignaling();
4919 caller()->CreateDataChannel();
4920
4921 caller()->CreateAndSetAndSignalOffer();
4922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4923 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4924
4925 auto caller_report = caller()->NewGetStats();
4926 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4927 auto callee_report = callee()->NewGetStats();
4928 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4929}
4930#endif // HAVE_SCTP
4931
Qingsi Wang7685e862018-06-11 20:15:46 -07004932TEST_P(PeerConnectionIntegrationTest,
4933 IceEventsGeneratedAndLoggedInRtcEventLog) {
4934 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4935 ConnectFakeSignaling();
4936 PeerConnectionInterface::RTCOfferAnswerOptions options;
4937 options.offer_to_receive_audio = 1;
4938 caller()->SetOfferAnswerOptions(options);
4939 caller()->CreateAndSetAndSignalOffer();
4940 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4941 ASSERT_NE(nullptr, caller()->event_log_factory());
4942 ASSERT_NE(nullptr, callee()->event_log_factory());
4943 webrtc::FakeRtcEventLog* caller_event_log =
4944 static_cast<webrtc::FakeRtcEventLog*>(
4945 caller()->event_log_factory()->last_log_created());
4946 webrtc::FakeRtcEventLog* callee_event_log =
4947 static_cast<webrtc::FakeRtcEventLog*>(
4948 callee()->event_log_factory()->last_log_created());
4949 ASSERT_NE(nullptr, caller_event_log);
4950 ASSERT_NE(nullptr, callee_event_log);
4951 int caller_ice_config_count = caller_event_log->GetEventCount(
4952 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4953 int caller_ice_event_count = caller_event_log->GetEventCount(
4954 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4955 int callee_ice_config_count = callee_event_log->GetEventCount(
4956 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4957 int callee_ice_event_count = callee_event_log->GetEventCount(
4958 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4959 EXPECT_LT(0, caller_ice_config_count);
4960 EXPECT_LT(0, caller_ice_event_count);
4961 EXPECT_LT(0, callee_ice_config_count);
4962 EXPECT_LT(0, callee_ice_event_count);
4963}
4964
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004965INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
4966 PeerConnectionIntegrationTest,
4967 Values(SdpSemantics::kPlanB,
4968 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004969
Steve Anton74255ff2018-01-24 18:32:57 -08004970// Tests that verify interoperability between Plan B and Unified Plan
4971// PeerConnections.
4972class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004973 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004974 public ::testing::WithParamInterface<
4975 std::tuple<SdpSemantics, SdpSemantics>> {
4976 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004977 // Setting the SdpSemantics for the base test to kDefault does not matter
4978 // because we specify not to use the test semantics when creating
4979 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004980 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004981 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004982 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004983 callee_semantics_(std::get<1>(GetParam())) {}
4984
4985 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004986 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4987 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004988 }
4989
4990 const SdpSemantics caller_semantics_;
4991 const SdpSemantics callee_semantics_;
4992};
4993
4994TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4995 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4996 ConnectFakeSignaling();
4997
4998 caller()->CreateAndSetAndSignalOffer();
4999 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5000}
5001
5002TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5003 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5004 ConnectFakeSignaling();
5005 auto audio_sender = caller()->AddAudioTrack();
5006
5007 caller()->CreateAndSetAndSignalOffer();
5008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5009
5010 // Verify that one audio receiver has been created on the remote and that it
5011 // has the same track ID as the sending track.
5012 auto receivers = callee()->pc()->GetReceivers();
5013 ASSERT_EQ(1u, receivers.size());
5014 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5015 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5016
Seth Hampson2f0d7022018-02-20 11:54:42 -08005017 MediaExpectations media_expectations;
5018 media_expectations.CalleeExpectsSomeAudio();
5019 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005020}
5021
5022TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5023 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5024 ConnectFakeSignaling();
5025 auto video_sender = caller()->AddVideoTrack();
5026 auto audio_sender = caller()->AddAudioTrack();
5027
5028 caller()->CreateAndSetAndSignalOffer();
5029 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5030
5031 // Verify that one audio and one video receiver have been created on the
5032 // remote and that they have the same track IDs as the sending tracks.
5033 auto audio_receivers =
5034 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5035 ASSERT_EQ(1u, audio_receivers.size());
5036 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5037 auto video_receivers =
5038 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5039 ASSERT_EQ(1u, video_receivers.size());
5040 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5041
Seth Hampson2f0d7022018-02-20 11:54:42 -08005042 MediaExpectations media_expectations;
5043 media_expectations.CalleeExpectsSomeAudioAndVideo();
5044 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005045}
5046
5047TEST_P(PeerConnectionIntegrationInteropTest,
5048 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5049 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5050 ConnectFakeSignaling();
5051 caller()->AddAudioVideoTracks();
5052 callee()->AddAudioVideoTracks();
5053
5054 caller()->CreateAndSetAndSignalOffer();
5055 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5056
Seth Hampson2f0d7022018-02-20 11:54:42 -08005057 MediaExpectations media_expectations;
5058 media_expectations.ExpectBidirectionalAudioAndVideo();
5059 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005060}
5061
5062TEST_P(PeerConnectionIntegrationInteropTest,
5063 ReverseRolesOneAudioLocalToOneVideoRemote) {
5064 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5065 ConnectFakeSignaling();
5066 caller()->AddAudioTrack();
5067 callee()->AddVideoTrack();
5068
5069 caller()->CreateAndSetAndSignalOffer();
5070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5071
5072 // Verify that only the audio track has been negotiated.
5073 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5074 // Might also check that the callee's NegotiationNeeded flag is set.
5075
5076 // Reverse roles.
5077 callee()->CreateAndSetAndSignalOffer();
5078 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5079
Seth Hampson2f0d7022018-02-20 11:54:42 -08005080 MediaExpectations media_expectations;
5081 media_expectations.CallerExpectsSomeVideo();
5082 media_expectations.CalleeExpectsSomeAudio();
5083 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005084}
5085
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005086INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005087 PeerConnectionIntegrationTest,
5088 PeerConnectionIntegrationInteropTest,
5089 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5090 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5091
5092// Test that if the Unified Plan side offers two video tracks then the Plan B
5093// side will only see the first one and ignore the second.
5094TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005095 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5096 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005097 ConnectFakeSignaling();
5098 auto first_sender = caller()->AddVideoTrack();
5099 caller()->AddVideoTrack();
5100
5101 caller()->CreateAndSetAndSignalOffer();
5102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5103
5104 // Verify that there is only one receiver and it corresponds to the first
5105 // added track.
5106 auto receivers = callee()->pc()->GetReceivers();
5107 ASSERT_EQ(1u, receivers.size());
5108 EXPECT_TRUE(receivers[0]->track()->enabled());
5109 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5110
Seth Hampson2f0d7022018-02-20 11:54:42 -08005111 MediaExpectations media_expectations;
5112 media_expectations.CalleeExpectsSomeVideo();
5113 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005114}
5115
Steve Anton2bed3972019-01-04 17:04:30 -08005116// Test that if the initial offer tagged BUNDLE section is rejected due to its
5117// associated RtpTransceiver being stopped and another transceiver is added,
5118// then renegotiation causes the callee to receive the new video track without
5119// error.
5120// This is a regression test for bugs.webrtc.org/9954
5121TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5122 ReOfferWithStoppedBundleTaggedTransceiver) {
5123 RTCConfiguration config;
5124 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5125 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5126 ConnectFakeSignaling();
5127 auto audio_transceiver_or_error =
5128 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5129 ASSERT_TRUE(audio_transceiver_or_error.ok());
5130 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5131
5132 caller()->CreateAndSetAndSignalOffer();
5133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5134 {
5135 MediaExpectations media_expectations;
5136 media_expectations.CalleeExpectsSomeAudio();
5137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5138 }
5139
5140 audio_transceiver->Stop();
5141 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5142
5143 caller()->CreateAndSetAndSignalOffer();
5144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5145 {
5146 MediaExpectations media_expectations;
5147 media_expectations.CalleeExpectsSomeVideo();
5148 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5149 }
5150}
5151
deadbeef1dcb1642017-03-29 21:08:16 -07005152} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005153} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005154
5155#endif // if !defined(THREAD_SANITIZER)