blob: 33bf267e18a5859ebe0734590fb126418c66bdf2 [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,
Seth Hampson845e8782018-03-02 11:34:10 -0800316 const std::vector<std::string>& stream_ids = {}) {
317 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800318 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()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001090 network_thread_->SetName("PCNetworkThread", this);
1091 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001092 RTC_CHECK(network_thread_->Start());
1093 RTC_CHECK(worker_thread_->Start());
1094 }
1095
Seth Hampson2f0d7022018-02-20 11:54:42 -08001096 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001097 if (caller_) {
1098 caller_->set_signaling_message_receiver(nullptr);
1099 }
1100 if (callee_) {
1101 callee_->set_signaling_message_receiver(nullptr);
1102 }
1103 }
1104
1105 bool SignalingStateStable() {
1106 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1107 }
1108
deadbeef71452802017-05-07 17:21:01 -07001109 bool DtlsConnected() {
1110 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1111 // are connected. This is an important distinction. Once we have separate
1112 // ICE and DTLS state, this check needs to use the DTLS state.
1113 return (callee()->ice_connection_state() ==
1114 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1115 callee()->ice_connection_state() ==
1116 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1117 (caller()->ice_connection_state() ==
1118 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1119 caller()->ice_connection_state() ==
1120 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1121 }
1122
Seth Hampson2f0d7022018-02-20 11:54:42 -08001123 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1124 const std::string& debug_name,
1125 const MediaConstraintsInterface* constraints,
1126 const PeerConnectionFactory::Options* options,
1127 const RTCConfiguration* config,
1128 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1129 RTCConfiguration modified_config;
1130 if (config) {
1131 modified_config = *config;
1132 }
1133 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1134 modified_config.sdp_semantics = sdp_semantics_;
1135 }
1136 if (!cert_generator) {
1137 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1138 }
1139 std::unique_ptr<PeerConnectionWrapper> client(
1140 new PeerConnectionWrapper(debug_name));
1141 if (!client->Init(constraints, options, &modified_config,
1142 std::move(cert_generator), network_thread_.get(),
1143 worker_thread_.get())) {
1144 return nullptr;
1145 }
1146 return client;
1147 }
1148
deadbeef1dcb1642017-03-29 21:08:16 -07001149 bool CreatePeerConnectionWrappers() {
1150 return CreatePeerConnectionWrappersWithConfig(
1151 PeerConnectionInterface::RTCConfiguration(),
1152 PeerConnectionInterface::RTCConfiguration());
1153 }
1154
1155 bool CreatePeerConnectionWrappersWithConstraints(
1156 MediaConstraintsInterface* caller_constraints,
1157 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001158 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1159 nullptr, nullptr);
1160 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1161 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001162 return caller_ && callee_;
1163 }
1164
1165 bool CreatePeerConnectionWrappersWithConfig(
1166 const PeerConnectionInterface::RTCConfiguration& caller_config,
1167 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001168 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1169 &caller_config, nullptr);
1170 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1171 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001172 return caller_ && callee_;
1173 }
1174
1175 bool CreatePeerConnectionWrappersWithOptions(
1176 const PeerConnectionFactory::Options& caller_options,
1177 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001178 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1179 nullptr, nullptr);
1180 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1181 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001182 return caller_ && callee_;
1183 }
1184
Seth Hampson2f0d7022018-02-20 11:54:42 -08001185 std::unique_ptr<PeerConnectionWrapper>
1186 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001187 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1188 new FakeRTCCertificateGenerator());
1189 cert_generator->use_alternate_key();
1190
Seth Hampson2f0d7022018-02-20 11:54:42 -08001191 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1192 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001193 }
1194
1195 // Once called, SDP blobs and ICE candidates will be automatically signaled
1196 // between PeerConnections.
1197 void ConnectFakeSignaling() {
1198 caller_->set_signaling_message_receiver(callee_.get());
1199 callee_->set_signaling_message_receiver(caller_.get());
1200 }
1201
Steve Antonede9ca52017-10-16 13:04:27 -07001202 // Once called, SDP blobs will be automatically signaled between
1203 // PeerConnections. Note that ICE candidates will not be signaled unless they
1204 // are in the exchanged SDP blobs.
1205 void ConnectFakeSignalingForSdpOnly() {
1206 ConnectFakeSignaling();
1207 SetSignalIceCandidates(false);
1208 }
1209
deadbeef1dcb1642017-03-29 21:08:16 -07001210 void SetSignalingDelayMs(int delay_ms) {
1211 caller_->set_signaling_delay_ms(delay_ms);
1212 callee_->set_signaling_delay_ms(delay_ms);
1213 }
1214
Steve Antonede9ca52017-10-16 13:04:27 -07001215 void SetSignalIceCandidates(bool signal) {
1216 caller_->set_signal_ice_candidates(signal);
1217 callee_->set_signal_ice_candidates(signal);
1218 }
1219
deadbeef1dcb1642017-03-29 21:08:16 -07001220 void EnableVideoDecoderFactory() {
1221 caller_->EnableVideoDecoderFactory();
1222 callee_->EnableVideoDecoderFactory();
1223 }
1224
1225 // Messages may get lost on the unreliable DataChannel, so we send multiple
1226 // times to avoid test flakiness.
1227 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1228 const std::string& data,
1229 int retries) {
1230 for (int i = 0; i < retries; ++i) {
1231 dc->Send(DataBuffer(data));
1232 }
1233 }
1234
1235 rtc::Thread* network_thread() { return network_thread_.get(); }
1236
1237 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1238
1239 PeerConnectionWrapper* caller() { return caller_.get(); }
1240
1241 // Set the |caller_| to the |wrapper| passed in and return the
1242 // original |caller_|.
1243 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1244 PeerConnectionWrapper* wrapper) {
1245 PeerConnectionWrapper* old = caller_.release();
1246 caller_.reset(wrapper);
1247 return old;
1248 }
1249
1250 PeerConnectionWrapper* callee() { return callee_.get(); }
1251
1252 // Set the |callee_| to the |wrapper| passed in and return the
1253 // original |callee_|.
1254 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1255 PeerConnectionWrapper* wrapper) {
1256 PeerConnectionWrapper* old = callee_.release();
1257 callee_.reset(wrapper);
1258 return old;
1259 }
1260
Steve Antonede9ca52017-10-16 13:04:27 -07001261 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1262
Seth Hampson2f0d7022018-02-20 11:54:42 -08001263 // Expects the provided number of new frames to be received within
1264 // kMaxWaitForFramesMs. The new expected frames are specified in
1265 // |media_expectations|. Returns false if any of the expectations were
1266 // not met.
1267 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1268 // First initialize the expected frame counts based upon the current
1269 // frame count.
1270 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1271 if (media_expectations.caller_audio_expectation_ ==
1272 MediaExpectations::kExpectSomeFrames) {
1273 total_caller_audio_frames_expected +=
1274 media_expectations.caller_audio_frames_expected_;
1275 }
1276 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001277 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001278 if (media_expectations.caller_video_expectation_ ==
1279 MediaExpectations::kExpectSomeFrames) {
1280 total_caller_video_frames_expected +=
1281 media_expectations.caller_video_frames_expected_;
1282 }
1283 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1284 if (media_expectations.callee_audio_expectation_ ==
1285 MediaExpectations::kExpectSomeFrames) {
1286 total_callee_audio_frames_expected +=
1287 media_expectations.callee_audio_frames_expected_;
1288 }
1289 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001290 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 if (media_expectations.callee_video_expectation_ ==
1292 MediaExpectations::kExpectSomeFrames) {
1293 total_callee_video_frames_expected +=
1294 media_expectations.callee_video_frames_expected_;
1295 }
deadbeef1dcb1642017-03-29 21:08:16 -07001296
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001298 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001299 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001300 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001301 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001302 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001303 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001304 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001305 total_callee_video_frames_expected,
1306 kMaxWaitForFramesMs);
1307 bool expectations_correct =
1308 caller()->audio_frames_received() >=
1309 total_caller_audio_frames_expected &&
1310 caller()->min_video_frames_received_per_track() >=
1311 total_caller_video_frames_expected &&
1312 callee()->audio_frames_received() >=
1313 total_callee_audio_frames_expected &&
1314 callee()->min_video_frames_received_per_track() >=
1315 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001316
Seth Hampson2f0d7022018-02-20 11:54:42 -08001317 // After the combined wait, print out a more detailed message upon
1318 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001319 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001320 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001321 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001323 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001325 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001326 total_callee_video_frames_expected);
1327
1328 // We want to make sure nothing unexpected was received.
1329 if (media_expectations.caller_audio_expectation_ ==
1330 MediaExpectations::kExpectNoFrames) {
1331 EXPECT_EQ(caller()->audio_frames_received(),
1332 total_caller_audio_frames_expected);
1333 if (caller()->audio_frames_received() !=
1334 total_caller_audio_frames_expected) {
1335 expectations_correct = false;
1336 }
1337 }
1338 if (media_expectations.caller_video_expectation_ ==
1339 MediaExpectations::kExpectNoFrames) {
1340 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1341 total_caller_video_frames_expected);
1342 if (caller()->min_video_frames_received_per_track() !=
1343 total_caller_video_frames_expected) {
1344 expectations_correct = false;
1345 }
1346 }
1347 if (media_expectations.callee_audio_expectation_ ==
1348 MediaExpectations::kExpectNoFrames) {
1349 EXPECT_EQ(callee()->audio_frames_received(),
1350 total_callee_audio_frames_expected);
1351 if (callee()->audio_frames_received() !=
1352 total_callee_audio_frames_expected) {
1353 expectations_correct = false;
1354 }
1355 }
1356 if (media_expectations.callee_video_expectation_ ==
1357 MediaExpectations::kExpectNoFrames) {
1358 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1359 total_callee_video_frames_expected);
1360 if (callee()->min_video_frames_received_per_track() !=
1361 total_callee_video_frames_expected) {
1362 expectations_correct = false;
1363 }
1364 }
1365 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001366 }
1367
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001368 void TestNegotiatedCipherSuite(
1369 const PeerConnectionFactory::Options& caller_options,
1370 const PeerConnectionFactory::Options& callee_options,
1371 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001372 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1373 callee_options));
1374 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1375 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1376 caller()->pc()->RegisterUMAObserver(caller_observer);
1377 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001378 caller()->AddAudioVideoTracks();
1379 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001380 caller()->CreateAndSetAndSignalOffer();
1381 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1382 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001383 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001384 EXPECT_EQ(
1385 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1386 expected_cipher_suite));
1387 caller()->pc()->RegisterUMAObserver(nullptr);
1388 }
1389
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001390 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1391 bool remote_gcm_enabled,
1392 int expected_cipher_suite) {
1393 PeerConnectionFactory::Options caller_options;
1394 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1395 PeerConnectionFactory::Options callee_options;
1396 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1397 TestNegotiatedCipherSuite(caller_options, callee_options,
1398 expected_cipher_suite);
1399 }
1400
Seth Hampson2f0d7022018-02-20 11:54:42 -08001401 protected:
1402 const SdpSemantics sdp_semantics_;
1403
deadbeef1dcb1642017-03-29 21:08:16 -07001404 private:
1405 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001406 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001407 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001408 // |network_thread_| and |worker_thread_| are used by both
1409 // |caller_| and |callee_| so they must be destroyed
1410 // later.
1411 std::unique_ptr<rtc::Thread> network_thread_;
1412 std::unique_ptr<rtc::Thread> worker_thread_;
1413 std::unique_ptr<PeerConnectionWrapper> caller_;
1414 std::unique_ptr<PeerConnectionWrapper> callee_;
1415};
1416
Seth Hampson2f0d7022018-02-20 11:54:42 -08001417class PeerConnectionIntegrationTest
1418 : public PeerConnectionIntegrationBaseTest,
1419 public ::testing::WithParamInterface<SdpSemantics> {
1420 protected:
1421 PeerConnectionIntegrationTest()
1422 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1423};
1424
1425class PeerConnectionIntegrationTestPlanB
1426 : public PeerConnectionIntegrationBaseTest {
1427 protected:
1428 PeerConnectionIntegrationTestPlanB()
1429 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1430};
1431
1432class PeerConnectionIntegrationTestUnifiedPlan
1433 : public PeerConnectionIntegrationBaseTest {
1434 protected:
1435 PeerConnectionIntegrationTestUnifiedPlan()
1436 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1437};
1438
deadbeef1dcb1642017-03-29 21:08:16 -07001439// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1440// includes testing that the callback is invoked if an observer is connected
1441// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001442TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001443 RtpReceiverObserverOnFirstPacketReceived) {
1444 ASSERT_TRUE(CreatePeerConnectionWrappers());
1445 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001446 caller()->AddAudioVideoTracks();
1447 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001448 // Start offer/answer exchange and wait for it to complete.
1449 caller()->CreateAndSetAndSignalOffer();
1450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1451 // Should be one receiver each for audio/video.
1452 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1453 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1454 // Wait for all "first packet received" callbacks to be fired.
1455 EXPECT_TRUE_WAIT(
1456 std::all_of(caller()->rtp_receiver_observers().begin(),
1457 caller()->rtp_receiver_observers().end(),
1458 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1459 return o->first_packet_received();
1460 }),
1461 kMaxWaitForFramesMs);
1462 EXPECT_TRUE_WAIT(
1463 std::all_of(callee()->rtp_receiver_observers().begin(),
1464 callee()->rtp_receiver_observers().end(),
1465 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1466 return o->first_packet_received();
1467 }),
1468 kMaxWaitForFramesMs);
1469 // If new observers are set after the first packet was already received, the
1470 // callback should still be invoked.
1471 caller()->ResetRtpReceiverObservers();
1472 callee()->ResetRtpReceiverObservers();
1473 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1474 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1475 EXPECT_TRUE(
1476 std::all_of(caller()->rtp_receiver_observers().begin(),
1477 caller()->rtp_receiver_observers().end(),
1478 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1479 return o->first_packet_received();
1480 }));
1481 EXPECT_TRUE(
1482 std::all_of(callee()->rtp_receiver_observers().begin(),
1483 callee()->rtp_receiver_observers().end(),
1484 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1485 return o->first_packet_received();
1486 }));
1487}
1488
1489class DummyDtmfObserver : public DtmfSenderObserverInterface {
1490 public:
1491 DummyDtmfObserver() : completed_(false) {}
1492
1493 // Implements DtmfSenderObserverInterface.
1494 void OnToneChange(const std::string& tone) override {
1495 tones_.push_back(tone);
1496 if (tone.empty()) {
1497 completed_ = true;
1498 }
1499 }
1500
1501 const std::vector<std::string>& tones() const { return tones_; }
1502 bool completed() const { return completed_; }
1503
1504 private:
1505 bool completed_;
1506 std::vector<std::string> tones_;
1507};
1508
1509// Assumes |sender| already has an audio track added and the offer/answer
1510// exchange is done.
1511void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1512 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001513 // We should be able to get a DTMF sender from the local sender.
1514 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1515 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1516 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001517 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001518 dtmf_sender->RegisterObserver(&observer);
1519
1520 // Test the DtmfSender object just created.
1521 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1522 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1523
1524 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1525 std::vector<std::string> tones = {"1", "a", ""};
1526 EXPECT_EQ(tones, observer.tones());
1527 dtmf_sender->UnregisterObserver();
1528 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1529}
1530
1531// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1532// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001533TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001534 ASSERT_TRUE(CreatePeerConnectionWrappers());
1535 ConnectFakeSignaling();
1536 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001537 caller()->AddAudioTrack();
1538 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001539 caller()->CreateAndSetAndSignalOffer();
1540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001541 // DTLS must finish before the DTMF sender can be used reliably.
1542 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001543 TestDtmfFromSenderToReceiver(caller(), callee());
1544 TestDtmfFromSenderToReceiver(callee(), caller());
1545}
1546
1547// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1548// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001550 ASSERT_TRUE(CreatePeerConnectionWrappers());
1551 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001552 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1553 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1554 caller()->pc()->RegisterUMAObserver(caller_observer);
1555
deadbeef1dcb1642017-03-29 21:08:16 -07001556 // Do normal offer/answer and wait for some frames to be received in each
1557 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001558 caller()->AddAudioVideoTracks();
1559 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001560 caller()->CreateAndSetAndSignalOffer();
1561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001562 MediaExpectations media_expectations;
1563 media_expectations.ExpectBidirectionalAudioAndVideo();
1564 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001565 EXPECT_LE(
1566 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1567 webrtc::kEnumCounterKeyProtocolDtls));
1568 EXPECT_EQ(
1569 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1570 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001571}
1572
1573// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001574TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001575 PeerConnectionInterface::RTCConfiguration sdes_config;
1576 sdes_config.enable_dtls_srtp.emplace(false);
1577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1578 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001579 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1580 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1581 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001582
1583 // Do normal offer/answer and wait for some frames to be received in each
1584 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001585 caller()->AddAudioVideoTracks();
1586 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001587 caller()->CreateAndSetAndSignalOffer();
1588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001589 MediaExpectations media_expectations;
1590 media_expectations.ExpectBidirectionalAudioAndVideo();
1591 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001592 EXPECT_LE(
1593 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1594 webrtc::kEnumCounterKeyProtocolSdes));
1595 EXPECT_EQ(
1596 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1597 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001598}
1599
Steve Anton8c0f7a72017-10-03 10:03:10 -07001600// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1601// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001602TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001603 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1604 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1605 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1606 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1607 return pc->GetRemoteAudioSSLCertificate();
1608 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001609 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1610 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1611 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1612 return pc->GetRemoteAudioSSLCertChain();
1613 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001614
1615 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1616 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1617
1618 // Configure each side with a known certificate so they can be compared later.
1619 PeerConnectionInterface::RTCConfiguration caller_config;
1620 caller_config.enable_dtls_srtp.emplace(true);
1621 caller_config.certificates.push_back(caller_cert);
1622 PeerConnectionInterface::RTCConfiguration callee_config;
1623 callee_config.enable_dtls_srtp.emplace(true);
1624 callee_config.certificates.push_back(callee_cert);
1625 ASSERT_TRUE(
1626 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1627 ConnectFakeSignaling();
1628
1629 // When first initialized, there should not be a remote SSL certificate (and
1630 // calling this method should not crash).
1631 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1632 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001633 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1634 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001635
Steve Anton15324772018-01-16 10:26:49 -08001636 caller()->AddAudioTrack();
1637 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001638 caller()->CreateAndSetAndSignalOffer();
1639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1640 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1641
1642 // Once DTLS has been connected, each side should return the other's SSL
1643 // certificate when calling GetRemoteAudioSSLCertificate.
1644
1645 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1646 ASSERT_TRUE(caller_remote_cert);
1647 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1648 caller_remote_cert->ToPEMString());
1649
1650 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1651 ASSERT_TRUE(callee_remote_cert);
1652 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1653 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001654
1655 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1656 ASSERT_TRUE(caller_remote_cert_chain);
1657 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1658 auto remote_cert = &caller_remote_cert_chain->Get(0);
1659 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1660 remote_cert->ToPEMString());
1661
1662 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1663 ASSERT_TRUE(callee_remote_cert_chain);
1664 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1665 remote_cert = &callee_remote_cert_chain->Get(0);
1666 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1667 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001668}
1669
deadbeef1dcb1642017-03-29 21:08:16 -07001670// This test sets up a call between two parties (using DTLS) and tests that we
1671// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001672TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001673 ASSERT_TRUE(CreatePeerConnectionWrappers());
1674 ConnectFakeSignaling();
1675
1676 // Add video tracks with 16:9 constraint.
1677 FakeConstraints constraints;
1678 double requested_ratio = 16.0 / 9;
1679 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001680 caller()->AddTrack(
1681 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1682 callee()->AddTrack(
1683 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001684
1685 // Do normal offer/answer and wait for at least one frame to be received in
1686 // each direction.
1687 caller()->CreateAndSetAndSignalOffer();
1688 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1689 callee()->min_video_frames_received_per_track() > 0,
1690 kMaxWaitForFramesMs);
1691
1692 // Check rendered aspect ratio.
1693 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1694 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1695 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1696 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1697}
1698
1699// This test sets up a call between two parties with a source resolution of
1700// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001701TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001702 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1703 ASSERT_TRUE(CreatePeerConnectionWrappers());
1704 ConnectFakeSignaling();
1705
1706 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1707 // instead of aspect ratio constraint.
1708 FakeConstraints constraints;
1709 constraints.SetMandatoryMinWidth(1280);
1710 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001711 caller()->AddTrack(
1712 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1713 callee()->AddTrack(
1714 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001715
1716 // Do normal offer/answer and wait for at least one frame to be received in
1717 // each direction.
1718 caller()->CreateAndSetAndSignalOffer();
1719 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1720 callee()->min_video_frames_received_per_track() > 0,
1721 kMaxWaitForFramesMs);
1722
1723 // Check rendered aspect ratio.
1724 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1725 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1726 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1727 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1728}
1729
1730// This test sets up an one-way call, with media only from caller to
1731// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001732TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001733 ASSERT_TRUE(CreatePeerConnectionWrappers());
1734 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001735 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001736 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001737 MediaExpectations media_expectations;
1738 media_expectations.CalleeExpectsSomeAudioAndVideo();
1739 media_expectations.CallerExpectsNoAudio();
1740 media_expectations.CallerExpectsNoVideo();
1741 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001742}
1743
1744// This test sets up a audio call initially, with the callee rejecting video
1745// initially. Then later the callee decides to upgrade to audio/video, and
1746// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001747TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001748 ASSERT_TRUE(CreatePeerConnectionWrappers());
1749 ConnectFakeSignaling();
1750 // Initially, offer an audio/video stream from the caller, but refuse to
1751 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001752 caller()->AddAudioVideoTracks();
1753 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001754 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1755 PeerConnectionInterface::RTCOfferAnswerOptions options;
1756 options.offer_to_receive_video = 0;
1757 callee()->SetOfferAnswerOptions(options);
1758 } else {
1759 callee()->SetRemoteOfferHandler([this] {
1760 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1761 });
1762 }
deadbeef1dcb1642017-03-29 21:08:16 -07001763 // Do offer/answer and make sure audio is still received end-to-end.
1764 caller()->CreateAndSetAndSignalOffer();
1765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001766 {
1767 MediaExpectations media_expectations;
1768 media_expectations.ExpectBidirectionalAudio();
1769 media_expectations.ExpectNoVideo();
1770 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1771 }
deadbeef1dcb1642017-03-29 21:08:16 -07001772 // Sanity check that the callee's description has a rejected video section.
1773 ASSERT_NE(nullptr, callee()->pc()->local_description());
1774 const ContentInfo* callee_video_content =
1775 GetFirstVideoContent(callee()->pc()->local_description()->description());
1776 ASSERT_NE(nullptr, callee_video_content);
1777 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778
deadbeef1dcb1642017-03-29 21:08:16 -07001779 // Now negotiate with video and ensure negotiation succeeds, with video
1780 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001781 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001782 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1783 PeerConnectionInterface::RTCOfferAnswerOptions options;
1784 options.offer_to_receive_video = 1;
1785 callee()->SetOfferAnswerOptions(options);
1786 } else {
1787 callee()->SetRemoteOfferHandler(nullptr);
1788 caller()->SetRemoteOfferHandler([this] {
1789 // The caller creates a new transceiver to receive video on when receiving
1790 // the offer, but by default it is send only.
1791 auto transceivers = caller()->pc()->GetTransceivers();
1792 ASSERT_EQ(3, transceivers.size());
1793 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1794 transceivers[2]->receiver()->media_type());
1795 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1796 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1797 });
1798 }
deadbeef1dcb1642017-03-29 21:08:16 -07001799 callee()->CreateAndSetAndSignalOffer();
1800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001801 {
1802 // Expect additional audio frames to be received after the upgrade.
1803 MediaExpectations media_expectations;
1804 media_expectations.ExpectBidirectionalAudioAndVideo();
1805 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1806 }
deadbeef1dcb1642017-03-29 21:08:16 -07001807}
1808
deadbeef4389b4d2017-09-07 09:07:36 -07001809// Simpler than the above test; just add an audio track to an established
1810// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001811TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001812 ASSERT_TRUE(CreatePeerConnectionWrappers());
1813 ConnectFakeSignaling();
1814 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001815 caller()->AddVideoTrack();
1816 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001817 caller()->CreateAndSetAndSignalOffer();
1818 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1819 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001820 caller()->AddAudioTrack();
1821 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001822 caller()->CreateAndSetAndSignalOffer();
1823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1824 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001825 MediaExpectations media_expectations;
1826 media_expectations.ExpectBidirectionalAudioAndVideo();
1827 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001828}
1829
deadbeef1dcb1642017-03-29 21:08:16 -07001830// This test sets up a call that's transferred to a new caller with a different
1831// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001832TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001833 ASSERT_TRUE(CreatePeerConnectionWrappers());
1834 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001835 caller()->AddAudioVideoTracks();
1836 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001837 caller()->CreateAndSetAndSignalOffer();
1838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1839
1840 // Keep the original peer around which will still send packets to the
1841 // receiving client. These SRTP packets will be dropped.
1842 std::unique_ptr<PeerConnectionWrapper> original_peer(
1843 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001844 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001845 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1846 // directly above.
1847 original_peer->pc()->Close();
1848
1849 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001850 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001851 caller()->CreateAndSetAndSignalOffer();
1852 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1853 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001854 MediaExpectations media_expectations;
1855 media_expectations.ExpectBidirectionalAudioAndVideo();
1856 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001857}
1858
1859// This test sets up a call that's transferred to a new callee with a different
1860// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001861TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001862 ASSERT_TRUE(CreatePeerConnectionWrappers());
1863 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001864 caller()->AddAudioVideoTracks();
1865 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001866 caller()->CreateAndSetAndSignalOffer();
1867 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1868
1869 // Keep the original peer around which will still send packets to the
1870 // receiving client. These SRTP packets will be dropped.
1871 std::unique_ptr<PeerConnectionWrapper> original_peer(
1872 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001873 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001874 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1875 // directly above.
1876 original_peer->pc()->Close();
1877
1878 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001879 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001880 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1881 caller()->CreateAndSetAndSignalOffer();
1882 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1883 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001884 MediaExpectations media_expectations;
1885 media_expectations.ExpectBidirectionalAudioAndVideo();
1886 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001887}
1888
1889// This test sets up a non-bundled call and negotiates bundling at the same
1890// time as starting an ICE restart. When bundling is in effect in the restart,
1891// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001892TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001893 ASSERT_TRUE(CreatePeerConnectionWrappers());
1894 ConnectFakeSignaling();
1895
Steve Anton15324772018-01-16 10:26:49 -08001896 caller()->AddAudioVideoTracks();
1897 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001898 // Remove the bundle group from the SDP received by the callee.
1899 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1900 desc->RemoveGroupByName("BUNDLE");
1901 });
1902 caller()->CreateAndSetAndSignalOffer();
1903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001904 {
1905 MediaExpectations media_expectations;
1906 media_expectations.ExpectBidirectionalAudioAndVideo();
1907 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1908 }
deadbeef1dcb1642017-03-29 21:08:16 -07001909 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1910 callee()->SetReceivedSdpMunger(nullptr);
1911 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1912 caller()->CreateAndSetAndSignalOffer();
1913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1914
1915 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001916 {
1917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1920 }
deadbeef1dcb1642017-03-29 21:08:16 -07001921}
1922
1923// Test CVO (Coordination of Video Orientation). If a video source is rotated
1924// and both peers support the CVO RTP header extension, the actual video frames
1925// don't need to be encoded in different resolutions, since the rotation is
1926// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001927TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001928 ASSERT_TRUE(CreatePeerConnectionWrappers());
1929 ConnectFakeSignaling();
1930 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001931 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001932 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001933 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001934 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1935
1936 // Wait for video frames to be received by both sides.
1937 caller()->CreateAndSetAndSignalOffer();
1938 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1939 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1940 callee()->min_video_frames_received_per_track() > 0,
1941 kMaxWaitForFramesMs);
1942
1943 // Ensure that the aspect ratio is unmodified.
1944 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1945 // not just assumed.
1946 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1947 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1948 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1949 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1950 // Ensure that the CVO bits were surfaced to the renderer.
1951 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1952 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1953}
1954
1955// Test that when the CVO extension isn't supported, video is rotated the
1956// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001957TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001958 ASSERT_TRUE(CreatePeerConnectionWrappers());
1959 ConnectFakeSignaling();
1960 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001961 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001962 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001963 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001964 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1965
1966 // Remove the CVO extension from the offered SDP.
1967 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1968 cricket::VideoContentDescription* video =
1969 GetFirstVideoContentDescription(desc);
1970 video->ClearRtpHeaderExtensions();
1971 });
1972 // Wait for video frames to be received by both sides.
1973 caller()->CreateAndSetAndSignalOffer();
1974 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1975 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1976 callee()->min_video_frames_received_per_track() > 0,
1977 kMaxWaitForFramesMs);
1978
1979 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1980 // rotation.
1981 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1982 // not just assumed.
1983 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1984 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1985 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1986 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1987 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1988 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1989 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1990}
1991
deadbeef1dcb1642017-03-29 21:08:16 -07001992// Test that if the answerer rejects the audio m= section, no audio is sent or
1993// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001994TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001995 ASSERT_TRUE(CreatePeerConnectionWrappers());
1996 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001997 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1999 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2000 // it will reject the audio m= section completely.
2001 PeerConnectionInterface::RTCOfferAnswerOptions options;
2002 options.offer_to_receive_audio = 0;
2003 callee()->SetOfferAnswerOptions(options);
2004 } else {
2005 // Stopping the audio RtpTransceiver will cause the media section to be
2006 // rejected in the answer.
2007 callee()->SetRemoteOfferHandler([this] {
2008 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2009 });
2010 }
Steve Anton15324772018-01-16 10:26:49 -08002011 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002012 // Do offer/answer and wait for successful end-to-end video frames.
2013 caller()->CreateAndSetAndSignalOffer();
2014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002015 MediaExpectations media_expectations;
2016 media_expectations.ExpectBidirectionalVideo();
2017 media_expectations.ExpectNoAudio();
2018 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2019
deadbeef1dcb1642017-03-29 21:08:16 -07002020 // Sanity check that the callee's description has a rejected audio section.
2021 ASSERT_NE(nullptr, callee()->pc()->local_description());
2022 const ContentInfo* callee_audio_content =
2023 GetFirstAudioContent(callee()->pc()->local_description()->description());
2024 ASSERT_NE(nullptr, callee_audio_content);
2025 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002026 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2027 // The caller's transceiver should have stopped after receiving the answer.
2028 EXPECT_TRUE(caller()
2029 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2030 ->stopped());
2031 }
deadbeef1dcb1642017-03-29 21:08:16 -07002032}
2033
2034// Test that if the answerer rejects the video m= section, no video is sent or
2035// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002036TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002037 ASSERT_TRUE(CreatePeerConnectionWrappers());
2038 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002039 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002040 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2041 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2042 // it will reject the video m= section completely.
2043 PeerConnectionInterface::RTCOfferAnswerOptions options;
2044 options.offer_to_receive_video = 0;
2045 callee()->SetOfferAnswerOptions(options);
2046 } else {
2047 // Stopping the video RtpTransceiver will cause the media section to be
2048 // rejected in the answer.
2049 callee()->SetRemoteOfferHandler([this] {
2050 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2051 });
2052 }
Steve Anton15324772018-01-16 10:26:49 -08002053 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002054 // Do offer/answer and wait for successful end-to-end audio frames.
2055 caller()->CreateAndSetAndSignalOffer();
2056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002057 MediaExpectations media_expectations;
2058 media_expectations.ExpectBidirectionalAudio();
2059 media_expectations.ExpectNoVideo();
2060 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2061
deadbeef1dcb1642017-03-29 21:08:16 -07002062 // Sanity check that the callee's description has a rejected video section.
2063 ASSERT_NE(nullptr, callee()->pc()->local_description());
2064 const ContentInfo* callee_video_content =
2065 GetFirstVideoContent(callee()->pc()->local_description()->description());
2066 ASSERT_NE(nullptr, callee_video_content);
2067 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002068 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2069 // The caller's transceiver should have stopped after receiving the answer.
2070 EXPECT_TRUE(caller()
2071 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2072 ->stopped());
2073 }
deadbeef1dcb1642017-03-29 21:08:16 -07002074}
2075
2076// Test that if the answerer rejects both audio and video m= sections, nothing
2077// bad happens.
2078// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2079// test anything but the fact that negotiation succeeds, which doesn't mean
2080// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002081TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002082 ASSERT_TRUE(CreatePeerConnectionWrappers());
2083 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002084 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002085 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2086 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2087 // will reject both audio and video m= sections.
2088 PeerConnectionInterface::RTCOfferAnswerOptions options;
2089 options.offer_to_receive_audio = 0;
2090 options.offer_to_receive_video = 0;
2091 callee()->SetOfferAnswerOptions(options);
2092 } else {
2093 callee()->SetRemoteOfferHandler([this] {
2094 // Stopping all transceivers will cause all media sections to be rejected.
2095 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2096 transceiver->Stop();
2097 }
2098 });
2099 }
deadbeef1dcb1642017-03-29 21:08:16 -07002100 // Do offer/answer and wait for stable signaling state.
2101 caller()->CreateAndSetAndSignalOffer();
2102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002103
deadbeef1dcb1642017-03-29 21:08:16 -07002104 // Sanity check that the callee's description has rejected m= sections.
2105 ASSERT_NE(nullptr, callee()->pc()->local_description());
2106 const ContentInfo* callee_audio_content =
2107 GetFirstAudioContent(callee()->pc()->local_description()->description());
2108 ASSERT_NE(nullptr, callee_audio_content);
2109 EXPECT_TRUE(callee_audio_content->rejected);
2110 const ContentInfo* callee_video_content =
2111 GetFirstVideoContent(callee()->pc()->local_description()->description());
2112 ASSERT_NE(nullptr, callee_video_content);
2113 EXPECT_TRUE(callee_video_content->rejected);
2114}
2115
2116// This test sets up an audio and video call between two parties. After the
2117// call runs for a while, the caller sends an updated offer with video being
2118// rejected. Once the re-negotiation is done, the video flow should stop and
2119// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002120TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002121 ASSERT_TRUE(CreatePeerConnectionWrappers());
2122 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002123 caller()->AddAudioVideoTracks();
2124 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002125 caller()->CreateAndSetAndSignalOffer();
2126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002127 {
2128 MediaExpectations media_expectations;
2129 media_expectations.ExpectBidirectionalAudioAndVideo();
2130 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2131 }
deadbeef1dcb1642017-03-29 21:08:16 -07002132 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002133 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2134 caller()->SetGeneratedSdpMunger(
2135 [](cricket::SessionDescription* description) {
2136 for (cricket::ContentInfo& content : description->contents()) {
2137 if (cricket::IsVideoContent(&content)) {
2138 content.rejected = true;
2139 }
2140 }
2141 });
2142 } else {
2143 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2144 }
deadbeef1dcb1642017-03-29 21:08:16 -07002145 caller()->CreateAndSetAndSignalOffer();
2146 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2147
2148 // Sanity check that the caller's description has a rejected video section.
2149 ASSERT_NE(nullptr, caller()->pc()->local_description());
2150 const ContentInfo* caller_video_content =
2151 GetFirstVideoContent(caller()->pc()->local_description()->description());
2152 ASSERT_NE(nullptr, caller_video_content);
2153 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002154 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002155 {
2156 MediaExpectations media_expectations;
2157 media_expectations.ExpectBidirectionalAudio();
2158 media_expectations.ExpectNoVideo();
2159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2160 }
deadbeef1dcb1642017-03-29 21:08:16 -07002161}
2162
2163// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2164// is needed to support legacy endpoints.
2165// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2166// add a test for an end-to-end test without MID signaling either (basically,
2167// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002168TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002169 ASSERT_TRUE(CreatePeerConnectionWrappers());
2170 ConnectFakeSignaling();
2171 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002172 caller()->AddAudioVideoTracks();
2173 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002174 // Remove SSRCs and MSIDs from the received offer SDP.
2175 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002176 caller()->CreateAndSetAndSignalOffer();
2177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002178 MediaExpectations media_expectations;
2179 media_expectations.ExpectBidirectionalAudioAndVideo();
2180 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002181}
2182
2183// Test that if two video tracks are sent (from caller to callee, in this test),
2184// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002185TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002186 ASSERT_TRUE(CreatePeerConnectionWrappers());
2187 ConnectFakeSignaling();
2188 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002189 caller()->AddAudioVideoTracks();
2190 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002191 caller()->CreateAndSetAndSignalOffer();
2192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002193 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002194
2195 MediaExpectations media_expectations;
2196 media_expectations.CalleeExpectsSomeAudioAndVideo();
2197 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002198}
2199
2200static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2201 bool first = true;
2202 for (cricket::ContentInfo& content : desc->contents()) {
2203 if (first) {
2204 first = false;
2205 continue;
2206 }
2207 content.bundle_only = true;
2208 }
2209 first = true;
2210 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2211 if (first) {
2212 first = false;
2213 continue;
2214 }
2215 transport.description.ice_ufrag.clear();
2216 transport.description.ice_pwd.clear();
2217 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2218 transport.description.identity_fingerprint.reset(nullptr);
2219 }
2220}
2221
2222// Test that if applying a true "max bundle" offer, which uses ports of 0,
2223// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2224// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2225// successfully and media flows.
2226// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2227// TODO(deadbeef): Won't need this test once we start generating actual
2228// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002229TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002230 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2231 ASSERT_TRUE(CreatePeerConnectionWrappers());
2232 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002233 caller()->AddAudioVideoTracks();
2234 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002235 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2236 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2237 // but the first m= section.
2238 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2239 caller()->CreateAndSetAndSignalOffer();
2240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002241 MediaExpectations media_expectations;
2242 media_expectations.ExpectBidirectionalAudioAndVideo();
2243 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002244}
2245
2246// Test that we can receive the audio output level from a remote audio track.
2247// TODO(deadbeef): Use a fake audio source and verify that the output level is
2248// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002250 ASSERT_TRUE(CreatePeerConnectionWrappers());
2251 ConnectFakeSignaling();
2252 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002253 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002254 caller()->CreateAndSetAndSignalOffer();
2255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2256
2257 // Get the audio output level stats. Note that the level is not available
2258 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002259 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002260 kMaxWaitForFramesMs);
2261}
2262
2263// Test that an audio input level is reported.
2264// TODO(deadbeef): Use a fake audio source and verify that the input level is
2265// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002266TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002267 ASSERT_TRUE(CreatePeerConnectionWrappers());
2268 ConnectFakeSignaling();
2269 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002270 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002271 caller()->CreateAndSetAndSignalOffer();
2272 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2273
2274 // Get the audio input level stats. The level should be available very
2275 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002276 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002277 kMaxWaitForStatsMs);
2278}
2279
2280// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002281TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002282 ASSERT_TRUE(CreatePeerConnectionWrappers());
2283 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002284 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002285 // Do offer/answer, wait for the callee to receive some frames.
2286 caller()->CreateAndSetAndSignalOffer();
2287 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002288
2289 MediaExpectations media_expectations;
2290 media_expectations.CalleeExpectsSomeAudioAndVideo();
2291 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002292
2293 // Get a handle to the remote tracks created, so they can be used as GetStats
2294 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002295 for (auto receiver : callee()->pc()->GetReceivers()) {
2296 // We received frames, so we definitely should have nonzero "received bytes"
2297 // stats at this point.
2298 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2299 0);
2300 }
deadbeef1dcb1642017-03-29 21:08:16 -07002301}
2302
2303// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002304TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002305 ASSERT_TRUE(CreatePeerConnectionWrappers());
2306 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002307 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002308 auto audio_track = caller()->CreateLocalAudioTrack();
2309 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002310 caller()->AddTrack(audio_track);
2311 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002312 // Do offer/answer, wait for the callee to receive some frames.
2313 caller()->CreateAndSetAndSignalOffer();
2314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002315 MediaExpectations media_expectations;
2316 media_expectations.CalleeExpectsSomeAudioAndVideo();
2317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002318
2319 // The callee received frames, so we definitely should have nonzero "sent
2320 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002321 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2322 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2323}
2324
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002325// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002326TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002327 ASSERT_TRUE(CreatePeerConnectionWrappers());
2328 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002329 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002330
Steve Anton15324772018-01-16 10:26:49 -08002331 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002332
2333 // Do offer/answer, wait for the callee to receive some frames.
2334 caller()->CreateAndSetAndSignalOffer();
2335 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2336
2337 // Get the remote audio track created on the receiver, so they can be used as
2338 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002339 auto receivers = callee()->pc()->GetReceivers();
2340 ASSERT_EQ(1u, receivers.size());
2341 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002342
2343 // Get the audio output level stats. Note that the level is not available
2344 // until an RTCP packet has been received.
2345 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
2346 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
2347}
2348
deadbeefd8ad7882017-04-18 16:01:17 -07002349// Test that we can get stats (using the new stats implemnetation) for
2350// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2351// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002352TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002353 GetStatsForUnsignaledStreamWithNewStatsApi) {
2354 ASSERT_TRUE(CreatePeerConnectionWrappers());
2355 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002356 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002357 // Remove SSRCs and MSIDs from the received offer SDP.
2358 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2359 caller()->CreateAndSetAndSignalOffer();
2360 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002361 MediaExpectations media_expectations;
2362 media_expectations.CalleeExpectsSomeAudio(1);
2363 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002364
2365 // We received a frame, so we should have nonzero "bytes received" stats for
2366 // the unsignaled stream, if stats are working for it.
2367 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2368 callee()->NewGetStats();
2369 ASSERT_NE(nullptr, report);
2370 auto inbound_stream_stats =
2371 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2372 ASSERT_EQ(1U, inbound_stream_stats.size());
2373 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2374 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002375 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2376}
2377
2378// Test that we can successfully get the media related stats (audio level
2379// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002380TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002381 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2382 ASSERT_TRUE(CreatePeerConnectionWrappers());
2383 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002384 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002385 // Remove SSRCs and MSIDs from the received offer SDP.
2386 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2387 caller()->CreateAndSetAndSignalOffer();
2388 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002389 MediaExpectations media_expectations;
2390 media_expectations.CalleeExpectsSomeAudio(1);
2391 media_expectations.CalleeExpectsSomeVideo(1);
2392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002393
2394 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2395 callee()->NewGetStats();
2396 ASSERT_NE(nullptr, report);
2397
2398 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2399 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2400 ASSERT_GE(audio_index, 0);
2401 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002402}
2403
deadbeef4e2deab2017-09-20 13:56:21 -07002404// Helper for test below.
2405void ModifySsrcs(cricket::SessionDescription* desc) {
2406 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002407 for (cricket::StreamParams& stream :
2408 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002409 for (uint32_t& ssrc : stream.ssrcs) {
2410 ssrc = rtc::CreateRandomId();
2411 }
2412 }
2413 }
2414}
2415
2416// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2417// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2418// This should result in two "RTCInboundRTPStreamStats", but only one
2419// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2420// being reset to 0 once the SSRC change occurs.
2421//
2422// Regression test for this bug:
2423// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2424//
2425// The bug causes the track stats to only represent one of the two streams:
2426// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2427// that the track stat counters would reset to 0 when the new stream is
2428// received, and a 50% chance that they'll stop updating (while
2429// "concealed_samples" continues increasing, due to silence being generated for
2430// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002431TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002432 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002433 ASSERT_TRUE(CreatePeerConnectionWrappers());
2434 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002435 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002436 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2437 // that doesn't signal SSRCs (from the callee's perspective).
2438 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2439 caller()->CreateAndSetAndSignalOffer();
2440 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2441 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002442 {
2443 MediaExpectations media_expectations;
2444 media_expectations.CalleeExpectsSomeAudio(50);
2445 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2446 }
deadbeef4e2deab2017-09-20 13:56:21 -07002447 // Some audio frames were received, so we should have nonzero "samples
2448 // received" for the track.
2449 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2450 callee()->NewGetStats();
2451 ASSERT_NE(nullptr, report);
2452 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2453 ASSERT_EQ(1U, track_stats.size());
2454 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2455 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2456 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2457
2458 // Create a new offer and munge it to cause the caller to use a new SSRC.
2459 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2460 caller()->CreateAndSetAndSignalOffer();
2461 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2462 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2463 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002464 {
2465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudio(25);
2467 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2468 }
deadbeef4e2deab2017-09-20 13:56:21 -07002469
2470 report = callee()->NewGetStats();
2471 ASSERT_NE(nullptr, report);
2472 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2473 ASSERT_EQ(1U, track_stats.size());
2474 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2475 // The "total samples received" stat should only be greater than it was
2476 // before.
2477 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2478 // Right now, the new SSRC will cause the counters to reset to 0.
2479 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2480
2481 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002482 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002483 // good sign that we're seeing stats from the old stream that's no longer
2484 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002485 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002486 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2487 EXPECT_LT(*track_stats[0]->concealed_samples,
2488 *track_stats[0]->total_samples_received *
2489 kAcceptableConcealedSamplesPercentage);
2490
2491 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2492 // sanity check that the SSRC really changed.
2493 // TODO(deadbeef): This isn't working right now, because we're not returning
2494 // *any* stats for the inactive stream. Uncomment when the bug is completely
2495 // fixed.
2496 // auto inbound_stream_stats =
2497 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2498 // ASSERT_EQ(2U, inbound_stream_stats.size());
2499}
2500
deadbeef1dcb1642017-03-29 21:08:16 -07002501// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002502TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002503 PeerConnectionFactory::Options dtls_10_options;
2504 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2505 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2506 dtls_10_options));
2507 ConnectFakeSignaling();
2508 // Do normal offer/answer and wait for some frames to be received in each
2509 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002510 caller()->AddAudioVideoTracks();
2511 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002512 caller()->CreateAndSetAndSignalOffer();
2513 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002514 MediaExpectations media_expectations;
2515 media_expectations.ExpectBidirectionalAudioAndVideo();
2516 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002517}
2518
2519// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002521 PeerConnectionFactory::Options dtls_10_options;
2522 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2523 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2524 dtls_10_options));
2525 ConnectFakeSignaling();
2526 // Register UMA observer before signaling begins.
2527 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2528 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2529 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002530 caller()->AddAudioVideoTracks();
2531 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002532 caller()->CreateAndSetAndSignalOffer();
2533 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2534 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002535 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002536 kDefaultTimeout);
2537 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002538 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002539 EXPECT_EQ(1,
2540 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2541 kDefaultSrtpCryptoSuite));
2542}
2543
2544// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002545TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002546 PeerConnectionFactory::Options dtls_12_options;
2547 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2548 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2549 dtls_12_options));
2550 ConnectFakeSignaling();
2551 // Register UMA observer before signaling begins.
2552 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2553 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2554 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002555 caller()->AddAudioVideoTracks();
2556 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002557 caller()->CreateAndSetAndSignalOffer();
2558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2559 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002560 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002561 kDefaultTimeout);
2562 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002563 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002564 EXPECT_EQ(1,
2565 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2566 kDefaultSrtpCryptoSuite));
2567}
2568
2569// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2570// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002571TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002572 PeerConnectionFactory::Options caller_options;
2573 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2574 PeerConnectionFactory::Options callee_options;
2575 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2576 ASSERT_TRUE(
2577 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2578 ConnectFakeSignaling();
2579 // Do normal offer/answer and wait for some frames to be received in each
2580 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002581 caller()->AddAudioVideoTracks();
2582 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002583 caller()->CreateAndSetAndSignalOffer();
2584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002585 MediaExpectations media_expectations;
2586 media_expectations.ExpectBidirectionalAudioAndVideo();
2587 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002588}
2589
2590// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2591// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002592TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002593 PeerConnectionFactory::Options caller_options;
2594 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2595 PeerConnectionFactory::Options callee_options;
2596 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2597 ASSERT_TRUE(
2598 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2599 ConnectFakeSignaling();
2600 // Do normal offer/answer and wait for some frames to be received in each
2601 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002602 caller()->AddAudioVideoTracks();
2603 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002604 caller()->CreateAndSetAndSignalOffer();
2605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002606 MediaExpectations media_expectations;
2607 media_expectations.ExpectBidirectionalAudioAndVideo();
2608 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002609}
2610
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002611// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2612// works as expected; the cipher should only be used if enabled by both sides.
2613TEST_P(PeerConnectionIntegrationTest,
2614 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2615 PeerConnectionFactory::Options caller_options;
2616 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2617 PeerConnectionFactory::Options callee_options;
2618 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2619 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2620 TestNegotiatedCipherSuite(caller_options, callee_options,
2621 expected_cipher_suite);
2622}
2623
2624TEST_P(PeerConnectionIntegrationTest,
2625 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2626 PeerConnectionFactory::Options caller_options;
2627 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2628 PeerConnectionFactory::Options callee_options;
2629 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2630 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2631 TestNegotiatedCipherSuite(caller_options, callee_options,
2632 expected_cipher_suite);
2633}
2634
2635TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2636 PeerConnectionFactory::Options caller_options;
2637 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2638 PeerConnectionFactory::Options callee_options;
2639 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2640 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2641 TestNegotiatedCipherSuite(caller_options, callee_options,
2642 expected_cipher_suite);
2643}
2644
deadbeef1dcb1642017-03-29 21:08:16 -07002645// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002646TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002647 bool local_gcm_enabled = false;
2648 bool remote_gcm_enabled = false;
2649 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2650 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2651 expected_cipher_suite);
2652}
2653
2654// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002655TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002656 bool local_gcm_enabled = true;
2657 bool remote_gcm_enabled = true;
2658 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2659 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2660 expected_cipher_suite);
2661}
2662
2663// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002664TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002665 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2666 bool local_gcm_enabled = true;
2667 bool remote_gcm_enabled = false;
2668 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2669 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2670 expected_cipher_suite);
2671}
2672
2673// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002674TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002675 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2676 bool local_gcm_enabled = false;
2677 bool remote_gcm_enabled = true;
2678 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2679 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2680 expected_cipher_suite);
2681}
2682
deadbeef7914b8c2017-04-21 03:23:33 -07002683// Verify that media can be transmitted end-to-end when GCM crypto suites are
2684// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2685// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2686// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002687TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002688 PeerConnectionFactory::Options gcm_options;
2689 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2690 ASSERT_TRUE(
2691 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2692 ConnectFakeSignaling();
2693 // Do normal offer/answer and wait for some frames to be received in each
2694 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002695 caller()->AddAudioVideoTracks();
2696 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002697 caller()->CreateAndSetAndSignalOffer();
2698 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002699 MediaExpectations media_expectations;
2700 media_expectations.ExpectBidirectionalAudioAndVideo();
2701 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002702}
2703
deadbeef1dcb1642017-03-29 21:08:16 -07002704// This test sets up a call between two parties with audio, video and an RTP
2705// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002706TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002707 FakeConstraints setup_constraints;
2708 setup_constraints.SetAllowRtpDataChannels();
2709 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2710 &setup_constraints));
2711 ConnectFakeSignaling();
2712 // Expect that data channel created on caller side will show up for callee as
2713 // well.
2714 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002715 caller()->AddAudioVideoTracks();
2716 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002717 caller()->CreateAndSetAndSignalOffer();
2718 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2719 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002720 MediaExpectations media_expectations;
2721 media_expectations.ExpectBidirectionalAudioAndVideo();
2722 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002723 ASSERT_NE(nullptr, caller()->data_channel());
2724 ASSERT_NE(nullptr, callee()->data_channel());
2725 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2726 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2727
2728 // Ensure data can be sent in both directions.
2729 std::string data = "hello world";
2730 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2731 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2732 kDefaultTimeout);
2733 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2734 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2735 kDefaultTimeout);
2736}
2737
2738// Ensure that an RTP data channel is signaled as closed for the caller when
2739// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002740TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002741 RtpDataChannelSignaledClosedInCalleeOffer) {
2742 // Same procedure as above test.
2743 FakeConstraints setup_constraints;
2744 setup_constraints.SetAllowRtpDataChannels();
2745 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2746 &setup_constraints));
2747 ConnectFakeSignaling();
2748 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002749 caller()->AddAudioVideoTracks();
2750 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002751 caller()->CreateAndSetAndSignalOffer();
2752 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2753 ASSERT_NE(nullptr, caller()->data_channel());
2754 ASSERT_NE(nullptr, callee()->data_channel());
2755 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2756 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2757
2758 // Close the data channel on the callee, and do an updated offer/answer.
2759 callee()->data_channel()->Close();
2760 callee()->CreateAndSetAndSignalOffer();
2761 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2762 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2763 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2764}
2765
2766// Tests that data is buffered in an RTP data channel until an observer is
2767// registered for it.
2768//
2769// NOTE: RTP data channels can receive data before the underlying
2770// transport has detected that a channel is writable and thus data can be
2771// received before the data channel state changes to open. That is hard to test
2772// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002773TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002774 DataBufferedUntilRtpDataChannelObserverRegistered) {
2775 // Use fake clock and simulated network delay so that we predictably can wait
2776 // until an SCTP message has been delivered without "sleep()"ing.
2777 rtc::ScopedFakeClock fake_clock;
2778 // Some things use a time of "0" as a special value, so we need to start out
2779 // the fake clock at a nonzero time.
2780 // TODO(deadbeef): Fix this.
2781 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2782 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2783 virtual_socket_server()->UpdateDelayDistribution();
2784
2785 FakeConstraints constraints;
2786 constraints.SetAllowRtpDataChannels();
2787 ASSERT_TRUE(
2788 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2789 ConnectFakeSignaling();
2790 caller()->CreateDataChannel();
2791 caller()->CreateAndSetAndSignalOffer();
2792 ASSERT_TRUE(caller()->data_channel() != nullptr);
2793 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2794 kDefaultTimeout, fake_clock);
2795 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2796 kDefaultTimeout, fake_clock);
2797 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2798 callee()->data_channel()->state(), kDefaultTimeout,
2799 fake_clock);
2800
2801 // Unregister the observer which is normally automatically registered.
2802 callee()->data_channel()->UnregisterObserver();
2803 // Send data and advance fake clock until it should have been received.
2804 std::string data = "hello world";
2805 caller()->data_channel()->Send(DataBuffer(data));
2806 SIMULATED_WAIT(false, 50, fake_clock);
2807
2808 // Attach data channel and expect data to be received immediately. Note that
2809 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2810 // further, but data can be received even if the callback is asynchronous.
2811 MockDataChannelObserver new_observer(callee()->data_channel());
2812 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2813 fake_clock);
2814}
2815
2816// This test sets up a call between two parties with audio, video and but only
2817// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002818TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002819 FakeConstraints setup_constraints_1;
2820 setup_constraints_1.SetAllowRtpDataChannels();
2821 // Must disable DTLS to make negotiation succeed.
2822 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2823 false);
2824 FakeConstraints setup_constraints_2;
2825 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2826 false);
2827 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2828 &setup_constraints_1, &setup_constraints_2));
2829 ConnectFakeSignaling();
2830 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002831 caller()->AddAudioVideoTracks();
2832 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002833 caller()->CreateAndSetAndSignalOffer();
2834 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2835 // The caller should still have a data channel, but it should be closed, and
2836 // one should ever have been created for the callee.
2837 EXPECT_TRUE(caller()->data_channel() != nullptr);
2838 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2839 EXPECT_EQ(nullptr, callee()->data_channel());
2840}
2841
2842// This test sets up a call between two parties with audio, and video. When
2843// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002844TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002845 FakeConstraints setup_constraints;
2846 setup_constraints.SetAllowRtpDataChannels();
2847 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2848 &setup_constraints));
2849 ConnectFakeSignaling();
2850 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002851 caller()->AddAudioVideoTracks();
2852 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002853 caller()->CreateAndSetAndSignalOffer();
2854 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2855 // Create data channel and do new offer and answer.
2856 caller()->CreateDataChannel();
2857 caller()->CreateAndSetAndSignalOffer();
2858 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2859 ASSERT_NE(nullptr, caller()->data_channel());
2860 ASSERT_NE(nullptr, callee()->data_channel());
2861 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2862 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2863 // Ensure data can be sent in both directions.
2864 std::string data = "hello world";
2865 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2866 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2867 kDefaultTimeout);
2868 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2869 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2870 kDefaultTimeout);
2871}
2872
2873#ifdef HAVE_SCTP
2874
2875// This test sets up a call between two parties with audio, video and an SCTP
2876// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002877TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002878 ASSERT_TRUE(CreatePeerConnectionWrappers());
2879 ConnectFakeSignaling();
2880 // Expect that data channel created on caller side will show up for callee as
2881 // well.
2882 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002883 caller()->AddAudioVideoTracks();
2884 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002885 caller()->CreateAndSetAndSignalOffer();
2886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2887 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002888 MediaExpectations media_expectations;
2889 media_expectations.ExpectBidirectionalAudioAndVideo();
2890 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002891 // Caller data channel should already exist (it created one). Callee data
2892 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2893 ASSERT_NE(nullptr, caller()->data_channel());
2894 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2895 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2896 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2897
2898 // Ensure data can be sent in both directions.
2899 std::string data = "hello world";
2900 caller()->data_channel()->Send(DataBuffer(data));
2901 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2902 kDefaultTimeout);
2903 callee()->data_channel()->Send(DataBuffer(data));
2904 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2905 kDefaultTimeout);
2906}
2907
2908// Ensure that when the callee closes an SCTP data channel, the closing
2909// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002910TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002911 // Same procedure as above test.
2912 ASSERT_TRUE(CreatePeerConnectionWrappers());
2913 ConnectFakeSignaling();
2914 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002915 caller()->AddAudioVideoTracks();
2916 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002917 caller()->CreateAndSetAndSignalOffer();
2918 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2919 ASSERT_NE(nullptr, caller()->data_channel());
2920 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2921 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2922 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2923
2924 // Close the data channel on the callee side, and wait for it to reach the
2925 // "closed" state on both sides.
2926 callee()->data_channel()->Close();
2927 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2928 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2929}
2930
Seth Hampson2f0d7022018-02-20 11:54:42 -08002931TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07002932 ASSERT_TRUE(CreatePeerConnectionWrappers());
2933 ConnectFakeSignaling();
2934 webrtc::DataChannelInit init;
2935 init.id = 53;
2936 init.maxRetransmits = 52;
2937 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002938 caller()->AddAudioVideoTracks();
2939 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002940 caller()->CreateAndSetAndSignalOffer();
2941 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002942 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2943 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002944 EXPECT_EQ(init.id, callee()->data_channel()->id());
2945 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2946 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2947 EXPECT_FALSE(callee()->data_channel()->negotiated());
2948}
2949
deadbeef1dcb1642017-03-29 21:08:16 -07002950// Test usrsctp's ability to process unordered data stream, where data actually
2951// arrives out of order using simulated delays. Previously there have been some
2952// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002953TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002954 // Introduce random network delays.
2955 // Otherwise it's not a true "unordered" test.
2956 virtual_socket_server()->set_delay_mean(20);
2957 virtual_socket_server()->set_delay_stddev(5);
2958 virtual_socket_server()->UpdateDelayDistribution();
2959 // Normal procedure, but with unordered data channel config.
2960 ASSERT_TRUE(CreatePeerConnectionWrappers());
2961 ConnectFakeSignaling();
2962 webrtc::DataChannelInit init;
2963 init.ordered = false;
2964 caller()->CreateDataChannel(&init);
2965 caller()->CreateAndSetAndSignalOffer();
2966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2967 ASSERT_NE(nullptr, caller()->data_channel());
2968 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2969 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2970 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2971
2972 static constexpr int kNumMessages = 100;
2973 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2974 static constexpr size_t kMaxMessageSize = 4096;
2975 // Create and send random messages.
2976 std::vector<std::string> sent_messages;
2977 for (int i = 0; i < kNumMessages; ++i) {
2978 size_t length =
2979 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2980 std::string message;
2981 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2982 caller()->data_channel()->Send(DataBuffer(message));
2983 callee()->data_channel()->Send(DataBuffer(message));
2984 sent_messages.push_back(message);
2985 }
2986
2987 // Wait for all messages to be received.
2988 EXPECT_EQ_WAIT(kNumMessages,
2989 caller()->data_observer()->received_message_count(),
2990 kDefaultTimeout);
2991 EXPECT_EQ_WAIT(kNumMessages,
2992 callee()->data_observer()->received_message_count(),
2993 kDefaultTimeout);
2994
2995 // Sort and compare to make sure none of the messages were corrupted.
2996 std::vector<std::string> caller_received_messages =
2997 caller()->data_observer()->messages();
2998 std::vector<std::string> callee_received_messages =
2999 callee()->data_observer()->messages();
3000 std::sort(sent_messages.begin(), sent_messages.end());
3001 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3002 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3003 EXPECT_EQ(sent_messages, caller_received_messages);
3004 EXPECT_EQ(sent_messages, callee_received_messages);
3005}
3006
3007// This test sets up a call between two parties with audio, and video. When
3008// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003009TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003010 ASSERT_TRUE(CreatePeerConnectionWrappers());
3011 ConnectFakeSignaling();
3012 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003013 caller()->AddAudioVideoTracks();
3014 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003015 caller()->CreateAndSetAndSignalOffer();
3016 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3017 // Create data channel and do new offer and answer.
3018 caller()->CreateDataChannel();
3019 caller()->CreateAndSetAndSignalOffer();
3020 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3021 // Caller data channel should already exist (it created one). Callee data
3022 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3023 ASSERT_NE(nullptr, caller()->data_channel());
3024 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3025 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3026 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3027 // Ensure data can be sent in both directions.
3028 std::string data = "hello world";
3029 caller()->data_channel()->Send(DataBuffer(data));
3030 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3031 kDefaultTimeout);
3032 callee()->data_channel()->Send(DataBuffer(data));
3033 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3034 kDefaultTimeout);
3035}
3036
deadbeef7914b8c2017-04-21 03:23:33 -07003037// Set up a connection initially just using SCTP data channels, later upgrading
3038// to audio/video, ensuring frames are received end-to-end. Effectively the
3039// inverse of the test above.
3040// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003041TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003042 ASSERT_TRUE(CreatePeerConnectionWrappers());
3043 ConnectFakeSignaling();
3044 // Do initial offer/answer with just data channel.
3045 caller()->CreateDataChannel();
3046 caller()->CreateAndSetAndSignalOffer();
3047 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3048 // Wait until data can be sent over the data channel.
3049 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3050 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3051 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3052
3053 // Do subsequent offer/answer with two-way audio and video. Audio and video
3054 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003055 caller()->AddAudioVideoTracks();
3056 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003057 caller()->CreateAndSetAndSignalOffer();
3058 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003059 MediaExpectations media_expectations;
3060 media_expectations.ExpectBidirectionalAudioAndVideo();
3061 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003062}
3063
deadbeef8b7e9ad2017-05-25 09:38:55 -07003064static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003065 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003066 GetFirstDataContentDescription(desc);
3067 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003068 dcd_offer->set_use_sctpmap(false);
3069 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3070}
3071
3072// Test that the data channel works when a spec-compliant SCTP m= section is
3073// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3074// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003075TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003076 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3077 ASSERT_TRUE(CreatePeerConnectionWrappers());
3078 ConnectFakeSignaling();
3079 caller()->CreateDataChannel();
3080 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3081 caller()->CreateAndSetAndSignalOffer();
3082 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3083 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3084 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3085 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3086
3087 // Ensure data can be sent in both directions.
3088 std::string data = "hello world";
3089 caller()->data_channel()->Send(DataBuffer(data));
3090 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3091 kDefaultTimeout);
3092 callee()->data_channel()->Send(DataBuffer(data));
3093 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3094 kDefaultTimeout);
3095}
3096
deadbeef1dcb1642017-03-29 21:08:16 -07003097#endif // HAVE_SCTP
3098
3099// Test that the ICE connection and gathering states eventually reach
3100// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003101TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003102 ASSERT_TRUE(CreatePeerConnectionWrappers());
3103 ConnectFakeSignaling();
3104 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003105 caller()->AddAudioVideoTracks();
3106 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003107 caller()->CreateAndSetAndSignalOffer();
3108 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3109 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3110 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3111 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3112 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3113 // After the best candidate pair is selected and all candidates are signaled,
3114 // the ICE connection state should reach "complete".
3115 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3116 // answerer/"callee" by default) only reaches "connected". When this is
3117 // fixed, this test should be updated.
3118 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3119 caller()->ice_connection_state(), kDefaultTimeout);
3120 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3121 callee()->ice_connection_state(), kDefaultTimeout);
3122}
3123
Steve Antonede9ca52017-10-16 13:04:27 -07003124// Test that firewalling the ICE connection causes the clients to identify the
3125// disconnected state and then removing the firewall causes them to reconnect.
3126class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003127 : public PeerConnectionIntegrationBaseTest,
3128 public ::testing::WithParamInterface<
3129 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003130 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003131 PeerConnectionIntegrationIceStatesTest()
3132 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3133 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003134 }
3135
3136 void StartStunServer(const SocketAddress& server_address) {
3137 stun_server_.reset(
3138 cricket::TestStunServer::Create(network_thread(), server_address));
3139 }
3140
3141 bool TestIPv6() {
3142 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3143 }
3144
3145 void SetPortAllocatorFlags() {
3146 caller()->port_allocator()->set_flags(port_allocator_flags_);
3147 callee()->port_allocator()->set_flags(port_allocator_flags_);
3148 }
3149
3150 std::vector<SocketAddress> CallerAddresses() {
3151 std::vector<SocketAddress> addresses;
3152 addresses.push_back(SocketAddress("1.1.1.1", 0));
3153 if (TestIPv6()) {
3154 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3155 }
3156 return addresses;
3157 }
3158
3159 std::vector<SocketAddress> CalleeAddresses() {
3160 std::vector<SocketAddress> addresses;
3161 addresses.push_back(SocketAddress("2.2.2.2", 0));
3162 if (TestIPv6()) {
3163 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3164 }
3165 return addresses;
3166 }
3167
3168 void SetUpNetworkInterfaces() {
3169 // Remove the default interfaces added by the test infrastructure.
3170 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3171 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3172
3173 // Add network addresses for test.
3174 for (const auto& caller_address : CallerAddresses()) {
3175 caller()->network()->AddInterface(caller_address);
3176 }
3177 for (const auto& callee_address : CalleeAddresses()) {
3178 callee()->network()->AddInterface(callee_address);
3179 }
3180 }
3181
3182 private:
3183 uint32_t port_allocator_flags_;
3184 std::unique_ptr<cricket::TestStunServer> stun_server_;
3185};
3186
3187// Tests that the PeerConnection goes through all the ICE gathering/connection
3188// states over the duration of the call. This includes Disconnected and Failed
3189// states, induced by putting a firewall between the peers and waiting for them
3190// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003191TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3192 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3193 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3194 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003195
3196 const SocketAddress kStunServerAddress =
3197 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3198 StartStunServer(kStunServerAddress);
3199
3200 PeerConnectionInterface::RTCConfiguration config;
3201 PeerConnectionInterface::IceServer ice_stun_server;
3202 ice_stun_server.urls.push_back(
3203 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3204 kStunServerAddress.PortAsString());
3205 config.servers.push_back(ice_stun_server);
3206
3207 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3208 ConnectFakeSignaling();
3209 SetPortAllocatorFlags();
3210 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003211 caller()->AddAudioVideoTracks();
3212 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003213
3214 // Initial state before anything happens.
3215 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3216 caller()->ice_gathering_state());
3217 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3218 caller()->ice_connection_state());
3219
3220 // Start the call by creating the offer, setting it as the local description,
3221 // then sending it to the peer who will respond with an answer. This happens
3222 // asynchronously so that we can watch the states as it runs in the
3223 // background.
3224 caller()->CreateAndSetAndSignalOffer();
3225
Steve Anton83119dd2017-11-10 16:19:52 -08003226 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3227 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003228
3229 // Verify that the observer was notified of the intermediate transitions.
3230 EXPECT_THAT(caller()->ice_connection_state_history(),
3231 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3232 PeerConnectionInterface::kIceConnectionConnected,
3233 PeerConnectionInterface::kIceConnectionCompleted));
3234 EXPECT_THAT(caller()->ice_gathering_state_history(),
3235 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3236 PeerConnectionInterface::kIceGatheringComplete));
3237
3238 // Block connections to/from the caller and wait for ICE to become
3239 // disconnected.
3240 for (const auto& caller_address : CallerAddresses()) {
3241 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3242 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003243 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003244 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3245 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003246
3247 // Let ICE re-establish by removing the firewall rules.
3248 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003249 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003250 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3251 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003252
3253 // According to RFC7675, if there is no response within 30 seconds then the
3254 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003255 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003256 constexpr int kConsentTimeout = 30000;
3257 for (const auto& caller_address : CallerAddresses()) {
3258 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3259 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003260 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003261 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3262 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003263}
3264
3265// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3266// and that the statistics in the metric observers are updated correctly.
3267TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3268 ASSERT_TRUE(CreatePeerConnectionWrappers());
3269 ConnectFakeSignaling();
3270 SetPortAllocatorFlags();
3271 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003272 caller()->AddAudioVideoTracks();
3273 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003274
3275 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3276 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3277 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3278
3279 caller()->CreateAndSetAndSignalOffer();
3280
3281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3282
3283 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3284 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3285 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3286 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3287 if (TestIPv6()) {
3288 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3289 // connection.
3290 EXPECT_EQ(0u, num_best_ipv4);
3291 EXPECT_EQ(1u, num_best_ipv6);
3292 } else {
3293 EXPECT_EQ(1u, num_best_ipv4);
3294 EXPECT_EQ(0u, num_best_ipv6);
3295 }
3296
3297 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3298 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3299 webrtc::kIceCandidatePairHostHost));
3300 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3301 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3302 webrtc::kIceCandidatePairHostPublicHostPublic));
3303}
3304
3305constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3306 cricket::PORTALLOCATOR_DISABLE_STUN |
3307 cricket::PORTALLOCATOR_DISABLE_RELAY;
3308constexpr uint32_t kFlagsIPv6NoStun =
3309 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3310 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3311constexpr uint32_t kFlagsIPv4Stun =
3312 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3313
Seth Hampson2f0d7022018-02-20 11:54:42 -08003314INSTANTIATE_TEST_CASE_P(
3315 PeerConnectionIntegrationTest,
3316 PeerConnectionIntegrationIceStatesTest,
3317 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3318 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3319 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3320 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003321
deadbeef1dcb1642017-03-29 21:08:16 -07003322// This test sets up a call between two parties with audio and video.
3323// During the call, the caller restarts ICE and the test verifies that
3324// new ICE candidates are generated and audio and video still can flow, and the
3325// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003326TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003327 ASSERT_TRUE(CreatePeerConnectionWrappers());
3328 ConnectFakeSignaling();
3329 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003330 caller()->AddAudioVideoTracks();
3331 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003332 caller()->CreateAndSetAndSignalOffer();
3333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3334 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3335 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3336 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3337 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3338
3339 // To verify that the ICE restart actually occurs, get
3340 // ufrag/password/candidates before and after restart.
3341 // Create an SDP string of the first audio candidate for both clients.
3342 const webrtc::IceCandidateCollection* audio_candidates_caller =
3343 caller()->pc()->local_description()->candidates(0);
3344 const webrtc::IceCandidateCollection* audio_candidates_callee =
3345 callee()->pc()->local_description()->candidates(0);
3346 ASSERT_GT(audio_candidates_caller->count(), 0u);
3347 ASSERT_GT(audio_candidates_callee->count(), 0u);
3348 std::string caller_candidate_pre_restart;
3349 ASSERT_TRUE(
3350 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3351 std::string callee_candidate_pre_restart;
3352 ASSERT_TRUE(
3353 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3354 const cricket::SessionDescription* desc =
3355 caller()->pc()->local_description()->description();
3356 std::string caller_ufrag_pre_restart =
3357 desc->transport_infos()[0].description.ice_ufrag;
3358 desc = callee()->pc()->local_description()->description();
3359 std::string callee_ufrag_pre_restart =
3360 desc->transport_infos()[0].description.ice_ufrag;
3361
3362 // Have the caller initiate an ICE restart.
3363 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3364 caller()->CreateAndSetAndSignalOffer();
3365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3366 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3367 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3368 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3369 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3370
3371 // Grab the ufrags/candidates again.
3372 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3373 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3374 ASSERT_GT(audio_candidates_caller->count(), 0u);
3375 ASSERT_GT(audio_candidates_callee->count(), 0u);
3376 std::string caller_candidate_post_restart;
3377 ASSERT_TRUE(
3378 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3379 std::string callee_candidate_post_restart;
3380 ASSERT_TRUE(
3381 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3382 desc = caller()->pc()->local_description()->description();
3383 std::string caller_ufrag_post_restart =
3384 desc->transport_infos()[0].description.ice_ufrag;
3385 desc = callee()->pc()->local_description()->description();
3386 std::string callee_ufrag_post_restart =
3387 desc->transport_infos()[0].description.ice_ufrag;
3388 // Sanity check that an ICE restart was actually negotiated in SDP.
3389 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3390 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3391 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3392 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3393
3394 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003395 MediaExpectations media_expectations;
3396 media_expectations.ExpectBidirectionalAudioAndVideo();
3397 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003398}
3399
3400// Verify that audio/video can be received end-to-end when ICE renomination is
3401// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003402TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003403 PeerConnectionInterface::RTCConfiguration config;
3404 config.enable_ice_renomination = true;
3405 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3406 ConnectFakeSignaling();
3407 // Do normal offer/answer and wait for some frames to be received in each
3408 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003409 caller()->AddAudioVideoTracks();
3410 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003411 caller()->CreateAndSetAndSignalOffer();
3412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3413 // Sanity check that ICE renomination was actually negotiated.
3414 const cricket::SessionDescription* desc =
3415 caller()->pc()->local_description()->description();
3416 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003417 ASSERT_NE(
3418 info.description.transport_options.end(),
3419 std::find(info.description.transport_options.begin(),
3420 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003421 }
3422 desc = callee()->pc()->local_description()->description();
3423 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003424 ASSERT_NE(
3425 info.description.transport_options.end(),
3426 std::find(info.description.transport_options.begin(),
3427 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003428 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003429 MediaExpectations media_expectations;
3430 media_expectations.ExpectBidirectionalAudioAndVideo();
3431 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003432}
3433
Steve Anton6f25b092017-10-23 09:39:20 -07003434// With a max bundle policy and RTCP muxing, adding a new media description to
3435// the connection should not affect ICE at all because the new media will use
3436// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003437TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003438 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003439 PeerConnectionInterface::RTCConfiguration config;
3440 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3441 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3442 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3443 config, PeerConnectionInterface::RTCConfiguration()));
3444 ConnectFakeSignaling();
3445
Steve Anton15324772018-01-16 10:26:49 -08003446 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003447 caller()->CreateAndSetAndSignalOffer();
3448 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003449 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3450 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003451
3452 caller()->clear_ice_connection_state_history();
3453
Steve Anton15324772018-01-16 10:26:49 -08003454 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003455 caller()->CreateAndSetAndSignalOffer();
3456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3457
3458 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3459}
3460
deadbeef1dcb1642017-03-29 21:08:16 -07003461// This test sets up a call between two parties with audio and video. It then
3462// renegotiates setting the video m-line to "port 0", then later renegotiates
3463// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003464TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003465 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3466 ASSERT_TRUE(CreatePeerConnectionWrappers());
3467 ConnectFakeSignaling();
3468
3469 // Do initial negotiation, only sending media from the caller. Will result in
3470 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003471 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003472 caller()->CreateAndSetAndSignalOffer();
3473 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3474
3475 // Negotiate again, disabling the video "m=" section (the callee will set the
3476 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003477 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3478 PeerConnectionInterface::RTCOfferAnswerOptions options;
3479 options.offer_to_receive_video = 0;
3480 callee()->SetOfferAnswerOptions(options);
3481 } else {
3482 callee()->SetRemoteOfferHandler([this] {
3483 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3484 });
3485 }
deadbeef1dcb1642017-03-29 21:08:16 -07003486 caller()->CreateAndSetAndSignalOffer();
3487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3488 // Sanity check that video "m=" section was actually rejected.
3489 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3490 callee()->pc()->local_description()->description());
3491 ASSERT_NE(nullptr, answer_video_content);
3492 ASSERT_TRUE(answer_video_content->rejected);
3493
3494 // Enable video and do negotiation again, making sure video is received
3495 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003496 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3497 PeerConnectionInterface::RTCOfferAnswerOptions options;
3498 options.offer_to_receive_video = 1;
3499 callee()->SetOfferAnswerOptions(options);
3500 } else {
3501 // The caller's transceiver is stopped, so we need to add another track.
3502 auto caller_transceiver =
3503 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3504 EXPECT_TRUE(caller_transceiver->stopped());
3505 caller()->AddVideoTrack();
3506 }
3507 callee()->AddVideoTrack();
3508 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003509 caller()->CreateAndSetAndSignalOffer();
3510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003511
deadbeef1dcb1642017-03-29 21:08:16 -07003512 // Verify the caller receives frames from the newly added stream, and the
3513 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003514 MediaExpectations media_expectations;
3515 media_expectations.CalleeExpectsSomeAudio();
3516 media_expectations.ExpectBidirectionalVideo();
3517 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003518}
3519
3520// This test sets up a Jsep call between two parties with external
3521// VideoDecoderFactory.
3522// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3523// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003524TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003525 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3526 ASSERT_TRUE(CreatePeerConnectionWrappers());
3527 EnableVideoDecoderFactory();
3528 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003529 caller()->AddAudioVideoTracks();
3530 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003531 caller()->CreateAndSetAndSignalOffer();
3532 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003533 MediaExpectations media_expectations;
3534 media_expectations.ExpectBidirectionalAudioAndVideo();
3535 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003536}
3537
3538// This tests that if we negotiate after calling CreateSender but before we
3539// have a track, then set a track later, frames from the newly-set track are
3540// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003541TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003542 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3543 ASSERT_TRUE(CreatePeerConnectionWrappers());
3544 ConnectFakeSignaling();
3545 auto caller_audio_sender =
3546 caller()->pc()->CreateSender("audio", "caller_stream");
3547 auto caller_video_sender =
3548 caller()->pc()->CreateSender("video", "caller_stream");
3549 auto callee_audio_sender =
3550 callee()->pc()->CreateSender("audio", "callee_stream");
3551 auto callee_video_sender =
3552 callee()->pc()->CreateSender("video", "callee_stream");
3553 caller()->CreateAndSetAndSignalOffer();
3554 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3555 // Wait for ICE to complete, without any tracks being set.
3556 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3557 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3558 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3559 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3560 // Now set the tracks, and expect frames to immediately start flowing.
3561 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3562 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3563 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3564 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003565 MediaExpectations media_expectations;
3566 media_expectations.ExpectBidirectionalAudioAndVideo();
3567 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3568}
3569
3570// This tests that if we negotiate after calling AddTransceiver but before we
3571// have a track, then set a track later, frames from the newly-set tracks are
3572// received end-to-end.
3573TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3574 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3575 ASSERT_TRUE(CreatePeerConnectionWrappers());
3576 ConnectFakeSignaling();
3577 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3578 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3579 auto caller_audio_sender = audio_result.MoveValue()->sender();
3580 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3581 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3582 auto caller_video_sender = video_result.MoveValue()->sender();
3583 callee()->SetRemoteOfferHandler([this] {
3584 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3585 callee()->pc()->GetTransceivers()[0]->SetDirection(
3586 RtpTransceiverDirection::kSendRecv);
3587 callee()->pc()->GetTransceivers()[1]->SetDirection(
3588 RtpTransceiverDirection::kSendRecv);
3589 });
3590 caller()->CreateAndSetAndSignalOffer();
3591 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3592 // Wait for ICE to complete, without any tracks being set.
3593 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3594 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3595 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3596 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3597 // Now set the tracks, and expect frames to immediately start flowing.
3598 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3599 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3600 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3601 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3602 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3603 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3604 MediaExpectations media_expectations;
3605 media_expectations.ExpectBidirectionalAudioAndVideo();
3606 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003607}
3608
3609// This test verifies that a remote video track can be added via AddStream,
3610// and sent end-to-end. For this particular test, it's simply echoed back
3611// from the caller to the callee, rather than being forwarded to a third
3612// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003613TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003614 ASSERT_TRUE(CreatePeerConnectionWrappers());
3615 ConnectFakeSignaling();
3616 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003617 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003618 caller()->CreateAndSetAndSignalOffer();
3619 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3620 ASSERT_EQ(1, callee()->remote_streams()->count());
3621
3622 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3623 // time).
3624 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3625 callee()->CreateAndSetAndSignalOffer();
3626 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3627
Seth Hampson2f0d7022018-02-20 11:54:42 -08003628 MediaExpectations media_expectations;
3629 media_expectations.ExpectBidirectionalVideo();
3630 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003631}
3632
3633// Test that we achieve the expected end-to-end connection time, using a
3634// fake clock and simulated latency on the media and signaling paths.
3635// We use a TURN<->TURN connection because this is usually the quickest to
3636// set up initially, especially when we're confident the connection will work
3637// and can start sending media before we get a STUN response.
3638//
3639// With various optimizations enabled, here are the network delays we expect to
3640// be on the critical path:
3641// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3642// signaling answer (with DTLS fingerprint).
3643// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3644// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3645// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003646TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003647 rtc::ScopedFakeClock fake_clock;
3648 // Some things use a time of "0" as a special value, so we need to start out
3649 // the fake clock at a nonzero time.
3650 // TODO(deadbeef): Fix this.
3651 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3652
3653 static constexpr int media_hop_delay_ms = 50;
3654 static constexpr int signaling_trip_delay_ms = 500;
3655 // For explanation of these values, see comment above.
3656 static constexpr int required_media_hops = 9;
3657 static constexpr int required_signaling_trips = 2;
3658 // For internal delays (such as posting an event asychronously).
3659 static constexpr int allowed_internal_delay_ms = 20;
3660 static constexpr int total_connection_time_ms =
3661 media_hop_delay_ms * required_media_hops +
3662 signaling_trip_delay_ms * required_signaling_trips +
3663 allowed_internal_delay_ms;
3664
3665 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3666 3478};
3667 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3668 0};
3669 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3670 3478};
3671 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3672 0};
3673 cricket::TestTurnServer turn_server_1(network_thread(),
3674 turn_server_1_internal_address,
3675 turn_server_1_external_address);
3676 cricket::TestTurnServer turn_server_2(network_thread(),
3677 turn_server_2_internal_address,
3678 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003679
deadbeef1dcb1642017-03-29 21:08:16 -07003680 // Bypass permission check on received packets so media can be sent before
3681 // the candidate is signaled.
3682 turn_server_1.set_enable_permission_checks(false);
3683 turn_server_2.set_enable_permission_checks(false);
3684
3685 PeerConnectionInterface::RTCConfiguration client_1_config;
3686 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3687 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3688 ice_server_1.username = "test";
3689 ice_server_1.password = "test";
3690 client_1_config.servers.push_back(ice_server_1);
3691 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3692 client_1_config.presume_writable_when_fully_relayed = true;
3693
3694 PeerConnectionInterface::RTCConfiguration client_2_config;
3695 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3696 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3697 ice_server_2.username = "test";
3698 ice_server_2.password = "test";
3699 client_2_config.servers.push_back(ice_server_2);
3700 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3701 client_2_config.presume_writable_when_fully_relayed = true;
3702
3703 ASSERT_TRUE(
3704 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3705 // Set up the simulated delays.
3706 SetSignalingDelayMs(signaling_trip_delay_ms);
3707 ConnectFakeSignaling();
3708 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3709 virtual_socket_server()->UpdateDelayDistribution();
3710
3711 // Set "offer to receive audio/video" without adding any tracks, so we just
3712 // set up ICE/DTLS with no media.
3713 PeerConnectionInterface::RTCOfferAnswerOptions options;
3714 options.offer_to_receive_audio = 1;
3715 options.offer_to_receive_video = 1;
3716 caller()->SetOfferAnswerOptions(options);
3717 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003718 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3719 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003720 // Need to free the clients here since they're using things we created on
3721 // the stack.
3722 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3723 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3724}
3725
Jonas Orelandbdcee282017-10-10 14:01:40 +02003726// Verify that a TurnCustomizer passed in through RTCConfiguration
3727// is actually used by the underlying TURN candidate pair.
3728// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003729TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003730 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3731 3478};
3732 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3733 0};
3734 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3735 3478};
3736 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3737 0};
3738 cricket::TestTurnServer turn_server_1(network_thread(),
3739 turn_server_1_internal_address,
3740 turn_server_1_external_address);
3741 cricket::TestTurnServer turn_server_2(network_thread(),
3742 turn_server_2_internal_address,
3743 turn_server_2_external_address);
3744
3745 PeerConnectionInterface::RTCConfiguration client_1_config;
3746 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3747 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3748 ice_server_1.username = "test";
3749 ice_server_1.password = "test";
3750 client_1_config.servers.push_back(ice_server_1);
3751 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3752 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3753 client_1_config.turn_customizer = customizer1.get();
3754
3755 PeerConnectionInterface::RTCConfiguration client_2_config;
3756 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3757 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3758 ice_server_2.username = "test";
3759 ice_server_2.password = "test";
3760 client_2_config.servers.push_back(ice_server_2);
3761 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3762 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3763 client_2_config.turn_customizer = customizer2.get();
3764
3765 ASSERT_TRUE(
3766 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3767 ConnectFakeSignaling();
3768
3769 // Set "offer to receive audio/video" without adding any tracks, so we just
3770 // set up ICE/DTLS with no media.
3771 PeerConnectionInterface::RTCOfferAnswerOptions options;
3772 options.offer_to_receive_audio = 1;
3773 options.offer_to_receive_video = 1;
3774 caller()->SetOfferAnswerOptions(options);
3775 caller()->CreateAndSetAndSignalOffer();
3776 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3777
3778 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3779 EXPECT_GT(customizer1->modify_cnt_, 0u);
3780
3781 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3782 EXPECT_GT(customizer2->modify_cnt_, 0u);
3783
3784 // Need to free the clients here since they're using things we created on
3785 // the stack.
3786 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3787 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3788}
3789
deadbeefc964d0b2017-04-03 10:03:35 -07003790// Test that audio and video flow end-to-end when codec names don't use the
3791// expected casing, given that they're supposed to be case insensitive. To test
3792// this, all but one codec is removed from each media description, and its
3793// casing is changed.
3794//
3795// In the past, this has regressed and caused crashes/black video, due to the
3796// fact that code at some layers was doing case-insensitive comparisons and
3797// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003798TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003799 ASSERT_TRUE(CreatePeerConnectionWrappers());
3800 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003801 caller()->AddAudioVideoTracks();
3802 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003803
3804 // Remove all but one audio/video codec (opus and VP8), and change the
3805 // casing of the caller's generated offer.
3806 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3807 cricket::AudioContentDescription* audio =
3808 GetFirstAudioContentDescription(description);
3809 ASSERT_NE(nullptr, audio);
3810 auto audio_codecs = audio->codecs();
3811 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3812 [](const cricket::AudioCodec& codec) {
3813 return codec.name != "opus";
3814 }),
3815 audio_codecs.end());
3816 ASSERT_EQ(1u, audio_codecs.size());
3817 audio_codecs[0].name = "OpUs";
3818 audio->set_codecs(audio_codecs);
3819
3820 cricket::VideoContentDescription* video =
3821 GetFirstVideoContentDescription(description);
3822 ASSERT_NE(nullptr, video);
3823 auto video_codecs = video->codecs();
3824 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3825 [](const cricket::VideoCodec& codec) {
3826 return codec.name != "VP8";
3827 }),
3828 video_codecs.end());
3829 ASSERT_EQ(1u, video_codecs.size());
3830 video_codecs[0].name = "vP8";
3831 video->set_codecs(video_codecs);
3832 });
3833
3834 caller()->CreateAndSetAndSignalOffer();
3835 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3836
3837 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003838 MediaExpectations media_expectations;
3839 media_expectations.ExpectBidirectionalAudioAndVideo();
3840 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003841}
3842
Seth Hampson2f0d7022018-02-20 11:54:42 -08003843TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003844 ASSERT_TRUE(CreatePeerConnectionWrappers());
3845 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003846 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003847 caller()->CreateAndSetAndSignalOffer();
3848 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003849 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003850 MediaExpectations media_expectations;
3851 media_expectations.CalleeExpectsSomeAudio(1);
3852 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003853 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3854 auto receiver = callee()->pc()->GetReceivers()[0];
3855 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3856
3857 auto contributing_sources = receiver->GetSources();
3858 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3859 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3860 contributing_sources[0].source_id());
3861}
3862
deadbeef2f425aa2017-04-14 10:41:32 -07003863// Test that if a track is removed and added again with a different stream ID,
3864// the new stream ID is successfully communicated in SDP and media continues to
3865// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003866// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3867// it will not reuse a transceiver that has already been sending. After creating
3868// a new transceiver it tries to create an offer with two senders of the same
3869// track ids and it fails.
3870TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003871 ASSERT_TRUE(CreatePeerConnectionWrappers());
3872 ConnectFakeSignaling();
3873
3874 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3875 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3876 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3877 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3878
3879 // Add track using stream 1, do offer/answer.
3880 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3881 caller()->CreateLocalAudioTrack();
3882 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3883 caller()->pc()->AddTrack(track, {stream_1.get()});
3884 caller()->CreateAndSetAndSignalOffer();
3885 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003886 {
3887 MediaExpectations media_expectations;
3888 media_expectations.CalleeExpectsSomeAudio(1);
3889 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3890 }
deadbeef2f425aa2017-04-14 10:41:32 -07003891 // Remove the sender, and create a new one with the new stream.
3892 caller()->pc()->RemoveTrack(sender);
3893 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3894 caller()->CreateAndSetAndSignalOffer();
3895 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3896 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003897 {
3898 MediaExpectations media_expectations;
3899 media_expectations.CalleeExpectsSomeAudio();
3900 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3901 }
deadbeef2f425aa2017-04-14 10:41:32 -07003902}
3903
Seth Hampson2f0d7022018-02-20 11:54:42 -08003904TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003905 ASSERT_TRUE(CreatePeerConnectionWrappers());
3906 ConnectFakeSignaling();
3907
3908 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3909 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3910 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3911 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003912 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3913 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003914
Steve Anton15324772018-01-16 10:26:49 -08003915 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003916 caller()->CreateAndSetAndSignalOffer();
3917 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3918}
3919
Steve Antonede9ca52017-10-16 13:04:27 -07003920// Test that if candidates are only signaled by applying full session
3921// descriptions (instead of using AddIceCandidate), the peers can connect to
3922// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003923TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07003924 ASSERT_TRUE(CreatePeerConnectionWrappers());
3925 // Each side will signal the session descriptions but not candidates.
3926 ConnectFakeSignalingForSdpOnly();
3927
3928 // Add audio video track and exchange the initial offer/answer with media
3929 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003930 caller()->AddAudioVideoTracks();
3931 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003932 caller()->CreateAndSetAndSignalOffer();
3933
3934 // Wait for all candidates to be gathered on both the caller and callee.
3935 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3936 caller()->ice_gathering_state(), kDefaultTimeout);
3937 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3938 callee()->ice_gathering_state(), kDefaultTimeout);
3939
3940 // The candidates will now be included in the session description, so
3941 // signaling them will start the ICE connection.
3942 caller()->CreateAndSetAndSignalOffer();
3943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3944
3945 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003946 MediaExpectations media_expectations;
3947 media_expectations.ExpectBidirectionalAudioAndVideo();
3948 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003949}
3950
henrika5f6bf242017-11-01 11:06:56 +01003951// Test that SetAudioPlayout can be used to disable audio playout from the
3952// start, then later enable it. This may be useful, for example, if the caller
3953// needs to play a local ringtone until some event occurs, after which it
3954// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003955TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003956 ASSERT_TRUE(CreatePeerConnectionWrappers());
3957 ConnectFakeSignaling();
3958
3959 // Set up audio-only call where audio playout is disabled on caller's side.
3960 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003961 caller()->AddAudioTrack();
3962 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003963 caller()->CreateAndSetAndSignalOffer();
3964 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3965
3966 // Pump messages for a second.
3967 WAIT(false, 1000);
3968 // Since audio playout is disabled, the caller shouldn't have received
3969 // anything (at the playout level, at least).
3970 EXPECT_EQ(0, caller()->audio_frames_received());
3971 // As a sanity check, make sure the callee (for which playout isn't disabled)
3972 // did still see frames on its audio level.
3973 ASSERT_GT(callee()->audio_frames_received(), 0);
3974
3975 // Enable playout again, and ensure audio starts flowing.
3976 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003977 MediaExpectations media_expectations;
3978 media_expectations.ExpectBidirectionalAudio();
3979 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003980}
3981
3982double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3983 auto report = pc->NewGetStats();
3984 auto track_stats_list =
3985 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3986 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3987 for (const auto* track_stats : track_stats_list) {
3988 if (track_stats->remote_source.is_defined() &&
3989 *track_stats->remote_source) {
3990 remote_track_stats = track_stats;
3991 break;
3992 }
3993 }
3994
3995 if (!remote_track_stats->total_audio_energy.is_defined()) {
3996 return 0.0;
3997 }
3998 return *remote_track_stats->total_audio_energy;
3999}
4000
4001// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4002// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004003TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004004 DisableAudioPlayoutStillGeneratesAudioStats) {
4005 ASSERT_TRUE(CreatePeerConnectionWrappers());
4006 ConnectFakeSignaling();
4007
4008 // Set up audio-only call where playout is disabled but audio-processing is
4009 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004010 caller()->AddAudioTrack();
4011 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004012 caller()->pc()->SetAudioPlayout(false);
4013
4014 caller()->CreateAndSetAndSignalOffer();
4015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4016
4017 // Wait for the callee to receive audio stats.
4018 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4019}
4020
henrika4f167df2017-11-01 14:45:55 +01004021// Test that SetAudioRecording can be used to disable audio recording from the
4022// start, then later enable it. This may be useful, for example, if the caller
4023// wants to ensure that no audio resources are active before a certain state
4024// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004025TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004026 ASSERT_TRUE(CreatePeerConnectionWrappers());
4027 ConnectFakeSignaling();
4028
4029 // Set up audio-only call where audio recording is disabled on caller's side.
4030 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004031 caller()->AddAudioTrack();
4032 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004033 caller()->CreateAndSetAndSignalOffer();
4034 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4035
4036 // Pump messages for a second.
4037 WAIT(false, 1000);
4038 // Since caller has disabled audio recording, the callee shouldn't have
4039 // received anything.
4040 EXPECT_EQ(0, callee()->audio_frames_received());
4041 // As a sanity check, make sure the caller did still see frames on its
4042 // audio level since audio recording is enabled on the calle side.
4043 ASSERT_GT(caller()->audio_frames_received(), 0);
4044
4045 // Enable audio recording again, and ensure audio starts flowing.
4046 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004047 MediaExpectations media_expectations;
4048 media_expectations.ExpectBidirectionalAudio();
4049 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004050}
4051
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004052// Test that after closing PeerConnections, they stop sending any packets (ICE,
4053// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004054TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004055 // Set up audio/video/data, wait for some frames to be received.
4056 ASSERT_TRUE(CreatePeerConnectionWrappers());
4057 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004058 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004059#ifdef HAVE_SCTP
4060 caller()->CreateDataChannel();
4061#endif
4062 caller()->CreateAndSetAndSignalOffer();
4063 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004064 MediaExpectations media_expectations;
4065 media_expectations.CalleeExpectsSomeAudioAndVideo();
4066 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004067 // Close PeerConnections.
4068 caller()->pc()->Close();
4069 callee()->pc()->Close();
4070 // Pump messages for a second, and ensure no new packets end up sent.
4071 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4072 WAIT(false, 1000);
4073 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4074 EXPECT_EQ(sent_packets_a, sent_packets_b);
4075}
4076
Seth Hampson2f0d7022018-02-20 11:54:42 -08004077INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4078 PeerConnectionIntegrationTest,
4079 Values(SdpSemantics::kPlanB,
4080 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004081
Steve Anton74255ff2018-01-24 18:32:57 -08004082// Tests that verify interoperability between Plan B and Unified Plan
4083// PeerConnections.
4084class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004085 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004086 public ::testing::WithParamInterface<
4087 std::tuple<SdpSemantics, SdpSemantics>> {
4088 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004089 // Setting the SdpSemantics for the base test to kDefault does not matter
4090 // because we specify not to use the test semantics when creating
4091 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004092 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004093 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4094 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004095 callee_semantics_(std::get<1>(GetParam())) {}
4096
4097 bool CreatePeerConnectionWrappersWithSemantics() {
4098 RTCConfiguration caller_config;
4099 caller_config.sdp_semantics = caller_semantics_;
4100 RTCConfiguration callee_config;
4101 callee_config.sdp_semantics = callee_semantics_;
4102 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4103 }
4104
4105 const SdpSemantics caller_semantics_;
4106 const SdpSemantics callee_semantics_;
4107};
4108
4109TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4110 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4111 ConnectFakeSignaling();
4112
4113 caller()->CreateAndSetAndSignalOffer();
4114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4115}
4116
4117TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4118 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4119 ConnectFakeSignaling();
4120 auto audio_sender = caller()->AddAudioTrack();
4121
4122 caller()->CreateAndSetAndSignalOffer();
4123 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4124
4125 // Verify that one audio receiver has been created on the remote and that it
4126 // has the same track ID as the sending track.
4127 auto receivers = callee()->pc()->GetReceivers();
4128 ASSERT_EQ(1u, receivers.size());
4129 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4130 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4131
Seth Hampson2f0d7022018-02-20 11:54:42 -08004132 MediaExpectations media_expectations;
4133 media_expectations.CalleeExpectsSomeAudio();
4134 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004135}
4136
4137TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4138 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4139 ConnectFakeSignaling();
4140 auto video_sender = caller()->AddVideoTrack();
4141 auto audio_sender = caller()->AddAudioTrack();
4142
4143 caller()->CreateAndSetAndSignalOffer();
4144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4145
4146 // Verify that one audio and one video receiver have been created on the
4147 // remote and that they have the same track IDs as the sending tracks.
4148 auto audio_receivers =
4149 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4150 ASSERT_EQ(1u, audio_receivers.size());
4151 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4152 auto video_receivers =
4153 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4154 ASSERT_EQ(1u, video_receivers.size());
4155 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4156
Seth Hampson2f0d7022018-02-20 11:54:42 -08004157 MediaExpectations media_expectations;
4158 media_expectations.CalleeExpectsSomeAudioAndVideo();
4159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004160}
4161
4162TEST_P(PeerConnectionIntegrationInteropTest,
4163 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4164 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4165 ConnectFakeSignaling();
4166 caller()->AddAudioVideoTracks();
4167 callee()->AddAudioVideoTracks();
4168
4169 caller()->CreateAndSetAndSignalOffer();
4170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4171
Seth Hampson2f0d7022018-02-20 11:54:42 -08004172 MediaExpectations media_expectations;
4173 media_expectations.ExpectBidirectionalAudioAndVideo();
4174 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004175}
4176
4177TEST_P(PeerConnectionIntegrationInteropTest,
4178 ReverseRolesOneAudioLocalToOneVideoRemote) {
4179 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4180 ConnectFakeSignaling();
4181 caller()->AddAudioTrack();
4182 callee()->AddVideoTrack();
4183
4184 caller()->CreateAndSetAndSignalOffer();
4185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4186
4187 // Verify that only the audio track has been negotiated.
4188 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4189 // Might also check that the callee's NegotiationNeeded flag is set.
4190
4191 // Reverse roles.
4192 callee()->CreateAndSetAndSignalOffer();
4193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4194
Seth Hampson2f0d7022018-02-20 11:54:42 -08004195 MediaExpectations media_expectations;
4196 media_expectations.CallerExpectsSomeVideo();
4197 media_expectations.CalleeExpectsSomeAudio();
4198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004199}
4200
4201// Test that if one side offers two video tracks then the other side will only
4202// see the first one and ignore the second.
4203TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToNoMediaRemote) {
4204 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4205 ConnectFakeSignaling();
4206 auto first_sender = caller()->AddVideoTrack();
4207 caller()->AddVideoTrack();
4208
4209 caller()->CreateAndSetAndSignalOffer();
4210 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4211
4212 // Verify that there is only one receiver and it corresponds to the first
4213 // added track.
4214 auto receivers = callee()->pc()->GetReceivers();
4215 ASSERT_EQ(1u, receivers.size());
4216 EXPECT_TRUE(receivers[0]->track()->enabled());
4217 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4218
Seth Hampson2f0d7022018-02-20 11:54:42 -08004219 MediaExpectations media_expectations;
4220 media_expectations.CalleeExpectsSomeVideo();
4221 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004222}
4223
4224// Test that in the multi-track case each endpoint only looks at the first track
4225// and ignores the second one.
4226TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToTwoVideoRemote) {
4227 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4228 ConnectFakeSignaling();
4229 caller()->AddVideoTrack();
4230 caller()->AddVideoTrack();
4231 callee()->AddVideoTrack();
4232 callee()->AddVideoTrack();
4233
4234 caller()->CreateAndSetAndSignalOffer();
4235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4236
4237 PeerConnectionWrapper* plan_b =
4238 (caller_semantics_ == SdpSemantics::kPlanB ? caller() : callee());
4239 PeerConnectionWrapper* unified_plan =
4240 (caller_semantics_ == SdpSemantics::kUnifiedPlan ? caller() : callee());
4241
4242 // Should have two senders each, one for each track.
4243 EXPECT_EQ(2u, plan_b->pc()->GetSenders().size());
4244 EXPECT_EQ(2u, unified_plan->pc()->GetSenders().size());
4245
4246 // Plan B will have one receiver since it only looks at the first video
4247 // section. The receiver should have the same track ID as the sender's first
4248 // track.
4249 ASSERT_EQ(1u, plan_b->pc()->GetReceivers().size());
4250 EXPECT_EQ(unified_plan->pc()->GetSenders()[0]->track()->id(),
4251 plan_b->pc()->GetReceivers()[0]->track()->id());
4252
4253 // Unified Plan will have two receivers since they were created with the
4254 // transceivers when the tracks were added.
4255 ASSERT_EQ(2u, unified_plan->pc()->GetReceivers().size());
4256
4257 if (unified_plan == caller()) {
4258 // If the Unified Plan endpoint was the caller, then the Plan B endpoint
4259 // would have rejected the second video media section so we would expect the
4260 // transceiver to be stopped.
4261 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[0]->stopped());
4262 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[1]->stopped());
4263 } else {
4264 // If the Unified Plan endpoint was the callee, then the Plan B endpoint
4265 // would have offered only one video section so we would expect the first
4266 // transceiver to map to the first track and the second transceiver to be
4267 // missing a mid.
4268 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[0]->mid());
4269 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[1]->mid());
4270 }
4271
4272 // Should be exchanging video frames for the first tracks on each endpoint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004273 MediaExpectations media_expectations;
4274 media_expectations.ExpectBidirectionalVideo();
4275 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004276}
4277
4278INSTANTIATE_TEST_CASE_P(
4279 PeerConnectionIntegrationTest,
4280 PeerConnectionIntegrationInteropTest,
4281 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4282 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4283
deadbeef1dcb1642017-03-29 21:08:16 -07004284} // namespace
4285
4286#endif // if !defined(THREAD_SANITIZER)