blob: 312407cbad9948386eee1e40d9718f3285e342bf [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070036#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080045#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080046#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "pc/test/fakeaudiocapturemodule.h"
48#include "pc/test/fakeperiodicvideocapturer.h"
49#include "pc/test/fakertccertificategenerator.h"
50#include "pc/test/fakevideotrackrenderer.h"
51#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070053#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020056#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070057
58using cricket::ContentInfo;
59using cricket::FakeWebRtcVideoDecoder;
60using cricket::FakeWebRtcVideoDecoderFactory;
61using cricket::FakeWebRtcVideoEncoder;
62using cricket::FakeWebRtcVideoEncoderFactory;
63using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070064using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080065using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070066using ::testing::ElementsAre;
67using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070068using webrtc::DataBuffer;
69using webrtc::DataChannelInterface;
70using webrtc::DtmfSender;
71using webrtc::DtmfSenderInterface;
72using webrtc::DtmfSenderObserverInterface;
73using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080074using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070075using webrtc::MediaConstraintsInterface;
76using webrtc::MediaStreamInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070083using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070084using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080085using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070086using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070087using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080088using webrtc::RTCErrorType;
Steve Anton74255ff2018-01-24 18:32:57 -080089using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010090using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080091using webrtc::RtpSenderInterface;
92using webrtc::RtpTransceiverDirection;
93using webrtc::RtpTransceiverInit;
94using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080095using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080096using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070097using webrtc::SessionDescriptionInterface;
98using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -080099using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700100
101namespace {
102
103static const int kDefaultTimeout = 10000;
104static const int kMaxWaitForStatsMs = 3000;
105static const int kMaxWaitForActivationMs = 5000;
106static const int kMaxWaitForFramesMs = 10000;
107// Default number of audio/video frames to wait for before considering a test
108// successful.
109static const int kDefaultExpectedAudioFrameCount = 3;
110static const int kDefaultExpectedVideoFrameCount = 3;
111
deadbeef1dcb1642017-03-29 21:08:16 -0700112static const char kDataChannelLabel[] = "data_channel";
113
114// SRTP cipher name negotiated by the tests. This must be updated if the
115// default changes.
Tommi8e545ee2018-02-08 16:25:20 +0000116static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
deadbeef1dcb1642017-03-29 21:08:16 -0700117static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
118
Steve Antonede9ca52017-10-16 13:04:27 -0700119static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
120
deadbeef1dcb1642017-03-29 21:08:16 -0700121// Helper function for constructing offer/answer options to initiate an ICE
122// restart.
123PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
124 PeerConnectionInterface::RTCOfferAnswerOptions options;
125 options.ice_restart = true;
126 return options;
127}
128
deadbeefd8ad7882017-04-18 16:01:17 -0700129// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
130// attribute from received SDP, simulating a legacy endpoint.
131void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
132 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800133 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700134 }
135 desc->set_msid_supported(false);
136}
137
zhihuangf8164932017-05-19 13:09:47 -0700138int FindFirstMediaStatsIndexByKind(
139 const std::string& kind,
140 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
141 media_stats_vec) {
142 for (size_t i = 0; i < media_stats_vec.size(); i++) {
143 if (media_stats_vec[i]->kind.ValueToString() == kind) {
144 return i;
145 }
146 }
147 return -1;
148}
149
deadbeef1dcb1642017-03-29 21:08:16 -0700150class SignalingMessageReceiver {
151 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800152 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700153 virtual void ReceiveIceMessage(const std::string& sdp_mid,
154 int sdp_mline_index,
155 const std::string& msg) = 0;
156
157 protected:
158 SignalingMessageReceiver() {}
159 virtual ~SignalingMessageReceiver() {}
160};
161
162class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
163 public:
164 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
165 : expected_media_type_(media_type) {}
166
167 void OnFirstPacketReceived(cricket::MediaType media_type) override {
168 ASSERT_EQ(expected_media_type_, media_type);
169 first_packet_received_ = true;
170 }
171
172 bool first_packet_received() const { return first_packet_received_; }
173
174 virtual ~MockRtpReceiverObserver() {}
175
176 private:
177 bool first_packet_received_ = false;
178 cricket::MediaType expected_media_type_;
179};
180
181// Helper class that wraps a peer connection, observes it, and can accept
182// signaling messages from another wrapper.
183//
184// Uses a fake network, fake A/V capture, and optionally fake
185// encoders/decoders, though they aren't used by default since they don't
186// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700187// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800188// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700189class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800190 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700191 public:
192 // Different factory methods for convenience.
193 // TODO(deadbeef): Could use the pattern of:
194 //
195 // PeerConnectionWrapper =
196 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
197 //
198 // To reduce some code duplication.
199 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
200 const std::string& debug_name,
201 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
202 rtc::Thread* network_thread,
203 rtc::Thread* worker_thread) {
204 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
205 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
206 network_thread, worker_thread)) {
207 delete client;
208 return nullptr;
209 }
210 return client;
211 }
212
deadbeef2f425aa2017-04-14 10:41:32 -0700213 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
214 return peer_connection_factory_.get();
215 }
216
deadbeef1dcb1642017-03-29 21:08:16 -0700217 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
218
219 // If a signaling message receiver is set (via ConnectFakeSignaling), this
220 // will set the whole offer/answer exchange in motion. Just need to wait for
221 // the signaling state to reach "stable".
222 void CreateAndSetAndSignalOffer() {
223 auto offer = CreateOffer();
224 ASSERT_NE(nullptr, offer);
225 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
226 }
227
228 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
229 // when a remote offer is received (via fake signaling) and an answer is
230 // generated. By default, uses default options.
231 void SetOfferAnswerOptions(
232 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
233 offer_answer_options_ = options;
234 }
235
236 // Set a callback to be invoked when SDP is received via the fake signaling
237 // channel, which provides an opportunity to munge (modify) the SDP. This is
238 // used to test SDP being applied that a PeerConnection would normally not
239 // generate, but a non-JSEP endpoint might.
240 void SetReceivedSdpMunger(
241 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100242 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700243 }
244
deadbeefc964d0b2017-04-03 10:03:35 -0700245 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700246 // generated.
247 void SetGeneratedSdpMunger(
248 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100249 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700250 }
251
Seth Hampson2f0d7022018-02-20 11:54:42 -0800252 // Set a callback to be invoked when a remote offer is received via the fake
253 // signaling channel. This provides an opportunity to change the
254 // PeerConnection state before an answer is created and sent to the caller.
255 void SetRemoteOfferHandler(std::function<void()> handler) {
256 remote_offer_handler_ = std::move(handler);
257 }
258
Steve Antonede9ca52017-10-16 13:04:27 -0700259 // Every ICE connection state in order that has been seen by the observer.
260 std::vector<PeerConnectionInterface::IceConnectionState>
261 ice_connection_state_history() const {
262 return ice_connection_state_history_;
263 }
Steve Anton6f25b092017-10-23 09:39:20 -0700264 void clear_ice_connection_state_history() {
265 ice_connection_state_history_.clear();
266 }
Steve Antonede9ca52017-10-16 13:04:27 -0700267
268 // Every ICE gathering state in order that has been seen by the observer.
269 std::vector<PeerConnectionInterface::IceGatheringState>
270 ice_gathering_state_history() const {
271 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700272 }
273
Steve Anton15324772018-01-16 10:26:49 -0800274 void AddAudioVideoTracks() {
275 AddAudioTrack();
276 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700277 }
278
Steve Anton74255ff2018-01-24 18:32:57 -0800279 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
280 return AddTrack(CreateLocalAudioTrack());
281 }
deadbeef1dcb1642017-03-29 21:08:16 -0700282
Steve Anton74255ff2018-01-24 18:32:57 -0800283 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
284 return AddTrack(CreateLocalVideoTrack());
285 }
deadbeef1dcb1642017-03-29 21:08:16 -0700286
287 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
288 FakeConstraints constraints;
289 // Disable highpass filter so that we can get all the test audio frames.
290 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
291 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
292 peer_connection_factory_->CreateAudioSource(&constraints);
293 // TODO(perkj): Test audio source when it is implemented. Currently audio
294 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700295 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700296 source);
297 }
298
299 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700300 return CreateLocalVideoTrackInternal(FakeConstraints(),
301 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
303
304 rtc::scoped_refptr<webrtc::VideoTrackInterface>
305 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700306 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700307 }
308
309 rtc::scoped_refptr<webrtc::VideoTrackInterface>
310 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700311 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700312 }
313
Steve Anton74255ff2018-01-24 18:32:57 -0800314 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
315 rtc::scoped_refptr<MediaStreamTrackInterface> track,
316 const std::vector<std::string>& stream_labels = {}) {
Steve Anton15324772018-01-16 10:26:49 -0800317 auto result = pc()->AddTrack(track, stream_labels);
318 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800319 return result.MoveValue();
320 }
321
322 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
323 cricket::MediaType media_type) {
324 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
325 for (auto receiver : pc()->GetReceivers()) {
326 if (receiver->media_type() == media_type) {
327 receivers.push_back(receiver);
328 }
329 }
330 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
Seth Hampson2f0d7022018-02-20 11:54:42 -0800333 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
334 cricket::MediaType media_type) {
335 for (auto transceiver : pc()->GetTransceivers()) {
336 if (transceiver->receiver()->media_type() == media_type) {
337 return transceiver;
338 }
339 }
340 return nullptr;
341 }
342
deadbeef1dcb1642017-03-29 21:08:16 -0700343 bool SignalingStateStable() {
344 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
345 }
346
347 void CreateDataChannel() { CreateDataChannel(nullptr); }
348
349 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700350 CreateDataChannel(kDataChannelLabel, init);
351 }
352
353 void CreateDataChannel(const std::string& label,
354 const webrtc::DataChannelInit* init) {
355 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700356 ASSERT_TRUE(data_channel_.get() != nullptr);
357 data_observer_.reset(new MockDataChannelObserver(data_channel_));
358 }
359
360 DataChannelInterface* data_channel() { return data_channel_; }
361 const MockDataChannelObserver* data_observer() const {
362 return data_observer_.get();
363 }
364
365 int audio_frames_received() const {
366 return fake_audio_capture_module_->frames_received();
367 }
368
369 // Takes minimum of video frames received for each track.
370 //
371 // Can be used like:
372 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
373 //
374 // To ensure that all video tracks received at least a certain number of
375 // frames.
376 int min_video_frames_received_per_track() const {
377 int min_frames = INT_MAX;
378 if (video_decoder_factory_enabled_) {
379 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
380 fake_video_decoder_factory_->decoders();
381 if (decoders.empty()) {
382 return 0;
383 }
384 for (FakeWebRtcVideoDecoder* decoder : decoders) {
385 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
386 }
387 return min_frames;
388 } else {
389 if (fake_video_renderers_.empty()) {
390 return 0;
391 }
392
393 for (const auto& pair : fake_video_renderers_) {
394 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
395 }
396 return min_frames;
397 }
398 }
399
400 // In contrast to the above, sums the video frames received for all tracks.
401 // Can be used to verify that no video frames were received, or that the
402 // counts didn't increase.
403 int total_video_frames_received() const {
404 int total = 0;
405 if (video_decoder_factory_enabled_) {
406 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
407 fake_video_decoder_factory_->decoders();
408 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
409 total += decoder->GetNumFramesReceived();
410 }
411 } else {
412 for (const auto& pair : fake_video_renderers_) {
413 total += pair.second->num_rendered_frames();
414 }
415 for (const auto& renderer : removed_fake_video_renderers_) {
416 total += renderer->num_rendered_frames();
417 }
418 }
419 return total;
420 }
421
422 // Returns a MockStatsObserver in a state after stats gathering finished,
423 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700424 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700425 webrtc::MediaStreamTrackInterface* track) {
426 rtc::scoped_refptr<MockStatsObserver> observer(
427 new rtc::RefCountedObject<MockStatsObserver>());
428 EXPECT_TRUE(peer_connection_->GetStats(
429 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
430 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
431 return observer;
432 }
433
434 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
436 return OldGetStatsForTrack(nullptr);
437 }
438
439 // Synchronously gets stats and returns them. If it times out, fails the test
440 // and returns null.
441 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
442 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
443 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
444 peer_connection_->GetStats(callback);
445 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
446 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
448
449 int rendered_width() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->width();
454 }
455
456 int rendered_height() {
457 EXPECT_FALSE(fake_video_renderers_.empty());
458 return fake_video_renderers_.empty()
459 ? 0
460 : fake_video_renderers_.begin()->second->height();
461 }
462
463 double rendered_aspect_ratio() {
464 if (rendered_height() == 0) {
465 return 0.0;
466 }
467 return static_cast<double>(rendered_width()) / rendered_height();
468 }
469
470 webrtc::VideoRotation rendered_rotation() {
471 EXPECT_FALSE(fake_video_renderers_.empty());
472 return fake_video_renderers_.empty()
473 ? webrtc::kVideoRotation_0
474 : fake_video_renderers_.begin()->second->rotation();
475 }
476
477 int local_rendered_width() {
478 return local_video_renderer_ ? local_video_renderer_->width() : 0;
479 }
480
481 int local_rendered_height() {
482 return local_video_renderer_ ? local_video_renderer_->height() : 0;
483 }
484
485 double local_rendered_aspect_ratio() {
486 if (local_rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(local_rendered_width()) /
490 local_rendered_height();
491 }
492
493 size_t number_of_remote_streams() {
494 if (!pc()) {
495 return 0;
496 }
497 return pc()->remote_streams()->count();
498 }
499
500 StreamCollectionInterface* remote_streams() const {
501 if (!pc()) {
502 ADD_FAILURE();
503 return nullptr;
504 }
505 return pc()->remote_streams();
506 }
507
508 StreamCollectionInterface* local_streams() {
509 if (!pc()) {
510 ADD_FAILURE();
511 return nullptr;
512 }
513 return pc()->local_streams();
514 }
515
516 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
517 return pc()->signaling_state();
518 }
519
520 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
521 return pc()->ice_connection_state();
522 }
523
524 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
525 return pc()->ice_gathering_state();
526 }
527
528 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
529 // GetReceivers. They're updated automatically when a remote offer/answer
530 // from the fake signaling channel is applied, or when
531 // ResetRtpReceiverObservers below is called.
532 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
533 rtp_receiver_observers() {
534 return rtp_receiver_observers_;
535 }
536
537 void ResetRtpReceiverObservers() {
538 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100539 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
540 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700541 std::unique_ptr<MockRtpReceiverObserver> observer(
542 new MockRtpReceiverObserver(receiver->media_type()));
543 receiver->SetObserver(observer.get());
544 rtp_receiver_observers_.push_back(std::move(observer));
545 }
546 }
547
Steve Antonede9ca52017-10-16 13:04:27 -0700548 rtc::FakeNetworkManager* network() const {
549 return fake_network_manager_.get();
550 }
551 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
552
deadbeef1dcb1642017-03-29 21:08:16 -0700553 private:
554 explicit PeerConnectionWrapper(const std::string& debug_name)
555 : debug_name_(debug_name) {}
556
557 bool Init(
558 const MediaConstraintsInterface* constraints,
559 const PeerConnectionFactory::Options* options,
560 const PeerConnectionInterface::RTCConfiguration* config,
561 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
562 rtc::Thread* network_thread,
563 rtc::Thread* worker_thread) {
564 // There's an error in this test code if Init ends up being called twice.
565 RTC_DCHECK(!peer_connection_);
566 RTC_DCHECK(!peer_connection_factory_);
567
568 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700569 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700570
571 std::unique_ptr<cricket::PortAllocator> port_allocator(
572 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700573 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700574 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
575 if (!fake_audio_capture_module_) {
576 return false;
577 }
578 // Note that these factories don't end up getting used unless supported
579 // codecs are added to them.
580 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
581 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
582 rtc::Thread* const signaling_thread = rtc::Thread::Current();
583 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
584 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200585 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
586 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700587 fake_video_decoder_factory_);
588 if (!peer_connection_factory_) {
589 return false;
590 }
591 if (options) {
592 peer_connection_factory_->SetOptions(*options);
593 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800594 if (config) {
595 sdp_semantics_ = config->sdp_semantics;
596 }
deadbeef1dcb1642017-03-29 21:08:16 -0700597 peer_connection_ =
598 CreatePeerConnection(std::move(port_allocator), constraints, config,
599 std::move(cert_generator));
600 return peer_connection_.get() != nullptr;
601 }
602
603 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
604 std::unique_ptr<cricket::PortAllocator> port_allocator,
605 const MediaConstraintsInterface* constraints,
606 const PeerConnectionInterface::RTCConfiguration* config,
607 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
608 PeerConnectionInterface::RTCConfiguration modified_config;
609 // If |config| is null, this will result in a default configuration being
610 // used.
611 if (config) {
612 modified_config = *config;
613 }
614 // Disable resolution adaptation; we don't want it interfering with the
615 // test results.
616 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
617 // ratios and not specific resolutions, is this even necessary?
618 modified_config.set_cpu_adaptation(false);
619
620 return peer_connection_factory_->CreatePeerConnection(
621 modified_config, constraints, std::move(port_allocator),
622 std::move(cert_generator), this);
623 }
624
625 void set_signaling_message_receiver(
626 SignalingMessageReceiver* signaling_message_receiver) {
627 signaling_message_receiver_ = signaling_message_receiver;
628 }
629
630 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
631
Steve Antonede9ca52017-10-16 13:04:27 -0700632 void set_signal_ice_candidates(bool signal) {
633 signal_ice_candidates_ = signal;
634 }
635
deadbeef1dcb1642017-03-29 21:08:16 -0700636 void EnableVideoDecoderFactory() {
637 video_decoder_factory_enabled_ = true;
638 fake_video_decoder_factory_->AddSupportedVideoCodecType(
639 webrtc::kVideoCodecVP8);
640 }
641
642 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700643 const FakeConstraints& constraints,
644 webrtc::VideoRotation rotation) {
645 // Set max frame rate to 10fps to reduce the risk of test flakiness.
646 // TODO(deadbeef): Do something more robust.
647 FakeConstraints source_constraints = constraints;
648 source_constraints.SetMandatoryMaxFrameRate(10);
649
650 cricket::FakeVideoCapturer* fake_capturer =
651 new webrtc::FakePeriodicVideoCapturer();
652 fake_capturer->SetRotation(rotation);
653 video_capturers_.push_back(fake_capturer);
654 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
655 peer_connection_factory_->CreateVideoSource(fake_capturer,
656 &source_constraints);
657 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700658 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
659 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700660 if (!local_video_renderer_) {
661 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
662 }
663 return track;
664 }
665
666 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100667 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800668 std::unique_ptr<SessionDescriptionInterface> desc =
669 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700670 if (received_sdp_munger_) {
671 received_sdp_munger_(desc->description());
672 }
673
674 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
675 // Setting a remote description may have changed the number of receivers,
676 // so reset the receiver observers.
677 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800678 if (remote_offer_handler_) {
679 remote_offer_handler_();
680 }
deadbeef1dcb1642017-03-29 21:08:16 -0700681 auto answer = CreateAnswer();
682 ASSERT_NE(nullptr, answer);
683 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
684 }
685
686 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800688 std::unique_ptr<SessionDescriptionInterface> desc =
689 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700690 if (received_sdp_munger_) {
691 received_sdp_munger_(desc->description());
692 }
693
694 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
695 // Set the RtpReceiverObserver after receivers are created.
696 ResetRtpReceiverObservers();
697 }
698
699 // Returns null on failure.
700 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
702 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
703 pc()->CreateOffer(observer, offer_answer_options_);
704 return WaitForDescriptionFromObserver(observer);
705 }
706
707 // Returns null on failure.
708 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
709 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
710 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
711 pc()->CreateAnswer(observer, offer_answer_options_);
712 return WaitForDescriptionFromObserver(observer);
713 }
714
715 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100716 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700717 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
718 if (!observer->result()) {
719 return nullptr;
720 }
721 auto description = observer->MoveDescription();
722 if (generated_sdp_munger_) {
723 generated_sdp_munger_(description->description());
724 }
725 return description;
726 }
727
728 // Setting the local description and sending the SDP message over the fake
729 // signaling channel are combined into the same method because the SDP
730 // message needs to be sent as soon as SetLocalDescription finishes, without
731 // waiting for the observer to be called. This ensures that ICE candidates
732 // don't outrace the description.
733 bool SetLocalDescriptionAndSendSdpMessage(
734 std::unique_ptr<SessionDescriptionInterface> desc) {
735 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
736 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800738 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700739 std::string sdp;
740 EXPECT_TRUE(desc->ToString(&sdp));
741 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800742 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
743 RemoveUnusedVideoRenderers();
744 }
deadbeef1dcb1642017-03-29 21:08:16 -0700745 // As mentioned above, we need to send the message immediately after
746 // SetLocalDescription.
747 SendSdpMessage(type, sdp);
748 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
749 return true;
750 }
751
752 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
753 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
754 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100755 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700756 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800757 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
758 RemoveUnusedVideoRenderers();
759 }
deadbeef1dcb1642017-03-29 21:08:16 -0700760 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
761 return observer->result();
762 }
763
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 // This is a work around to remove unused fake_video_renderers from
765 // transceivers that have either stopped or are no longer receiving.
766 void RemoveUnusedVideoRenderers() {
767 auto transceivers = pc()->GetTransceivers();
768 for (auto& transceiver : transceivers) {
769 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
770 continue;
771 }
772 // Remove fake video renderers from any stopped transceivers.
773 if (transceiver->stopped()) {
774 auto it =
775 fake_video_renderers_.find(transceiver->receiver()->track()->id());
776 if (it != fake_video_renderers_.end()) {
777 fake_video_renderers_.erase(it);
778 }
779 }
780 // Remove fake video renderers from any transceivers that are no longer
781 // receiving.
782 if ((transceiver->current_direction() &&
783 !webrtc::RtpTransceiverDirectionHasRecv(
784 *transceiver->current_direction()))) {
785 auto it =
786 fake_video_renderers_.find(transceiver->receiver()->track()->id());
787 if (it != fake_video_renderers_.end()) {
788 fake_video_renderers_.erase(it);
789 }
790 }
791 }
792 }
793
deadbeef1dcb1642017-03-29 21:08:16 -0700794 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
795 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800796 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700797 if (signaling_delay_ms_ == 0) {
798 RelaySdpMessageIfReceiverExists(type, msg);
799 } else {
800 invoker_.AsyncInvokeDelayed<void>(
801 RTC_FROM_HERE, rtc::Thread::Current(),
802 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
803 this, type, msg),
804 signaling_delay_ms_);
805 }
806 }
807
Steve Antona3a92c22017-12-07 10:27:41 -0800808 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700809 if (signaling_message_receiver_) {
810 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
811 }
812 }
813
814 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
815 // default).
816 void SendIceMessage(const std::string& sdp_mid,
817 int sdp_mline_index,
818 const std::string& msg) {
819 if (signaling_delay_ms_ == 0) {
820 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
821 } else {
822 invoker_.AsyncInvokeDelayed<void>(
823 RTC_FROM_HERE, rtc::Thread::Current(),
824 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
825 this, sdp_mid, sdp_mline_index, msg),
826 signaling_delay_ms_);
827 }
828 }
829
830 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
831 int sdp_mline_index,
832 const std::string& msg) {
833 if (signaling_message_receiver_) {
834 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
835 msg);
836 }
837 }
838
839 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
841 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700842 HandleIncomingOffer(msg);
843 } else {
844 HandleIncomingAnswer(msg);
845 }
846 }
847
848 void ReceiveIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100851 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700852 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
853 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
854 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
855 }
856
857 // PeerConnectionObserver callbacks.
858 void OnSignalingChange(
859 webrtc::PeerConnectionInterface::SignalingState new_state) override {
860 EXPECT_EQ(pc()->signaling_state(), new_state);
861 }
Steve Anton15324772018-01-16 10:26:49 -0800862 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
863 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
864 streams) override {
865 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
866 rtc::scoped_refptr<VideoTrackInterface> video_track(
867 static_cast<VideoTrackInterface*>(receiver->track().get()));
868 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700869 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800870 fake_video_renderers_[video_track->id()] =
871 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700872 }
873 }
Steve Anton15324772018-01-16 10:26:49 -0800874 void OnRemoveTrack(
875 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
876 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
877 auto it = fake_video_renderers_.find(receiver->track()->id());
878 RTC_DCHECK(it != fake_video_renderers_.end());
879 fake_video_renderers_.erase(it);
880 }
881 }
deadbeef1dcb1642017-03-29 21:08:16 -0700882 void OnRenegotiationNeeded() override {}
883 void OnIceConnectionChange(
884 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
885 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700886 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700887 }
888 void OnIceGatheringChange(
889 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700890 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700891 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700892 }
893 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700895
896 std::string ice_sdp;
897 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700898 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700899 // Remote party may be deleted.
900 return;
901 }
902 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
903 }
904 void OnDataChannel(
905 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700907 data_channel_ = data_channel;
908 data_observer_.reset(new MockDataChannelObserver(data_channel));
909 }
910
deadbeef1dcb1642017-03-29 21:08:16 -0700911 std::string debug_name_;
912
913 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
914
915 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
916 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
917 peer_connection_factory_;
918
Steve Antonede9ca52017-10-16 13:04:27 -0700919 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700920 // Needed to keep track of number of frames sent.
921 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
922 // Needed to keep track of number of frames received.
923 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
924 fake_video_renderers_;
925 // Needed to ensure frames aren't received for removed tracks.
926 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
927 removed_fake_video_renderers_;
928 // Needed to keep track of number of frames received when external decoder
929 // used.
930 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
931 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
932 bool video_decoder_factory_enabled_ = false;
933
934 // For remote peer communication.
935 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
936 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700937 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700938
939 // Store references to the video capturers we've created, so that we can stop
940 // them, if required.
941 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
942 // |local_video_renderer_| attached to the first created local video track.
943 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
944
Seth Hampson2f0d7022018-02-20 11:54:42 -0800945 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700946 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
947 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
948 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800949 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700950
951 rtc::scoped_refptr<DataChannelInterface> data_channel_;
952 std::unique_ptr<MockDataChannelObserver> data_observer_;
953
954 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
955
Steve Antonede9ca52017-10-16 13:04:27 -0700956 std::vector<PeerConnectionInterface::IceConnectionState>
957 ice_connection_state_history_;
958 std::vector<PeerConnectionInterface::IceGatheringState>
959 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700960
961 rtc::AsyncInvoker invoker_;
962
Seth Hampson2f0d7022018-02-20 11:54:42 -0800963 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700964};
965
Elad Alon99c3fe52017-10-13 16:29:40 +0200966class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
967 public:
968 virtual ~MockRtcEventLogOutput() = default;
969 MOCK_CONST_METHOD0(IsActive, bool());
970 MOCK_METHOD1(Write, bool(const std::string&));
971};
972
Seth Hampson2f0d7022018-02-20 11:54:42 -0800973// This helper object is used for both specifying how many audio/video frames
974// are expected to be received for a caller/callee. It provides helper functions
975// to specify these expectations. The object initially starts in a state of no
976// expectations.
977class MediaExpectations {
978 public:
979 enum ExpectFrames {
980 kExpectSomeFrames,
981 kExpectNoFrames,
982 kNoExpectation,
983 };
984
985 void ExpectBidirectionalAudioAndVideo() {
986 ExpectBidirectionalAudio();
987 ExpectBidirectionalVideo();
988 }
989
990 void ExpectBidirectionalAudio() {
991 CallerExpectsSomeAudio();
992 CalleeExpectsSomeAudio();
993 }
994
995 void ExpectNoAudio() {
996 CallerExpectsNoAudio();
997 CalleeExpectsNoAudio();
998 }
999
1000 void ExpectBidirectionalVideo() {
1001 CallerExpectsSomeVideo();
1002 CalleeExpectsSomeVideo();
1003 }
1004
1005 void ExpectNoVideo() {
1006 CallerExpectsNoVideo();
1007 CalleeExpectsNoVideo();
1008 }
1009
1010 void CallerExpectsSomeAudioAndVideo() {
1011 CallerExpectsSomeAudio();
1012 CallerExpectsSomeVideo();
1013 }
1014
1015 void CalleeExpectsSomeAudioAndVideo() {
1016 CalleeExpectsSomeAudio();
1017 CalleeExpectsSomeVideo();
1018 }
1019
1020 // Caller's audio functions.
1021 void CallerExpectsSomeAudio(
1022 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1023 caller_audio_expectation_ = kExpectSomeFrames;
1024 caller_audio_frames_expected_ = expected_audio_frames;
1025 }
1026
1027 void CallerExpectsNoAudio() {
1028 caller_audio_expectation_ = kExpectNoFrames;
1029 caller_audio_frames_expected_ = 0;
1030 }
1031
1032 // Caller's video functions.
1033 void CallerExpectsSomeVideo(
1034 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1035 caller_video_expectation_ = kExpectSomeFrames;
1036 caller_video_frames_expected_ = expected_video_frames;
1037 }
1038
1039 void CallerExpectsNoVideo() {
1040 caller_video_expectation_ = kExpectNoFrames;
1041 caller_video_frames_expected_ = 0;
1042 }
1043
1044 // Callee's audio functions.
1045 void CalleeExpectsSomeAudio(
1046 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1047 callee_audio_expectation_ = kExpectSomeFrames;
1048 callee_audio_frames_expected_ = expected_audio_frames;
1049 }
1050
1051 void CalleeExpectsNoAudio() {
1052 callee_audio_expectation_ = kExpectNoFrames;
1053 callee_audio_frames_expected_ = 0;
1054 }
1055
1056 // Callee's video functions.
1057 void CalleeExpectsSomeVideo(
1058 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1059 callee_video_expectation_ = kExpectSomeFrames;
1060 callee_video_frames_expected_ = expected_video_frames;
1061 }
1062
1063 void CalleeExpectsNoVideo() {
1064 callee_video_expectation_ = kExpectNoFrames;
1065 callee_video_frames_expected_ = 0;
1066 }
1067
1068 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1069 ExpectFrames caller_video_expectation_ = kNoExpectation;
1070 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1071 ExpectFrames callee_video_expectation_ = kNoExpectation;
1072 int caller_audio_frames_expected_ = 0;
1073 int caller_video_frames_expected_ = 0;
1074 int callee_audio_frames_expected_ = 0;
1075 int callee_video_frames_expected_ = 0;
1076};
1077
deadbeef1dcb1642017-03-29 21:08:16 -07001078// Tests two PeerConnections connecting to each other end-to-end, using a
1079// virtual network, fake A/V capture and fake encoder/decoders. The
1080// PeerConnections share the threads/socket servers, but use separate versions
1081// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001082class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001083 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001084 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1085 : sdp_semantics_(sdp_semantics),
1086 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001087 fss_(new rtc::FirewallSocketServer(ss_.get())),
1088 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001089 worker_thread_(rtc::Thread::Create()) {
1090 RTC_CHECK(network_thread_->Start());
1091 RTC_CHECK(worker_thread_->Start());
1092 }
1093
Seth Hampson2f0d7022018-02-20 11:54:42 -08001094 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001095 if (caller_) {
1096 caller_->set_signaling_message_receiver(nullptr);
1097 }
1098 if (callee_) {
1099 callee_->set_signaling_message_receiver(nullptr);
1100 }
1101 }
1102
1103 bool SignalingStateStable() {
1104 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1105 }
1106
deadbeef71452802017-05-07 17:21:01 -07001107 bool DtlsConnected() {
1108 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1109 // are connected. This is an important distinction. Once we have separate
1110 // ICE and DTLS state, this check needs to use the DTLS state.
1111 return (callee()->ice_connection_state() ==
1112 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1113 callee()->ice_connection_state() ==
1114 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1115 (caller()->ice_connection_state() ==
1116 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1117 caller()->ice_connection_state() ==
1118 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1119 }
1120
Seth Hampson2f0d7022018-02-20 11:54:42 -08001121 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1122 const std::string& debug_name,
1123 const MediaConstraintsInterface* constraints,
1124 const PeerConnectionFactory::Options* options,
1125 const RTCConfiguration* config,
1126 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1127 RTCConfiguration modified_config;
1128 if (config) {
1129 modified_config = *config;
1130 }
1131 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1132 modified_config.sdp_semantics = sdp_semantics_;
1133 }
1134 if (!cert_generator) {
1135 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1136 }
1137 std::unique_ptr<PeerConnectionWrapper> client(
1138 new PeerConnectionWrapper(debug_name));
1139 if (!client->Init(constraints, options, &modified_config,
1140 std::move(cert_generator), network_thread_.get(),
1141 worker_thread_.get())) {
1142 return nullptr;
1143 }
1144 return client;
1145 }
1146
deadbeef1dcb1642017-03-29 21:08:16 -07001147 bool CreatePeerConnectionWrappers() {
1148 return CreatePeerConnectionWrappersWithConfig(
1149 PeerConnectionInterface::RTCConfiguration(),
1150 PeerConnectionInterface::RTCConfiguration());
1151 }
1152
1153 bool CreatePeerConnectionWrappersWithConstraints(
1154 MediaConstraintsInterface* caller_constraints,
1155 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001156 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1157 nullptr, nullptr);
1158 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1159 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001160 return caller_ && callee_;
1161 }
1162
1163 bool CreatePeerConnectionWrappersWithConfig(
1164 const PeerConnectionInterface::RTCConfiguration& caller_config,
1165 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001166 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1167 &caller_config, nullptr);
1168 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1169 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001170 return caller_ && callee_;
1171 }
1172
1173 bool CreatePeerConnectionWrappersWithOptions(
1174 const PeerConnectionFactory::Options& caller_options,
1175 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001176 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1177 nullptr, nullptr);
1178 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1179 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001180 return caller_ && callee_;
1181 }
1182
Seth Hampson2f0d7022018-02-20 11:54:42 -08001183 std::unique_ptr<PeerConnectionWrapper>
1184 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001185 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1186 new FakeRTCCertificateGenerator());
1187 cert_generator->use_alternate_key();
1188
Seth Hampson2f0d7022018-02-20 11:54:42 -08001189 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1190 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001191 }
1192
1193 // Once called, SDP blobs and ICE candidates will be automatically signaled
1194 // between PeerConnections.
1195 void ConnectFakeSignaling() {
1196 caller_->set_signaling_message_receiver(callee_.get());
1197 callee_->set_signaling_message_receiver(caller_.get());
1198 }
1199
Steve Antonede9ca52017-10-16 13:04:27 -07001200 // Once called, SDP blobs will be automatically signaled between
1201 // PeerConnections. Note that ICE candidates will not be signaled unless they
1202 // are in the exchanged SDP blobs.
1203 void ConnectFakeSignalingForSdpOnly() {
1204 ConnectFakeSignaling();
1205 SetSignalIceCandidates(false);
1206 }
1207
deadbeef1dcb1642017-03-29 21:08:16 -07001208 void SetSignalingDelayMs(int delay_ms) {
1209 caller_->set_signaling_delay_ms(delay_ms);
1210 callee_->set_signaling_delay_ms(delay_ms);
1211 }
1212
Steve Antonede9ca52017-10-16 13:04:27 -07001213 void SetSignalIceCandidates(bool signal) {
1214 caller_->set_signal_ice_candidates(signal);
1215 callee_->set_signal_ice_candidates(signal);
1216 }
1217
deadbeef1dcb1642017-03-29 21:08:16 -07001218 void EnableVideoDecoderFactory() {
1219 caller_->EnableVideoDecoderFactory();
1220 callee_->EnableVideoDecoderFactory();
1221 }
1222
1223 // Messages may get lost on the unreliable DataChannel, so we send multiple
1224 // times to avoid test flakiness.
1225 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1226 const std::string& data,
1227 int retries) {
1228 for (int i = 0; i < retries; ++i) {
1229 dc->Send(DataBuffer(data));
1230 }
1231 }
1232
1233 rtc::Thread* network_thread() { return network_thread_.get(); }
1234
1235 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1236
1237 PeerConnectionWrapper* caller() { return caller_.get(); }
1238
1239 // Set the |caller_| to the |wrapper| passed in and return the
1240 // original |caller_|.
1241 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1242 PeerConnectionWrapper* wrapper) {
1243 PeerConnectionWrapper* old = caller_.release();
1244 caller_.reset(wrapper);
1245 return old;
1246 }
1247
1248 PeerConnectionWrapper* callee() { return callee_.get(); }
1249
1250 // Set the |callee_| to the |wrapper| passed in and return the
1251 // original |callee_|.
1252 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1253 PeerConnectionWrapper* wrapper) {
1254 PeerConnectionWrapper* old = callee_.release();
1255 callee_.reset(wrapper);
1256 return old;
1257 }
1258
Steve Antonede9ca52017-10-16 13:04:27 -07001259 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1260
Seth Hampson2f0d7022018-02-20 11:54:42 -08001261 // Expects the provided number of new frames to be received within
1262 // kMaxWaitForFramesMs. The new expected frames are specified in
1263 // |media_expectations|. Returns false if any of the expectations were
1264 // not met.
1265 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1266 // First initialize the expected frame counts based upon the current
1267 // frame count.
1268 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1269 if (media_expectations.caller_audio_expectation_ ==
1270 MediaExpectations::kExpectSomeFrames) {
1271 total_caller_audio_frames_expected +=
1272 media_expectations.caller_audio_frames_expected_;
1273 }
1274 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001275 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001276 if (media_expectations.caller_video_expectation_ ==
1277 MediaExpectations::kExpectSomeFrames) {
1278 total_caller_video_frames_expected +=
1279 media_expectations.caller_video_frames_expected_;
1280 }
1281 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1282 if (media_expectations.callee_audio_expectation_ ==
1283 MediaExpectations::kExpectSomeFrames) {
1284 total_callee_audio_frames_expected +=
1285 media_expectations.callee_audio_frames_expected_;
1286 }
1287 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001288 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001289 if (media_expectations.callee_video_expectation_ ==
1290 MediaExpectations::kExpectSomeFrames) {
1291 total_callee_video_frames_expected +=
1292 media_expectations.callee_video_frames_expected_;
1293 }
deadbeef1dcb1642017-03-29 21:08:16 -07001294
Seth Hampson2f0d7022018-02-20 11:54:42 -08001295 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001296 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001298 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001299 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001300 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001301 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001302 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001303 total_callee_video_frames_expected,
1304 kMaxWaitForFramesMs);
1305 bool expectations_correct =
1306 caller()->audio_frames_received() >=
1307 total_caller_audio_frames_expected &&
1308 caller()->min_video_frames_received_per_track() >=
1309 total_caller_video_frames_expected &&
1310 callee()->audio_frames_received() >=
1311 total_callee_audio_frames_expected &&
1312 callee()->min_video_frames_received_per_track() >=
1313 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001314
Seth Hampson2f0d7022018-02-20 11:54:42 -08001315 // After the combined wait, print out a more detailed message upon
1316 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001317 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001318 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001319 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001320 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001321 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001323 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 total_callee_video_frames_expected);
1325
1326 // We want to make sure nothing unexpected was received.
1327 if (media_expectations.caller_audio_expectation_ ==
1328 MediaExpectations::kExpectNoFrames) {
1329 EXPECT_EQ(caller()->audio_frames_received(),
1330 total_caller_audio_frames_expected);
1331 if (caller()->audio_frames_received() !=
1332 total_caller_audio_frames_expected) {
1333 expectations_correct = false;
1334 }
1335 }
1336 if (media_expectations.caller_video_expectation_ ==
1337 MediaExpectations::kExpectNoFrames) {
1338 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1339 total_caller_video_frames_expected);
1340 if (caller()->min_video_frames_received_per_track() !=
1341 total_caller_video_frames_expected) {
1342 expectations_correct = false;
1343 }
1344 }
1345 if (media_expectations.callee_audio_expectation_ ==
1346 MediaExpectations::kExpectNoFrames) {
1347 EXPECT_EQ(callee()->audio_frames_received(),
1348 total_callee_audio_frames_expected);
1349 if (callee()->audio_frames_received() !=
1350 total_callee_audio_frames_expected) {
1351 expectations_correct = false;
1352 }
1353 }
1354 if (media_expectations.callee_video_expectation_ ==
1355 MediaExpectations::kExpectNoFrames) {
1356 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1357 total_callee_video_frames_expected);
1358 if (callee()->min_video_frames_received_per_track() !=
1359 total_callee_video_frames_expected) {
1360 expectations_correct = false;
1361 }
1362 }
1363 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001364 }
1365
Tommi8e545ee2018-02-08 16:25:20 +00001366 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1367 bool remote_gcm_enabled,
1368 int expected_cipher_suite) {
1369 PeerConnectionFactory::Options caller_options;
1370 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1371 PeerConnectionFactory::Options callee_options;
1372 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
deadbeef1dcb1642017-03-29 21:08:16 -07001373 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1374 callee_options));
1375 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1376 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1377 caller()->pc()->RegisterUMAObserver(caller_observer);
1378 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001379 caller()->AddAudioVideoTracks();
1380 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001381 caller()->CreateAndSetAndSignalOffer();
1382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1383 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001384 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001385 EXPECT_EQ(
1386 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1387 expected_cipher_suite));
1388 caller()->pc()->RegisterUMAObserver(nullptr);
1389 }
1390
Seth Hampson2f0d7022018-02-20 11:54:42 -08001391 protected:
1392 const SdpSemantics sdp_semantics_;
1393
deadbeef1dcb1642017-03-29 21:08:16 -07001394 private:
1395 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001396 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001397 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001398 // |network_thread_| and |worker_thread_| are used by both
1399 // |caller_| and |callee_| so they must be destroyed
1400 // later.
1401 std::unique_ptr<rtc::Thread> network_thread_;
1402 std::unique_ptr<rtc::Thread> worker_thread_;
1403 std::unique_ptr<PeerConnectionWrapper> caller_;
1404 std::unique_ptr<PeerConnectionWrapper> callee_;
1405};
1406
Seth Hampson2f0d7022018-02-20 11:54:42 -08001407class PeerConnectionIntegrationTest
1408 : public PeerConnectionIntegrationBaseTest,
1409 public ::testing::WithParamInterface<SdpSemantics> {
1410 protected:
1411 PeerConnectionIntegrationTest()
1412 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1413};
1414
1415class PeerConnectionIntegrationTestPlanB
1416 : public PeerConnectionIntegrationBaseTest {
1417 protected:
1418 PeerConnectionIntegrationTestPlanB()
1419 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1420};
1421
1422class PeerConnectionIntegrationTestUnifiedPlan
1423 : public PeerConnectionIntegrationBaseTest {
1424 protected:
1425 PeerConnectionIntegrationTestUnifiedPlan()
1426 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1427};
1428
deadbeef1dcb1642017-03-29 21:08:16 -07001429// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1430// includes testing that the callback is invoked if an observer is connected
1431// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001432TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001433 RtpReceiverObserverOnFirstPacketReceived) {
1434 ASSERT_TRUE(CreatePeerConnectionWrappers());
1435 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001436 caller()->AddAudioVideoTracks();
1437 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001438 // Start offer/answer exchange and wait for it to complete.
1439 caller()->CreateAndSetAndSignalOffer();
1440 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1441 // Should be one receiver each for audio/video.
1442 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1443 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1444 // Wait for all "first packet received" callbacks to be fired.
1445 EXPECT_TRUE_WAIT(
1446 std::all_of(caller()->rtp_receiver_observers().begin(),
1447 caller()->rtp_receiver_observers().end(),
1448 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1449 return o->first_packet_received();
1450 }),
1451 kMaxWaitForFramesMs);
1452 EXPECT_TRUE_WAIT(
1453 std::all_of(callee()->rtp_receiver_observers().begin(),
1454 callee()->rtp_receiver_observers().end(),
1455 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1456 return o->first_packet_received();
1457 }),
1458 kMaxWaitForFramesMs);
1459 // If new observers are set after the first packet was already received, the
1460 // callback should still be invoked.
1461 caller()->ResetRtpReceiverObservers();
1462 callee()->ResetRtpReceiverObservers();
1463 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1464 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1465 EXPECT_TRUE(
1466 std::all_of(caller()->rtp_receiver_observers().begin(),
1467 caller()->rtp_receiver_observers().end(),
1468 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1469 return o->first_packet_received();
1470 }));
1471 EXPECT_TRUE(
1472 std::all_of(callee()->rtp_receiver_observers().begin(),
1473 callee()->rtp_receiver_observers().end(),
1474 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1475 return o->first_packet_received();
1476 }));
1477}
1478
1479class DummyDtmfObserver : public DtmfSenderObserverInterface {
1480 public:
1481 DummyDtmfObserver() : completed_(false) {}
1482
1483 // Implements DtmfSenderObserverInterface.
1484 void OnToneChange(const std::string& tone) override {
1485 tones_.push_back(tone);
1486 if (tone.empty()) {
1487 completed_ = true;
1488 }
1489 }
1490
1491 const std::vector<std::string>& tones() const { return tones_; }
1492 bool completed() const { return completed_; }
1493
1494 private:
1495 bool completed_;
1496 std::vector<std::string> tones_;
1497};
1498
1499// Assumes |sender| already has an audio track added and the offer/answer
1500// exchange is done.
1501void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1502 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001503 // We should be able to get a DTMF sender from the local sender.
1504 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1505 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1506 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001507 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001508 dtmf_sender->RegisterObserver(&observer);
1509
1510 // Test the DtmfSender object just created.
1511 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1512 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1513
1514 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1515 std::vector<std::string> tones = {"1", "a", ""};
1516 EXPECT_EQ(tones, observer.tones());
1517 dtmf_sender->UnregisterObserver();
1518 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1519}
1520
1521// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1522// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001523TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001524 ASSERT_TRUE(CreatePeerConnectionWrappers());
1525 ConnectFakeSignaling();
1526 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001527 caller()->AddAudioTrack();
1528 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001529 caller()->CreateAndSetAndSignalOffer();
1530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001531 // DTLS must finish before the DTMF sender can be used reliably.
1532 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001533 TestDtmfFromSenderToReceiver(caller(), callee());
1534 TestDtmfFromSenderToReceiver(callee(), caller());
1535}
1536
1537// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1538// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001539TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001540 ASSERT_TRUE(CreatePeerConnectionWrappers());
1541 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001542 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1543 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1544 caller()->pc()->RegisterUMAObserver(caller_observer);
1545
deadbeef1dcb1642017-03-29 21:08:16 -07001546 // Do normal offer/answer and wait for some frames to be received in each
1547 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001548 caller()->AddAudioVideoTracks();
1549 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001550 caller()->CreateAndSetAndSignalOffer();
1551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001552 MediaExpectations media_expectations;
1553 media_expectations.ExpectBidirectionalAudioAndVideo();
1554 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001555 EXPECT_LE(
1556 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1557 webrtc::kEnumCounterKeyProtocolDtls));
1558 EXPECT_EQ(
1559 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1560 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001561}
1562
1563// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001564TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001565 PeerConnectionInterface::RTCConfiguration sdes_config;
1566 sdes_config.enable_dtls_srtp.emplace(false);
1567 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1568 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001569 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1570 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1571 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001572
1573 // Do normal offer/answer and wait for some frames to be received in each
1574 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001575 caller()->AddAudioVideoTracks();
1576 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001577 caller()->CreateAndSetAndSignalOffer();
1578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001579 MediaExpectations media_expectations;
1580 media_expectations.ExpectBidirectionalAudioAndVideo();
1581 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001582 EXPECT_LE(
1583 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1584 webrtc::kEnumCounterKeyProtocolSdes));
1585 EXPECT_EQ(
1586 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1587 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001588}
1589
Steve Anton8c0f7a72017-10-03 10:03:10 -07001590// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1591// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001592TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001593 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1594 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1595 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1596 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1597 return pc->GetRemoteAudioSSLCertificate();
1598 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001599 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1600 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1601 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1602 return pc->GetRemoteAudioSSLCertChain();
1603 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001604
1605 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1606 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1607
1608 // Configure each side with a known certificate so they can be compared later.
1609 PeerConnectionInterface::RTCConfiguration caller_config;
1610 caller_config.enable_dtls_srtp.emplace(true);
1611 caller_config.certificates.push_back(caller_cert);
1612 PeerConnectionInterface::RTCConfiguration callee_config;
1613 callee_config.enable_dtls_srtp.emplace(true);
1614 callee_config.certificates.push_back(callee_cert);
1615 ASSERT_TRUE(
1616 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1617 ConnectFakeSignaling();
1618
1619 // When first initialized, there should not be a remote SSL certificate (and
1620 // calling this method should not crash).
1621 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1622 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001623 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1624 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001625
Steve Anton15324772018-01-16 10:26:49 -08001626 caller()->AddAudioTrack();
1627 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001628 caller()->CreateAndSetAndSignalOffer();
1629 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1630 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1631
1632 // Once DTLS has been connected, each side should return the other's SSL
1633 // certificate when calling GetRemoteAudioSSLCertificate.
1634
1635 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1636 ASSERT_TRUE(caller_remote_cert);
1637 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1638 caller_remote_cert->ToPEMString());
1639
1640 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1641 ASSERT_TRUE(callee_remote_cert);
1642 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1643 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001644
1645 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1646 ASSERT_TRUE(caller_remote_cert_chain);
1647 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1648 auto remote_cert = &caller_remote_cert_chain->Get(0);
1649 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1650 remote_cert->ToPEMString());
1651
1652 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1653 ASSERT_TRUE(callee_remote_cert_chain);
1654 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1655 remote_cert = &callee_remote_cert_chain->Get(0);
1656 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1657 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001658}
1659
deadbeef1dcb1642017-03-29 21:08:16 -07001660// This test sets up a call between two parties (using DTLS) and tests that we
1661// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001662TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001663 ASSERT_TRUE(CreatePeerConnectionWrappers());
1664 ConnectFakeSignaling();
1665
1666 // Add video tracks with 16:9 constraint.
1667 FakeConstraints constraints;
1668 double requested_ratio = 16.0 / 9;
1669 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001670 caller()->AddTrack(
1671 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1672 callee()->AddTrack(
1673 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001674
1675 // Do normal offer/answer and wait for at least one frame to be received in
1676 // each direction.
1677 caller()->CreateAndSetAndSignalOffer();
1678 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1679 callee()->min_video_frames_received_per_track() > 0,
1680 kMaxWaitForFramesMs);
1681
1682 // Check rendered aspect ratio.
1683 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1684 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1685 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1686 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1687}
1688
1689// This test sets up a call between two parties with a source resolution of
1690// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001691TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001692 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1693 ASSERT_TRUE(CreatePeerConnectionWrappers());
1694 ConnectFakeSignaling();
1695
1696 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1697 // instead of aspect ratio constraint.
1698 FakeConstraints constraints;
1699 constraints.SetMandatoryMinWidth(1280);
1700 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001701 caller()->AddTrack(
1702 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1703 callee()->AddTrack(
1704 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001705
1706 // Do normal offer/answer and wait for at least one frame to be received in
1707 // each direction.
1708 caller()->CreateAndSetAndSignalOffer();
1709 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1710 callee()->min_video_frames_received_per_track() > 0,
1711 kMaxWaitForFramesMs);
1712
1713 // Check rendered aspect ratio.
1714 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1715 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1716 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1717 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1718}
1719
1720// This test sets up an one-way call, with media only from caller to
1721// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001722TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001723 ASSERT_TRUE(CreatePeerConnectionWrappers());
1724 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001725 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001726 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001727 MediaExpectations media_expectations;
1728 media_expectations.CalleeExpectsSomeAudioAndVideo();
1729 media_expectations.CallerExpectsNoAudio();
1730 media_expectations.CallerExpectsNoVideo();
1731 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001732}
1733
1734// This test sets up a audio call initially, with the callee rejecting video
1735// initially. Then later the callee decides to upgrade to audio/video, and
1736// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001737TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001738 ASSERT_TRUE(CreatePeerConnectionWrappers());
1739 ConnectFakeSignaling();
1740 // Initially, offer an audio/video stream from the caller, but refuse to
1741 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001742 caller()->AddAudioVideoTracks();
1743 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001744 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1745 PeerConnectionInterface::RTCOfferAnswerOptions options;
1746 options.offer_to_receive_video = 0;
1747 callee()->SetOfferAnswerOptions(options);
1748 } else {
1749 callee()->SetRemoteOfferHandler([this] {
1750 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1751 });
1752 }
deadbeef1dcb1642017-03-29 21:08:16 -07001753 // Do offer/answer and make sure audio is still received end-to-end.
1754 caller()->CreateAndSetAndSignalOffer();
1755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001756 {
1757 MediaExpectations media_expectations;
1758 media_expectations.ExpectBidirectionalAudio();
1759 media_expectations.ExpectNoVideo();
1760 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1761 }
deadbeef1dcb1642017-03-29 21:08:16 -07001762 // Sanity check that the callee's description has a rejected video section.
1763 ASSERT_NE(nullptr, callee()->pc()->local_description());
1764 const ContentInfo* callee_video_content =
1765 GetFirstVideoContent(callee()->pc()->local_description()->description());
1766 ASSERT_NE(nullptr, callee_video_content);
1767 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001768
deadbeef1dcb1642017-03-29 21:08:16 -07001769 // Now negotiate with video and ensure negotiation succeeds, with video
1770 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001771 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001772 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1773 PeerConnectionInterface::RTCOfferAnswerOptions options;
1774 options.offer_to_receive_video = 1;
1775 callee()->SetOfferAnswerOptions(options);
1776 } else {
1777 callee()->SetRemoteOfferHandler(nullptr);
1778 caller()->SetRemoteOfferHandler([this] {
1779 // The caller creates a new transceiver to receive video on when receiving
1780 // the offer, but by default it is send only.
1781 auto transceivers = caller()->pc()->GetTransceivers();
1782 ASSERT_EQ(3, transceivers.size());
1783 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1784 transceivers[2]->receiver()->media_type());
1785 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1786 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1787 });
1788 }
deadbeef1dcb1642017-03-29 21:08:16 -07001789 callee()->CreateAndSetAndSignalOffer();
1790 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001791 {
1792 // Expect additional audio frames to be received after the upgrade.
1793 MediaExpectations media_expectations;
1794 media_expectations.ExpectBidirectionalAudioAndVideo();
1795 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1796 }
deadbeef1dcb1642017-03-29 21:08:16 -07001797}
1798
deadbeef4389b4d2017-09-07 09:07:36 -07001799// Simpler than the above test; just add an audio track to an established
1800// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001801TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001802 ASSERT_TRUE(CreatePeerConnectionWrappers());
1803 ConnectFakeSignaling();
1804 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001805 caller()->AddVideoTrack();
1806 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001807 caller()->CreateAndSetAndSignalOffer();
1808 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1809 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001810 caller()->AddAudioTrack();
1811 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001812 caller()->CreateAndSetAndSignalOffer();
1813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1814 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001815 MediaExpectations media_expectations;
1816 media_expectations.ExpectBidirectionalAudioAndVideo();
1817 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001818}
1819
deadbeef1dcb1642017-03-29 21:08:16 -07001820// This test sets up a call that's transferred to a new caller with a different
1821// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001822TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001823 ASSERT_TRUE(CreatePeerConnectionWrappers());
1824 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001825 caller()->AddAudioVideoTracks();
1826 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001827 caller()->CreateAndSetAndSignalOffer();
1828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1829
1830 // Keep the original peer around which will still send packets to the
1831 // receiving client. These SRTP packets will be dropped.
1832 std::unique_ptr<PeerConnectionWrapper> original_peer(
1833 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001834 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001835 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1836 // directly above.
1837 original_peer->pc()->Close();
1838
1839 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001840 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001841 caller()->CreateAndSetAndSignalOffer();
1842 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1843 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001844 MediaExpectations media_expectations;
1845 media_expectations.ExpectBidirectionalAudioAndVideo();
1846 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001847}
1848
1849// This test sets up a call that's transferred to a new callee with a different
1850// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001851TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001852 ASSERT_TRUE(CreatePeerConnectionWrappers());
1853 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001854 caller()->AddAudioVideoTracks();
1855 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001856 caller()->CreateAndSetAndSignalOffer();
1857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1858
1859 // Keep the original peer around which will still send packets to the
1860 // receiving client. These SRTP packets will be dropped.
1861 std::unique_ptr<PeerConnectionWrapper> original_peer(
1862 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001863 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001864 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1865 // directly above.
1866 original_peer->pc()->Close();
1867
1868 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001869 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001870 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1871 caller()->CreateAndSetAndSignalOffer();
1872 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1873 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001874 MediaExpectations media_expectations;
1875 media_expectations.ExpectBidirectionalAudioAndVideo();
1876 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001877}
1878
1879// This test sets up a non-bundled call and negotiates bundling at the same
1880// time as starting an ICE restart. When bundling is in effect in the restart,
1881// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001882TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001883 ASSERT_TRUE(CreatePeerConnectionWrappers());
1884 ConnectFakeSignaling();
1885
Steve Anton15324772018-01-16 10:26:49 -08001886 caller()->AddAudioVideoTracks();
1887 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001888 // Remove the bundle group from the SDP received by the callee.
1889 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1890 desc->RemoveGroupByName("BUNDLE");
1891 });
1892 caller()->CreateAndSetAndSignalOffer();
1893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001894 {
1895 MediaExpectations media_expectations;
1896 media_expectations.ExpectBidirectionalAudioAndVideo();
1897 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1898 }
deadbeef1dcb1642017-03-29 21:08:16 -07001899 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1900 callee()->SetReceivedSdpMunger(nullptr);
1901 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1902 caller()->CreateAndSetAndSignalOffer();
1903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1904
1905 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001906 {
1907 MediaExpectations media_expectations;
1908 media_expectations.ExpectBidirectionalAudioAndVideo();
1909 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1910 }
deadbeef1dcb1642017-03-29 21:08:16 -07001911}
1912
1913// Test CVO (Coordination of Video Orientation). If a video source is rotated
1914// and both peers support the CVO RTP header extension, the actual video frames
1915// don't need to be encoded in different resolutions, since the rotation is
1916// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001917TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001918 ASSERT_TRUE(CreatePeerConnectionWrappers());
1919 ConnectFakeSignaling();
1920 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001921 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001922 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001923 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001924 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1925
1926 // Wait for video frames to be received by both sides.
1927 caller()->CreateAndSetAndSignalOffer();
1928 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1929 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1930 callee()->min_video_frames_received_per_track() > 0,
1931 kMaxWaitForFramesMs);
1932
1933 // Ensure that the aspect ratio is unmodified.
1934 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1935 // not just assumed.
1936 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1937 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1938 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1939 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1940 // Ensure that the CVO bits were surfaced to the renderer.
1941 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1942 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1943}
1944
1945// Test that when the CVO extension isn't supported, video is rotated the
1946// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001947TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001948 ASSERT_TRUE(CreatePeerConnectionWrappers());
1949 ConnectFakeSignaling();
1950 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001951 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001952 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001953 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001954 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1955
1956 // Remove the CVO extension from the offered SDP.
1957 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1958 cricket::VideoContentDescription* video =
1959 GetFirstVideoContentDescription(desc);
1960 video->ClearRtpHeaderExtensions();
1961 });
1962 // Wait for video frames to be received by both sides.
1963 caller()->CreateAndSetAndSignalOffer();
1964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1965 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1966 callee()->min_video_frames_received_per_track() > 0,
1967 kMaxWaitForFramesMs);
1968
1969 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1970 // rotation.
1971 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1972 // not just assumed.
1973 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1974 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1975 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1976 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1977 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1978 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1979 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1980}
1981
deadbeef1dcb1642017-03-29 21:08:16 -07001982// Test that if the answerer rejects the audio m= section, no audio is sent or
1983// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001984TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001985 ASSERT_TRUE(CreatePeerConnectionWrappers());
1986 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001987 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1989 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1990 // it will reject the audio m= section completely.
1991 PeerConnectionInterface::RTCOfferAnswerOptions options;
1992 options.offer_to_receive_audio = 0;
1993 callee()->SetOfferAnswerOptions(options);
1994 } else {
1995 // Stopping the audio RtpTransceiver will cause the media section to be
1996 // rejected in the answer.
1997 callee()->SetRemoteOfferHandler([this] {
1998 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
1999 });
2000 }
Steve Anton15324772018-01-16 10:26:49 -08002001 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002002 // Do offer/answer and wait for successful end-to-end video frames.
2003 caller()->CreateAndSetAndSignalOffer();
2004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002005 MediaExpectations media_expectations;
2006 media_expectations.ExpectBidirectionalVideo();
2007 media_expectations.ExpectNoAudio();
2008 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2009
deadbeef1dcb1642017-03-29 21:08:16 -07002010 // Sanity check that the callee's description has a rejected audio section.
2011 ASSERT_NE(nullptr, callee()->pc()->local_description());
2012 const ContentInfo* callee_audio_content =
2013 GetFirstAudioContent(callee()->pc()->local_description()->description());
2014 ASSERT_NE(nullptr, callee_audio_content);
2015 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002016 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2017 // The caller's transceiver should have stopped after receiving the answer.
2018 EXPECT_TRUE(caller()
2019 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2020 ->stopped());
2021 }
deadbeef1dcb1642017-03-29 21:08:16 -07002022}
2023
2024// Test that if the answerer rejects the video m= section, no video is sent or
2025// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002026TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002027 ASSERT_TRUE(CreatePeerConnectionWrappers());
2028 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002029 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002030 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2031 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2032 // it will reject the video m= section completely.
2033 PeerConnectionInterface::RTCOfferAnswerOptions options;
2034 options.offer_to_receive_video = 0;
2035 callee()->SetOfferAnswerOptions(options);
2036 } else {
2037 // Stopping the video RtpTransceiver will cause the media section to be
2038 // rejected in the answer.
2039 callee()->SetRemoteOfferHandler([this] {
2040 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2041 });
2042 }
Steve Anton15324772018-01-16 10:26:49 -08002043 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002044 // Do offer/answer and wait for successful end-to-end audio frames.
2045 caller()->CreateAndSetAndSignalOffer();
2046 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002047 MediaExpectations media_expectations;
2048 media_expectations.ExpectBidirectionalAudio();
2049 media_expectations.ExpectNoVideo();
2050 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2051
deadbeef1dcb1642017-03-29 21:08:16 -07002052 // Sanity check that the callee's description has a rejected video section.
2053 ASSERT_NE(nullptr, callee()->pc()->local_description());
2054 const ContentInfo* callee_video_content =
2055 GetFirstVideoContent(callee()->pc()->local_description()->description());
2056 ASSERT_NE(nullptr, callee_video_content);
2057 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002058 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2059 // The caller's transceiver should have stopped after receiving the answer.
2060 EXPECT_TRUE(caller()
2061 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2062 ->stopped());
2063 }
deadbeef1dcb1642017-03-29 21:08:16 -07002064}
2065
2066// Test that if the answerer rejects both audio and video m= sections, nothing
2067// bad happens.
2068// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2069// test anything but the fact that negotiation succeeds, which doesn't mean
2070// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002071TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002072 ASSERT_TRUE(CreatePeerConnectionWrappers());
2073 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002074 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002075 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2076 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2077 // will reject both audio and video m= sections.
2078 PeerConnectionInterface::RTCOfferAnswerOptions options;
2079 options.offer_to_receive_audio = 0;
2080 options.offer_to_receive_video = 0;
2081 callee()->SetOfferAnswerOptions(options);
2082 } else {
2083 callee()->SetRemoteOfferHandler([this] {
2084 // Stopping all transceivers will cause all media sections to be rejected.
2085 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2086 transceiver->Stop();
2087 }
2088 });
2089 }
deadbeef1dcb1642017-03-29 21:08:16 -07002090 // Do offer/answer and wait for stable signaling state.
2091 caller()->CreateAndSetAndSignalOffer();
2092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002093
deadbeef1dcb1642017-03-29 21:08:16 -07002094 // Sanity check that the callee's description has rejected m= sections.
2095 ASSERT_NE(nullptr, callee()->pc()->local_description());
2096 const ContentInfo* callee_audio_content =
2097 GetFirstAudioContent(callee()->pc()->local_description()->description());
2098 ASSERT_NE(nullptr, callee_audio_content);
2099 EXPECT_TRUE(callee_audio_content->rejected);
2100 const ContentInfo* callee_video_content =
2101 GetFirstVideoContent(callee()->pc()->local_description()->description());
2102 ASSERT_NE(nullptr, callee_video_content);
2103 EXPECT_TRUE(callee_video_content->rejected);
2104}
2105
2106// This test sets up an audio and video call between two parties. After the
2107// call runs for a while, the caller sends an updated offer with video being
2108// rejected. Once the re-negotiation is done, the video flow should stop and
2109// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002110TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002111 ASSERT_TRUE(CreatePeerConnectionWrappers());
2112 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002113 caller()->AddAudioVideoTracks();
2114 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002115 caller()->CreateAndSetAndSignalOffer();
2116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002117 {
2118 MediaExpectations media_expectations;
2119 media_expectations.ExpectBidirectionalAudioAndVideo();
2120 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2121 }
deadbeef1dcb1642017-03-29 21:08:16 -07002122 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002123 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2124 caller()->SetGeneratedSdpMunger(
2125 [](cricket::SessionDescription* description) {
2126 for (cricket::ContentInfo& content : description->contents()) {
2127 if (cricket::IsVideoContent(&content)) {
2128 content.rejected = true;
2129 }
2130 }
2131 });
2132 } else {
2133 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2134 }
deadbeef1dcb1642017-03-29 21:08:16 -07002135 caller()->CreateAndSetAndSignalOffer();
2136 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2137
2138 // Sanity check that the caller's description has a rejected video section.
2139 ASSERT_NE(nullptr, caller()->pc()->local_description());
2140 const ContentInfo* caller_video_content =
2141 GetFirstVideoContent(caller()->pc()->local_description()->description());
2142 ASSERT_NE(nullptr, caller_video_content);
2143 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002144 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002145 {
2146 MediaExpectations media_expectations;
2147 media_expectations.ExpectBidirectionalAudio();
2148 media_expectations.ExpectNoVideo();
2149 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2150 }
deadbeef1dcb1642017-03-29 21:08:16 -07002151}
2152
2153// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2154// is needed to support legacy endpoints.
2155// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2156// add a test for an end-to-end test without MID signaling either (basically,
2157// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002158TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002159 ASSERT_TRUE(CreatePeerConnectionWrappers());
2160 ConnectFakeSignaling();
2161 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002162 caller()->AddAudioVideoTracks();
2163 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002164 // Remove SSRCs and MSIDs from the received offer SDP.
2165 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002166 caller()->CreateAndSetAndSignalOffer();
2167 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002168 MediaExpectations media_expectations;
2169 media_expectations.ExpectBidirectionalAudioAndVideo();
2170 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002171}
2172
2173// Test that if two video tracks are sent (from caller to callee, in this test),
2174// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002175TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002176 ASSERT_TRUE(CreatePeerConnectionWrappers());
2177 ConnectFakeSignaling();
2178 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002179 caller()->AddAudioVideoTracks();
2180 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002181 caller()->CreateAndSetAndSignalOffer();
2182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002183 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002184
2185 MediaExpectations media_expectations;
2186 media_expectations.CalleeExpectsSomeAudioAndVideo();
2187 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002188}
2189
2190static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2191 bool first = true;
2192 for (cricket::ContentInfo& content : desc->contents()) {
2193 if (first) {
2194 first = false;
2195 continue;
2196 }
2197 content.bundle_only = true;
2198 }
2199 first = true;
2200 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2201 if (first) {
2202 first = false;
2203 continue;
2204 }
2205 transport.description.ice_ufrag.clear();
2206 transport.description.ice_pwd.clear();
2207 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2208 transport.description.identity_fingerprint.reset(nullptr);
2209 }
2210}
2211
2212// Test that if applying a true "max bundle" offer, which uses ports of 0,
2213// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2214// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2215// successfully and media flows.
2216// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2217// TODO(deadbeef): Won't need this test once we start generating actual
2218// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002219TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002220 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2221 ASSERT_TRUE(CreatePeerConnectionWrappers());
2222 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002223 caller()->AddAudioVideoTracks();
2224 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002225 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2226 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2227 // but the first m= section.
2228 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2229 caller()->CreateAndSetAndSignalOffer();
2230 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002231 MediaExpectations media_expectations;
2232 media_expectations.ExpectBidirectionalAudioAndVideo();
2233 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002234}
2235
2236// Test that we can receive the audio output level from a remote audio track.
2237// TODO(deadbeef): Use a fake audio source and verify that the output level is
2238// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002239TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002240 ASSERT_TRUE(CreatePeerConnectionWrappers());
2241 ConnectFakeSignaling();
2242 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002243 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002244 caller()->CreateAndSetAndSignalOffer();
2245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2246
2247 // Get the audio output level stats. Note that the level is not available
2248 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002249 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002250 kMaxWaitForFramesMs);
2251}
2252
2253// Test that an audio input level is reported.
2254// TODO(deadbeef): Use a fake audio source and verify that the input level is
2255// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002256TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002257 ASSERT_TRUE(CreatePeerConnectionWrappers());
2258 ConnectFakeSignaling();
2259 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002260 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002261 caller()->CreateAndSetAndSignalOffer();
2262 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2263
2264 // Get the audio input level stats. The level should be available very
2265 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002266 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002267 kMaxWaitForStatsMs);
2268}
2269
2270// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002271TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002272 ASSERT_TRUE(CreatePeerConnectionWrappers());
2273 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002274 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002275 // Do offer/answer, wait for the callee to receive some frames.
2276 caller()->CreateAndSetAndSignalOffer();
2277 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002278
2279 MediaExpectations media_expectations;
2280 media_expectations.CalleeExpectsSomeAudioAndVideo();
2281 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002282
2283 // Get a handle to the remote tracks created, so they can be used as GetStats
2284 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002285 for (auto receiver : callee()->pc()->GetReceivers()) {
2286 // We received frames, so we definitely should have nonzero "received bytes"
2287 // stats at this point.
2288 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2289 0);
2290 }
deadbeef1dcb1642017-03-29 21:08:16 -07002291}
2292
2293// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002294TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002295 ASSERT_TRUE(CreatePeerConnectionWrappers());
2296 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002297 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002298 auto audio_track = caller()->CreateLocalAudioTrack();
2299 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002300 caller()->AddTrack(audio_track);
2301 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002302 // Do offer/answer, wait for the callee to receive some frames.
2303 caller()->CreateAndSetAndSignalOffer();
2304 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002305 MediaExpectations media_expectations;
2306 media_expectations.CalleeExpectsSomeAudioAndVideo();
2307 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002308
2309 // The callee received frames, so we definitely should have nonzero "sent
2310 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002311 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2312 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2313}
2314
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002315// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002316TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002317 ASSERT_TRUE(CreatePeerConnectionWrappers());
2318 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002319 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002320
Steve Anton15324772018-01-16 10:26:49 -08002321 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002322
2323 // Do offer/answer, wait for the callee to receive some frames.
2324 caller()->CreateAndSetAndSignalOffer();
2325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2326
2327 // Get the remote audio track created on the receiver, so they can be used as
2328 // GetStats filters.
2329 StreamCollectionInterface* remote_streams = callee()->remote_streams();
2330 ASSERT_EQ(1u, remote_streams->count());
2331 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
2332 MediaStreamTrackInterface* remote_audio_track =
2333 remote_streams->at(0)->GetAudioTracks()[0];
2334
2335 // Get the audio output level stats. Note that the level is not available
2336 // until an RTCP packet has been received.
2337 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
2338 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
2339}
2340
deadbeefd8ad7882017-04-18 16:01:17 -07002341// Test that we can get stats (using the new stats implemnetation) for
2342// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2343// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002344TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002345 GetStatsForUnsignaledStreamWithNewStatsApi) {
2346 ASSERT_TRUE(CreatePeerConnectionWrappers());
2347 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002348 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002349 // Remove SSRCs and MSIDs from the received offer SDP.
2350 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2351 caller()->CreateAndSetAndSignalOffer();
2352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002353 MediaExpectations media_expectations;
2354 media_expectations.CalleeExpectsSomeAudio(1);
2355 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002356
2357 // We received a frame, so we should have nonzero "bytes received" stats for
2358 // the unsignaled stream, if stats are working for it.
2359 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2360 callee()->NewGetStats();
2361 ASSERT_NE(nullptr, report);
2362 auto inbound_stream_stats =
2363 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2364 ASSERT_EQ(1U, inbound_stream_stats.size());
2365 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2366 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002367 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2368}
2369
2370// Test that we can successfully get the media related stats (audio level
2371// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002373 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2374 ASSERT_TRUE(CreatePeerConnectionWrappers());
2375 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002376 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002377 // Remove SSRCs and MSIDs from the received offer SDP.
2378 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2379 caller()->CreateAndSetAndSignalOffer();
2380 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002381 MediaExpectations media_expectations;
2382 media_expectations.CalleeExpectsSomeAudio(1);
2383 media_expectations.CalleeExpectsSomeVideo(1);
2384 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002385
2386 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2387 callee()->NewGetStats();
2388 ASSERT_NE(nullptr, report);
2389
2390 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2391 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2392 ASSERT_GE(audio_index, 0);
2393 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002394}
2395
deadbeef4e2deab2017-09-20 13:56:21 -07002396// Helper for test below.
2397void ModifySsrcs(cricket::SessionDescription* desc) {
2398 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002399 for (cricket::StreamParams& stream :
2400 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002401 for (uint32_t& ssrc : stream.ssrcs) {
2402 ssrc = rtc::CreateRandomId();
2403 }
2404 }
2405 }
2406}
2407
2408// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2409// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2410// This should result in two "RTCInboundRTPStreamStats", but only one
2411// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2412// being reset to 0 once the SSRC change occurs.
2413//
2414// Regression test for this bug:
2415// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2416//
2417// The bug causes the track stats to only represent one of the two streams:
2418// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2419// that the track stat counters would reset to 0 when the new stream is
2420// received, and a 50% chance that they'll stop updating (while
2421// "concealed_samples" continues increasing, due to silence being generated for
2422// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002423TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002424 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002425 ASSERT_TRUE(CreatePeerConnectionWrappers());
2426 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002427 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002428 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2429 // that doesn't signal SSRCs (from the callee's perspective).
2430 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2431 caller()->CreateAndSetAndSignalOffer();
2432 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2433 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002434 {
2435 MediaExpectations media_expectations;
2436 media_expectations.CalleeExpectsSomeAudio(50);
2437 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2438 }
deadbeef4e2deab2017-09-20 13:56:21 -07002439 // Some audio frames were received, so we should have nonzero "samples
2440 // received" for the track.
2441 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2442 callee()->NewGetStats();
2443 ASSERT_NE(nullptr, report);
2444 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2445 ASSERT_EQ(1U, track_stats.size());
2446 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2447 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2448 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2449
2450 // Create a new offer and munge it to cause the caller to use a new SSRC.
2451 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2452 caller()->CreateAndSetAndSignalOffer();
2453 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2454 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2455 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002456 {
2457 MediaExpectations media_expectations;
2458 media_expectations.CalleeExpectsSomeAudio(25);
2459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2460 }
deadbeef4e2deab2017-09-20 13:56:21 -07002461
2462 report = callee()->NewGetStats();
2463 ASSERT_NE(nullptr, report);
2464 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2465 ASSERT_EQ(1U, track_stats.size());
2466 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2467 // The "total samples received" stat should only be greater than it was
2468 // before.
2469 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2470 // Right now, the new SSRC will cause the counters to reset to 0.
2471 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2472
2473 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002474 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002475 // good sign that we're seeing stats from the old stream that's no longer
2476 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002477 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002478 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2479 EXPECT_LT(*track_stats[0]->concealed_samples,
2480 *track_stats[0]->total_samples_received *
2481 kAcceptableConcealedSamplesPercentage);
2482
2483 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2484 // sanity check that the SSRC really changed.
2485 // TODO(deadbeef): This isn't working right now, because we're not returning
2486 // *any* stats for the inactive stream. Uncomment when the bug is completely
2487 // fixed.
2488 // auto inbound_stream_stats =
2489 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2490 // ASSERT_EQ(2U, inbound_stream_stats.size());
2491}
2492
deadbeef1dcb1642017-03-29 21:08:16 -07002493// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002494TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002495 PeerConnectionFactory::Options dtls_10_options;
2496 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2497 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2498 dtls_10_options));
2499 ConnectFakeSignaling();
2500 // Do normal offer/answer and wait for some frames to be received in each
2501 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002502 caller()->AddAudioVideoTracks();
2503 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002504 caller()->CreateAndSetAndSignalOffer();
2505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002506 MediaExpectations media_expectations;
2507 media_expectations.ExpectBidirectionalAudioAndVideo();
2508 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002509}
2510
2511// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002512TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002513 PeerConnectionFactory::Options dtls_10_options;
2514 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2515 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2516 dtls_10_options));
2517 ConnectFakeSignaling();
2518 // Register UMA observer before signaling begins.
2519 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2520 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2521 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002522 caller()->AddAudioVideoTracks();
2523 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002524 caller()->CreateAndSetAndSignalOffer();
2525 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2526 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002527 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002528 kDefaultTimeout);
2529 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002530 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002531 EXPECT_EQ(1,
2532 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2533 kDefaultSrtpCryptoSuite));
2534}
2535
2536// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002537TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002538 PeerConnectionFactory::Options dtls_12_options;
2539 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2540 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2541 dtls_12_options));
2542 ConnectFakeSignaling();
2543 // Register UMA observer before signaling begins.
2544 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2545 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2546 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002547 caller()->AddAudioVideoTracks();
2548 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002549 caller()->CreateAndSetAndSignalOffer();
2550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2551 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002552 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002553 kDefaultTimeout);
2554 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002555 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002556 EXPECT_EQ(1,
2557 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2558 kDefaultSrtpCryptoSuite));
2559}
2560
2561// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2562// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002563TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002564 PeerConnectionFactory::Options caller_options;
2565 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2566 PeerConnectionFactory::Options callee_options;
2567 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2568 ASSERT_TRUE(
2569 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2570 ConnectFakeSignaling();
2571 // Do normal offer/answer and wait for some frames to be received in each
2572 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002573 caller()->AddAudioVideoTracks();
2574 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002575 caller()->CreateAndSetAndSignalOffer();
2576 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002577 MediaExpectations media_expectations;
2578 media_expectations.ExpectBidirectionalAudioAndVideo();
2579 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002580}
2581
2582// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2583// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002584TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002585 PeerConnectionFactory::Options caller_options;
2586 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2587 PeerConnectionFactory::Options callee_options;
2588 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2589 ASSERT_TRUE(
2590 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2591 ConnectFakeSignaling();
2592 // Do normal offer/answer and wait for some frames to be received in each
2593 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002594 caller()->AddAudioVideoTracks();
2595 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002596 caller()->CreateAndSetAndSignalOffer();
2597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002598 MediaExpectations media_expectations;
2599 media_expectations.ExpectBidirectionalAudioAndVideo();
2600 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002601}
2602
2603// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002604TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002605 bool local_gcm_enabled = false;
2606 bool remote_gcm_enabled = false;
2607 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2608 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2609 expected_cipher_suite);
2610}
2611
2612// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002613TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002614 bool local_gcm_enabled = true;
2615 bool remote_gcm_enabled = true;
2616 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2617 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2618 expected_cipher_suite);
2619}
2620
2621// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002622TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002623 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2624 bool local_gcm_enabled = true;
2625 bool remote_gcm_enabled = false;
2626 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2627 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2628 expected_cipher_suite);
2629}
2630
2631// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002632TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002633 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2634 bool local_gcm_enabled = false;
2635 bool remote_gcm_enabled = true;
2636 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2637 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2638 expected_cipher_suite);
2639}
2640
deadbeef7914b8c2017-04-21 03:23:33 -07002641// Verify that media can be transmitted end-to-end when GCM crypto suites are
2642// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2643// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2644// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002645TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002646 PeerConnectionFactory::Options gcm_options;
2647 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2648 ASSERT_TRUE(
2649 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2650 ConnectFakeSignaling();
2651 // Do normal offer/answer and wait for some frames to be received in each
2652 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002653 caller()->AddAudioVideoTracks();
2654 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002655 caller()->CreateAndSetAndSignalOffer();
2656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002657 MediaExpectations media_expectations;
2658 media_expectations.ExpectBidirectionalAudioAndVideo();
2659 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002660}
2661
deadbeef1dcb1642017-03-29 21:08:16 -07002662// This test sets up a call between two parties with audio, video and an RTP
2663// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002664TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002665 FakeConstraints setup_constraints;
2666 setup_constraints.SetAllowRtpDataChannels();
2667 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2668 &setup_constraints));
2669 ConnectFakeSignaling();
2670 // Expect that data channel created on caller side will show up for callee as
2671 // well.
2672 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002673 caller()->AddAudioVideoTracks();
2674 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002675 caller()->CreateAndSetAndSignalOffer();
2676 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2677 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002678 MediaExpectations media_expectations;
2679 media_expectations.ExpectBidirectionalAudioAndVideo();
2680 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002681 ASSERT_NE(nullptr, caller()->data_channel());
2682 ASSERT_NE(nullptr, callee()->data_channel());
2683 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2684 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2685
2686 // Ensure data can be sent in both directions.
2687 std::string data = "hello world";
2688 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2689 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2690 kDefaultTimeout);
2691 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2692 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2693 kDefaultTimeout);
2694}
2695
2696// Ensure that an RTP data channel is signaled as closed for the caller when
2697// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002698TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002699 RtpDataChannelSignaledClosedInCalleeOffer) {
2700 // Same procedure as above test.
2701 FakeConstraints setup_constraints;
2702 setup_constraints.SetAllowRtpDataChannels();
2703 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2704 &setup_constraints));
2705 ConnectFakeSignaling();
2706 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002707 caller()->AddAudioVideoTracks();
2708 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002709 caller()->CreateAndSetAndSignalOffer();
2710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2711 ASSERT_NE(nullptr, caller()->data_channel());
2712 ASSERT_NE(nullptr, callee()->data_channel());
2713 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2714 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2715
2716 // Close the data channel on the callee, and do an updated offer/answer.
2717 callee()->data_channel()->Close();
2718 callee()->CreateAndSetAndSignalOffer();
2719 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2720 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2721 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2722}
2723
2724// Tests that data is buffered in an RTP data channel until an observer is
2725// registered for it.
2726//
2727// NOTE: RTP data channels can receive data before the underlying
2728// transport has detected that a channel is writable and thus data can be
2729// received before the data channel state changes to open. That is hard to test
2730// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002731TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002732 DataBufferedUntilRtpDataChannelObserverRegistered) {
2733 // Use fake clock and simulated network delay so that we predictably can wait
2734 // until an SCTP message has been delivered without "sleep()"ing.
2735 rtc::ScopedFakeClock fake_clock;
2736 // Some things use a time of "0" as a special value, so we need to start out
2737 // the fake clock at a nonzero time.
2738 // TODO(deadbeef): Fix this.
2739 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2740 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2741 virtual_socket_server()->UpdateDelayDistribution();
2742
2743 FakeConstraints constraints;
2744 constraints.SetAllowRtpDataChannels();
2745 ASSERT_TRUE(
2746 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2747 ConnectFakeSignaling();
2748 caller()->CreateDataChannel();
2749 caller()->CreateAndSetAndSignalOffer();
2750 ASSERT_TRUE(caller()->data_channel() != nullptr);
2751 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2752 kDefaultTimeout, fake_clock);
2753 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2754 kDefaultTimeout, fake_clock);
2755 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2756 callee()->data_channel()->state(), kDefaultTimeout,
2757 fake_clock);
2758
2759 // Unregister the observer which is normally automatically registered.
2760 callee()->data_channel()->UnregisterObserver();
2761 // Send data and advance fake clock until it should have been received.
2762 std::string data = "hello world";
2763 caller()->data_channel()->Send(DataBuffer(data));
2764 SIMULATED_WAIT(false, 50, fake_clock);
2765
2766 // Attach data channel and expect data to be received immediately. Note that
2767 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2768 // further, but data can be received even if the callback is asynchronous.
2769 MockDataChannelObserver new_observer(callee()->data_channel());
2770 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2771 fake_clock);
2772}
2773
2774// This test sets up a call between two parties with audio, video and but only
2775// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002776TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002777 FakeConstraints setup_constraints_1;
2778 setup_constraints_1.SetAllowRtpDataChannels();
2779 // Must disable DTLS to make negotiation succeed.
2780 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2781 false);
2782 FakeConstraints setup_constraints_2;
2783 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2784 false);
2785 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2786 &setup_constraints_1, &setup_constraints_2));
2787 ConnectFakeSignaling();
2788 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002789 caller()->AddAudioVideoTracks();
2790 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002791 caller()->CreateAndSetAndSignalOffer();
2792 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2793 // The caller should still have a data channel, but it should be closed, and
2794 // one should ever have been created for the callee.
2795 EXPECT_TRUE(caller()->data_channel() != nullptr);
2796 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2797 EXPECT_EQ(nullptr, callee()->data_channel());
2798}
2799
2800// This test sets up a call between two parties with audio, and video. When
2801// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002802TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002803 FakeConstraints setup_constraints;
2804 setup_constraints.SetAllowRtpDataChannels();
2805 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2806 &setup_constraints));
2807 ConnectFakeSignaling();
2808 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002809 caller()->AddAudioVideoTracks();
2810 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002811 caller()->CreateAndSetAndSignalOffer();
2812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2813 // Create data channel and do new offer and answer.
2814 caller()->CreateDataChannel();
2815 caller()->CreateAndSetAndSignalOffer();
2816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2817 ASSERT_NE(nullptr, caller()->data_channel());
2818 ASSERT_NE(nullptr, callee()->data_channel());
2819 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2820 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2821 // Ensure data can be sent in both directions.
2822 std::string data = "hello world";
2823 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2824 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2825 kDefaultTimeout);
2826 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2827 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2828 kDefaultTimeout);
2829}
2830
2831#ifdef HAVE_SCTP
2832
2833// This test sets up a call between two parties with audio, video and an SCTP
2834// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002835TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002836 ASSERT_TRUE(CreatePeerConnectionWrappers());
2837 ConnectFakeSignaling();
2838 // Expect that data channel created on caller side will show up for callee as
2839 // well.
2840 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002841 caller()->AddAudioVideoTracks();
2842 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002843 caller()->CreateAndSetAndSignalOffer();
2844 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2845 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002846 MediaExpectations media_expectations;
2847 media_expectations.ExpectBidirectionalAudioAndVideo();
2848 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002849 // Caller data channel should already exist (it created one). Callee data
2850 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2851 ASSERT_NE(nullptr, caller()->data_channel());
2852 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2853 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2854 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2855
2856 // Ensure data can be sent in both directions.
2857 std::string data = "hello world";
2858 caller()->data_channel()->Send(DataBuffer(data));
2859 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2860 kDefaultTimeout);
2861 callee()->data_channel()->Send(DataBuffer(data));
2862 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2863 kDefaultTimeout);
2864}
2865
2866// Ensure that when the callee closes an SCTP data channel, the closing
2867// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002868TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002869 // Same procedure as above test.
2870 ASSERT_TRUE(CreatePeerConnectionWrappers());
2871 ConnectFakeSignaling();
2872 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002873 caller()->AddAudioVideoTracks();
2874 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002875 caller()->CreateAndSetAndSignalOffer();
2876 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2877 ASSERT_NE(nullptr, caller()->data_channel());
2878 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2879 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2880 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2881
2882 // Close the data channel on the callee side, and wait for it to reach the
2883 // "closed" state on both sides.
2884 callee()->data_channel()->Close();
2885 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2886 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2887}
2888
Seth Hampson2f0d7022018-02-20 11:54:42 -08002889TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07002890 ASSERT_TRUE(CreatePeerConnectionWrappers());
2891 ConnectFakeSignaling();
2892 webrtc::DataChannelInit init;
2893 init.id = 53;
2894 init.maxRetransmits = 52;
2895 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002896 caller()->AddAudioVideoTracks();
2897 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002898 caller()->CreateAndSetAndSignalOffer();
2899 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002900 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2901 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002902 EXPECT_EQ(init.id, callee()->data_channel()->id());
2903 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2904 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2905 EXPECT_FALSE(callee()->data_channel()->negotiated());
2906}
2907
deadbeef1dcb1642017-03-29 21:08:16 -07002908// Test usrsctp's ability to process unordered data stream, where data actually
2909// arrives out of order using simulated delays. Previously there have been some
2910// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002911TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002912 // Introduce random network delays.
2913 // Otherwise it's not a true "unordered" test.
2914 virtual_socket_server()->set_delay_mean(20);
2915 virtual_socket_server()->set_delay_stddev(5);
2916 virtual_socket_server()->UpdateDelayDistribution();
2917 // Normal procedure, but with unordered data channel config.
2918 ASSERT_TRUE(CreatePeerConnectionWrappers());
2919 ConnectFakeSignaling();
2920 webrtc::DataChannelInit init;
2921 init.ordered = false;
2922 caller()->CreateDataChannel(&init);
2923 caller()->CreateAndSetAndSignalOffer();
2924 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2925 ASSERT_NE(nullptr, caller()->data_channel());
2926 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2927 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2928 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2929
2930 static constexpr int kNumMessages = 100;
2931 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2932 static constexpr size_t kMaxMessageSize = 4096;
2933 // Create and send random messages.
2934 std::vector<std::string> sent_messages;
2935 for (int i = 0; i < kNumMessages; ++i) {
2936 size_t length =
2937 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2938 std::string message;
2939 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2940 caller()->data_channel()->Send(DataBuffer(message));
2941 callee()->data_channel()->Send(DataBuffer(message));
2942 sent_messages.push_back(message);
2943 }
2944
2945 // Wait for all messages to be received.
2946 EXPECT_EQ_WAIT(kNumMessages,
2947 caller()->data_observer()->received_message_count(),
2948 kDefaultTimeout);
2949 EXPECT_EQ_WAIT(kNumMessages,
2950 callee()->data_observer()->received_message_count(),
2951 kDefaultTimeout);
2952
2953 // Sort and compare to make sure none of the messages were corrupted.
2954 std::vector<std::string> caller_received_messages =
2955 caller()->data_observer()->messages();
2956 std::vector<std::string> callee_received_messages =
2957 callee()->data_observer()->messages();
2958 std::sort(sent_messages.begin(), sent_messages.end());
2959 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2960 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2961 EXPECT_EQ(sent_messages, caller_received_messages);
2962 EXPECT_EQ(sent_messages, callee_received_messages);
2963}
2964
2965// This test sets up a call between two parties with audio, and video. When
2966// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002968 ASSERT_TRUE(CreatePeerConnectionWrappers());
2969 ConnectFakeSignaling();
2970 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002971 caller()->AddAudioVideoTracks();
2972 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002973 caller()->CreateAndSetAndSignalOffer();
2974 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2975 // Create data channel and do new offer and answer.
2976 caller()->CreateDataChannel();
2977 caller()->CreateAndSetAndSignalOffer();
2978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2979 // Caller data channel should already exist (it created one). Callee data
2980 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2981 ASSERT_NE(nullptr, caller()->data_channel());
2982 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2983 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2984 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2985 // Ensure data can be sent in both directions.
2986 std::string data = "hello world";
2987 caller()->data_channel()->Send(DataBuffer(data));
2988 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2989 kDefaultTimeout);
2990 callee()->data_channel()->Send(DataBuffer(data));
2991 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2992 kDefaultTimeout);
2993}
2994
deadbeef7914b8c2017-04-21 03:23:33 -07002995// Set up a connection initially just using SCTP data channels, later upgrading
2996// to audio/video, ensuring frames are received end-to-end. Effectively the
2997// inverse of the test above.
2998// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08002999TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003000 ASSERT_TRUE(CreatePeerConnectionWrappers());
3001 ConnectFakeSignaling();
3002 // Do initial offer/answer with just data channel.
3003 caller()->CreateDataChannel();
3004 caller()->CreateAndSetAndSignalOffer();
3005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3006 // Wait until data can be sent over the data channel.
3007 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3008 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3009 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3010
3011 // Do subsequent offer/answer with two-way audio and video. Audio and video
3012 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003013 caller()->AddAudioVideoTracks();
3014 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003015 caller()->CreateAndSetAndSignalOffer();
3016 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003017 MediaExpectations media_expectations;
3018 media_expectations.ExpectBidirectionalAudioAndVideo();
3019 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003020}
3021
deadbeef8b7e9ad2017-05-25 09:38:55 -07003022static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003023 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003024 GetFirstDataContentDescription(desc);
3025 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003026 dcd_offer->set_use_sctpmap(false);
3027 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3028}
3029
3030// Test that the data channel works when a spec-compliant SCTP m= section is
3031// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3032// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003033TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003034 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3035 ASSERT_TRUE(CreatePeerConnectionWrappers());
3036 ConnectFakeSignaling();
3037 caller()->CreateDataChannel();
3038 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3039 caller()->CreateAndSetAndSignalOffer();
3040 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3041 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3042 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3043 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3044
3045 // Ensure data can be sent in both directions.
3046 std::string data = "hello world";
3047 caller()->data_channel()->Send(DataBuffer(data));
3048 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3049 kDefaultTimeout);
3050 callee()->data_channel()->Send(DataBuffer(data));
3051 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3052 kDefaultTimeout);
3053}
3054
deadbeef1dcb1642017-03-29 21:08:16 -07003055#endif // HAVE_SCTP
3056
3057// Test that the ICE connection and gathering states eventually reach
3058// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003059TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003060 ASSERT_TRUE(CreatePeerConnectionWrappers());
3061 ConnectFakeSignaling();
3062 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003063 caller()->AddAudioVideoTracks();
3064 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003065 caller()->CreateAndSetAndSignalOffer();
3066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3067 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3068 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3069 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3070 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3071 // After the best candidate pair is selected and all candidates are signaled,
3072 // the ICE connection state should reach "complete".
3073 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3074 // answerer/"callee" by default) only reaches "connected". When this is
3075 // fixed, this test should be updated.
3076 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3077 caller()->ice_connection_state(), kDefaultTimeout);
3078 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3079 callee()->ice_connection_state(), kDefaultTimeout);
3080}
3081
Steve Antonede9ca52017-10-16 13:04:27 -07003082// Test that firewalling the ICE connection causes the clients to identify the
3083// disconnected state and then removing the firewall causes them to reconnect.
3084class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003085 : public PeerConnectionIntegrationBaseTest,
3086 public ::testing::WithParamInterface<
3087 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003088 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003089 PeerConnectionIntegrationIceStatesTest()
3090 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3091 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003092 }
3093
3094 void StartStunServer(const SocketAddress& server_address) {
3095 stun_server_.reset(
3096 cricket::TestStunServer::Create(network_thread(), server_address));
3097 }
3098
3099 bool TestIPv6() {
3100 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3101 }
3102
3103 void SetPortAllocatorFlags() {
3104 caller()->port_allocator()->set_flags(port_allocator_flags_);
3105 callee()->port_allocator()->set_flags(port_allocator_flags_);
3106 }
3107
3108 std::vector<SocketAddress> CallerAddresses() {
3109 std::vector<SocketAddress> addresses;
3110 addresses.push_back(SocketAddress("1.1.1.1", 0));
3111 if (TestIPv6()) {
3112 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3113 }
3114 return addresses;
3115 }
3116
3117 std::vector<SocketAddress> CalleeAddresses() {
3118 std::vector<SocketAddress> addresses;
3119 addresses.push_back(SocketAddress("2.2.2.2", 0));
3120 if (TestIPv6()) {
3121 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3122 }
3123 return addresses;
3124 }
3125
3126 void SetUpNetworkInterfaces() {
3127 // Remove the default interfaces added by the test infrastructure.
3128 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3129 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3130
3131 // Add network addresses for test.
3132 for (const auto& caller_address : CallerAddresses()) {
3133 caller()->network()->AddInterface(caller_address);
3134 }
3135 for (const auto& callee_address : CalleeAddresses()) {
3136 callee()->network()->AddInterface(callee_address);
3137 }
3138 }
3139
3140 private:
3141 uint32_t port_allocator_flags_;
3142 std::unique_ptr<cricket::TestStunServer> stun_server_;
3143};
3144
3145// Tests that the PeerConnection goes through all the ICE gathering/connection
3146// states over the duration of the call. This includes Disconnected and Failed
3147// states, induced by putting a firewall between the peers and waiting for them
3148// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003149TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3150 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3151 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3152 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003153
3154 const SocketAddress kStunServerAddress =
3155 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3156 StartStunServer(kStunServerAddress);
3157
3158 PeerConnectionInterface::RTCConfiguration config;
3159 PeerConnectionInterface::IceServer ice_stun_server;
3160 ice_stun_server.urls.push_back(
3161 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3162 kStunServerAddress.PortAsString());
3163 config.servers.push_back(ice_stun_server);
3164
3165 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3166 ConnectFakeSignaling();
3167 SetPortAllocatorFlags();
3168 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003169 caller()->AddAudioVideoTracks();
3170 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003171
3172 // Initial state before anything happens.
3173 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3174 caller()->ice_gathering_state());
3175 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3176 caller()->ice_connection_state());
3177
3178 // Start the call by creating the offer, setting it as the local description,
3179 // then sending it to the peer who will respond with an answer. This happens
3180 // asynchronously so that we can watch the states as it runs in the
3181 // background.
3182 caller()->CreateAndSetAndSignalOffer();
3183
Steve Anton83119dd2017-11-10 16:19:52 -08003184 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3185 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003186
3187 // Verify that the observer was notified of the intermediate transitions.
3188 EXPECT_THAT(caller()->ice_connection_state_history(),
3189 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3190 PeerConnectionInterface::kIceConnectionConnected,
3191 PeerConnectionInterface::kIceConnectionCompleted));
3192 EXPECT_THAT(caller()->ice_gathering_state_history(),
3193 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3194 PeerConnectionInterface::kIceGatheringComplete));
3195
3196 // Block connections to/from the caller and wait for ICE to become
3197 // disconnected.
3198 for (const auto& caller_address : CallerAddresses()) {
3199 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3200 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003201 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003202 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3203 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003204
3205 // Let ICE re-establish by removing the firewall rules.
3206 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003207 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003208 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3209 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003210
3211 // According to RFC7675, if there is no response within 30 seconds then the
3212 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003213 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003214 constexpr int kConsentTimeout = 30000;
3215 for (const auto& caller_address : CallerAddresses()) {
3216 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3217 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003218 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003219 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3220 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003221}
3222
3223// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3224// and that the statistics in the metric observers are updated correctly.
3225TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3226 ASSERT_TRUE(CreatePeerConnectionWrappers());
3227 ConnectFakeSignaling();
3228 SetPortAllocatorFlags();
3229 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003230 caller()->AddAudioVideoTracks();
3231 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003232
3233 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3234 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3235 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3236
3237 caller()->CreateAndSetAndSignalOffer();
3238
3239 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3240
3241 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3242 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3243 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3244 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3245 if (TestIPv6()) {
3246 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3247 // connection.
3248 EXPECT_EQ(0u, num_best_ipv4);
3249 EXPECT_EQ(1u, num_best_ipv6);
3250 } else {
3251 EXPECT_EQ(1u, num_best_ipv4);
3252 EXPECT_EQ(0u, num_best_ipv6);
3253 }
3254
3255 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3256 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3257 webrtc::kIceCandidatePairHostHost));
3258 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3259 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3260 webrtc::kIceCandidatePairHostPublicHostPublic));
3261}
3262
3263constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3264 cricket::PORTALLOCATOR_DISABLE_STUN |
3265 cricket::PORTALLOCATOR_DISABLE_RELAY;
3266constexpr uint32_t kFlagsIPv6NoStun =
3267 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3268 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3269constexpr uint32_t kFlagsIPv4Stun =
3270 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3271
Seth Hampson2f0d7022018-02-20 11:54:42 -08003272INSTANTIATE_TEST_CASE_P(
3273 PeerConnectionIntegrationTest,
3274 PeerConnectionIntegrationIceStatesTest,
3275 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3276 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3277 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3278 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003279
deadbeef1dcb1642017-03-29 21:08:16 -07003280// This test sets up a call between two parties with audio and video.
3281// During the call, the caller restarts ICE and the test verifies that
3282// new ICE candidates are generated and audio and video still can flow, and the
3283// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003284TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003285 ASSERT_TRUE(CreatePeerConnectionWrappers());
3286 ConnectFakeSignaling();
3287 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003288 caller()->AddAudioVideoTracks();
3289 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003290 caller()->CreateAndSetAndSignalOffer();
3291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3292 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3293 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3294 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3295 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3296
3297 // To verify that the ICE restart actually occurs, get
3298 // ufrag/password/candidates before and after restart.
3299 // Create an SDP string of the first audio candidate for both clients.
3300 const webrtc::IceCandidateCollection* audio_candidates_caller =
3301 caller()->pc()->local_description()->candidates(0);
3302 const webrtc::IceCandidateCollection* audio_candidates_callee =
3303 callee()->pc()->local_description()->candidates(0);
3304 ASSERT_GT(audio_candidates_caller->count(), 0u);
3305 ASSERT_GT(audio_candidates_callee->count(), 0u);
3306 std::string caller_candidate_pre_restart;
3307 ASSERT_TRUE(
3308 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3309 std::string callee_candidate_pre_restart;
3310 ASSERT_TRUE(
3311 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3312 const cricket::SessionDescription* desc =
3313 caller()->pc()->local_description()->description();
3314 std::string caller_ufrag_pre_restart =
3315 desc->transport_infos()[0].description.ice_ufrag;
3316 desc = callee()->pc()->local_description()->description();
3317 std::string callee_ufrag_pre_restart =
3318 desc->transport_infos()[0].description.ice_ufrag;
3319
3320 // Have the caller initiate an ICE restart.
3321 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3322 caller()->CreateAndSetAndSignalOffer();
3323 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3324 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3325 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3326 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3327 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3328
3329 // Grab the ufrags/candidates again.
3330 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3331 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3332 ASSERT_GT(audio_candidates_caller->count(), 0u);
3333 ASSERT_GT(audio_candidates_callee->count(), 0u);
3334 std::string caller_candidate_post_restart;
3335 ASSERT_TRUE(
3336 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3337 std::string callee_candidate_post_restart;
3338 ASSERT_TRUE(
3339 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3340 desc = caller()->pc()->local_description()->description();
3341 std::string caller_ufrag_post_restart =
3342 desc->transport_infos()[0].description.ice_ufrag;
3343 desc = callee()->pc()->local_description()->description();
3344 std::string callee_ufrag_post_restart =
3345 desc->transport_infos()[0].description.ice_ufrag;
3346 // Sanity check that an ICE restart was actually negotiated in SDP.
3347 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3348 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3349 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3350 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3351
3352 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003353 MediaExpectations media_expectations;
3354 media_expectations.ExpectBidirectionalAudioAndVideo();
3355 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003356}
3357
3358// Verify that audio/video can be received end-to-end when ICE renomination is
3359// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003360TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003361 PeerConnectionInterface::RTCConfiguration config;
3362 config.enable_ice_renomination = true;
3363 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3364 ConnectFakeSignaling();
3365 // Do normal offer/answer and wait for some frames to be received in each
3366 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003367 caller()->AddAudioVideoTracks();
3368 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003369 caller()->CreateAndSetAndSignalOffer();
3370 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3371 // Sanity check that ICE renomination was actually negotiated.
3372 const cricket::SessionDescription* desc =
3373 caller()->pc()->local_description()->description();
3374 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003375 ASSERT_NE(
3376 info.description.transport_options.end(),
3377 std::find(info.description.transport_options.begin(),
3378 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003379 }
3380 desc = callee()->pc()->local_description()->description();
3381 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003382 ASSERT_NE(
3383 info.description.transport_options.end(),
3384 std::find(info.description.transport_options.begin(),
3385 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003386 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003387 MediaExpectations media_expectations;
3388 media_expectations.ExpectBidirectionalAudioAndVideo();
3389 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003390}
3391
Steve Anton6f25b092017-10-23 09:39:20 -07003392// With a max bundle policy and RTCP muxing, adding a new media description to
3393// the connection should not affect ICE at all because the new media will use
3394// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003395TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003396 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003397 PeerConnectionInterface::RTCConfiguration config;
3398 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3399 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3400 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3401 config, PeerConnectionInterface::RTCConfiguration()));
3402 ConnectFakeSignaling();
3403
Steve Anton15324772018-01-16 10:26:49 -08003404 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003405 caller()->CreateAndSetAndSignalOffer();
3406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003407 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3408 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003409
3410 caller()->clear_ice_connection_state_history();
3411
Steve Anton15324772018-01-16 10:26:49 -08003412 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003413 caller()->CreateAndSetAndSignalOffer();
3414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3415
3416 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3417}
3418
deadbeef1dcb1642017-03-29 21:08:16 -07003419// This test sets up a call between two parties with audio and video. It then
3420// renegotiates setting the video m-line to "port 0", then later renegotiates
3421// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003422TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003423 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3424 ASSERT_TRUE(CreatePeerConnectionWrappers());
3425 ConnectFakeSignaling();
3426
3427 // Do initial negotiation, only sending media from the caller. Will result in
3428 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003429 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003430 caller()->CreateAndSetAndSignalOffer();
3431 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3432
3433 // Negotiate again, disabling the video "m=" section (the callee will set the
3434 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003435 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3436 PeerConnectionInterface::RTCOfferAnswerOptions options;
3437 options.offer_to_receive_video = 0;
3438 callee()->SetOfferAnswerOptions(options);
3439 } else {
3440 callee()->SetRemoteOfferHandler([this] {
3441 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3442 });
3443 }
deadbeef1dcb1642017-03-29 21:08:16 -07003444 caller()->CreateAndSetAndSignalOffer();
3445 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3446 // Sanity check that video "m=" section was actually rejected.
3447 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3448 callee()->pc()->local_description()->description());
3449 ASSERT_NE(nullptr, answer_video_content);
3450 ASSERT_TRUE(answer_video_content->rejected);
3451
3452 // Enable video and do negotiation again, making sure video is received
3453 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003454 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3455 PeerConnectionInterface::RTCOfferAnswerOptions options;
3456 options.offer_to_receive_video = 1;
3457 callee()->SetOfferAnswerOptions(options);
3458 } else {
3459 // The caller's transceiver is stopped, so we need to add another track.
3460 auto caller_transceiver =
3461 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3462 EXPECT_TRUE(caller_transceiver->stopped());
3463 caller()->AddVideoTrack();
3464 }
3465 callee()->AddVideoTrack();
3466 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003467 caller()->CreateAndSetAndSignalOffer();
3468 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003469
deadbeef1dcb1642017-03-29 21:08:16 -07003470 // Verify the caller receives frames from the newly added stream, and the
3471 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003472 MediaExpectations media_expectations;
3473 media_expectations.CalleeExpectsSomeAudio();
3474 media_expectations.ExpectBidirectionalVideo();
3475 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003476}
3477
3478// This test sets up a Jsep call between two parties with external
3479// VideoDecoderFactory.
3480// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3481// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003482TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003483 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3484 ASSERT_TRUE(CreatePeerConnectionWrappers());
3485 EnableVideoDecoderFactory();
3486 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003487 caller()->AddAudioVideoTracks();
3488 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003489 caller()->CreateAndSetAndSignalOffer();
3490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003491 MediaExpectations media_expectations;
3492 media_expectations.ExpectBidirectionalAudioAndVideo();
3493 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003494}
3495
3496// This tests that if we negotiate after calling CreateSender but before we
3497// have a track, then set a track later, frames from the newly-set track are
3498// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003499TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003500 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3501 ASSERT_TRUE(CreatePeerConnectionWrappers());
3502 ConnectFakeSignaling();
3503 auto caller_audio_sender =
3504 caller()->pc()->CreateSender("audio", "caller_stream");
3505 auto caller_video_sender =
3506 caller()->pc()->CreateSender("video", "caller_stream");
3507 auto callee_audio_sender =
3508 callee()->pc()->CreateSender("audio", "callee_stream");
3509 auto callee_video_sender =
3510 callee()->pc()->CreateSender("video", "callee_stream");
3511 caller()->CreateAndSetAndSignalOffer();
3512 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3513 // Wait for ICE to complete, without any tracks being set.
3514 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3515 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3516 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3517 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3518 // Now set the tracks, and expect frames to immediately start flowing.
3519 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3520 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3521 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3522 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003523 MediaExpectations media_expectations;
3524 media_expectations.ExpectBidirectionalAudioAndVideo();
3525 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3526}
3527
3528// This tests that if we negotiate after calling AddTransceiver but before we
3529// have a track, then set a track later, frames from the newly-set tracks are
3530// received end-to-end.
3531TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3532 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3533 ASSERT_TRUE(CreatePeerConnectionWrappers());
3534 ConnectFakeSignaling();
3535 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3536 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3537 auto caller_audio_sender = audio_result.MoveValue()->sender();
3538 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3539 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3540 auto caller_video_sender = video_result.MoveValue()->sender();
3541 callee()->SetRemoteOfferHandler([this] {
3542 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3543 callee()->pc()->GetTransceivers()[0]->SetDirection(
3544 RtpTransceiverDirection::kSendRecv);
3545 callee()->pc()->GetTransceivers()[1]->SetDirection(
3546 RtpTransceiverDirection::kSendRecv);
3547 });
3548 caller()->CreateAndSetAndSignalOffer();
3549 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3550 // Wait for ICE to complete, without any tracks being set.
3551 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3552 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3553 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3554 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3555 // Now set the tracks, and expect frames to immediately start flowing.
3556 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3557 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3558 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3559 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3560 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3561 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3562 MediaExpectations media_expectations;
3563 media_expectations.ExpectBidirectionalAudioAndVideo();
3564 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003565}
3566
3567// This test verifies that a remote video track can be added via AddStream,
3568// and sent end-to-end. For this particular test, it's simply echoed back
3569// from the caller to the callee, rather than being forwarded to a third
3570// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003571TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003572 ASSERT_TRUE(CreatePeerConnectionWrappers());
3573 ConnectFakeSignaling();
3574 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003575 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003576 caller()->CreateAndSetAndSignalOffer();
3577 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3578 ASSERT_EQ(1, callee()->remote_streams()->count());
3579
3580 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3581 // time).
3582 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3583 callee()->CreateAndSetAndSignalOffer();
3584 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3585
Seth Hampson2f0d7022018-02-20 11:54:42 -08003586 MediaExpectations media_expectations;
3587 media_expectations.ExpectBidirectionalVideo();
3588 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003589}
3590
3591// Test that we achieve the expected end-to-end connection time, using a
3592// fake clock and simulated latency on the media and signaling paths.
3593// We use a TURN<->TURN connection because this is usually the quickest to
3594// set up initially, especially when we're confident the connection will work
3595// and can start sending media before we get a STUN response.
3596//
3597// With various optimizations enabled, here are the network delays we expect to
3598// be on the critical path:
3599// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3600// signaling answer (with DTLS fingerprint).
3601// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3602// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3603// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003604TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003605 rtc::ScopedFakeClock fake_clock;
3606 // Some things use a time of "0" as a special value, so we need to start out
3607 // the fake clock at a nonzero time.
3608 // TODO(deadbeef): Fix this.
3609 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3610
3611 static constexpr int media_hop_delay_ms = 50;
3612 static constexpr int signaling_trip_delay_ms = 500;
3613 // For explanation of these values, see comment above.
3614 static constexpr int required_media_hops = 9;
3615 static constexpr int required_signaling_trips = 2;
3616 // For internal delays (such as posting an event asychronously).
3617 static constexpr int allowed_internal_delay_ms = 20;
3618 static constexpr int total_connection_time_ms =
3619 media_hop_delay_ms * required_media_hops +
3620 signaling_trip_delay_ms * required_signaling_trips +
3621 allowed_internal_delay_ms;
3622
3623 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3624 3478};
3625 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3626 0};
3627 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3628 3478};
3629 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3630 0};
3631 cricket::TestTurnServer turn_server_1(network_thread(),
3632 turn_server_1_internal_address,
3633 turn_server_1_external_address);
3634 cricket::TestTurnServer turn_server_2(network_thread(),
3635 turn_server_2_internal_address,
3636 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003637
deadbeef1dcb1642017-03-29 21:08:16 -07003638 // Bypass permission check on received packets so media can be sent before
3639 // the candidate is signaled.
3640 turn_server_1.set_enable_permission_checks(false);
3641 turn_server_2.set_enable_permission_checks(false);
3642
3643 PeerConnectionInterface::RTCConfiguration client_1_config;
3644 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3645 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3646 ice_server_1.username = "test";
3647 ice_server_1.password = "test";
3648 client_1_config.servers.push_back(ice_server_1);
3649 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3650 client_1_config.presume_writable_when_fully_relayed = true;
3651
3652 PeerConnectionInterface::RTCConfiguration client_2_config;
3653 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3654 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3655 ice_server_2.username = "test";
3656 ice_server_2.password = "test";
3657 client_2_config.servers.push_back(ice_server_2);
3658 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3659 client_2_config.presume_writable_when_fully_relayed = true;
3660
3661 ASSERT_TRUE(
3662 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3663 // Set up the simulated delays.
3664 SetSignalingDelayMs(signaling_trip_delay_ms);
3665 ConnectFakeSignaling();
3666 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3667 virtual_socket_server()->UpdateDelayDistribution();
3668
3669 // Set "offer to receive audio/video" without adding any tracks, so we just
3670 // set up ICE/DTLS with no media.
3671 PeerConnectionInterface::RTCOfferAnswerOptions options;
3672 options.offer_to_receive_audio = 1;
3673 options.offer_to_receive_video = 1;
3674 caller()->SetOfferAnswerOptions(options);
3675 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003676 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3677 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003678 // Need to free the clients here since they're using things we created on
3679 // the stack.
3680 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3681 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3682}
3683
Jonas Orelandbdcee282017-10-10 14:01:40 +02003684// Verify that a TurnCustomizer passed in through RTCConfiguration
3685// is actually used by the underlying TURN candidate pair.
3686// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003687TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003688 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3689 3478};
3690 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3691 0};
3692 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3693 3478};
3694 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3695 0};
3696 cricket::TestTurnServer turn_server_1(network_thread(),
3697 turn_server_1_internal_address,
3698 turn_server_1_external_address);
3699 cricket::TestTurnServer turn_server_2(network_thread(),
3700 turn_server_2_internal_address,
3701 turn_server_2_external_address);
3702
3703 PeerConnectionInterface::RTCConfiguration client_1_config;
3704 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3705 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3706 ice_server_1.username = "test";
3707 ice_server_1.password = "test";
3708 client_1_config.servers.push_back(ice_server_1);
3709 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3710 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3711 client_1_config.turn_customizer = customizer1.get();
3712
3713 PeerConnectionInterface::RTCConfiguration client_2_config;
3714 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3715 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3716 ice_server_2.username = "test";
3717 ice_server_2.password = "test";
3718 client_2_config.servers.push_back(ice_server_2);
3719 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3720 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3721 client_2_config.turn_customizer = customizer2.get();
3722
3723 ASSERT_TRUE(
3724 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3725 ConnectFakeSignaling();
3726
3727 // Set "offer to receive audio/video" without adding any tracks, so we just
3728 // set up ICE/DTLS with no media.
3729 PeerConnectionInterface::RTCOfferAnswerOptions options;
3730 options.offer_to_receive_audio = 1;
3731 options.offer_to_receive_video = 1;
3732 caller()->SetOfferAnswerOptions(options);
3733 caller()->CreateAndSetAndSignalOffer();
3734 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3735
3736 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3737 EXPECT_GT(customizer1->modify_cnt_, 0u);
3738
3739 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3740 EXPECT_GT(customizer2->modify_cnt_, 0u);
3741
3742 // Need to free the clients here since they're using things we created on
3743 // the stack.
3744 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3745 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3746}
3747
deadbeefc964d0b2017-04-03 10:03:35 -07003748// Test that audio and video flow end-to-end when codec names don't use the
3749// expected casing, given that they're supposed to be case insensitive. To test
3750// this, all but one codec is removed from each media description, and its
3751// casing is changed.
3752//
3753// In the past, this has regressed and caused crashes/black video, due to the
3754// fact that code at some layers was doing case-insensitive comparisons and
3755// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003756TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003757 ASSERT_TRUE(CreatePeerConnectionWrappers());
3758 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003759 caller()->AddAudioVideoTracks();
3760 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003761
3762 // Remove all but one audio/video codec (opus and VP8), and change the
3763 // casing of the caller's generated offer.
3764 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3765 cricket::AudioContentDescription* audio =
3766 GetFirstAudioContentDescription(description);
3767 ASSERT_NE(nullptr, audio);
3768 auto audio_codecs = audio->codecs();
3769 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3770 [](const cricket::AudioCodec& codec) {
3771 return codec.name != "opus";
3772 }),
3773 audio_codecs.end());
3774 ASSERT_EQ(1u, audio_codecs.size());
3775 audio_codecs[0].name = "OpUs";
3776 audio->set_codecs(audio_codecs);
3777
3778 cricket::VideoContentDescription* video =
3779 GetFirstVideoContentDescription(description);
3780 ASSERT_NE(nullptr, video);
3781 auto video_codecs = video->codecs();
3782 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3783 [](const cricket::VideoCodec& codec) {
3784 return codec.name != "VP8";
3785 }),
3786 video_codecs.end());
3787 ASSERT_EQ(1u, video_codecs.size());
3788 video_codecs[0].name = "vP8";
3789 video->set_codecs(video_codecs);
3790 });
3791
3792 caller()->CreateAndSetAndSignalOffer();
3793 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3794
3795 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003796 MediaExpectations media_expectations;
3797 media_expectations.ExpectBidirectionalAudioAndVideo();
3798 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003799}
3800
Seth Hampson2f0d7022018-02-20 11:54:42 -08003801TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003802 ASSERT_TRUE(CreatePeerConnectionWrappers());
3803 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003804 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003805 caller()->CreateAndSetAndSignalOffer();
3806 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003807 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003808 MediaExpectations media_expectations;
3809 media_expectations.CalleeExpectsSomeAudio(1);
3810 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003811 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3812 auto receiver = callee()->pc()->GetReceivers()[0];
3813 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3814
3815 auto contributing_sources = receiver->GetSources();
3816 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3817 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3818 contributing_sources[0].source_id());
3819}
3820
deadbeef2f425aa2017-04-14 10:41:32 -07003821// Test that if a track is removed and added again with a different stream ID,
3822// the new stream ID is successfully communicated in SDP and media continues to
3823// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003824// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3825// it will not reuse a transceiver that has already been sending. After creating
3826// a new transceiver it tries to create an offer with two senders of the same
3827// track ids and it fails.
3828TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003829 ASSERT_TRUE(CreatePeerConnectionWrappers());
3830 ConnectFakeSignaling();
3831
3832 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3833 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3834 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3835 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3836
3837 // Add track using stream 1, do offer/answer.
3838 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3839 caller()->CreateLocalAudioTrack();
3840 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3841 caller()->pc()->AddTrack(track, {stream_1.get()});
3842 caller()->CreateAndSetAndSignalOffer();
3843 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003844 {
3845 MediaExpectations media_expectations;
3846 media_expectations.CalleeExpectsSomeAudio(1);
3847 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3848 }
deadbeef2f425aa2017-04-14 10:41:32 -07003849 // Remove the sender, and create a new one with the new stream.
3850 caller()->pc()->RemoveTrack(sender);
3851 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3852 caller()->CreateAndSetAndSignalOffer();
3853 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3854 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003855 {
3856 MediaExpectations media_expectations;
3857 media_expectations.CalleeExpectsSomeAudio();
3858 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3859 }
deadbeef2f425aa2017-04-14 10:41:32 -07003860}
3861
Seth Hampson2f0d7022018-02-20 11:54:42 -08003862TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003863 ASSERT_TRUE(CreatePeerConnectionWrappers());
3864 ConnectFakeSignaling();
3865
3866 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3867 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3868 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3869 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003870 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3871 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003872
Steve Anton15324772018-01-16 10:26:49 -08003873 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003874 caller()->CreateAndSetAndSignalOffer();
3875 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3876}
3877
Steve Antonede9ca52017-10-16 13:04:27 -07003878// Test that if candidates are only signaled by applying full session
3879// descriptions (instead of using AddIceCandidate), the peers can connect to
3880// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003881TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07003882 ASSERT_TRUE(CreatePeerConnectionWrappers());
3883 // Each side will signal the session descriptions but not candidates.
3884 ConnectFakeSignalingForSdpOnly();
3885
3886 // Add audio video track and exchange the initial offer/answer with media
3887 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003888 caller()->AddAudioVideoTracks();
3889 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003890 caller()->CreateAndSetAndSignalOffer();
3891
3892 // Wait for all candidates to be gathered on both the caller and callee.
3893 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3894 caller()->ice_gathering_state(), kDefaultTimeout);
3895 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3896 callee()->ice_gathering_state(), kDefaultTimeout);
3897
3898 // The candidates will now be included in the session description, so
3899 // signaling them will start the ICE connection.
3900 caller()->CreateAndSetAndSignalOffer();
3901 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3902
3903 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003904 MediaExpectations media_expectations;
3905 media_expectations.ExpectBidirectionalAudioAndVideo();
3906 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003907}
3908
henrika5f6bf242017-11-01 11:06:56 +01003909// Test that SetAudioPlayout can be used to disable audio playout from the
3910// start, then later enable it. This may be useful, for example, if the caller
3911// needs to play a local ringtone until some event occurs, after which it
3912// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003913TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003914 ASSERT_TRUE(CreatePeerConnectionWrappers());
3915 ConnectFakeSignaling();
3916
3917 // Set up audio-only call where audio playout is disabled on caller's side.
3918 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003919 caller()->AddAudioTrack();
3920 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003921 caller()->CreateAndSetAndSignalOffer();
3922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3923
3924 // Pump messages for a second.
3925 WAIT(false, 1000);
3926 // Since audio playout is disabled, the caller shouldn't have received
3927 // anything (at the playout level, at least).
3928 EXPECT_EQ(0, caller()->audio_frames_received());
3929 // As a sanity check, make sure the callee (for which playout isn't disabled)
3930 // did still see frames on its audio level.
3931 ASSERT_GT(callee()->audio_frames_received(), 0);
3932
3933 // Enable playout again, and ensure audio starts flowing.
3934 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003935 MediaExpectations media_expectations;
3936 media_expectations.ExpectBidirectionalAudio();
3937 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003938}
3939
3940double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3941 auto report = pc->NewGetStats();
3942 auto track_stats_list =
3943 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3944 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3945 for (const auto* track_stats : track_stats_list) {
3946 if (track_stats->remote_source.is_defined() &&
3947 *track_stats->remote_source) {
3948 remote_track_stats = track_stats;
3949 break;
3950 }
3951 }
3952
3953 if (!remote_track_stats->total_audio_energy.is_defined()) {
3954 return 0.0;
3955 }
3956 return *remote_track_stats->total_audio_energy;
3957}
3958
3959// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3960// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003961TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01003962 DisableAudioPlayoutStillGeneratesAudioStats) {
3963 ASSERT_TRUE(CreatePeerConnectionWrappers());
3964 ConnectFakeSignaling();
3965
3966 // Set up audio-only call where playout is disabled but audio-processing is
3967 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003968 caller()->AddAudioTrack();
3969 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003970 caller()->pc()->SetAudioPlayout(false);
3971
3972 caller()->CreateAndSetAndSignalOffer();
3973 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3974
3975 // Wait for the callee to receive audio stats.
3976 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3977}
3978
henrika4f167df2017-11-01 14:45:55 +01003979// Test that SetAudioRecording can be used to disable audio recording from the
3980// start, then later enable it. This may be useful, for example, if the caller
3981// wants to ensure that no audio resources are active before a certain state
3982// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003983TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01003984 ASSERT_TRUE(CreatePeerConnectionWrappers());
3985 ConnectFakeSignaling();
3986
3987 // Set up audio-only call where audio recording is disabled on caller's side.
3988 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003989 caller()->AddAudioTrack();
3990 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003991 caller()->CreateAndSetAndSignalOffer();
3992 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3993
3994 // Pump messages for a second.
3995 WAIT(false, 1000);
3996 // Since caller has disabled audio recording, the callee shouldn't have
3997 // received anything.
3998 EXPECT_EQ(0, callee()->audio_frames_received());
3999 // As a sanity check, make sure the caller did still see frames on its
4000 // audio level since audio recording is enabled on the calle side.
4001 ASSERT_GT(caller()->audio_frames_received(), 0);
4002
4003 // Enable audio recording again, and ensure audio starts flowing.
4004 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004005 MediaExpectations media_expectations;
4006 media_expectations.ExpectBidirectionalAudio();
4007 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004008}
4009
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004010// Test that after closing PeerConnections, they stop sending any packets (ICE,
4011// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004012TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004013 // Set up audio/video/data, wait for some frames to be received.
4014 ASSERT_TRUE(CreatePeerConnectionWrappers());
4015 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004016 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004017#ifdef HAVE_SCTP
4018 caller()->CreateDataChannel();
4019#endif
4020 caller()->CreateAndSetAndSignalOffer();
4021 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004022 MediaExpectations media_expectations;
4023 media_expectations.CalleeExpectsSomeAudioAndVideo();
4024 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004025 // Close PeerConnections.
4026 caller()->pc()->Close();
4027 callee()->pc()->Close();
4028 // Pump messages for a second, and ensure no new packets end up sent.
4029 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4030 WAIT(false, 1000);
4031 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4032 EXPECT_EQ(sent_packets_a, sent_packets_b);
4033}
4034
Seth Hampson2f0d7022018-02-20 11:54:42 -08004035INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4036 PeerConnectionIntegrationTest,
4037 Values(SdpSemantics::kPlanB,
4038 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004039
Steve Anton74255ff2018-01-24 18:32:57 -08004040// Tests that verify interoperability between Plan B and Unified Plan
4041// PeerConnections.
4042class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004043 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004044 public ::testing::WithParamInterface<
4045 std::tuple<SdpSemantics, SdpSemantics>> {
4046 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004047 // Setting the SdpSemantics for the base test to kDefault does not matter
4048 // because we specify not to use the test semantics when creating
4049 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004050 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004051 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4052 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004053 callee_semantics_(std::get<1>(GetParam())) {}
4054
4055 bool CreatePeerConnectionWrappersWithSemantics() {
4056 RTCConfiguration caller_config;
4057 caller_config.sdp_semantics = caller_semantics_;
4058 RTCConfiguration callee_config;
4059 callee_config.sdp_semantics = callee_semantics_;
4060 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4061 }
4062
4063 const SdpSemantics caller_semantics_;
4064 const SdpSemantics callee_semantics_;
4065};
4066
4067TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4068 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4069 ConnectFakeSignaling();
4070
4071 caller()->CreateAndSetAndSignalOffer();
4072 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4073}
4074
4075TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4076 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4077 ConnectFakeSignaling();
4078 auto audio_sender = caller()->AddAudioTrack();
4079
4080 caller()->CreateAndSetAndSignalOffer();
4081 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4082
4083 // Verify that one audio receiver has been created on the remote and that it
4084 // has the same track ID as the sending track.
4085 auto receivers = callee()->pc()->GetReceivers();
4086 ASSERT_EQ(1u, receivers.size());
4087 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4088 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4089
Seth Hampson2f0d7022018-02-20 11:54:42 -08004090 MediaExpectations media_expectations;
4091 media_expectations.CalleeExpectsSomeAudio();
4092 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004093}
4094
4095TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4096 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4097 ConnectFakeSignaling();
4098 auto video_sender = caller()->AddVideoTrack();
4099 auto audio_sender = caller()->AddAudioTrack();
4100
4101 caller()->CreateAndSetAndSignalOffer();
4102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4103
4104 // Verify that one audio and one video receiver have been created on the
4105 // remote and that they have the same track IDs as the sending tracks.
4106 auto audio_receivers =
4107 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4108 ASSERT_EQ(1u, audio_receivers.size());
4109 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4110 auto video_receivers =
4111 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4112 ASSERT_EQ(1u, video_receivers.size());
4113 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4114
Seth Hampson2f0d7022018-02-20 11:54:42 -08004115 MediaExpectations media_expectations;
4116 media_expectations.CalleeExpectsSomeAudioAndVideo();
4117 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004118}
4119
4120TEST_P(PeerConnectionIntegrationInteropTest,
4121 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4122 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4123 ConnectFakeSignaling();
4124 caller()->AddAudioVideoTracks();
4125 callee()->AddAudioVideoTracks();
4126
4127 caller()->CreateAndSetAndSignalOffer();
4128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4129
Seth Hampson2f0d7022018-02-20 11:54:42 -08004130 MediaExpectations media_expectations;
4131 media_expectations.ExpectBidirectionalAudioAndVideo();
4132 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004133}
4134
4135TEST_P(PeerConnectionIntegrationInteropTest,
4136 ReverseRolesOneAudioLocalToOneVideoRemote) {
4137 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4138 ConnectFakeSignaling();
4139 caller()->AddAudioTrack();
4140 callee()->AddVideoTrack();
4141
4142 caller()->CreateAndSetAndSignalOffer();
4143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4144
4145 // Verify that only the audio track has been negotiated.
4146 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4147 // Might also check that the callee's NegotiationNeeded flag is set.
4148
4149 // Reverse roles.
4150 callee()->CreateAndSetAndSignalOffer();
4151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4152
Seth Hampson2f0d7022018-02-20 11:54:42 -08004153 MediaExpectations media_expectations;
4154 media_expectations.CallerExpectsSomeVideo();
4155 media_expectations.CalleeExpectsSomeAudio();
4156 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004157}
4158
4159// Test that if one side offers two video tracks then the other side will only
4160// see the first one and ignore the second.
4161TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToNoMediaRemote) {
4162 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4163 ConnectFakeSignaling();
4164 auto first_sender = caller()->AddVideoTrack();
4165 caller()->AddVideoTrack();
4166
4167 caller()->CreateAndSetAndSignalOffer();
4168 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4169
4170 // Verify that there is only one receiver and it corresponds to the first
4171 // added track.
4172 auto receivers = callee()->pc()->GetReceivers();
4173 ASSERT_EQ(1u, receivers.size());
4174 EXPECT_TRUE(receivers[0]->track()->enabled());
4175 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4176
Seth Hampson2f0d7022018-02-20 11:54:42 -08004177 MediaExpectations media_expectations;
4178 media_expectations.CalleeExpectsSomeVideo();
4179 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004180}
4181
4182// Test that in the multi-track case each endpoint only looks at the first track
4183// and ignores the second one.
4184TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToTwoVideoRemote) {
4185 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4186 ConnectFakeSignaling();
4187 caller()->AddVideoTrack();
4188 caller()->AddVideoTrack();
4189 callee()->AddVideoTrack();
4190 callee()->AddVideoTrack();
4191
4192 caller()->CreateAndSetAndSignalOffer();
4193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4194
4195 PeerConnectionWrapper* plan_b =
4196 (caller_semantics_ == SdpSemantics::kPlanB ? caller() : callee());
4197 PeerConnectionWrapper* unified_plan =
4198 (caller_semantics_ == SdpSemantics::kUnifiedPlan ? caller() : callee());
4199
4200 // Should have two senders each, one for each track.
4201 EXPECT_EQ(2u, plan_b->pc()->GetSenders().size());
4202 EXPECT_EQ(2u, unified_plan->pc()->GetSenders().size());
4203
4204 // Plan B will have one receiver since it only looks at the first video
4205 // section. The receiver should have the same track ID as the sender's first
4206 // track.
4207 ASSERT_EQ(1u, plan_b->pc()->GetReceivers().size());
4208 EXPECT_EQ(unified_plan->pc()->GetSenders()[0]->track()->id(),
4209 plan_b->pc()->GetReceivers()[0]->track()->id());
4210
4211 // Unified Plan will have two receivers since they were created with the
4212 // transceivers when the tracks were added.
4213 ASSERT_EQ(2u, unified_plan->pc()->GetReceivers().size());
4214
4215 if (unified_plan == caller()) {
4216 // If the Unified Plan endpoint was the caller, then the Plan B endpoint
4217 // would have rejected the second video media section so we would expect the
4218 // transceiver to be stopped.
4219 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[0]->stopped());
4220 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[1]->stopped());
4221 } else {
4222 // If the Unified Plan endpoint was the callee, then the Plan B endpoint
4223 // would have offered only one video section so we would expect the first
4224 // transceiver to map to the first track and the second transceiver to be
4225 // missing a mid.
4226 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[0]->mid());
4227 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[1]->mid());
4228 }
4229
4230 // Should be exchanging video frames for the first tracks on each endpoint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004231 MediaExpectations media_expectations;
4232 media_expectations.ExpectBidirectionalVideo();
4233 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004234}
4235
4236INSTANTIATE_TEST_CASE_P(
4237 PeerConnectionIntegrationTest,
4238 PeerConnectionIntegrationInteropTest,
4239 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4240 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4241
deadbeef1dcb1642017-03-29 21:08:16 -07004242} // namespace
4243
4244#endif // if !defined(THREAD_SANITIZER)