blob: 84c5c886f3113068739f157aa788c97a2c59c848 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Qingsi Wang1dac6d82018-12-12 15:28:47 -080025#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020026#include "api/audio_codecs/builtin_audio_decoder_factory.h"
27#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/media_stream_interface.h"
29#include "api/peer_connection_interface.h"
30#include "api/peer_connection_proxy.h"
31#include "api/rtp_receiver_interface.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080032#include "api/test/loopback_media_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020034#include "api/video_codecs/builtin_video_decoder_factory.h"
35#include "api/video_codecs/builtin_video_encoder_factory.h"
36#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070037#include "call/call.h"
38#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010039#include "logging/rtc_event_log/rtc_event_log_factory.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "media/engine/fake_webrtc_video_engine.h"
42#include "media/engine/webrtc_media_engine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070043#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "p2p/base/mock_async_resolver.h"
45#include "p2p/base/p2p_constants.h"
46#include "p2p/base/port_interface.h"
47#include "p2p/base/test_stun_server.h"
48#include "p2p/base/test_turn_customizer.h"
49#include "p2p/base/test_turn_server.h"
50#include "p2p/client/basic_port_allocator.h"
51#include "pc/dtmf_sender.h"
52#include "pc/local_audio_source.h"
53#include "pc/media_session.h"
54#include "pc/peer_connection.h"
55#include "pc/peer_connection_factory.h"
56#include "pc/rtp_media_utils.h"
57#include "pc/session_description.h"
58#include "pc/test/fake_audio_capture_module.h"
59#include "pc/test/fake_periodic_video_track_source.h"
60#include "pc/test/fake_rtc_certificate_generator.h"
61#include "pc/test/fake_video_track_renderer.h"
62#include "pc/test/mock_peer_connection_observers.h"
63#include "rtc_base/fake_network.h"
64#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020066#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080067#include "rtc_base/test_certificate_verifier.h"
68#include "rtc_base/time_utils.h"
69#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020070#include "system_wrappers/include/metrics.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020071#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070072
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010073namespace webrtc {
74namespace {
75
76using ::cricket::ContentInfo;
77using ::cricket::StreamParams;
78using ::rtc::SocketAddress;
79using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080080using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070081using ::testing::ElementsAre;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070082using ::testing::Return;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080083using ::testing::NiceMock;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070084using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080085using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010086using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080087using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070088
89static const int kDefaultTimeout = 10000;
90static const int kMaxWaitForStatsMs = 3000;
91static const int kMaxWaitForActivationMs = 5000;
92static const int kMaxWaitForFramesMs = 10000;
93// Default number of audio/video frames to wait for before considering a test
94// successful.
95static const int kDefaultExpectedAudioFrameCount = 3;
96static const int kDefaultExpectedVideoFrameCount = 3;
97
deadbeef1dcb1642017-03-29 21:08:16 -070098static const char kDataChannelLabel[] = "data_channel";
99
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700103static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
104
Steve Antonede9ca52017-10-16 13:04:27 -0700105static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
106
deadbeef1dcb1642017-03-29 21:08:16 -0700107// Helper function for constructing offer/answer options to initiate an ICE
108// restart.
109PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
110 PeerConnectionInterface::RTCOfferAnswerOptions options;
111 options.ice_restart = true;
112 return options;
113}
114
deadbeefd8ad7882017-04-18 16:01:17 -0700115// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
116// attribute from received SDP, simulating a legacy endpoint.
117void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
118 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800119 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700120 }
121 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100122 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700123}
124
Seth Hampson5897a6e2018-04-03 11:16:33 -0700125// Removes all stream information besides the stream ids, simulating an
126// endpoint that only signals a=msid lines to convey stream_ids.
127void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
128 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700129 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700130 std::vector<std::string> stream_ids;
131 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700132 const StreamParams& first_stream =
133 content.media_description()->streams()[0];
134 track_id = first_stream.id;
135 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700136 }
137 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700138 StreamParams new_stream;
139 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700140 new_stream.set_stream_ids(stream_ids);
141 content.media_description()->AddStream(new_stream);
142 }
143}
144
zhihuangf8164932017-05-19 13:09:47 -0700145int FindFirstMediaStatsIndexByKind(
146 const std::string& kind,
147 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
148 media_stats_vec) {
149 for (size_t i = 0; i < media_stats_vec.size(); i++) {
150 if (media_stats_vec[i]->kind.ValueToString() == kind) {
151 return i;
152 }
153 }
154 return -1;
155}
156
deadbeef1dcb1642017-03-29 21:08:16 -0700157class SignalingMessageReceiver {
158 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800159 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700160 virtual void ReceiveIceMessage(const std::string& sdp_mid,
161 int sdp_mline_index,
162 const std::string& msg) = 0;
163
164 protected:
165 SignalingMessageReceiver() {}
166 virtual ~SignalingMessageReceiver() {}
167};
168
169class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
170 public:
171 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
172 : expected_media_type_(media_type) {}
173
174 void OnFirstPacketReceived(cricket::MediaType media_type) override {
175 ASSERT_EQ(expected_media_type_, media_type);
176 first_packet_received_ = true;
177 }
178
179 bool first_packet_received() const { return first_packet_received_; }
180
181 virtual ~MockRtpReceiverObserver() {}
182
183 private:
184 bool first_packet_received_ = false;
185 cricket::MediaType expected_media_type_;
186};
187
188// Helper class that wraps a peer connection, observes it, and can accept
189// signaling messages from another wrapper.
190//
191// Uses a fake network, fake A/V capture, and optionally fake
192// encoders/decoders, though they aren't used by default since they don't
193// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700194// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800195// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700196class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800197 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700198 public:
199 // Different factory methods for convenience.
200 // TODO(deadbeef): Could use the pattern of:
201 //
202 // PeerConnectionWrapper =
203 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
204 //
205 // To reduce some code duplication.
206 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
207 const std::string& debug_name,
208 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
209 rtc::Thread* network_thread,
210 rtc::Thread* worker_thread) {
211 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700212 webrtc::PeerConnectionDependencies dependencies(nullptr);
213 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200214 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800215 worker_thread, nullptr,
216 /*media_transport_factory=*/nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700217 delete client;
218 return nullptr;
219 }
220 return client;
221 }
222
deadbeef2f425aa2017-04-14 10:41:32 -0700223 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
224 return peer_connection_factory_.get();
225 }
226
deadbeef1dcb1642017-03-29 21:08:16 -0700227 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
228
229 // If a signaling message receiver is set (via ConnectFakeSignaling), this
230 // will set the whole offer/answer exchange in motion. Just need to wait for
231 // the signaling state to reach "stable".
232 void CreateAndSetAndSignalOffer() {
233 auto offer = CreateOffer();
234 ASSERT_NE(nullptr, offer);
235 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
236 }
237
238 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
239 // when a remote offer is received (via fake signaling) and an answer is
240 // generated. By default, uses default options.
241 void SetOfferAnswerOptions(
242 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
243 offer_answer_options_ = options;
244 }
245
246 // Set a callback to be invoked when SDP is received via the fake signaling
247 // channel, which provides an opportunity to munge (modify) the SDP. This is
248 // used to test SDP being applied that a PeerConnection would normally not
249 // generate, but a non-JSEP endpoint might.
250 void SetReceivedSdpMunger(
251 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100252 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700253 }
254
deadbeefc964d0b2017-04-03 10:03:35 -0700255 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700256 // generated.
257 void SetGeneratedSdpMunger(
258 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100259 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700260 }
261
Seth Hampson2f0d7022018-02-20 11:54:42 -0800262 // Set a callback to be invoked when a remote offer is received via the fake
263 // signaling channel. This provides an opportunity to change the
264 // PeerConnection state before an answer is created and sent to the caller.
265 void SetRemoteOfferHandler(std::function<void()> handler) {
266 remote_offer_handler_ = std::move(handler);
267 }
268
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800269 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
270 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700271 }
272
Steve Antonede9ca52017-10-16 13:04:27 -0700273 // Every ICE connection state in order that has been seen by the observer.
274 std::vector<PeerConnectionInterface::IceConnectionState>
275 ice_connection_state_history() const {
276 return ice_connection_state_history_;
277 }
Steve Anton6f25b092017-10-23 09:39:20 -0700278 void clear_ice_connection_state_history() {
279 ice_connection_state_history_.clear();
280 }
Steve Antonede9ca52017-10-16 13:04:27 -0700281
Jonas Olsson635474e2018-10-18 15:58:17 +0200282 // Every PeerConnection state in order that has been seen by the observer.
283 std::vector<PeerConnectionInterface::PeerConnectionState>
284 peer_connection_state_history() const {
285 return peer_connection_state_history_;
286 }
287
Steve Antonede9ca52017-10-16 13:04:27 -0700288 // Every ICE gathering state in order that has been seen by the observer.
289 std::vector<PeerConnectionInterface::IceGatheringState>
290 ice_gathering_state_history() const {
291 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700292 }
293
Steve Anton15324772018-01-16 10:26:49 -0800294 void AddAudioVideoTracks() {
295 AddAudioTrack();
296 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700297 }
298
Steve Anton74255ff2018-01-24 18:32:57 -0800299 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
300 return AddTrack(CreateLocalAudioTrack());
301 }
deadbeef1dcb1642017-03-29 21:08:16 -0700302
Steve Anton74255ff2018-01-24 18:32:57 -0800303 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
304 return AddTrack(CreateLocalVideoTrack());
305 }
deadbeef1dcb1642017-03-29 21:08:16 -0700306
307 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200308 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700309 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200310 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700311 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200312 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700313 // TODO(perkj): Test audio source when it is implemented. Currently audio
314 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700315 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700316 source);
317 }
318
319 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200320 webrtc::FakePeriodicVideoSource::Config config;
321 config.timestamp_offset_ms = rtc::TimeMillis();
322 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700323 }
324
325 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200326 CreateLocalVideoTrackWithConfig(
327 webrtc::FakePeriodicVideoSource::Config config) {
328 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700329 }
330
331 rtc::scoped_refptr<webrtc::VideoTrackInterface>
332 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200333 webrtc::FakePeriodicVideoSource::Config config;
334 config.rotation = rotation;
Johannes Kron965e7942018-09-13 15:36:20 +0200335 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200336 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700337 }
338
Steve Anton74255ff2018-01-24 18:32:57 -0800339 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
340 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800341 const std::vector<std::string>& stream_ids = {}) {
342 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800343 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800344 return result.MoveValue();
345 }
346
347 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
348 cricket::MediaType media_type) {
349 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
350 for (auto receiver : pc()->GetReceivers()) {
351 if (receiver->media_type() == media_type) {
352 receivers.push_back(receiver);
353 }
354 }
355 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700356 }
357
Seth Hampson2f0d7022018-02-20 11:54:42 -0800358 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
359 cricket::MediaType media_type) {
360 for (auto transceiver : pc()->GetTransceivers()) {
361 if (transceiver->receiver()->media_type() == media_type) {
362 return transceiver;
363 }
364 }
365 return nullptr;
366 }
367
deadbeef1dcb1642017-03-29 21:08:16 -0700368 bool SignalingStateStable() {
369 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
370 }
371
372 void CreateDataChannel() { CreateDataChannel(nullptr); }
373
374 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700375 CreateDataChannel(kDataChannelLabel, init);
376 }
377
378 void CreateDataChannel(const std::string& label,
379 const webrtc::DataChannelInit* init) {
380 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700381 ASSERT_TRUE(data_channel_.get() != nullptr);
382 data_observer_.reset(new MockDataChannelObserver(data_channel_));
383 }
384
385 DataChannelInterface* data_channel() { return data_channel_; }
386 const MockDataChannelObserver* data_observer() const {
387 return data_observer_.get();
388 }
389
390 int audio_frames_received() const {
391 return fake_audio_capture_module_->frames_received();
392 }
393
394 // Takes minimum of video frames received for each track.
395 //
396 // Can be used like:
397 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
398 //
399 // To ensure that all video tracks received at least a certain number of
400 // frames.
401 int min_video_frames_received_per_track() const {
402 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200403 if (fake_video_renderers_.empty()) {
404 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700405 }
deadbeef1dcb1642017-03-29 21:08:16 -0700406
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200407 for (const auto& pair : fake_video_renderers_) {
408 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700409 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200410 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700411 }
412
413 // Returns a MockStatsObserver in a state after stats gathering finished,
414 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700415 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700416 webrtc::MediaStreamTrackInterface* track) {
417 rtc::scoped_refptr<MockStatsObserver> observer(
418 new rtc::RefCountedObject<MockStatsObserver>());
419 EXPECT_TRUE(peer_connection_->GetStats(
420 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
421 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
422 return observer;
423 }
424
425 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700426 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
427 return OldGetStatsForTrack(nullptr);
428 }
429
430 // Synchronously gets stats and returns them. If it times out, fails the test
431 // and returns null.
432 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
433 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
434 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
435 peer_connection_->GetStats(callback);
436 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
437 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700438 }
439
440 int rendered_width() {
441 EXPECT_FALSE(fake_video_renderers_.empty());
442 return fake_video_renderers_.empty()
443 ? 0
444 : fake_video_renderers_.begin()->second->width();
445 }
446
447 int rendered_height() {
448 EXPECT_FALSE(fake_video_renderers_.empty());
449 return fake_video_renderers_.empty()
450 ? 0
451 : fake_video_renderers_.begin()->second->height();
452 }
453
454 double rendered_aspect_ratio() {
455 if (rendered_height() == 0) {
456 return 0.0;
457 }
458 return static_cast<double>(rendered_width()) / rendered_height();
459 }
460
461 webrtc::VideoRotation rendered_rotation() {
462 EXPECT_FALSE(fake_video_renderers_.empty());
463 return fake_video_renderers_.empty()
464 ? webrtc::kVideoRotation_0
465 : fake_video_renderers_.begin()->second->rotation();
466 }
467
468 int local_rendered_width() {
469 return local_video_renderer_ ? local_video_renderer_->width() : 0;
470 }
471
472 int local_rendered_height() {
473 return local_video_renderer_ ? local_video_renderer_->height() : 0;
474 }
475
476 double local_rendered_aspect_ratio() {
477 if (local_rendered_height() == 0) {
478 return 0.0;
479 }
480 return static_cast<double>(local_rendered_width()) /
481 local_rendered_height();
482 }
483
484 size_t number_of_remote_streams() {
485 if (!pc()) {
486 return 0;
487 }
488 return pc()->remote_streams()->count();
489 }
490
491 StreamCollectionInterface* remote_streams() const {
492 if (!pc()) {
493 ADD_FAILURE();
494 return nullptr;
495 }
496 return pc()->remote_streams();
497 }
498
499 StreamCollectionInterface* local_streams() {
500 if (!pc()) {
501 ADD_FAILURE();
502 return nullptr;
503 }
504 return pc()->local_streams();
505 }
506
507 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
508 return pc()->signaling_state();
509 }
510
511 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
512 return pc()->ice_connection_state();
513 }
514
515 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
516 return pc()->ice_gathering_state();
517 }
518
519 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
520 // GetReceivers. They're updated automatically when a remote offer/answer
521 // from the fake signaling channel is applied, or when
522 // ResetRtpReceiverObservers below is called.
523 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
524 rtp_receiver_observers() {
525 return rtp_receiver_observers_;
526 }
527
528 void ResetRtpReceiverObservers() {
529 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100530 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
531 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700532 std::unique_ptr<MockRtpReceiverObserver> observer(
533 new MockRtpReceiverObserver(receiver->media_type()));
534 receiver->SetObserver(observer.get());
535 rtp_receiver_observers_.push_back(std::move(observer));
536 }
537 }
538
Steve Antonede9ca52017-10-16 13:04:27 -0700539 rtc::FakeNetworkManager* network() const {
540 return fake_network_manager_.get();
541 }
542 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
543
Qingsi Wang7685e862018-06-11 20:15:46 -0700544 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
545 return event_log_factory_;
546 }
547
deadbeef1dcb1642017-03-29 21:08:16 -0700548 private:
549 explicit PeerConnectionWrapper(const std::string& debug_name)
550 : debug_name_(debug_name) {}
551
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800552 bool Init(
553 const PeerConnectionFactory::Options* options,
554 const PeerConnectionInterface::RTCConfiguration* config,
555 webrtc::PeerConnectionDependencies dependencies,
556 rtc::Thread* network_thread,
557 rtc::Thread* worker_thread,
558 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
559 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700560 // There's an error in this test code if Init ends up being called twice.
561 RTC_DCHECK(!peer_connection_);
562 RTC_DCHECK(!peer_connection_factory_);
563
564 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700565 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700566
567 std::unique_ptr<cricket::PortAllocator> port_allocator(
568 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700569 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700570 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
571 if (!fake_audio_capture_module_) {
572 return false;
573 }
deadbeef1dcb1642017-03-29 21:08:16 -0700574 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700575
576 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
577 pc_factory_dependencies.network_thread = network_thread;
578 pc_factory_dependencies.worker_thread = worker_thread;
579 pc_factory_dependencies.signaling_thread = signaling_thread;
580 pc_factory_dependencies.media_engine =
581 cricket::WebRtcMediaEngineFactory::Create(
582 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
583 fake_audio_capture_module_),
584 webrtc::CreateBuiltinAudioEncoderFactory(),
585 webrtc::CreateBuiltinAudioDecoderFactory(),
586 webrtc::CreateBuiltinVideoEncoderFactory(),
Qingsi Wang59844ce2018-11-01 04:45:53 +0000587 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -0700588 webrtc::AudioProcessingBuilder().Create());
589 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
590 if (event_log_factory) {
591 event_log_factory_ = event_log_factory.get();
592 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
593 } else {
594 pc_factory_dependencies.event_log_factory =
595 webrtc::CreateRtcEventLogFactory();
596 }
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800597 if (media_transport_factory) {
598 pc_factory_dependencies.media_transport_factory =
599 std::move(media_transport_factory);
600 }
Qingsi Wang7685e862018-06-11 20:15:46 -0700601 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
602 std::move(pc_factory_dependencies));
603
deadbeef1dcb1642017-03-29 21:08:16 -0700604 if (!peer_connection_factory_) {
605 return false;
606 }
607 if (options) {
608 peer_connection_factory_->SetOptions(*options);
609 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800610 if (config) {
611 sdp_semantics_ = config->sdp_semantics;
612 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700613
614 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200615 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700616 return peer_connection_.get() != nullptr;
617 }
618
619 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700620 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700621 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700622 PeerConnectionInterface::RTCConfiguration modified_config;
623 // If |config| is null, this will result in a default configuration being
624 // used.
625 if (config) {
626 modified_config = *config;
627 }
628 // Disable resolution adaptation; we don't want it interfering with the
629 // test results.
630 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
631 // ratios and not specific resolutions, is this even necessary?
632 modified_config.set_cpu_adaptation(false);
633
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700634 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700635 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700636 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700637 }
638
639 void set_signaling_message_receiver(
640 SignalingMessageReceiver* signaling_message_receiver) {
641 signaling_message_receiver_ = signaling_message_receiver;
642 }
643
644 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
645
Steve Antonede9ca52017-10-16 13:04:27 -0700646 void set_signal_ice_candidates(bool signal) {
647 signal_ice_candidates_ = signal;
648 }
649
deadbeef1dcb1642017-03-29 21:08:16 -0700650 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200651 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700652 // Set max frame rate to 10fps to reduce the risk of test flakiness.
653 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200654 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700655
Niels Möller5c7efe72018-05-11 10:34:46 +0200656 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200657 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
658 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700659 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200660 peer_connection_factory_->CreateVideoTrack(
661 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700662 if (!local_video_renderer_) {
663 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
664 }
665 return track;
666 }
667
668 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100669 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800670 std::unique_ptr<SessionDescriptionInterface> desc =
671 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700672 if (received_sdp_munger_) {
673 received_sdp_munger_(desc->description());
674 }
675
676 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
677 // Setting a remote description may have changed the number of receivers,
678 // so reset the receiver observers.
679 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800680 if (remote_offer_handler_) {
681 remote_offer_handler_();
682 }
deadbeef1dcb1642017-03-29 21:08:16 -0700683 auto answer = CreateAnswer();
684 ASSERT_NE(nullptr, answer);
685 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
686 }
687
688 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100689 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800690 std::unique_ptr<SessionDescriptionInterface> desc =
691 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700692 if (received_sdp_munger_) {
693 received_sdp_munger_(desc->description());
694 }
695
696 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
697 // Set the RtpReceiverObserver after receivers are created.
698 ResetRtpReceiverObservers();
699 }
700
701 // Returns null on failure.
702 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
703 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
704 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
705 pc()->CreateOffer(observer, offer_answer_options_);
706 return WaitForDescriptionFromObserver(observer);
707 }
708
709 // Returns null on failure.
710 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
711 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
712 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
713 pc()->CreateAnswer(observer, offer_answer_options_);
714 return WaitForDescriptionFromObserver(observer);
715 }
716
717 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100718 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700719 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
720 if (!observer->result()) {
721 return nullptr;
722 }
723 auto description = observer->MoveDescription();
724 if (generated_sdp_munger_) {
725 generated_sdp_munger_(description->description());
726 }
727 return description;
728 }
729
730 // Setting the local description and sending the SDP message over the fake
731 // signaling channel are combined into the same method because the SDP
732 // message needs to be sent as soon as SetLocalDescription finishes, without
733 // waiting for the observer to be called. This ensures that ICE candidates
734 // don't outrace the description.
735 bool SetLocalDescriptionAndSendSdpMessage(
736 std::unique_ptr<SessionDescriptionInterface> desc) {
737 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
738 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100739 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800740 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700741 std::string sdp;
742 EXPECT_TRUE(desc->ToString(&sdp));
743 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800744 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
745 RemoveUnusedVideoRenderers();
746 }
deadbeef1dcb1642017-03-29 21:08:16 -0700747 // As mentioned above, we need to send the message immediately after
748 // SetLocalDescription.
749 SendSdpMessage(type, sdp);
750 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
751 return true;
752 }
753
754 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
755 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
756 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100757 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700758 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800759 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
760 RemoveUnusedVideoRenderers();
761 }
deadbeef1dcb1642017-03-29 21:08:16 -0700762 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
763 return observer->result();
764 }
765
Seth Hampson2f0d7022018-02-20 11:54:42 -0800766 // This is a work around to remove unused fake_video_renderers from
767 // transceivers that have either stopped or are no longer receiving.
768 void RemoveUnusedVideoRenderers() {
769 auto transceivers = pc()->GetTransceivers();
770 for (auto& transceiver : transceivers) {
771 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
772 continue;
773 }
774 // Remove fake video renderers from any stopped transceivers.
775 if (transceiver->stopped()) {
776 auto it =
777 fake_video_renderers_.find(transceiver->receiver()->track()->id());
778 if (it != fake_video_renderers_.end()) {
779 fake_video_renderers_.erase(it);
780 }
781 }
782 // Remove fake video renderers from any transceivers that are no longer
783 // receiving.
784 if ((transceiver->current_direction() &&
785 !webrtc::RtpTransceiverDirectionHasRecv(
786 *transceiver->current_direction()))) {
787 auto it =
788 fake_video_renderers_.find(transceiver->receiver()->track()->id());
789 if (it != fake_video_renderers_.end()) {
790 fake_video_renderers_.erase(it);
791 }
792 }
793 }
794 }
795
deadbeef1dcb1642017-03-29 21:08:16 -0700796 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
797 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800798 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700799 if (signaling_delay_ms_ == 0) {
800 RelaySdpMessageIfReceiverExists(type, msg);
801 } else {
802 invoker_.AsyncInvokeDelayed<void>(
803 RTC_FROM_HERE, rtc::Thread::Current(),
804 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
805 this, type, msg),
806 signaling_delay_ms_);
807 }
808 }
809
Steve Antona3a92c22017-12-07 10:27:41 -0800810 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700811 if (signaling_message_receiver_) {
812 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
813 }
814 }
815
816 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
817 // default).
818 void SendIceMessage(const std::string& sdp_mid,
819 int sdp_mline_index,
820 const std::string& msg) {
821 if (signaling_delay_ms_ == 0) {
822 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
823 } else {
824 invoker_.AsyncInvokeDelayed<void>(
825 RTC_FROM_HERE, rtc::Thread::Current(),
826 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
827 this, sdp_mid, sdp_mline_index, msg),
828 signaling_delay_ms_);
829 }
830 }
831
832 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
833 int sdp_mline_index,
834 const std::string& msg) {
835 if (signaling_message_receiver_) {
836 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
837 msg);
838 }
839 }
840
841 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800842 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
843 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700844 HandleIncomingOffer(msg);
845 } else {
846 HandleIncomingAnswer(msg);
847 }
848 }
849
850 void ReceiveIceMessage(const std::string& sdp_mid,
851 int sdp_mline_index,
852 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100853 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700854 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
855 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
856 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
857 }
858
859 // PeerConnectionObserver callbacks.
860 void OnSignalingChange(
861 webrtc::PeerConnectionInterface::SignalingState new_state) override {
862 EXPECT_EQ(pc()->signaling_state(), new_state);
863 }
Steve Anton15324772018-01-16 10:26:49 -0800864 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
865 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
866 streams) override {
867 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
868 rtc::scoped_refptr<VideoTrackInterface> video_track(
869 static_cast<VideoTrackInterface*>(receiver->track().get()));
870 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700871 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800872 fake_video_renderers_[video_track->id()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200873 absl::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700874 }
875 }
Steve Anton15324772018-01-16 10:26:49 -0800876 void OnRemoveTrack(
877 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
878 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
879 auto it = fake_video_renderers_.find(receiver->track()->id());
880 RTC_DCHECK(it != fake_video_renderers_.end());
881 fake_video_renderers_.erase(it);
882 }
883 }
deadbeef1dcb1642017-03-29 21:08:16 -0700884 void OnRenegotiationNeeded() override {}
885 void OnIceConnectionChange(
886 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
887 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700888 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700889 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200890 void OnConnectionChange(
891 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
892 peer_connection_state_history_.push_back(new_state);
893 }
894
deadbeef1dcb1642017-03-29 21:08:16 -0700895 void OnIceGatheringChange(
896 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700897 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700898 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700899 }
900 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100901 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700902
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800903 if (remote_async_resolver_) {
904 const auto& local_candidate = candidate->candidate();
905 const auto& mdns_responder = network()->GetMdnsResponderForTesting();
906 if (local_candidate.address().IsUnresolvedIP()) {
907 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
908 rtc::SocketAddress resolved_addr(local_candidate.address());
909 const auto resolved_ip = mdns_responder->GetMappedAddressForName(
910 local_candidate.address().hostname());
911 RTC_DCHECK(!resolved_ip.IsNil());
912 resolved_addr.SetResolvedIP(resolved_ip);
913 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
914 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
915 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700916 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700917 }
918
deadbeef1dcb1642017-03-29 21:08:16 -0700919 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800920 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700921 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700922 // Remote party may be deleted.
923 return;
924 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800925 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeef1dcb1642017-03-29 21:08:16 -0700926 }
927 void OnDataChannel(
928 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100929 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700930 data_channel_ = data_channel;
931 data_observer_.reset(new MockDataChannelObserver(data_channel));
932 }
933
deadbeef1dcb1642017-03-29 21:08:16 -0700934 std::string debug_name_;
935
936 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
937
938 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
939 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
940 peer_connection_factory_;
941
Steve Antonede9ca52017-10-16 13:04:27 -0700942 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700943 // Needed to keep track of number of frames sent.
944 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
945 // Needed to keep track of number of frames received.
946 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
947 fake_video_renderers_;
948 // Needed to ensure frames aren't received for removed tracks.
949 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
950 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700951
952 // For remote peer communication.
953 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
954 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700955 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700956
Niels Möller5c7efe72018-05-11 10:34:46 +0200957 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700958 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200959 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
960 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700961 // |local_video_renderer_| attached to the first created local video track.
962 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
963
Seth Hampson2f0d7022018-02-20 11:54:42 -0800964 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700965 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
966 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
967 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800968 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800969 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -0700970 rtc::scoped_refptr<DataChannelInterface> data_channel_;
971 std::unique_ptr<MockDataChannelObserver> data_observer_;
972
973 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
974
Steve Antonede9ca52017-10-16 13:04:27 -0700975 std::vector<PeerConnectionInterface::IceConnectionState>
976 ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +0200977 std::vector<PeerConnectionInterface::PeerConnectionState>
978 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -0700979 std::vector<PeerConnectionInterface::IceGatheringState>
980 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700981
Qingsi Wang7685e862018-06-11 20:15:46 -0700982 webrtc::FakeRtcEventLogFactory* event_log_factory_;
983
deadbeef1dcb1642017-03-29 21:08:16 -0700984 rtc::AsyncInvoker invoker_;
985
Seth Hampson2f0d7022018-02-20 11:54:42 -0800986 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700987};
988
Elad Alon99c3fe52017-10-13 16:29:40 +0200989class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
990 public:
991 virtual ~MockRtcEventLogOutput() = default;
992 MOCK_CONST_METHOD0(IsActive, bool());
993 MOCK_METHOD1(Write, bool(const std::string&));
994};
995
Seth Hampson2f0d7022018-02-20 11:54:42 -0800996// This helper object is used for both specifying how many audio/video frames
997// are expected to be received for a caller/callee. It provides helper functions
998// to specify these expectations. The object initially starts in a state of no
999// expectations.
1000class MediaExpectations {
1001 public:
1002 enum ExpectFrames {
1003 kExpectSomeFrames,
1004 kExpectNoFrames,
1005 kNoExpectation,
1006 };
1007
1008 void ExpectBidirectionalAudioAndVideo() {
1009 ExpectBidirectionalAudio();
1010 ExpectBidirectionalVideo();
1011 }
1012
1013 void ExpectBidirectionalAudio() {
1014 CallerExpectsSomeAudio();
1015 CalleeExpectsSomeAudio();
1016 }
1017
1018 void ExpectNoAudio() {
1019 CallerExpectsNoAudio();
1020 CalleeExpectsNoAudio();
1021 }
1022
1023 void ExpectBidirectionalVideo() {
1024 CallerExpectsSomeVideo();
1025 CalleeExpectsSomeVideo();
1026 }
1027
1028 void ExpectNoVideo() {
1029 CallerExpectsNoVideo();
1030 CalleeExpectsNoVideo();
1031 }
1032
1033 void CallerExpectsSomeAudioAndVideo() {
1034 CallerExpectsSomeAudio();
1035 CallerExpectsSomeVideo();
1036 }
1037
1038 void CalleeExpectsSomeAudioAndVideo() {
1039 CalleeExpectsSomeAudio();
1040 CalleeExpectsSomeVideo();
1041 }
1042
1043 // Caller's audio functions.
1044 void CallerExpectsSomeAudio(
1045 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1046 caller_audio_expectation_ = kExpectSomeFrames;
1047 caller_audio_frames_expected_ = expected_audio_frames;
1048 }
1049
1050 void CallerExpectsNoAudio() {
1051 caller_audio_expectation_ = kExpectNoFrames;
1052 caller_audio_frames_expected_ = 0;
1053 }
1054
1055 // Caller's video functions.
1056 void CallerExpectsSomeVideo(
1057 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1058 caller_video_expectation_ = kExpectSomeFrames;
1059 caller_video_frames_expected_ = expected_video_frames;
1060 }
1061
1062 void CallerExpectsNoVideo() {
1063 caller_video_expectation_ = kExpectNoFrames;
1064 caller_video_frames_expected_ = 0;
1065 }
1066
1067 // Callee's audio functions.
1068 void CalleeExpectsSomeAudio(
1069 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1070 callee_audio_expectation_ = kExpectSomeFrames;
1071 callee_audio_frames_expected_ = expected_audio_frames;
1072 }
1073
1074 void CalleeExpectsNoAudio() {
1075 callee_audio_expectation_ = kExpectNoFrames;
1076 callee_audio_frames_expected_ = 0;
1077 }
1078
1079 // Callee's video functions.
1080 void CalleeExpectsSomeVideo(
1081 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1082 callee_video_expectation_ = kExpectSomeFrames;
1083 callee_video_frames_expected_ = expected_video_frames;
1084 }
1085
1086 void CalleeExpectsNoVideo() {
1087 callee_video_expectation_ = kExpectNoFrames;
1088 callee_video_frames_expected_ = 0;
1089 }
1090
1091 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1092 ExpectFrames caller_video_expectation_ = kNoExpectation;
1093 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1094 ExpectFrames callee_video_expectation_ = kNoExpectation;
1095 int caller_audio_frames_expected_ = 0;
1096 int caller_video_frames_expected_ = 0;
1097 int callee_audio_frames_expected_ = 0;
1098 int callee_video_frames_expected_ = 0;
1099};
1100
deadbeef1dcb1642017-03-29 21:08:16 -07001101// Tests two PeerConnections connecting to each other end-to-end, using a
1102// virtual network, fake A/V capture and fake encoder/decoders. The
1103// PeerConnections share the threads/socket servers, but use separate versions
1104// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001105class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001106 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001107 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1108 : sdp_semantics_(sdp_semantics),
1109 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001110 fss_(new rtc::FirewallSocketServer(ss_.get())),
1111 network_thread_(new rtc::Thread(fss_.get())),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001112 worker_thread_(rtc::Thread::Create()),
1113 loopback_media_transports_(network_thread_.get()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001114 network_thread_->SetName("PCNetworkThread", this);
1115 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001116 RTC_CHECK(network_thread_->Start());
1117 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001118 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001119 }
1120
Seth Hampson2f0d7022018-02-20 11:54:42 -08001121 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001122 // The PeerConnections should deleted before the TurnCustomizers.
1123 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1124 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1125 // that the TurnCustomizer outlives the life of the PeerConnection or else
1126 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001127 if (caller_) {
1128 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001129 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001130 }
1131 if (callee_) {
1132 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001133 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001134 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001135
1136 // If turn servers were created for the test they need to be destroyed on
1137 // the network thread.
1138 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1139 turn_servers_.clear();
1140 turn_customizers_.clear();
1141 });
deadbeef1dcb1642017-03-29 21:08:16 -07001142 }
1143
1144 bool SignalingStateStable() {
1145 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1146 }
1147
deadbeef71452802017-05-07 17:21:01 -07001148 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001149 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1150 // are connected. This is an important distinction. Once we have separate
1151 // ICE and DTLS state, this check needs to use the DTLS state.
1152 return (callee()->ice_connection_state() ==
1153 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1154 callee()->ice_connection_state() ==
1155 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1156 (caller()->ice_connection_state() ==
1157 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1158 caller()->ice_connection_state() ==
1159 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001160 }
1161
Qingsi Wang7685e862018-06-11 20:15:46 -07001162 // When |event_log_factory| is null, the default implementation of the event
1163 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001164 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1165 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001166 const PeerConnectionFactory::Options* options,
1167 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001168 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001169 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
1170 std::unique_ptr<webrtc::MediaTransportFactory> media_transport_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001171 RTCConfiguration modified_config;
1172 if (config) {
1173 modified_config = *config;
1174 }
Steve Anton3acffc32018-04-12 17:21:03 -07001175 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001176 if (!dependencies.cert_generator) {
1177 dependencies.cert_generator =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001178 absl::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001179 }
1180 std::unique_ptr<PeerConnectionWrapper> client(
1181 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001182
Niels Möllerf06f9232018-08-07 12:32:18 +02001183 if (!client->Init(options, &modified_config, std::move(dependencies),
1184 network_thread_.get(), worker_thread_.get(),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001185 std::move(event_log_factory),
1186 std::move(media_transport_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001187 return nullptr;
1188 }
1189 return client;
1190 }
1191
Qingsi Wang7685e862018-06-11 20:15:46 -07001192 std::unique_ptr<PeerConnectionWrapper>
1193 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1194 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001195 const PeerConnectionFactory::Options* options,
1196 const RTCConfiguration* config,
1197 webrtc::PeerConnectionDependencies dependencies) {
1198 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1199 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möllerf06f9232018-08-07 12:32:18 +02001200 return CreatePeerConnectionWrapper(debug_name, options, config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001201 std::move(dependencies),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001202 std::move(event_log_factory),
1203 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001204 }
1205
deadbeef1dcb1642017-03-29 21:08:16 -07001206 bool CreatePeerConnectionWrappers() {
1207 return CreatePeerConnectionWrappersWithConfig(
1208 PeerConnectionInterface::RTCConfiguration(),
1209 PeerConnectionInterface::RTCConfiguration());
1210 }
1211
Steve Anton3acffc32018-04-12 17:21:03 -07001212 bool CreatePeerConnectionWrappersWithSdpSemantics(
1213 SdpSemantics caller_semantics,
1214 SdpSemantics callee_semantics) {
1215 // Can't specify the sdp_semantics in the passed-in configuration since it
1216 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1217 // stored in sdp_semantics_. So get around this by modifying the instance
1218 // variable before calling CreatePeerConnectionWrapper for the caller and
1219 // callee PeerConnections.
1220 SdpSemantics original_semantics = sdp_semantics_;
1221 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001222 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001223 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001224 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001225 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001226 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001227 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001228 nullptr, /*media_transport_factory=*/nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001229 sdp_semantics_ = original_semantics;
1230 return caller_ && callee_;
1231 }
1232
deadbeef1dcb1642017-03-29 21:08:16 -07001233 bool CreatePeerConnectionWrappersWithConfig(
1234 const PeerConnectionInterface::RTCConfiguration& caller_config,
1235 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001236 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001237 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001238 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1239 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001240 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001241 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001242 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1243 /*media_transport_factory=*/nullptr);
1244 return caller_ && callee_;
1245 }
1246
1247 bool CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
1248 const PeerConnectionInterface::RTCConfiguration& caller_config,
1249 const PeerConnectionInterface::RTCConfiguration& callee_config,
1250 std::unique_ptr<webrtc::MediaTransportFactory> caller_factory,
1251 std::unique_ptr<webrtc::MediaTransportFactory> callee_factory) {
1252 caller_ =
1253 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1254 webrtc::PeerConnectionDependencies(nullptr),
1255 nullptr, std::move(caller_factory));
1256 callee_ =
1257 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1258 webrtc::PeerConnectionDependencies(nullptr),
1259 nullptr, std::move(callee_factory));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001260 return caller_ && callee_;
1261 }
1262
1263 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1264 const PeerConnectionInterface::RTCConfiguration& caller_config,
1265 webrtc::PeerConnectionDependencies caller_dependencies,
1266 const PeerConnectionInterface::RTCConfiguration& callee_config,
1267 webrtc::PeerConnectionDependencies callee_dependencies) {
1268 caller_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001269 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001270 std::move(caller_dependencies), nullptr,
1271 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001272 callee_ =
Niels Möllerf06f9232018-08-07 12:32:18 +02001273 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001274 std::move(callee_dependencies), nullptr,
1275 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001276 return caller_ && callee_;
1277 }
1278
1279 bool CreatePeerConnectionWrappersWithOptions(
1280 const PeerConnectionFactory::Options& caller_options,
1281 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001282 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001283 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001284 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1285 /*media_transport_factory=*/nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001286 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001287 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001288 webrtc::PeerConnectionDependencies(nullptr), nullptr,
1289 /*media_transport_factory=*/nullptr);
Qingsi Wang7685e862018-06-11 20:15:46 -07001290 return caller_ && callee_;
1291 }
1292
1293 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1294 PeerConnectionInterface::RTCConfiguration default_config;
1295 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001296 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001297 webrtc::PeerConnectionDependencies(nullptr));
1298 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001299 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001300 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001301 return caller_ && callee_;
1302 }
1303
Seth Hampson2f0d7022018-02-20 11:54:42 -08001304 std::unique_ptr<PeerConnectionWrapper>
1305 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001306 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1307 new FakeRTCCertificateGenerator());
1308 cert_generator->use_alternate_key();
1309
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001310 webrtc::PeerConnectionDependencies dependencies(nullptr);
1311 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +02001312 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001313 std::move(dependencies), nullptr,
1314 /*media_transport_factory=*/nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001315 }
1316
Seth Hampsonaed71642018-06-11 07:41:32 -07001317 cricket::TestTurnServer* CreateTurnServer(
1318 rtc::SocketAddress internal_address,
1319 rtc::SocketAddress external_address,
1320 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1321 const std::string& common_name = "test turn server") {
1322 rtc::Thread* thread = network_thread();
1323 std::unique_ptr<cricket::TestTurnServer> turn_server =
1324 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1325 RTC_FROM_HERE,
1326 [thread, internal_address, external_address, type, common_name] {
Karl Wiberg918f50c2018-07-05 11:40:33 +02001327 return absl::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001328 thread, internal_address, external_address, type,
1329 /*ignore_bad_certs=*/true, common_name);
1330 });
1331 turn_servers_.push_back(std::move(turn_server));
1332 // Interactions with the turn server should be done on the network thread.
1333 return turn_servers_.back().get();
1334 }
1335
1336 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1337 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1338 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1339 RTC_FROM_HERE,
Karl Wiberg918f50c2018-07-05 11:40:33 +02001340 [] { return absl::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001341 turn_customizers_.push_back(std::move(turn_customizer));
1342 // Interactions with the turn customizer should be done on the network
1343 // thread.
1344 return turn_customizers_.back().get();
1345 }
1346
1347 // Checks that the function counters for a TestTurnCustomizer are greater than
1348 // 0.
1349 void ExpectTurnCustomizerCountersIncremented(
1350 cricket::TestTurnCustomizer* turn_customizer) {
1351 unsigned int allow_channel_data_counter =
1352 network_thread()->Invoke<unsigned int>(
1353 RTC_FROM_HERE, [turn_customizer] {
1354 return turn_customizer->allow_channel_data_cnt_;
1355 });
1356 EXPECT_GT(allow_channel_data_counter, 0u);
1357 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1358 RTC_FROM_HERE,
1359 [turn_customizer] { return turn_customizer->modify_cnt_; });
1360 EXPECT_GT(modify_counter, 0u);
1361 }
1362
deadbeef1dcb1642017-03-29 21:08:16 -07001363 // Once called, SDP blobs and ICE candidates will be automatically signaled
1364 // between PeerConnections.
1365 void ConnectFakeSignaling() {
1366 caller_->set_signaling_message_receiver(callee_.get());
1367 callee_->set_signaling_message_receiver(caller_.get());
1368 }
1369
Steve Antonede9ca52017-10-16 13:04:27 -07001370 // Once called, SDP blobs will be automatically signaled between
1371 // PeerConnections. Note that ICE candidates will not be signaled unless they
1372 // are in the exchanged SDP blobs.
1373 void ConnectFakeSignalingForSdpOnly() {
1374 ConnectFakeSignaling();
1375 SetSignalIceCandidates(false);
1376 }
1377
deadbeef1dcb1642017-03-29 21:08:16 -07001378 void SetSignalingDelayMs(int delay_ms) {
1379 caller_->set_signaling_delay_ms(delay_ms);
1380 callee_->set_signaling_delay_ms(delay_ms);
1381 }
1382
Steve Antonede9ca52017-10-16 13:04:27 -07001383 void SetSignalIceCandidates(bool signal) {
1384 caller_->set_signal_ice_candidates(signal);
1385 callee_->set_signal_ice_candidates(signal);
1386 }
1387
deadbeef1dcb1642017-03-29 21:08:16 -07001388 // Messages may get lost on the unreliable DataChannel, so we send multiple
1389 // times to avoid test flakiness.
1390 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1391 const std::string& data,
1392 int retries) {
1393 for (int i = 0; i < retries; ++i) {
1394 dc->Send(DataBuffer(data));
1395 }
1396 }
1397
1398 rtc::Thread* network_thread() { return network_thread_.get(); }
1399
1400 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1401
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001402 webrtc::MediaTransportPair* loopback_media_transports() {
1403 return &loopback_media_transports_;
1404 }
1405
deadbeef1dcb1642017-03-29 21:08:16 -07001406 PeerConnectionWrapper* caller() { return caller_.get(); }
1407
1408 // Set the |caller_| to the |wrapper| passed in and return the
1409 // original |caller_|.
1410 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1411 PeerConnectionWrapper* wrapper) {
1412 PeerConnectionWrapper* old = caller_.release();
1413 caller_.reset(wrapper);
1414 return old;
1415 }
1416
1417 PeerConnectionWrapper* callee() { return callee_.get(); }
1418
1419 // Set the |callee_| to the |wrapper| passed in and return the
1420 // original |callee_|.
1421 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1422 PeerConnectionWrapper* wrapper) {
1423 PeerConnectionWrapper* old = callee_.release();
1424 callee_.reset(wrapper);
1425 return old;
1426 }
1427
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001428 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1429 network_thread()->Invoke<void>(
1430 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1431 caller()->port_allocator(), caller_flags));
1432 network_thread()->Invoke<void>(
1433 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1434 callee()->port_allocator(), callee_flags));
1435 }
1436
Steve Antonede9ca52017-10-16 13:04:27 -07001437 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1438
Seth Hampson2f0d7022018-02-20 11:54:42 -08001439 // Expects the provided number of new frames to be received within
1440 // kMaxWaitForFramesMs. The new expected frames are specified in
1441 // |media_expectations|. Returns false if any of the expectations were
1442 // not met.
1443 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1444 // First initialize the expected frame counts based upon the current
1445 // frame count.
1446 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1447 if (media_expectations.caller_audio_expectation_ ==
1448 MediaExpectations::kExpectSomeFrames) {
1449 total_caller_audio_frames_expected +=
1450 media_expectations.caller_audio_frames_expected_;
1451 }
1452 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001453 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001454 if (media_expectations.caller_video_expectation_ ==
1455 MediaExpectations::kExpectSomeFrames) {
1456 total_caller_video_frames_expected +=
1457 media_expectations.caller_video_frames_expected_;
1458 }
1459 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1460 if (media_expectations.callee_audio_expectation_ ==
1461 MediaExpectations::kExpectSomeFrames) {
1462 total_callee_audio_frames_expected +=
1463 media_expectations.callee_audio_frames_expected_;
1464 }
1465 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001466 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001467 if (media_expectations.callee_video_expectation_ ==
1468 MediaExpectations::kExpectSomeFrames) {
1469 total_callee_video_frames_expected +=
1470 media_expectations.callee_video_frames_expected_;
1471 }
deadbeef1dcb1642017-03-29 21:08:16 -07001472
Seth Hampson2f0d7022018-02-20 11:54:42 -08001473 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001474 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001475 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001476 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001477 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001478 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001479 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001480 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001481 total_callee_video_frames_expected,
1482 kMaxWaitForFramesMs);
1483 bool expectations_correct =
1484 caller()->audio_frames_received() >=
1485 total_caller_audio_frames_expected &&
1486 caller()->min_video_frames_received_per_track() >=
1487 total_caller_video_frames_expected &&
1488 callee()->audio_frames_received() >=
1489 total_callee_audio_frames_expected &&
1490 callee()->min_video_frames_received_per_track() >=
1491 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001492
Seth Hampson2f0d7022018-02-20 11:54:42 -08001493 // After the combined wait, print out a more detailed message upon
1494 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001495 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001496 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001497 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001498 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001499 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001500 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001501 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001502 total_callee_video_frames_expected);
1503
1504 // We want to make sure nothing unexpected was received.
1505 if (media_expectations.caller_audio_expectation_ ==
1506 MediaExpectations::kExpectNoFrames) {
1507 EXPECT_EQ(caller()->audio_frames_received(),
1508 total_caller_audio_frames_expected);
1509 if (caller()->audio_frames_received() !=
1510 total_caller_audio_frames_expected) {
1511 expectations_correct = false;
1512 }
1513 }
1514 if (media_expectations.caller_video_expectation_ ==
1515 MediaExpectations::kExpectNoFrames) {
1516 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1517 total_caller_video_frames_expected);
1518 if (caller()->min_video_frames_received_per_track() !=
1519 total_caller_video_frames_expected) {
1520 expectations_correct = false;
1521 }
1522 }
1523 if (media_expectations.callee_audio_expectation_ ==
1524 MediaExpectations::kExpectNoFrames) {
1525 EXPECT_EQ(callee()->audio_frames_received(),
1526 total_callee_audio_frames_expected);
1527 if (callee()->audio_frames_received() !=
1528 total_callee_audio_frames_expected) {
1529 expectations_correct = false;
1530 }
1531 }
1532 if (media_expectations.callee_video_expectation_ ==
1533 MediaExpectations::kExpectNoFrames) {
1534 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1535 total_callee_video_frames_expected);
1536 if (callee()->min_video_frames_received_per_track() !=
1537 total_callee_video_frames_expected) {
1538 expectations_correct = false;
1539 }
1540 }
1541 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001542 }
1543
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001544 void TestNegotiatedCipherSuite(
1545 const PeerConnectionFactory::Options& caller_options,
1546 const PeerConnectionFactory::Options& callee_options,
1547 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001548 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1549 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001550 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001551 caller()->AddAudioVideoTracks();
1552 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001553 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001554 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001555 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001556 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001557 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00001558 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001559 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1560 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001561 }
1562
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001563 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1564 bool remote_gcm_enabled,
1565 int expected_cipher_suite) {
1566 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001567 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1568 local_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001569 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001570 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1571 remote_gcm_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001572 TestNegotiatedCipherSuite(caller_options, callee_options,
1573 expected_cipher_suite);
1574 }
1575
Seth Hampson2f0d7022018-02-20 11:54:42 -08001576 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001577 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001578
deadbeef1dcb1642017-03-29 21:08:16 -07001579 private:
1580 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001581 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001582 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001583 // |network_thread_| and |worker_thread_| are used by both
1584 // |caller_| and |callee_| so they must be destroyed
1585 // later.
1586 std::unique_ptr<rtc::Thread> network_thread_;
1587 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001588 // The turn servers and turn customizers should be accessed & deleted on the
1589 // network thread to avoid a race with the socket read/write that occurs
1590 // on the network thread.
1591 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1592 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001593 webrtc::MediaTransportPair loopback_media_transports_;
deadbeef1dcb1642017-03-29 21:08:16 -07001594 std::unique_ptr<PeerConnectionWrapper> caller_;
1595 std::unique_ptr<PeerConnectionWrapper> callee_;
1596};
1597
Seth Hampson2f0d7022018-02-20 11:54:42 -08001598class PeerConnectionIntegrationTest
1599 : public PeerConnectionIntegrationBaseTest,
1600 public ::testing::WithParamInterface<SdpSemantics> {
1601 protected:
1602 PeerConnectionIntegrationTest()
1603 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1604};
1605
1606class PeerConnectionIntegrationTestPlanB
1607 : public PeerConnectionIntegrationBaseTest {
1608 protected:
1609 PeerConnectionIntegrationTestPlanB()
1610 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1611};
1612
1613class PeerConnectionIntegrationTestUnifiedPlan
1614 : public PeerConnectionIntegrationBaseTest {
1615 protected:
1616 PeerConnectionIntegrationTestUnifiedPlan()
1617 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1618};
1619
deadbeef1dcb1642017-03-29 21:08:16 -07001620// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1621// includes testing that the callback is invoked if an observer is connected
1622// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001623TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001624 RtpReceiverObserverOnFirstPacketReceived) {
1625 ASSERT_TRUE(CreatePeerConnectionWrappers());
1626 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001627 caller()->AddAudioVideoTracks();
1628 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001629 // Start offer/answer exchange and wait for it to complete.
1630 caller()->CreateAndSetAndSignalOffer();
1631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1632 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001633 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1634 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001635 // Wait for all "first packet received" callbacks to be fired.
1636 EXPECT_TRUE_WAIT(
1637 std::all_of(caller()->rtp_receiver_observers().begin(),
1638 caller()->rtp_receiver_observers().end(),
1639 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1640 return o->first_packet_received();
1641 }),
1642 kMaxWaitForFramesMs);
1643 EXPECT_TRUE_WAIT(
1644 std::all_of(callee()->rtp_receiver_observers().begin(),
1645 callee()->rtp_receiver_observers().end(),
1646 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1647 return o->first_packet_received();
1648 }),
1649 kMaxWaitForFramesMs);
1650 // If new observers are set after the first packet was already received, the
1651 // callback should still be invoked.
1652 caller()->ResetRtpReceiverObservers();
1653 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001654 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1655 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001656 EXPECT_TRUE(
1657 std::all_of(caller()->rtp_receiver_observers().begin(),
1658 caller()->rtp_receiver_observers().end(),
1659 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1660 return o->first_packet_received();
1661 }));
1662 EXPECT_TRUE(
1663 std::all_of(callee()->rtp_receiver_observers().begin(),
1664 callee()->rtp_receiver_observers().end(),
1665 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1666 return o->first_packet_received();
1667 }));
1668}
1669
1670class DummyDtmfObserver : public DtmfSenderObserverInterface {
1671 public:
1672 DummyDtmfObserver() : completed_(false) {}
1673
1674 // Implements DtmfSenderObserverInterface.
1675 void OnToneChange(const std::string& tone) override {
1676 tones_.push_back(tone);
1677 if (tone.empty()) {
1678 completed_ = true;
1679 }
1680 }
1681
1682 const std::vector<std::string>& tones() const { return tones_; }
1683 bool completed() const { return completed_; }
1684
1685 private:
1686 bool completed_;
1687 std::vector<std::string> tones_;
1688};
1689
1690// Assumes |sender| already has an audio track added and the offer/answer
1691// exchange is done.
1692void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1693 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001694 // We should be able to get a DTMF sender from the local sender.
1695 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1696 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1697 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001698 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001699 dtmf_sender->RegisterObserver(&observer);
1700
1701 // Test the DtmfSender object just created.
1702 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1703 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1704
1705 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1706 std::vector<std::string> tones = {"1", "a", ""};
1707 EXPECT_EQ(tones, observer.tones());
1708 dtmf_sender->UnregisterObserver();
1709 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1710}
1711
1712// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1713// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001714TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001715 ASSERT_TRUE(CreatePeerConnectionWrappers());
1716 ConnectFakeSignaling();
1717 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001718 caller()->AddAudioTrack();
1719 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001720 caller()->CreateAndSetAndSignalOffer();
1721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001722 // DTLS must finish before the DTMF sender can be used reliably.
1723 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001724 TestDtmfFromSenderToReceiver(caller(), callee());
1725 TestDtmfFromSenderToReceiver(callee(), caller());
1726}
1727
1728// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1729// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001730TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001731 ASSERT_TRUE(CreatePeerConnectionWrappers());
1732 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001733
deadbeef1dcb1642017-03-29 21:08:16 -07001734 // Do normal offer/answer and wait for some frames to be received in each
1735 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001736 caller()->AddAudioVideoTracks();
1737 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001738 caller()->CreateAndSetAndSignalOffer();
1739 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001740 MediaExpectations media_expectations;
1741 media_expectations.ExpectBidirectionalAudioAndVideo();
1742 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001743 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1744 webrtc::kEnumCounterKeyProtocolDtls));
1745 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1746 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001747}
1748
1749// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001750TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001751 PeerConnectionInterface::RTCConfiguration sdes_config;
1752 sdes_config.enable_dtls_srtp.emplace(false);
1753 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1754 ConnectFakeSignaling();
1755
1756 // Do normal offer/answer and wait for some frames to be received in each
1757 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001758 caller()->AddAudioVideoTracks();
1759 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001760 caller()->CreateAndSetAndSignalOffer();
1761 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001762 MediaExpectations media_expectations;
1763 media_expectations.ExpectBidirectionalAudioAndVideo();
1764 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001765 EXPECT_LE(2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1766 webrtc::kEnumCounterKeyProtocolSdes));
1767 EXPECT_EQ(0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1768 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001769}
1770
Steve Anton8c0f7a72017-10-03 10:03:10 -07001771// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1772// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001773TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001774 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1775 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1776 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1777 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1778 return pc->GetRemoteAudioSSLCertificate();
1779 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001780 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1781 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1782 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1783 return pc->GetRemoteAudioSSLCertChain();
1784 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001785
1786 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1787 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1788
1789 // Configure each side with a known certificate so they can be compared later.
1790 PeerConnectionInterface::RTCConfiguration caller_config;
1791 caller_config.enable_dtls_srtp.emplace(true);
1792 caller_config.certificates.push_back(caller_cert);
1793 PeerConnectionInterface::RTCConfiguration callee_config;
1794 callee_config.enable_dtls_srtp.emplace(true);
1795 callee_config.certificates.push_back(callee_cert);
1796 ASSERT_TRUE(
1797 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1798 ConnectFakeSignaling();
1799
1800 // When first initialized, there should not be a remote SSL certificate (and
1801 // calling this method should not crash).
1802 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1803 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001804 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1805 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001806
Steve Anton15324772018-01-16 10:26:49 -08001807 caller()->AddAudioTrack();
1808 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001809 caller()->CreateAndSetAndSignalOffer();
1810 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1811 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1812
1813 // Once DTLS has been connected, each side should return the other's SSL
1814 // certificate when calling GetRemoteAudioSSLCertificate.
1815
1816 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1817 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001818 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001819 caller_remote_cert->ToPEMString());
1820
1821 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1822 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001823 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001824 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001825
1826 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1827 ASSERT_TRUE(caller_remote_cert_chain);
1828 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1829 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001830 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001831 remote_cert->ToPEMString());
1832
1833 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1834 ASSERT_TRUE(callee_remote_cert_chain);
1835 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1836 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001837 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08001838 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001839}
1840
deadbeef1dcb1642017-03-29 21:08:16 -07001841// This test sets up a call between two parties with a source resolution of
1842// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001843TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001844 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1845 ASSERT_TRUE(CreatePeerConnectionWrappers());
1846 ConnectFakeSignaling();
1847
Niels Möller5c7efe72018-05-11 10:34:46 +02001848 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1849 webrtc::FakePeriodicVideoSource::Config config;
1850 config.width = 1280;
1851 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02001852 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02001853 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1854 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001855
1856 // Do normal offer/answer and wait for at least one frame to be received in
1857 // each direction.
1858 caller()->CreateAndSetAndSignalOffer();
1859 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1860 callee()->min_video_frames_received_per_track() > 0,
1861 kMaxWaitForFramesMs);
1862
1863 // Check rendered aspect ratio.
1864 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1865 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1866 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1867 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1868}
1869
1870// This test sets up an one-way call, with media only from caller to
1871// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001872TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001873 ASSERT_TRUE(CreatePeerConnectionWrappers());
1874 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001875 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001876 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001877 MediaExpectations media_expectations;
1878 media_expectations.CalleeExpectsSomeAudioAndVideo();
1879 media_expectations.CallerExpectsNoAudio();
1880 media_expectations.CallerExpectsNoVideo();
1881 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001882}
1883
1884// This test sets up a audio call initially, with the callee rejecting video
1885// initially. Then later the callee decides to upgrade to audio/video, and
1886// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001887TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001888 ASSERT_TRUE(CreatePeerConnectionWrappers());
1889 ConnectFakeSignaling();
1890 // Initially, offer an audio/video stream from the caller, but refuse to
1891 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001892 caller()->AddAudioVideoTracks();
1893 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001894 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1895 PeerConnectionInterface::RTCOfferAnswerOptions options;
1896 options.offer_to_receive_video = 0;
1897 callee()->SetOfferAnswerOptions(options);
1898 } else {
1899 callee()->SetRemoteOfferHandler([this] {
1900 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1901 });
1902 }
deadbeef1dcb1642017-03-29 21:08:16 -07001903 // Do offer/answer and make sure audio is still received end-to-end.
1904 caller()->CreateAndSetAndSignalOffer();
1905 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001906 {
1907 MediaExpectations media_expectations;
1908 media_expectations.ExpectBidirectionalAudio();
1909 media_expectations.ExpectNoVideo();
1910 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1911 }
deadbeef1dcb1642017-03-29 21:08:16 -07001912 // Sanity check that the callee's description has a rejected video section.
1913 ASSERT_NE(nullptr, callee()->pc()->local_description());
1914 const ContentInfo* callee_video_content =
1915 GetFirstVideoContent(callee()->pc()->local_description()->description());
1916 ASSERT_NE(nullptr, callee_video_content);
1917 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001918
deadbeef1dcb1642017-03-29 21:08:16 -07001919 // Now negotiate with video and ensure negotiation succeeds, with video
1920 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001921 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001922 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1923 PeerConnectionInterface::RTCOfferAnswerOptions options;
1924 options.offer_to_receive_video = 1;
1925 callee()->SetOfferAnswerOptions(options);
1926 } else {
1927 callee()->SetRemoteOfferHandler(nullptr);
1928 caller()->SetRemoteOfferHandler([this] {
1929 // The caller creates a new transceiver to receive video on when receiving
1930 // the offer, but by default it is send only.
1931 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001932 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08001933 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1934 transceivers[2]->receiver()->media_type());
1935 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1936 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1937 });
1938 }
deadbeef1dcb1642017-03-29 21:08:16 -07001939 callee()->CreateAndSetAndSignalOffer();
1940 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001941 {
1942 // Expect additional audio frames to be received after the upgrade.
1943 MediaExpectations media_expectations;
1944 media_expectations.ExpectBidirectionalAudioAndVideo();
1945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1946 }
deadbeef1dcb1642017-03-29 21:08:16 -07001947}
1948
deadbeef4389b4d2017-09-07 09:07:36 -07001949// Simpler than the above test; just add an audio track to an established
1950// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001951TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001952 ASSERT_TRUE(CreatePeerConnectionWrappers());
1953 ConnectFakeSignaling();
1954 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001955 caller()->AddVideoTrack();
1956 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001957 caller()->CreateAndSetAndSignalOffer();
1958 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1959 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001960 caller()->AddAudioTrack();
1961 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001962 caller()->CreateAndSetAndSignalOffer();
1963 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1964 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001965 MediaExpectations media_expectations;
1966 media_expectations.ExpectBidirectionalAudioAndVideo();
1967 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001968}
1969
deadbeef1dcb1642017-03-29 21:08:16 -07001970// This test sets up a call that's transferred to a new caller with a different
1971// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001972TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001973 ASSERT_TRUE(CreatePeerConnectionWrappers());
1974 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001975 caller()->AddAudioVideoTracks();
1976 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001977 caller()->CreateAndSetAndSignalOffer();
1978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1979
1980 // Keep the original peer around which will still send packets to the
1981 // receiving client. These SRTP packets will be dropped.
1982 std::unique_ptr<PeerConnectionWrapper> original_peer(
1983 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001984 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001985 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1986 // directly above.
1987 original_peer->pc()->Close();
1988
1989 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001990 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001991 caller()->CreateAndSetAndSignalOffer();
1992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1993 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001994 MediaExpectations media_expectations;
1995 media_expectations.ExpectBidirectionalAudioAndVideo();
1996 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001997}
1998
1999// This test sets up a call that's transferred to a new callee with a different
2000// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002001TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002002 ASSERT_TRUE(CreatePeerConnectionWrappers());
2003 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002004 caller()->AddAudioVideoTracks();
2005 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002006 caller()->CreateAndSetAndSignalOffer();
2007 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2008
2009 // Keep the original peer around which will still send packets to the
2010 // receiving client. These SRTP packets will be dropped.
2011 std::unique_ptr<PeerConnectionWrapper> original_peer(
2012 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002013 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002014 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2015 // directly above.
2016 original_peer->pc()->Close();
2017
2018 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002019 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002020 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2021 caller()->CreateAndSetAndSignalOffer();
2022 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2023 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002024 MediaExpectations media_expectations;
2025 media_expectations.ExpectBidirectionalAudioAndVideo();
2026 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002027}
2028
2029// This test sets up a non-bundled call and negotiates bundling at the same
2030// time as starting an ICE restart. When bundling is in effect in the restart,
2031// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002032TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002033 ASSERT_TRUE(CreatePeerConnectionWrappers());
2034 ConnectFakeSignaling();
2035
Steve Anton15324772018-01-16 10:26:49 -08002036 caller()->AddAudioVideoTracks();
2037 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002038 // Remove the bundle group from the SDP received by the callee.
2039 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2040 desc->RemoveGroupByName("BUNDLE");
2041 });
2042 caller()->CreateAndSetAndSignalOffer();
2043 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002044 {
2045 MediaExpectations media_expectations;
2046 media_expectations.ExpectBidirectionalAudioAndVideo();
2047 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2048 }
deadbeef1dcb1642017-03-29 21:08:16 -07002049 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2050 callee()->SetReceivedSdpMunger(nullptr);
2051 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2052 caller()->CreateAndSetAndSignalOffer();
2053 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2054
2055 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002056 {
2057 MediaExpectations media_expectations;
2058 media_expectations.ExpectBidirectionalAudioAndVideo();
2059 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2060 }
deadbeef1dcb1642017-03-29 21:08:16 -07002061}
2062
2063// Test CVO (Coordination of Video Orientation). If a video source is rotated
2064// and both peers support the CVO RTP header extension, the actual video frames
2065// don't need to be encoded in different resolutions, since the rotation is
2066// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002067TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002068 ASSERT_TRUE(CreatePeerConnectionWrappers());
2069 ConnectFakeSignaling();
2070 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002071 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002072 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002073 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002074 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2075
2076 // Wait for video frames to be received by both sides.
2077 caller()->CreateAndSetAndSignalOffer();
2078 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2079 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2080 callee()->min_video_frames_received_per_track() > 0,
2081 kMaxWaitForFramesMs);
2082
2083 // Ensure that the aspect ratio is unmodified.
2084 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2085 // not just assumed.
2086 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2087 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2088 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2089 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2090 // Ensure that the CVO bits were surfaced to the renderer.
2091 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2092 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2093}
2094
2095// Test that when the CVO extension isn't supported, video is rotated the
2096// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002098 ASSERT_TRUE(CreatePeerConnectionWrappers());
2099 ConnectFakeSignaling();
2100 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002101 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002102 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002103 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002104 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2105
2106 // Remove the CVO extension from the offered SDP.
2107 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2108 cricket::VideoContentDescription* video =
2109 GetFirstVideoContentDescription(desc);
2110 video->ClearRtpHeaderExtensions();
2111 });
2112 // Wait for video frames to be received by both sides.
2113 caller()->CreateAndSetAndSignalOffer();
2114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2115 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2116 callee()->min_video_frames_received_per_track() > 0,
2117 kMaxWaitForFramesMs);
2118
2119 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2120 // rotation.
2121 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2122 // not just assumed.
2123 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2124 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2125 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2126 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2127 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2128 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2129 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2130}
2131
deadbeef1dcb1642017-03-29 21:08:16 -07002132// Test that if the answerer rejects the audio m= section, no audio is sent or
2133// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002134TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002135 ASSERT_TRUE(CreatePeerConnectionWrappers());
2136 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002137 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002138 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2139 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2140 // it will reject the audio m= section completely.
2141 PeerConnectionInterface::RTCOfferAnswerOptions options;
2142 options.offer_to_receive_audio = 0;
2143 callee()->SetOfferAnswerOptions(options);
2144 } else {
2145 // Stopping the audio RtpTransceiver will cause the media section to be
2146 // rejected in the answer.
2147 callee()->SetRemoteOfferHandler([this] {
2148 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2149 });
2150 }
Steve Anton15324772018-01-16 10:26:49 -08002151 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002152 // Do offer/answer and wait for successful end-to-end video frames.
2153 caller()->CreateAndSetAndSignalOffer();
2154 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002155 MediaExpectations media_expectations;
2156 media_expectations.ExpectBidirectionalVideo();
2157 media_expectations.ExpectNoAudio();
2158 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2159
deadbeef1dcb1642017-03-29 21:08:16 -07002160 // Sanity check that the callee's description has a rejected audio section.
2161 ASSERT_NE(nullptr, callee()->pc()->local_description());
2162 const ContentInfo* callee_audio_content =
2163 GetFirstAudioContent(callee()->pc()->local_description()->description());
2164 ASSERT_NE(nullptr, callee_audio_content);
2165 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002166 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2167 // The caller's transceiver should have stopped after receiving the answer.
2168 EXPECT_TRUE(caller()
2169 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2170 ->stopped());
2171 }
deadbeef1dcb1642017-03-29 21:08:16 -07002172}
2173
2174// Test that if the answerer rejects the video m= section, no video is sent or
2175// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002176TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002177 ASSERT_TRUE(CreatePeerConnectionWrappers());
2178 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002179 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002180 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2181 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2182 // it will reject the video m= section completely.
2183 PeerConnectionInterface::RTCOfferAnswerOptions options;
2184 options.offer_to_receive_video = 0;
2185 callee()->SetOfferAnswerOptions(options);
2186 } else {
2187 // Stopping the video RtpTransceiver will cause the media section to be
2188 // rejected in the answer.
2189 callee()->SetRemoteOfferHandler([this] {
2190 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2191 });
2192 }
Steve Anton15324772018-01-16 10:26:49 -08002193 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002194 // Do offer/answer and wait for successful end-to-end audio frames.
2195 caller()->CreateAndSetAndSignalOffer();
2196 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002197 MediaExpectations media_expectations;
2198 media_expectations.ExpectBidirectionalAudio();
2199 media_expectations.ExpectNoVideo();
2200 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2201
deadbeef1dcb1642017-03-29 21:08:16 -07002202 // Sanity check that the callee's description has a rejected video section.
2203 ASSERT_NE(nullptr, callee()->pc()->local_description());
2204 const ContentInfo* callee_video_content =
2205 GetFirstVideoContent(callee()->pc()->local_description()->description());
2206 ASSERT_NE(nullptr, callee_video_content);
2207 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002208 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2209 // The caller's transceiver should have stopped after receiving the answer.
2210 EXPECT_TRUE(caller()
2211 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2212 ->stopped());
2213 }
deadbeef1dcb1642017-03-29 21:08:16 -07002214}
2215
2216// Test that if the answerer rejects both audio and video m= sections, nothing
2217// bad happens.
2218// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2219// test anything but the fact that negotiation succeeds, which doesn't mean
2220// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002221TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002222 ASSERT_TRUE(CreatePeerConnectionWrappers());
2223 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002224 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002225 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2226 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2227 // will reject both audio and video m= sections.
2228 PeerConnectionInterface::RTCOfferAnswerOptions options;
2229 options.offer_to_receive_audio = 0;
2230 options.offer_to_receive_video = 0;
2231 callee()->SetOfferAnswerOptions(options);
2232 } else {
2233 callee()->SetRemoteOfferHandler([this] {
2234 // Stopping all transceivers will cause all media sections to be rejected.
2235 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2236 transceiver->Stop();
2237 }
2238 });
2239 }
deadbeef1dcb1642017-03-29 21:08:16 -07002240 // Do offer/answer and wait for stable signaling state.
2241 caller()->CreateAndSetAndSignalOffer();
2242 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002243
deadbeef1dcb1642017-03-29 21:08:16 -07002244 // Sanity check that the callee's description has rejected m= sections.
2245 ASSERT_NE(nullptr, callee()->pc()->local_description());
2246 const ContentInfo* callee_audio_content =
2247 GetFirstAudioContent(callee()->pc()->local_description()->description());
2248 ASSERT_NE(nullptr, callee_audio_content);
2249 EXPECT_TRUE(callee_audio_content->rejected);
2250 const ContentInfo* callee_video_content =
2251 GetFirstVideoContent(callee()->pc()->local_description()->description());
2252 ASSERT_NE(nullptr, callee_video_content);
2253 EXPECT_TRUE(callee_video_content->rejected);
2254}
2255
2256// This test sets up an audio and video call between two parties. After the
2257// call runs for a while, the caller sends an updated offer with video being
2258// rejected. Once the re-negotiation is done, the video flow should stop and
2259// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002261 ASSERT_TRUE(CreatePeerConnectionWrappers());
2262 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002263 caller()->AddAudioVideoTracks();
2264 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002265 caller()->CreateAndSetAndSignalOffer();
2266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002267 {
2268 MediaExpectations media_expectations;
2269 media_expectations.ExpectBidirectionalAudioAndVideo();
2270 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2271 }
deadbeef1dcb1642017-03-29 21:08:16 -07002272 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002273 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2274 caller()->SetGeneratedSdpMunger(
2275 [](cricket::SessionDescription* description) {
2276 for (cricket::ContentInfo& content : description->contents()) {
2277 if (cricket::IsVideoContent(&content)) {
2278 content.rejected = true;
2279 }
2280 }
2281 });
2282 } else {
2283 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2284 }
deadbeef1dcb1642017-03-29 21:08:16 -07002285 caller()->CreateAndSetAndSignalOffer();
2286 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2287
2288 // Sanity check that the caller's description has a rejected video section.
2289 ASSERT_NE(nullptr, caller()->pc()->local_description());
2290 const ContentInfo* caller_video_content =
2291 GetFirstVideoContent(caller()->pc()->local_description()->description());
2292 ASSERT_NE(nullptr, caller_video_content);
2293 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002294 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002295 {
2296 MediaExpectations media_expectations;
2297 media_expectations.ExpectBidirectionalAudio();
2298 media_expectations.ExpectNoVideo();
2299 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2300 }
deadbeef1dcb1642017-03-29 21:08:16 -07002301}
2302
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002303// Do one offer/answer with audio, another that disables it (rejecting the m=
2304// section), and another that re-enables it. Regression test for:
2305// bugs.webrtc.org/6023
2306TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2307 ASSERT_TRUE(CreatePeerConnectionWrappers());
2308 ConnectFakeSignaling();
2309
2310 // Add audio track, do normal offer/answer.
2311 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2312 caller()->CreateLocalAudioTrack();
2313 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2314 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2315 caller()->CreateAndSetAndSignalOffer();
2316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2317
2318 // Remove audio track, and set offer_to_receive_audio to false to cause the
2319 // m= section to be completely disabled, not just "recvonly".
2320 caller()->pc()->RemoveTrack(sender);
2321 PeerConnectionInterface::RTCOfferAnswerOptions options;
2322 options.offer_to_receive_audio = 0;
2323 caller()->SetOfferAnswerOptions(options);
2324 caller()->CreateAndSetAndSignalOffer();
2325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2326
2327 // Add the audio track again, expecting negotiation to succeed and frames to
2328 // flow.
2329 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2330 options.offer_to_receive_audio = 1;
2331 caller()->SetOfferAnswerOptions(options);
2332 caller()->CreateAndSetAndSignalOffer();
2333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2334
2335 MediaExpectations media_expectations;
2336 media_expectations.CalleeExpectsSomeAudio();
2337 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2338}
2339
deadbeef1dcb1642017-03-29 21:08:16 -07002340// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2341// is needed to support legacy endpoints.
2342// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2343// add a test for an end-to-end test without MID signaling either (basically,
2344// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002345TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002346 ASSERT_TRUE(CreatePeerConnectionWrappers());
2347 ConnectFakeSignaling();
2348 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002349 caller()->AddAudioVideoTracks();
2350 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002351 // Remove SSRCs and MSIDs from the received offer SDP.
2352 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002353 caller()->CreateAndSetAndSignalOffer();
2354 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002355 MediaExpectations media_expectations;
2356 media_expectations.ExpectBidirectionalAudioAndVideo();
2357 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002358}
2359
Seth Hampson5897a6e2018-04-03 11:16:33 -07002360// Basic end-to-end test, without SSRC signaling. This means that the track
2361// was created properly and frames are delivered when the MSIDs are communicated
2362// with a=msid lines and no a=ssrc lines.
2363TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2364 EndToEndCallWithoutSsrcSignaling) {
2365 const char kStreamId[] = "streamId";
2366 ASSERT_TRUE(CreatePeerConnectionWrappers());
2367 ConnectFakeSignaling();
2368 // Add just audio tracks.
2369 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2370 callee()->AddAudioTrack();
2371
2372 // Remove SSRCs from the received offer SDP.
2373 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2374 caller()->CreateAndSetAndSignalOffer();
2375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2376 MediaExpectations media_expectations;
2377 media_expectations.ExpectBidirectionalAudio();
2378 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2379}
2380
Steve Antondf527fd2018-04-27 15:52:03 -07002381// Tests that video flows between multiple video tracks when SSRCs are not
2382// signaled. This exercises the MID RTP header extension which is needed to
2383// demux the incoming video tracks.
2384TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2385 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2386 ASSERT_TRUE(CreatePeerConnectionWrappers());
2387 ConnectFakeSignaling();
2388 caller()->AddVideoTrack();
2389 caller()->AddVideoTrack();
2390 callee()->AddVideoTrack();
2391 callee()->AddVideoTrack();
2392
2393 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2394 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2395 caller()->CreateAndSetAndSignalOffer();
2396 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2397 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2398 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2399
2400 // Expect video to be received in both directions on both tracks.
2401 MediaExpectations media_expectations;
2402 media_expectations.ExpectBidirectionalVideo();
2403 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2404}
2405
Henrik Boström5b147782018-12-04 11:25:05 +01002406TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2407 ASSERT_TRUE(CreatePeerConnectionWrappers());
2408 ConnectFakeSignaling();
2409 caller()->AddAudioTrack();
2410 caller()->AddVideoTrack();
2411 caller()->CreateAndSetAndSignalOffer();
2412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2413 auto callee_receivers = callee()->pc()->GetReceivers();
2414 ASSERT_EQ(2u, callee_receivers.size());
2415 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2416 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2417}
2418
2419TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2420 ASSERT_TRUE(CreatePeerConnectionWrappers());
2421 ConnectFakeSignaling();
2422 caller()->AddAudioTrack();
2423 caller()->AddVideoTrack();
2424 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2425 caller()->CreateAndSetAndSignalOffer();
2426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2427 auto callee_receivers = callee()->pc()->GetReceivers();
2428 ASSERT_EQ(2u, callee_receivers.size());
2429 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2430 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2431 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2432 callee_receivers[1]->stream_ids()[0]);
2433 EXPECT_EQ(callee_receivers[0]->streams()[0],
2434 callee_receivers[1]->streams()[0]);
2435}
2436
deadbeef1dcb1642017-03-29 21:08:16 -07002437// Test that if two video tracks are sent (from caller to callee, in this test),
2438// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002439TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002440 ASSERT_TRUE(CreatePeerConnectionWrappers());
2441 ConnectFakeSignaling();
2442 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002443 caller()->AddAudioVideoTracks();
2444 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002445 caller()->CreateAndSetAndSignalOffer();
2446 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002447 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002448
2449 MediaExpectations media_expectations;
2450 media_expectations.CalleeExpectsSomeAudioAndVideo();
2451 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002452}
2453
2454static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2455 bool first = true;
2456 for (cricket::ContentInfo& content : desc->contents()) {
2457 if (first) {
2458 first = false;
2459 continue;
2460 }
2461 content.bundle_only = true;
2462 }
2463 first = true;
2464 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2465 if (first) {
2466 first = false;
2467 continue;
2468 }
2469 transport.description.ice_ufrag.clear();
2470 transport.description.ice_pwd.clear();
2471 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2472 transport.description.identity_fingerprint.reset(nullptr);
2473 }
2474}
2475
2476// Test that if applying a true "max bundle" offer, which uses ports of 0,
2477// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2478// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2479// successfully and media flows.
2480// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2481// TODO(deadbeef): Won't need this test once we start generating actual
2482// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002483TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002484 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2485 ASSERT_TRUE(CreatePeerConnectionWrappers());
2486 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002487 caller()->AddAudioVideoTracks();
2488 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002489 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2490 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2491 // but the first m= section.
2492 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2493 caller()->CreateAndSetAndSignalOffer();
2494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002495 MediaExpectations media_expectations;
2496 media_expectations.ExpectBidirectionalAudioAndVideo();
2497 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002498}
2499
2500// Test that we can receive the audio output level from a remote audio track.
2501// TODO(deadbeef): Use a fake audio source and verify that the output level is
2502// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002503TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002504 ASSERT_TRUE(CreatePeerConnectionWrappers());
2505 ConnectFakeSignaling();
2506 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002507 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002508 caller()->CreateAndSetAndSignalOffer();
2509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2510
2511 // Get the audio output level stats. Note that the level is not available
2512 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002513 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002514 kMaxWaitForFramesMs);
2515}
2516
2517// Test that an audio input level is reported.
2518// TODO(deadbeef): Use a fake audio source and verify that the input level is
2519// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002521 ASSERT_TRUE(CreatePeerConnectionWrappers());
2522 ConnectFakeSignaling();
2523 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002524 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002525 caller()->CreateAndSetAndSignalOffer();
2526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2527
2528 // Get the audio input level stats. The level should be available very
2529 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002530 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002531 kMaxWaitForStatsMs);
2532}
2533
2534// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002535TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002536 ASSERT_TRUE(CreatePeerConnectionWrappers());
2537 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002538 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002539 // Do offer/answer, wait for the callee to receive some frames.
2540 caller()->CreateAndSetAndSignalOffer();
2541 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002542
2543 MediaExpectations media_expectations;
2544 media_expectations.CalleeExpectsSomeAudioAndVideo();
2545 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002546
2547 // Get a handle to the remote tracks created, so they can be used as GetStats
2548 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002549 for (auto receiver : callee()->pc()->GetReceivers()) {
2550 // We received frames, so we definitely should have nonzero "received bytes"
2551 // stats at this point.
2552 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2553 0);
2554 }
deadbeef1dcb1642017-03-29 21:08:16 -07002555}
2556
2557// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002559 ASSERT_TRUE(CreatePeerConnectionWrappers());
2560 ConnectFakeSignaling();
2561 auto audio_track = caller()->CreateLocalAudioTrack();
2562 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002563 caller()->AddTrack(audio_track);
2564 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002565 // Do offer/answer, wait for the callee to receive some frames.
2566 caller()->CreateAndSetAndSignalOffer();
2567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002568 MediaExpectations media_expectations;
2569 media_expectations.CalleeExpectsSomeAudioAndVideo();
2570 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002571
2572 // The callee received frames, so we definitely should have nonzero "sent
2573 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002574 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2575 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2576}
2577
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002578// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002579TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002580 ASSERT_TRUE(CreatePeerConnectionWrappers());
2581 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002582 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002583
Steve Anton15324772018-01-16 10:26:49 -08002584 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002585
2586 // Do offer/answer, wait for the callee to receive some frames.
2587 caller()->CreateAndSetAndSignalOffer();
2588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2589
2590 // Get the remote audio track created on the receiver, so they can be used as
2591 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002592 auto receivers = callee()->pc()->GetReceivers();
2593 ASSERT_EQ(1u, receivers.size());
2594 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002595
2596 // Get the audio output level stats. Note that the level is not available
2597 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002598 EXPECT_TRUE_WAIT(
2599 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2600 0,
2601 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002602}
2603
Steve Antona41959e2018-11-28 11:15:33 -08002604// Test that the track ID is associated with all local and remote SSRC stats
2605// using the old GetStats() and more than 1 audio and more than 1 video track.
2606// This is a regression test for crbug.com/906988
2607TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2608 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2609 ASSERT_TRUE(CreatePeerConnectionWrappers());
2610 ConnectFakeSignaling();
2611 auto audio_sender_1 = caller()->AddAudioTrack();
2612 auto video_sender_1 = caller()->AddVideoTrack();
2613 auto audio_sender_2 = caller()->AddAudioTrack();
2614 auto video_sender_2 = caller()->AddVideoTrack();
2615 caller()->CreateAndSetAndSignalOffer();
2616 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2617
2618 MediaExpectations media_expectations;
2619 media_expectations.CalleeExpectsSomeAudioAndVideo();
2620 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2621
2622 std::vector<std::string> track_ids = {
2623 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2624 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2625
2626 auto caller_stats = caller()->OldGetStats();
2627 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2628 auto callee_stats = callee()->OldGetStats();
2629 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2630}
2631
Steve Antonffa6ce42018-11-30 09:26:08 -08002632// Test that the new GetStats() returns stats for all outgoing/incoming streams
2633// with the correct track IDs if there are more than one audio and more than one
2634// video senders/receivers.
2635TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
2636 ASSERT_TRUE(CreatePeerConnectionWrappers());
2637 ConnectFakeSignaling();
2638 auto audio_sender_1 = caller()->AddAudioTrack();
2639 auto video_sender_1 = caller()->AddVideoTrack();
2640 auto audio_sender_2 = caller()->AddAudioTrack();
2641 auto video_sender_2 = caller()->AddVideoTrack();
2642 caller()->CreateAndSetAndSignalOffer();
2643 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2644
2645 MediaExpectations media_expectations;
2646 media_expectations.CalleeExpectsSomeAudioAndVideo();
2647 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2648
2649 std::vector<std::string> track_ids = {
2650 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2651 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2652
2653 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
2654 caller()->NewGetStats();
2655 ASSERT_TRUE(caller_report);
2656 auto outbound_stream_stats =
2657 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
2658 ASSERT_EQ(4u, outbound_stream_stats.size());
2659 std::vector<std::string> outbound_track_ids;
2660 for (const auto& stat : outbound_stream_stats) {
2661 ASSERT_TRUE(stat->bytes_sent.is_defined());
2662 EXPECT_LT(0u, *stat->bytes_sent);
2663 ASSERT_TRUE(stat->track_id.is_defined());
2664 const auto* track_stat =
2665 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2666 ASSERT_TRUE(track_stat);
2667 outbound_track_ids.push_back(*track_stat->track_identifier);
2668 }
2669 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
2670
2671 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
2672 callee()->NewGetStats();
2673 ASSERT_TRUE(callee_report);
2674 auto inbound_stream_stats =
2675 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2676 ASSERT_EQ(4u, inbound_stream_stats.size());
2677 std::vector<std::string> inbound_track_ids;
2678 for (const auto& stat : inbound_stream_stats) {
2679 ASSERT_TRUE(stat->bytes_received.is_defined());
2680 EXPECT_LT(0u, *stat->bytes_received);
2681 ASSERT_TRUE(stat->track_id.is_defined());
2682 const auto* track_stat =
2683 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
2684 ASSERT_TRUE(track_stat);
2685 inbound_track_ids.push_back(*track_stat->track_identifier);
2686 }
2687 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
2688}
2689
2690// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07002691// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2692// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002693TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002694 GetStatsForUnsignaledStreamWithNewStatsApi) {
2695 ASSERT_TRUE(CreatePeerConnectionWrappers());
2696 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002697 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002698 // Remove SSRCs and MSIDs from the received offer SDP.
2699 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2700 caller()->CreateAndSetAndSignalOffer();
2701 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002702 MediaExpectations media_expectations;
2703 media_expectations.CalleeExpectsSomeAudio(1);
2704 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002705
2706 // We received a frame, so we should have nonzero "bytes received" stats for
2707 // the unsignaled stream, if stats are working for it.
2708 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2709 callee()->NewGetStats();
2710 ASSERT_NE(nullptr, report);
2711 auto inbound_stream_stats =
2712 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2713 ASSERT_EQ(1U, inbound_stream_stats.size());
2714 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2715 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002716 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2717}
2718
Taylor Brandstettera4653442018-06-19 09:44:26 -07002719// Same as above but for the legacy stats implementation.
2720TEST_P(PeerConnectionIntegrationTest,
2721 GetStatsForUnsignaledStreamWithOldStatsApi) {
2722 ASSERT_TRUE(CreatePeerConnectionWrappers());
2723 ConnectFakeSignaling();
2724 caller()->AddAudioTrack();
2725 // Remove SSRCs and MSIDs from the received offer SDP.
2726 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2727 caller()->CreateAndSetAndSignalOffer();
2728 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2729
2730 // Note that, since the old stats implementation associates SSRCs with tracks
2731 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2732 // associated track ID. So we can't use the track "selector" argument.
2733 //
2734 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2735 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002736 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07002737 kDefaultTimeout);
2738}
2739
zhihuangf8164932017-05-19 13:09:47 -07002740// Test that we can successfully get the media related stats (audio level
2741// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002742TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002743 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2744 ASSERT_TRUE(CreatePeerConnectionWrappers());
2745 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002746 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002747 // Remove SSRCs and MSIDs from the received offer SDP.
2748 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2749 caller()->CreateAndSetAndSignalOffer();
2750 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002751 MediaExpectations media_expectations;
2752 media_expectations.CalleeExpectsSomeAudio(1);
2753 media_expectations.CalleeExpectsSomeVideo(1);
2754 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002755
2756 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2757 callee()->NewGetStats();
2758 ASSERT_NE(nullptr, report);
2759
2760 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2761 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2762 ASSERT_GE(audio_index, 0);
2763 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002764}
2765
deadbeef4e2deab2017-09-20 13:56:21 -07002766// Helper for test below.
2767void ModifySsrcs(cricket::SessionDescription* desc) {
2768 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002769 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002770 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002771 for (uint32_t& ssrc : stream.ssrcs) {
2772 ssrc = rtc::CreateRandomId();
2773 }
2774 }
2775 }
2776}
2777
2778// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2779// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2780// This should result in two "RTCInboundRTPStreamStats", but only one
2781// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2782// being reset to 0 once the SSRC change occurs.
2783//
2784// Regression test for this bug:
2785// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2786//
2787// The bug causes the track stats to only represent one of the two streams:
2788// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2789// that the track stat counters would reset to 0 when the new stream is
2790// received, and a 50% chance that they'll stop updating (while
2791// "concealed_samples" continues increasing, due to silence being generated for
2792// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002793TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002794 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002795 ASSERT_TRUE(CreatePeerConnectionWrappers());
2796 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002797 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002798 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2799 // that doesn't signal SSRCs (from the callee's perspective).
2800 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2801 caller()->CreateAndSetAndSignalOffer();
2802 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2803 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002804 {
2805 MediaExpectations media_expectations;
2806 media_expectations.CalleeExpectsSomeAudio(50);
2807 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2808 }
deadbeef4e2deab2017-09-20 13:56:21 -07002809 // Some audio frames were received, so we should have nonzero "samples
2810 // received" for the track.
2811 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2812 callee()->NewGetStats();
2813 ASSERT_NE(nullptr, report);
2814 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2815 ASSERT_EQ(1U, track_stats.size());
2816 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2817 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2818 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2819
2820 // Create a new offer and munge it to cause the caller to use a new SSRC.
2821 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2822 caller()->CreateAndSetAndSignalOffer();
2823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2824 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2825 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002826 {
2827 MediaExpectations media_expectations;
2828 media_expectations.CalleeExpectsSomeAudio(25);
2829 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2830 }
deadbeef4e2deab2017-09-20 13:56:21 -07002831
2832 report = callee()->NewGetStats();
2833 ASSERT_NE(nullptr, report);
2834 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2835 ASSERT_EQ(1U, track_stats.size());
2836 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2837 // The "total samples received" stat should only be greater than it was
2838 // before.
2839 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2840 // Right now, the new SSRC will cause the counters to reset to 0.
2841 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2842
2843 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002844 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002845 // good sign that we're seeing stats from the old stream that's no longer
2846 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002847 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002848 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2849 EXPECT_LT(*track_stats[0]->concealed_samples,
2850 *track_stats[0]->total_samples_received *
2851 kAcceptableConcealedSamplesPercentage);
2852
2853 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2854 // sanity check that the SSRC really changed.
2855 // TODO(deadbeef): This isn't working right now, because we're not returning
2856 // *any* stats for the inactive stream. Uncomment when the bug is completely
2857 // fixed.
2858 // auto inbound_stream_stats =
2859 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2860 // ASSERT_EQ(2U, inbound_stream_stats.size());
2861}
2862
deadbeef1dcb1642017-03-29 21:08:16 -07002863// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002864TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002865 PeerConnectionFactory::Options dtls_10_options;
2866 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2867 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2868 dtls_10_options));
2869 ConnectFakeSignaling();
2870 // Do normal offer/answer and wait for some frames to be received in each
2871 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002872 caller()->AddAudioVideoTracks();
2873 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002874 caller()->CreateAndSetAndSignalOffer();
2875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002876 MediaExpectations media_expectations;
2877 media_expectations.ExpectBidirectionalAudioAndVideo();
2878 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002879}
2880
2881// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002882TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002883 PeerConnectionFactory::Options dtls_10_options;
2884 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2885 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2886 dtls_10_options));
2887 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002888 caller()->AddAudioVideoTracks();
2889 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002890 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002891 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002892 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002893 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002894 kDefaultTimeout);
2895 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002896 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002897 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002898 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002899 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2900 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002901}
2902
2903// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002904TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002905 PeerConnectionFactory::Options dtls_12_options;
2906 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2907 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2908 dtls_12_options));
2909 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002910 caller()->AddAudioVideoTracks();
2911 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002912 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002913 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002914 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002915 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002916 kDefaultTimeout);
2917 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002918 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002919 // TODO(bugs.webrtc.org/9456): Fix it.
Alex Loiko9289eda2018-11-23 16:18:59 +00002920 EXPECT_EQ(1, webrtc::metrics::NumEvents(
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002921 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
2922 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07002923}
2924
2925// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2926// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002927TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002928 PeerConnectionFactory::Options caller_options;
2929 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2930 PeerConnectionFactory::Options callee_options;
2931 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2932 ASSERT_TRUE(
2933 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2934 ConnectFakeSignaling();
2935 // Do normal offer/answer and wait for some frames to be received in each
2936 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002937 caller()->AddAudioVideoTracks();
2938 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002939 caller()->CreateAndSetAndSignalOffer();
2940 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002941 MediaExpectations media_expectations;
2942 media_expectations.ExpectBidirectionalAudioAndVideo();
2943 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002944}
2945
2946// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2947// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002948TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002949 PeerConnectionFactory::Options caller_options;
2950 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2951 PeerConnectionFactory::Options callee_options;
2952 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2953 ASSERT_TRUE(
2954 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2955 ConnectFakeSignaling();
2956 // Do normal offer/answer and wait for some frames to be received in each
2957 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002958 caller()->AddAudioVideoTracks();
2959 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002960 caller()->CreateAndSetAndSignalOffer();
2961 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002962 MediaExpectations media_expectations;
2963 media_expectations.ExpectBidirectionalAudioAndVideo();
2964 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002965}
2966
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002967// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2968// works as expected; the cipher should only be used if enabled by both sides.
2969TEST_P(PeerConnectionIntegrationTest,
2970 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2971 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002972 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002973 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002974 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2975 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002976 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2977 TestNegotiatedCipherSuite(caller_options, callee_options,
2978 expected_cipher_suite);
2979}
2980
2981TEST_P(PeerConnectionIntegrationTest,
2982 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2983 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002984 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
2985 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002986 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002987 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002988 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2989 TestNegotiatedCipherSuite(caller_options, callee_options,
2990 expected_cipher_suite);
2991}
2992
2993TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2994 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002995 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002996 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07002997 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002998 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2999 TestNegotiatedCipherSuite(caller_options, callee_options,
3000 expected_cipher_suite);
3001}
3002
deadbeef1dcb1642017-03-29 21:08:16 -07003003// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003004TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003005 bool local_gcm_enabled = false;
3006 bool remote_gcm_enabled = false;
3007 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3008 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3009 expected_cipher_suite);
3010}
3011
3012// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003013TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003014 bool local_gcm_enabled = true;
3015 bool remote_gcm_enabled = true;
3016 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3017 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3018 expected_cipher_suite);
3019}
3020
3021// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003022TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003023 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
3024 bool local_gcm_enabled = true;
3025 bool remote_gcm_enabled = false;
3026 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3027 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3028 expected_cipher_suite);
3029}
3030
3031// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003032TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003033 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
3034 bool local_gcm_enabled = false;
3035 bool remote_gcm_enabled = true;
3036 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3037 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
3038 expected_cipher_suite);
3039}
3040
deadbeef7914b8c2017-04-21 03:23:33 -07003041// Verify that media can be transmitted end-to-end when GCM crypto suites are
3042// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3043// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3044// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003045TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003046 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003047 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
deadbeef7914b8c2017-04-21 03:23:33 -07003048 ASSERT_TRUE(
3049 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3050 ConnectFakeSignaling();
3051 // Do normal offer/answer and wait for some frames to be received in each
3052 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003053 caller()->AddAudioVideoTracks();
3054 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003055 caller()->CreateAndSetAndSignalOffer();
3056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003057 MediaExpectations media_expectations;
3058 media_expectations.ExpectBidirectionalAudioAndVideo();
3059 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003060}
3061
deadbeef1dcb1642017-03-29 21:08:16 -07003062// This test sets up a call between two parties with audio, video and an RTP
3063// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003064TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003065 PeerConnectionInterface::RTCConfiguration rtc_config;
3066 rtc_config.enable_rtp_data_channel = true;
3067 rtc_config.enable_dtls_srtp = false;
3068 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003069 ConnectFakeSignaling();
3070 // Expect that data channel created on caller side will show up for callee as
3071 // well.
3072 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003073 caller()->AddAudioVideoTracks();
3074 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003075 caller()->CreateAndSetAndSignalOffer();
3076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3077 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003078 MediaExpectations media_expectations;
3079 media_expectations.ExpectBidirectionalAudioAndVideo();
3080 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003081 ASSERT_NE(nullptr, caller()->data_channel());
3082 ASSERT_NE(nullptr, callee()->data_channel());
3083 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3084 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3085
3086 // Ensure data can be sent in both directions.
3087 std::string data = "hello world";
3088 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3089 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3090 kDefaultTimeout);
3091 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3092 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3093 kDefaultTimeout);
3094}
3095
3096// Ensure that an RTP data channel is signaled as closed for the caller when
3097// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003098TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003099 RtpDataChannelSignaledClosedInCalleeOffer) {
3100 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003101 PeerConnectionInterface::RTCConfiguration rtc_config;
3102 rtc_config.enable_rtp_data_channel = true;
3103 rtc_config.enable_dtls_srtp = false;
3104 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003105 ConnectFakeSignaling();
3106 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003107 caller()->AddAudioVideoTracks();
3108 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003109 caller()->CreateAndSetAndSignalOffer();
3110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3111 ASSERT_NE(nullptr, caller()->data_channel());
3112 ASSERT_NE(nullptr, callee()->data_channel());
3113 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3114 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3115
3116 // Close the data channel on the callee, and do an updated offer/answer.
3117 callee()->data_channel()->Close();
3118 callee()->CreateAndSetAndSignalOffer();
3119 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3120 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3121 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3122}
3123
3124// Tests that data is buffered in an RTP data channel until an observer is
3125// registered for it.
3126//
3127// NOTE: RTP data channels can receive data before the underlying
3128// transport has detected that a channel is writable and thus data can be
3129// received before the data channel state changes to open. That is hard to test
3130// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003131TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003132 DataBufferedUntilRtpDataChannelObserverRegistered) {
3133 // Use fake clock and simulated network delay so that we predictably can wait
3134 // until an SCTP message has been delivered without "sleep()"ing.
3135 rtc::ScopedFakeClock fake_clock;
3136 // Some things use a time of "0" as a special value, so we need to start out
3137 // the fake clock at a nonzero time.
3138 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003139 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003140 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3141 virtual_socket_server()->UpdateDelayDistribution();
3142
Niels Möllerf06f9232018-08-07 12:32:18 +02003143 PeerConnectionInterface::RTCConfiguration rtc_config;
3144 rtc_config.enable_rtp_data_channel = true;
3145 rtc_config.enable_dtls_srtp = false;
3146 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003147 ConnectFakeSignaling();
3148 caller()->CreateDataChannel();
3149 caller()->CreateAndSetAndSignalOffer();
3150 ASSERT_TRUE(caller()->data_channel() != nullptr);
3151 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3152 kDefaultTimeout, fake_clock);
3153 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3154 kDefaultTimeout, fake_clock);
3155 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3156 callee()->data_channel()->state(), kDefaultTimeout,
3157 fake_clock);
3158
3159 // Unregister the observer which is normally automatically registered.
3160 callee()->data_channel()->UnregisterObserver();
3161 // Send data and advance fake clock until it should have been received.
3162 std::string data = "hello world";
3163 caller()->data_channel()->Send(DataBuffer(data));
3164 SIMULATED_WAIT(false, 50, fake_clock);
3165
3166 // Attach data channel and expect data to be received immediately. Note that
3167 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3168 // further, but data can be received even if the callback is asynchronous.
3169 MockDataChannelObserver new_observer(callee()->data_channel());
3170 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3171 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003172 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3173 // If this is not done a DCHECK can be hit in ports.cc, because a large
3174 // negative number is calculated for the rtt due to the global clock changing.
3175 caller()->pc()->Close();
3176 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003177}
3178
3179// This test sets up a call between two parties with audio, video and but only
3180// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003181TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003182 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3183 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003184 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003185 rtc_config_1.enable_dtls_srtp = false;
3186 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3187 rtc_config_2.enable_dtls_srtp = false;
3188 rtc_config_2.enable_dtls_srtp = false;
3189 ASSERT_TRUE(
3190 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003191 ConnectFakeSignaling();
3192 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003193 caller()->AddAudioVideoTracks();
3194 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003195 caller()->CreateAndSetAndSignalOffer();
3196 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3197 // The caller should still have a data channel, but it should be closed, and
3198 // one should ever have been created for the callee.
3199 EXPECT_TRUE(caller()->data_channel() != nullptr);
3200 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3201 EXPECT_EQ(nullptr, callee()->data_channel());
3202}
3203
3204// This test sets up a call between two parties with audio, and video. When
3205// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003206TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003207 PeerConnectionInterface::RTCConfiguration rtc_config;
3208 rtc_config.enable_rtp_data_channel = true;
3209 rtc_config.enable_dtls_srtp = false;
3210 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003211 ConnectFakeSignaling();
3212 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003213 caller()->AddAudioVideoTracks();
3214 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003215 caller()->CreateAndSetAndSignalOffer();
3216 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3217 // Create data channel and do new offer and answer.
3218 caller()->CreateDataChannel();
3219 caller()->CreateAndSetAndSignalOffer();
3220 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3221 ASSERT_NE(nullptr, caller()->data_channel());
3222 ASSERT_NE(nullptr, callee()->data_channel());
3223 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3224 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3225 // Ensure data can be sent in both directions.
3226 std::string data = "hello world";
3227 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3228 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3229 kDefaultTimeout);
3230 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3231 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3232 kDefaultTimeout);
3233}
3234
3235#ifdef HAVE_SCTP
3236
3237// This test sets up a call between two parties with audio, video and an SCTP
3238// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003239TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003240 ASSERT_TRUE(CreatePeerConnectionWrappers());
3241 ConnectFakeSignaling();
3242 // Expect that data channel created on caller side will show up for callee as
3243 // well.
3244 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003245 caller()->AddAudioVideoTracks();
3246 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003247 caller()->CreateAndSetAndSignalOffer();
3248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3249 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003250 MediaExpectations media_expectations;
3251 media_expectations.ExpectBidirectionalAudioAndVideo();
3252 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003253 // Caller data channel should already exist (it created one). Callee data
3254 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3255 ASSERT_NE(nullptr, caller()->data_channel());
3256 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3257 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3258 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3259
3260 // Ensure data can be sent in both directions.
3261 std::string data = "hello world";
3262 caller()->data_channel()->Send(DataBuffer(data));
3263 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3264 kDefaultTimeout);
3265 callee()->data_channel()->Send(DataBuffer(data));
3266 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3267 kDefaultTimeout);
3268}
3269
3270// Ensure that when the callee closes an SCTP data channel, the closing
3271// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003272TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003273 // Same procedure as above test.
3274 ASSERT_TRUE(CreatePeerConnectionWrappers());
3275 ConnectFakeSignaling();
3276 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003277 caller()->AddAudioVideoTracks();
3278 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003279 caller()->CreateAndSetAndSignalOffer();
3280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3281 ASSERT_NE(nullptr, caller()->data_channel());
3282 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3283 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3284 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3285
3286 // Close the data channel on the callee side, and wait for it to reach the
3287 // "closed" state on both sides.
3288 callee()->data_channel()->Close();
3289 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3290 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3291}
3292
Seth Hampson2f0d7022018-02-20 11:54:42 -08003293TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003294 ASSERT_TRUE(CreatePeerConnectionWrappers());
3295 ConnectFakeSignaling();
3296 webrtc::DataChannelInit init;
3297 init.id = 53;
3298 init.maxRetransmits = 52;
3299 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003300 caller()->AddAudioVideoTracks();
3301 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003302 caller()->CreateAndSetAndSignalOffer();
3303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003304 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3305 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003306 EXPECT_EQ(init.id, callee()->data_channel()->id());
3307 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3308 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3309 EXPECT_FALSE(callee()->data_channel()->negotiated());
3310}
3311
deadbeef1dcb1642017-03-29 21:08:16 -07003312// Test usrsctp's ability to process unordered data stream, where data actually
3313// arrives out of order using simulated delays. Previously there have been some
3314// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003315TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003316 // Introduce random network delays.
3317 // Otherwise it's not a true "unordered" test.
3318 virtual_socket_server()->set_delay_mean(20);
3319 virtual_socket_server()->set_delay_stddev(5);
3320 virtual_socket_server()->UpdateDelayDistribution();
3321 // Normal procedure, but with unordered data channel config.
3322 ASSERT_TRUE(CreatePeerConnectionWrappers());
3323 ConnectFakeSignaling();
3324 webrtc::DataChannelInit init;
3325 init.ordered = false;
3326 caller()->CreateDataChannel(&init);
3327 caller()->CreateAndSetAndSignalOffer();
3328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3329 ASSERT_NE(nullptr, caller()->data_channel());
3330 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3331 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3332 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3333
3334 static constexpr int kNumMessages = 100;
3335 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3336 static constexpr size_t kMaxMessageSize = 4096;
3337 // Create and send random messages.
3338 std::vector<std::string> sent_messages;
3339 for (int i = 0; i < kNumMessages; ++i) {
3340 size_t length =
3341 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3342 std::string message;
3343 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3344 caller()->data_channel()->Send(DataBuffer(message));
3345 callee()->data_channel()->Send(DataBuffer(message));
3346 sent_messages.push_back(message);
3347 }
3348
3349 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003350 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003351 caller()->data_observer()->received_message_count(),
3352 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003353 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003354 callee()->data_observer()->received_message_count(),
3355 kDefaultTimeout);
3356
3357 // Sort and compare to make sure none of the messages were corrupted.
3358 std::vector<std::string> caller_received_messages =
3359 caller()->data_observer()->messages();
3360 std::vector<std::string> callee_received_messages =
3361 callee()->data_observer()->messages();
3362 std::sort(sent_messages.begin(), sent_messages.end());
3363 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3364 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3365 EXPECT_EQ(sent_messages, caller_received_messages);
3366 EXPECT_EQ(sent_messages, callee_received_messages);
3367}
3368
3369// This test sets up a call between two parties with audio, and video. When
3370// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003371TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003372 ASSERT_TRUE(CreatePeerConnectionWrappers());
3373 ConnectFakeSignaling();
3374 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003375 caller()->AddAudioVideoTracks();
3376 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003377 caller()->CreateAndSetAndSignalOffer();
3378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3379 // Create data channel and do new offer and answer.
3380 caller()->CreateDataChannel();
3381 caller()->CreateAndSetAndSignalOffer();
3382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3383 // Caller data channel should already exist (it created one). Callee data
3384 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3385 ASSERT_NE(nullptr, caller()->data_channel());
3386 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3387 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3388 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3389 // Ensure data can be sent in both directions.
3390 std::string data = "hello world";
3391 caller()->data_channel()->Send(DataBuffer(data));
3392 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3393 kDefaultTimeout);
3394 callee()->data_channel()->Send(DataBuffer(data));
3395 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3396 kDefaultTimeout);
3397}
3398
deadbeef7914b8c2017-04-21 03:23:33 -07003399// Set up a connection initially just using SCTP data channels, later upgrading
3400// to audio/video, ensuring frames are received end-to-end. Effectively the
3401// inverse of the test above.
3402// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003403TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003404 ASSERT_TRUE(CreatePeerConnectionWrappers());
3405 ConnectFakeSignaling();
3406 // Do initial offer/answer with just data channel.
3407 caller()->CreateDataChannel();
3408 caller()->CreateAndSetAndSignalOffer();
3409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3410 // Wait until data can be sent over the data channel.
3411 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3412 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3413 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3414
3415 // Do subsequent offer/answer with two-way audio and video. Audio and video
3416 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003417 caller()->AddAudioVideoTracks();
3418 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003419 caller()->CreateAndSetAndSignalOffer();
3420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003421 MediaExpectations media_expectations;
3422 media_expectations.ExpectBidirectionalAudioAndVideo();
3423 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003424}
3425
deadbeef8b7e9ad2017-05-25 09:38:55 -07003426static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003427 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003428 GetFirstDataContentDescription(desc);
3429 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003430 dcd_offer->set_use_sctpmap(false);
3431 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3432}
3433
3434// Test that the data channel works when a spec-compliant SCTP m= section is
3435// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3436// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003437TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003438 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3439 ASSERT_TRUE(CreatePeerConnectionWrappers());
3440 ConnectFakeSignaling();
3441 caller()->CreateDataChannel();
3442 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3443 caller()->CreateAndSetAndSignalOffer();
3444 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3445 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3446 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3447 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3448
3449 // Ensure data can be sent in both directions.
3450 std::string data = "hello world";
3451 caller()->data_channel()->Send(DataBuffer(data));
3452 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3453 kDefaultTimeout);
3454 callee()->data_channel()->Send(DataBuffer(data));
3455 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3456 kDefaultTimeout);
3457}
3458
deadbeef1dcb1642017-03-29 21:08:16 -07003459#endif // HAVE_SCTP
3460
Bjorn Mellema2eb0a72018-11-09 10:13:51 -08003461// This test sets up a call between two parties with a media transport data
3462// channel.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08003463TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelEndToEnd) {
3464 PeerConnectionInterface::RTCConfiguration rtc_config;
3465 rtc_config.use_media_transport_for_data_channels = true;
3466 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3467 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3468 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3469 loopback_media_transports()->second_factory()));
3470 ConnectFakeSignaling();
3471
3472 // Expect that data channel created on caller side will show up for callee as
3473 // well.
3474 caller()->CreateDataChannel();
3475 caller()->CreateAndSetAndSignalOffer();
3476 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3477
3478 // Ensure that the media transport is ready.
3479 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3480 loopback_media_transports()->FlushAsyncInvokes();
3481
3482 // Caller data channel should already exist (it created one). Callee data
3483 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3484 ASSERT_NE(nullptr, caller()->data_channel());
3485 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3486 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3487 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3488
3489 // Ensure data can be sent in both directions.
3490 std::string data = "hello world";
3491 caller()->data_channel()->Send(DataBuffer(data));
3492 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3493 kDefaultTimeout);
3494 callee()->data_channel()->Send(DataBuffer(data));
3495 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3496 kDefaultTimeout);
3497}
3498
3499// Ensure that when the callee closes a media transport data channel, the
3500// closing procedure results in the data channel being closed for the caller
3501// as well.
3502TEST_P(PeerConnectionIntegrationTest, MediaTransportDataChannelCalleeCloses) {
3503 PeerConnectionInterface::RTCConfiguration rtc_config;
3504 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 // Create a data channel on the caller and signal it to the callee.
3512 caller()->CreateDataChannel();
3513 caller()->CreateAndSetAndSignalOffer();
3514 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3515
3516 // Ensure that the media transport is ready.
3517 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3518 loopback_media_transports()->FlushAsyncInvokes();
3519
3520 // Data channels exist and open on both ends of the connection.
3521 ASSERT_NE(nullptr, caller()->data_channel());
3522 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3523 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3524 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3525
3526 // Close the data channel on the callee side, and wait for it to reach the
3527 // "closed" state on both sides.
3528 callee()->data_channel()->Close();
3529 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3530 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3531}
3532
3533TEST_P(PeerConnectionIntegrationTest,
3534 MediaTransportDataChannelConfigSentToOtherSide) {
3535 PeerConnectionInterface::RTCConfiguration rtc_config;
3536 rtc_config.use_media_transport_for_data_channels = true;
3537 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3538 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3539 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3540 loopback_media_transports()->second_factory()));
3541 ConnectFakeSignaling();
3542
3543 // Create a data channel with a non-default configuration and signal it to the
3544 // callee.
3545 webrtc::DataChannelInit init;
3546 init.id = 53;
3547 init.maxRetransmits = 52;
3548 caller()->CreateDataChannel("data-channel", &init);
3549 caller()->CreateAndSetAndSignalOffer();
3550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3551
3552 // Ensure that the media transport is ready.
3553 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3554 loopback_media_transports()->FlushAsyncInvokes();
3555
3556 // Ensure that the data channel exists on the callee with the correct
3557 // configuration.
3558 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3559 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3560 EXPECT_EQ(init.id, callee()->data_channel()->id());
3561 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3562 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3563 EXPECT_FALSE(callee()->data_channel()->negotiated());
3564}
3565
Niels Möllerc68d2822018-11-20 14:52:05 +01003566TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalAudio) {
3567 PeerConnectionInterface::RTCConfiguration rtc_config;
3568 rtc_config.use_media_transport = true;
3569 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3570 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3571 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3572 loopback_media_transports()->second_factory()));
3573 ConnectFakeSignaling();
3574
3575 caller()->AddAudioTrack();
3576 callee()->AddAudioTrack();
3577 // Start offer/answer exchange and wait for it to complete.
3578 caller()->CreateAndSetAndSignalOffer();
3579 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3580
3581 // Ensure that the media transport is ready.
3582 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3583 loopback_media_transports()->FlushAsyncInvokes();
3584
3585 MediaExpectations media_expectations;
3586 media_expectations.ExpectBidirectionalAudio();
3587 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3588
3589 webrtc::MediaTransportPair::Stats first_stats =
3590 loopback_media_transports()->FirstStats();
3591 webrtc::MediaTransportPair::Stats second_stats =
3592 loopback_media_transports()->SecondStats();
3593
3594 EXPECT_GT(first_stats.received_audio_frames, 0);
3595 EXPECT_GE(second_stats.sent_audio_frames, first_stats.received_audio_frames);
3596
3597 EXPECT_GT(second_stats.received_audio_frames, 0);
3598 EXPECT_GE(first_stats.sent_audio_frames, second_stats.received_audio_frames);
3599}
3600
Niels Möller46879152019-01-07 15:54:47 +01003601TEST_P(PeerConnectionIntegrationTest, MediaTransportBidirectionalVideo) {
3602 PeerConnectionInterface::RTCConfiguration rtc_config;
3603 rtc_config.use_media_transport = true;
3604 rtc_config.enable_dtls_srtp = false; // SDES is required for media transport.
3605 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndMediaTransportFactory(
3606 rtc_config, rtc_config, loopback_media_transports()->first_factory(),
3607 loopback_media_transports()->second_factory()));
3608 ConnectFakeSignaling();
3609
3610 caller()->AddVideoTrack();
3611 callee()->AddVideoTrack();
3612 // Start offer/answer exchange and wait for it to complete.
3613 caller()->CreateAndSetAndSignalOffer();
3614 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3615
3616 // Ensure that the media transport is ready.
3617 loopback_media_transports()->SetState(webrtc::MediaTransportState::kWritable);
3618 loopback_media_transports()->FlushAsyncInvokes();
3619
3620 MediaExpectations media_expectations;
3621 media_expectations.ExpectBidirectionalVideo();
3622 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3623
3624 webrtc::MediaTransportPair::Stats first_stats =
3625 loopback_media_transports()->FirstStats();
3626 webrtc::MediaTransportPair::Stats second_stats =
3627 loopback_media_transports()->SecondStats();
3628
3629 EXPECT_GT(first_stats.received_video_frames, 0);
3630 EXPECT_GE(second_stats.sent_video_frames, first_stats.received_video_frames);
3631
3632 EXPECT_GT(second_stats.received_video_frames, 0);
3633 EXPECT_GE(first_stats.sent_video_frames, second_stats.received_video_frames);
3634}
3635
deadbeef1dcb1642017-03-29 21:08:16 -07003636// Test that the ICE connection and gathering states eventually reach
3637// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003638TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003639 ASSERT_TRUE(CreatePeerConnectionWrappers());
3640 ConnectFakeSignaling();
3641 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003642 caller()->AddAudioVideoTracks();
3643 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003644 caller()->CreateAndSetAndSignalOffer();
3645 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3646 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3647 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3648 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3649 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3650 // After the best candidate pair is selected and all candidates are signaled,
3651 // the ICE connection state should reach "complete".
3652 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3653 // answerer/"callee" by default) only reaches "connected". When this is
3654 // fixed, this test should be updated.
3655 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3656 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003657 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3658 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003659}
3660
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003661constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3662 cricket::PORTALLOCATOR_DISABLE_RELAY |
3663 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003664
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003665// Use a mock resolver to resolve the hostname back to the original IP on both
3666// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003667TEST_P(PeerConnectionIntegrationTest,
3668 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003669 auto caller_resolver_factory =
3670 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3671 auto callee_resolver_factory =
3672 absl::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
3673 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3674 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003675
3676 // This also verifies that the injected AsyncResolverFactory is used by
3677 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003678 EXPECT_CALL(*caller_resolver_factory, Create())
3679 .WillOnce(Return(&caller_async_resolver));
3680 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3681 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3682
3683 EXPECT_CALL(*callee_resolver_factory, Create())
3684 .WillOnce(Return(&callee_async_resolver));
3685 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3686 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3687
3688 PeerConnectionInterface::RTCConfiguration config;
3689 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3690 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3691
3692 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3693 config, std::move(caller_deps), config, std::move(callee_deps)));
3694
3695 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3696 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3697
3698 // Enable hostname candidates with mDNS names.
3699 caller()->network()->CreateMdnsResponder(network_thread());
3700 callee()->network()->CreateMdnsResponder(network_thread());
3701
3702 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003703
3704 ConnectFakeSignaling();
3705 caller()->AddAudioVideoTracks();
3706 callee()->AddAudioVideoTracks();
3707 caller()->CreateAndSetAndSignalOffer();
3708 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3709 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3710 caller()->ice_connection_state(), kDefaultTimeout);
3711 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3712 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003713
3714 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3715 "WebRTC.PeerConnection.CandidatePairType_UDP",
3716 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003717}
3718
Steve Antonede9ca52017-10-16 13:04:27 -07003719// Test that firewalling the ICE connection causes the clients to identify the
3720// disconnected state and then removing the firewall causes them to reconnect.
3721class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003722 : public PeerConnectionIntegrationBaseTest,
3723 public ::testing::WithParamInterface<
3724 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003725 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003726 PeerConnectionIntegrationIceStatesTest()
3727 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3728 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003729 }
3730
3731 void StartStunServer(const SocketAddress& server_address) {
3732 stun_server_.reset(
3733 cricket::TestStunServer::Create(network_thread(), server_address));
3734 }
3735
3736 bool TestIPv6() {
3737 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3738 }
3739
3740 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003741 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3742 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003743 }
3744
3745 std::vector<SocketAddress> CallerAddresses() {
3746 std::vector<SocketAddress> addresses;
3747 addresses.push_back(SocketAddress("1.1.1.1", 0));
3748 if (TestIPv6()) {
3749 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3750 }
3751 return addresses;
3752 }
3753
3754 std::vector<SocketAddress> CalleeAddresses() {
3755 std::vector<SocketAddress> addresses;
3756 addresses.push_back(SocketAddress("2.2.2.2", 0));
3757 if (TestIPv6()) {
3758 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3759 }
3760 return addresses;
3761 }
3762
3763 void SetUpNetworkInterfaces() {
3764 // Remove the default interfaces added by the test infrastructure.
3765 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3766 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3767
3768 // Add network addresses for test.
3769 for (const auto& caller_address : CallerAddresses()) {
3770 caller()->network()->AddInterface(caller_address);
3771 }
3772 for (const auto& callee_address : CalleeAddresses()) {
3773 callee()->network()->AddInterface(callee_address);
3774 }
3775 }
3776
3777 private:
3778 uint32_t port_allocator_flags_;
3779 std::unique_ptr<cricket::TestStunServer> stun_server_;
3780};
3781
3782// Tests that the PeerConnection goes through all the ICE gathering/connection
3783// states over the duration of the call. This includes Disconnected and Failed
3784// states, induced by putting a firewall between the peers and waiting for them
3785// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003786TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3787 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3788 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3789 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003790
3791 const SocketAddress kStunServerAddress =
3792 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3793 StartStunServer(kStunServerAddress);
3794
3795 PeerConnectionInterface::RTCConfiguration config;
3796 PeerConnectionInterface::IceServer ice_stun_server;
3797 ice_stun_server.urls.push_back(
3798 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3799 kStunServerAddress.PortAsString());
3800 config.servers.push_back(ice_stun_server);
3801
3802 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3803 ConnectFakeSignaling();
3804 SetPortAllocatorFlags();
3805 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003806 caller()->AddAudioVideoTracks();
3807 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003808
3809 // Initial state before anything happens.
3810 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3811 caller()->ice_gathering_state());
3812 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3813 caller()->ice_connection_state());
3814
3815 // Start the call by creating the offer, setting it as the local description,
3816 // then sending it to the peer who will respond with an answer. This happens
3817 // asynchronously so that we can watch the states as it runs in the
3818 // background.
3819 caller()->CreateAndSetAndSignalOffer();
3820
Steve Anton83119dd2017-11-10 16:19:52 -08003821 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3822 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003823
3824 // Verify that the observer was notified of the intermediate transitions.
3825 EXPECT_THAT(caller()->ice_connection_state_history(),
3826 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3827 PeerConnectionInterface::kIceConnectionConnected,
3828 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02003829 // After the ice transport transitions from checking to connected we revert
3830 // back to new as the standard requires, as at that point the DTLS transport
3831 // is in the "new" state while no transports are "connecting", "checking",
3832 // "failed" or disconnected. This is pretty unintuitive, and we might want to
3833 // amend the spec to handle this case more gracefully.
3834 EXPECT_THAT(
3835 caller()->peer_connection_state_history(),
3836 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02003837 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07003838 EXPECT_THAT(caller()->ice_gathering_state_history(),
3839 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3840 PeerConnectionInterface::kIceGatheringComplete));
3841
3842 // Block connections to/from the caller and wait for ICE to become
3843 // disconnected.
3844 for (const auto& caller_address : CallerAddresses()) {
3845 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3846 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003847 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003848 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3849 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003850
3851 // Let ICE re-establish by removing the firewall rules.
3852 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003853 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003854 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3855 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003856
3857 // According to RFC7675, if there is no response within 30 seconds then the
3858 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003859 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003860 constexpr int kConsentTimeout = 30000;
3861 for (const auto& caller_address : CallerAddresses()) {
3862 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3863 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003864 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003865 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3866 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003867}
3868
3869// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3870// and that the statistics in the metric observers are updated correctly.
3871TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3872 ASSERT_TRUE(CreatePeerConnectionWrappers());
3873 ConnectFakeSignaling();
3874 SetPortAllocatorFlags();
3875 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003876 caller()->AddAudioVideoTracks();
3877 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003878 caller()->CreateAndSetAndSignalOffer();
3879
3880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3881
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003882 // TODO(bugs.webrtc.org/9456): Fix it.
3883 const int num_best_ipv4 = webrtc::metrics::NumEvents(
3884 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
3885 const int num_best_ipv6 = webrtc::metrics::NumEvents(
3886 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003887 if (TestIPv6()) {
3888 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3889 // connection.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003890 EXPECT_EQ(0, num_best_ipv4);
3891 EXPECT_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003892 } else {
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003893 EXPECT_EQ(1, num_best_ipv4);
3894 EXPECT_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07003895 }
3896
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003897 EXPECT_EQ(0, webrtc::metrics::NumEvents(
3898 "WebRTC.PeerConnection.CandidatePairType_UDP",
3899 webrtc::kIceCandidatePairHostHost));
3900 EXPECT_EQ(1, webrtc::metrics::NumEvents(
3901 "WebRTC.PeerConnection.CandidatePairType_UDP",
3902 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07003903}
3904
3905constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3906 cricket::PORTALLOCATOR_DISABLE_STUN |
3907 cricket::PORTALLOCATOR_DISABLE_RELAY;
3908constexpr uint32_t kFlagsIPv6NoStun =
3909 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3910 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3911constexpr uint32_t kFlagsIPv4Stun =
3912 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3913
Seth Hampson2f0d7022018-02-20 11:54:42 -08003914INSTANTIATE_TEST_CASE_P(
3915 PeerConnectionIntegrationTest,
3916 PeerConnectionIntegrationIceStatesTest,
3917 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3918 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3919 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3920 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003921
deadbeef1dcb1642017-03-29 21:08:16 -07003922// This test sets up a call between two parties with audio and video.
3923// During the call, the caller restarts ICE and the test verifies that
3924// new ICE candidates are generated and audio and video still can flow, and the
3925// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003926TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003927 ASSERT_TRUE(CreatePeerConnectionWrappers());
3928 ConnectFakeSignaling();
3929 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003930 caller()->AddAudioVideoTracks();
3931 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003932 caller()->CreateAndSetAndSignalOffer();
3933 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3934 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3935 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003936 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3937 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07003938
3939 // To verify that the ICE restart actually occurs, get
3940 // ufrag/password/candidates before and after restart.
3941 // Create an SDP string of the first audio candidate for both clients.
3942 const webrtc::IceCandidateCollection* audio_candidates_caller =
3943 caller()->pc()->local_description()->candidates(0);
3944 const webrtc::IceCandidateCollection* audio_candidates_callee =
3945 callee()->pc()->local_description()->candidates(0);
3946 ASSERT_GT(audio_candidates_caller->count(), 0u);
3947 ASSERT_GT(audio_candidates_callee->count(), 0u);
3948 std::string caller_candidate_pre_restart;
3949 ASSERT_TRUE(
3950 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3951 std::string callee_candidate_pre_restart;
3952 ASSERT_TRUE(
3953 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3954 const cricket::SessionDescription* desc =
3955 caller()->pc()->local_description()->description();
3956 std::string caller_ufrag_pre_restart =
3957 desc->transport_infos()[0].description.ice_ufrag;
3958 desc = callee()->pc()->local_description()->description();
3959 std::string callee_ufrag_pre_restart =
3960 desc->transport_infos()[0].description.ice_ufrag;
3961
3962 // Have the caller initiate an ICE restart.
3963 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3964 caller()->CreateAndSetAndSignalOffer();
3965 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3966 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3967 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00003968 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07003969 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3970
3971 // Grab the ufrags/candidates again.
3972 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3973 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3974 ASSERT_GT(audio_candidates_caller->count(), 0u);
3975 ASSERT_GT(audio_candidates_callee->count(), 0u);
3976 std::string caller_candidate_post_restart;
3977 ASSERT_TRUE(
3978 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3979 std::string callee_candidate_post_restart;
3980 ASSERT_TRUE(
3981 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3982 desc = caller()->pc()->local_description()->description();
3983 std::string caller_ufrag_post_restart =
3984 desc->transport_infos()[0].description.ice_ufrag;
3985 desc = callee()->pc()->local_description()->description();
3986 std::string callee_ufrag_post_restart =
3987 desc->transport_infos()[0].description.ice_ufrag;
3988 // Sanity check that an ICE restart was actually negotiated in SDP.
3989 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3990 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3991 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3992 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3993
3994 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003995 MediaExpectations media_expectations;
3996 media_expectations.ExpectBidirectionalAudioAndVideo();
3997 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003998}
3999
4000// Verify that audio/video can be received end-to-end when ICE renomination is
4001// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004002TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004003 PeerConnectionInterface::RTCConfiguration config;
4004 config.enable_ice_renomination = true;
4005 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4006 ConnectFakeSignaling();
4007 // Do normal offer/answer and wait for some frames to be received in each
4008 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004009 caller()->AddAudioVideoTracks();
4010 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004011 caller()->CreateAndSetAndSignalOffer();
4012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4013 // Sanity check that ICE renomination was actually negotiated.
4014 const cricket::SessionDescription* desc =
4015 caller()->pc()->local_description()->description();
4016 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004017 ASSERT_NE(
4018 info.description.transport_options.end(),
4019 std::find(info.description.transport_options.begin(),
4020 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004021 }
4022 desc = callee()->pc()->local_description()->description();
4023 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07004024 ASSERT_NE(
4025 info.description.transport_options.end(),
4026 std::find(info.description.transport_options.begin(),
4027 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004028 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004029 MediaExpectations media_expectations;
4030 media_expectations.ExpectBidirectionalAudioAndVideo();
4031 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004032}
4033
Steve Anton6f25b092017-10-23 09:39:20 -07004034// With a max bundle policy and RTCP muxing, adding a new media description to
4035// the connection should not affect ICE at all because the new media will use
4036// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004037TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004038 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004039 PeerConnectionInterface::RTCConfiguration config;
4040 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4041 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4042 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4043 config, PeerConnectionInterface::RTCConfiguration()));
4044 ConnectFakeSignaling();
4045
Steve Anton15324772018-01-16 10:26:49 -08004046 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004047 caller()->CreateAndSetAndSignalOffer();
4048 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004049 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4050 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004051
4052 caller()->clear_ice_connection_state_history();
4053
Steve Anton15324772018-01-16 10:26:49 -08004054 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004055 caller()->CreateAndSetAndSignalOffer();
4056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4057
4058 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4059}
4060
deadbeef1dcb1642017-03-29 21:08:16 -07004061// This test sets up a call between two parties with audio and video. It then
4062// renegotiates setting the video m-line to "port 0", then later renegotiates
4063// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004064TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004065 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4066 ASSERT_TRUE(CreatePeerConnectionWrappers());
4067 ConnectFakeSignaling();
4068
4069 // Do initial negotiation, only sending media from the caller. Will result in
4070 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004071 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004072 caller()->CreateAndSetAndSignalOffer();
4073 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4074
4075 // Negotiate again, disabling the video "m=" section (the callee will set the
4076 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004077 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4078 PeerConnectionInterface::RTCOfferAnswerOptions options;
4079 options.offer_to_receive_video = 0;
4080 callee()->SetOfferAnswerOptions(options);
4081 } else {
4082 callee()->SetRemoteOfferHandler([this] {
4083 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4084 });
4085 }
deadbeef1dcb1642017-03-29 21:08:16 -07004086 caller()->CreateAndSetAndSignalOffer();
4087 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4088 // Sanity check that video "m=" section was actually rejected.
4089 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4090 callee()->pc()->local_description()->description());
4091 ASSERT_NE(nullptr, answer_video_content);
4092 ASSERT_TRUE(answer_video_content->rejected);
4093
4094 // Enable video and do negotiation again, making sure video is received
4095 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004096 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4097 PeerConnectionInterface::RTCOfferAnswerOptions options;
4098 options.offer_to_receive_video = 1;
4099 callee()->SetOfferAnswerOptions(options);
4100 } else {
4101 // The caller's transceiver is stopped, so we need to add another track.
4102 auto caller_transceiver =
4103 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4104 EXPECT_TRUE(caller_transceiver->stopped());
4105 caller()->AddVideoTrack();
4106 }
4107 callee()->AddVideoTrack();
4108 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004109 caller()->CreateAndSetAndSignalOffer();
4110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004111
deadbeef1dcb1642017-03-29 21:08:16 -07004112 // Verify the caller receives frames from the newly added stream, and the
4113 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004114 MediaExpectations media_expectations;
4115 media_expectations.CalleeExpectsSomeAudio();
4116 media_expectations.ExpectBidirectionalVideo();
4117 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004118}
4119
deadbeef1dcb1642017-03-29 21:08:16 -07004120// This tests that if we negotiate after calling CreateSender but before we
4121// have a track, then set a track later, frames from the newly-set track are
4122// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004123TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004124 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4125 ASSERT_TRUE(CreatePeerConnectionWrappers());
4126 ConnectFakeSignaling();
4127 auto caller_audio_sender =
4128 caller()->pc()->CreateSender("audio", "caller_stream");
4129 auto caller_video_sender =
4130 caller()->pc()->CreateSender("video", "caller_stream");
4131 auto callee_audio_sender =
4132 callee()->pc()->CreateSender("audio", "callee_stream");
4133 auto callee_video_sender =
4134 callee()->pc()->CreateSender("video", "callee_stream");
4135 caller()->CreateAndSetAndSignalOffer();
4136 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4137 // Wait for ICE to complete, without any tracks being set.
4138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4139 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4140 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4141 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4142 // Now set the tracks, and expect frames to immediately start flowing.
4143 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4144 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4145 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4146 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004147 MediaExpectations media_expectations;
4148 media_expectations.ExpectBidirectionalAudioAndVideo();
4149 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4150}
4151
4152// This tests that if we negotiate after calling AddTransceiver but before we
4153// have a track, then set a track later, frames from the newly-set tracks are
4154// received end-to-end.
4155TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4156 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4157 ASSERT_TRUE(CreatePeerConnectionWrappers());
4158 ConnectFakeSignaling();
4159 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4160 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4161 auto caller_audio_sender = audio_result.MoveValue()->sender();
4162 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4163 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4164 auto caller_video_sender = video_result.MoveValue()->sender();
4165 callee()->SetRemoteOfferHandler([this] {
4166 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4167 callee()->pc()->GetTransceivers()[0]->SetDirection(
4168 RtpTransceiverDirection::kSendRecv);
4169 callee()->pc()->GetTransceivers()[1]->SetDirection(
4170 RtpTransceiverDirection::kSendRecv);
4171 });
4172 caller()->CreateAndSetAndSignalOffer();
4173 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4174 // Wait for ICE to complete, without any tracks being set.
4175 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4176 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4177 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4178 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4179 // Now set the tracks, and expect frames to immediately start flowing.
4180 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4181 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4182 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4183 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4184 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4185 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4186 MediaExpectations media_expectations;
4187 media_expectations.ExpectBidirectionalAudioAndVideo();
4188 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004189}
4190
4191// This test verifies that a remote video track can be added via AddStream,
4192// and sent end-to-end. For this particular test, it's simply echoed back
4193// from the caller to the callee, rather than being forwarded to a third
4194// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004195TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004196 ASSERT_TRUE(CreatePeerConnectionWrappers());
4197 ConnectFakeSignaling();
4198 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004199 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004200 caller()->CreateAndSetAndSignalOffer();
4201 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004202 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004203
4204 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4205 // time).
4206 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4207 callee()->CreateAndSetAndSignalOffer();
4208 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4209
Seth Hampson2f0d7022018-02-20 11:54:42 -08004210 MediaExpectations media_expectations;
4211 media_expectations.ExpectBidirectionalVideo();
4212 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004213}
4214
4215// Test that we achieve the expected end-to-end connection time, using a
4216// fake clock and simulated latency on the media and signaling paths.
4217// We use a TURN<->TURN connection because this is usually the quickest to
4218// set up initially, especially when we're confident the connection will work
4219// and can start sending media before we get a STUN response.
4220//
4221// With various optimizations enabled, here are the network delays we expect to
4222// be on the critical path:
4223// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4224// signaling answer (with DTLS fingerprint).
4225// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4226// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4227// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004228TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004229 rtc::ScopedFakeClock fake_clock;
4230 // Some things use a time of "0" as a special value, so we need to start out
4231 // the fake clock at a nonzero time.
4232 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02004233 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07004234
4235 static constexpr int media_hop_delay_ms = 50;
4236 static constexpr int signaling_trip_delay_ms = 500;
4237 // For explanation of these values, see comment above.
4238 static constexpr int required_media_hops = 9;
4239 static constexpr int required_signaling_trips = 2;
4240 // For internal delays (such as posting an event asychronously).
4241 static constexpr int allowed_internal_delay_ms = 20;
4242 static constexpr int total_connection_time_ms =
4243 media_hop_delay_ms * required_media_hops +
4244 signaling_trip_delay_ms * required_signaling_trips +
4245 allowed_internal_delay_ms;
4246
4247 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4248 3478};
4249 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4250 0};
4251 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4252 3478};
4253 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4254 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004255 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4256 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004257
Seth Hampsonaed71642018-06-11 07:41:32 -07004258 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4259 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004260 // Bypass permission check on received packets so media can be sent before
4261 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004262 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4263 turn_server_1->set_enable_permission_checks(false);
4264 });
4265 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4266 turn_server_2->set_enable_permission_checks(false);
4267 });
deadbeef1dcb1642017-03-29 21:08:16 -07004268
4269 PeerConnectionInterface::RTCConfiguration client_1_config;
4270 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4271 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4272 ice_server_1.username = "test";
4273 ice_server_1.password = "test";
4274 client_1_config.servers.push_back(ice_server_1);
4275 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4276 client_1_config.presume_writable_when_fully_relayed = true;
4277
4278 PeerConnectionInterface::RTCConfiguration client_2_config;
4279 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4280 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4281 ice_server_2.username = "test";
4282 ice_server_2.password = "test";
4283 client_2_config.servers.push_back(ice_server_2);
4284 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4285 client_2_config.presume_writable_when_fully_relayed = true;
4286
4287 ASSERT_TRUE(
4288 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4289 // Set up the simulated delays.
4290 SetSignalingDelayMs(signaling_trip_delay_ms);
4291 ConnectFakeSignaling();
4292 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4293 virtual_socket_server()->UpdateDelayDistribution();
4294
4295 // Set "offer to receive audio/video" without adding any tracks, so we just
4296 // set up ICE/DTLS with no media.
4297 PeerConnectionInterface::RTCOfferAnswerOptions options;
4298 options.offer_to_receive_audio = 1;
4299 options.offer_to_receive_video = 1;
4300 caller()->SetOfferAnswerOptions(options);
4301 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004302 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
4303 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004304 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4305 // If this is not done a DCHECK can be hit in ports.cc, because a large
4306 // negative number is calculated for the rtt due to the global clock changing.
4307 caller()->pc()->Close();
4308 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07004309}
4310
Jonas Orelandbdcee282017-10-10 14:01:40 +02004311// Verify that a TurnCustomizer passed in through RTCConfiguration
4312// is actually used by the underlying TURN candidate pair.
4313// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004314TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004315 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4316 3478};
4317 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4318 0};
4319 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4320 3478};
4321 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4322 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004323 CreateTurnServer(turn_server_1_internal_address,
4324 turn_server_1_external_address);
4325 CreateTurnServer(turn_server_2_internal_address,
4326 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004327
4328 PeerConnectionInterface::RTCConfiguration client_1_config;
4329 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4330 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4331 ice_server_1.username = "test";
4332 ice_server_1.password = "test";
4333 client_1_config.servers.push_back(ice_server_1);
4334 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004335 auto* customizer1 = CreateTurnCustomizer();
4336 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004337
4338 PeerConnectionInterface::RTCConfiguration client_2_config;
4339 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4340 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4341 ice_server_2.username = "test";
4342 ice_server_2.password = "test";
4343 client_2_config.servers.push_back(ice_server_2);
4344 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004345 auto* customizer2 = CreateTurnCustomizer();
4346 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004347
4348 ASSERT_TRUE(
4349 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4350 ConnectFakeSignaling();
4351
4352 // Set "offer to receive audio/video" without adding any tracks, so we just
4353 // set up ICE/DTLS with no media.
4354 PeerConnectionInterface::RTCOfferAnswerOptions options;
4355 options.offer_to_receive_audio = 1;
4356 options.offer_to_receive_video = 1;
4357 caller()->SetOfferAnswerOptions(options);
4358 caller()->CreateAndSetAndSignalOffer();
4359 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4360
Seth Hampsonaed71642018-06-11 07:41:32 -07004361 ExpectTurnCustomizerCountersIncremented(customizer1);
4362 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004363}
4364
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004365// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4366// send media between the caller and the callee.
4367TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4368 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4369 3478};
4370 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4371
4372 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004373 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4374 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004375
4376 webrtc::PeerConnectionInterface::IceServer ice_server;
4377 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4378 ice_server.username = "test";
4379 ice_server.password = "test";
4380
4381 PeerConnectionInterface::RTCConfiguration client_1_config;
4382 client_1_config.servers.push_back(ice_server);
4383 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4384
4385 PeerConnectionInterface::RTCConfiguration client_2_config;
4386 client_2_config.servers.push_back(ice_server);
4387 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4388
4389 ASSERT_TRUE(
4390 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4391
4392 // Do normal offer/answer and wait for ICE to complete.
4393 ConnectFakeSignaling();
4394 caller()->AddAudioVideoTracks();
4395 callee()->AddAudioVideoTracks();
4396 caller()->CreateAndSetAndSignalOffer();
4397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4398 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4399 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4400
4401 MediaExpectations media_expectations;
4402 media_expectations.ExpectBidirectionalAudioAndVideo();
4403 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4404}
4405
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004406// Verify that a SSLCertificateVerifier passed in through
4407// PeerConnectionDependencies is actually used by the underlying SSL
4408// implementation to determine whether a certificate presented by the TURN
4409// server is accepted by the client. Note that openssladapter_unittest.cc
4410// contains more detailed, lower-level tests.
4411TEST_P(PeerConnectionIntegrationTest,
4412 SSLCertificateVerifierUsedForTurnConnections) {
4413 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4414 3478};
4415 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4416
4417 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4418 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004419 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4420 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004421
4422 webrtc::PeerConnectionInterface::IceServer ice_server;
4423 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4424 ice_server.username = "test";
4425 ice_server.password = "test";
4426
4427 PeerConnectionInterface::RTCConfiguration client_1_config;
4428 client_1_config.servers.push_back(ice_server);
4429 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4430
4431 PeerConnectionInterface::RTCConfiguration client_2_config;
4432 client_2_config.servers.push_back(ice_server);
4433 // Setting the type to kRelay forces the connection to go through a TURN
4434 // server.
4435 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4436
4437 // Get a copy to the pointer so we can verify calls later.
4438 rtc::TestCertificateVerifier* client_1_cert_verifier =
4439 new rtc::TestCertificateVerifier();
4440 client_1_cert_verifier->verify_certificate_ = true;
4441 rtc::TestCertificateVerifier* client_2_cert_verifier =
4442 new rtc::TestCertificateVerifier();
4443 client_2_cert_verifier->verify_certificate_ = true;
4444
4445 // Create the dependencies with the test certificate verifier.
4446 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4447 client_1_deps.tls_cert_verifier =
4448 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4449 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4450 client_2_deps.tls_cert_verifier =
4451 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4452
4453 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4454 client_1_config, std::move(client_1_deps), client_2_config,
4455 std::move(client_2_deps)));
4456 ConnectFakeSignaling();
4457
4458 // Set "offer to receive audio/video" without adding any tracks, so we just
4459 // set up ICE/DTLS with no media.
4460 PeerConnectionInterface::RTCOfferAnswerOptions options;
4461 options.offer_to_receive_audio = 1;
4462 options.offer_to_receive_video = 1;
4463 caller()->SetOfferAnswerOptions(options);
4464 caller()->CreateAndSetAndSignalOffer();
4465 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4466
4467 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4468 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004469}
4470
4471TEST_P(PeerConnectionIntegrationTest,
4472 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4473 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4474 3478};
4475 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4476
4477 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4478 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004479 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4480 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004481
4482 webrtc::PeerConnectionInterface::IceServer ice_server;
4483 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4484 ice_server.username = "test";
4485 ice_server.password = "test";
4486
4487 PeerConnectionInterface::RTCConfiguration client_1_config;
4488 client_1_config.servers.push_back(ice_server);
4489 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4490
4491 PeerConnectionInterface::RTCConfiguration client_2_config;
4492 client_2_config.servers.push_back(ice_server);
4493 // Setting the type to kRelay forces the connection to go through a TURN
4494 // server.
4495 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4496
4497 // Get a copy to the pointer so we can verify calls later.
4498 rtc::TestCertificateVerifier* client_1_cert_verifier =
4499 new rtc::TestCertificateVerifier();
4500 client_1_cert_verifier->verify_certificate_ = false;
4501 rtc::TestCertificateVerifier* client_2_cert_verifier =
4502 new rtc::TestCertificateVerifier();
4503 client_2_cert_verifier->verify_certificate_ = false;
4504
4505 // Create the dependencies with the test certificate verifier.
4506 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4507 client_1_deps.tls_cert_verifier =
4508 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4509 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4510 client_2_deps.tls_cert_verifier =
4511 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4512
4513 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4514 client_1_config, std::move(client_1_deps), client_2_config,
4515 std::move(client_2_deps)));
4516 ConnectFakeSignaling();
4517
4518 // Set "offer to receive audio/video" without adding any tracks, so we just
4519 // set up ICE/DTLS with no media.
4520 PeerConnectionInterface::RTCOfferAnswerOptions options;
4521 options.offer_to_receive_audio = 1;
4522 options.offer_to_receive_video = 1;
4523 caller()->SetOfferAnswerOptions(options);
4524 caller()->CreateAndSetAndSignalOffer();
4525 bool wait_res = true;
4526 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4527 // properly, should be able to just wait for a state of "failed" instead of
4528 // waiting a fixed 10 seconds.
4529 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4530 ASSERT_FALSE(wait_res);
4531
4532 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4533 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004534}
4535
deadbeefc964d0b2017-04-03 10:03:35 -07004536// Test that audio and video flow end-to-end when codec names don't use the
4537// expected casing, given that they're supposed to be case insensitive. To test
4538// this, all but one codec is removed from each media description, and its
4539// casing is changed.
4540//
4541// In the past, this has regressed and caused crashes/black video, due to the
4542// fact that code at some layers was doing case-insensitive comparisons and
4543// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004544TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004545 ASSERT_TRUE(CreatePeerConnectionWrappers());
4546 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004547 caller()->AddAudioVideoTracks();
4548 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004549
4550 // Remove all but one audio/video codec (opus and VP8), and change the
4551 // casing of the caller's generated offer.
4552 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4553 cricket::AudioContentDescription* audio =
4554 GetFirstAudioContentDescription(description);
4555 ASSERT_NE(nullptr, audio);
4556 auto audio_codecs = audio->codecs();
4557 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4558 [](const cricket::AudioCodec& codec) {
4559 return codec.name != "opus";
4560 }),
4561 audio_codecs.end());
4562 ASSERT_EQ(1u, audio_codecs.size());
4563 audio_codecs[0].name = "OpUs";
4564 audio->set_codecs(audio_codecs);
4565
4566 cricket::VideoContentDescription* video =
4567 GetFirstVideoContentDescription(description);
4568 ASSERT_NE(nullptr, video);
4569 auto video_codecs = video->codecs();
4570 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4571 [](const cricket::VideoCodec& codec) {
4572 return codec.name != "VP8";
4573 }),
4574 video_codecs.end());
4575 ASSERT_EQ(1u, video_codecs.size());
4576 video_codecs[0].name = "vP8";
4577 video->set_codecs(video_codecs);
4578 });
4579
4580 caller()->CreateAndSetAndSignalOffer();
4581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4582
4583 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004584 MediaExpectations media_expectations;
4585 media_expectations.ExpectBidirectionalAudioAndVideo();
4586 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004587}
4588
Jonas Oreland49ac5952018-09-26 16:04:32 +02004589TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004590 ASSERT_TRUE(CreatePeerConnectionWrappers());
4591 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004592 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004593 caller()->CreateAndSetAndSignalOffer();
4594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004595 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004596 MediaExpectations media_expectations;
4597 media_expectations.CalleeExpectsSomeAudio(1);
4598 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004599 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004600 auto receiver = callee()->pc()->GetReceivers()[0];
4601 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004602 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004603 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4604 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004605 sources[0].source_id());
4606 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4607}
4608
4609TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4610 ASSERT_TRUE(CreatePeerConnectionWrappers());
4611 ConnectFakeSignaling();
4612 caller()->AddVideoTrack();
4613 caller()->CreateAndSetAndSignalOffer();
4614 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4615 // Wait for one video frame to be received by the callee.
4616 MediaExpectations media_expectations;
4617 media_expectations.CalleeExpectsSomeVideo(1);
4618 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4619 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4620 auto receiver = callee()->pc()->GetReceivers()[0];
4621 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4622 auto sources = receiver->GetSources();
4623 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4624 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4625 sources[0].source_id());
4626 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004627}
4628
deadbeef2f425aa2017-04-14 10:41:32 -07004629// Test that if a track is removed and added again with a different stream ID,
4630// the new stream ID is successfully communicated in SDP and media continues to
4631// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004632// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4633// it will not reuse a transceiver that has already been sending. After creating
4634// a new transceiver it tries to create an offer with two senders of the same
4635// track ids and it fails.
4636TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004637 ASSERT_TRUE(CreatePeerConnectionWrappers());
4638 ConnectFakeSignaling();
4639
deadbeef2f425aa2017-04-14 10:41:32 -07004640 // Add track using stream 1, do offer/answer.
4641 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4642 caller()->CreateLocalAudioTrack();
4643 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004644 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004645 caller()->CreateAndSetAndSignalOffer();
4646 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004647 {
4648 MediaExpectations media_expectations;
4649 media_expectations.CalleeExpectsSomeAudio(1);
4650 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4651 }
deadbeef2f425aa2017-04-14 10:41:32 -07004652 // Remove the sender, and create a new one with the new stream.
4653 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004654 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004655 caller()->CreateAndSetAndSignalOffer();
4656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4657 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004658 {
4659 MediaExpectations media_expectations;
4660 media_expectations.CalleeExpectsSomeAudio();
4661 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4662 }
deadbeef2f425aa2017-04-14 10:41:32 -07004663}
4664
Seth Hampson2f0d7022018-02-20 11:54:42 -08004665TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004666 ASSERT_TRUE(CreatePeerConnectionWrappers());
4667 ConnectFakeSignaling();
4668
Karl Wiberg918f50c2018-07-05 11:40:33 +02004669 auto output = absl::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Elad Alon99c3fe52017-10-13 16:29:40 +02004670 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4671 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4672 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004673 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4674 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004675
Steve Anton15324772018-01-16 10:26:49 -08004676 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004677 caller()->CreateAndSetAndSignalOffer();
4678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4679}
4680
Steve Antonede9ca52017-10-16 13:04:27 -07004681// Test that if candidates are only signaled by applying full session
4682// descriptions (instead of using AddIceCandidate), the peers can connect to
4683// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004684TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004685 ASSERT_TRUE(CreatePeerConnectionWrappers());
4686 // Each side will signal the session descriptions but not candidates.
4687 ConnectFakeSignalingForSdpOnly();
4688
4689 // Add audio video track and exchange the initial offer/answer with media
4690 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004691 caller()->AddAudioVideoTracks();
4692 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004693 caller()->CreateAndSetAndSignalOffer();
4694
4695 // Wait for all candidates to be gathered on both the caller and callee.
4696 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4697 caller()->ice_gathering_state(), kDefaultTimeout);
4698 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4699 callee()->ice_gathering_state(), kDefaultTimeout);
4700
4701 // The candidates will now be included in the session description, so
4702 // signaling them will start the ICE connection.
4703 caller()->CreateAndSetAndSignalOffer();
4704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4705
4706 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004707 MediaExpectations media_expectations;
4708 media_expectations.ExpectBidirectionalAudioAndVideo();
4709 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004710}
4711
henrika5f6bf242017-11-01 11:06:56 +01004712// Test that SetAudioPlayout can be used to disable audio playout from the
4713// start, then later enable it. This may be useful, for example, if the caller
4714// needs to play a local ringtone until some event occurs, after which it
4715// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004716TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004717 ASSERT_TRUE(CreatePeerConnectionWrappers());
4718 ConnectFakeSignaling();
4719
4720 // Set up audio-only call where audio playout is disabled on caller's side.
4721 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004722 caller()->AddAudioTrack();
4723 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004724 caller()->CreateAndSetAndSignalOffer();
4725 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4726
4727 // Pump messages for a second.
4728 WAIT(false, 1000);
4729 // Since audio playout is disabled, the caller shouldn't have received
4730 // anything (at the playout level, at least).
4731 EXPECT_EQ(0, caller()->audio_frames_received());
4732 // As a sanity check, make sure the callee (for which playout isn't disabled)
4733 // did still see frames on its audio level.
4734 ASSERT_GT(callee()->audio_frames_received(), 0);
4735
4736 // Enable playout again, and ensure audio starts flowing.
4737 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004738 MediaExpectations media_expectations;
4739 media_expectations.ExpectBidirectionalAudio();
4740 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004741}
4742
4743double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4744 auto report = pc->NewGetStats();
4745 auto track_stats_list =
4746 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4747 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4748 for (const auto* track_stats : track_stats_list) {
4749 if (track_stats->remote_source.is_defined() &&
4750 *track_stats->remote_source) {
4751 remote_track_stats = track_stats;
4752 break;
4753 }
4754 }
4755
4756 if (!remote_track_stats->total_audio_energy.is_defined()) {
4757 return 0.0;
4758 }
4759 return *remote_track_stats->total_audio_energy;
4760}
4761
4762// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4763// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004764TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004765 DisableAudioPlayoutStillGeneratesAudioStats) {
4766 ASSERT_TRUE(CreatePeerConnectionWrappers());
4767 ConnectFakeSignaling();
4768
4769 // Set up audio-only call where playout is disabled but audio-processing is
4770 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004771 caller()->AddAudioTrack();
4772 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004773 caller()->pc()->SetAudioPlayout(false);
4774
4775 caller()->CreateAndSetAndSignalOffer();
4776 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4777
4778 // Wait for the callee to receive audio stats.
4779 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4780}
4781
henrika4f167df2017-11-01 14:45:55 +01004782// Test that SetAudioRecording can be used to disable audio recording from the
4783// start, then later enable it. This may be useful, for example, if the caller
4784// wants to ensure that no audio resources are active before a certain state
4785// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004786TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004787 ASSERT_TRUE(CreatePeerConnectionWrappers());
4788 ConnectFakeSignaling();
4789
4790 // Set up audio-only call where audio recording is disabled on caller's side.
4791 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004792 caller()->AddAudioTrack();
4793 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004794 caller()->CreateAndSetAndSignalOffer();
4795 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4796
4797 // Pump messages for a second.
4798 WAIT(false, 1000);
4799 // Since caller has disabled audio recording, the callee shouldn't have
4800 // received anything.
4801 EXPECT_EQ(0, callee()->audio_frames_received());
4802 // As a sanity check, make sure the caller did still see frames on its
4803 // audio level since audio recording is enabled on the calle side.
4804 ASSERT_GT(caller()->audio_frames_received(), 0);
4805
4806 // Enable audio recording again, and ensure audio starts flowing.
4807 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004808 MediaExpectations media_expectations;
4809 media_expectations.ExpectBidirectionalAudio();
4810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004811}
4812
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004813// Test that after closing PeerConnections, they stop sending any packets (ICE,
4814// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004815TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004816 // Set up audio/video/data, wait for some frames to be received.
4817 ASSERT_TRUE(CreatePeerConnectionWrappers());
4818 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004819 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004820#ifdef HAVE_SCTP
4821 caller()->CreateDataChannel();
4822#endif
4823 caller()->CreateAndSetAndSignalOffer();
4824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004825 MediaExpectations media_expectations;
4826 media_expectations.CalleeExpectsSomeAudioAndVideo();
4827 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004828 // Close PeerConnections.
4829 caller()->pc()->Close();
4830 callee()->pc()->Close();
4831 // Pump messages for a second, and ensure no new packets end up sent.
4832 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4833 WAIT(false, 1000);
4834 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4835 EXPECT_EQ(sent_packets_a, sent_packets_b);
4836}
4837
Steve Anton7eca0932018-03-30 15:18:41 -07004838// Test that transport stats are generated by the RTCStatsCollector for a
4839// connection that only involves data channels. This is a regression test for
4840// crbug.com/826972.
4841#ifdef HAVE_SCTP
4842TEST_P(PeerConnectionIntegrationTest,
4843 TransportStatsReportedForDataChannelOnlyConnection) {
4844 ASSERT_TRUE(CreatePeerConnectionWrappers());
4845 ConnectFakeSignaling();
4846 caller()->CreateDataChannel();
4847
4848 caller()->CreateAndSetAndSignalOffer();
4849 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4850 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4851
4852 auto caller_report = caller()->NewGetStats();
4853 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4854 auto callee_report = callee()->NewGetStats();
4855 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4856}
4857#endif // HAVE_SCTP
4858
Qingsi Wang7685e862018-06-11 20:15:46 -07004859TEST_P(PeerConnectionIntegrationTest,
4860 IceEventsGeneratedAndLoggedInRtcEventLog) {
4861 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4862 ConnectFakeSignaling();
4863 PeerConnectionInterface::RTCOfferAnswerOptions options;
4864 options.offer_to_receive_audio = 1;
4865 caller()->SetOfferAnswerOptions(options);
4866 caller()->CreateAndSetAndSignalOffer();
4867 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4868 ASSERT_NE(nullptr, caller()->event_log_factory());
4869 ASSERT_NE(nullptr, callee()->event_log_factory());
4870 webrtc::FakeRtcEventLog* caller_event_log =
4871 static_cast<webrtc::FakeRtcEventLog*>(
4872 caller()->event_log_factory()->last_log_created());
4873 webrtc::FakeRtcEventLog* callee_event_log =
4874 static_cast<webrtc::FakeRtcEventLog*>(
4875 callee()->event_log_factory()->last_log_created());
4876 ASSERT_NE(nullptr, caller_event_log);
4877 ASSERT_NE(nullptr, callee_event_log);
4878 int caller_ice_config_count = caller_event_log->GetEventCount(
4879 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4880 int caller_ice_event_count = caller_event_log->GetEventCount(
4881 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4882 int callee_ice_config_count = callee_event_log->GetEventCount(
4883 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4884 int callee_ice_event_count = callee_event_log->GetEventCount(
4885 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4886 EXPECT_LT(0, caller_ice_config_count);
4887 EXPECT_LT(0, caller_ice_event_count);
4888 EXPECT_LT(0, callee_ice_config_count);
4889 EXPECT_LT(0, callee_ice_event_count);
4890}
4891
Seth Hampson2f0d7022018-02-20 11:54:42 -08004892INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4893 PeerConnectionIntegrationTest,
4894 Values(SdpSemantics::kPlanB,
4895 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004896
Steve Anton74255ff2018-01-24 18:32:57 -08004897// Tests that verify interoperability between Plan B and Unified Plan
4898// PeerConnections.
4899class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004900 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004901 public ::testing::WithParamInterface<
4902 std::tuple<SdpSemantics, SdpSemantics>> {
4903 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004904 // Setting the SdpSemantics for the base test to kDefault does not matter
4905 // because we specify not to use the test semantics when creating
4906 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004907 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004908 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004909 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004910 callee_semantics_(std::get<1>(GetParam())) {}
4911
4912 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004913 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4914 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004915 }
4916
4917 const SdpSemantics caller_semantics_;
4918 const SdpSemantics callee_semantics_;
4919};
4920
4921TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4922 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4923 ConnectFakeSignaling();
4924
4925 caller()->CreateAndSetAndSignalOffer();
4926 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4927}
4928
4929TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4930 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4931 ConnectFakeSignaling();
4932 auto audio_sender = caller()->AddAudioTrack();
4933
4934 caller()->CreateAndSetAndSignalOffer();
4935 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4936
4937 // Verify that one audio receiver has been created on the remote and that it
4938 // has the same track ID as the sending track.
4939 auto receivers = callee()->pc()->GetReceivers();
4940 ASSERT_EQ(1u, receivers.size());
4941 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4942 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4943
Seth Hampson2f0d7022018-02-20 11:54:42 -08004944 MediaExpectations media_expectations;
4945 media_expectations.CalleeExpectsSomeAudio();
4946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004947}
4948
4949TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4950 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4951 ConnectFakeSignaling();
4952 auto video_sender = caller()->AddVideoTrack();
4953 auto audio_sender = caller()->AddAudioTrack();
4954
4955 caller()->CreateAndSetAndSignalOffer();
4956 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4957
4958 // Verify that one audio and one video receiver have been created on the
4959 // remote and that they have the same track IDs as the sending tracks.
4960 auto audio_receivers =
4961 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4962 ASSERT_EQ(1u, audio_receivers.size());
4963 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4964 auto video_receivers =
4965 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4966 ASSERT_EQ(1u, video_receivers.size());
4967 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4968
Seth Hampson2f0d7022018-02-20 11:54:42 -08004969 MediaExpectations media_expectations;
4970 media_expectations.CalleeExpectsSomeAudioAndVideo();
4971 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004972}
4973
4974TEST_P(PeerConnectionIntegrationInteropTest,
4975 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4976 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4977 ConnectFakeSignaling();
4978 caller()->AddAudioVideoTracks();
4979 callee()->AddAudioVideoTracks();
4980
4981 caller()->CreateAndSetAndSignalOffer();
4982 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4983
Seth Hampson2f0d7022018-02-20 11:54:42 -08004984 MediaExpectations media_expectations;
4985 media_expectations.ExpectBidirectionalAudioAndVideo();
4986 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004987}
4988
4989TEST_P(PeerConnectionIntegrationInteropTest,
4990 ReverseRolesOneAudioLocalToOneVideoRemote) {
4991 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4992 ConnectFakeSignaling();
4993 caller()->AddAudioTrack();
4994 callee()->AddVideoTrack();
4995
4996 caller()->CreateAndSetAndSignalOffer();
4997 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4998
4999 // Verify that only the audio track has been negotiated.
5000 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5001 // Might also check that the callee's NegotiationNeeded flag is set.
5002
5003 // Reverse roles.
5004 callee()->CreateAndSetAndSignalOffer();
5005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5006
Seth Hampson2f0d7022018-02-20 11:54:42 -08005007 MediaExpectations media_expectations;
5008 media_expectations.CallerExpectsSomeVideo();
5009 media_expectations.CalleeExpectsSomeAudio();
5010 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005011}
5012
Steve Antonba42e992018-04-09 14:10:01 -07005013INSTANTIATE_TEST_CASE_P(
5014 PeerConnectionIntegrationTest,
5015 PeerConnectionIntegrationInteropTest,
5016 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5017 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5018
5019// Test that if the Unified Plan side offers two video tracks then the Plan B
5020// side will only see the first one and ignore the second.
5021TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005022 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5023 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005024 ConnectFakeSignaling();
5025 auto first_sender = caller()->AddVideoTrack();
5026 caller()->AddVideoTrack();
5027
5028 caller()->CreateAndSetAndSignalOffer();
5029 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5030
5031 // Verify that there is only one receiver and it corresponds to the first
5032 // added track.
5033 auto receivers = callee()->pc()->GetReceivers();
5034 ASSERT_EQ(1u, receivers.size());
5035 EXPECT_TRUE(receivers[0]->track()->enabled());
5036 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5037
Seth Hampson2f0d7022018-02-20 11:54:42 -08005038 MediaExpectations media_expectations;
5039 media_expectations.CalleeExpectsSomeVideo();
5040 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005041}
5042
Steve Anton2bed3972019-01-04 17:04:30 -08005043// Test that if the initial offer tagged BUNDLE section is rejected due to its
5044// associated RtpTransceiver being stopped and another transceiver is added,
5045// then renegotiation causes the callee to receive the new video track without
5046// error.
5047// This is a regression test for bugs.webrtc.org/9954
5048TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5049 ReOfferWithStoppedBundleTaggedTransceiver) {
5050 RTCConfiguration config;
5051 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5052 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5053 ConnectFakeSignaling();
5054 auto audio_transceiver_or_error =
5055 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5056 ASSERT_TRUE(audio_transceiver_or_error.ok());
5057 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5058
5059 caller()->CreateAndSetAndSignalOffer();
5060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5061 {
5062 MediaExpectations media_expectations;
5063 media_expectations.CalleeExpectsSomeAudio();
5064 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5065 }
5066
5067 audio_transceiver->Stop();
5068 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5069
5070 caller()->CreateAndSetAndSignalOffer();
5071 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5072 {
5073 MediaExpectations media_expectations;
5074 media_expectations.CalleeExpectsSomeVideo();
5075 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5076 }
5077}
5078
deadbeef1dcb1642017-03-29 21:08:16 -07005079} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005080} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005081
5082#endif // if !defined(THREAD_SANITIZER)