blob: 0fc2e152c3dbcb81640b446696fc4548d99bc793 [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"
Qingsi Wangecd30542019-05-22 14:34:56 -070064#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "rtc_base/fake_network.h"
66#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020068#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080069#include "rtc_base/test_certificate_verifier.h"
70#include "rtc_base/time_utils.h"
71#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020072#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070073#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020074#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070075
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010076namespace webrtc {
77namespace {
78
79using ::cricket::ContentInfo;
80using ::cricket::StreamParams;
81using ::rtc::SocketAddress;
82using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080083using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080084using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010085using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070086using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080087using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080088using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070089using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080090using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010091using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080092using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070093
94static const int kDefaultTimeout = 10000;
95static const int kMaxWaitForStatsMs = 3000;
96static const int kMaxWaitForActivationMs = 5000;
97static const int kMaxWaitForFramesMs = 10000;
98// Default number of audio/video frames to wait for before considering a test
99// successful.
100static const int kDefaultExpectedAudioFrameCount = 3;
101static const int kDefaultExpectedVideoFrameCount = 3;
102
deadbeef1dcb1642017-03-29 21:08:16 -0700103static const char kDataChannelLabel[] = "data_channel";
104
105// SRTP cipher name negotiated by the tests. This must be updated if the
106// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700107static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700108static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
109
Steve Antonede9ca52017-10-16 13:04:27 -0700110static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
111
deadbeef1dcb1642017-03-29 21:08:16 -0700112// Helper function for constructing offer/answer options to initiate an ICE
113// restart.
114PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
115 PeerConnectionInterface::RTCOfferAnswerOptions options;
116 options.ice_restart = true;
117 return options;
118}
119
deadbeefd8ad7882017-04-18 16:01:17 -0700120// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
121// attribute from received SDP, simulating a legacy endpoint.
122void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
123 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800124 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700125 }
126 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100127 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700128}
129
Seth Hampson5897a6e2018-04-03 11:16:33 -0700130// Removes all stream information besides the stream ids, simulating an
131// endpoint that only signals a=msid lines to convey stream_ids.
132void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
133 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700134 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700135 std::vector<std::string> stream_ids;
136 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700137 const StreamParams& first_stream =
138 content.media_description()->streams()[0];
139 track_id = first_stream.id;
140 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700141 }
142 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700143 StreamParams new_stream;
144 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700145 new_stream.set_stream_ids(stream_ids);
146 content.media_description()->AddStream(new_stream);
147 }
148}
149
zhihuangf8164932017-05-19 13:09:47 -0700150int FindFirstMediaStatsIndexByKind(
151 const std::string& kind,
152 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
153 media_stats_vec) {
154 for (size_t i = 0; i < media_stats_vec.size(); i++) {
155 if (media_stats_vec[i]->kind.ValueToString() == kind) {
156 return i;
157 }
158 }
159 return -1;
160}
161
deadbeef1dcb1642017-03-29 21:08:16 -0700162class SignalingMessageReceiver {
163 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800164 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700165 virtual void ReceiveIceMessage(const std::string& sdp_mid,
166 int sdp_mline_index,
167 const std::string& msg) = 0;
168
169 protected:
170 SignalingMessageReceiver() {}
171 virtual ~SignalingMessageReceiver() {}
172};
173
174class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
175 public:
176 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
177 : expected_media_type_(media_type) {}
178
179 void OnFirstPacketReceived(cricket::MediaType media_type) override {
180 ASSERT_EQ(expected_media_type_, media_type);
181 first_packet_received_ = true;
182 }
183
184 bool first_packet_received() const { return first_packet_received_; }
185
186 virtual ~MockRtpReceiverObserver() {}
187
188 private:
189 bool first_packet_received_ = false;
190 cricket::MediaType expected_media_type_;
191};
192
193// Helper class that wraps a peer connection, observes it, and can accept
194// signaling messages from another wrapper.
195//
196// Uses a fake network, fake A/V capture, and optionally fake
197// encoders/decoders, though they aren't used by default since they don't
198// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700199// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800200// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700201class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800202 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700203 public:
204 // Different factory methods for convenience.
205 // TODO(deadbeef): Could use the pattern of:
206 //
207 // PeerConnectionWrapper =
208 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
209 //
210 // To reduce some code duplication.
211 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
212 const std::string& debug_name,
213 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
214 rtc::Thread* network_thread,
215 rtc::Thread* worker_thread) {
216 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700217 webrtc::PeerConnectionDependencies dependencies(nullptr);
218 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200219 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800220 worker_thread, nullptr,
221 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700222 delete client;
223 return nullptr;
224 }
225 return client;
226 }
227
deadbeef2f425aa2017-04-14 10:41:32 -0700228 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
229 return peer_connection_factory_.get();
230 }
231
deadbeef1dcb1642017-03-29 21:08:16 -0700232 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
233
234 // If a signaling message receiver is set (via ConnectFakeSignaling), this
235 // will set the whole offer/answer exchange in motion. Just need to wait for
236 // the signaling state to reach "stable".
237 void CreateAndSetAndSignalOffer() {
238 auto offer = CreateOffer();
239 ASSERT_NE(nullptr, offer);
240 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
241 }
242
243 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
244 // when a remote offer is received (via fake signaling) and an answer is
245 // generated. By default, uses default options.
246 void SetOfferAnswerOptions(
247 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
248 offer_answer_options_ = options;
249 }
250
251 // Set a callback to be invoked when SDP is received via the fake signaling
252 // channel, which provides an opportunity to munge (modify) the SDP. This is
253 // used to test SDP being applied that a PeerConnection would normally not
254 // generate, but a non-JSEP endpoint might.
255 void SetReceivedSdpMunger(
256 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100257 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700258 }
259
deadbeefc964d0b2017-04-03 10:03:35 -0700260 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700261 // generated.
262 void SetGeneratedSdpMunger(
263 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100264 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700265 }
266
Seth Hampson2f0d7022018-02-20 11:54:42 -0800267 // Set a callback to be invoked when a remote offer is received via the fake
268 // signaling channel. This provides an opportunity to change the
269 // PeerConnection state before an answer is created and sent to the caller.
270 void SetRemoteOfferHandler(std::function<void()> handler) {
271 remote_offer_handler_ = std::move(handler);
272 }
273
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800274 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
275 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700276 }
277
Steve Antonede9ca52017-10-16 13:04:27 -0700278 // Every ICE connection state in order that has been seen by the observer.
279 std::vector<PeerConnectionInterface::IceConnectionState>
280 ice_connection_state_history() const {
281 return ice_connection_state_history_;
282 }
Steve Anton6f25b092017-10-23 09:39:20 -0700283 void clear_ice_connection_state_history() {
284 ice_connection_state_history_.clear();
285 }
Steve Antonede9ca52017-10-16 13:04:27 -0700286
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100287 // Every standardized ICE connection state in order that has been seen by the
288 // observer.
289 std::vector<PeerConnectionInterface::IceConnectionState>
290 standardized_ice_connection_state_history() const {
291 return standardized_ice_connection_state_history_;
292 }
293
Jonas Olsson635474e2018-10-18 15:58:17 +0200294 // Every PeerConnection state in order that has been seen by the observer.
295 std::vector<PeerConnectionInterface::PeerConnectionState>
296 peer_connection_state_history() const {
297 return peer_connection_state_history_;
298 }
299
Steve Antonede9ca52017-10-16 13:04:27 -0700300 // Every ICE gathering state in order that has been seen by the observer.
301 std::vector<PeerConnectionInterface::IceGatheringState>
302 ice_gathering_state_history() const {
303 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700304 }
305
Steve Anton15324772018-01-16 10:26:49 -0800306 void AddAudioVideoTracks() {
307 AddAudioTrack();
308 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700309 }
310
Steve Anton74255ff2018-01-24 18:32:57 -0800311 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
312 return AddTrack(CreateLocalAudioTrack());
313 }
deadbeef1dcb1642017-03-29 21:08:16 -0700314
Steve Anton74255ff2018-01-24 18:32:57 -0800315 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
316 return AddTrack(CreateLocalVideoTrack());
317 }
deadbeef1dcb1642017-03-29 21:08:16 -0700318
319 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200320 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700321 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200322 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700323 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200324 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700325 // TODO(perkj): Test audio source when it is implemented. Currently audio
326 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700327 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700328 source);
329 }
330
331 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200332 webrtc::FakePeriodicVideoSource::Config config;
333 config.timestamp_offset_ms = rtc::TimeMillis();
334 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700335 }
336
337 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200338 CreateLocalVideoTrackWithConfig(
339 webrtc::FakePeriodicVideoSource::Config config) {
340 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700341 }
342
343 rtc::scoped_refptr<webrtc::VideoTrackInterface>
344 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200345 webrtc::FakePeriodicVideoSource::Config config;
346 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200347 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200348 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700349 }
350
Steve Anton74255ff2018-01-24 18:32:57 -0800351 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
352 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800353 const std::vector<std::string>& stream_ids = {}) {
354 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800355 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800356 return result.MoveValue();
357 }
358
359 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
360 cricket::MediaType media_type) {
361 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100362 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800363 if (receiver->media_type() == media_type) {
364 receivers.push_back(receiver);
365 }
366 }
367 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700368 }
369
Seth Hampson2f0d7022018-02-20 11:54:42 -0800370 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
371 cricket::MediaType media_type) {
372 for (auto transceiver : pc()->GetTransceivers()) {
373 if (transceiver->receiver()->media_type() == media_type) {
374 return transceiver;
375 }
376 }
377 return nullptr;
378 }
379
deadbeef1dcb1642017-03-29 21:08:16 -0700380 bool SignalingStateStable() {
381 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
382 }
383
384 void CreateDataChannel() { CreateDataChannel(nullptr); }
385
386 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700387 CreateDataChannel(kDataChannelLabel, init);
388 }
389
390 void CreateDataChannel(const std::string& label,
391 const webrtc::DataChannelInit* init) {
392 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700393 ASSERT_TRUE(data_channel_.get() != nullptr);
394 data_observer_.reset(new MockDataChannelObserver(data_channel_));
395 }
396
397 DataChannelInterface* data_channel() { return data_channel_; }
398 const MockDataChannelObserver* data_observer() const {
399 return data_observer_.get();
400 }
401
402 int audio_frames_received() const {
403 return fake_audio_capture_module_->frames_received();
404 }
405
406 // Takes minimum of video frames received for each track.
407 //
408 // Can be used like:
409 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
410 //
411 // To ensure that all video tracks received at least a certain number of
412 // frames.
413 int min_video_frames_received_per_track() const {
414 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200415 if (fake_video_renderers_.empty()) {
416 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700417 }
deadbeef1dcb1642017-03-29 21:08:16 -0700418
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200419 for (const auto& pair : fake_video_renderers_) {
420 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700421 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200422 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700423 }
424
425 // Returns a MockStatsObserver in a state after stats gathering finished,
426 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700427 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700428 webrtc::MediaStreamTrackInterface* track) {
429 rtc::scoped_refptr<MockStatsObserver> observer(
430 new rtc::RefCountedObject<MockStatsObserver>());
431 EXPECT_TRUE(peer_connection_->GetStats(
432 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
433 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
434 return observer;
435 }
436
437 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700438 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
439 return OldGetStatsForTrack(nullptr);
440 }
441
442 // Synchronously gets stats and returns them. If it times out, fails the test
443 // and returns null.
444 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
445 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
446 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
447 peer_connection_->GetStats(callback);
448 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
449 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700450 }
451
452 int rendered_width() {
453 EXPECT_FALSE(fake_video_renderers_.empty());
454 return fake_video_renderers_.empty()
455 ? 0
456 : fake_video_renderers_.begin()->second->width();
457 }
458
459 int rendered_height() {
460 EXPECT_FALSE(fake_video_renderers_.empty());
461 return fake_video_renderers_.empty()
462 ? 0
463 : fake_video_renderers_.begin()->second->height();
464 }
465
466 double rendered_aspect_ratio() {
467 if (rendered_height() == 0) {
468 return 0.0;
469 }
470 return static_cast<double>(rendered_width()) / rendered_height();
471 }
472
473 webrtc::VideoRotation rendered_rotation() {
474 EXPECT_FALSE(fake_video_renderers_.empty());
475 return fake_video_renderers_.empty()
476 ? webrtc::kVideoRotation_0
477 : fake_video_renderers_.begin()->second->rotation();
478 }
479
480 int local_rendered_width() {
481 return local_video_renderer_ ? local_video_renderer_->width() : 0;
482 }
483
484 int local_rendered_height() {
485 return local_video_renderer_ ? local_video_renderer_->height() : 0;
486 }
487
488 double local_rendered_aspect_ratio() {
489 if (local_rendered_height() == 0) {
490 return 0.0;
491 }
492 return static_cast<double>(local_rendered_width()) /
493 local_rendered_height();
494 }
495
496 size_t number_of_remote_streams() {
497 if (!pc()) {
498 return 0;
499 }
500 return pc()->remote_streams()->count();
501 }
502
503 StreamCollectionInterface* remote_streams() const {
504 if (!pc()) {
505 ADD_FAILURE();
506 return nullptr;
507 }
508 return pc()->remote_streams();
509 }
510
511 StreamCollectionInterface* local_streams() {
512 if (!pc()) {
513 ADD_FAILURE();
514 return nullptr;
515 }
516 return pc()->local_streams();
517 }
518
519 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
520 return pc()->signaling_state();
521 }
522
523 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
524 return pc()->ice_connection_state();
525 }
526
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100527 webrtc::PeerConnectionInterface::IceConnectionState
528 standardized_ice_connection_state() {
529 return pc()->standardized_ice_connection_state();
530 }
531
deadbeef1dcb1642017-03-29 21:08:16 -0700532 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
533 return pc()->ice_gathering_state();
534 }
535
536 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
537 // GetReceivers. They're updated automatically when a remote offer/answer
538 // from the fake signaling channel is applied, or when
539 // ResetRtpReceiverObservers below is called.
540 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
541 rtp_receiver_observers() {
542 return rtp_receiver_observers_;
543 }
544
545 void ResetRtpReceiverObservers() {
546 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100547 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
548 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700549 std::unique_ptr<MockRtpReceiverObserver> observer(
550 new MockRtpReceiverObserver(receiver->media_type()));
551 receiver->SetObserver(observer.get());
552 rtp_receiver_observers_.push_back(std::move(observer));
553 }
554 }
555
Qingsi Wangecd30542019-05-22 14:34:56 -0700556 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700557 return fake_network_manager_.get();
558 }
559 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
560
Qingsi Wang7685e862018-06-11 20:15:46 -0700561 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
562 return event_log_factory_;
563 }
564
Qingsi Wangc129c352019-04-18 10:41:58 -0700565 const cricket::Candidate& last_candidate_gathered() const {
566 return last_candidate_gathered_;
567 }
568
Qingsi Wangecd30542019-05-22 14:34:56 -0700569 // Sets the mDNS responder for the owned fake network manager and keeps a
570 // reference to the responder.
571 void SetMdnsResponder(
572 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
573 RTC_DCHECK(mdns_responder != nullptr);
574 mdns_responder_ = mdns_responder.get();
575 network_manager()->set_mdns_responder(std::move(mdns_responder));
576 }
577
deadbeef1dcb1642017-03-29 21:08:16 -0700578 private:
579 explicit PeerConnectionWrapper(const std::string& debug_name)
580 : debug_name_(debug_name) {}
581
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800582 bool Init(
583 const PeerConnectionFactory::Options* options,
584 const PeerConnectionInterface::RTCConfiguration* config,
585 webrtc::PeerConnectionDependencies dependencies,
586 rtc::Thread* network_thread,
587 rtc::Thread* worker_thread,
588 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
589 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700590 // There's an error in this test code if Init ends up being called twice.
591 RTC_DCHECK(!peer_connection_);
592 RTC_DCHECK(!peer_connection_factory_);
593
594 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700595 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700596
597 std::unique_ptr<cricket::PortAllocator> port_allocator(
598 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700599 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700600 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
601 if (!fake_audio_capture_module_) {
602 return false;
603 }
deadbeef1dcb1642017-03-29 21:08:16 -0700604 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700605
606 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
607 pc_factory_dependencies.network_thread = network_thread;
608 pc_factory_dependencies.worker_thread = worker_thread;
609 pc_factory_dependencies.signaling_thread = signaling_thread;
610 pc_factory_dependencies.media_engine =
611 cricket::WebRtcMediaEngineFactory::Create(
612 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
613 fake_audio_capture_module_),
614 webrtc::CreateBuiltinAudioEncoderFactory(),
615 webrtc::CreateBuiltinAudioDecoderFactory(),
616 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000617 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700618 webrtc::AudioProcessingBuilder().Create());
619 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
620 if (event_log_factory) {
621 event_log_factory_ = event_log_factory.get();
622 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
623 } else {
624 pc_factory_dependencies.event_log_factory =
625 webrtc::CreateRtcEventLogFactory();
626 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800627 if (media_transport_factory) {
628 pc_factory_dependencies.media_transport_factory =
629 std::move(media_transport_factory);
630 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700631 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
632 std::move(pc_factory_dependencies));
633
deadbeef1dcb1642017-03-29 21:08:16 -0700634 if (!peer_connection_factory_) {
635 return false;
636 }
637 if (options) {
638 peer_connection_factory_->SetOptions(*options);
639 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800640 if (config) {
641 sdp_semantics_ = config->sdp_semantics;
642 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700643
644 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200645 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700646 return peer_connection_.get() != nullptr;
647 }
648
649 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700650 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700651 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700652 PeerConnectionInterface::RTCConfiguration modified_config;
653 // If |config| is null, this will result in a default configuration being
654 // used.
655 if (config) {
656 modified_config = *config;
657 }
658 // Disable resolution adaptation; we don't want it interfering with the
659 // test results.
660 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
661 // ratios and not specific resolutions, is this even necessary?
662 modified_config.set_cpu_adaptation(false);
663
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700664 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700665 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700666 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700667 }
668
669 void set_signaling_message_receiver(
670 SignalingMessageReceiver* signaling_message_receiver) {
671 signaling_message_receiver_ = signaling_message_receiver;
672 }
673
674 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
675
Steve Antonede9ca52017-10-16 13:04:27 -0700676 void set_signal_ice_candidates(bool signal) {
677 signal_ice_candidates_ = signal;
678 }
679
deadbeef1dcb1642017-03-29 21:08:16 -0700680 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200681 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700682 // Set max frame rate to 10fps to reduce the risk of test flakiness.
683 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200684 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700685
Niels Möller5c7efe72018-05-11 10:34:46 +0200686 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200687 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
688 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700689 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200690 peer_connection_factory_->CreateVideoTrack(
691 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (!local_video_renderer_) {
693 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
694 }
695 return track;
696 }
697
698 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800700 std::unique_ptr<SessionDescriptionInterface> desc =
701 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700702 if (received_sdp_munger_) {
703 received_sdp_munger_(desc->description());
704 }
705
706 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
707 // Setting a remote description may have changed the number of receivers,
708 // so reset the receiver observers.
709 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800710 if (remote_offer_handler_) {
711 remote_offer_handler_();
712 }
deadbeef1dcb1642017-03-29 21:08:16 -0700713 auto answer = CreateAnswer();
714 ASSERT_NE(nullptr, answer);
715 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
716 }
717
718 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800720 std::unique_ptr<SessionDescriptionInterface> desc =
721 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700722 if (received_sdp_munger_) {
723 received_sdp_munger_(desc->description());
724 }
725
726 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
727 // Set the RtpReceiverObserver after receivers are created.
728 ResetRtpReceiverObservers();
729 }
730
731 // Returns null on failure.
732 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
733 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
734 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
735 pc()->CreateOffer(observer, offer_answer_options_);
736 return WaitForDescriptionFromObserver(observer);
737 }
738
739 // Returns null on failure.
740 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
741 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
742 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
743 pc()->CreateAnswer(observer, offer_answer_options_);
744 return WaitForDescriptionFromObserver(observer);
745 }
746
747 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100748 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700749 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
750 if (!observer->result()) {
751 return nullptr;
752 }
753 auto description = observer->MoveDescription();
754 if (generated_sdp_munger_) {
755 generated_sdp_munger_(description->description());
756 }
757 return description;
758 }
759
760 // Setting the local description and sending the SDP message over the fake
761 // signaling channel are combined into the same method because the SDP
762 // message needs to be sent as soon as SetLocalDescription finishes, without
763 // waiting for the observer to be called. This ensures that ICE candidates
764 // don't outrace the description.
765 bool SetLocalDescriptionAndSendSdpMessage(
766 std::unique_ptr<SessionDescriptionInterface> desc) {
767 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
768 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100769 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800770 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700771 std::string sdp;
772 EXPECT_TRUE(desc->ToString(&sdp));
773 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800774 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
775 RemoveUnusedVideoRenderers();
776 }
deadbeef1dcb1642017-03-29 21:08:16 -0700777 // As mentioned above, we need to send the message immediately after
778 // SetLocalDescription.
779 SendSdpMessage(type, sdp);
780 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
781 return true;
782 }
783
784 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
785 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
786 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100787 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700788 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800789 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
790 RemoveUnusedVideoRenderers();
791 }
deadbeef1dcb1642017-03-29 21:08:16 -0700792 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
793 return observer->result();
794 }
795
Seth Hampson2f0d7022018-02-20 11:54:42 -0800796 // This is a work around to remove unused fake_video_renderers from
797 // transceivers that have either stopped or are no longer receiving.
798 void RemoveUnusedVideoRenderers() {
799 auto transceivers = pc()->GetTransceivers();
800 for (auto& transceiver : transceivers) {
801 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
802 continue;
803 }
804 // Remove fake video renderers from any stopped transceivers.
805 if (transceiver->stopped()) {
806 auto it =
807 fake_video_renderers_.find(transceiver->receiver()->track()->id());
808 if (it != fake_video_renderers_.end()) {
809 fake_video_renderers_.erase(it);
810 }
811 }
812 // Remove fake video renderers from any transceivers that are no longer
813 // receiving.
814 if ((transceiver->current_direction() &&
815 !webrtc::RtpTransceiverDirectionHasRecv(
816 *transceiver->current_direction()))) {
817 auto it =
818 fake_video_renderers_.find(transceiver->receiver()->track()->id());
819 if (it != fake_video_renderers_.end()) {
820 fake_video_renderers_.erase(it);
821 }
822 }
823 }
824 }
825
deadbeef1dcb1642017-03-29 21:08:16 -0700826 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
827 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800828 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700829 if (signaling_delay_ms_ == 0) {
830 RelaySdpMessageIfReceiverExists(type, msg);
831 } else {
832 invoker_.AsyncInvokeDelayed<void>(
833 RTC_FROM_HERE, rtc::Thread::Current(),
834 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
835 this, type, msg),
836 signaling_delay_ms_);
837 }
838 }
839
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700841 if (signaling_message_receiver_) {
842 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
843 }
844 }
845
846 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
847 // default).
848 void SendIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) {
851 if (signaling_delay_ms_ == 0) {
852 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
853 } else {
854 invoker_.AsyncInvokeDelayed<void>(
855 RTC_FROM_HERE, rtc::Thread::Current(),
856 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
857 this, sdp_mid, sdp_mline_index, msg),
858 signaling_delay_ms_);
859 }
860 }
861
862 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
863 int sdp_mline_index,
864 const std::string& msg) {
865 if (signaling_message_receiver_) {
866 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
867 msg);
868 }
869 }
870
871 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800872 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
873 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700874 HandleIncomingOffer(msg);
875 } else {
876 HandleIncomingAnswer(msg);
877 }
878 }
879
880 void ReceiveIceMessage(const std::string& sdp_mid,
881 int sdp_mline_index,
882 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100883 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700884 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
885 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
886 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
887 }
888
889 // PeerConnectionObserver callbacks.
890 void OnSignalingChange(
891 webrtc::PeerConnectionInterface::SignalingState new_state) override {
892 EXPECT_EQ(pc()->signaling_state(), new_state);
893 }
Steve Anton15324772018-01-16 10:26:49 -0800894 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
895 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
896 streams) override {
897 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
898 rtc::scoped_refptr<VideoTrackInterface> video_track(
899 static_cast<VideoTrackInterface*>(receiver->track().get()));
900 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700901 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800902 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200903 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700904 }
905 }
Steve Anton15324772018-01-16 10:26:49 -0800906 void OnRemoveTrack(
907 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
908 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
909 auto it = fake_video_renderers_.find(receiver->track()->id());
910 RTC_DCHECK(it != fake_video_renderers_.end());
911 fake_video_renderers_.erase(it);
912 }
913 }
deadbeef1dcb1642017-03-29 21:08:16 -0700914 void OnRenegotiationNeeded() override {}
915 void OnIceConnectionChange(
916 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
917 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700918 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700919 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100920 void OnStandardizedIceConnectionChange(
921 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
922 standardized_ice_connection_state_history_.push_back(new_state);
923 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200924 void OnConnectionChange(
925 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
926 peer_connection_state_history_.push_back(new_state);
927 }
928
deadbeef1dcb1642017-03-29 21:08:16 -0700929 void OnIceGatheringChange(
930 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700931 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700932 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100935 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700936
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800937 if (remote_async_resolver_) {
938 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800939 if (local_candidate.address().IsUnresolvedIP()) {
940 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
941 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700942 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800943 local_candidate.address().hostname());
944 RTC_DCHECK(!resolved_ip.IsNil());
945 resolved_addr.SetResolvedIP(resolved_ip);
946 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
947 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
948 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700949 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700950 }
951
deadbeef1dcb1642017-03-29 21:08:16 -0700952 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800953 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700954 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700955 // Remote party may be deleted.
956 return;
957 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800958 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700959 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700960 }
961 void OnDataChannel(
962 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100963 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700964 data_channel_ = data_channel;
965 data_observer_.reset(new MockDataChannelObserver(data_channel));
966 }
967
deadbeef1dcb1642017-03-29 21:08:16 -0700968 std::string debug_name_;
969
970 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -0700971 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
972 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700973
974 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
975 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
976 peer_connection_factory_;
977
Steve Antonede9ca52017-10-16 13:04:27 -0700978 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700979 // Needed to keep track of number of frames sent.
980 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
981 // Needed to keep track of number of frames received.
982 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
983 fake_video_renderers_;
984 // Needed to ensure frames aren't received for removed tracks.
985 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
986 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700987
988 // For remote peer communication.
989 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
990 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700991 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -0700992 cricket::Candidate last_candidate_gathered_;
deadbeef1dcb1642017-03-29 21:08:16 -0700993
Niels Möller5c7efe72018-05-11 10:34:46 +0200994 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700995 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200996 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
997 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700998 // |local_video_renderer_| attached to the first created local video track.
999 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1000
Seth Hampson2f0d7022018-02-20 11:54:42 -08001001 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001002 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1003 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1004 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001005 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001006 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001007 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1008 std::unique_ptr<MockDataChannelObserver> data_observer_;
1009
1010 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1011
Steve Antonede9ca52017-10-16 13:04:27 -07001012 std::vector<PeerConnectionInterface::IceConnectionState>
1013 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001014 std::vector<PeerConnectionInterface::IceConnectionState>
1015 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001016 std::vector<PeerConnectionInterface::PeerConnectionState>
1017 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001018 std::vector<PeerConnectionInterface::IceGatheringState>
1019 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001020
Qingsi Wang7685e862018-06-11 20:15:46 -07001021 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1022
deadbeef1dcb1642017-03-29 21:08:16 -07001023 rtc::AsyncInvoker invoker_;
1024
Seth Hampson2f0d7022018-02-20 11:54:42 -08001025 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001026};
1027
Elad Alon99c3fe52017-10-13 16:29:40 +02001028class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1029 public:
1030 virtual ~MockRtcEventLogOutput() = default;
1031 MOCK_CONST_METHOD0(IsActive, bool());
1032 MOCK_METHOD1(Write, bool(const std::string&));
1033};
1034
Seth Hampson2f0d7022018-02-20 11:54:42 -08001035// This helper object is used for both specifying how many audio/video frames
1036// are expected to be received for a caller/callee. It provides helper functions
1037// to specify these expectations. The object initially starts in a state of no
1038// expectations.
1039class MediaExpectations {
1040 public:
1041 enum ExpectFrames {
1042 kExpectSomeFrames,
1043 kExpectNoFrames,
1044 kNoExpectation,
1045 };
1046
1047 void ExpectBidirectionalAudioAndVideo() {
1048 ExpectBidirectionalAudio();
1049 ExpectBidirectionalVideo();
1050 }
1051
1052 void ExpectBidirectionalAudio() {
1053 CallerExpectsSomeAudio();
1054 CalleeExpectsSomeAudio();
1055 }
1056
1057 void ExpectNoAudio() {
1058 CallerExpectsNoAudio();
1059 CalleeExpectsNoAudio();
1060 }
1061
1062 void ExpectBidirectionalVideo() {
1063 CallerExpectsSomeVideo();
1064 CalleeExpectsSomeVideo();
1065 }
1066
1067 void ExpectNoVideo() {
1068 CallerExpectsNoVideo();
1069 CalleeExpectsNoVideo();
1070 }
1071
1072 void CallerExpectsSomeAudioAndVideo() {
1073 CallerExpectsSomeAudio();
1074 CallerExpectsSomeVideo();
1075 }
1076
1077 void CalleeExpectsSomeAudioAndVideo() {
1078 CalleeExpectsSomeAudio();
1079 CalleeExpectsSomeVideo();
1080 }
1081
1082 // Caller's audio functions.
1083 void CallerExpectsSomeAudio(
1084 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1085 caller_audio_expectation_ = kExpectSomeFrames;
1086 caller_audio_frames_expected_ = expected_audio_frames;
1087 }
1088
1089 void CallerExpectsNoAudio() {
1090 caller_audio_expectation_ = kExpectNoFrames;
1091 caller_audio_frames_expected_ = 0;
1092 }
1093
1094 // Caller's video functions.
1095 void CallerExpectsSomeVideo(
1096 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1097 caller_video_expectation_ = kExpectSomeFrames;
1098 caller_video_frames_expected_ = expected_video_frames;
1099 }
1100
1101 void CallerExpectsNoVideo() {
1102 caller_video_expectation_ = kExpectNoFrames;
1103 caller_video_frames_expected_ = 0;
1104 }
1105
1106 // Callee's audio functions.
1107 void CalleeExpectsSomeAudio(
1108 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1109 callee_audio_expectation_ = kExpectSomeFrames;
1110 callee_audio_frames_expected_ = expected_audio_frames;
1111 }
1112
1113 void CalleeExpectsNoAudio() {
1114 callee_audio_expectation_ = kExpectNoFrames;
1115 callee_audio_frames_expected_ = 0;
1116 }
1117
1118 // Callee's video functions.
1119 void CalleeExpectsSomeVideo(
1120 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1121 callee_video_expectation_ = kExpectSomeFrames;
1122 callee_video_frames_expected_ = expected_video_frames;
1123 }
1124
1125 void CalleeExpectsNoVideo() {
1126 callee_video_expectation_ = kExpectNoFrames;
1127 callee_video_frames_expected_ = 0;
1128 }
1129
1130 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1131 ExpectFrames caller_video_expectation_ = kNoExpectation;
1132 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1133 ExpectFrames callee_video_expectation_ = kNoExpectation;
1134 int caller_audio_frames_expected_ = 0;
1135 int caller_video_frames_expected_ = 0;
1136 int callee_audio_frames_expected_ = 0;
1137 int callee_video_frames_expected_ = 0;
1138};
1139
deadbeef1dcb1642017-03-29 21:08:16 -07001140// Tests two PeerConnections connecting to each other end-to-end, using a
1141// virtual network, fake A/V capture and fake encoder/decoders. The
1142// PeerConnections share the threads/socket servers, but use separate versions
1143// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001144class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001145 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001146 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1147 : sdp_semantics_(sdp_semantics),
1148 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001149 fss_(new rtc::FirewallSocketServer(ss_.get())),
1150 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001151 worker_thread_(rtc::Thread::Create()),
1152 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001153 network_thread_->SetName("PCNetworkThread", this);
1154 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001155 RTC_CHECK(network_thread_->Start());
1156 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001157 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001158 }
1159
Seth Hampson2f0d7022018-02-20 11:54:42 -08001160 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001161 // The PeerConnections should deleted before the TurnCustomizers.
1162 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1163 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1164 // that the TurnCustomizer outlives the life of the PeerConnection or else
1165 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001166 if (caller_) {
1167 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001168 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001169 }
1170 if (callee_) {
1171 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001172 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001173 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001174
1175 // If turn servers were created for the test they need to be destroyed on
1176 // the network thread.
1177 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1178 turn_servers_.clear();
1179 turn_customizers_.clear();
1180 });
deadbeef1dcb1642017-03-29 21:08:16 -07001181 }
1182
1183 bool SignalingStateStable() {
1184 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1185 }
1186
deadbeef71452802017-05-07 17:21:01 -07001187 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001188 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1189 // are connected. This is an important distinction. Once we have separate
1190 // ICE and DTLS state, this check needs to use the DTLS state.
1191 return (callee()->ice_connection_state() ==
1192 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1193 callee()->ice_connection_state() ==
1194 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1195 (caller()->ice_connection_state() ==
1196 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1197 caller()->ice_connection_state() ==
1198 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001199 }
1200
Qingsi Wang7685e862018-06-11 20:15:46 -07001201 // When |event_log_factory| is null, the default implementation of the event
1202 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001203 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1204 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001205 const PeerConnectionFactory::Options* options,
1206 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001207 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001208 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1209 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001210 RTCConfiguration modified_config;
1211 if (config) {
1212 modified_config = *config;
1213 }
Steve Anton3acffc32018-04-12 17:21:03 -07001214 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001215 if (!dependencies.cert_generator) {
1216 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001217 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001218 }
1219 std::unique_ptr<PeerConnectionWrapper> client(
1220 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001221
Niels Möllerf06f9232018-08-07 12:32:18 +02001222 if (!client->Init(options, &modified_config, std::move(dependencies),
1223 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001224 std::move(event_log_factory),
1225 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001226 return nullptr;
1227 }
1228 return client;
1229 }
1230
Qingsi Wang7685e862018-06-11 20:15:46 -07001231 std::unique_ptr<PeerConnectionWrapper>
1232 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1233 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001234 const PeerConnectionFactory::Options* options,
1235 const RTCConfiguration* config,
1236 webrtc::PeerConnectionDependencies dependencies) {
1237 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1238 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001239 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001240 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001241 std::move(event_log_factory),
1242 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001243 }
1244
deadbeef1dcb1642017-03-29 21:08:16 -07001245 bool CreatePeerConnectionWrappers() {
1246 return CreatePeerConnectionWrappersWithConfig(
1247 PeerConnectionInterface::RTCConfiguration(),
1248 PeerConnectionInterface::RTCConfiguration());
1249 }
1250
Steve Anton3acffc32018-04-12 17:21:03 -07001251 bool CreatePeerConnectionWrappersWithSdpSemantics(
1252 SdpSemantics caller_semantics,
1253 SdpSemantics callee_semantics) {
1254 // Can't specify the sdp_semantics in the passed-in configuration since it
1255 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1256 // stored in sdp_semantics_. So get around this by modifying the instance
1257 // variable before calling CreatePeerConnectionWrapper for the caller and
1258 // callee PeerConnections.
1259 SdpSemantics original_semantics = sdp_semantics_;
1260 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001261 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001262 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001263 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001264 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001265 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001266 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001267 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001268 sdp_semantics_ = original_semantics;
1269 return caller_ && callee_;
1270 }
1271
deadbeef1dcb1642017-03-29 21:08:16 -07001272 bool CreatePeerConnectionWrappersWithConfig(
1273 const PeerConnectionInterface::RTCConfiguration& caller_config,
1274 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001275 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001276 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001277 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1278 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001279 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001280 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001281 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1282 /*media_transport_factory=*/nullptr);
1283 return caller_ && callee_;
1284 }
1285
1286 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1287 const PeerConnectionInterface::RTCConfiguration& caller_config,
1288 const PeerConnectionInterface::RTCConfiguration& callee_config,
1289 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1290 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1291 caller_ =
1292 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1293 webrtc::PeerConnectionDependencies(nullptr),
1294 nullptr, std::move(caller_factory));
1295 callee_ =
1296 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1297 webrtc::PeerConnectionDependencies(nullptr),
1298 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001299 return caller_ && callee_;
1300 }
1301
1302 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1303 const PeerConnectionInterface::RTCConfiguration& caller_config,
1304 webrtc::PeerConnectionDependencies caller_dependencies,
1305 const PeerConnectionInterface::RTCConfiguration& callee_config,
1306 webrtc::PeerConnectionDependencies callee_dependencies) {
1307 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001308 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001309 std::move(caller_dependencies), nullptr,
1310 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001311 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001312 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001313 std::move(callee_dependencies), nullptr,
1314 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001315 return caller_ && callee_;
1316 }
1317
1318 bool CreatePeerConnectionWrappersWithOptions(
1319 const PeerConnectionFactory::Options& caller_options,
1320 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001321 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001322 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001323 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1324 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001325 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001326 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001327 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1328 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001329 return caller_ && callee_;
1330 }
1331
1332 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1333 PeerConnectionInterface::RTCConfiguration default_config;
1334 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001335 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001336 webrtc::PeerConnectionDependencies(nullptr));
1337 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001338 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001339 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001340 return caller_ && callee_;
1341 }
1342
Seth Hampson2f0d7022018-02-20 11:54:42 -08001343 std::unique_ptr<PeerConnectionWrapper>
1344 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001345 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1346 new FakeRTCCertificateGenerator());
1347 cert_generator->use_alternate_key();
1348
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001349 webrtc::PeerConnectionDependencies dependencies(nullptr);
1350 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001351 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001352 std::move(dependencies), nullptr,
1353 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001354 }
1355
Seth Hampsonaed71642018-06-11 07:41:32 -07001356 cricket::TestTurnServer* CreateTurnServer(
1357 rtc::SocketAddress internal_address,
1358 rtc::SocketAddress external_address,
1359 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1360 const std::string& common_name = "test turn server") {
1361 rtc::Thread* thread = network_thread();
1362 std::unique_ptr<cricket::TestTurnServer> turn_server =
1363 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1364 RTC_FROM_HERE,
1365 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001366 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001367 thread, internal_address, external_address, type,
1368 /*ignore_bad_certs=*/true, common_name);
1369 });
1370 turn_servers_.push_back(std::move(turn_server));
1371 // Interactions with the turn server should be done on the network thread.
1372 return turn_servers_.back().get();
1373 }
1374
1375 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1376 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1377 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1378 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001379 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001380 turn_customizers_.push_back(std::move(turn_customizer));
1381 // Interactions with the turn customizer should be done on the network
1382 // thread.
1383 return turn_customizers_.back().get();
1384 }
1385
1386 // Checks that the function counters for a TestTurnCustomizer are greater than
1387 // 0.
1388 void ExpectTurnCustomizerCountersIncremented(
1389 cricket::TestTurnCustomizer* turn_customizer) {
1390 unsigned int allow_channel_data_counter =
1391 network_thread()->Invoke<unsigned int>(
1392 RTC_FROM_HERE, [turn_customizer] {
1393 return turn_customizer->allow_channel_data_cnt_;
1394 });
1395 EXPECT_GT(allow_channel_data_counter, 0u);
1396 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1397 RTC_FROM_HERE,
1398 [turn_customizer] { return turn_customizer->modify_cnt_; });
1399 EXPECT_GT(modify_counter, 0u);
1400 }
1401
deadbeef1dcb1642017-03-29 21:08:16 -07001402 // Once called, SDP blobs and ICE candidates will be automatically signaled
1403 // between PeerConnections.
1404 void ConnectFakeSignaling() {
1405 caller_->set_signaling_message_receiver(callee_.get());
1406 callee_->set_signaling_message_receiver(caller_.get());
1407 }
1408
Steve Antonede9ca52017-10-16 13:04:27 -07001409 // Once called, SDP blobs will be automatically signaled between
1410 // PeerConnections. Note that ICE candidates will not be signaled unless they
1411 // are in the exchanged SDP blobs.
1412 void ConnectFakeSignalingForSdpOnly() {
1413 ConnectFakeSignaling();
1414 SetSignalIceCandidates(false);
1415 }
1416
deadbeef1dcb1642017-03-29 21:08:16 -07001417 void SetSignalingDelayMs(int delay_ms) {
1418 caller_->set_signaling_delay_ms(delay_ms);
1419 callee_->set_signaling_delay_ms(delay_ms);
1420 }
1421
Steve Antonede9ca52017-10-16 13:04:27 -07001422 void SetSignalIceCandidates(bool signal) {
1423 caller_->set_signal_ice_candidates(signal);
1424 callee_->set_signal_ice_candidates(signal);
1425 }
1426
deadbeef1dcb1642017-03-29 21:08:16 -07001427 // Messages may get lost on the unreliable DataChannel, so we send multiple
1428 // times to avoid test flakiness.
1429 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1430 const std::string& data,
1431 int retries) {
1432 for (int i = 0; i < retries; ++i) {
1433 dc->Send(DataBuffer(data));
1434 }
1435 }
1436
1437 rtc::Thread* network_thread() { return network_thread_.get(); }
1438
1439 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1440
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001441 webrtc::MediaTransportPair* loopback_media_transports() {
1442 return &loopback_media_transports_;
1443 }
1444
deadbeef1dcb1642017-03-29 21:08:16 -07001445 PeerConnectionWrapper* caller() { return caller_.get(); }
1446
1447 // Set the |caller_| to the |wrapper| passed in and return the
1448 // original |caller_|.
1449 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1450 PeerConnectionWrapper* wrapper) {
1451 PeerConnectionWrapper* old = caller_.release();
1452 caller_.reset(wrapper);
1453 return old;
1454 }
1455
1456 PeerConnectionWrapper* callee() { return callee_.get(); }
1457
1458 // Set the |callee_| to the |wrapper| passed in and return the
1459 // original |callee_|.
1460 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1461 PeerConnectionWrapper* wrapper) {
1462 PeerConnectionWrapper* old = callee_.release();
1463 callee_.reset(wrapper);
1464 return old;
1465 }
1466
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001467 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1468 network_thread()->Invoke<void>(
1469 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1470 caller()->port_allocator(), caller_flags));
1471 network_thread()->Invoke<void>(
1472 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1473 callee()->port_allocator(), callee_flags));
1474 }
1475
Steve Antonede9ca52017-10-16 13:04:27 -07001476 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1477
Seth Hampson2f0d7022018-02-20 11:54:42 -08001478 // Expects the provided number of new frames to be received within
1479 // kMaxWaitForFramesMs. The new expected frames are specified in
1480 // |media_expectations|. Returns false if any of the expectations were
1481 // not met.
1482 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1483 // First initialize the expected frame counts based upon the current
1484 // frame count.
1485 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1486 if (media_expectations.caller_audio_expectation_ ==
1487 MediaExpectations::kExpectSomeFrames) {
1488 total_caller_audio_frames_expected +=
1489 media_expectations.caller_audio_frames_expected_;
1490 }
1491 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001492 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001493 if (media_expectations.caller_video_expectation_ ==
1494 MediaExpectations::kExpectSomeFrames) {
1495 total_caller_video_frames_expected +=
1496 media_expectations.caller_video_frames_expected_;
1497 }
1498 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1499 if (media_expectations.callee_audio_expectation_ ==
1500 MediaExpectations::kExpectSomeFrames) {
1501 total_callee_audio_frames_expected +=
1502 media_expectations.callee_audio_frames_expected_;
1503 }
1504 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001505 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001506 if (media_expectations.callee_video_expectation_ ==
1507 MediaExpectations::kExpectSomeFrames) {
1508 total_callee_video_frames_expected +=
1509 media_expectations.callee_video_frames_expected_;
1510 }
deadbeef1dcb1642017-03-29 21:08:16 -07001511
Seth Hampson2f0d7022018-02-20 11:54:42 -08001512 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001513 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001514 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001515 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001516 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001517 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001518 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001519 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001520 total_callee_video_frames_expected,
1521 kMaxWaitForFramesMs);
1522 bool expectations_correct =
1523 caller()->audio_frames_received() >=
1524 total_caller_audio_frames_expected &&
1525 caller()->min_video_frames_received_per_track() >=
1526 total_caller_video_frames_expected &&
1527 callee()->audio_frames_received() >=
1528 total_callee_audio_frames_expected &&
1529 callee()->min_video_frames_received_per_track() >=
1530 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001531
Seth Hampson2f0d7022018-02-20 11:54:42 -08001532 // After the combined wait, print out a more detailed message upon
1533 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001534 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001535 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001536 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001537 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001538 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001539 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001540 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001541 total_callee_video_frames_expected);
1542
1543 // We want to make sure nothing unexpected was received.
1544 if (media_expectations.caller_audio_expectation_ ==
1545 MediaExpectations::kExpectNoFrames) {
1546 EXPECT_EQ(caller()->audio_frames_received(),
1547 total_caller_audio_frames_expected);
1548 if (caller()->audio_frames_received() !=
1549 total_caller_audio_frames_expected) {
1550 expectations_correct = false;
1551 }
1552 }
1553 if (media_expectations.caller_video_expectation_ ==
1554 MediaExpectations::kExpectNoFrames) {
1555 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1556 total_caller_video_frames_expected);
1557 if (caller()->min_video_frames_received_per_track() !=
1558 total_caller_video_frames_expected) {
1559 expectations_correct = false;
1560 }
1561 }
1562 if (media_expectations.callee_audio_expectation_ ==
1563 MediaExpectations::kExpectNoFrames) {
1564 EXPECT_EQ(callee()->audio_frames_received(),
1565 total_callee_audio_frames_expected);
1566 if (callee()->audio_frames_received() !=
1567 total_callee_audio_frames_expected) {
1568 expectations_correct = false;
1569 }
1570 }
1571 if (media_expectations.callee_video_expectation_ ==
1572 MediaExpectations::kExpectNoFrames) {
1573 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1574 total_callee_video_frames_expected);
1575 if (callee()->min_video_frames_received_per_track() !=
1576 total_callee_video_frames_expected) {
1577 expectations_correct = false;
1578 }
1579 }
1580 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001581 }
1582
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001583 void TestNegotiatedCipherSuite(
1584 const PeerConnectionFactory::Options& caller_options,
1585 const PeerConnectionFactory::Options& callee_options,
1586 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001587 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1588 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001589 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001590 caller()->AddAudioVideoTracks();
1591 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001592 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001593 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001594 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001595 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001596 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001597 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001598 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1599 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001600 }
1601
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001602 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1603 bool remote_gcm_enabled,
1604 int expected_cipher_suite) {
1605 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001606 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1607 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001608 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001609 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1610 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001611 TestNegotiatedCipherSuite(caller_options, callee_options,
1612 expected_cipher_suite);
1613 }
1614
Seth Hampson2f0d7022018-02-20 11:54:42 -08001615 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001616 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001617
deadbeef1dcb1642017-03-29 21:08:16 -07001618 private:
1619 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001620 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001621 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001622 // |network_thread_| and |worker_thread_| are used by both
1623 // |caller_| and |callee_| so they must be destroyed
1624 // later.
1625 std::unique_ptr<rtc::Thread> network_thread_;
1626 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001627 // The turn servers and turn customizers should be accessed & deleted on the
1628 // network thread to avoid a race with the socket read/write that occurs
1629 // on the network thread.
1630 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1631 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001632 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001633 std::unique_ptr<PeerConnectionWrapper> caller_;
1634 std::unique_ptr<PeerConnectionWrapper> callee_;
1635};
1636
Seth Hampson2f0d7022018-02-20 11:54:42 -08001637class PeerConnectionIntegrationTest
1638 : public PeerConnectionIntegrationBaseTest,
1639 public ::testing::WithParamInterface<SdpSemantics> {
1640 protected:
1641 PeerConnectionIntegrationTest()
1642 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1643};
1644
1645class PeerConnectionIntegrationTestPlanB
1646 : public PeerConnectionIntegrationBaseTest {
1647 protected:
1648 PeerConnectionIntegrationTestPlanB()
1649 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1650};
1651
1652class PeerConnectionIntegrationTestUnifiedPlan
1653 : public PeerConnectionIntegrationBaseTest {
1654 protected:
1655 PeerConnectionIntegrationTestUnifiedPlan()
1656 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1657};
1658
deadbeef1dcb1642017-03-29 21:08:16 -07001659// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1660// includes testing that the callback is invoked if an observer is connected
1661// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001662TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001663 RtpReceiverObserverOnFirstPacketReceived) {
1664 ASSERT_TRUE(CreatePeerConnectionWrappers());
1665 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001666 caller()->AddAudioVideoTracks();
1667 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001668 // Start offer/answer exchange and wait for it to complete.
1669 caller()->CreateAndSetAndSignalOffer();
1670 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1671 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001672 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1673 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001674 // Wait for all "first packet received" callbacks to be fired.
1675 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001676 absl::c_all_of(caller()->rtp_receiver_observers(),
1677 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1678 return o->first_packet_received();
1679 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001680 kMaxWaitForFramesMs);
1681 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001682 absl::c_all_of(callee()->rtp_receiver_observers(),
1683 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1684 return o->first_packet_received();
1685 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001686 kMaxWaitForFramesMs);
1687 // If new observers are set after the first packet was already received, the
1688 // callback should still be invoked.
1689 caller()->ResetRtpReceiverObservers();
1690 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001691 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1692 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001693 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001694 absl::c_all_of(caller()->rtp_receiver_observers(),
1695 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1696 return o->first_packet_received();
1697 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001698 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001699 absl::c_all_of(callee()->rtp_receiver_observers(),
1700 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1701 return o->first_packet_received();
1702 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001703}
1704
1705class DummyDtmfObserver : public DtmfSenderObserverInterface {
1706 public:
1707 DummyDtmfObserver() : completed_(false) {}
1708
1709 // Implements DtmfSenderObserverInterface.
1710 void OnToneChange(const std::string& tone) override {
1711 tones_.push_back(tone);
1712 if (tone.empty()) {
1713 completed_ = true;
1714 }
1715 }
1716
1717 const std::vector<std::string>& tones() const { return tones_; }
1718 bool completed() const { return completed_; }
1719
1720 private:
1721 bool completed_;
1722 std::vector<std::string> tones_;
1723};
1724
1725// Assumes |sender| already has an audio track added and the offer/answer
1726// exchange is done.
1727void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1728 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001729 // We should be able to get a DTMF sender from the local sender.
1730 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1731 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1732 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001733 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001734 dtmf_sender->RegisterObserver(&observer);
1735
1736 // Test the DtmfSender object just created.
1737 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1738 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1739
1740 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1741 std::vector<std::string> tones = {"1", "a", ""};
1742 EXPECT_EQ(tones, observer.tones());
1743 dtmf_sender->UnregisterObserver();
1744 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1745}
1746
1747// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1748// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001749TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001750 ASSERT_TRUE(CreatePeerConnectionWrappers());
1751 ConnectFakeSignaling();
1752 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001753 caller()->AddAudioTrack();
1754 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001755 caller()->CreateAndSetAndSignalOffer();
1756 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001757 // DTLS must finish before the DTMF sender can be used reliably.
1758 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001759 TestDtmfFromSenderToReceiver(caller(), callee());
1760 TestDtmfFromSenderToReceiver(callee(), caller());
1761}
1762
1763// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1764// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001765TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001766 ASSERT_TRUE(CreatePeerConnectionWrappers());
1767 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001768
deadbeef1dcb1642017-03-29 21:08:16 -07001769 // Do normal offer/answer and wait for some frames to be received in each
1770 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001771 caller()->AddAudioVideoTracks();
1772 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001773 caller()->CreateAndSetAndSignalOffer();
1774 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001775 MediaExpectations media_expectations;
1776 media_expectations.ExpectBidirectionalAudioAndVideo();
1777 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001778 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1779 webrtc::kEnumCounterKeyProtocolDtls));
1780 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1781 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001782}
1783
1784// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001785TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001786 PeerConnectionInterface::RTCConfiguration sdes_config;
1787 sdes_config.enable_dtls_srtp.emplace(false);
1788 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1789 ConnectFakeSignaling();
1790
1791 // Do normal offer/answer and wait for some frames to be received in each
1792 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001793 caller()->AddAudioVideoTracks();
1794 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001795 caller()->CreateAndSetAndSignalOffer();
1796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001797 MediaExpectations media_expectations;
1798 media_expectations.ExpectBidirectionalAudioAndVideo();
1799 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001800 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1801 webrtc::kEnumCounterKeyProtocolSdes));
1802 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1803 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001804}
1805
Steve Anton8c0f7a72017-10-03 10:03:10 -07001806// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1807// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001808TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001809 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1810 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1811 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1812 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1813 return pc->GetRemoteAudioSSLCertificate();
1814 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001815 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1816 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1817 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1818 return pc->GetRemoteAudioSSLCertChain();
1819 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001820
1821 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1822 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1823
1824 // Configure each side with a known certificate so they can be compared later.
1825 PeerConnectionInterface::RTCConfiguration caller_config;
1826 caller_config.enable_dtls_srtp.emplace(true);
1827 caller_config.certificates.push_back(caller_cert);
1828 PeerConnectionInterface::RTCConfiguration callee_config;
1829 callee_config.enable_dtls_srtp.emplace(true);
1830 callee_config.certificates.push_back(callee_cert);
1831 ASSERT_TRUE(
1832 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1833 ConnectFakeSignaling();
1834
1835 // When first initialized, there should not be a remote SSL certificate (and
1836 // calling this method should not crash).
1837 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1838 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001839 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1840 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001841
Steve Anton15324772018-01-16 10:26:49 -08001842 caller()->AddAudioTrack();
1843 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001844 caller()->CreateAndSetAndSignalOffer();
1845 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1846 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1847
1848 // Once DTLS has been connected, each side should return the other's SSL
1849 // certificate when calling GetRemoteAudioSSLCertificate.
1850
1851 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1852 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001853 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001854 caller_remote_cert->ToPEMString());
1855
1856 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1857 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001858 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001859 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001860
1861 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1862 ASSERT_TRUE(caller_remote_cert_chain);
1863 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1864 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001865 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001866 remote_cert->ToPEMString());
1867
1868 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1869 ASSERT_TRUE(callee_remote_cert_chain);
1870 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1871 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001872 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001873 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001874}
1875
deadbeef1dcb1642017-03-29 21:08:16 -07001876// This test sets up a call between two parties with a source resolution of
1877// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001878TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001879 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1880 ASSERT_TRUE(CreatePeerConnectionWrappers());
1881 ConnectFakeSignaling();
1882
Niels Möller5c7efe72018-05-11 10:34:46 +02001883 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1884 webrtc::FakePeriodicVideoSource::Config config;
1885 config.width = 1280;
1886 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001887 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001888 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1889 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001890
1891 // Do normal offer/answer and wait for at least one frame to be received in
1892 // each direction.
1893 caller()->CreateAndSetAndSignalOffer();
1894 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1895 callee()->min_video_frames_received_per_track() > 0,
1896 kMaxWaitForFramesMs);
1897
1898 // Check rendered aspect ratio.
1899 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1900 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1901 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1902 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1903}
1904
1905// This test sets up an one-way call, with media only from caller to
1906// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001907TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001908 ASSERT_TRUE(CreatePeerConnectionWrappers());
1909 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001910 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001911 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912 MediaExpectations media_expectations;
1913 media_expectations.CalleeExpectsSomeAudioAndVideo();
1914 media_expectations.CallerExpectsNoAudio();
1915 media_expectations.CallerExpectsNoVideo();
1916 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001917}
1918
1919// This test sets up a audio call initially, with the callee rejecting video
1920// initially. Then later the callee decides to upgrade to audio/video, and
1921// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001922TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001923 ASSERT_TRUE(CreatePeerConnectionWrappers());
1924 ConnectFakeSignaling();
1925 // Initially, offer an audio/video stream from the caller, but refuse to
1926 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001927 caller()->AddAudioVideoTracks();
1928 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001929 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1930 PeerConnectionInterface::RTCOfferAnswerOptions options;
1931 options.offer_to_receive_video = 0;
1932 callee()->SetOfferAnswerOptions(options);
1933 } else {
1934 callee()->SetRemoteOfferHandler([this] {
1935 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1936 });
1937 }
deadbeef1dcb1642017-03-29 21:08:16 -07001938 // Do offer/answer and make sure audio is still received end-to-end.
1939 caller()->CreateAndSetAndSignalOffer();
1940 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001941 {
1942 MediaExpectations media_expectations;
1943 media_expectations.ExpectBidirectionalAudio();
1944 media_expectations.ExpectNoVideo();
1945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1946 }
deadbeef1dcb1642017-03-29 21:08:16 -07001947 // Sanity check that the callee's description has a rejected video section.
1948 ASSERT_NE(nullptr, callee()->pc()->local_description());
1949 const ContentInfo* callee_video_content =
1950 GetFirstVideoContent(callee()->pc()->local_description()->description());
1951 ASSERT_NE(nullptr, callee_video_content);
1952 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001953
deadbeef1dcb1642017-03-29 21:08:16 -07001954 // Now negotiate with video and ensure negotiation succeeds, with video
1955 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001956 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001957 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1958 PeerConnectionInterface::RTCOfferAnswerOptions options;
1959 options.offer_to_receive_video = 1;
1960 callee()->SetOfferAnswerOptions(options);
1961 } else {
1962 callee()->SetRemoteOfferHandler(nullptr);
1963 caller()->SetRemoteOfferHandler([this] {
1964 // The caller creates a new transceiver to receive video on when receiving
1965 // the offer, but by default it is send only.
1966 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001967 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001968 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1969 transceivers[2]->receiver()->media_type());
1970 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1971 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1972 });
1973 }
deadbeef1dcb1642017-03-29 21:08:16 -07001974 callee()->CreateAndSetAndSignalOffer();
1975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001976 {
1977 // Expect additional audio frames to be received after the upgrade.
1978 MediaExpectations media_expectations;
1979 media_expectations.ExpectBidirectionalAudioAndVideo();
1980 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1981 }
deadbeef1dcb1642017-03-29 21:08:16 -07001982}
1983
deadbeef4389b4d2017-09-07 09:07:36 -07001984// Simpler than the above test; just add an audio track to an established
1985// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001986TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001987 ASSERT_TRUE(CreatePeerConnectionWrappers());
1988 ConnectFakeSignaling();
1989 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001990 caller()->AddVideoTrack();
1991 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001992 caller()->CreateAndSetAndSignalOffer();
1993 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1994 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001995 caller()->AddAudioTrack();
1996 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001997 caller()->CreateAndSetAndSignalOffer();
1998 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1999 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002000 MediaExpectations media_expectations;
2001 media_expectations.ExpectBidirectionalAudioAndVideo();
2002 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002003}
2004
deadbeef1dcb1642017-03-29 21:08:16 -07002005// This test sets up a call that's transferred to a new caller with a different
2006// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002007TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002008 ASSERT_TRUE(CreatePeerConnectionWrappers());
2009 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002010 caller()->AddAudioVideoTracks();
2011 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002012 caller()->CreateAndSetAndSignalOffer();
2013 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2014
2015 // Keep the original peer around which will still send packets to the
2016 // receiving client. These SRTP packets will be dropped.
2017 std::unique_ptr<PeerConnectionWrapper> original_peer(
2018 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002019 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002020 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2021 // directly above.
2022 original_peer->pc()->Close();
2023
2024 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002025 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002026 caller()->CreateAndSetAndSignalOffer();
2027 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2028 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002029 MediaExpectations media_expectations;
2030 media_expectations.ExpectBidirectionalAudioAndVideo();
2031 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002032}
2033
2034// This test sets up a call that's transferred to a new callee with a different
2035// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002036TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002037 ASSERT_TRUE(CreatePeerConnectionWrappers());
2038 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002039 caller()->AddAudioVideoTracks();
2040 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002041 caller()->CreateAndSetAndSignalOffer();
2042 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2043
2044 // Keep the original peer around which will still send packets to the
2045 // receiving client. These SRTP packets will be dropped.
2046 std::unique_ptr<PeerConnectionWrapper> original_peer(
2047 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002048 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002049 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2050 // directly above.
2051 original_peer->pc()->Close();
2052
2053 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002054 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002055 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2056 caller()->CreateAndSetAndSignalOffer();
2057 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2058 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002059 MediaExpectations media_expectations;
2060 media_expectations.ExpectBidirectionalAudioAndVideo();
2061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002062}
2063
2064// This test sets up a non-bundled call and negotiates bundling at the same
2065// time as starting an ICE restart. When bundling is in effect in the restart,
2066// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002067TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002068 ASSERT_TRUE(CreatePeerConnectionWrappers());
2069 ConnectFakeSignaling();
2070
Steve Anton15324772018-01-16 10:26:49 -08002071 caller()->AddAudioVideoTracks();
2072 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002073 // Remove the bundle group from the SDP received by the callee.
2074 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2075 desc->RemoveGroupByName("BUNDLE");
2076 });
2077 caller()->CreateAndSetAndSignalOffer();
2078 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079 {
2080 MediaExpectations media_expectations;
2081 media_expectations.ExpectBidirectionalAudioAndVideo();
2082 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2083 }
deadbeef1dcb1642017-03-29 21:08:16 -07002084 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2085 callee()->SetReceivedSdpMunger(nullptr);
2086 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2087 caller()->CreateAndSetAndSignalOffer();
2088 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2089
2090 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002091 {
2092 MediaExpectations media_expectations;
2093 media_expectations.ExpectBidirectionalAudioAndVideo();
2094 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2095 }
deadbeef1dcb1642017-03-29 21:08:16 -07002096}
2097
2098// Test CVO (Coordination of Video Orientation). If a video source is rotated
2099// and both peers support the CVO RTP header extension, the actual video frames
2100// don't need to be encoded in different resolutions, since the rotation is
2101// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002102TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002103 ASSERT_TRUE(CreatePeerConnectionWrappers());
2104 ConnectFakeSignaling();
2105 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002106 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002107 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002108 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002109 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2110
2111 // Wait for video frames to be received by both sides.
2112 caller()->CreateAndSetAndSignalOffer();
2113 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2114 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2115 callee()->min_video_frames_received_per_track() > 0,
2116 kMaxWaitForFramesMs);
2117
2118 // Ensure that the aspect ratio is unmodified.
2119 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2120 // not just assumed.
2121 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2122 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2123 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2124 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2125 // Ensure that the CVO bits were surfaced to the renderer.
2126 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2127 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2128}
2129
2130// Test that when the CVO extension isn't supported, video is rotated the
2131// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002132TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002133 ASSERT_TRUE(CreatePeerConnectionWrappers());
2134 ConnectFakeSignaling();
2135 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002136 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002137 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002138 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002139 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2140
2141 // Remove the CVO extension from the offered SDP.
2142 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2143 cricket::VideoContentDescription* video =
2144 GetFirstVideoContentDescription(desc);
2145 video->ClearRtpHeaderExtensions();
2146 });
2147 // Wait for video frames to be received by both sides.
2148 caller()->CreateAndSetAndSignalOffer();
2149 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2150 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2151 callee()->min_video_frames_received_per_track() > 0,
2152 kMaxWaitForFramesMs);
2153
2154 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2155 // rotation.
2156 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2157 // not just assumed.
2158 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2159 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2160 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2161 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2162 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2163 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2164 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2165}
2166
deadbeef1dcb1642017-03-29 21:08:16 -07002167// Test that if the answerer rejects the audio m= section, no audio is sent or
2168// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002169TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002170 ASSERT_TRUE(CreatePeerConnectionWrappers());
2171 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002172 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002173 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2174 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2175 // it will reject the audio m= section completely.
2176 PeerConnectionInterface::RTCOfferAnswerOptions options;
2177 options.offer_to_receive_audio = 0;
2178 callee()->SetOfferAnswerOptions(options);
2179 } else {
2180 // Stopping the audio RtpTransceiver will cause the media section to be
2181 // rejected in the answer.
2182 callee()->SetRemoteOfferHandler([this] {
2183 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2184 });
2185 }
Steve Anton15324772018-01-16 10:26:49 -08002186 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002187 // Do offer/answer and wait for successful end-to-end video frames.
2188 caller()->CreateAndSetAndSignalOffer();
2189 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002190 MediaExpectations media_expectations;
2191 media_expectations.ExpectBidirectionalVideo();
2192 media_expectations.ExpectNoAudio();
2193 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2194
deadbeef1dcb1642017-03-29 21:08:16 -07002195 // Sanity check that the callee's description has a rejected audio section.
2196 ASSERT_NE(nullptr, callee()->pc()->local_description());
2197 const ContentInfo* callee_audio_content =
2198 GetFirstAudioContent(callee()->pc()->local_description()->description());
2199 ASSERT_NE(nullptr, callee_audio_content);
2200 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002201 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2202 // The caller's transceiver should have stopped after receiving the answer.
2203 EXPECT_TRUE(caller()
2204 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2205 ->stopped());
2206 }
deadbeef1dcb1642017-03-29 21:08:16 -07002207}
2208
2209// Test that if the answerer rejects the video m= section, no video is sent or
2210// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002211TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002212 ASSERT_TRUE(CreatePeerConnectionWrappers());
2213 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002214 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002215 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2216 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2217 // it will reject the video m= section completely.
2218 PeerConnectionInterface::RTCOfferAnswerOptions options;
2219 options.offer_to_receive_video = 0;
2220 callee()->SetOfferAnswerOptions(options);
2221 } else {
2222 // Stopping the video RtpTransceiver will cause the media section to be
2223 // rejected in the answer.
2224 callee()->SetRemoteOfferHandler([this] {
2225 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2226 });
2227 }
Steve Anton15324772018-01-16 10:26:49 -08002228 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002229 // Do offer/answer and wait for successful end-to-end audio frames.
2230 caller()->CreateAndSetAndSignalOffer();
2231 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002232 MediaExpectations media_expectations;
2233 media_expectations.ExpectBidirectionalAudio();
2234 media_expectations.ExpectNoVideo();
2235 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2236
deadbeef1dcb1642017-03-29 21:08:16 -07002237 // Sanity check that the callee's description has a rejected video section.
2238 ASSERT_NE(nullptr, callee()->pc()->local_description());
2239 const ContentInfo* callee_video_content =
2240 GetFirstVideoContent(callee()->pc()->local_description()->description());
2241 ASSERT_NE(nullptr, callee_video_content);
2242 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002243 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2244 // The caller's transceiver should have stopped after receiving the answer.
2245 EXPECT_TRUE(caller()
2246 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2247 ->stopped());
2248 }
deadbeef1dcb1642017-03-29 21:08:16 -07002249}
2250
2251// Test that if the answerer rejects both audio and video m= sections, nothing
2252// bad happens.
2253// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2254// test anything but the fact that negotiation succeeds, which doesn't mean
2255// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002256TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002257 ASSERT_TRUE(CreatePeerConnectionWrappers());
2258 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002259 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2261 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2262 // will reject both audio and video m= sections.
2263 PeerConnectionInterface::RTCOfferAnswerOptions options;
2264 options.offer_to_receive_audio = 0;
2265 options.offer_to_receive_video = 0;
2266 callee()->SetOfferAnswerOptions(options);
2267 } else {
2268 callee()->SetRemoteOfferHandler([this] {
2269 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002270 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002271 transceiver->Stop();
2272 }
2273 });
2274 }
deadbeef1dcb1642017-03-29 21:08:16 -07002275 // Do offer/answer and wait for stable signaling state.
2276 caller()->CreateAndSetAndSignalOffer();
2277 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278
deadbeef1dcb1642017-03-29 21:08:16 -07002279 // Sanity check that the callee's description has rejected m= sections.
2280 ASSERT_NE(nullptr, callee()->pc()->local_description());
2281 const ContentInfo* callee_audio_content =
2282 GetFirstAudioContent(callee()->pc()->local_description()->description());
2283 ASSERT_NE(nullptr, callee_audio_content);
2284 EXPECT_TRUE(callee_audio_content->rejected);
2285 const ContentInfo* callee_video_content =
2286 GetFirstVideoContent(callee()->pc()->local_description()->description());
2287 ASSERT_NE(nullptr, callee_video_content);
2288 EXPECT_TRUE(callee_video_content->rejected);
2289}
2290
2291// This test sets up an audio and video call between two parties. After the
2292// call runs for a while, the caller sends an updated offer with video being
2293// rejected. Once the re-negotiation is done, the video flow should stop and
2294// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002295TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002296 ASSERT_TRUE(CreatePeerConnectionWrappers());
2297 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002298 caller()->AddAudioVideoTracks();
2299 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002300 caller()->CreateAndSetAndSignalOffer();
2301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002302 {
2303 MediaExpectations media_expectations;
2304 media_expectations.ExpectBidirectionalAudioAndVideo();
2305 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2306 }
deadbeef1dcb1642017-03-29 21:08:16 -07002307 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002308 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2309 caller()->SetGeneratedSdpMunger(
2310 [](cricket::SessionDescription* description) {
2311 for (cricket::ContentInfo& content : description->contents()) {
2312 if (cricket::IsVideoContent(&content)) {
2313 content.rejected = true;
2314 }
2315 }
2316 });
2317 } else {
2318 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2319 }
deadbeef1dcb1642017-03-29 21:08:16 -07002320 caller()->CreateAndSetAndSignalOffer();
2321 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2322
2323 // Sanity check that the caller's description has a rejected video section.
2324 ASSERT_NE(nullptr, caller()->pc()->local_description());
2325 const ContentInfo* caller_video_content =
2326 GetFirstVideoContent(caller()->pc()->local_description()->description());
2327 ASSERT_NE(nullptr, caller_video_content);
2328 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002329 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002330 {
2331 MediaExpectations media_expectations;
2332 media_expectations.ExpectBidirectionalAudio();
2333 media_expectations.ExpectNoVideo();
2334 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2335 }
deadbeef1dcb1642017-03-29 21:08:16 -07002336}
2337
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002338// Do one offer/answer with audio, another that disables it (rejecting the m=
2339// section), and another that re-enables it. Regression test for:
2340// bugs.webrtc.org/6023
2341TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2342 ASSERT_TRUE(CreatePeerConnectionWrappers());
2343 ConnectFakeSignaling();
2344
2345 // Add audio track, do normal offer/answer.
2346 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2347 caller()->CreateLocalAudioTrack();
2348 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2349 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2350 caller()->CreateAndSetAndSignalOffer();
2351 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2352
2353 // Remove audio track, and set offer_to_receive_audio to false to cause the
2354 // m= section to be completely disabled, not just "recvonly".
2355 caller()->pc()->RemoveTrack(sender);
2356 PeerConnectionInterface::RTCOfferAnswerOptions options;
2357 options.offer_to_receive_audio = 0;
2358 caller()->SetOfferAnswerOptions(options);
2359 caller()->CreateAndSetAndSignalOffer();
2360 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2361
2362 // Add the audio track again, expecting negotiation to succeed and frames to
2363 // flow.
2364 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2365 options.offer_to_receive_audio = 1;
2366 caller()->SetOfferAnswerOptions(options);
2367 caller()->CreateAndSetAndSignalOffer();
2368 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2369
2370 MediaExpectations media_expectations;
2371 media_expectations.CalleeExpectsSomeAudio();
2372 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2373}
2374
deadbeef1dcb1642017-03-29 21:08:16 -07002375// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2376// is needed to support legacy endpoints.
2377// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2378// add a test for an end-to-end test without MID signaling either (basically,
2379// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002380TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002381 ASSERT_TRUE(CreatePeerConnectionWrappers());
2382 ConnectFakeSignaling();
2383 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002384 caller()->AddAudioVideoTracks();
2385 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002386 // Remove SSRCs and MSIDs from the received offer SDP.
2387 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002388 caller()->CreateAndSetAndSignalOffer();
2389 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002390 MediaExpectations media_expectations;
2391 media_expectations.ExpectBidirectionalAudioAndVideo();
2392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002393}
2394
Seth Hampson5897a6e2018-04-03 11:16:33 -07002395// Basic end-to-end test, without SSRC signaling. This means that the track
2396// was created properly and frames are delivered when the MSIDs are communicated
2397// with a=msid lines and no a=ssrc lines.
2398TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2399 EndToEndCallWithoutSsrcSignaling) {
2400 const char kStreamId[] = "streamId";
2401 ASSERT_TRUE(CreatePeerConnectionWrappers());
2402 ConnectFakeSignaling();
2403 // Add just audio tracks.
2404 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2405 callee()->AddAudioTrack();
2406
2407 // Remove SSRCs from the received offer SDP.
2408 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2409 caller()->CreateAndSetAndSignalOffer();
2410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2411 MediaExpectations media_expectations;
2412 media_expectations.ExpectBidirectionalAudio();
2413 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2414}
2415
Steve Antondf527fd2018-04-27 15:52:03 -07002416// Tests that video flows between multiple video tracks when SSRCs are not
2417// signaled. This exercises the MID RTP header extension which is needed to
2418// demux the incoming video tracks.
2419TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2420 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2421 ASSERT_TRUE(CreatePeerConnectionWrappers());
2422 ConnectFakeSignaling();
2423 caller()->AddVideoTrack();
2424 caller()->AddVideoTrack();
2425 callee()->AddVideoTrack();
2426 callee()->AddVideoTrack();
2427
2428 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2429 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2430 caller()->CreateAndSetAndSignalOffer();
2431 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2432 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2433 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2434
2435 // Expect video to be received in both directions on both tracks.
2436 MediaExpectations media_expectations;
2437 media_expectations.ExpectBidirectionalVideo();
2438 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2439}
2440
Henrik Boström5b147782018-12-04 11:25:05 +01002441TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2442 ASSERT_TRUE(CreatePeerConnectionWrappers());
2443 ConnectFakeSignaling();
2444 caller()->AddAudioTrack();
2445 caller()->AddVideoTrack();
2446 caller()->CreateAndSetAndSignalOffer();
2447 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2448 auto callee_receivers = callee()->pc()->GetReceivers();
2449 ASSERT_EQ(2u, callee_receivers.size());
2450 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2451 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2452}
2453
2454TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2455 ASSERT_TRUE(CreatePeerConnectionWrappers());
2456 ConnectFakeSignaling();
2457 caller()->AddAudioTrack();
2458 caller()->AddVideoTrack();
2459 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2460 caller()->CreateAndSetAndSignalOffer();
2461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2462 auto callee_receivers = callee()->pc()->GetReceivers();
2463 ASSERT_EQ(2u, callee_receivers.size());
2464 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2465 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2466 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2467 callee_receivers[1]->stream_ids()[0]);
2468 EXPECT_EQ(callee_receivers[0]->streams()[0],
2469 callee_receivers[1]->streams()[0]);
2470}
2471
deadbeef1dcb1642017-03-29 21:08:16 -07002472// Test that if two video tracks are sent (from caller to callee, in this test),
2473// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002474TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002475 ASSERT_TRUE(CreatePeerConnectionWrappers());
2476 ConnectFakeSignaling();
2477 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002478 caller()->AddAudioVideoTracks();
2479 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002480 caller()->CreateAndSetAndSignalOffer();
2481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002482 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002483
2484 MediaExpectations media_expectations;
2485 media_expectations.CalleeExpectsSomeAudioAndVideo();
2486 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002487}
2488
2489static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2490 bool first = true;
2491 for (cricket::ContentInfo& content : desc->contents()) {
2492 if (first) {
2493 first = false;
2494 continue;
2495 }
2496 content.bundle_only = true;
2497 }
2498 first = true;
2499 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2500 if (first) {
2501 first = false;
2502 continue;
2503 }
2504 transport.description.ice_ufrag.clear();
2505 transport.description.ice_pwd.clear();
2506 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2507 transport.description.identity_fingerprint.reset(nullptr);
2508 }
2509}
2510
2511// Test that if applying a true "max bundle" offer, which uses ports of 0,
2512// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2513// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2514// successfully and media flows.
2515// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2516// TODO(deadbeef): Won't need this test once we start generating actual
2517// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002518TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002519 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2520 ASSERT_TRUE(CreatePeerConnectionWrappers());
2521 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002522 caller()->AddAudioVideoTracks();
2523 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002524 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2525 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2526 // but the first m= section.
2527 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2528 caller()->CreateAndSetAndSignalOffer();
2529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002530 MediaExpectations media_expectations;
2531 media_expectations.ExpectBidirectionalAudioAndVideo();
2532 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002533}
2534
2535// Test that we can receive the audio output level from a remote audio track.
2536// TODO(deadbeef): Use a fake audio source and verify that the output level is
2537// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002538TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002539 ASSERT_TRUE(CreatePeerConnectionWrappers());
2540 ConnectFakeSignaling();
2541 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002542 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002543 caller()->CreateAndSetAndSignalOffer();
2544 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2545
2546 // Get the audio output level stats. Note that the level is not available
2547 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002548 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002549 kMaxWaitForFramesMs);
2550}
2551
2552// Test that an audio input level is reported.
2553// TODO(deadbeef): Use a fake audio source and verify that the input level is
2554// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002555TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002556 ASSERT_TRUE(CreatePeerConnectionWrappers());
2557 ConnectFakeSignaling();
2558 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002559 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002560 caller()->CreateAndSetAndSignalOffer();
2561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2562
2563 // Get the audio input level stats. The level should be available very
2564 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002565 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002566 kMaxWaitForStatsMs);
2567}
2568
2569// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002570TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002571 ASSERT_TRUE(CreatePeerConnectionWrappers());
2572 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002573 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002574 // Do offer/answer, wait for the callee to receive some frames.
2575 caller()->CreateAndSetAndSignalOffer();
2576 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002577
2578 MediaExpectations media_expectations;
2579 media_expectations.CalleeExpectsSomeAudioAndVideo();
2580 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002581
2582 // Get a handle to the remote tracks created, so they can be used as GetStats
2583 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002584 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002585 // We received frames, so we definitely should have nonzero "received bytes"
2586 // stats at this point.
2587 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2588 0);
2589 }
deadbeef1dcb1642017-03-29 21:08:16 -07002590}
2591
2592// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002593TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002594 ASSERT_TRUE(CreatePeerConnectionWrappers());
2595 ConnectFakeSignaling();
2596 auto audio_track = caller()->CreateLocalAudioTrack();
2597 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002598 caller()->AddTrack(audio_track);
2599 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002600 // Do offer/answer, wait for the callee to receive some frames.
2601 caller()->CreateAndSetAndSignalOffer();
2602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002603 MediaExpectations media_expectations;
2604 media_expectations.CalleeExpectsSomeAudioAndVideo();
2605 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002606
2607 // The callee received frames, so we definitely should have nonzero "sent
2608 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002609 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2610 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2611}
2612
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002613// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002614TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002615 ASSERT_TRUE(CreatePeerConnectionWrappers());
2616 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002617 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002618
Steve Anton15324772018-01-16 10:26:49 -08002619 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002620
2621 // Do offer/answer, wait for the callee to receive some frames.
2622 caller()->CreateAndSetAndSignalOffer();
2623 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2624
2625 // Get the remote audio track created on the receiver, so they can be used as
2626 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002627 auto receivers = callee()->pc()->GetReceivers();
2628 ASSERT_EQ(1u, receivers.size());
2629 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002630
2631 // Get the audio output level stats. Note that the level is not available
2632 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002633 EXPECT_TRUE_WAIT(
2634 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2635 0,
2636 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002637}
2638
Steve Antona41959e2018-11-28 11:15:33 -08002639// Test that the track ID is associated with all local and remote SSRC stats
2640// using the old GetStats() and more than 1 audio and more than 1 video track.
2641// This is a regression test for crbug.com/906988
2642TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2643 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2644 ASSERT_TRUE(CreatePeerConnectionWrappers());
2645 ConnectFakeSignaling();
2646 auto audio_sender_1 = caller()->AddAudioTrack();
2647 auto video_sender_1 = caller()->AddVideoTrack();
2648 auto audio_sender_2 = caller()->AddAudioTrack();
2649 auto video_sender_2 = caller()->AddVideoTrack();
2650 caller()->CreateAndSetAndSignalOffer();
2651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2652
2653 MediaExpectations media_expectations;
2654 media_expectations.CalleeExpectsSomeAudioAndVideo();
2655 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2656
2657 std::vector<std::string> track_ids = {
2658 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2659 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2660
2661 auto caller_stats = caller()->OldGetStats();
2662 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2663 auto callee_stats = callee()->OldGetStats();
2664 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2665}
2666
Steve Antonffa6ce42018-11-30 09:26:08 -08002667// Test that the new GetStats() returns stats for all outgoing/incoming streams
2668// with the correct track IDs if there are more than one audio and more than one
2669// video senders/receivers.
2670TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2671 ASSERT_TRUE(CreatePeerConnectionWrappers());
2672 ConnectFakeSignaling();
2673 auto audio_sender_1 = caller()->AddAudioTrack();
2674 auto video_sender_1 = caller()->AddVideoTrack();
2675 auto audio_sender_2 = caller()->AddAudioTrack();
2676 auto video_sender_2 = caller()->AddVideoTrack();
2677 caller()->CreateAndSetAndSignalOffer();
2678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2679
2680 MediaExpectations media_expectations;
2681 media_expectations.CalleeExpectsSomeAudioAndVideo();
2682 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2683
2684 std::vector<std::string> track_ids = {
2685 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2686 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2687
2688 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2689 caller()->NewGetStats();
2690 ASSERT_TRUE(caller_report);
2691 auto outbound_stream_stats =
2692 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2693 ASSERT_EQ(4u, outbound_stream_stats.size());
2694 std::vector<std::string> outbound_track_ids;
2695 for (const auto& stat : outbound_stream_stats) {
2696 ASSERT_TRUE(stat->bytes_sent.is_defined());
2697 EXPECT_LT(0u, *stat->bytes_sent);
2698 ASSERT_TRUE(stat->track_id.is_defined());
2699 const auto* track_stat =
2700 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2701 ASSERT_TRUE(track_stat);
2702 outbound_track_ids.push_back(*track_stat->track_identifier);
2703 }
2704 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2705
2706 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2707 callee()->NewGetStats();
2708 ASSERT_TRUE(callee_report);
2709 auto inbound_stream_stats =
2710 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2711 ASSERT_EQ(4u, inbound_stream_stats.size());
2712 std::vector<std::string> inbound_track_ids;
2713 for (const auto& stat : inbound_stream_stats) {
2714 ASSERT_TRUE(stat->bytes_received.is_defined());
2715 EXPECT_LT(0u, *stat->bytes_received);
2716 ASSERT_TRUE(stat->track_id.is_defined());
2717 const auto* track_stat =
2718 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2719 ASSERT_TRUE(track_stat);
2720 inbound_track_ids.push_back(*track_stat->track_identifier);
2721 }
2722 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2723}
2724
2725// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002726// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2727// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002728TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002729 GetStatsForUnsignaledStreamWithNewStatsApi) {
2730 ASSERT_TRUE(CreatePeerConnectionWrappers());
2731 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002732 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002733 // Remove SSRCs and MSIDs from the received offer SDP.
2734 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2735 caller()->CreateAndSetAndSignalOffer();
2736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002737 MediaExpectations media_expectations;
2738 media_expectations.CalleeExpectsSomeAudio(1);
2739 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002740
2741 // We received a frame, so we should have nonzero "bytes received" stats for
2742 // the unsignaled stream, if stats are working for it.
2743 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2744 callee()->NewGetStats();
2745 ASSERT_NE(nullptr, report);
2746 auto inbound_stream_stats =
2747 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2748 ASSERT_EQ(1U, inbound_stream_stats.size());
2749 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2750 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002751 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2752}
2753
Taylor Brandstettera4653442018-06-19 09:44:26 -07002754// Same as above but for the legacy stats implementation.
2755TEST_P(PeerConnectionIntegrationTest,
2756 GetStatsForUnsignaledStreamWithOldStatsApi) {
2757 ASSERT_TRUE(CreatePeerConnectionWrappers());
2758 ConnectFakeSignaling();
2759 caller()->AddAudioTrack();
2760 // Remove SSRCs and MSIDs from the received offer SDP.
2761 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2762 caller()->CreateAndSetAndSignalOffer();
2763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2764
2765 // Note that, since the old stats implementation associates SSRCs with tracks
2766 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2767 // associated track ID. So we can't use the track "selector" argument.
2768 //
2769 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2770 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002771 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002772 kDefaultTimeout);
2773}
2774
zhihuangf8164932017-05-19 13:09:47 -07002775// Test that we can successfully get the media related stats (audio level
2776// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002777TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002778 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2779 ASSERT_TRUE(CreatePeerConnectionWrappers());
2780 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002781 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002782 // Remove SSRCs and MSIDs from the received offer SDP.
2783 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2784 caller()->CreateAndSetAndSignalOffer();
2785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002786 MediaExpectations media_expectations;
2787 media_expectations.CalleeExpectsSomeAudio(1);
2788 media_expectations.CalleeExpectsSomeVideo(1);
2789 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002790
2791 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2792 callee()->NewGetStats();
2793 ASSERT_NE(nullptr, report);
2794
2795 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2796 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2797 ASSERT_GE(audio_index, 0);
2798 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002799}
2800
deadbeef4e2deab2017-09-20 13:56:21 -07002801// Helper for test below.
2802void ModifySsrcs(cricket::SessionDescription* desc) {
2803 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002804 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002805 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002806 for (uint32_t& ssrc : stream.ssrcs) {
2807 ssrc = rtc::CreateRandomId();
2808 }
2809 }
2810 }
2811}
2812
2813// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2814// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2815// This should result in two "RTCInboundRTPStreamStats", but only one
2816// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2817// being reset to 0 once the SSRC change occurs.
2818//
2819// Regression test for this bug:
2820// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2821//
2822// The bug causes the track stats to only represent one of the two streams:
2823// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2824// that the track stat counters would reset to 0 when the new stream is
2825// received, and a 50% chance that they'll stop updating (while
2826// "concealed_samples" continues increasing, due to silence being generated for
2827// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002828TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002829 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002830 ASSERT_TRUE(CreatePeerConnectionWrappers());
2831 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002832 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002833 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2834 // that doesn't signal SSRCs (from the callee's perspective).
2835 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2836 caller()->CreateAndSetAndSignalOffer();
2837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2838 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002839 {
2840 MediaExpectations media_expectations;
2841 media_expectations.CalleeExpectsSomeAudio(50);
2842 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2843 }
deadbeef4e2deab2017-09-20 13:56:21 -07002844 // Some audio frames were received, so we should have nonzero "samples
2845 // received" for the track.
2846 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2847 callee()->NewGetStats();
2848 ASSERT_NE(nullptr, report);
2849 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2850 ASSERT_EQ(1U, track_stats.size());
2851 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2852 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2853 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2854
2855 // Create a new offer and munge it to cause the caller to use a new SSRC.
2856 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2857 caller()->CreateAndSetAndSignalOffer();
2858 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2859 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2860 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002861 {
2862 MediaExpectations media_expectations;
2863 media_expectations.CalleeExpectsSomeAudio(25);
2864 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2865 }
deadbeef4e2deab2017-09-20 13:56:21 -07002866
2867 report = callee()->NewGetStats();
2868 ASSERT_NE(nullptr, report);
2869 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2870 ASSERT_EQ(1U, track_stats.size());
2871 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2872 // The "total samples received" stat should only be greater than it was
2873 // before.
2874 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2875 // Right now, the new SSRC will cause the counters to reset to 0.
2876 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2877
2878 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002879 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002880 // good sign that we're seeing stats from the old stream that's no longer
2881 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002882 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002883 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2884 EXPECT_LT(*track_stats[0]->concealed_samples,
2885 *track_stats[0]->total_samples_received *
2886 kAcceptableConcealedSamplesPercentage);
2887
2888 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2889 // sanity check that the SSRC really changed.
2890 // TODO(deadbeef): This isn't working right now, because we're not returning
2891 // *any* stats for the inactive stream. Uncomment when the bug is completely
2892 // fixed.
2893 // auto inbound_stream_stats =
2894 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2895 // ASSERT_EQ(2U, inbound_stream_stats.size());
2896}
2897
deadbeef1dcb1642017-03-29 21:08:16 -07002898// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002900 PeerConnectionFactory::Options dtls_10_options;
2901 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2902 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2903 dtls_10_options));
2904 ConnectFakeSignaling();
2905 // Do normal offer/answer and wait for some frames to be received in each
2906 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002907 caller()->AddAudioVideoTracks();
2908 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002909 caller()->CreateAndSetAndSignalOffer();
2910 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002911 MediaExpectations media_expectations;
2912 media_expectations.ExpectBidirectionalAudioAndVideo();
2913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002914}
2915
2916// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002917TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002918 PeerConnectionFactory::Options dtls_10_options;
2919 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2920 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2921 dtls_10_options));
2922 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002923 caller()->AddAudioVideoTracks();
2924 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002925 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002926 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002927 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002928 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002929 kDefaultTimeout);
2930 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002931 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002932 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002933 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002934 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2935 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002936}
2937
2938// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002939TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002940 PeerConnectionFactory::Options dtls_12_options;
2941 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2942 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2943 dtls_12_options));
2944 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002945 caller()->AddAudioVideoTracks();
2946 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002947 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002948 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002949 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002950 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002951 kDefaultTimeout);
2952 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002953 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002954 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002955 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002956 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2957 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002958}
2959
2960// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2961// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002963 PeerConnectionFactory::Options caller_options;
2964 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2965 PeerConnectionFactory::Options callee_options;
2966 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2967 ASSERT_TRUE(
2968 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2969 ConnectFakeSignaling();
2970 // Do normal offer/answer and wait for some frames to be received in each
2971 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002972 caller()->AddAudioVideoTracks();
2973 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002974 caller()->CreateAndSetAndSignalOffer();
2975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002976 MediaExpectations media_expectations;
2977 media_expectations.ExpectBidirectionalAudioAndVideo();
2978 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002979}
2980
2981// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2982// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002983TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002984 PeerConnectionFactory::Options caller_options;
2985 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2986 PeerConnectionFactory::Options callee_options;
2987 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2988 ASSERT_TRUE(
2989 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2990 ConnectFakeSignaling();
2991 // Do normal offer/answer and wait for some frames to be received in each
2992 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002993 caller()->AddAudioVideoTracks();
2994 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002995 caller()->CreateAndSetAndSignalOffer();
2996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002997 MediaExpectations media_expectations;
2998 media_expectations.ExpectBidirectionalAudioAndVideo();
2999 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003000}
3001
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003002// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3003// works as expected; the cipher should only be used if enabled by both sides.
3004TEST_P(PeerConnectionIntegrationTest,
3005 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3006 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003007 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003008 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003009 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3010 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003011 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3012 TestNegotiatedCipherSuite(caller_options, callee_options,
3013 expected_cipher_suite);
3014}
3015
3016TEST_P(PeerConnectionIntegrationTest,
3017 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3018 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003019 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3020 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003021 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003022 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003023 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3024 TestNegotiatedCipherSuite(caller_options, callee_options,
3025 expected_cipher_suite);
3026}
3027
3028TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3029 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003030 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003031 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003032 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003033 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3034 TestNegotiatedCipherSuite(caller_options, callee_options,
3035 expected_cipher_suite);
3036}
3037
deadbeef1dcb1642017-03-29 21:08:16 -07003038// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003039TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003040 bool local_gcm_enabled = false;
3041 bool remote_gcm_enabled = false;
3042 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3043 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3044 expected_cipher_suite);
3045}
3046
3047// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003048TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003049 bool local_gcm_enabled = true;
3050 bool remote_gcm_enabled = true;
3051 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3052 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3053 expected_cipher_suite);
3054}
3055
3056// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003057TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003058 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3059 bool local_gcm_enabled = true;
3060 bool remote_gcm_enabled = false;
3061 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3062 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3063 expected_cipher_suite);
3064}
3065
3066// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003067TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003068 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3069 bool local_gcm_enabled = false;
3070 bool remote_gcm_enabled = true;
3071 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3072 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3073 expected_cipher_suite);
3074}
3075
deadbeef7914b8c2017-04-21 03:23:33 -07003076// Verify that media can be transmitted end-to-end when GCM crypto suites are
3077// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3078// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3079// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003080TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003081 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003082 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003083 ASSERT_TRUE(
3084 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3085 ConnectFakeSignaling();
3086 // Do normal offer/answer and wait for some frames to be received in each
3087 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003088 caller()->AddAudioVideoTracks();
3089 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003090 caller()->CreateAndSetAndSignalOffer();
3091 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003092 MediaExpectations media_expectations;
3093 media_expectations.ExpectBidirectionalAudioAndVideo();
3094 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003095}
3096
deadbeef1dcb1642017-03-29 21:08:16 -07003097// This test sets up a call between two parties with audio, video and an RTP
3098// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003099TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003100 PeerConnectionInterface::RTCConfiguration rtc_config;
3101 rtc_config.enable_rtp_data_channel = true;
3102 rtc_config.enable_dtls_srtp = false;
3103 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003104 ConnectFakeSignaling();
3105 // Expect that data channel created on caller side will show up for callee as
3106 // well.
3107 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003108 caller()->AddAudioVideoTracks();
3109 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003110 caller()->CreateAndSetAndSignalOffer();
3111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3112 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003113 MediaExpectations media_expectations;
3114 media_expectations.ExpectBidirectionalAudioAndVideo();
3115 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003116 ASSERT_NE(nullptr, caller()->data_channel());
3117 ASSERT_NE(nullptr, callee()->data_channel());
3118 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3119 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3120
3121 // Ensure data can be sent in both directions.
3122 std::string data = "hello world";
3123 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3124 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3125 kDefaultTimeout);
3126 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3127 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3128 kDefaultTimeout);
3129}
3130
3131// Ensure that an RTP data channel is signaled as closed for the caller when
3132// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003133TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003134 RtpDataChannelSignaledClosedInCalleeOffer) {
3135 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003136 PeerConnectionInterface::RTCConfiguration rtc_config;
3137 rtc_config.enable_rtp_data_channel = true;
3138 rtc_config.enable_dtls_srtp = false;
3139 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003140 ConnectFakeSignaling();
3141 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003142 caller()->AddAudioVideoTracks();
3143 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003144 caller()->CreateAndSetAndSignalOffer();
3145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3146 ASSERT_NE(nullptr, caller()->data_channel());
3147 ASSERT_NE(nullptr, callee()->data_channel());
3148 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3149 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3150
3151 // Close the data channel on the callee, and do an updated offer/answer.
3152 callee()->data_channel()->Close();
3153 callee()->CreateAndSetAndSignalOffer();
3154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3155 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3156 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3157}
3158
3159// Tests that data is buffered in an RTP data channel until an observer is
3160// registered for it.
3161//
3162// NOTE: RTP data channels can receive data before the underlying
3163// transport has detected that a channel is writable and thus data can be
3164// received before the data channel state changes to open. That is hard to test
3165// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003166TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003167 DataBufferedUntilRtpDataChannelObserverRegistered) {
3168 // Use fake clock and simulated network delay so that we predictably can wait
3169 // until an SCTP message has been delivered without "sleep()"ing.
3170 rtc::ScopedFakeClock fake_clock;
3171 // Some things use a time of "0" as a special value, so we need to start out
3172 // the fake clock at a nonzero time.
3173 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003174 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003175 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3176 virtual_socket_server()->UpdateDelayDistribution();
3177
Niels Möllerf06f9232018-08-07 12:32:18 +02003178 PeerConnectionInterface::RTCConfiguration rtc_config;
3179 rtc_config.enable_rtp_data_channel = true;
3180 rtc_config.enable_dtls_srtp = false;
3181 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003182 ConnectFakeSignaling();
3183 caller()->CreateDataChannel();
3184 caller()->CreateAndSetAndSignalOffer();
3185 ASSERT_TRUE(caller()->data_channel() != nullptr);
3186 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3187 kDefaultTimeout, fake_clock);
3188 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3189 kDefaultTimeout, fake_clock);
3190 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3191 callee()->data_channel()->state(), kDefaultTimeout,
3192 fake_clock);
3193
3194 // Unregister the observer which is normally automatically registered.
3195 callee()->data_channel()->UnregisterObserver();
3196 // Send data and advance fake clock until it should have been received.
3197 std::string data = "hello world";
3198 caller()->data_channel()->Send(DataBuffer(data));
3199 SIMULATED_WAIT(false, 50, fake_clock);
3200
3201 // Attach data channel and expect data to be received immediately. Note that
3202 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3203 // further, but data can be received even if the callback is asynchronous.
3204 MockDataChannelObserver new_observer(callee()->data_channel());
3205 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3206 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003207 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3208 // If this is not done a DCHECK can be hit in ports.cc, because a large
3209 // negative number is calculated for the rtt due to the global clock changing.
3210 caller()->pc()->Close();
3211 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003212}
3213
3214// This test sets up a call between two parties with audio, video and but only
3215// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003216TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003217 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3218 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003219 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003220 rtc_config_1.enable_dtls_srtp = false;
3221 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3222 rtc_config_2.enable_dtls_srtp = false;
3223 rtc_config_2.enable_dtls_srtp = false;
3224 ASSERT_TRUE(
3225 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003226 ConnectFakeSignaling();
3227 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003228 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003229 caller()->AddAudioVideoTracks();
3230 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003231 caller()->CreateAndSetAndSignalOffer();
3232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3233 // The caller should still have a data channel, but it should be closed, and
3234 // one should ever have been created for the callee.
3235 EXPECT_TRUE(caller()->data_channel() != nullptr);
3236 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3237 EXPECT_EQ(nullptr, callee()->data_channel());
3238}
3239
3240// This test sets up a call between two parties with audio, and video. When
3241// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003242TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003243 PeerConnectionInterface::RTCConfiguration rtc_config;
3244 rtc_config.enable_rtp_data_channel = true;
3245 rtc_config.enable_dtls_srtp = false;
3246 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003247 ConnectFakeSignaling();
3248 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003249 caller()->AddAudioVideoTracks();
3250 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003251 caller()->CreateAndSetAndSignalOffer();
3252 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3253 // Create data channel and do new offer and answer.
3254 caller()->CreateDataChannel();
3255 caller()->CreateAndSetAndSignalOffer();
3256 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3257 ASSERT_NE(nullptr, caller()->data_channel());
3258 ASSERT_NE(nullptr, callee()->data_channel());
3259 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3260 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3261 // Ensure data can be sent in both directions.
3262 std::string data = "hello world";
3263 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3264 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3265 kDefaultTimeout);
3266 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3267 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3268 kDefaultTimeout);
3269}
3270
3271#ifdef HAVE_SCTP
3272
3273// This test sets up a call between two parties with audio, video and an SCTP
3274// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003275TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003276 ASSERT_TRUE(CreatePeerConnectionWrappers());
3277 ConnectFakeSignaling();
3278 // Expect that data channel created on caller side will show up for callee as
3279 // well.
3280 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003281 caller()->AddAudioVideoTracks();
3282 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003283 caller()->CreateAndSetAndSignalOffer();
3284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3285 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003286 MediaExpectations media_expectations;
3287 media_expectations.ExpectBidirectionalAudioAndVideo();
3288 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003289 // Caller data channel should already exist (it created one). Callee data
3290 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3291 ASSERT_NE(nullptr, caller()->data_channel());
3292 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3293 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3294 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3295
3296 // Ensure data can be sent in both directions.
3297 std::string data = "hello world";
3298 caller()->data_channel()->Send(DataBuffer(data));
3299 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3300 kDefaultTimeout);
3301 callee()->data_channel()->Send(DataBuffer(data));
3302 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3303 kDefaultTimeout);
3304}
3305
3306// Ensure that when the callee closes an SCTP data channel, the closing
3307// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003308TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003309 // Same procedure as above test.
3310 ASSERT_TRUE(CreatePeerConnectionWrappers());
3311 ConnectFakeSignaling();
3312 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003313 caller()->AddAudioVideoTracks();
3314 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003315 caller()->CreateAndSetAndSignalOffer();
3316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3317 ASSERT_NE(nullptr, caller()->data_channel());
3318 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3319 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3320 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3321
3322 // Close the data channel on the callee side, and wait for it to reach the
3323 // "closed" state on both sides.
3324 callee()->data_channel()->Close();
3325 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3326 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3327}
3328
Seth Hampson2f0d7022018-02-20 11:54:42 -08003329TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003330 ASSERT_TRUE(CreatePeerConnectionWrappers());
3331 ConnectFakeSignaling();
3332 webrtc::DataChannelInit init;
3333 init.id = 53;
3334 init.maxRetransmits = 52;
3335 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003336 caller()->AddAudioVideoTracks();
3337 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003338 caller()->CreateAndSetAndSignalOffer();
3339 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003340 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3341 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003342 // Since "negotiated" is false, the "id" parameter should be ignored.
3343 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003344 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3345 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3346 EXPECT_FALSE(callee()->data_channel()->negotiated());
3347}
3348
deadbeef1dcb1642017-03-29 21:08:16 -07003349// Test usrsctp's ability to process unordered data stream, where data actually
3350// arrives out of order using simulated delays. Previously there have been some
3351// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003352TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003353 // Introduce random network delays.
3354 // Otherwise it's not a true "unordered" test.
3355 virtual_socket_server()->set_delay_mean(20);
3356 virtual_socket_server()->set_delay_stddev(5);
3357 virtual_socket_server()->UpdateDelayDistribution();
3358 // Normal procedure, but with unordered data channel config.
3359 ASSERT_TRUE(CreatePeerConnectionWrappers());
3360 ConnectFakeSignaling();
3361 webrtc::DataChannelInit init;
3362 init.ordered = false;
3363 caller()->CreateDataChannel(&init);
3364 caller()->CreateAndSetAndSignalOffer();
3365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3366 ASSERT_NE(nullptr, caller()->data_channel());
3367 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3368 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3369 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3370
3371 static constexpr int kNumMessages = 100;
3372 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3373 static constexpr size_t kMaxMessageSize = 4096;
3374 // Create and send random messages.
3375 std::vector<std::string> sent_messages;
3376 for (int i = 0; i < kNumMessages; ++i) {
3377 size_t length =
3378 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3379 std::string message;
3380 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3381 caller()->data_channel()->Send(DataBuffer(message));
3382 callee()->data_channel()->Send(DataBuffer(message));
3383 sent_messages.push_back(message);
3384 }
3385
3386 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003387 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003388 caller()->data_observer()->received_message_count(),
3389 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003390 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003391 callee()->data_observer()->received_message_count(),
3392 kDefaultTimeout);
3393
3394 // Sort and compare to make sure none of the messages were corrupted.
3395 std::vector<std::string> caller_received_messages =
3396 caller()->data_observer()->messages();
3397 std::vector<std::string> callee_received_messages =
3398 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003399 absl::c_sort(sent_messages);
3400 absl::c_sort(caller_received_messages);
3401 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003402 EXPECT_EQ(sent_messages, caller_received_messages);
3403 EXPECT_EQ(sent_messages, callee_received_messages);
3404}
3405
3406// This test sets up a call between two parties with audio, and video. When
3407// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003408TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003409 ASSERT_TRUE(CreatePeerConnectionWrappers());
3410 ConnectFakeSignaling();
3411 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003412 caller()->AddAudioVideoTracks();
3413 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003414 caller()->CreateAndSetAndSignalOffer();
3415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3416 // Create data channel and do new offer and answer.
3417 caller()->CreateDataChannel();
3418 caller()->CreateAndSetAndSignalOffer();
3419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3420 // Caller data channel should already exist (it created one). Callee data
3421 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3422 ASSERT_NE(nullptr, caller()->data_channel());
3423 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3424 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3425 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3426 // Ensure data can be sent in both directions.
3427 std::string data = "hello world";
3428 caller()->data_channel()->Send(DataBuffer(data));
3429 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3430 kDefaultTimeout);
3431 callee()->data_channel()->Send(DataBuffer(data));
3432 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3433 kDefaultTimeout);
3434}
3435
deadbeef7914b8c2017-04-21 03:23:33 -07003436// Set up a connection initially just using SCTP data channels, later upgrading
3437// to audio/video, ensuring frames are received end-to-end. Effectively the
3438// inverse of the test above.
3439// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003440TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003441 ASSERT_TRUE(CreatePeerConnectionWrappers());
3442 ConnectFakeSignaling();
3443 // Do initial offer/answer with just data channel.
3444 caller()->CreateDataChannel();
3445 caller()->CreateAndSetAndSignalOffer();
3446 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3447 // Wait until data can be sent over the data channel.
3448 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3449 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3450 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3451
3452 // Do subsequent offer/answer with two-way audio and video. Audio and video
3453 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003454 caller()->AddAudioVideoTracks();
3455 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003456 caller()->CreateAndSetAndSignalOffer();
3457 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003458 MediaExpectations media_expectations;
3459 media_expectations.ExpectBidirectionalAudioAndVideo();
3460 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003461}
3462
deadbeef8b7e9ad2017-05-25 09:38:55 -07003463static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003464 cricket::SctpDataContentDescription* dcd_offer =
3465 GetFirstSctpDataContentDescription(desc);
Steve Antonb1c1de12017-12-21 15:14:30 -08003466 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003467 dcd_offer->set_use_sctpmap(false);
3468 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3469}
3470
3471// Test that the data channel works when a spec-compliant SCTP m= section is
3472// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3473// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003474TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003475 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3476 ASSERT_TRUE(CreatePeerConnectionWrappers());
3477 ConnectFakeSignaling();
3478 caller()->CreateDataChannel();
3479 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3480 caller()->CreateAndSetAndSignalOffer();
3481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3482 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3483 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3484 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3485
3486 // Ensure data can be sent in both directions.
3487 std::string data = "hello world";
3488 caller()->data_channel()->Send(DataBuffer(data));
3489 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3490 kDefaultTimeout);
3491 callee()->data_channel()->Send(DataBuffer(data));
3492 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3493 kDefaultTimeout);
3494}
3495
deadbeef1dcb1642017-03-29 21:08:16 -07003496#endif // HAVE_SCTP
3497
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003498// This test sets up a call between two parties with a media transport data
3499// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003500TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3501 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003502 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3503 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003504 rtc_config.use_media_transport_for_data_channels = true;
3505 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3506 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3507 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3508 loopback_media_transports()->second_factory()));
3509 ConnectFakeSignaling();
3510
3511 // Expect that data channel created on caller side will show up for callee as
3512 // well.
3513 caller()->CreateDataChannel();
3514 caller()->CreateAndSetAndSignalOffer();
3515 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3516
3517 // Ensure that the media transport is ready.
3518 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3519 loopback_media_transports()->FlushAsyncInvokes();
3520
3521 // Caller data channel should already exist (it created one). Callee data
3522 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3523 ASSERT_NE(nullptr, caller()->data_channel());
3524 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3525 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3526 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3527
3528 // Ensure data can be sent in both directions.
3529 std::string data = "hello world";
3530 caller()->data_channel()->Send(DataBuffer(data));
3531 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3532 kDefaultTimeout);
3533 callee()->data_channel()->Send(DataBuffer(data));
3534 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3535 kDefaultTimeout);
3536}
3537
3538// Ensure that when the callee closes a media transport data channel, the
3539// closing procedure results in the data channel being closed for the caller
3540// as well.
3541TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3542 PeerConnectionInterface::RTCConfiguration rtc_config;
3543 rtc_config.use_media_transport_for_data_channels = true;
3544 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3545 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3546 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3547 loopback_media_transports()->second_factory()));
3548 ConnectFakeSignaling();
3549
3550 // Create a data channel on the caller and signal it to the callee.
3551 caller()->CreateDataChannel();
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 // Data channels exist and open on both ends of the connection.
3560 ASSERT_NE(nullptr, caller()->data_channel());
3561 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3562 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3563 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3564
3565 // Close the data channel on the callee side, and wait for it to reach the
3566 // "closed" state on both sides.
3567 callee()->data_channel()->Close();
3568 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3569 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3570}
3571
3572TEST_P(PeerConnectionIntegrationTest,
3573 MediaTransportDataChannelConfigSentToOtherSide) {
3574 PeerConnectionInterface::RTCConfiguration rtc_config;
3575 rtc_config.use_media_transport_for_data_channels = true;
3576 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3578 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3579 loopback_media_transports()->second_factory()));
3580 ConnectFakeSignaling();
3581
3582 // Create a data channel with a non-default configuration and signal it to the
3583 // callee.
3584 webrtc::DataChannelInit init;
3585 init.id = 53;
3586 init.maxRetransmits = 52;
3587 caller()->CreateDataChannel("data-channel", &init);
3588 caller()->CreateAndSetAndSignalOffer();
3589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3590
3591 // Ensure that the media transport is ready.
3592 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3593 loopback_media_transports()->FlushAsyncInvokes();
3594
3595 // Ensure that the data channel exists on the callee with the correct
3596 // configuration.
3597 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3598 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003599 // Since "negotiate" is false, the "id" parameter is ignored.
3600 EXPECT_NE(init.id, callee()->data_channel()->id());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003601 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3602 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3603 EXPECT_FALSE(callee()->data_channel()->negotiated());
3604}
3605
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003606TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgrade) {
3607 PeerConnectionInterface::RTCConfiguration rtc_config;
3608 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3609 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3610 rtc_config.use_media_transport = true;
3611 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3612 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3613 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3614 loopback_media_transports()->second_factory()));
3615 ConnectFakeSignaling();
3616
3617 // Do initial offer/answer with just a video track.
3618 caller()->AddVideoTrack();
3619 callee()->AddVideoTrack();
3620 caller()->CreateAndSetAndSignalOffer();
3621 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3622
3623 // Ensure that the media transport is ready.
3624 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3625 loopback_media_transports()->FlushAsyncInvokes();
3626
3627 // Now add an audio track and do another offer/answer.
3628 caller()->AddAudioTrack();
3629 callee()->AddAudioTrack();
3630 caller()->CreateAndSetAndSignalOffer();
3631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3632
3633 // Ensure both audio and video frames are received end-to-end.
3634 MediaExpectations media_expectations;
3635 media_expectations.ExpectBidirectionalAudioAndVideo();
3636 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3637
3638 // The second offer should not have generated another media transport.
3639 // Media transport was kept alive, and was not recreated.
3640 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3641 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3642}
3643
3644TEST_P(PeerConnectionIntegrationTest, MediaTransportOfferUpgradeOnTheCallee) {
3645 PeerConnectionInterface::RTCConfiguration rtc_config;
3646 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3647 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3648 rtc_config.use_media_transport = true;
3649 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3650 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3651 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3652 loopback_media_transports()->second_factory()));
3653 ConnectFakeSignaling();
3654
3655 // Do initial offer/answer with just a video track.
3656 caller()->AddVideoTrack();
3657 callee()->AddVideoTrack();
3658 caller()->CreateAndSetAndSignalOffer();
3659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3660
3661 // Ensure that the media transport is ready.
3662 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3663 loopback_media_transports()->FlushAsyncInvokes();
3664
3665 // Now add an audio track and do another offer/answer.
3666 caller()->AddAudioTrack();
3667 callee()->AddAudioTrack();
3668 callee()->CreateAndSetAndSignalOffer();
3669 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3670
3671 // Ensure both audio and video frames are received end-to-end.
3672 MediaExpectations media_expectations;
3673 media_expectations.ExpectBidirectionalAudioAndVideo();
3674 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3675
3676 // The second offer should not have generated another media transport.
3677 // Media transport was kept alive, and was not recreated.
3678 EXPECT_EQ(1, loopback_media_transports()->first_factory_transport_count());
3679 EXPECT_EQ(1, loopback_media_transports()->second_factory_transport_count());
3680}
3681
Niels Möllerc68d2822018-11-20 14:52:05 +01003682TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3683 PeerConnectionInterface::RTCConfiguration rtc_config;
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08003684 rtc_config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3685 rtc_config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
Niels Möllerc68d2822018-11-20 14:52:05 +01003686 rtc_config.use_media_transport = true;
3687 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3688 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3689 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3690 loopback_media_transports()->second_factory()));
3691 ConnectFakeSignaling();
3692
3693 caller()->AddAudioTrack();
3694 callee()->AddAudioTrack();
3695 // Start offer/answer exchange and wait for it to complete.
3696 caller()->CreateAndSetAndSignalOffer();
3697 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3698
3699 // Ensure that the media transport is ready.
3700 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3701 loopback_media_transports()->FlushAsyncInvokes();
3702
3703 MediaExpectations media_expectations;
3704 media_expectations.ExpectBidirectionalAudio();
3705 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3706
3707 webrtc::MediaTransportPair::Stats first_stats =
3708 loopback_media_transports()->FirstStats();
3709 webrtc::MediaTransportPair::Stats second_stats =
3710 loopback_media_transports()->SecondStats();
3711
3712 EXPECT_GT(first_stats.received_audio_frames, 0);
3713 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3714
3715 EXPECT_GT(second_stats.received_audio_frames, 0);
3716 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3717}
3718
Niels Möller46879152019-01-07 15:54:47 +01003719TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3720 PeerConnectionInterface::RTCConfiguration rtc_config;
3721 rtc_config.use_media_transport = true;
3722 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3723 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3724 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3725 loopback_media_transports()->second_factory()));
3726 ConnectFakeSignaling();
3727
3728 caller()->AddVideoTrack();
3729 callee()->AddVideoTrack();
3730 // Start offer/answer exchange and wait for it to complete.
3731 caller()->CreateAndSetAndSignalOffer();
3732 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3733
3734 // Ensure that the media transport is ready.
3735 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3736 loopback_media_transports()->FlushAsyncInvokes();
3737
3738 MediaExpectations media_expectations;
3739 media_expectations.ExpectBidirectionalVideo();
3740 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3741
3742 webrtc::MediaTransportPair::Stats first_stats =
3743 loopback_media_transports()->FirstStats();
3744 webrtc::MediaTransportPair::Stats second_stats =
3745 loopback_media_transports()->SecondStats();
3746
3747 EXPECT_GT(first_stats.received_video_frames, 0);
3748 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3749
3750 EXPECT_GT(second_stats.received_video_frames, 0);
3751 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3752}
3753
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08003754TEST_P(PeerConnectionIntegrationTest,
3755 MediaTransportDataChannelUsesRtpBidirectionalVideo) {
3756 PeerConnectionInterface::RTCConfiguration rtc_config;
3757 rtc_config.use_media_transport = false;
3758 rtc_config.use_media_transport_for_data_channels = true;
3759 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3760 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3761 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3762 loopback_media_transports()->second_factory()));
3763 ConnectFakeSignaling();
3764
3765 caller()->AddVideoTrack();
3766 callee()->AddVideoTrack();
3767 // Start offer/answer exchange and wait for it to complete.
3768 caller()->CreateAndSetAndSignalOffer();
3769 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3770
3771 MediaExpectations media_expectations;
3772 media_expectations.ExpectBidirectionalVideo();
3773 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3774}
3775
deadbeef1dcb1642017-03-29 21:08:16 -07003776// Test that the ICE connection and gathering states eventually reach
3777// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003778TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003779 ASSERT_TRUE(CreatePeerConnectionWrappers());
3780 ConnectFakeSignaling();
3781 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003782 caller()->AddAudioVideoTracks();
3783 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003784 caller()->CreateAndSetAndSignalOffer();
3785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3786 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3787 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3788 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3789 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3790 // After the best candidate pair is selected and all candidates are signaled,
3791 // the ICE connection state should reach "complete".
3792 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3793 // answerer/"callee" by default) only reaches "connected". When this is
3794 // fixed, this test should be updated.
3795 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3796 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003797 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3798 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003799}
3800
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003801constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3802 cricket::PORTALLOCATOR_DISABLE_RELAY |
3803 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003804
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003805// Use a mock resolver to resolve the hostname back to the original IP on both
3806// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003807TEST_P(PeerConnectionIntegrationTest,
3808 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003809 auto caller_resolver_factory =
3810 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3811 auto callee_resolver_factory =
3812 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3813 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3814 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003815
3816 // This also verifies that the injected AsyncResolverFactory is used by
3817 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003818 EXPECT_CALL(*caller_resolver_factory, Create())
3819 .WillOnce(Return(&caller_async_resolver));
3820 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3821 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3822
3823 EXPECT_CALL(*callee_resolver_factory, Create())
3824 .WillOnce(Return(&callee_async_resolver));
3825 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3826 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3827
3828 PeerConnectionInterface::RTCConfiguration config;
3829 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3830 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3831
3832 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3833 config, std::move(caller_deps), config, std::move(callee_deps)));
3834
3835 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3836 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3837
3838 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07003839 caller()->SetMdnsResponder(
3840 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
3841 callee()->SetMdnsResponder(
3842 absl::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003843
3844 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003845
3846 ConnectFakeSignaling();
3847 caller()->AddAudioVideoTracks();
3848 callee()->AddAudioVideoTracks();
3849 caller()->CreateAndSetAndSignalOffer();
3850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3851 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3852 caller()->ice_connection_state(), kDefaultTimeout);
3853 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3854 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003855
3856 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3857 "WebRTC.PeerConnection.CandidatePairType_UDP",
3858 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003859}
3860
Steve Antonede9ca52017-10-16 13:04:27 -07003861// Test that firewalling the ICE connection causes the clients to identify the
3862// disconnected state and then removing the firewall causes them to reconnect.
3863class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003864 : public PeerConnectionIntegrationBaseTest,
3865 public ::testing::WithParamInterface<
3866 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003867 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003868 PeerConnectionIntegrationIceStatesTest()
3869 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3870 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003871 }
3872
3873 void StartStunServer(const SocketAddress& server_address) {
3874 stun_server_.reset(
3875 cricket::TestStunServer::Create(network_thread(), server_address));
3876 }
3877
3878 bool TestIPv6() {
3879 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3880 }
3881
3882 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003883 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3884 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003885 }
3886
3887 std::vector<SocketAddress> CallerAddresses() {
3888 std::vector<SocketAddress> addresses;
3889 addresses.push_back(SocketAddress("1.1.1.1", 0));
3890 if (TestIPv6()) {
3891 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3892 }
3893 return addresses;
3894 }
3895
3896 std::vector<SocketAddress> CalleeAddresses() {
3897 std::vector<SocketAddress> addresses;
3898 addresses.push_back(SocketAddress("2.2.2.2", 0));
3899 if (TestIPv6()) {
3900 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3901 }
3902 return addresses;
3903 }
3904
3905 void SetUpNetworkInterfaces() {
3906 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07003907 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3908 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07003909
3910 // Add network addresses for test.
3911 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003912 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003913 }
3914 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003915 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003916 }
3917 }
3918
3919 private:
3920 uint32_t port_allocator_flags_;
3921 std::unique_ptr<cricket::TestStunServer> stun_server_;
3922};
3923
3924// Tests that the PeerConnection goes through all the ICE gathering/connection
3925// states over the duration of the call. This includes Disconnected and Failed
3926// states, induced by putting a firewall between the peers and waiting for them
3927// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003928TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003929 rtc::ScopedFakeClock fake_clock;
3930 // Some things use a time of "0" as a special value, so we need to start out
3931 // the fake clock at a nonzero time.
3932 fake_clock.AdvanceTime(TimeDelta::seconds(1));
Steve Antonede9ca52017-10-16 13:04:27 -07003933
3934 const SocketAddress kStunServerAddress =
3935 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3936 StartStunServer(kStunServerAddress);
3937
3938 PeerConnectionInterface::RTCConfiguration config;
3939 PeerConnectionInterface::IceServer ice_stun_server;
3940 ice_stun_server.urls.push_back(
3941 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3942 kStunServerAddress.PortAsString());
3943 config.servers.push_back(ice_stun_server);
3944
3945 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3946 ConnectFakeSignaling();
3947 SetPortAllocatorFlags();
3948 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003949 caller()->AddAudioVideoTracks();
3950 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003951
3952 // Initial state before anything happens.
3953 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3954 caller()->ice_gathering_state());
3955 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3956 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003957 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3958 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003959
3960 // Start the call by creating the offer, setting it as the local description,
3961 // then sending it to the peer who will respond with an answer. This happens
3962 // asynchronously so that we can watch the states as it runs in the
3963 // background.
3964 caller()->CreateAndSetAndSignalOffer();
3965
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003966 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
3967 caller()->ice_connection_state());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003968 ASSERT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olsson7a6739e2019-01-15 16:31:55 +01003969 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07003970
3971 // Verify that the observer was notified of the intermediate transitions.
3972 EXPECT_THAT(caller()->ice_connection_state_history(),
3973 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3974 PeerConnectionInterface::kIceConnectionConnected,
3975 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01003976 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
3977 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3978 PeerConnectionInterface::kIceConnectionConnected,
3979 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003980 EXPECT_THAT(
3981 caller()->peer_connection_state_history(),
3982 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003983 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003984 EXPECT_THAT(caller()->ice_gathering_state_history(),
3985 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3986 PeerConnectionInterface::kIceGatheringComplete));
3987
3988 // Block connections to/from the caller and wait for ICE to become
3989 // disconnected.
3990 for (const auto& caller_address : CallerAddresses()) {
3991 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3992 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003993 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01003994 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3995 caller()->ice_connection_state(), kDefaultTimeout,
3996 fake_clock);
3997 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3998 caller()->standardized_ice_connection_state(),
3999 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004000
4001 // Let ICE re-establish by removing the firewall rules.
4002 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004003 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004004 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4005 caller()->ice_connection_state(), kDefaultTimeout,
4006 fake_clock);
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004007 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004008 caller()->standardized_ice_connection_state(),
4009 kDefaultTimeout, fake_clock);
Steve Antonede9ca52017-10-16 13:04:27 -07004010
4011 // According to RFC7675, if there is no response within 30 seconds then the
4012 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004013 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004014 constexpr int kConsentTimeout = 30000;
4015 for (const auto& caller_address : CallerAddresses()) {
4016 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4017 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004018 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004019 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4020 caller()->ice_connection_state(), kConsentTimeout,
4021 fake_clock);
4022 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4023 caller()->standardized_ice_connection_state(),
4024 kConsentTimeout, fake_clock);
4025
4026 // We need to manually close the peerconnections before the fake clock goes
4027 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4028 // return to using non-faked time.
4029 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4030 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4031}
4032
4033// Tests that if the connection doesn't get set up properly we eventually reach
4034// the "failed" iceConnectionState.
4035TEST_P(PeerConnectionIntegrationIceStatesTest, IceStateSetupFailure) {
4036 rtc::ScopedFakeClock fake_clock;
4037 // Some things use a time of "0" as a special value, so we need to start out
4038 // the fake clock at a nonzero time.
4039 fake_clock.AdvanceTime(TimeDelta::seconds(1));
4040
4041 // Block connections to/from the caller and wait for ICE to become
4042 // disconnected.
4043 for (const auto& caller_address : CallerAddresses()) {
4044 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4045 }
4046
4047 ASSERT_TRUE(CreatePeerConnectionWrappers());
4048 ConnectFakeSignaling();
4049 SetPortAllocatorFlags();
4050 SetUpNetworkInterfaces();
4051 caller()->AddAudioVideoTracks();
4052 caller()->CreateAndSetAndSignalOffer();
4053
4054 // According to RFC7675, if there is no response within 30 seconds then the
4055 // peer should consider the other side to have rejected the connection. This
4056 // is signaled by the state transitioning to "failed".
4057 constexpr int kConsentTimeout = 30000;
4058 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4059 caller()->standardized_ice_connection_state(),
4060 kConsentTimeout, fake_clock);
4061
4062 // We need to manually close the peerconnections before the fake clock goes
4063 // out of scope, or we trigger a DCHECK in rtp_sender.cc when we briefly
4064 // return to using non-faked time.
4065 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4066 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
Steve Antonede9ca52017-10-16 13:04:27 -07004067}
4068
4069// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4070// and that the statistics in the metric observers are updated correctly.
4071TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4072 ASSERT_TRUE(CreatePeerConnectionWrappers());
4073 ConnectFakeSignaling();
4074 SetPortAllocatorFlags();
4075 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004076 caller()->AddAudioVideoTracks();
4077 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004078 caller()->CreateAndSetAndSignalOffer();
4079
4080 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4081
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004082 // TODO(bugs.webrtc.org/9456): Fix it.
4083 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4084 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4085 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4086 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004087 if (TestIPv6()) {
4088 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4089 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004090 EXPECT_EQ(0, num_best_ipv4);
4091 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004092 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004093 EXPECT_EQ(1, num_best_ipv4);
4094 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004095 }
4096
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004097 EXPECT_EQ(0, webrtc::metrics::NumEvents(
4098 "WebRTC.PeerConnection.CandidatePairType_UDP",
4099 webrtc::kIceCandidatePairHostHost));
4100 EXPECT_EQ(1, webrtc::metrics::NumEvents(
4101 "WebRTC.PeerConnection.CandidatePairType_UDP",
4102 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004103}
4104
4105constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4106 cricket::PORTALLOCATOR_DISABLE_STUN |
4107 cricket::PORTALLOCATOR_DISABLE_RELAY;
4108constexpr uint32_t kFlagsIPv6NoStun =
4109 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4110 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4111constexpr uint32_t kFlagsIPv4Stun =
4112 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4113
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004114INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004115 PeerConnectionIntegrationTest,
4116 PeerConnectionIntegrationIceStatesTest,
4117 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4118 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4119 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4120 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004121
deadbeef1dcb1642017-03-29 21:08:16 -07004122// This test sets up a call between two parties with audio and video.
4123// During the call, the caller restarts ICE and the test verifies that
4124// new ICE candidates are generated and audio and video still can flow, and the
4125// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004126TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004127 ASSERT_TRUE(CreatePeerConnectionWrappers());
4128 ConnectFakeSignaling();
4129 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004130 caller()->AddAudioVideoTracks();
4131 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004132 caller()->CreateAndSetAndSignalOffer();
4133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4134 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4135 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004136 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4137 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004138
4139 // To verify that the ICE restart actually occurs, get
4140 // ufrag/password/candidates before and after restart.
4141 // Create an SDP string of the first audio candidate for both clients.
4142 const webrtc::IceCandidateCollection* audio_candidates_caller =
4143 caller()->pc()->local_description()->candidates(0);
4144 const webrtc::IceCandidateCollection* audio_candidates_callee =
4145 callee()->pc()->local_description()->candidates(0);
4146 ASSERT_GT(audio_candidates_caller->count(), 0u);
4147 ASSERT_GT(audio_candidates_callee->count(), 0u);
4148 std::string caller_candidate_pre_restart;
4149 ASSERT_TRUE(
4150 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4151 std::string callee_candidate_pre_restart;
4152 ASSERT_TRUE(
4153 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4154 const cricket::SessionDescription* desc =
4155 caller()->pc()->local_description()->description();
4156 std::string caller_ufrag_pre_restart =
4157 desc->transport_infos()[0].description.ice_ufrag;
4158 desc = callee()->pc()->local_description()->description();
4159 std::string callee_ufrag_pre_restart =
4160 desc->transport_infos()[0].description.ice_ufrag;
4161
4162 // Have the caller initiate an ICE restart.
4163 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4164 caller()->CreateAndSetAndSignalOffer();
4165 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4166 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4167 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004168 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004169 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4170
4171 // Grab the ufrags/candidates again.
4172 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4173 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4174 ASSERT_GT(audio_candidates_caller->count(), 0u);
4175 ASSERT_GT(audio_candidates_callee->count(), 0u);
4176 std::string caller_candidate_post_restart;
4177 ASSERT_TRUE(
4178 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4179 std::string callee_candidate_post_restart;
4180 ASSERT_TRUE(
4181 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4182 desc = caller()->pc()->local_description()->description();
4183 std::string caller_ufrag_post_restart =
4184 desc->transport_infos()[0].description.ice_ufrag;
4185 desc = callee()->pc()->local_description()->description();
4186 std::string callee_ufrag_post_restart =
4187 desc->transport_infos()[0].description.ice_ufrag;
4188 // Sanity check that an ICE restart was actually negotiated in SDP.
4189 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4190 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4191 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4192 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
4193
4194 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004195 MediaExpectations media_expectations;
4196 media_expectations.ExpectBidirectionalAudioAndVideo();
4197 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004198}
4199
4200// Verify that audio/video can be received end-to-end when ICE renomination is
4201// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004202TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004203 PeerConnectionInterface::RTCConfiguration config;
4204 config.enable_ice_renomination = true;
4205 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4206 ConnectFakeSignaling();
4207 // Do normal offer/answer and wait for some frames to be received in each
4208 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004209 caller()->AddAudioVideoTracks();
4210 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004211 caller()->CreateAndSetAndSignalOffer();
4212 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4213 // Sanity check that ICE renomination was actually negotiated.
4214 const cricket::SessionDescription* desc =
4215 caller()->pc()->local_description()->description();
4216 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004217 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004218 }
4219 desc = callee()->pc()->local_description()->description();
4220 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004221 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004222 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004223 MediaExpectations media_expectations;
4224 media_expectations.ExpectBidirectionalAudioAndVideo();
4225 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004226}
4227
Steve Anton6f25b092017-10-23 09:39:20 -07004228// With a max bundle policy and RTCP muxing, adding a new media description to
4229// the connection should not affect ICE at all because the new media will use
4230// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004231TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004232 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004233 PeerConnectionInterface::RTCConfiguration config;
4234 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4235 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4236 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4237 config, PeerConnectionInterface::RTCConfiguration()));
4238 ConnectFakeSignaling();
4239
Steve Anton15324772018-01-16 10:26:49 -08004240 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004241 caller()->CreateAndSetAndSignalOffer();
4242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004243 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4244 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004245
4246 caller()->clear_ice_connection_state_history();
4247
Steve Anton15324772018-01-16 10:26:49 -08004248 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004249 caller()->CreateAndSetAndSignalOffer();
4250 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4251
4252 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4253}
4254
deadbeef1dcb1642017-03-29 21:08:16 -07004255// This test sets up a call between two parties with audio and video. It then
4256// renegotiates setting the video m-line to "port 0", then later renegotiates
4257// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004258TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004259 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4260 ASSERT_TRUE(CreatePeerConnectionWrappers());
4261 ConnectFakeSignaling();
4262
4263 // Do initial negotiation, only sending media from the caller. Will result in
4264 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004265 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004266 caller()->CreateAndSetAndSignalOffer();
4267 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4268
4269 // Negotiate again, disabling the video "m=" section (the callee will set the
4270 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004271 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4272 PeerConnectionInterface::RTCOfferAnswerOptions options;
4273 options.offer_to_receive_video = 0;
4274 callee()->SetOfferAnswerOptions(options);
4275 } else {
4276 callee()->SetRemoteOfferHandler([this] {
4277 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4278 });
4279 }
deadbeef1dcb1642017-03-29 21:08:16 -07004280 caller()->CreateAndSetAndSignalOffer();
4281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4282 // Sanity check that video "m=" section was actually rejected.
4283 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4284 callee()->pc()->local_description()->description());
4285 ASSERT_NE(nullptr, answer_video_content);
4286 ASSERT_TRUE(answer_video_content->rejected);
4287
4288 // Enable video and do negotiation again, making sure video is received
4289 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004290 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4291 PeerConnectionInterface::RTCOfferAnswerOptions options;
4292 options.offer_to_receive_video = 1;
4293 callee()->SetOfferAnswerOptions(options);
4294 } else {
4295 // The caller's transceiver is stopped, so we need to add another track.
4296 auto caller_transceiver =
4297 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4298 EXPECT_TRUE(caller_transceiver->stopped());
4299 caller()->AddVideoTrack();
4300 }
4301 callee()->AddVideoTrack();
4302 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004303 caller()->CreateAndSetAndSignalOffer();
4304 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004305
deadbeef1dcb1642017-03-29 21:08:16 -07004306 // Verify the caller receives frames from the newly added stream, and the
4307 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004308 MediaExpectations media_expectations;
4309 media_expectations.CalleeExpectsSomeAudio();
4310 media_expectations.ExpectBidirectionalVideo();
4311 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004312}
4313
deadbeef1dcb1642017-03-29 21:08:16 -07004314// This tests that if we negotiate after calling CreateSender but before we
4315// have a track, then set a track later, frames from the newly-set track are
4316// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004317TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004318 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4319 ASSERT_TRUE(CreatePeerConnectionWrappers());
4320 ConnectFakeSignaling();
4321 auto caller_audio_sender =
4322 caller()->pc()->CreateSender("audio", "caller_stream");
4323 auto caller_video_sender =
4324 caller()->pc()->CreateSender("video", "caller_stream");
4325 auto callee_audio_sender =
4326 callee()->pc()->CreateSender("audio", "callee_stream");
4327 auto callee_video_sender =
4328 callee()->pc()->CreateSender("video", "callee_stream");
4329 caller()->CreateAndSetAndSignalOffer();
4330 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4331 // Wait for ICE to complete, without any tracks being set.
4332 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4333 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4334 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4335 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4336 // Now set the tracks, and expect frames to immediately start flowing.
4337 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4338 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4339 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4340 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004341 MediaExpectations media_expectations;
4342 media_expectations.ExpectBidirectionalAudioAndVideo();
4343 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4344}
4345
4346// This tests that if we negotiate after calling AddTransceiver but before we
4347// have a track, then set a track later, frames from the newly-set tracks are
4348// received end-to-end.
4349TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4350 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4351 ASSERT_TRUE(CreatePeerConnectionWrappers());
4352 ConnectFakeSignaling();
4353 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4354 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4355 auto caller_audio_sender = audio_result.MoveValue()->sender();
4356 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4357 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4358 auto caller_video_sender = video_result.MoveValue()->sender();
4359 callee()->SetRemoteOfferHandler([this] {
4360 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4361 callee()->pc()->GetTransceivers()[0]->SetDirection(
4362 RtpTransceiverDirection::kSendRecv);
4363 callee()->pc()->GetTransceivers()[1]->SetDirection(
4364 RtpTransceiverDirection::kSendRecv);
4365 });
4366 caller()->CreateAndSetAndSignalOffer();
4367 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4368 // Wait for ICE to complete, without any tracks being set.
4369 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4370 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4371 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4372 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4373 // Now set the tracks, and expect frames to immediately start flowing.
4374 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4375 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4376 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4377 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4378 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4379 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4380 MediaExpectations media_expectations;
4381 media_expectations.ExpectBidirectionalAudioAndVideo();
4382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004383}
4384
4385// This test verifies that a remote video track can be added via AddStream,
4386// and sent end-to-end. For this particular test, it's simply echoed back
4387// from the caller to the callee, rather than being forwarded to a third
4388// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004389TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004390 ASSERT_TRUE(CreatePeerConnectionWrappers());
4391 ConnectFakeSignaling();
4392 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004393 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004394 caller()->CreateAndSetAndSignalOffer();
4395 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004396 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004397
4398 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4399 // time).
4400 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4401 callee()->CreateAndSetAndSignalOffer();
4402 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4403
Seth Hampson2f0d7022018-02-20 11:54:42 -08004404 MediaExpectations media_expectations;
4405 media_expectations.ExpectBidirectionalVideo();
4406 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004407}
4408
4409// Test that we achieve the expected end-to-end connection time, using a
4410// fake clock and simulated latency on the media and signaling paths.
4411// We use a TURN<->TURN connection because this is usually the quickest to
4412// set up initially, especially when we're confident the connection will work
4413// and can start sending media before we get a STUN response.
4414//
4415// With various optimizations enabled, here are the network delays we expect to
4416// be on the critical path:
4417// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4418// signaling answer (with DTLS fingerprint).
4419// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4420// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4421// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004422TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004423 rtc::ScopedFakeClock fake_clock;
4424 // Some things use a time of "0" as a special value, so we need to start out
4425 // the fake clock at a nonzero time.
4426 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004427 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004428
4429 static constexpr int media_hop_delay_ms = 50;
4430 static constexpr int signaling_trip_delay_ms = 500;
4431 // For explanation of these values, see comment above.
4432 static constexpr int required_media_hops = 9;
4433 static constexpr int required_signaling_trips = 2;
4434 // For internal delays (such as posting an event asychronously).
4435 static constexpr int allowed_internal_delay_ms = 20;
4436 static constexpr int total_connection_time_ms =
4437 media_hop_delay_ms * required_media_hops +
4438 signaling_trip_delay_ms * required_signaling_trips +
4439 allowed_internal_delay_ms;
4440
4441 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4442 3478};
4443 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4444 0};
4445 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4446 3478};
4447 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4448 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004449 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4450 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004451
Seth Hampsonaed71642018-06-11 07:41:32 -07004452 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4453 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004454 // Bypass permission check on received packets so media can be sent before
4455 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004456 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4457 turn_server_1->set_enable_permission_checks(false);
4458 });
4459 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4460 turn_server_2->set_enable_permission_checks(false);
4461 });
deadbeef1dcb1642017-03-29 21:08:16 -07004462
4463 PeerConnectionInterface::RTCConfiguration client_1_config;
4464 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4465 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4466 ice_server_1.username = "test";
4467 ice_server_1.password = "test";
4468 client_1_config.servers.push_back(ice_server_1);
4469 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4470 client_1_config.presume_writable_when_fully_relayed = true;
4471
4472 PeerConnectionInterface::RTCConfiguration client_2_config;
4473 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4474 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4475 ice_server_2.username = "test";
4476 ice_server_2.password = "test";
4477 client_2_config.servers.push_back(ice_server_2);
4478 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4479 client_2_config.presume_writable_when_fully_relayed = true;
4480
4481 ASSERT_TRUE(
4482 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4483 // Set up the simulated delays.
4484 SetSignalingDelayMs(signaling_trip_delay_ms);
4485 ConnectFakeSignaling();
4486 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4487 virtual_socket_server()->UpdateDelayDistribution();
4488
4489 // Set "offer to receive audio/video" without adding any tracks, so we just
4490 // set up ICE/DTLS with no media.
4491 PeerConnectionInterface::RTCOfferAnswerOptions options;
4492 options.offer_to_receive_audio = 1;
4493 options.offer_to_receive_video = 1;
4494 caller()->SetOfferAnswerOptions(options);
4495 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004496 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4497 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004498 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4499 // If this is not done a DCHECK can be hit in ports.cc, because a large
4500 // negative number is calculated for the rtt due to the global clock changing.
4501 caller()->pc()->Close();
4502 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004503}
4504
Jonas Orelandbdcee282017-10-10 14:01:40 +02004505// Verify that a TurnCustomizer passed in through RTCConfiguration
4506// is actually used by the underlying TURN candidate pair.
4507// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004508TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004509 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4510 3478};
4511 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4512 0};
4513 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4514 3478};
4515 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4516 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004517 CreateTurnServer(turn_server_1_internal_address,
4518 turn_server_1_external_address);
4519 CreateTurnServer(turn_server_2_internal_address,
4520 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004521
4522 PeerConnectionInterface::RTCConfiguration client_1_config;
4523 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4524 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4525 ice_server_1.username = "test";
4526 ice_server_1.password = "test";
4527 client_1_config.servers.push_back(ice_server_1);
4528 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004529 auto* customizer1 = CreateTurnCustomizer();
4530 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004531
4532 PeerConnectionInterface::RTCConfiguration client_2_config;
4533 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4534 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4535 ice_server_2.username = "test";
4536 ice_server_2.password = "test";
4537 client_2_config.servers.push_back(ice_server_2);
4538 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004539 auto* customizer2 = CreateTurnCustomizer();
4540 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004541
4542 ASSERT_TRUE(
4543 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4544 ConnectFakeSignaling();
4545
4546 // Set "offer to receive audio/video" without adding any tracks, so we just
4547 // set up ICE/DTLS with no media.
4548 PeerConnectionInterface::RTCOfferAnswerOptions options;
4549 options.offer_to_receive_audio = 1;
4550 options.offer_to_receive_video = 1;
4551 caller()->SetOfferAnswerOptions(options);
4552 caller()->CreateAndSetAndSignalOffer();
4553 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4554
Seth Hampsonaed71642018-06-11 07:41:32 -07004555 ExpectTurnCustomizerCountersIncremented(customizer1);
4556 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004557}
4558
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004559// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4560// send media between the caller and the callee.
4561TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4562 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4563 3478};
4564 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4565
4566 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004567 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4568 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004569
4570 webrtc::PeerConnectionInterface::IceServer ice_server;
4571 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4572 ice_server.username = "test";
4573 ice_server.password = "test";
4574
4575 PeerConnectionInterface::RTCConfiguration client_1_config;
4576 client_1_config.servers.push_back(ice_server);
4577 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4578
4579 PeerConnectionInterface::RTCConfiguration client_2_config;
4580 client_2_config.servers.push_back(ice_server);
4581 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4582
4583 ASSERT_TRUE(
4584 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4585
4586 // Do normal offer/answer and wait for ICE to complete.
4587 ConnectFakeSignaling();
4588 caller()->AddAudioVideoTracks();
4589 callee()->AddAudioVideoTracks();
4590 caller()->CreateAndSetAndSignalOffer();
4591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4592 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4593 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4594
4595 MediaExpectations media_expectations;
4596 media_expectations.ExpectBidirectionalAudioAndVideo();
4597 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4598}
4599
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004600// Verify that a SSLCertificateVerifier passed in through
4601// PeerConnectionDependencies is actually used by the underlying SSL
4602// implementation to determine whether a certificate presented by the TURN
4603// server is accepted by the client. Note that openssladapter_unittest.cc
4604// contains more detailed, lower-level tests.
4605TEST_P(PeerConnectionIntegrationTest,
4606 SSLCertificateVerifierUsedForTurnConnections) {
4607 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4608 3478};
4609 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4610
4611 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4612 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004613 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4614 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004615
4616 webrtc::PeerConnectionInterface::IceServer ice_server;
4617 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4618 ice_server.username = "test";
4619 ice_server.password = "test";
4620
4621 PeerConnectionInterface::RTCConfiguration client_1_config;
4622 client_1_config.servers.push_back(ice_server);
4623 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4624
4625 PeerConnectionInterface::RTCConfiguration client_2_config;
4626 client_2_config.servers.push_back(ice_server);
4627 // Setting the type to kRelay forces the connection to go through a TURN
4628 // server.
4629 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4630
4631 // Get a copy to the pointer so we can verify calls later.
4632 rtc::TestCertificateVerifier* client_1_cert_verifier =
4633 new rtc::TestCertificateVerifier();
4634 client_1_cert_verifier->verify_certificate_ = true;
4635 rtc::TestCertificateVerifier* client_2_cert_verifier =
4636 new rtc::TestCertificateVerifier();
4637 client_2_cert_verifier->verify_certificate_ = true;
4638
4639 // Create the dependencies with the test certificate verifier.
4640 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4641 client_1_deps.tls_cert_verifier =
4642 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4643 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4644 client_2_deps.tls_cert_verifier =
4645 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4646
4647 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4648 client_1_config, std::move(client_1_deps), client_2_config,
4649 std::move(client_2_deps)));
4650 ConnectFakeSignaling();
4651
4652 // Set "offer to receive audio/video" without adding any tracks, so we just
4653 // set up ICE/DTLS with no media.
4654 PeerConnectionInterface::RTCOfferAnswerOptions options;
4655 options.offer_to_receive_audio = 1;
4656 options.offer_to_receive_video = 1;
4657 caller()->SetOfferAnswerOptions(options);
4658 caller()->CreateAndSetAndSignalOffer();
4659 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4660
4661 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4662 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004663}
4664
4665TEST_P(PeerConnectionIntegrationTest,
4666 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4667 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4668 3478};
4669 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4670
4671 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4672 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004673 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4674 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004675
4676 webrtc::PeerConnectionInterface::IceServer ice_server;
4677 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4678 ice_server.username = "test";
4679 ice_server.password = "test";
4680
4681 PeerConnectionInterface::RTCConfiguration client_1_config;
4682 client_1_config.servers.push_back(ice_server);
4683 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4684
4685 PeerConnectionInterface::RTCConfiguration client_2_config;
4686 client_2_config.servers.push_back(ice_server);
4687 // Setting the type to kRelay forces the connection to go through a TURN
4688 // server.
4689 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4690
4691 // Get a copy to the pointer so we can verify calls later.
4692 rtc::TestCertificateVerifier* client_1_cert_verifier =
4693 new rtc::TestCertificateVerifier();
4694 client_1_cert_verifier->verify_certificate_ = false;
4695 rtc::TestCertificateVerifier* client_2_cert_verifier =
4696 new rtc::TestCertificateVerifier();
4697 client_2_cert_verifier->verify_certificate_ = false;
4698
4699 // Create the dependencies with the test certificate verifier.
4700 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4701 client_1_deps.tls_cert_verifier =
4702 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4703 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4704 client_2_deps.tls_cert_verifier =
4705 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4706
4707 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4708 client_1_config, std::move(client_1_deps), client_2_config,
4709 std::move(client_2_deps)));
4710 ConnectFakeSignaling();
4711
4712 // Set "offer to receive audio/video" without adding any tracks, so we just
4713 // set up ICE/DTLS with no media.
4714 PeerConnectionInterface::RTCOfferAnswerOptions options;
4715 options.offer_to_receive_audio = 1;
4716 options.offer_to_receive_video = 1;
4717 caller()->SetOfferAnswerOptions(options);
4718 caller()->CreateAndSetAndSignalOffer();
4719 bool wait_res = true;
4720 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4721 // properly, should be able to just wait for a state of "failed" instead of
4722 // waiting a fixed 10 seconds.
4723 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4724 ASSERT_FALSE(wait_res);
4725
4726 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4727 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004728}
4729
deadbeefc964d0b2017-04-03 10:03:35 -07004730// Test that audio and video flow end-to-end when codec names don't use the
4731// expected casing, given that they're supposed to be case insensitive. To test
4732// this, all but one codec is removed from each media description, and its
4733// casing is changed.
4734//
4735// In the past, this has regressed and caused crashes/black video, due to the
4736// fact that code at some layers was doing case-insensitive comparisons and
4737// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004738TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004739 ASSERT_TRUE(CreatePeerConnectionWrappers());
4740 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004741 caller()->AddAudioVideoTracks();
4742 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004743
4744 // Remove all but one audio/video codec (opus and VP8), and change the
4745 // casing of the caller's generated offer.
4746 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4747 cricket::AudioContentDescription* audio =
4748 GetFirstAudioContentDescription(description);
4749 ASSERT_NE(nullptr, audio);
4750 auto audio_codecs = audio->codecs();
4751 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4752 [](const cricket::AudioCodec& codec) {
4753 return codec.name != "opus";
4754 }),
4755 audio_codecs.end());
4756 ASSERT_EQ(1u, audio_codecs.size());
4757 audio_codecs[0].name = "OpUs";
4758 audio->set_codecs(audio_codecs);
4759
4760 cricket::VideoContentDescription* video =
4761 GetFirstVideoContentDescription(description);
4762 ASSERT_NE(nullptr, video);
4763 auto video_codecs = video->codecs();
4764 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4765 [](const cricket::VideoCodec& codec) {
4766 return codec.name != "VP8";
4767 }),
4768 video_codecs.end());
4769 ASSERT_EQ(1u, video_codecs.size());
4770 video_codecs[0].name = "vP8";
4771 video->set_codecs(video_codecs);
4772 });
4773
4774 caller()->CreateAndSetAndSignalOffer();
4775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4776
4777 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004778 MediaExpectations media_expectations;
4779 media_expectations.ExpectBidirectionalAudioAndVideo();
4780 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004781}
4782
Jonas Oreland49ac5952018-09-26 16:04:32 +02004783TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004784 ASSERT_TRUE(CreatePeerConnectionWrappers());
4785 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004786 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004787 caller()->CreateAndSetAndSignalOffer();
4788 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004789 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004790 MediaExpectations media_expectations;
4791 media_expectations.CalleeExpectsSomeAudio(1);
4792 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004793 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004794 auto receiver = callee()->pc()->GetReceivers()[0];
4795 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004796 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004797 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4798 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004799 sources[0].source_id());
4800 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4801}
4802
4803TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4804 ASSERT_TRUE(CreatePeerConnectionWrappers());
4805 ConnectFakeSignaling();
4806 caller()->AddVideoTrack();
4807 caller()->CreateAndSetAndSignalOffer();
4808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4809 // Wait for one video frame to be received by the callee.
4810 MediaExpectations media_expectations;
4811 media_expectations.CalleeExpectsSomeVideo(1);
4812 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4813 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4814 auto receiver = callee()->pc()->GetReceivers()[0];
4815 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4816 auto sources = receiver->GetSources();
4817 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4818 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4819 sources[0].source_id());
4820 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004821}
4822
deadbeef2f425aa2017-04-14 10:41:32 -07004823// Test that if a track is removed and added again with a different stream ID,
4824// the new stream ID is successfully communicated in SDP and media continues to
4825// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004826// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4827// it will not reuse a transceiver that has already been sending. After creating
4828// a new transceiver it tries to create an offer with two senders of the same
4829// track ids and it fails.
4830TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004831 ASSERT_TRUE(CreatePeerConnectionWrappers());
4832 ConnectFakeSignaling();
4833
deadbeef2f425aa2017-04-14 10:41:32 -07004834 // Add track using stream 1, do offer/answer.
4835 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4836 caller()->CreateLocalAudioTrack();
4837 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004838 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004839 caller()->CreateAndSetAndSignalOffer();
4840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004841 {
4842 MediaExpectations media_expectations;
4843 media_expectations.CalleeExpectsSomeAudio(1);
4844 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4845 }
deadbeef2f425aa2017-04-14 10:41:32 -07004846 // Remove the sender, and create a new one with the new stream.
4847 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004848 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004849 caller()->CreateAndSetAndSignalOffer();
4850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4851 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004852 {
4853 MediaExpectations media_expectations;
4854 media_expectations.CalleeExpectsSomeAudio();
4855 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4856 }
deadbeef2f425aa2017-04-14 10:41:32 -07004857}
4858
Seth Hampson2f0d7022018-02-20 11:54:42 -08004859TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004860 ASSERT_TRUE(CreatePeerConnectionWrappers());
4861 ConnectFakeSignaling();
4862
Karl Wiberg918f50c2018-07-05 11:40:33 +02004863 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004864 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4865 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004866 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004867 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4868 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004869
Steve Anton15324772018-01-16 10:26:49 -08004870 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004871 caller()->CreateAndSetAndSignalOffer();
4872 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4873}
4874
Steve Antonede9ca52017-10-16 13:04:27 -07004875// Test that if candidates are only signaled by applying full session
4876// descriptions (instead of using AddIceCandidate), the peers can connect to
4877// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004878TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004879 ASSERT_TRUE(CreatePeerConnectionWrappers());
4880 // Each side will signal the session descriptions but not candidates.
4881 ConnectFakeSignalingForSdpOnly();
4882
4883 // Add audio video track and exchange the initial offer/answer with media
4884 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004885 caller()->AddAudioVideoTracks();
4886 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004887 caller()->CreateAndSetAndSignalOffer();
4888
4889 // Wait for all candidates to be gathered on both the caller and callee.
4890 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4891 caller()->ice_gathering_state(), kDefaultTimeout);
4892 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4893 callee()->ice_gathering_state(), kDefaultTimeout);
4894
4895 // The candidates will now be included in the session description, so
4896 // signaling them will start the ICE connection.
4897 caller()->CreateAndSetAndSignalOffer();
4898 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4899
4900 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004901 MediaExpectations media_expectations;
4902 media_expectations.ExpectBidirectionalAudioAndVideo();
4903 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004904}
4905
henrika5f6bf242017-11-01 11:06:56 +01004906// Test that SetAudioPlayout can be used to disable audio playout from the
4907// start, then later enable it. This may be useful, for example, if the caller
4908// needs to play a local ringtone until some event occurs, after which it
4909// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004910TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004911 ASSERT_TRUE(CreatePeerConnectionWrappers());
4912 ConnectFakeSignaling();
4913
4914 // Set up audio-only call where audio playout is disabled on caller's side.
4915 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004916 caller()->AddAudioTrack();
4917 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004918 caller()->CreateAndSetAndSignalOffer();
4919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4920
4921 // Pump messages for a second.
4922 WAIT(false, 1000);
4923 // Since audio playout is disabled, the caller shouldn't have received
4924 // anything (at the playout level, at least).
4925 EXPECT_EQ(0, caller()->audio_frames_received());
4926 // As a sanity check, make sure the callee (for which playout isn't disabled)
4927 // did still see frames on its audio level.
4928 ASSERT_GT(callee()->audio_frames_received(), 0);
4929
4930 // Enable playout again, and ensure audio starts flowing.
4931 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004932 MediaExpectations media_expectations;
4933 media_expectations.ExpectBidirectionalAudio();
4934 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004935}
4936
4937double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4938 auto report = pc->NewGetStats();
4939 auto track_stats_list =
4940 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4941 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4942 for (const auto* track_stats : track_stats_list) {
4943 if (track_stats->remote_source.is_defined() &&
4944 *track_stats->remote_source) {
4945 remote_track_stats = track_stats;
4946 break;
4947 }
4948 }
4949
4950 if (!remote_track_stats->total_audio_energy.is_defined()) {
4951 return 0.0;
4952 }
4953 return *remote_track_stats->total_audio_energy;
4954}
4955
4956// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4957// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004958TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004959 DisableAudioPlayoutStillGeneratesAudioStats) {
4960 ASSERT_TRUE(CreatePeerConnectionWrappers());
4961 ConnectFakeSignaling();
4962
4963 // Set up audio-only call where playout is disabled but audio-processing is
4964 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004965 caller()->AddAudioTrack();
4966 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004967 caller()->pc()->SetAudioPlayout(false);
4968
4969 caller()->CreateAndSetAndSignalOffer();
4970 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4971
4972 // Wait for the callee to receive audio stats.
4973 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4974}
4975
henrika4f167df2017-11-01 14:45:55 +01004976// Test that SetAudioRecording can be used to disable audio recording from the
4977// start, then later enable it. This may be useful, for example, if the caller
4978// wants to ensure that no audio resources are active before a certain state
4979// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004980TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004981 ASSERT_TRUE(CreatePeerConnectionWrappers());
4982 ConnectFakeSignaling();
4983
4984 // Set up audio-only call where audio recording is disabled on caller's side.
4985 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004986 caller()->AddAudioTrack();
4987 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004988 caller()->CreateAndSetAndSignalOffer();
4989 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4990
4991 // Pump messages for a second.
4992 WAIT(false, 1000);
4993 // Since caller has disabled audio recording, the callee shouldn't have
4994 // received anything.
4995 EXPECT_EQ(0, callee()->audio_frames_received());
4996 // As a sanity check, make sure the caller did still see frames on its
4997 // audio level since audio recording is enabled on the calle side.
4998 ASSERT_GT(caller()->audio_frames_received(), 0);
4999
5000 // Enable audio recording again, and ensure audio starts flowing.
5001 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005002 MediaExpectations media_expectations;
5003 media_expectations.ExpectBidirectionalAudio();
5004 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005005}
5006
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005007// Test that after closing PeerConnections, they stop sending any packets (ICE,
5008// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005009TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005010 // Set up audio/video/data, wait for some frames to be received.
5011 ASSERT_TRUE(CreatePeerConnectionWrappers());
5012 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005013 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005014#ifdef HAVE_SCTP
5015 caller()->CreateDataChannel();
5016#endif
5017 caller()->CreateAndSetAndSignalOffer();
5018 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005019 MediaExpectations media_expectations;
5020 media_expectations.CalleeExpectsSomeAudioAndVideo();
5021 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005022 // Close PeerConnections.
5023 caller()->pc()->Close();
5024 callee()->pc()->Close();
5025 // Pump messages for a second, and ensure no new packets end up sent.
5026 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5027 WAIT(false, 1000);
5028 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5029 EXPECT_EQ(sent_packets_a, sent_packets_b);
5030}
5031
Steve Anton7eca0932018-03-30 15:18:41 -07005032// Test that transport stats are generated by the RTCStatsCollector for a
5033// connection that only involves data channels. This is a regression test for
5034// crbug.com/826972.
5035#ifdef HAVE_SCTP
5036TEST_P(PeerConnectionIntegrationTest,
5037 TransportStatsReportedForDataChannelOnlyConnection) {
5038 ASSERT_TRUE(CreatePeerConnectionWrappers());
5039 ConnectFakeSignaling();
5040 caller()->CreateDataChannel();
5041
5042 caller()->CreateAndSetAndSignalOffer();
5043 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5044 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5045
5046 auto caller_report = caller()->NewGetStats();
5047 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5048 auto callee_report = callee()->NewGetStats();
5049 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5050}
5051#endif // HAVE_SCTP
5052
Qingsi Wang7685e862018-06-11 20:15:46 -07005053TEST_P(PeerConnectionIntegrationTest,
5054 IceEventsGeneratedAndLoggedInRtcEventLog) {
5055 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5056 ConnectFakeSignaling();
5057 PeerConnectionInterface::RTCOfferAnswerOptions options;
5058 options.offer_to_receive_audio = 1;
5059 caller()->SetOfferAnswerOptions(options);
5060 caller()->CreateAndSetAndSignalOffer();
5061 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5062 ASSERT_NE(nullptr, caller()->event_log_factory());
5063 ASSERT_NE(nullptr, callee()->event_log_factory());
5064 webrtc::FakeRtcEventLog* caller_event_log =
5065 static_cast<webrtc::FakeRtcEventLog*>(
5066 caller()->event_log_factory()->last_log_created());
5067 webrtc::FakeRtcEventLog* callee_event_log =
5068 static_cast<webrtc::FakeRtcEventLog*>(
5069 callee()->event_log_factory()->last_log_created());
5070 ASSERT_NE(nullptr, caller_event_log);
5071 ASSERT_NE(nullptr, callee_event_log);
5072 int caller_ice_config_count = caller_event_log->GetEventCount(
5073 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5074 int caller_ice_event_count = caller_event_log->GetEventCount(
5075 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5076 int callee_ice_config_count = callee_event_log->GetEventCount(
5077 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5078 int callee_ice_event_count = callee_event_log->GetEventCount(
5079 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5080 EXPECT_LT(0, caller_ice_config_count);
5081 EXPECT_LT(0, caller_ice_event_count);
5082 EXPECT_LT(0, callee_ice_config_count);
5083 EXPECT_LT(0, callee_ice_event_count);
5084}
5085
Qingsi Wangc129c352019-04-18 10:41:58 -07005086TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
5087 webrtc::test::ScopedFieldTrials field_trials(
5088 "WebRTC-GatherOnCandidateFilterChanged/Enabled/");
5089 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5090 3478};
5091 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5092
5093 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5094
5095 webrtc::PeerConnectionInterface::IceServer ice_server;
5096 ice_server.urls.push_back("turn:88.88.88.0:3478");
5097 ice_server.username = "test";
5098 ice_server.password = "test";
5099
5100 PeerConnectionInterface::RTCConfiguration caller_config;
5101 caller_config.servers.push_back(ice_server);
5102 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5103 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5104
5105 PeerConnectionInterface::RTCConfiguration callee_config;
5106 callee_config.servers.push_back(ice_server);
5107 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5108 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5109
5110 ASSERT_TRUE(
5111 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5112
5113 // Do normal offer/answer and wait for ICE to complete.
5114 ConnectFakeSignaling();
5115 caller()->AddAudioVideoTracks();
5116 callee()->AddAudioVideoTracks();
5117 caller()->CreateAndSetAndSignalOffer();
5118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5119 // Since we are doing continual gathering, the ICE transport does not reach
5120 // kIceGatheringComplete (see
5121 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5122 // kIceConnectionComplete.
5123 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5124 caller()->ice_connection_state(), kDefaultTimeout);
5125 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5126 callee()->ice_connection_state(), kDefaultTimeout);
5127 // Note that we cannot use the metric
5128 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5129 // metric is only populated when we reach kIceConnectionComplete in the
5130 // current implementation.
5131 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5132 caller()->last_candidate_gathered().type());
5133 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5134 callee()->last_candidate_gathered().type());
5135
5136 // Loosen the caller's candidate filter.
5137 caller_config = caller()->pc()->GetConfiguration();
5138 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5139 caller()->pc()->SetConfiguration(caller_config);
5140 // We should have gathered a new host candidate.
5141 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5142 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5143
5144 // Loosen the callee's candidate filter.
5145 callee_config = callee()->pc()->GetConfiguration();
5146 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5147 callee()->pc()->SetConfiguration(callee_config);
5148 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5149 callee()->last_candidate_gathered().type(), kDefaultTimeout);
5150}
5151
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005152INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5153 PeerConnectionIntegrationTest,
5154 Values(SdpSemantics::kPlanB,
5155 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005156
Steve Anton74255ff2018-01-24 18:32:57 -08005157// Tests that verify interoperability between Plan B and Unified Plan
5158// PeerConnections.
5159class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005160 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005161 public ::testing::WithParamInterface<
5162 std::tuple<SdpSemantics, SdpSemantics>> {
5163 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005164 // Setting the SdpSemantics for the base test to kDefault does not matter
5165 // because we specify not to use the test semantics when creating
5166 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005167 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005168 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005169 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005170 callee_semantics_(std::get<1>(GetParam())) {}
5171
5172 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005173 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5174 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005175 }
5176
5177 const SdpSemantics caller_semantics_;
5178 const SdpSemantics callee_semantics_;
5179};
5180
5181TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5182 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5183 ConnectFakeSignaling();
5184
5185 caller()->CreateAndSetAndSignalOffer();
5186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5187}
5188
5189TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5190 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5191 ConnectFakeSignaling();
5192 auto audio_sender = caller()->AddAudioTrack();
5193
5194 caller()->CreateAndSetAndSignalOffer();
5195 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5196
5197 // Verify that one audio receiver has been created on the remote and that it
5198 // has the same track ID as the sending track.
5199 auto receivers = callee()->pc()->GetReceivers();
5200 ASSERT_EQ(1u, receivers.size());
5201 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5202 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5203
Seth Hampson2f0d7022018-02-20 11:54:42 -08005204 MediaExpectations media_expectations;
5205 media_expectations.CalleeExpectsSomeAudio();
5206 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005207}
5208
5209TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5210 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5211 ConnectFakeSignaling();
5212 auto video_sender = caller()->AddVideoTrack();
5213 auto audio_sender = caller()->AddAudioTrack();
5214
5215 caller()->CreateAndSetAndSignalOffer();
5216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5217
5218 // Verify that one audio and one video receiver have been created on the
5219 // remote and that they have the same track IDs as the sending tracks.
5220 auto audio_receivers =
5221 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5222 ASSERT_EQ(1u, audio_receivers.size());
5223 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5224 auto video_receivers =
5225 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5226 ASSERT_EQ(1u, video_receivers.size());
5227 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5228
Seth Hampson2f0d7022018-02-20 11:54:42 -08005229 MediaExpectations media_expectations;
5230 media_expectations.CalleeExpectsSomeAudioAndVideo();
5231 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005232}
5233
5234TEST_P(PeerConnectionIntegrationInteropTest,
5235 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5236 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5237 ConnectFakeSignaling();
5238 caller()->AddAudioVideoTracks();
5239 callee()->AddAudioVideoTracks();
5240
5241 caller()->CreateAndSetAndSignalOffer();
5242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5243
Seth Hampson2f0d7022018-02-20 11:54:42 -08005244 MediaExpectations media_expectations;
5245 media_expectations.ExpectBidirectionalAudioAndVideo();
5246 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005247}
5248
5249TEST_P(PeerConnectionIntegrationInteropTest,
5250 ReverseRolesOneAudioLocalToOneVideoRemote) {
5251 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5252 ConnectFakeSignaling();
5253 caller()->AddAudioTrack();
5254 callee()->AddVideoTrack();
5255
5256 caller()->CreateAndSetAndSignalOffer();
5257 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5258
5259 // Verify that only the audio track has been negotiated.
5260 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5261 // Might also check that the callee's NegotiationNeeded flag is set.
5262
5263 // Reverse roles.
5264 callee()->CreateAndSetAndSignalOffer();
5265 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5266
Seth Hampson2f0d7022018-02-20 11:54:42 -08005267 MediaExpectations media_expectations;
5268 media_expectations.CallerExpectsSomeVideo();
5269 media_expectations.CalleeExpectsSomeAudio();
5270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005271}
5272
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005273INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005274 PeerConnectionIntegrationTest,
5275 PeerConnectionIntegrationInteropTest,
5276 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5277 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5278
5279// Test that if the Unified Plan side offers two video tracks then the Plan B
5280// side will only see the first one and ignore the second.
5281TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005282 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5283 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005284 ConnectFakeSignaling();
5285 auto first_sender = caller()->AddVideoTrack();
5286 caller()->AddVideoTrack();
5287
5288 caller()->CreateAndSetAndSignalOffer();
5289 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5290
5291 // Verify that there is only one receiver and it corresponds to the first
5292 // added track.
5293 auto receivers = callee()->pc()->GetReceivers();
5294 ASSERT_EQ(1u, receivers.size());
5295 EXPECT_TRUE(receivers[0]->track()->enabled());
5296 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5297
Seth Hampson2f0d7022018-02-20 11:54:42 -08005298 MediaExpectations media_expectations;
5299 media_expectations.CalleeExpectsSomeVideo();
5300 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005301}
5302
Steve Anton2bed3972019-01-04 17:04:30 -08005303// Test that if the initial offer tagged BUNDLE section is rejected due to its
5304// associated RtpTransceiver being stopped and another transceiver is added,
5305// then renegotiation causes the callee to receive the new video track without
5306// error.
5307// This is a regression test for bugs.webrtc.org/9954
5308TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5309 ReOfferWithStoppedBundleTaggedTransceiver) {
5310 RTCConfiguration config;
5311 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5312 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5313 ConnectFakeSignaling();
5314 auto audio_transceiver_or_error =
5315 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5316 ASSERT_TRUE(audio_transceiver_or_error.ok());
5317 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5318
5319 caller()->CreateAndSetAndSignalOffer();
5320 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5321 {
5322 MediaExpectations media_expectations;
5323 media_expectations.CalleeExpectsSomeAudio();
5324 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5325 }
5326
5327 audio_transceiver->Stop();
5328 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5329
5330 caller()->CreateAndSetAndSignalOffer();
5331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5332 {
5333 MediaExpectations media_expectations;
5334 media_expectations.CalleeExpectsSomeVideo();
5335 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5336 }
5337}
5338
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005339#ifdef HAVE_SCTP
5340
5341TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5342 EndToEndCallWithBundledSctpDataChannel) {
5343 ASSERT_TRUE(CreatePeerConnectionWrappers());
5344 ConnectFakeSignaling();
5345 caller()->CreateDataChannel();
5346 caller()->AddAudioVideoTracks();
5347 callee()->AddAudioVideoTracks();
5348 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
5349 caller()->CreateAndSetAndSignalOffer();
5350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5351 // Ensure that media and data are multiplexed on the same DTLS transport.
5352 // This only works on Unified Plan, because transports are not exposed in plan
5353 // B.
5354 auto sctp_info = caller()->pc()->GetSctpTransport()->Information();
5355 EXPECT_EQ(sctp_info.dtls_transport(),
5356 caller()->pc()->GetSenders()[0]->dtls_transport());
5357}
5358
5359#endif // HAVE_SCTP
5360
deadbeef1dcb1642017-03-29 21:08:16 -07005361} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005362} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005363
5364#endif // if !defined(THREAD_SANITIZER)