blob: d5037a82b101dfae9cfc73ea6c56ea2c432ded3c [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070036#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080045#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080046#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "pc/test/fakeaudiocapturemodule.h"
48#include "pc/test/fakeperiodicvideocapturer.h"
49#include "pc/test/fakertccertificategenerator.h"
50#include "pc/test/fakevideotrackrenderer.h"
51#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070053#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020056#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070057
58using cricket::ContentInfo;
59using cricket::FakeWebRtcVideoDecoder;
60using cricket::FakeWebRtcVideoDecoderFactory;
61using cricket::FakeWebRtcVideoEncoder;
62using cricket::FakeWebRtcVideoEncoderFactory;
63using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070064using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080065using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070066using ::testing::ElementsAre;
67using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070068using webrtc::DataBuffer;
69using webrtc::DataChannelInterface;
70using webrtc::DtmfSender;
71using webrtc::DtmfSenderInterface;
72using webrtc::DtmfSenderObserverInterface;
73using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080074using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070075using webrtc::MediaConstraintsInterface;
76using webrtc::MediaStreamInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070083using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070084using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080085using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070086using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070087using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080088using webrtc::RTCErrorType;
Steve Anton74255ff2018-01-24 18:32:57 -080089using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010090using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080091using webrtc::RtpSenderInterface;
92using webrtc::RtpTransceiverDirection;
93using webrtc::RtpTransceiverInit;
94using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080095using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080096using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070097using webrtc::SessionDescriptionInterface;
98using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -080099using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700100
101namespace {
102
103static const int kDefaultTimeout = 10000;
104static const int kMaxWaitForStatsMs = 3000;
105static const int kMaxWaitForActivationMs = 5000;
106static const int kMaxWaitForFramesMs = 10000;
107// Default number of audio/video frames to wait for before considering a test
108// successful.
109static const int kDefaultExpectedAudioFrameCount = 3;
110static const int kDefaultExpectedVideoFrameCount = 3;
111
deadbeef1dcb1642017-03-29 21:08:16 -0700112static const char kDataChannelLabel[] = "data_channel";
113
114// SRTP cipher name negotiated by the tests. This must be updated if the
115// default changes.
Tommi8e545ee2018-02-08 16:25:20 +0000116static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
deadbeef1dcb1642017-03-29 21:08:16 -0700117static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
118
Steve Antonede9ca52017-10-16 13:04:27 -0700119static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
120
deadbeef1dcb1642017-03-29 21:08:16 -0700121// Helper function for constructing offer/answer options to initiate an ICE
122// restart.
123PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
124 PeerConnectionInterface::RTCOfferAnswerOptions options;
125 options.ice_restart = true;
126 return options;
127}
128
deadbeefd8ad7882017-04-18 16:01:17 -0700129// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
130// attribute from received SDP, simulating a legacy endpoint.
131void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
132 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800133 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700134 }
135 desc->set_msid_supported(false);
136}
137
zhihuangf8164932017-05-19 13:09:47 -0700138int FindFirstMediaStatsIndexByKind(
139 const std::string& kind,
140 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
141 media_stats_vec) {
142 for (size_t i = 0; i < media_stats_vec.size(); i++) {
143 if (media_stats_vec[i]->kind.ValueToString() == kind) {
144 return i;
145 }
146 }
147 return -1;
148}
149
deadbeef1dcb1642017-03-29 21:08:16 -0700150class SignalingMessageReceiver {
151 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800152 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700153 virtual void ReceiveIceMessage(const std::string& sdp_mid,
154 int sdp_mline_index,
155 const std::string& msg) = 0;
156
157 protected:
158 SignalingMessageReceiver() {}
159 virtual ~SignalingMessageReceiver() {}
160};
161
162class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
163 public:
164 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
165 : expected_media_type_(media_type) {}
166
167 void OnFirstPacketReceived(cricket::MediaType media_type) override {
168 ASSERT_EQ(expected_media_type_, media_type);
169 first_packet_received_ = true;
170 }
171
172 bool first_packet_received() const { return first_packet_received_; }
173
174 virtual ~MockRtpReceiverObserver() {}
175
176 private:
177 bool first_packet_received_ = false;
178 cricket::MediaType expected_media_type_;
179};
180
181// Helper class that wraps a peer connection, observes it, and can accept
182// signaling messages from another wrapper.
183//
184// Uses a fake network, fake A/V capture, and optionally fake
185// encoders/decoders, though they aren't used by default since they don't
186// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700187// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800188// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700189class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800190 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700191 public:
192 // Different factory methods for convenience.
193 // TODO(deadbeef): Could use the pattern of:
194 //
195 // PeerConnectionWrapper =
196 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
197 //
198 // To reduce some code duplication.
199 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
200 const std::string& debug_name,
201 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
202 rtc::Thread* network_thread,
203 rtc::Thread* worker_thread) {
204 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
205 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
206 network_thread, worker_thread)) {
207 delete client;
208 return nullptr;
209 }
210 return client;
211 }
212
deadbeef2f425aa2017-04-14 10:41:32 -0700213 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
214 return peer_connection_factory_.get();
215 }
216
deadbeef1dcb1642017-03-29 21:08:16 -0700217 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
218
219 // If a signaling message receiver is set (via ConnectFakeSignaling), this
220 // will set the whole offer/answer exchange in motion. Just need to wait for
221 // the signaling state to reach "stable".
222 void CreateAndSetAndSignalOffer() {
223 auto offer = CreateOffer();
224 ASSERT_NE(nullptr, offer);
225 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
226 }
227
228 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
229 // when a remote offer is received (via fake signaling) and an answer is
230 // generated. By default, uses default options.
231 void SetOfferAnswerOptions(
232 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
233 offer_answer_options_ = options;
234 }
235
236 // Set a callback to be invoked when SDP is received via the fake signaling
237 // channel, which provides an opportunity to munge (modify) the SDP. This is
238 // used to test SDP being applied that a PeerConnection would normally not
239 // generate, but a non-JSEP endpoint might.
240 void SetReceivedSdpMunger(
241 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100242 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700243 }
244
deadbeefc964d0b2017-04-03 10:03:35 -0700245 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700246 // generated.
247 void SetGeneratedSdpMunger(
248 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100249 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700250 }
251
Seth Hampson2f0d7022018-02-20 11:54:42 -0800252 // Set a callback to be invoked when a remote offer is received via the fake
253 // signaling channel. This provides an opportunity to change the
254 // PeerConnection state before an answer is created and sent to the caller.
255 void SetRemoteOfferHandler(std::function<void()> handler) {
256 remote_offer_handler_ = std::move(handler);
257 }
258
Steve Antonede9ca52017-10-16 13:04:27 -0700259 // Every ICE connection state in order that has been seen by the observer.
260 std::vector<PeerConnectionInterface::IceConnectionState>
261 ice_connection_state_history() const {
262 return ice_connection_state_history_;
263 }
Steve Anton6f25b092017-10-23 09:39:20 -0700264 void clear_ice_connection_state_history() {
265 ice_connection_state_history_.clear();
266 }
Steve Antonede9ca52017-10-16 13:04:27 -0700267
268 // Every ICE gathering state in order that has been seen by the observer.
269 std::vector<PeerConnectionInterface::IceGatheringState>
270 ice_gathering_state_history() const {
271 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700272 }
273
Steve Anton15324772018-01-16 10:26:49 -0800274 void AddAudioVideoTracks() {
275 AddAudioTrack();
276 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700277 }
278
Steve Anton74255ff2018-01-24 18:32:57 -0800279 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
280 return AddTrack(CreateLocalAudioTrack());
281 }
deadbeef1dcb1642017-03-29 21:08:16 -0700282
Steve Anton74255ff2018-01-24 18:32:57 -0800283 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
284 return AddTrack(CreateLocalVideoTrack());
285 }
deadbeef1dcb1642017-03-29 21:08:16 -0700286
287 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
288 FakeConstraints constraints;
289 // Disable highpass filter so that we can get all the test audio frames.
290 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
291 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
292 peer_connection_factory_->CreateAudioSource(&constraints);
293 // TODO(perkj): Test audio source when it is implemented. Currently audio
294 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700295 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700296 source);
297 }
298
299 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700300 return CreateLocalVideoTrackInternal(FakeConstraints(),
301 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
303
304 rtc::scoped_refptr<webrtc::VideoTrackInterface>
305 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700306 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700307 }
308
309 rtc::scoped_refptr<webrtc::VideoTrackInterface>
310 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700311 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700312 }
313
Steve Anton74255ff2018-01-24 18:32:57 -0800314 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
315 rtc::scoped_refptr<MediaStreamTrackInterface> track,
316 const std::vector<std::string>& stream_labels = {}) {
Steve Anton15324772018-01-16 10:26:49 -0800317 auto result = pc()->AddTrack(track, stream_labels);
318 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800319 return result.MoveValue();
320 }
321
322 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
323 cricket::MediaType media_type) {
324 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
325 for (auto receiver : pc()->GetReceivers()) {
326 if (receiver->media_type() == media_type) {
327 receivers.push_back(receiver);
328 }
329 }
330 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
Seth Hampson2f0d7022018-02-20 11:54:42 -0800333 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
334 cricket::MediaType media_type) {
335 for (auto transceiver : pc()->GetTransceivers()) {
336 if (transceiver->receiver()->media_type() == media_type) {
337 return transceiver;
338 }
339 }
340 return nullptr;
341 }
342
deadbeef1dcb1642017-03-29 21:08:16 -0700343 bool SignalingStateStable() {
344 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
345 }
346
347 void CreateDataChannel() { CreateDataChannel(nullptr); }
348
349 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700350 CreateDataChannel(kDataChannelLabel, init);
351 }
352
353 void CreateDataChannel(const std::string& label,
354 const webrtc::DataChannelInit* init) {
355 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700356 ASSERT_TRUE(data_channel_.get() != nullptr);
357 data_observer_.reset(new MockDataChannelObserver(data_channel_));
358 }
359
360 DataChannelInterface* data_channel() { return data_channel_; }
361 const MockDataChannelObserver* data_observer() const {
362 return data_observer_.get();
363 }
364
365 int audio_frames_received() const {
366 return fake_audio_capture_module_->frames_received();
367 }
368
369 // Takes minimum of video frames received for each track.
370 //
371 // Can be used like:
372 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
373 //
374 // To ensure that all video tracks received at least a certain number of
375 // frames.
376 int min_video_frames_received_per_track() const {
377 int min_frames = INT_MAX;
378 if (video_decoder_factory_enabled_) {
379 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
380 fake_video_decoder_factory_->decoders();
381 if (decoders.empty()) {
382 return 0;
383 }
384 for (FakeWebRtcVideoDecoder* decoder : decoders) {
385 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
386 }
387 return min_frames;
388 } else {
389 if (fake_video_renderers_.empty()) {
390 return 0;
391 }
392
393 for (const auto& pair : fake_video_renderers_) {
394 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
395 }
396 return min_frames;
397 }
398 }
399
400 // In contrast to the above, sums the video frames received for all tracks.
401 // Can be used to verify that no video frames were received, or that the
402 // counts didn't increase.
403 int total_video_frames_received() const {
404 int total = 0;
405 if (video_decoder_factory_enabled_) {
406 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
407 fake_video_decoder_factory_->decoders();
408 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
409 total += decoder->GetNumFramesReceived();
410 }
411 } else {
412 for (const auto& pair : fake_video_renderers_) {
413 total += pair.second->num_rendered_frames();
414 }
415 for (const auto& renderer : removed_fake_video_renderers_) {
416 total += renderer->num_rendered_frames();
417 }
418 }
419 return total;
420 }
421
422 // Returns a MockStatsObserver in a state after stats gathering finished,
423 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700424 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700425 webrtc::MediaStreamTrackInterface* track) {
426 rtc::scoped_refptr<MockStatsObserver> observer(
427 new rtc::RefCountedObject<MockStatsObserver>());
428 EXPECT_TRUE(peer_connection_->GetStats(
429 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
430 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
431 return observer;
432 }
433
434 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
436 return OldGetStatsForTrack(nullptr);
437 }
438
439 // Synchronously gets stats and returns them. If it times out, fails the test
440 // and returns null.
441 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
442 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
443 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
444 peer_connection_->GetStats(callback);
445 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
446 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
448
449 int rendered_width() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->width();
454 }
455
456 int rendered_height() {
457 EXPECT_FALSE(fake_video_renderers_.empty());
458 return fake_video_renderers_.empty()
459 ? 0
460 : fake_video_renderers_.begin()->second->height();
461 }
462
463 double rendered_aspect_ratio() {
464 if (rendered_height() == 0) {
465 return 0.0;
466 }
467 return static_cast<double>(rendered_width()) / rendered_height();
468 }
469
470 webrtc::VideoRotation rendered_rotation() {
471 EXPECT_FALSE(fake_video_renderers_.empty());
472 return fake_video_renderers_.empty()
473 ? webrtc::kVideoRotation_0
474 : fake_video_renderers_.begin()->second->rotation();
475 }
476
477 int local_rendered_width() {
478 return local_video_renderer_ ? local_video_renderer_->width() : 0;
479 }
480
481 int local_rendered_height() {
482 return local_video_renderer_ ? local_video_renderer_->height() : 0;
483 }
484
485 double local_rendered_aspect_ratio() {
486 if (local_rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(local_rendered_width()) /
490 local_rendered_height();
491 }
492
493 size_t number_of_remote_streams() {
494 if (!pc()) {
495 return 0;
496 }
497 return pc()->remote_streams()->count();
498 }
499
500 StreamCollectionInterface* remote_streams() const {
501 if (!pc()) {
502 ADD_FAILURE();
503 return nullptr;
504 }
505 return pc()->remote_streams();
506 }
507
508 StreamCollectionInterface* local_streams() {
509 if (!pc()) {
510 ADD_FAILURE();
511 return nullptr;
512 }
513 return pc()->local_streams();
514 }
515
516 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
517 return pc()->signaling_state();
518 }
519
520 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
521 return pc()->ice_connection_state();
522 }
523
524 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
525 return pc()->ice_gathering_state();
526 }
527
528 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
529 // GetReceivers. They're updated automatically when a remote offer/answer
530 // from the fake signaling channel is applied, or when
531 // ResetRtpReceiverObservers below is called.
532 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
533 rtp_receiver_observers() {
534 return rtp_receiver_observers_;
535 }
536
537 void ResetRtpReceiverObservers() {
538 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100539 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
540 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700541 std::unique_ptr<MockRtpReceiverObserver> observer(
542 new MockRtpReceiverObserver(receiver->media_type()));
543 receiver->SetObserver(observer.get());
544 rtp_receiver_observers_.push_back(std::move(observer));
545 }
546 }
547
Steve Antonede9ca52017-10-16 13:04:27 -0700548 rtc::FakeNetworkManager* network() const {
549 return fake_network_manager_.get();
550 }
551 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
552
deadbeef1dcb1642017-03-29 21:08:16 -0700553 private:
554 explicit PeerConnectionWrapper(const std::string& debug_name)
555 : debug_name_(debug_name) {}
556
557 bool Init(
558 const MediaConstraintsInterface* constraints,
559 const PeerConnectionFactory::Options* options,
560 const PeerConnectionInterface::RTCConfiguration* config,
561 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
562 rtc::Thread* network_thread,
563 rtc::Thread* worker_thread) {
564 // There's an error in this test code if Init ends up being called twice.
565 RTC_DCHECK(!peer_connection_);
566 RTC_DCHECK(!peer_connection_factory_);
567
568 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700569 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700570
571 std::unique_ptr<cricket::PortAllocator> port_allocator(
572 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700573 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700574 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
575 if (!fake_audio_capture_module_) {
576 return false;
577 }
578 // Note that these factories don't end up getting used unless supported
579 // codecs are added to them.
580 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
581 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
582 rtc::Thread* const signaling_thread = rtc::Thread::Current();
583 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
584 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200585 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
586 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700587 fake_video_decoder_factory_);
588 if (!peer_connection_factory_) {
589 return false;
590 }
591 if (options) {
592 peer_connection_factory_->SetOptions(*options);
593 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800594 if (config) {
595 sdp_semantics_ = config->sdp_semantics;
596 }
deadbeef1dcb1642017-03-29 21:08:16 -0700597 peer_connection_ =
598 CreatePeerConnection(std::move(port_allocator), constraints, config,
599 std::move(cert_generator));
600 return peer_connection_.get() != nullptr;
601 }
602
603 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
604 std::unique_ptr<cricket::PortAllocator> port_allocator,
605 const MediaConstraintsInterface* constraints,
606 const PeerConnectionInterface::RTCConfiguration* config,
607 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
608 PeerConnectionInterface::RTCConfiguration modified_config;
609 // If |config| is null, this will result in a default configuration being
610 // used.
611 if (config) {
612 modified_config = *config;
613 }
614 // Disable resolution adaptation; we don't want it interfering with the
615 // test results.
616 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
617 // ratios and not specific resolutions, is this even necessary?
618 modified_config.set_cpu_adaptation(false);
619
620 return peer_connection_factory_->CreatePeerConnection(
621 modified_config, constraints, std::move(port_allocator),
622 std::move(cert_generator), this);
623 }
624
625 void set_signaling_message_receiver(
626 SignalingMessageReceiver* signaling_message_receiver) {
627 signaling_message_receiver_ = signaling_message_receiver;
628 }
629
630 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
631
Steve Antonede9ca52017-10-16 13:04:27 -0700632 void set_signal_ice_candidates(bool signal) {
633 signal_ice_candidates_ = signal;
634 }
635
deadbeef1dcb1642017-03-29 21:08:16 -0700636 void EnableVideoDecoderFactory() {
637 video_decoder_factory_enabled_ = true;
638 fake_video_decoder_factory_->AddSupportedVideoCodecType(
639 webrtc::kVideoCodecVP8);
640 }
641
642 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700643 const FakeConstraints& constraints,
644 webrtc::VideoRotation rotation) {
645 // Set max frame rate to 10fps to reduce the risk of test flakiness.
646 // TODO(deadbeef): Do something more robust.
647 FakeConstraints source_constraints = constraints;
648 source_constraints.SetMandatoryMaxFrameRate(10);
649
650 cricket::FakeVideoCapturer* fake_capturer =
651 new webrtc::FakePeriodicVideoCapturer();
652 fake_capturer->SetRotation(rotation);
653 video_capturers_.push_back(fake_capturer);
654 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
655 peer_connection_factory_->CreateVideoSource(fake_capturer,
656 &source_constraints);
657 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700658 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
659 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700660 if (!local_video_renderer_) {
661 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
662 }
663 return track;
664 }
665
666 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100667 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800668 std::unique_ptr<SessionDescriptionInterface> desc =
669 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700670 if (received_sdp_munger_) {
671 received_sdp_munger_(desc->description());
672 }
673
674 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
675 // Setting a remote description may have changed the number of receivers,
676 // so reset the receiver observers.
677 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800678 if (remote_offer_handler_) {
679 remote_offer_handler_();
680 }
deadbeef1dcb1642017-03-29 21:08:16 -0700681 auto answer = CreateAnswer();
682 ASSERT_NE(nullptr, answer);
683 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
684 }
685
686 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800688 std::unique_ptr<SessionDescriptionInterface> desc =
689 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700690 if (received_sdp_munger_) {
691 received_sdp_munger_(desc->description());
692 }
693
694 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
695 // Set the RtpReceiverObserver after receivers are created.
696 ResetRtpReceiverObservers();
697 }
698
699 // Returns null on failure.
700 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
702 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
703 pc()->CreateOffer(observer, offer_answer_options_);
704 return WaitForDescriptionFromObserver(observer);
705 }
706
707 // Returns null on failure.
708 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
709 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
710 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
711 pc()->CreateAnswer(observer, offer_answer_options_);
712 return WaitForDescriptionFromObserver(observer);
713 }
714
715 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100716 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700717 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
718 if (!observer->result()) {
719 return nullptr;
720 }
721 auto description = observer->MoveDescription();
722 if (generated_sdp_munger_) {
723 generated_sdp_munger_(description->description());
724 }
725 return description;
726 }
727
728 // Setting the local description and sending the SDP message over the fake
729 // signaling channel are combined into the same method because the SDP
730 // message needs to be sent as soon as SetLocalDescription finishes, without
731 // waiting for the observer to be called. This ensures that ICE candidates
732 // don't outrace the description.
733 bool SetLocalDescriptionAndSendSdpMessage(
734 std::unique_ptr<SessionDescriptionInterface> desc) {
735 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
736 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800738 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700739 std::string sdp;
740 EXPECT_TRUE(desc->ToString(&sdp));
741 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800742 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
743 RemoveUnusedVideoRenderers();
744 }
deadbeef1dcb1642017-03-29 21:08:16 -0700745 // As mentioned above, we need to send the message immediately after
746 // SetLocalDescription.
747 SendSdpMessage(type, sdp);
748 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
749 return true;
750 }
751
752 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
753 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
754 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100755 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700756 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800757 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
758 RemoveUnusedVideoRenderers();
759 }
deadbeef1dcb1642017-03-29 21:08:16 -0700760 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
761 return observer->result();
762 }
763
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 // This is a work around to remove unused fake_video_renderers from
765 // transceivers that have either stopped or are no longer receiving.
766 void RemoveUnusedVideoRenderers() {
767 auto transceivers = pc()->GetTransceivers();
768 for (auto& transceiver : transceivers) {
769 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
770 continue;
771 }
772 // Remove fake video renderers from any stopped transceivers.
773 if (transceiver->stopped()) {
774 auto it =
775 fake_video_renderers_.find(transceiver->receiver()->track()->id());
776 if (it != fake_video_renderers_.end()) {
777 fake_video_renderers_.erase(it);
778 }
779 }
780 // Remove fake video renderers from any transceivers that are no longer
781 // receiving.
782 if ((transceiver->current_direction() &&
783 !webrtc::RtpTransceiverDirectionHasRecv(
784 *transceiver->current_direction()))) {
785 auto it =
786 fake_video_renderers_.find(transceiver->receiver()->track()->id());
787 if (it != fake_video_renderers_.end()) {
788 fake_video_renderers_.erase(it);
789 }
790 }
791 }
792 }
793
deadbeef1dcb1642017-03-29 21:08:16 -0700794 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
795 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800796 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700797 if (signaling_delay_ms_ == 0) {
798 RelaySdpMessageIfReceiverExists(type, msg);
799 } else {
800 invoker_.AsyncInvokeDelayed<void>(
801 RTC_FROM_HERE, rtc::Thread::Current(),
802 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
803 this, type, msg),
804 signaling_delay_ms_);
805 }
806 }
807
Steve Antona3a92c22017-12-07 10:27:41 -0800808 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700809 if (signaling_message_receiver_) {
810 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
811 }
812 }
813
814 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
815 // default).
816 void SendIceMessage(const std::string& sdp_mid,
817 int sdp_mline_index,
818 const std::string& msg) {
819 if (signaling_delay_ms_ == 0) {
820 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
821 } else {
822 invoker_.AsyncInvokeDelayed<void>(
823 RTC_FROM_HERE, rtc::Thread::Current(),
824 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
825 this, sdp_mid, sdp_mline_index, msg),
826 signaling_delay_ms_);
827 }
828 }
829
830 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
831 int sdp_mline_index,
832 const std::string& msg) {
833 if (signaling_message_receiver_) {
834 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
835 msg);
836 }
837 }
838
839 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
841 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700842 HandleIncomingOffer(msg);
843 } else {
844 HandleIncomingAnswer(msg);
845 }
846 }
847
848 void ReceiveIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100851 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700852 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
853 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
854 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
855 }
856
857 // PeerConnectionObserver callbacks.
858 void OnSignalingChange(
859 webrtc::PeerConnectionInterface::SignalingState new_state) override {
860 EXPECT_EQ(pc()->signaling_state(), new_state);
861 }
Steve Anton15324772018-01-16 10:26:49 -0800862 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
863 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
864 streams) override {
865 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
866 rtc::scoped_refptr<VideoTrackInterface> video_track(
867 static_cast<VideoTrackInterface*>(receiver->track().get()));
868 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700869 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800870 fake_video_renderers_[video_track->id()] =
871 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700872 }
873 }
Steve Anton15324772018-01-16 10:26:49 -0800874 void OnRemoveTrack(
875 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
876 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
877 auto it = fake_video_renderers_.find(receiver->track()->id());
878 RTC_DCHECK(it != fake_video_renderers_.end());
879 fake_video_renderers_.erase(it);
880 }
881 }
deadbeef1dcb1642017-03-29 21:08:16 -0700882 void OnRenegotiationNeeded() override {}
883 void OnIceConnectionChange(
884 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
885 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700886 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700887 }
888 void OnIceGatheringChange(
889 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700890 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700891 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700892 }
893 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700895
896 std::string ice_sdp;
897 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700898 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700899 // Remote party may be deleted.
900 return;
901 }
902 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
903 }
904 void OnDataChannel(
905 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700907 data_channel_ = data_channel;
908 data_observer_.reset(new MockDataChannelObserver(data_channel));
909 }
910
deadbeef1dcb1642017-03-29 21:08:16 -0700911 std::string debug_name_;
912
913 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
914
915 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
916 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
917 peer_connection_factory_;
918
Steve Antonede9ca52017-10-16 13:04:27 -0700919 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700920 // Needed to keep track of number of frames sent.
921 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
922 // Needed to keep track of number of frames received.
923 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
924 fake_video_renderers_;
925 // Needed to ensure frames aren't received for removed tracks.
926 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
927 removed_fake_video_renderers_;
928 // Needed to keep track of number of frames received when external decoder
929 // used.
930 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
931 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
932 bool video_decoder_factory_enabled_ = false;
933
934 // For remote peer communication.
935 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
936 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700937 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700938
939 // Store references to the video capturers we've created, so that we can stop
940 // them, if required.
941 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
942 // |local_video_renderer_| attached to the first created local video track.
943 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
944
Seth Hampson2f0d7022018-02-20 11:54:42 -0800945 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700946 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
947 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
948 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800949 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700950
951 rtc::scoped_refptr<DataChannelInterface> data_channel_;
952 std::unique_ptr<MockDataChannelObserver> data_observer_;
953
954 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
955
Steve Antonede9ca52017-10-16 13:04:27 -0700956 std::vector<PeerConnectionInterface::IceConnectionState>
957 ice_connection_state_history_;
958 std::vector<PeerConnectionInterface::IceGatheringState>
959 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700960
961 rtc::AsyncInvoker invoker_;
962
Seth Hampson2f0d7022018-02-20 11:54:42 -0800963 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700964};
965
Elad Alon99c3fe52017-10-13 16:29:40 +0200966class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
967 public:
968 virtual ~MockRtcEventLogOutput() = default;
969 MOCK_CONST_METHOD0(IsActive, bool());
970 MOCK_METHOD1(Write, bool(const std::string&));
971};
972
Seth Hampson2f0d7022018-02-20 11:54:42 -0800973// This helper object is used for both specifying how many audio/video frames
974// are expected to be received for a caller/callee. It provides helper functions
975// to specify these expectations. The object initially starts in a state of no
976// expectations.
977class MediaExpectations {
978 public:
979 enum ExpectFrames {
980 kExpectSomeFrames,
981 kExpectNoFrames,
982 kNoExpectation,
983 };
984
985 void ExpectBidirectionalAudioAndVideo() {
986 ExpectBidirectionalAudio();
987 ExpectBidirectionalVideo();
988 }
989
990 void ExpectBidirectionalAudio() {
991 CallerExpectsSomeAudio();
992 CalleeExpectsSomeAudio();
993 }
994
995 void ExpectNoAudio() {
996 CallerExpectsNoAudio();
997 CalleeExpectsNoAudio();
998 }
999
1000 void ExpectBidirectionalVideo() {
1001 CallerExpectsSomeVideo();
1002 CalleeExpectsSomeVideo();
1003 }
1004
1005 void ExpectNoVideo() {
1006 CallerExpectsNoVideo();
1007 CalleeExpectsNoVideo();
1008 }
1009
1010 void CallerExpectsSomeAudioAndVideo() {
1011 CallerExpectsSomeAudio();
1012 CallerExpectsSomeVideo();
1013 }
1014
1015 void CalleeExpectsSomeAudioAndVideo() {
1016 CalleeExpectsSomeAudio();
1017 CalleeExpectsSomeVideo();
1018 }
1019
1020 // Caller's audio functions.
1021 void CallerExpectsSomeAudio(
1022 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1023 caller_audio_expectation_ = kExpectSomeFrames;
1024 caller_audio_frames_expected_ = expected_audio_frames;
1025 }
1026
1027 void CallerExpectsNoAudio() {
1028 caller_audio_expectation_ = kExpectNoFrames;
1029 caller_audio_frames_expected_ = 0;
1030 }
1031
1032 // Caller's video functions.
1033 void CallerExpectsSomeVideo(
1034 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1035 caller_video_expectation_ = kExpectSomeFrames;
1036 caller_video_frames_expected_ = expected_video_frames;
1037 }
1038
1039 void CallerExpectsNoVideo() {
1040 caller_video_expectation_ = kExpectNoFrames;
1041 caller_video_frames_expected_ = 0;
1042 }
1043
1044 // Callee's audio functions.
1045 void CalleeExpectsSomeAudio(
1046 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1047 callee_audio_expectation_ = kExpectSomeFrames;
1048 callee_audio_frames_expected_ = expected_audio_frames;
1049 }
1050
1051 void CalleeExpectsNoAudio() {
1052 callee_audio_expectation_ = kExpectNoFrames;
1053 callee_audio_frames_expected_ = 0;
1054 }
1055
1056 // Callee's video functions.
1057 void CalleeExpectsSomeVideo(
1058 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1059 callee_video_expectation_ = kExpectSomeFrames;
1060 callee_video_frames_expected_ = expected_video_frames;
1061 }
1062
1063 void CalleeExpectsNoVideo() {
1064 callee_video_expectation_ = kExpectNoFrames;
1065 callee_video_frames_expected_ = 0;
1066 }
1067
1068 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1069 ExpectFrames caller_video_expectation_ = kNoExpectation;
1070 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1071 ExpectFrames callee_video_expectation_ = kNoExpectation;
1072 int caller_audio_frames_expected_ = 0;
1073 int caller_video_frames_expected_ = 0;
1074 int callee_audio_frames_expected_ = 0;
1075 int callee_video_frames_expected_ = 0;
1076};
1077
deadbeef1dcb1642017-03-29 21:08:16 -07001078// Tests two PeerConnections connecting to each other end-to-end, using a
1079// virtual network, fake A/V capture and fake encoder/decoders. The
1080// PeerConnections share the threads/socket servers, but use separate versions
1081// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001082class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001083 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001084 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1085 : sdp_semantics_(sdp_semantics),
1086 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001087 fss_(new rtc::FirewallSocketServer(ss_.get())),
1088 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001089 worker_thread_(rtc::Thread::Create()) {
1090 RTC_CHECK(network_thread_->Start());
1091 RTC_CHECK(worker_thread_->Start());
1092 }
1093
Seth Hampson2f0d7022018-02-20 11:54:42 -08001094 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001095 if (caller_) {
1096 caller_->set_signaling_message_receiver(nullptr);
1097 }
1098 if (callee_) {
1099 callee_->set_signaling_message_receiver(nullptr);
1100 }
1101 }
1102
1103 bool SignalingStateStable() {
1104 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1105 }
1106
deadbeef71452802017-05-07 17:21:01 -07001107 bool DtlsConnected() {
1108 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1109 // are connected. This is an important distinction. Once we have separate
1110 // ICE and DTLS state, this check needs to use the DTLS state.
1111 return (callee()->ice_connection_state() ==
1112 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1113 callee()->ice_connection_state() ==
1114 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1115 (caller()->ice_connection_state() ==
1116 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1117 caller()->ice_connection_state() ==
1118 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1119 }
1120
Seth Hampson2f0d7022018-02-20 11:54:42 -08001121 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1122 const std::string& debug_name,
1123 const MediaConstraintsInterface* constraints,
1124 const PeerConnectionFactory::Options* options,
1125 const RTCConfiguration* config,
1126 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1127 RTCConfiguration modified_config;
1128 if (config) {
1129 modified_config = *config;
1130 }
1131 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1132 modified_config.sdp_semantics = sdp_semantics_;
1133 }
1134 if (!cert_generator) {
1135 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1136 }
1137 std::unique_ptr<PeerConnectionWrapper> client(
1138 new PeerConnectionWrapper(debug_name));
1139 if (!client->Init(constraints, options, &modified_config,
1140 std::move(cert_generator), network_thread_.get(),
1141 worker_thread_.get())) {
1142 return nullptr;
1143 }
1144 return client;
1145 }
1146
deadbeef1dcb1642017-03-29 21:08:16 -07001147 bool CreatePeerConnectionWrappers() {
1148 return CreatePeerConnectionWrappersWithConfig(
1149 PeerConnectionInterface::RTCConfiguration(),
1150 PeerConnectionInterface::RTCConfiguration());
1151 }
1152
1153 bool CreatePeerConnectionWrappersWithConstraints(
1154 MediaConstraintsInterface* caller_constraints,
1155 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001156 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1157 nullptr, nullptr);
1158 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1159 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001160 return caller_ && callee_;
1161 }
1162
1163 bool CreatePeerConnectionWrappersWithConfig(
1164 const PeerConnectionInterface::RTCConfiguration& caller_config,
1165 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001166 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1167 &caller_config, nullptr);
1168 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1169 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001170 return caller_ && callee_;
1171 }
1172
1173 bool CreatePeerConnectionWrappersWithOptions(
1174 const PeerConnectionFactory::Options& caller_options,
1175 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001176 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1177 nullptr, nullptr);
1178 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1179 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001180 return caller_ && callee_;
1181 }
1182
Seth Hampson2f0d7022018-02-20 11:54:42 -08001183 std::unique_ptr<PeerConnectionWrapper>
1184 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001185 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1186 new FakeRTCCertificateGenerator());
1187 cert_generator->use_alternate_key();
1188
Seth Hampson2f0d7022018-02-20 11:54:42 -08001189 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1190 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001191 }
1192
1193 // Once called, SDP blobs and ICE candidates will be automatically signaled
1194 // between PeerConnections.
1195 void ConnectFakeSignaling() {
1196 caller_->set_signaling_message_receiver(callee_.get());
1197 callee_->set_signaling_message_receiver(caller_.get());
1198 }
1199
Steve Antonede9ca52017-10-16 13:04:27 -07001200 // Once called, SDP blobs will be automatically signaled between
1201 // PeerConnections. Note that ICE candidates will not be signaled unless they
1202 // are in the exchanged SDP blobs.
1203 void ConnectFakeSignalingForSdpOnly() {
1204 ConnectFakeSignaling();
1205 SetSignalIceCandidates(false);
1206 }
1207
deadbeef1dcb1642017-03-29 21:08:16 -07001208 void SetSignalingDelayMs(int delay_ms) {
1209 caller_->set_signaling_delay_ms(delay_ms);
1210 callee_->set_signaling_delay_ms(delay_ms);
1211 }
1212
Steve Antonede9ca52017-10-16 13:04:27 -07001213 void SetSignalIceCandidates(bool signal) {
1214 caller_->set_signal_ice_candidates(signal);
1215 callee_->set_signal_ice_candidates(signal);
1216 }
1217
deadbeef1dcb1642017-03-29 21:08:16 -07001218 void EnableVideoDecoderFactory() {
1219 caller_->EnableVideoDecoderFactory();
1220 callee_->EnableVideoDecoderFactory();
1221 }
1222
1223 // Messages may get lost on the unreliable DataChannel, so we send multiple
1224 // times to avoid test flakiness.
1225 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1226 const std::string& data,
1227 int retries) {
1228 for (int i = 0; i < retries; ++i) {
1229 dc->Send(DataBuffer(data));
1230 }
1231 }
1232
1233 rtc::Thread* network_thread() { return network_thread_.get(); }
1234
1235 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1236
1237 PeerConnectionWrapper* caller() { return caller_.get(); }
1238
1239 // Set the |caller_| to the |wrapper| passed in and return the
1240 // original |caller_|.
1241 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1242 PeerConnectionWrapper* wrapper) {
1243 PeerConnectionWrapper* old = caller_.release();
1244 caller_.reset(wrapper);
1245 return old;
1246 }
1247
1248 PeerConnectionWrapper* callee() { return callee_.get(); }
1249
1250 // Set the |callee_| to the |wrapper| passed in and return the
1251 // original |callee_|.
1252 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1253 PeerConnectionWrapper* wrapper) {
1254 PeerConnectionWrapper* old = callee_.release();
1255 callee_.reset(wrapper);
1256 return old;
1257 }
1258
Steve Antonede9ca52017-10-16 13:04:27 -07001259 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1260
Seth Hampson2f0d7022018-02-20 11:54:42 -08001261 // Expects the provided number of new frames to be received within
1262 // kMaxWaitForFramesMs. The new expected frames are specified in
1263 // |media_expectations|. Returns false if any of the expectations were
1264 // not met.
1265 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1266 // First initialize the expected frame counts based upon the current
1267 // frame count.
1268 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1269 if (media_expectations.caller_audio_expectation_ ==
1270 MediaExpectations::kExpectSomeFrames) {
1271 total_caller_audio_frames_expected +=
1272 media_expectations.caller_audio_frames_expected_;
1273 }
1274 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001275 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001276 if (media_expectations.caller_video_expectation_ ==
1277 MediaExpectations::kExpectSomeFrames) {
1278 total_caller_video_frames_expected +=
1279 media_expectations.caller_video_frames_expected_;
1280 }
1281 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1282 if (media_expectations.callee_audio_expectation_ ==
1283 MediaExpectations::kExpectSomeFrames) {
1284 total_callee_audio_frames_expected +=
1285 media_expectations.callee_audio_frames_expected_;
1286 }
1287 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001288 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001289 if (media_expectations.callee_video_expectation_ ==
1290 MediaExpectations::kExpectSomeFrames) {
1291 total_callee_video_frames_expected +=
1292 media_expectations.callee_video_frames_expected_;
1293 }
deadbeef1dcb1642017-03-29 21:08:16 -07001294
Seth Hampson2f0d7022018-02-20 11:54:42 -08001295 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001296 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001298 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001299 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001300 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001301 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001302 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001303 total_callee_video_frames_expected,
1304 kMaxWaitForFramesMs);
1305 bool expectations_correct =
1306 caller()->audio_frames_received() >=
1307 total_caller_audio_frames_expected &&
1308 caller()->min_video_frames_received_per_track() >=
1309 total_caller_video_frames_expected &&
1310 callee()->audio_frames_received() >=
1311 total_callee_audio_frames_expected &&
1312 callee()->min_video_frames_received_per_track() >=
1313 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001314
Seth Hampson2f0d7022018-02-20 11:54:42 -08001315 // After the combined wait, print out a more detailed message upon
1316 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001317 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001318 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001319 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001320 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001321 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001323 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 total_callee_video_frames_expected);
1325
1326 // We want to make sure nothing unexpected was received.
1327 if (media_expectations.caller_audio_expectation_ ==
1328 MediaExpectations::kExpectNoFrames) {
1329 EXPECT_EQ(caller()->audio_frames_received(),
1330 total_caller_audio_frames_expected);
1331 if (caller()->audio_frames_received() !=
1332 total_caller_audio_frames_expected) {
1333 expectations_correct = false;
1334 }
1335 }
1336 if (media_expectations.caller_video_expectation_ ==
1337 MediaExpectations::kExpectNoFrames) {
1338 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1339 total_caller_video_frames_expected);
1340 if (caller()->min_video_frames_received_per_track() !=
1341 total_caller_video_frames_expected) {
1342 expectations_correct = false;
1343 }
1344 }
1345 if (media_expectations.callee_audio_expectation_ ==
1346 MediaExpectations::kExpectNoFrames) {
1347 EXPECT_EQ(callee()->audio_frames_received(),
1348 total_callee_audio_frames_expected);
1349 if (callee()->audio_frames_received() !=
1350 total_callee_audio_frames_expected) {
1351 expectations_correct = false;
1352 }
1353 }
1354 if (media_expectations.callee_video_expectation_ ==
1355 MediaExpectations::kExpectNoFrames) {
1356 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1357 total_callee_video_frames_expected);
1358 if (callee()->min_video_frames_received_per_track() !=
1359 total_callee_video_frames_expected) {
1360 expectations_correct = false;
1361 }
1362 }
1363 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001364 }
1365
Tommi8e545ee2018-02-08 16:25:20 +00001366 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1367 bool remote_gcm_enabled,
1368 int expected_cipher_suite) {
1369 PeerConnectionFactory::Options caller_options;
1370 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1371 PeerConnectionFactory::Options callee_options;
1372 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
deadbeef1dcb1642017-03-29 21:08:16 -07001373 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1374 callee_options));
1375 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1376 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1377 caller()->pc()->RegisterUMAObserver(caller_observer);
1378 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001379 caller()->AddAudioVideoTracks();
1380 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001381 caller()->CreateAndSetAndSignalOffer();
1382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1383 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001384 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001385 EXPECT_EQ(
1386 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1387 expected_cipher_suite));
1388 caller()->pc()->RegisterUMAObserver(nullptr);
1389 }
1390
Seth Hampson2f0d7022018-02-20 11:54:42 -08001391 protected:
1392 const SdpSemantics sdp_semantics_;
1393
deadbeef1dcb1642017-03-29 21:08:16 -07001394 private:
1395 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001396 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001397 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001398 // |network_thread_| and |worker_thread_| are used by both
1399 // |caller_| and |callee_| so they must be destroyed
1400 // later.
1401 std::unique_ptr<rtc::Thread> network_thread_;
1402 std::unique_ptr<rtc::Thread> worker_thread_;
1403 std::unique_ptr<PeerConnectionWrapper> caller_;
1404 std::unique_ptr<PeerConnectionWrapper> callee_;
1405};
1406
Seth Hampson2f0d7022018-02-20 11:54:42 -08001407class PeerConnectionIntegrationTest
1408 : public PeerConnectionIntegrationBaseTest,
1409 public ::testing::WithParamInterface<SdpSemantics> {
1410 protected:
1411 PeerConnectionIntegrationTest()
1412 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1413};
1414
1415class PeerConnectionIntegrationTestPlanB
1416 : public PeerConnectionIntegrationBaseTest {
1417 protected:
1418 PeerConnectionIntegrationTestPlanB()
1419 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1420};
1421
1422class PeerConnectionIntegrationTestUnifiedPlan
1423 : public PeerConnectionIntegrationBaseTest {
1424 protected:
1425 PeerConnectionIntegrationTestUnifiedPlan()
1426 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1427};
1428
deadbeef1dcb1642017-03-29 21:08:16 -07001429// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1430// includes testing that the callback is invoked if an observer is connected
1431// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001432TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001433 RtpReceiverObserverOnFirstPacketReceived) {
1434 ASSERT_TRUE(CreatePeerConnectionWrappers());
1435 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001436 caller()->AddAudioVideoTracks();
1437 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001438 // Start offer/answer exchange and wait for it to complete.
1439 caller()->CreateAndSetAndSignalOffer();
1440 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1441 // Should be one receiver each for audio/video.
1442 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1443 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1444 // Wait for all "first packet received" callbacks to be fired.
1445 EXPECT_TRUE_WAIT(
1446 std::all_of(caller()->rtp_receiver_observers().begin(),
1447 caller()->rtp_receiver_observers().end(),
1448 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1449 return o->first_packet_received();
1450 }),
1451 kMaxWaitForFramesMs);
1452 EXPECT_TRUE_WAIT(
1453 std::all_of(callee()->rtp_receiver_observers().begin(),
1454 callee()->rtp_receiver_observers().end(),
1455 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1456 return o->first_packet_received();
1457 }),
1458 kMaxWaitForFramesMs);
1459 // If new observers are set after the first packet was already received, the
1460 // callback should still be invoked.
1461 caller()->ResetRtpReceiverObservers();
1462 callee()->ResetRtpReceiverObservers();
1463 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1464 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1465 EXPECT_TRUE(
1466 std::all_of(caller()->rtp_receiver_observers().begin(),
1467 caller()->rtp_receiver_observers().end(),
1468 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1469 return o->first_packet_received();
1470 }));
1471 EXPECT_TRUE(
1472 std::all_of(callee()->rtp_receiver_observers().begin(),
1473 callee()->rtp_receiver_observers().end(),
1474 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1475 return o->first_packet_received();
1476 }));
1477}
1478
1479class DummyDtmfObserver : public DtmfSenderObserverInterface {
1480 public:
1481 DummyDtmfObserver() : completed_(false) {}
1482
1483 // Implements DtmfSenderObserverInterface.
1484 void OnToneChange(const std::string& tone) override {
1485 tones_.push_back(tone);
1486 if (tone.empty()) {
1487 completed_ = true;
1488 }
1489 }
1490
1491 const std::vector<std::string>& tones() const { return tones_; }
1492 bool completed() const { return completed_; }
1493
1494 private:
1495 bool completed_;
1496 std::vector<std::string> tones_;
1497};
1498
1499// Assumes |sender| already has an audio track added and the offer/answer
1500// exchange is done.
1501void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1502 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001503 // We should be able to get a DTMF sender from the local sender.
1504 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1505 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1506 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001507 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001508 dtmf_sender->RegisterObserver(&observer);
1509
1510 // Test the DtmfSender object just created.
1511 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1512 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1513
1514 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1515 std::vector<std::string> tones = {"1", "a", ""};
1516 EXPECT_EQ(tones, observer.tones());
1517 dtmf_sender->UnregisterObserver();
1518 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1519}
1520
1521// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1522// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001523TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001524 ASSERT_TRUE(CreatePeerConnectionWrappers());
1525 ConnectFakeSignaling();
1526 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001527 caller()->AddAudioTrack();
1528 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001529 caller()->CreateAndSetAndSignalOffer();
1530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001531 // DTLS must finish before the DTMF sender can be used reliably.
1532 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001533 TestDtmfFromSenderToReceiver(caller(), callee());
1534 TestDtmfFromSenderToReceiver(callee(), caller());
1535}
1536
1537// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1538// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001539TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001540 ASSERT_TRUE(CreatePeerConnectionWrappers());
1541 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001542 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1543 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1544 caller()->pc()->RegisterUMAObserver(caller_observer);
1545
deadbeef1dcb1642017-03-29 21:08:16 -07001546 // Do normal offer/answer and wait for some frames to be received in each
1547 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001548 caller()->AddAudioVideoTracks();
1549 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001550 caller()->CreateAndSetAndSignalOffer();
1551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001552 MediaExpectations media_expectations;
1553 media_expectations.ExpectBidirectionalAudioAndVideo();
1554 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001555 EXPECT_LE(
1556 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1557 webrtc::kEnumCounterKeyProtocolDtls));
1558 EXPECT_EQ(
1559 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1560 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001561}
1562
1563// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001564TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001565 PeerConnectionInterface::RTCConfiguration sdes_config;
1566 sdes_config.enable_dtls_srtp.emplace(false);
1567 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1568 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001569 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1570 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1571 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001572
1573 // Do normal offer/answer and wait for some frames to be received in each
1574 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001575 caller()->AddAudioVideoTracks();
1576 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001577 caller()->CreateAndSetAndSignalOffer();
1578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001579 MediaExpectations media_expectations;
1580 media_expectations.ExpectBidirectionalAudioAndVideo();
1581 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001582 EXPECT_LE(
1583 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1584 webrtc::kEnumCounterKeyProtocolSdes));
1585 EXPECT_EQ(
1586 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1587 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001588}
1589
Steve Anton8c0f7a72017-10-03 10:03:10 -07001590// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1591// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001592TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001593 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001594 // TODO(bugs.webrtc.org/8764): Enable for Unified Plan once stats work.
1595 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
1596 return;
1597 }
Steve Anton8c0f7a72017-10-03 10:03:10 -07001598 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1599 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1600 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1601 return pc->GetRemoteAudioSSLCertificate();
1602 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001603 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1604 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1605 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1606 return pc->GetRemoteAudioSSLCertChain();
1607 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001608
1609 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1610 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1611
1612 // Configure each side with a known certificate so they can be compared later.
1613 PeerConnectionInterface::RTCConfiguration caller_config;
1614 caller_config.enable_dtls_srtp.emplace(true);
1615 caller_config.certificates.push_back(caller_cert);
1616 PeerConnectionInterface::RTCConfiguration callee_config;
1617 callee_config.enable_dtls_srtp.emplace(true);
1618 callee_config.certificates.push_back(callee_cert);
1619 ASSERT_TRUE(
1620 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1621 ConnectFakeSignaling();
1622
1623 // When first initialized, there should not be a remote SSL certificate (and
1624 // calling this method should not crash).
1625 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1626 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001627 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1628 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001629
Steve Anton15324772018-01-16 10:26:49 -08001630 caller()->AddAudioTrack();
1631 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001632 caller()->CreateAndSetAndSignalOffer();
1633 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1634 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1635
1636 // Once DTLS has been connected, each side should return the other's SSL
1637 // certificate when calling GetRemoteAudioSSLCertificate.
1638
1639 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1640 ASSERT_TRUE(caller_remote_cert);
1641 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1642 caller_remote_cert->ToPEMString());
1643
1644 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1645 ASSERT_TRUE(callee_remote_cert);
1646 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1647 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001648
1649 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1650 ASSERT_TRUE(caller_remote_cert_chain);
1651 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1652 auto remote_cert = &caller_remote_cert_chain->Get(0);
1653 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1654 remote_cert->ToPEMString());
1655
1656 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1657 ASSERT_TRUE(callee_remote_cert_chain);
1658 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1659 remote_cert = &callee_remote_cert_chain->Get(0);
1660 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1661 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001662}
1663
deadbeef1dcb1642017-03-29 21:08:16 -07001664// This test sets up a call between two parties (using DTLS) and tests that we
1665// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001666TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001667 ASSERT_TRUE(CreatePeerConnectionWrappers());
1668 ConnectFakeSignaling();
1669
1670 // Add video tracks with 16:9 constraint.
1671 FakeConstraints constraints;
1672 double requested_ratio = 16.0 / 9;
1673 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001674 caller()->AddTrack(
1675 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1676 callee()->AddTrack(
1677 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001678
1679 // Do normal offer/answer and wait for at least one frame to be received in
1680 // each direction.
1681 caller()->CreateAndSetAndSignalOffer();
1682 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1683 callee()->min_video_frames_received_per_track() > 0,
1684 kMaxWaitForFramesMs);
1685
1686 // Check rendered aspect ratio.
1687 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1688 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1689 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1690 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1691}
1692
1693// This test sets up a call between two parties with a source resolution of
1694// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001695TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001696 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1697 ASSERT_TRUE(CreatePeerConnectionWrappers());
1698 ConnectFakeSignaling();
1699
1700 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1701 // instead of aspect ratio constraint.
1702 FakeConstraints constraints;
1703 constraints.SetMandatoryMinWidth(1280);
1704 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001705 caller()->AddTrack(
1706 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1707 callee()->AddTrack(
1708 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001709
1710 // Do normal offer/answer and wait for at least one frame to be received in
1711 // each direction.
1712 caller()->CreateAndSetAndSignalOffer();
1713 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1714 callee()->min_video_frames_received_per_track() > 0,
1715 kMaxWaitForFramesMs);
1716
1717 // Check rendered aspect ratio.
1718 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1719 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1720 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1721 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1722}
1723
1724// This test sets up an one-way call, with media only from caller to
1725// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001726TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001727 ASSERT_TRUE(CreatePeerConnectionWrappers());
1728 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001729 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001730 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001731 MediaExpectations media_expectations;
1732 media_expectations.CalleeExpectsSomeAudioAndVideo();
1733 media_expectations.CallerExpectsNoAudio();
1734 media_expectations.CallerExpectsNoVideo();
1735 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001736}
1737
1738// This test sets up a audio call initially, with the callee rejecting video
1739// initially. Then later the callee decides to upgrade to audio/video, and
1740// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001741TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001742 ASSERT_TRUE(CreatePeerConnectionWrappers());
1743 ConnectFakeSignaling();
1744 // Initially, offer an audio/video stream from the caller, but refuse to
1745 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001746 caller()->AddAudioVideoTracks();
1747 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001748 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1749 PeerConnectionInterface::RTCOfferAnswerOptions options;
1750 options.offer_to_receive_video = 0;
1751 callee()->SetOfferAnswerOptions(options);
1752 } else {
1753 callee()->SetRemoteOfferHandler([this] {
1754 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1755 });
1756 }
deadbeef1dcb1642017-03-29 21:08:16 -07001757 // Do offer/answer and make sure audio is still received end-to-end.
1758 caller()->CreateAndSetAndSignalOffer();
1759 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001760 {
1761 MediaExpectations media_expectations;
1762 media_expectations.ExpectBidirectionalAudio();
1763 media_expectations.ExpectNoVideo();
1764 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1765 }
deadbeef1dcb1642017-03-29 21:08:16 -07001766 // Sanity check that the callee's description has a rejected video section.
1767 ASSERT_NE(nullptr, callee()->pc()->local_description());
1768 const ContentInfo* callee_video_content =
1769 GetFirstVideoContent(callee()->pc()->local_description()->description());
1770 ASSERT_NE(nullptr, callee_video_content);
1771 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001772
deadbeef1dcb1642017-03-29 21:08:16 -07001773 // Now negotiate with video and ensure negotiation succeeds, with video
1774 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001775 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1777 PeerConnectionInterface::RTCOfferAnswerOptions options;
1778 options.offer_to_receive_video = 1;
1779 callee()->SetOfferAnswerOptions(options);
1780 } else {
1781 callee()->SetRemoteOfferHandler(nullptr);
1782 caller()->SetRemoteOfferHandler([this] {
1783 // The caller creates a new transceiver to receive video on when receiving
1784 // the offer, but by default it is send only.
1785 auto transceivers = caller()->pc()->GetTransceivers();
1786 ASSERT_EQ(3, transceivers.size());
1787 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1788 transceivers[2]->receiver()->media_type());
1789 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1790 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1791 });
1792 }
deadbeef1dcb1642017-03-29 21:08:16 -07001793 callee()->CreateAndSetAndSignalOffer();
1794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001795 {
1796 // Expect additional audio frames to be received after the upgrade.
1797 MediaExpectations media_expectations;
1798 media_expectations.ExpectBidirectionalAudioAndVideo();
1799 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1800 }
deadbeef1dcb1642017-03-29 21:08:16 -07001801}
1802
deadbeef4389b4d2017-09-07 09:07:36 -07001803// Simpler than the above test; just add an audio track to an established
1804// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001805TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001806 ASSERT_TRUE(CreatePeerConnectionWrappers());
1807 ConnectFakeSignaling();
1808 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001809 caller()->AddVideoTrack();
1810 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001811 caller()->CreateAndSetAndSignalOffer();
1812 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1813 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001814 caller()->AddAudioTrack();
1815 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001816 caller()->CreateAndSetAndSignalOffer();
1817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1818 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001819 MediaExpectations media_expectations;
1820 media_expectations.ExpectBidirectionalAudioAndVideo();
1821 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001822}
1823
deadbeef1dcb1642017-03-29 21:08:16 -07001824// This test sets up a call that's transferred to a new caller with a different
1825// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001826TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001827 ASSERT_TRUE(CreatePeerConnectionWrappers());
1828 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001829 caller()->AddAudioVideoTracks();
1830 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001831 caller()->CreateAndSetAndSignalOffer();
1832 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1833
1834 // Keep the original peer around which will still send packets to the
1835 // receiving client. These SRTP packets will be dropped.
1836 std::unique_ptr<PeerConnectionWrapper> original_peer(
1837 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001838 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001839 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1840 // directly above.
1841 original_peer->pc()->Close();
1842
1843 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001844 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001845 caller()->CreateAndSetAndSignalOffer();
1846 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1847 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001848 MediaExpectations media_expectations;
1849 media_expectations.ExpectBidirectionalAudioAndVideo();
1850 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001851}
1852
1853// This test sets up a call that's transferred to a new callee with a different
1854// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001855TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001856 ASSERT_TRUE(CreatePeerConnectionWrappers());
1857 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001858 caller()->AddAudioVideoTracks();
1859 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001860 caller()->CreateAndSetAndSignalOffer();
1861 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1862
1863 // Keep the original peer around which will still send packets to the
1864 // receiving client. These SRTP packets will be dropped.
1865 std::unique_ptr<PeerConnectionWrapper> original_peer(
1866 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001867 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001868 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1869 // directly above.
1870 original_peer->pc()->Close();
1871
1872 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001873 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001874 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1875 caller()->CreateAndSetAndSignalOffer();
1876 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1877 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001878 MediaExpectations media_expectations;
1879 media_expectations.ExpectBidirectionalAudioAndVideo();
1880 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001881}
1882
1883// This test sets up a non-bundled call and negotiates bundling at the same
1884// time as starting an ICE restart. When bundling is in effect in the restart,
1885// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001886TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001887 ASSERT_TRUE(CreatePeerConnectionWrappers());
1888 ConnectFakeSignaling();
1889
Steve Anton15324772018-01-16 10:26:49 -08001890 caller()->AddAudioVideoTracks();
1891 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001892 // Remove the bundle group from the SDP received by the callee.
1893 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1894 desc->RemoveGroupByName("BUNDLE");
1895 });
1896 caller()->CreateAndSetAndSignalOffer();
1897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001898 {
1899 MediaExpectations media_expectations;
1900 media_expectations.ExpectBidirectionalAudioAndVideo();
1901 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1902 }
deadbeef1dcb1642017-03-29 21:08:16 -07001903 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1904 callee()->SetReceivedSdpMunger(nullptr);
1905 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1906 caller()->CreateAndSetAndSignalOffer();
1907 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1908
1909 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001910 {
1911 MediaExpectations media_expectations;
1912 media_expectations.ExpectBidirectionalAudioAndVideo();
1913 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1914 }
deadbeef1dcb1642017-03-29 21:08:16 -07001915}
1916
1917// Test CVO (Coordination of Video Orientation). If a video source is rotated
1918// and both peers support the CVO RTP header extension, the actual video frames
1919// don't need to be encoded in different resolutions, since the rotation is
1920// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001921TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001922 ASSERT_TRUE(CreatePeerConnectionWrappers());
1923 ConnectFakeSignaling();
1924 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001925 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001926 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001927 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001928 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1929
1930 // Wait for video frames to be received by both sides.
1931 caller()->CreateAndSetAndSignalOffer();
1932 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1933 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1934 callee()->min_video_frames_received_per_track() > 0,
1935 kMaxWaitForFramesMs);
1936
1937 // Ensure that the aspect ratio is unmodified.
1938 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1939 // not just assumed.
1940 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1941 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1942 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1943 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1944 // Ensure that the CVO bits were surfaced to the renderer.
1945 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1946 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1947}
1948
1949// Test that when the CVO extension isn't supported, video is rotated the
1950// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001951TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001952 ASSERT_TRUE(CreatePeerConnectionWrappers());
1953 ConnectFakeSignaling();
1954 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001955 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001956 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001957 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001958 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1959
1960 // Remove the CVO extension from the offered SDP.
1961 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1962 cricket::VideoContentDescription* video =
1963 GetFirstVideoContentDescription(desc);
1964 video->ClearRtpHeaderExtensions();
1965 });
1966 // Wait for video frames to be received by both sides.
1967 caller()->CreateAndSetAndSignalOffer();
1968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1969 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1970 callee()->min_video_frames_received_per_track() > 0,
1971 kMaxWaitForFramesMs);
1972
1973 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1974 // rotation.
1975 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1976 // not just assumed.
1977 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1978 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1979 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1980 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1981 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1982 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1983 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1984}
1985
deadbeef1dcb1642017-03-29 21:08:16 -07001986// Test that if the answerer rejects the audio m= section, no audio is sent or
1987// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001988TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001989 ASSERT_TRUE(CreatePeerConnectionWrappers());
1990 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001991 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001992 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1993 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1994 // it will reject the audio m= section completely.
1995 PeerConnectionInterface::RTCOfferAnswerOptions options;
1996 options.offer_to_receive_audio = 0;
1997 callee()->SetOfferAnswerOptions(options);
1998 } else {
1999 // Stopping the audio RtpTransceiver will cause the media section to be
2000 // rejected in the answer.
2001 callee()->SetRemoteOfferHandler([this] {
2002 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2003 });
2004 }
Steve Anton15324772018-01-16 10:26:49 -08002005 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002006 // Do offer/answer and wait for successful end-to-end video frames.
2007 caller()->CreateAndSetAndSignalOffer();
2008 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002009 MediaExpectations media_expectations;
2010 media_expectations.ExpectBidirectionalVideo();
2011 media_expectations.ExpectNoAudio();
2012 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2013
deadbeef1dcb1642017-03-29 21:08:16 -07002014 // Sanity check that the callee's description has a rejected audio section.
2015 ASSERT_NE(nullptr, callee()->pc()->local_description());
2016 const ContentInfo* callee_audio_content =
2017 GetFirstAudioContent(callee()->pc()->local_description()->description());
2018 ASSERT_NE(nullptr, callee_audio_content);
2019 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002020 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2021 // The caller's transceiver should have stopped after receiving the answer.
2022 EXPECT_TRUE(caller()
2023 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2024 ->stopped());
2025 }
deadbeef1dcb1642017-03-29 21:08:16 -07002026}
2027
2028// Test that if the answerer rejects the video m= section, no video is sent or
2029// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002030TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002031 ASSERT_TRUE(CreatePeerConnectionWrappers());
2032 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002033 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002034 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2035 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2036 // it will reject the video m= section completely.
2037 PeerConnectionInterface::RTCOfferAnswerOptions options;
2038 options.offer_to_receive_video = 0;
2039 callee()->SetOfferAnswerOptions(options);
2040 } else {
2041 // Stopping the video RtpTransceiver will cause the media section to be
2042 // rejected in the answer.
2043 callee()->SetRemoteOfferHandler([this] {
2044 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2045 });
2046 }
Steve Anton15324772018-01-16 10:26:49 -08002047 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002048 // Do offer/answer and wait for successful end-to-end audio frames.
2049 caller()->CreateAndSetAndSignalOffer();
2050 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002051 MediaExpectations media_expectations;
2052 media_expectations.ExpectBidirectionalAudio();
2053 media_expectations.ExpectNoVideo();
2054 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2055
deadbeef1dcb1642017-03-29 21:08:16 -07002056 // Sanity check that the callee's description has a rejected video section.
2057 ASSERT_NE(nullptr, callee()->pc()->local_description());
2058 const ContentInfo* callee_video_content =
2059 GetFirstVideoContent(callee()->pc()->local_description()->description());
2060 ASSERT_NE(nullptr, callee_video_content);
2061 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002062 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2063 // The caller's transceiver should have stopped after receiving the answer.
2064 EXPECT_TRUE(caller()
2065 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2066 ->stopped());
2067 }
deadbeef1dcb1642017-03-29 21:08:16 -07002068}
2069
2070// Test that if the answerer rejects both audio and video m= sections, nothing
2071// bad happens.
2072// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2073// test anything but the fact that negotiation succeeds, which doesn't mean
2074// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002075TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002076 ASSERT_TRUE(CreatePeerConnectionWrappers());
2077 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002078 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2080 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2081 // will reject both audio and video m= sections.
2082 PeerConnectionInterface::RTCOfferAnswerOptions options;
2083 options.offer_to_receive_audio = 0;
2084 options.offer_to_receive_video = 0;
2085 callee()->SetOfferAnswerOptions(options);
2086 } else {
2087 callee()->SetRemoteOfferHandler([this] {
2088 // Stopping all transceivers will cause all media sections to be rejected.
2089 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2090 transceiver->Stop();
2091 }
2092 });
2093 }
deadbeef1dcb1642017-03-29 21:08:16 -07002094 // Do offer/answer and wait for stable signaling state.
2095 caller()->CreateAndSetAndSignalOffer();
2096 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002097
deadbeef1dcb1642017-03-29 21:08:16 -07002098 // Sanity check that the callee's description has rejected m= sections.
2099 ASSERT_NE(nullptr, callee()->pc()->local_description());
2100 const ContentInfo* callee_audio_content =
2101 GetFirstAudioContent(callee()->pc()->local_description()->description());
2102 ASSERT_NE(nullptr, callee_audio_content);
2103 EXPECT_TRUE(callee_audio_content->rejected);
2104 const ContentInfo* callee_video_content =
2105 GetFirstVideoContent(callee()->pc()->local_description()->description());
2106 ASSERT_NE(nullptr, callee_video_content);
2107 EXPECT_TRUE(callee_video_content->rejected);
2108}
2109
2110// This test sets up an audio and video call between two parties. After the
2111// call runs for a while, the caller sends an updated offer with video being
2112// rejected. Once the re-negotiation is done, the video flow should stop and
2113// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002114TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002115 ASSERT_TRUE(CreatePeerConnectionWrappers());
2116 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002117 caller()->AddAudioVideoTracks();
2118 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002119 caller()->CreateAndSetAndSignalOffer();
2120 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002121 {
2122 MediaExpectations media_expectations;
2123 media_expectations.ExpectBidirectionalAudioAndVideo();
2124 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2125 }
deadbeef1dcb1642017-03-29 21:08:16 -07002126 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002127 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2128 caller()->SetGeneratedSdpMunger(
2129 [](cricket::SessionDescription* description) {
2130 for (cricket::ContentInfo& content : description->contents()) {
2131 if (cricket::IsVideoContent(&content)) {
2132 content.rejected = true;
2133 }
2134 }
2135 });
2136 } else {
2137 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2138 }
deadbeef1dcb1642017-03-29 21:08:16 -07002139 caller()->CreateAndSetAndSignalOffer();
2140 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2141
2142 // Sanity check that the caller's description has a rejected video section.
2143 ASSERT_NE(nullptr, caller()->pc()->local_description());
2144 const ContentInfo* caller_video_content =
2145 GetFirstVideoContent(caller()->pc()->local_description()->description());
2146 ASSERT_NE(nullptr, caller_video_content);
2147 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002148 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002149 {
2150 MediaExpectations media_expectations;
2151 media_expectations.ExpectBidirectionalAudio();
2152 media_expectations.ExpectNoVideo();
2153 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2154 }
deadbeef1dcb1642017-03-29 21:08:16 -07002155}
2156
2157// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2158// is needed to support legacy endpoints.
2159// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2160// add a test for an end-to-end test without MID signaling either (basically,
2161// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002162TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002163 ASSERT_TRUE(CreatePeerConnectionWrappers());
2164 ConnectFakeSignaling();
2165 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002166 caller()->AddAudioVideoTracks();
2167 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002168 // Remove SSRCs and MSIDs from the received offer SDP.
2169 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002170 caller()->CreateAndSetAndSignalOffer();
2171 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002172 MediaExpectations media_expectations;
2173 media_expectations.ExpectBidirectionalAudioAndVideo();
2174 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002175}
2176
2177// Test that if two video tracks are sent (from caller to callee, in this test),
2178// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002179TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002180 ASSERT_TRUE(CreatePeerConnectionWrappers());
2181 ConnectFakeSignaling();
2182 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002183 caller()->AddAudioVideoTracks();
2184 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002185 caller()->CreateAndSetAndSignalOffer();
2186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002187 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002188
2189 MediaExpectations media_expectations;
2190 media_expectations.CalleeExpectsSomeAudioAndVideo();
2191 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002192}
2193
2194static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2195 bool first = true;
2196 for (cricket::ContentInfo& content : desc->contents()) {
2197 if (first) {
2198 first = false;
2199 continue;
2200 }
2201 content.bundle_only = true;
2202 }
2203 first = true;
2204 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2205 if (first) {
2206 first = false;
2207 continue;
2208 }
2209 transport.description.ice_ufrag.clear();
2210 transport.description.ice_pwd.clear();
2211 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2212 transport.description.identity_fingerprint.reset(nullptr);
2213 }
2214}
2215
2216// Test that if applying a true "max bundle" offer, which uses ports of 0,
2217// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2218// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2219// successfully and media flows.
2220// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2221// TODO(deadbeef): Won't need this test once we start generating actual
2222// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002223TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002224 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2225 ASSERT_TRUE(CreatePeerConnectionWrappers());
2226 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002227 caller()->AddAudioVideoTracks();
2228 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002229 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2230 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2231 // but the first m= section.
2232 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2233 caller()->CreateAndSetAndSignalOffer();
2234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002235 MediaExpectations media_expectations;
2236 media_expectations.ExpectBidirectionalAudioAndVideo();
2237 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002238}
2239
2240// Test that we can receive the audio output level from a remote audio track.
2241// TODO(deadbeef): Use a fake audio source and verify that the output level is
2242// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002243TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002244 ASSERT_TRUE(CreatePeerConnectionWrappers());
2245 ConnectFakeSignaling();
2246 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002247 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002248 caller()->CreateAndSetAndSignalOffer();
2249 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2250
2251 // Get the audio output level stats. Note that the level is not available
2252 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002253 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002254 kMaxWaitForFramesMs);
2255}
2256
2257// Test that an audio input level is reported.
2258// TODO(deadbeef): Use a fake audio source and verify that the input level is
2259// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002260TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002261 ASSERT_TRUE(CreatePeerConnectionWrappers());
2262 ConnectFakeSignaling();
2263 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002264 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002265 caller()->CreateAndSetAndSignalOffer();
2266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2267
2268 // Get the audio input level stats. The level should be available very
2269 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002270 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002271 kMaxWaitForStatsMs);
2272}
2273
2274// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002275TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002276 ASSERT_TRUE(CreatePeerConnectionWrappers());
2277 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002278 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002279 // Do offer/answer, wait for the callee to receive some frames.
2280 caller()->CreateAndSetAndSignalOffer();
2281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002282
2283 MediaExpectations media_expectations;
2284 media_expectations.CalleeExpectsSomeAudioAndVideo();
2285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002286
2287 // Get a handle to the remote tracks created, so they can be used as GetStats
2288 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002289 for (auto receiver : callee()->pc()->GetReceivers()) {
2290 // We received frames, so we definitely should have nonzero "received bytes"
2291 // stats at this point.
2292 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2293 0);
2294 }
deadbeef1dcb1642017-03-29 21:08:16 -07002295}
2296
2297// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002298TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002299 ASSERT_TRUE(CreatePeerConnectionWrappers());
2300 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002301 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002302 auto audio_track = caller()->CreateLocalAudioTrack();
2303 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002304 caller()->AddTrack(audio_track);
2305 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002306 // Do offer/answer, wait for the callee to receive some frames.
2307 caller()->CreateAndSetAndSignalOffer();
2308 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002309 MediaExpectations media_expectations;
2310 media_expectations.CalleeExpectsSomeAudioAndVideo();
2311 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002312
2313 // The callee received frames, so we definitely should have nonzero "sent
2314 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002315 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2316 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2317}
2318
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002319// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002320TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002321 ASSERT_TRUE(CreatePeerConnectionWrappers());
2322 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002323 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002324
Steve Anton15324772018-01-16 10:26:49 -08002325 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002326
2327 // Do offer/answer, wait for the callee to receive some frames.
2328 caller()->CreateAndSetAndSignalOffer();
2329 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2330
2331 // Get the remote audio track created on the receiver, so they can be used as
2332 // GetStats filters.
2333 StreamCollectionInterface* remote_streams = callee()->remote_streams();
2334 ASSERT_EQ(1u, remote_streams->count());
2335 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
2336 MediaStreamTrackInterface* remote_audio_track =
2337 remote_streams->at(0)->GetAudioTracks()[0];
2338
2339 // Get the audio output level stats. Note that the level is not available
2340 // until an RTCP packet has been received.
2341 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
2342 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
2343}
2344
deadbeefd8ad7882017-04-18 16:01:17 -07002345// Test that we can get stats (using the new stats implemnetation) for
2346// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2347// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002348TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002349 GetStatsForUnsignaledStreamWithNewStatsApi) {
2350 ASSERT_TRUE(CreatePeerConnectionWrappers());
2351 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002352 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002353 // Remove SSRCs and MSIDs from the received offer SDP.
2354 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2355 caller()->CreateAndSetAndSignalOffer();
2356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002357 MediaExpectations media_expectations;
2358 media_expectations.CalleeExpectsSomeAudio(1);
2359 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002360
2361 // We received a frame, so we should have nonzero "bytes received" stats for
2362 // the unsignaled stream, if stats are working for it.
2363 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2364 callee()->NewGetStats();
2365 ASSERT_NE(nullptr, report);
2366 auto inbound_stream_stats =
2367 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2368 ASSERT_EQ(1U, inbound_stream_stats.size());
2369 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2370 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002371 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2372}
2373
2374// Test that we can successfully get the media related stats (audio level
2375// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002376TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002377 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2378 ASSERT_TRUE(CreatePeerConnectionWrappers());
2379 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002380 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002381 // Remove SSRCs and MSIDs from the received offer SDP.
2382 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2383 caller()->CreateAndSetAndSignalOffer();
2384 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002385 MediaExpectations media_expectations;
2386 media_expectations.CalleeExpectsSomeAudio(1);
2387 media_expectations.CalleeExpectsSomeVideo(1);
2388 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002389
2390 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2391 callee()->NewGetStats();
2392 ASSERT_NE(nullptr, report);
2393
2394 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2395 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2396 ASSERT_GE(audio_index, 0);
2397 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002398}
2399
deadbeef4e2deab2017-09-20 13:56:21 -07002400// Helper for test below.
2401void ModifySsrcs(cricket::SessionDescription* desc) {
2402 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002403 for (cricket::StreamParams& stream :
2404 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002405 for (uint32_t& ssrc : stream.ssrcs) {
2406 ssrc = rtc::CreateRandomId();
2407 }
2408 }
2409 }
2410}
2411
2412// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2413// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2414// This should result in two "RTCInboundRTPStreamStats", but only one
2415// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2416// being reset to 0 once the SSRC change occurs.
2417//
2418// Regression test for this bug:
2419// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2420//
2421// The bug causes the track stats to only represent one of the two streams:
2422// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2423// that the track stat counters would reset to 0 when the new stream is
2424// received, and a 50% chance that they'll stop updating (while
2425// "concealed_samples" continues increasing, due to silence being generated for
2426// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002427TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002428 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002429 ASSERT_TRUE(CreatePeerConnectionWrappers());
2430 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002431 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002432 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2433 // that doesn't signal SSRCs (from the callee's perspective).
2434 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2435 caller()->CreateAndSetAndSignalOffer();
2436 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2437 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002438 {
2439 MediaExpectations media_expectations;
2440 media_expectations.CalleeExpectsSomeAudio(50);
2441 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2442 }
deadbeef4e2deab2017-09-20 13:56:21 -07002443 // Some audio frames were received, so we should have nonzero "samples
2444 // received" for the track.
2445 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2446 callee()->NewGetStats();
2447 ASSERT_NE(nullptr, report);
2448 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2449 ASSERT_EQ(1U, track_stats.size());
2450 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2451 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2452 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2453
2454 // Create a new offer and munge it to cause the caller to use a new SSRC.
2455 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2456 caller()->CreateAndSetAndSignalOffer();
2457 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2458 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2459 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002460 {
2461 MediaExpectations media_expectations;
2462 media_expectations.CalleeExpectsSomeAudio(25);
2463 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2464 }
deadbeef4e2deab2017-09-20 13:56:21 -07002465
2466 report = callee()->NewGetStats();
2467 ASSERT_NE(nullptr, report);
2468 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2469 ASSERT_EQ(1U, track_stats.size());
2470 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2471 // The "total samples received" stat should only be greater than it was
2472 // before.
2473 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2474 // Right now, the new SSRC will cause the counters to reset to 0.
2475 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2476
2477 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002478 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002479 // good sign that we're seeing stats from the old stream that's no longer
2480 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002481 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002482 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2483 EXPECT_LT(*track_stats[0]->concealed_samples,
2484 *track_stats[0]->total_samples_received *
2485 kAcceptableConcealedSamplesPercentage);
2486
2487 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2488 // sanity check that the SSRC really changed.
2489 // TODO(deadbeef): This isn't working right now, because we're not returning
2490 // *any* stats for the inactive stream. Uncomment when the bug is completely
2491 // fixed.
2492 // auto inbound_stream_stats =
2493 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2494 // ASSERT_EQ(2U, inbound_stream_stats.size());
2495}
2496
deadbeef1dcb1642017-03-29 21:08:16 -07002497// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002498TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002499 PeerConnectionFactory::Options dtls_10_options;
2500 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2501 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2502 dtls_10_options));
2503 ConnectFakeSignaling();
2504 // Do normal offer/answer and wait for some frames to be received in each
2505 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002506 caller()->AddAudioVideoTracks();
2507 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002508 caller()->CreateAndSetAndSignalOffer();
2509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002510 MediaExpectations media_expectations;
2511 media_expectations.ExpectBidirectionalAudioAndVideo();
2512 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002513}
2514
2515// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002516TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002517 PeerConnectionFactory::Options dtls_10_options;
2518 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2519 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2520 dtls_10_options));
2521 ConnectFakeSignaling();
2522 // Register UMA observer before signaling begins.
2523 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2524 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2525 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002526 caller()->AddAudioVideoTracks();
2527 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002528 caller()->CreateAndSetAndSignalOffer();
2529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2530 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002531 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002532 kDefaultTimeout);
2533 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002534 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002535 EXPECT_EQ(1,
2536 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2537 kDefaultSrtpCryptoSuite));
2538}
2539
2540// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002541TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002542 PeerConnectionFactory::Options dtls_12_options;
2543 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2544 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2545 dtls_12_options));
2546 ConnectFakeSignaling();
2547 // Register UMA observer before signaling begins.
2548 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2549 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2550 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002551 caller()->AddAudioVideoTracks();
2552 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002553 caller()->CreateAndSetAndSignalOffer();
2554 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2555 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002556 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002557 kDefaultTimeout);
2558 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002559 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002560 EXPECT_EQ(1,
2561 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2562 kDefaultSrtpCryptoSuite));
2563}
2564
2565// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2566// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002567TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002568 PeerConnectionFactory::Options caller_options;
2569 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2570 PeerConnectionFactory::Options callee_options;
2571 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2572 ASSERT_TRUE(
2573 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2574 ConnectFakeSignaling();
2575 // Do normal offer/answer and wait for some frames to be received in each
2576 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002577 caller()->AddAudioVideoTracks();
2578 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002579 caller()->CreateAndSetAndSignalOffer();
2580 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002581 MediaExpectations media_expectations;
2582 media_expectations.ExpectBidirectionalAudioAndVideo();
2583 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002584}
2585
2586// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2587// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002588TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002589 PeerConnectionFactory::Options caller_options;
2590 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2591 PeerConnectionFactory::Options callee_options;
2592 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2593 ASSERT_TRUE(
2594 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2595 ConnectFakeSignaling();
2596 // Do normal offer/answer and wait for some frames to be received in each
2597 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002598 caller()->AddAudioVideoTracks();
2599 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002600 caller()->CreateAndSetAndSignalOffer();
2601 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002602 MediaExpectations media_expectations;
2603 media_expectations.ExpectBidirectionalAudioAndVideo();
2604 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002605}
2606
2607// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002609 bool local_gcm_enabled = false;
2610 bool remote_gcm_enabled = false;
2611 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2612 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2613 expected_cipher_suite);
2614}
2615
2616// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002617TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002618 bool local_gcm_enabled = true;
2619 bool remote_gcm_enabled = true;
2620 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2621 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2622 expected_cipher_suite);
2623}
2624
2625// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002626TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002627 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2628 bool local_gcm_enabled = true;
2629 bool remote_gcm_enabled = false;
2630 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2631 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2632 expected_cipher_suite);
2633}
2634
2635// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002636TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002637 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2638 bool local_gcm_enabled = false;
2639 bool remote_gcm_enabled = true;
2640 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2641 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2642 expected_cipher_suite);
2643}
2644
deadbeef7914b8c2017-04-21 03:23:33 -07002645// Verify that media can be transmitted end-to-end when GCM crypto suites are
2646// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2647// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2648// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002649TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002650 PeerConnectionFactory::Options gcm_options;
2651 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2652 ASSERT_TRUE(
2653 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2654 ConnectFakeSignaling();
2655 // Do normal offer/answer and wait for some frames to be received in each
2656 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002657 caller()->AddAudioVideoTracks();
2658 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002659 caller()->CreateAndSetAndSignalOffer();
2660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002661 MediaExpectations media_expectations;
2662 media_expectations.ExpectBidirectionalAudioAndVideo();
2663 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002664}
2665
deadbeef1dcb1642017-03-29 21:08:16 -07002666// This test sets up a call between two parties with audio, video and an RTP
2667// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002668TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002669 FakeConstraints setup_constraints;
2670 setup_constraints.SetAllowRtpDataChannels();
2671 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2672 &setup_constraints));
2673 ConnectFakeSignaling();
2674 // Expect that data channel created on caller side will show up for callee as
2675 // well.
2676 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002677 caller()->AddAudioVideoTracks();
2678 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002679 caller()->CreateAndSetAndSignalOffer();
2680 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2681 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002682 MediaExpectations media_expectations;
2683 media_expectations.ExpectBidirectionalAudioAndVideo();
2684 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002685 ASSERT_NE(nullptr, caller()->data_channel());
2686 ASSERT_NE(nullptr, callee()->data_channel());
2687 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2688 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2689
2690 // Ensure data can be sent in both directions.
2691 std::string data = "hello world";
2692 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2693 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2694 kDefaultTimeout);
2695 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2696 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2697 kDefaultTimeout);
2698}
2699
2700// Ensure that an RTP data channel is signaled as closed for the caller when
2701// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002702TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002703 RtpDataChannelSignaledClosedInCalleeOffer) {
2704 // Same procedure as above test.
2705 FakeConstraints setup_constraints;
2706 setup_constraints.SetAllowRtpDataChannels();
2707 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2708 &setup_constraints));
2709 ConnectFakeSignaling();
2710 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002711 caller()->AddAudioVideoTracks();
2712 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002713 caller()->CreateAndSetAndSignalOffer();
2714 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2715 ASSERT_NE(nullptr, caller()->data_channel());
2716 ASSERT_NE(nullptr, callee()->data_channel());
2717 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2718 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2719
2720 // Close the data channel on the callee, and do an updated offer/answer.
2721 callee()->data_channel()->Close();
2722 callee()->CreateAndSetAndSignalOffer();
2723 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2724 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2725 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2726}
2727
2728// Tests that data is buffered in an RTP data channel until an observer is
2729// registered for it.
2730//
2731// NOTE: RTP data channels can receive data before the underlying
2732// transport has detected that a channel is writable and thus data can be
2733// received before the data channel state changes to open. That is hard to test
2734// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002735TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002736 DataBufferedUntilRtpDataChannelObserverRegistered) {
2737 // Use fake clock and simulated network delay so that we predictably can wait
2738 // until an SCTP message has been delivered without "sleep()"ing.
2739 rtc::ScopedFakeClock fake_clock;
2740 // Some things use a time of "0" as a special value, so we need to start out
2741 // the fake clock at a nonzero time.
2742 // TODO(deadbeef): Fix this.
2743 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2744 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2745 virtual_socket_server()->UpdateDelayDistribution();
2746
2747 FakeConstraints constraints;
2748 constraints.SetAllowRtpDataChannels();
2749 ASSERT_TRUE(
2750 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2751 ConnectFakeSignaling();
2752 caller()->CreateDataChannel();
2753 caller()->CreateAndSetAndSignalOffer();
2754 ASSERT_TRUE(caller()->data_channel() != nullptr);
2755 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2756 kDefaultTimeout, fake_clock);
2757 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2758 kDefaultTimeout, fake_clock);
2759 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2760 callee()->data_channel()->state(), kDefaultTimeout,
2761 fake_clock);
2762
2763 // Unregister the observer which is normally automatically registered.
2764 callee()->data_channel()->UnregisterObserver();
2765 // Send data and advance fake clock until it should have been received.
2766 std::string data = "hello world";
2767 caller()->data_channel()->Send(DataBuffer(data));
2768 SIMULATED_WAIT(false, 50, fake_clock);
2769
2770 // Attach data channel and expect data to be received immediately. Note that
2771 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2772 // further, but data can be received even if the callback is asynchronous.
2773 MockDataChannelObserver new_observer(callee()->data_channel());
2774 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2775 fake_clock);
2776}
2777
2778// This test sets up a call between two parties with audio, video and but only
2779// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002780TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002781 FakeConstraints setup_constraints_1;
2782 setup_constraints_1.SetAllowRtpDataChannels();
2783 // Must disable DTLS to make negotiation succeed.
2784 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2785 false);
2786 FakeConstraints setup_constraints_2;
2787 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2788 false);
2789 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2790 &setup_constraints_1, &setup_constraints_2));
2791 ConnectFakeSignaling();
2792 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002793 caller()->AddAudioVideoTracks();
2794 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002795 caller()->CreateAndSetAndSignalOffer();
2796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2797 // The caller should still have a data channel, but it should be closed, and
2798 // one should ever have been created for the callee.
2799 EXPECT_TRUE(caller()->data_channel() != nullptr);
2800 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2801 EXPECT_EQ(nullptr, callee()->data_channel());
2802}
2803
2804// This test sets up a call between two parties with audio, and video. When
2805// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002806TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002807 FakeConstraints setup_constraints;
2808 setup_constraints.SetAllowRtpDataChannels();
2809 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2810 &setup_constraints));
2811 ConnectFakeSignaling();
2812 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002813 caller()->AddAudioVideoTracks();
2814 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002815 caller()->CreateAndSetAndSignalOffer();
2816 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2817 // Create data channel and do new offer and answer.
2818 caller()->CreateDataChannel();
2819 caller()->CreateAndSetAndSignalOffer();
2820 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2821 ASSERT_NE(nullptr, caller()->data_channel());
2822 ASSERT_NE(nullptr, callee()->data_channel());
2823 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2824 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2825 // Ensure data can be sent in both directions.
2826 std::string data = "hello world";
2827 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2828 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2829 kDefaultTimeout);
2830 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2831 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2832 kDefaultTimeout);
2833}
2834
2835#ifdef HAVE_SCTP
2836
2837// This test sets up a call between two parties with audio, video and an SCTP
2838// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002839TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002840 ASSERT_TRUE(CreatePeerConnectionWrappers());
2841 ConnectFakeSignaling();
2842 // Expect that data channel created on caller side will show up for callee as
2843 // well.
2844 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002845 caller()->AddAudioVideoTracks();
2846 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002847 caller()->CreateAndSetAndSignalOffer();
2848 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2849 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002850 MediaExpectations media_expectations;
2851 media_expectations.ExpectBidirectionalAudioAndVideo();
2852 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002853 // Caller data channel should already exist (it created one). Callee data
2854 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2855 ASSERT_NE(nullptr, caller()->data_channel());
2856 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2857 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2858 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2859
2860 // Ensure data can be sent in both directions.
2861 std::string data = "hello world";
2862 caller()->data_channel()->Send(DataBuffer(data));
2863 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2864 kDefaultTimeout);
2865 callee()->data_channel()->Send(DataBuffer(data));
2866 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2867 kDefaultTimeout);
2868}
2869
2870// Ensure that when the callee closes an SCTP data channel, the closing
2871// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002872TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002873 // Same procedure as above test.
2874 ASSERT_TRUE(CreatePeerConnectionWrappers());
2875 ConnectFakeSignaling();
2876 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002877 caller()->AddAudioVideoTracks();
2878 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002879 caller()->CreateAndSetAndSignalOffer();
2880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2881 ASSERT_NE(nullptr, caller()->data_channel());
2882 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2883 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2884 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2885
2886 // Close the data channel on the callee side, and wait for it to reach the
2887 // "closed" state on both sides.
2888 callee()->data_channel()->Close();
2889 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2890 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2891}
2892
Seth Hampson2f0d7022018-02-20 11:54:42 -08002893TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07002894 ASSERT_TRUE(CreatePeerConnectionWrappers());
2895 ConnectFakeSignaling();
2896 webrtc::DataChannelInit init;
2897 init.id = 53;
2898 init.maxRetransmits = 52;
2899 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002900 caller()->AddAudioVideoTracks();
2901 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002902 caller()->CreateAndSetAndSignalOffer();
2903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002904 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2905 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002906 EXPECT_EQ(init.id, callee()->data_channel()->id());
2907 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2908 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2909 EXPECT_FALSE(callee()->data_channel()->negotiated());
2910}
2911
deadbeef1dcb1642017-03-29 21:08:16 -07002912// Test usrsctp's ability to process unordered data stream, where data actually
2913// arrives out of order using simulated delays. Previously there have been some
2914// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002915TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002916 // Introduce random network delays.
2917 // Otherwise it's not a true "unordered" test.
2918 virtual_socket_server()->set_delay_mean(20);
2919 virtual_socket_server()->set_delay_stddev(5);
2920 virtual_socket_server()->UpdateDelayDistribution();
2921 // Normal procedure, but with unordered data channel config.
2922 ASSERT_TRUE(CreatePeerConnectionWrappers());
2923 ConnectFakeSignaling();
2924 webrtc::DataChannelInit init;
2925 init.ordered = false;
2926 caller()->CreateDataChannel(&init);
2927 caller()->CreateAndSetAndSignalOffer();
2928 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2929 ASSERT_NE(nullptr, caller()->data_channel());
2930 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2931 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2932 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2933
2934 static constexpr int kNumMessages = 100;
2935 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2936 static constexpr size_t kMaxMessageSize = 4096;
2937 // Create and send random messages.
2938 std::vector<std::string> sent_messages;
2939 for (int i = 0; i < kNumMessages; ++i) {
2940 size_t length =
2941 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2942 std::string message;
2943 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2944 caller()->data_channel()->Send(DataBuffer(message));
2945 callee()->data_channel()->Send(DataBuffer(message));
2946 sent_messages.push_back(message);
2947 }
2948
2949 // Wait for all messages to be received.
2950 EXPECT_EQ_WAIT(kNumMessages,
2951 caller()->data_observer()->received_message_count(),
2952 kDefaultTimeout);
2953 EXPECT_EQ_WAIT(kNumMessages,
2954 callee()->data_observer()->received_message_count(),
2955 kDefaultTimeout);
2956
2957 // Sort and compare to make sure none of the messages were corrupted.
2958 std::vector<std::string> caller_received_messages =
2959 caller()->data_observer()->messages();
2960 std::vector<std::string> callee_received_messages =
2961 callee()->data_observer()->messages();
2962 std::sort(sent_messages.begin(), sent_messages.end());
2963 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2964 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2965 EXPECT_EQ(sent_messages, caller_received_messages);
2966 EXPECT_EQ(sent_messages, callee_received_messages);
2967}
2968
2969// This test sets up a call between two parties with audio, and video. When
2970// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002971TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002972 ASSERT_TRUE(CreatePeerConnectionWrappers());
2973 ConnectFakeSignaling();
2974 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002975 caller()->AddAudioVideoTracks();
2976 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002977 caller()->CreateAndSetAndSignalOffer();
2978 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2979 // Create data channel and do new offer and answer.
2980 caller()->CreateDataChannel();
2981 caller()->CreateAndSetAndSignalOffer();
2982 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2983 // Caller data channel should already exist (it created one). Callee data
2984 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2985 ASSERT_NE(nullptr, caller()->data_channel());
2986 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2987 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2988 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2989 // Ensure data can be sent in both directions.
2990 std::string data = "hello world";
2991 caller()->data_channel()->Send(DataBuffer(data));
2992 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2993 kDefaultTimeout);
2994 callee()->data_channel()->Send(DataBuffer(data));
2995 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2996 kDefaultTimeout);
2997}
2998
deadbeef7914b8c2017-04-21 03:23:33 -07002999// Set up a connection initially just using SCTP data channels, later upgrading
3000// to audio/video, ensuring frames are received end-to-end. Effectively the
3001// inverse of the test above.
3002// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003003TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003004 ASSERT_TRUE(CreatePeerConnectionWrappers());
3005 ConnectFakeSignaling();
3006 // Do initial offer/answer with just data channel.
3007 caller()->CreateDataChannel();
3008 caller()->CreateAndSetAndSignalOffer();
3009 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3010 // Wait until data can be sent over the data channel.
3011 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3012 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3013 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3014
3015 // Do subsequent offer/answer with two-way audio and video. Audio and video
3016 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003017 caller()->AddAudioVideoTracks();
3018 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003019 caller()->CreateAndSetAndSignalOffer();
3020 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003021 MediaExpectations media_expectations;
3022 media_expectations.ExpectBidirectionalAudioAndVideo();
3023 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003024}
3025
deadbeef8b7e9ad2017-05-25 09:38:55 -07003026static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003027 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003028 GetFirstDataContentDescription(desc);
3029 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003030 dcd_offer->set_use_sctpmap(false);
3031 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3032}
3033
3034// Test that the data channel works when a spec-compliant SCTP m= section is
3035// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3036// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003037TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003038 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3039 ASSERT_TRUE(CreatePeerConnectionWrappers());
3040 ConnectFakeSignaling();
3041 caller()->CreateDataChannel();
3042 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3043 caller()->CreateAndSetAndSignalOffer();
3044 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3045 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3046 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3047 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3048
3049 // Ensure data can be sent in both directions.
3050 std::string data = "hello world";
3051 caller()->data_channel()->Send(DataBuffer(data));
3052 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3053 kDefaultTimeout);
3054 callee()->data_channel()->Send(DataBuffer(data));
3055 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3056 kDefaultTimeout);
3057}
3058
deadbeef1dcb1642017-03-29 21:08:16 -07003059#endif // HAVE_SCTP
3060
3061// Test that the ICE connection and gathering states eventually reach
3062// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003063TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003064 ASSERT_TRUE(CreatePeerConnectionWrappers());
3065 ConnectFakeSignaling();
3066 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003067 caller()->AddAudioVideoTracks();
3068 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003069 caller()->CreateAndSetAndSignalOffer();
3070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3071 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3072 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3073 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3074 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3075 // After the best candidate pair is selected and all candidates are signaled,
3076 // the ICE connection state should reach "complete".
3077 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3078 // answerer/"callee" by default) only reaches "connected". When this is
3079 // fixed, this test should be updated.
3080 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3081 caller()->ice_connection_state(), kDefaultTimeout);
3082 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3083 callee()->ice_connection_state(), kDefaultTimeout);
3084}
3085
Steve Antonede9ca52017-10-16 13:04:27 -07003086// Test that firewalling the ICE connection causes the clients to identify the
3087// disconnected state and then removing the firewall causes them to reconnect.
3088class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003089 : public PeerConnectionIntegrationBaseTest,
3090 public ::testing::WithParamInterface<
3091 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003092 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003093 PeerConnectionIntegrationIceStatesTest()
3094 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3095 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003096 }
3097
3098 void StartStunServer(const SocketAddress& server_address) {
3099 stun_server_.reset(
3100 cricket::TestStunServer::Create(network_thread(), server_address));
3101 }
3102
3103 bool TestIPv6() {
3104 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3105 }
3106
3107 void SetPortAllocatorFlags() {
3108 caller()->port_allocator()->set_flags(port_allocator_flags_);
3109 callee()->port_allocator()->set_flags(port_allocator_flags_);
3110 }
3111
3112 std::vector<SocketAddress> CallerAddresses() {
3113 std::vector<SocketAddress> addresses;
3114 addresses.push_back(SocketAddress("1.1.1.1", 0));
3115 if (TestIPv6()) {
3116 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3117 }
3118 return addresses;
3119 }
3120
3121 std::vector<SocketAddress> CalleeAddresses() {
3122 std::vector<SocketAddress> addresses;
3123 addresses.push_back(SocketAddress("2.2.2.2", 0));
3124 if (TestIPv6()) {
3125 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3126 }
3127 return addresses;
3128 }
3129
3130 void SetUpNetworkInterfaces() {
3131 // Remove the default interfaces added by the test infrastructure.
3132 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3133 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3134
3135 // Add network addresses for test.
3136 for (const auto& caller_address : CallerAddresses()) {
3137 caller()->network()->AddInterface(caller_address);
3138 }
3139 for (const auto& callee_address : CalleeAddresses()) {
3140 callee()->network()->AddInterface(callee_address);
3141 }
3142 }
3143
3144 private:
3145 uint32_t port_allocator_flags_;
3146 std::unique_ptr<cricket::TestStunServer> stun_server_;
3147};
3148
3149// Tests that the PeerConnection goes through all the ICE gathering/connection
3150// states over the duration of the call. This includes Disconnected and Failed
3151// states, induced by putting a firewall between the peers and waiting for them
3152// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003153TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3154 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3155 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3156 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003157
3158 const SocketAddress kStunServerAddress =
3159 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3160 StartStunServer(kStunServerAddress);
3161
3162 PeerConnectionInterface::RTCConfiguration config;
3163 PeerConnectionInterface::IceServer ice_stun_server;
3164 ice_stun_server.urls.push_back(
3165 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3166 kStunServerAddress.PortAsString());
3167 config.servers.push_back(ice_stun_server);
3168
3169 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3170 ConnectFakeSignaling();
3171 SetPortAllocatorFlags();
3172 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003173 caller()->AddAudioVideoTracks();
3174 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003175
3176 // Initial state before anything happens.
3177 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3178 caller()->ice_gathering_state());
3179 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3180 caller()->ice_connection_state());
3181
3182 // Start the call by creating the offer, setting it as the local description,
3183 // then sending it to the peer who will respond with an answer. This happens
3184 // asynchronously so that we can watch the states as it runs in the
3185 // background.
3186 caller()->CreateAndSetAndSignalOffer();
3187
Steve Anton83119dd2017-11-10 16:19:52 -08003188 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3189 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003190
3191 // Verify that the observer was notified of the intermediate transitions.
3192 EXPECT_THAT(caller()->ice_connection_state_history(),
3193 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3194 PeerConnectionInterface::kIceConnectionConnected,
3195 PeerConnectionInterface::kIceConnectionCompleted));
3196 EXPECT_THAT(caller()->ice_gathering_state_history(),
3197 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3198 PeerConnectionInterface::kIceGatheringComplete));
3199
3200 // Block connections to/from the caller and wait for ICE to become
3201 // disconnected.
3202 for (const auto& caller_address : CallerAddresses()) {
3203 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3204 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003205 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003206 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3207 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003208
3209 // Let ICE re-establish by removing the firewall rules.
3210 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003211 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003212 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3213 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003214
3215 // According to RFC7675, if there is no response within 30 seconds then the
3216 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003217 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003218 constexpr int kConsentTimeout = 30000;
3219 for (const auto& caller_address : CallerAddresses()) {
3220 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3221 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003222 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003223 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3224 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003225}
3226
3227// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3228// and that the statistics in the metric observers are updated correctly.
3229TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3230 ASSERT_TRUE(CreatePeerConnectionWrappers());
3231 ConnectFakeSignaling();
3232 SetPortAllocatorFlags();
3233 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003234 caller()->AddAudioVideoTracks();
3235 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003236
3237 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3238 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3239 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3240
3241 caller()->CreateAndSetAndSignalOffer();
3242
3243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3244
3245 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3246 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3247 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3248 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3249 if (TestIPv6()) {
3250 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3251 // connection.
3252 EXPECT_EQ(0u, num_best_ipv4);
3253 EXPECT_EQ(1u, num_best_ipv6);
3254 } else {
3255 EXPECT_EQ(1u, num_best_ipv4);
3256 EXPECT_EQ(0u, num_best_ipv6);
3257 }
3258
3259 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3260 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3261 webrtc::kIceCandidatePairHostHost));
3262 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3263 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3264 webrtc::kIceCandidatePairHostPublicHostPublic));
3265}
3266
3267constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3268 cricket::PORTALLOCATOR_DISABLE_STUN |
3269 cricket::PORTALLOCATOR_DISABLE_RELAY;
3270constexpr uint32_t kFlagsIPv6NoStun =
3271 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3272 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3273constexpr uint32_t kFlagsIPv4Stun =
3274 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3275
Seth Hampson2f0d7022018-02-20 11:54:42 -08003276INSTANTIATE_TEST_CASE_P(
3277 PeerConnectionIntegrationTest,
3278 PeerConnectionIntegrationIceStatesTest,
3279 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3280 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3281 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3282 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003283
deadbeef1dcb1642017-03-29 21:08:16 -07003284// This test sets up a call between two parties with audio and video.
3285// During the call, the caller restarts ICE and the test verifies that
3286// new ICE candidates are generated and audio and video still can flow, and the
3287// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003288TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003289 ASSERT_TRUE(CreatePeerConnectionWrappers());
3290 ConnectFakeSignaling();
3291 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003292 caller()->AddAudioVideoTracks();
3293 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003294 caller()->CreateAndSetAndSignalOffer();
3295 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3296 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3297 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3298 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3299 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3300
3301 // To verify that the ICE restart actually occurs, get
3302 // ufrag/password/candidates before and after restart.
3303 // Create an SDP string of the first audio candidate for both clients.
3304 const webrtc::IceCandidateCollection* audio_candidates_caller =
3305 caller()->pc()->local_description()->candidates(0);
3306 const webrtc::IceCandidateCollection* audio_candidates_callee =
3307 callee()->pc()->local_description()->candidates(0);
3308 ASSERT_GT(audio_candidates_caller->count(), 0u);
3309 ASSERT_GT(audio_candidates_callee->count(), 0u);
3310 std::string caller_candidate_pre_restart;
3311 ASSERT_TRUE(
3312 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3313 std::string callee_candidate_pre_restart;
3314 ASSERT_TRUE(
3315 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3316 const cricket::SessionDescription* desc =
3317 caller()->pc()->local_description()->description();
3318 std::string caller_ufrag_pre_restart =
3319 desc->transport_infos()[0].description.ice_ufrag;
3320 desc = callee()->pc()->local_description()->description();
3321 std::string callee_ufrag_pre_restart =
3322 desc->transport_infos()[0].description.ice_ufrag;
3323
3324 // Have the caller initiate an ICE restart.
3325 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3326 caller()->CreateAndSetAndSignalOffer();
3327 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3328 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3329 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3330 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3331 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3332
3333 // Grab the ufrags/candidates again.
3334 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3335 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3336 ASSERT_GT(audio_candidates_caller->count(), 0u);
3337 ASSERT_GT(audio_candidates_callee->count(), 0u);
3338 std::string caller_candidate_post_restart;
3339 ASSERT_TRUE(
3340 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3341 std::string callee_candidate_post_restart;
3342 ASSERT_TRUE(
3343 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3344 desc = caller()->pc()->local_description()->description();
3345 std::string caller_ufrag_post_restart =
3346 desc->transport_infos()[0].description.ice_ufrag;
3347 desc = callee()->pc()->local_description()->description();
3348 std::string callee_ufrag_post_restart =
3349 desc->transport_infos()[0].description.ice_ufrag;
3350 // Sanity check that an ICE restart was actually negotiated in SDP.
3351 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3352 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3353 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3354 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3355
3356 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003357 MediaExpectations media_expectations;
3358 media_expectations.ExpectBidirectionalAudioAndVideo();
3359 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003360}
3361
3362// Verify that audio/video can be received end-to-end when ICE renomination is
3363// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003364TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003365 PeerConnectionInterface::RTCConfiguration config;
3366 config.enable_ice_renomination = true;
3367 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3368 ConnectFakeSignaling();
3369 // Do normal offer/answer and wait for some frames to be received in each
3370 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003371 caller()->AddAudioVideoTracks();
3372 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003373 caller()->CreateAndSetAndSignalOffer();
3374 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3375 // Sanity check that ICE renomination was actually negotiated.
3376 const cricket::SessionDescription* desc =
3377 caller()->pc()->local_description()->description();
3378 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003379 ASSERT_NE(
3380 info.description.transport_options.end(),
3381 std::find(info.description.transport_options.begin(),
3382 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003383 }
3384 desc = callee()->pc()->local_description()->description();
3385 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003386 ASSERT_NE(
3387 info.description.transport_options.end(),
3388 std::find(info.description.transport_options.begin(),
3389 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003390 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003391 MediaExpectations media_expectations;
3392 media_expectations.ExpectBidirectionalAudioAndVideo();
3393 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003394}
3395
Steve Anton6f25b092017-10-23 09:39:20 -07003396// With a max bundle policy and RTCP muxing, adding a new media description to
3397// the connection should not affect ICE at all because the new media will use
3398// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003399TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003400 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003401 PeerConnectionInterface::RTCConfiguration config;
3402 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3403 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3404 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3405 config, PeerConnectionInterface::RTCConfiguration()));
3406 ConnectFakeSignaling();
3407
Steve Anton15324772018-01-16 10:26:49 -08003408 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003409 caller()->CreateAndSetAndSignalOffer();
3410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003411 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3412 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003413
3414 caller()->clear_ice_connection_state_history();
3415
Steve Anton15324772018-01-16 10:26:49 -08003416 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003417 caller()->CreateAndSetAndSignalOffer();
3418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3419
3420 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3421}
3422
deadbeef1dcb1642017-03-29 21:08:16 -07003423// This test sets up a call between two parties with audio and video. It then
3424// renegotiates setting the video m-line to "port 0", then later renegotiates
3425// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003426TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003427 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3428 ASSERT_TRUE(CreatePeerConnectionWrappers());
3429 ConnectFakeSignaling();
3430
3431 // Do initial negotiation, only sending media from the caller. Will result in
3432 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003433 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003434 caller()->CreateAndSetAndSignalOffer();
3435 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3436
3437 // Negotiate again, disabling the video "m=" section (the callee will set the
3438 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003439 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3440 PeerConnectionInterface::RTCOfferAnswerOptions options;
3441 options.offer_to_receive_video = 0;
3442 callee()->SetOfferAnswerOptions(options);
3443 } else {
3444 callee()->SetRemoteOfferHandler([this] {
3445 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3446 });
3447 }
deadbeef1dcb1642017-03-29 21:08:16 -07003448 caller()->CreateAndSetAndSignalOffer();
3449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450 // Sanity check that video "m=" section was actually rejected.
3451 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3452 callee()->pc()->local_description()->description());
3453 ASSERT_NE(nullptr, answer_video_content);
3454 ASSERT_TRUE(answer_video_content->rejected);
3455
3456 // Enable video and do negotiation again, making sure video is received
3457 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003458 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3459 PeerConnectionInterface::RTCOfferAnswerOptions options;
3460 options.offer_to_receive_video = 1;
3461 callee()->SetOfferAnswerOptions(options);
3462 } else {
3463 // The caller's transceiver is stopped, so we need to add another track.
3464 auto caller_transceiver =
3465 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3466 EXPECT_TRUE(caller_transceiver->stopped());
3467 caller()->AddVideoTrack();
3468 }
3469 callee()->AddVideoTrack();
3470 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003471 caller()->CreateAndSetAndSignalOffer();
3472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003473
deadbeef1dcb1642017-03-29 21:08:16 -07003474 // Verify the caller receives frames from the newly added stream, and the
3475 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003476 MediaExpectations media_expectations;
3477 media_expectations.CalleeExpectsSomeAudio();
3478 media_expectations.ExpectBidirectionalVideo();
3479 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003480}
3481
3482// This test sets up a Jsep call between two parties with external
3483// VideoDecoderFactory.
3484// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3485// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003486TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003487 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3488 ASSERT_TRUE(CreatePeerConnectionWrappers());
3489 EnableVideoDecoderFactory();
3490 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003491 caller()->AddAudioVideoTracks();
3492 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003493 caller()->CreateAndSetAndSignalOffer();
3494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003495 MediaExpectations media_expectations;
3496 media_expectations.ExpectBidirectionalAudioAndVideo();
3497 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003498}
3499
3500// This tests that if we negotiate after calling CreateSender but before we
3501// have a track, then set a track later, frames from the newly-set track are
3502// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003503TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003504 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3505 ASSERT_TRUE(CreatePeerConnectionWrappers());
3506 ConnectFakeSignaling();
3507 auto caller_audio_sender =
3508 caller()->pc()->CreateSender("audio", "caller_stream");
3509 auto caller_video_sender =
3510 caller()->pc()->CreateSender("video", "caller_stream");
3511 auto callee_audio_sender =
3512 callee()->pc()->CreateSender("audio", "callee_stream");
3513 auto callee_video_sender =
3514 callee()->pc()->CreateSender("video", "callee_stream");
3515 caller()->CreateAndSetAndSignalOffer();
3516 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3517 // Wait for ICE to complete, without any tracks being set.
3518 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3519 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3520 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3521 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3522 // Now set the tracks, and expect frames to immediately start flowing.
3523 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3524 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3525 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3526 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003527 MediaExpectations media_expectations;
3528 media_expectations.ExpectBidirectionalAudioAndVideo();
3529 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3530}
3531
3532// This tests that if we negotiate after calling AddTransceiver but before we
3533// have a track, then set a track later, frames from the newly-set tracks are
3534// received end-to-end.
3535TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3536 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3537 ASSERT_TRUE(CreatePeerConnectionWrappers());
3538 ConnectFakeSignaling();
3539 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3540 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3541 auto caller_audio_sender = audio_result.MoveValue()->sender();
3542 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3543 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3544 auto caller_video_sender = video_result.MoveValue()->sender();
3545 callee()->SetRemoteOfferHandler([this] {
3546 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3547 callee()->pc()->GetTransceivers()[0]->SetDirection(
3548 RtpTransceiverDirection::kSendRecv);
3549 callee()->pc()->GetTransceivers()[1]->SetDirection(
3550 RtpTransceiverDirection::kSendRecv);
3551 });
3552 caller()->CreateAndSetAndSignalOffer();
3553 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3554 // Wait for ICE to complete, without any tracks being set.
3555 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3556 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3557 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3558 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3559 // Now set the tracks, and expect frames to immediately start flowing.
3560 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3561 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3562 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3563 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3564 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3565 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3566 MediaExpectations media_expectations;
3567 media_expectations.ExpectBidirectionalAudioAndVideo();
3568 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003569}
3570
3571// This test verifies that a remote video track can be added via AddStream,
3572// and sent end-to-end. For this particular test, it's simply echoed back
3573// from the caller to the callee, rather than being forwarded to a third
3574// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003575TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003576 ASSERT_TRUE(CreatePeerConnectionWrappers());
3577 ConnectFakeSignaling();
3578 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003579 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003580 caller()->CreateAndSetAndSignalOffer();
3581 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3582 ASSERT_EQ(1, callee()->remote_streams()->count());
3583
3584 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3585 // time).
3586 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3587 callee()->CreateAndSetAndSignalOffer();
3588 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3589
Seth Hampson2f0d7022018-02-20 11:54:42 -08003590 MediaExpectations media_expectations;
3591 media_expectations.ExpectBidirectionalVideo();
3592 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003593}
3594
3595// Test that we achieve the expected end-to-end connection time, using a
3596// fake clock and simulated latency on the media and signaling paths.
3597// We use a TURN<->TURN connection because this is usually the quickest to
3598// set up initially, especially when we're confident the connection will work
3599// and can start sending media before we get a STUN response.
3600//
3601// With various optimizations enabled, here are the network delays we expect to
3602// be on the critical path:
3603// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3604// signaling answer (with DTLS fingerprint).
3605// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3606// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3607// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003608TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003609 rtc::ScopedFakeClock fake_clock;
3610 // Some things use a time of "0" as a special value, so we need to start out
3611 // the fake clock at a nonzero time.
3612 // TODO(deadbeef): Fix this.
3613 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3614
3615 static constexpr int media_hop_delay_ms = 50;
3616 static constexpr int signaling_trip_delay_ms = 500;
3617 // For explanation of these values, see comment above.
3618 static constexpr int required_media_hops = 9;
3619 static constexpr int required_signaling_trips = 2;
3620 // For internal delays (such as posting an event asychronously).
3621 static constexpr int allowed_internal_delay_ms = 20;
3622 static constexpr int total_connection_time_ms =
3623 media_hop_delay_ms * required_media_hops +
3624 signaling_trip_delay_ms * required_signaling_trips +
3625 allowed_internal_delay_ms;
3626
3627 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3628 3478};
3629 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3630 0};
3631 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3632 3478};
3633 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3634 0};
3635 cricket::TestTurnServer turn_server_1(network_thread(),
3636 turn_server_1_internal_address,
3637 turn_server_1_external_address);
3638 cricket::TestTurnServer turn_server_2(network_thread(),
3639 turn_server_2_internal_address,
3640 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003641
deadbeef1dcb1642017-03-29 21:08:16 -07003642 // Bypass permission check on received packets so media can be sent before
3643 // the candidate is signaled.
3644 turn_server_1.set_enable_permission_checks(false);
3645 turn_server_2.set_enable_permission_checks(false);
3646
3647 PeerConnectionInterface::RTCConfiguration client_1_config;
3648 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3649 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3650 ice_server_1.username = "test";
3651 ice_server_1.password = "test";
3652 client_1_config.servers.push_back(ice_server_1);
3653 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3654 client_1_config.presume_writable_when_fully_relayed = true;
3655
3656 PeerConnectionInterface::RTCConfiguration client_2_config;
3657 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3658 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3659 ice_server_2.username = "test";
3660 ice_server_2.password = "test";
3661 client_2_config.servers.push_back(ice_server_2);
3662 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3663 client_2_config.presume_writable_when_fully_relayed = true;
3664
3665 ASSERT_TRUE(
3666 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3667 // Set up the simulated delays.
3668 SetSignalingDelayMs(signaling_trip_delay_ms);
3669 ConnectFakeSignaling();
3670 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3671 virtual_socket_server()->UpdateDelayDistribution();
3672
3673 // Set "offer to receive audio/video" without adding any tracks, so we just
3674 // set up ICE/DTLS with no media.
3675 PeerConnectionInterface::RTCOfferAnswerOptions options;
3676 options.offer_to_receive_audio = 1;
3677 options.offer_to_receive_video = 1;
3678 caller()->SetOfferAnswerOptions(options);
3679 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003680 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3681 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003682 // Need to free the clients here since they're using things we created on
3683 // the stack.
3684 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3685 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3686}
3687
Jonas Orelandbdcee282017-10-10 14:01:40 +02003688// Verify that a TurnCustomizer passed in through RTCConfiguration
3689// is actually used by the underlying TURN candidate pair.
3690// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003691TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003692 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3693 3478};
3694 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3695 0};
3696 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3697 3478};
3698 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3699 0};
3700 cricket::TestTurnServer turn_server_1(network_thread(),
3701 turn_server_1_internal_address,
3702 turn_server_1_external_address);
3703 cricket::TestTurnServer turn_server_2(network_thread(),
3704 turn_server_2_internal_address,
3705 turn_server_2_external_address);
3706
3707 PeerConnectionInterface::RTCConfiguration client_1_config;
3708 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3709 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3710 ice_server_1.username = "test";
3711 ice_server_1.password = "test";
3712 client_1_config.servers.push_back(ice_server_1);
3713 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3714 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3715 client_1_config.turn_customizer = customizer1.get();
3716
3717 PeerConnectionInterface::RTCConfiguration client_2_config;
3718 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3719 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3720 ice_server_2.username = "test";
3721 ice_server_2.password = "test";
3722 client_2_config.servers.push_back(ice_server_2);
3723 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3724 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3725 client_2_config.turn_customizer = customizer2.get();
3726
3727 ASSERT_TRUE(
3728 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3729 ConnectFakeSignaling();
3730
3731 // Set "offer to receive audio/video" without adding any tracks, so we just
3732 // set up ICE/DTLS with no media.
3733 PeerConnectionInterface::RTCOfferAnswerOptions options;
3734 options.offer_to_receive_audio = 1;
3735 options.offer_to_receive_video = 1;
3736 caller()->SetOfferAnswerOptions(options);
3737 caller()->CreateAndSetAndSignalOffer();
3738 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3739
3740 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3741 EXPECT_GT(customizer1->modify_cnt_, 0u);
3742
3743 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3744 EXPECT_GT(customizer2->modify_cnt_, 0u);
3745
3746 // Need to free the clients here since they're using things we created on
3747 // the stack.
3748 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3749 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3750}
3751
deadbeefc964d0b2017-04-03 10:03:35 -07003752// Test that audio and video flow end-to-end when codec names don't use the
3753// expected casing, given that they're supposed to be case insensitive. To test
3754// this, all but one codec is removed from each media description, and its
3755// casing is changed.
3756//
3757// In the past, this has regressed and caused crashes/black video, due to the
3758// fact that code at some layers was doing case-insensitive comparisons and
3759// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003760TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003761 ASSERT_TRUE(CreatePeerConnectionWrappers());
3762 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003763 caller()->AddAudioVideoTracks();
3764 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003765
3766 // Remove all but one audio/video codec (opus and VP8), and change the
3767 // casing of the caller's generated offer.
3768 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3769 cricket::AudioContentDescription* audio =
3770 GetFirstAudioContentDescription(description);
3771 ASSERT_NE(nullptr, audio);
3772 auto audio_codecs = audio->codecs();
3773 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3774 [](const cricket::AudioCodec& codec) {
3775 return codec.name != "opus";
3776 }),
3777 audio_codecs.end());
3778 ASSERT_EQ(1u, audio_codecs.size());
3779 audio_codecs[0].name = "OpUs";
3780 audio->set_codecs(audio_codecs);
3781
3782 cricket::VideoContentDescription* video =
3783 GetFirstVideoContentDescription(description);
3784 ASSERT_NE(nullptr, video);
3785 auto video_codecs = video->codecs();
3786 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3787 [](const cricket::VideoCodec& codec) {
3788 return codec.name != "VP8";
3789 }),
3790 video_codecs.end());
3791 ASSERT_EQ(1u, video_codecs.size());
3792 video_codecs[0].name = "vP8";
3793 video->set_codecs(video_codecs);
3794 });
3795
3796 caller()->CreateAndSetAndSignalOffer();
3797 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3798
3799 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003800 MediaExpectations media_expectations;
3801 media_expectations.ExpectBidirectionalAudioAndVideo();
3802 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003803}
3804
Seth Hampson2f0d7022018-02-20 11:54:42 -08003805TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003806 ASSERT_TRUE(CreatePeerConnectionWrappers());
3807 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003808 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003809 caller()->CreateAndSetAndSignalOffer();
3810 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003811 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003812 MediaExpectations media_expectations;
3813 media_expectations.CalleeExpectsSomeAudio(1);
3814 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003815 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3816 auto receiver = callee()->pc()->GetReceivers()[0];
3817 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3818
3819 auto contributing_sources = receiver->GetSources();
3820 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3821 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3822 contributing_sources[0].source_id());
3823}
3824
deadbeef2f425aa2017-04-14 10:41:32 -07003825// Test that if a track is removed and added again with a different stream ID,
3826// the new stream ID is successfully communicated in SDP and media continues to
3827// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003828// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3829// it will not reuse a transceiver that has already been sending. After creating
3830// a new transceiver it tries to create an offer with two senders of the same
3831// track ids and it fails.
3832TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003833 ASSERT_TRUE(CreatePeerConnectionWrappers());
3834 ConnectFakeSignaling();
3835
3836 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3837 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3838 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3839 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3840
3841 // Add track using stream 1, do offer/answer.
3842 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3843 caller()->CreateLocalAudioTrack();
3844 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3845 caller()->pc()->AddTrack(track, {stream_1.get()});
3846 caller()->CreateAndSetAndSignalOffer();
3847 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003848 {
3849 MediaExpectations media_expectations;
3850 media_expectations.CalleeExpectsSomeAudio(1);
3851 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3852 }
deadbeef2f425aa2017-04-14 10:41:32 -07003853 // Remove the sender, and create a new one with the new stream.
3854 caller()->pc()->RemoveTrack(sender);
3855 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3856 caller()->CreateAndSetAndSignalOffer();
3857 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3858 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003859 {
3860 MediaExpectations media_expectations;
3861 media_expectations.CalleeExpectsSomeAudio();
3862 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3863 }
deadbeef2f425aa2017-04-14 10:41:32 -07003864}
3865
Seth Hampson2f0d7022018-02-20 11:54:42 -08003866TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003867 ASSERT_TRUE(CreatePeerConnectionWrappers());
3868 ConnectFakeSignaling();
3869
3870 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3871 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3872 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3873 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003874 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3875 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003876
Steve Anton15324772018-01-16 10:26:49 -08003877 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003878 caller()->CreateAndSetAndSignalOffer();
3879 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3880}
3881
Steve Antonede9ca52017-10-16 13:04:27 -07003882// Test that if candidates are only signaled by applying full session
3883// descriptions (instead of using AddIceCandidate), the peers can connect to
3884// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003885TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07003886 ASSERT_TRUE(CreatePeerConnectionWrappers());
3887 // Each side will signal the session descriptions but not candidates.
3888 ConnectFakeSignalingForSdpOnly();
3889
3890 // Add audio video track and exchange the initial offer/answer with media
3891 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003892 caller()->AddAudioVideoTracks();
3893 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003894 caller()->CreateAndSetAndSignalOffer();
3895
3896 // Wait for all candidates to be gathered on both the caller and callee.
3897 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3898 caller()->ice_gathering_state(), kDefaultTimeout);
3899 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3900 callee()->ice_gathering_state(), kDefaultTimeout);
3901
3902 // The candidates will now be included in the session description, so
3903 // signaling them will start the ICE connection.
3904 caller()->CreateAndSetAndSignalOffer();
3905 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3906
3907 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003908 MediaExpectations media_expectations;
3909 media_expectations.ExpectBidirectionalAudioAndVideo();
3910 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003911}
3912
henrika5f6bf242017-11-01 11:06:56 +01003913// Test that SetAudioPlayout can be used to disable audio playout from the
3914// start, then later enable it. This may be useful, for example, if the caller
3915// needs to play a local ringtone until some event occurs, after which it
3916// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003917TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003918 ASSERT_TRUE(CreatePeerConnectionWrappers());
3919 ConnectFakeSignaling();
3920
3921 // Set up audio-only call where audio playout is disabled on caller's side.
3922 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003923 caller()->AddAudioTrack();
3924 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003925 caller()->CreateAndSetAndSignalOffer();
3926 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3927
3928 // Pump messages for a second.
3929 WAIT(false, 1000);
3930 // Since audio playout is disabled, the caller shouldn't have received
3931 // anything (at the playout level, at least).
3932 EXPECT_EQ(0, caller()->audio_frames_received());
3933 // As a sanity check, make sure the callee (for which playout isn't disabled)
3934 // did still see frames on its audio level.
3935 ASSERT_GT(callee()->audio_frames_received(), 0);
3936
3937 // Enable playout again, and ensure audio starts flowing.
3938 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003939 MediaExpectations media_expectations;
3940 media_expectations.ExpectBidirectionalAudio();
3941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003942}
3943
3944double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3945 auto report = pc->NewGetStats();
3946 auto track_stats_list =
3947 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3948 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3949 for (const auto* track_stats : track_stats_list) {
3950 if (track_stats->remote_source.is_defined() &&
3951 *track_stats->remote_source) {
3952 remote_track_stats = track_stats;
3953 break;
3954 }
3955 }
3956
3957 if (!remote_track_stats->total_audio_energy.is_defined()) {
3958 return 0.0;
3959 }
3960 return *remote_track_stats->total_audio_energy;
3961}
3962
3963// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3964// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003965TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01003966 DisableAudioPlayoutStillGeneratesAudioStats) {
3967 ASSERT_TRUE(CreatePeerConnectionWrappers());
3968 ConnectFakeSignaling();
3969
3970 // Set up audio-only call where playout is disabled but audio-processing is
3971 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003972 caller()->AddAudioTrack();
3973 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003974 caller()->pc()->SetAudioPlayout(false);
3975
3976 caller()->CreateAndSetAndSignalOffer();
3977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3978
3979 // Wait for the callee to receive audio stats.
3980 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3981}
3982
henrika4f167df2017-11-01 14:45:55 +01003983// Test that SetAudioRecording can be used to disable audio recording from the
3984// start, then later enable it. This may be useful, for example, if the caller
3985// wants to ensure that no audio resources are active before a certain state
3986// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003987TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01003988 ASSERT_TRUE(CreatePeerConnectionWrappers());
3989 ConnectFakeSignaling();
3990
3991 // Set up audio-only call where audio recording is disabled on caller's side.
3992 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003993 caller()->AddAudioTrack();
3994 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003995 caller()->CreateAndSetAndSignalOffer();
3996 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3997
3998 // Pump messages for a second.
3999 WAIT(false, 1000);
4000 // Since caller has disabled audio recording, the callee shouldn't have
4001 // received anything.
4002 EXPECT_EQ(0, callee()->audio_frames_received());
4003 // As a sanity check, make sure the caller did still see frames on its
4004 // audio level since audio recording is enabled on the calle side.
4005 ASSERT_GT(caller()->audio_frames_received(), 0);
4006
4007 // Enable audio recording again, and ensure audio starts flowing.
4008 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004009 MediaExpectations media_expectations;
4010 media_expectations.ExpectBidirectionalAudio();
4011 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004012}
4013
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004014// Test that after closing PeerConnections, they stop sending any packets (ICE,
4015// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004016TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004017 // Set up audio/video/data, wait for some frames to be received.
4018 ASSERT_TRUE(CreatePeerConnectionWrappers());
4019 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004020 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004021#ifdef HAVE_SCTP
4022 caller()->CreateDataChannel();
4023#endif
4024 caller()->CreateAndSetAndSignalOffer();
4025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004026 MediaExpectations media_expectations;
4027 media_expectations.CalleeExpectsSomeAudioAndVideo();
4028 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004029 // Close PeerConnections.
4030 caller()->pc()->Close();
4031 callee()->pc()->Close();
4032 // Pump messages for a second, and ensure no new packets end up sent.
4033 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4034 WAIT(false, 1000);
4035 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4036 EXPECT_EQ(sent_packets_a, sent_packets_b);
4037}
4038
Seth Hampson2f0d7022018-02-20 11:54:42 -08004039INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4040 PeerConnectionIntegrationTest,
4041 Values(SdpSemantics::kPlanB,
4042 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004043
Steve Anton74255ff2018-01-24 18:32:57 -08004044// Tests that verify interoperability between Plan B and Unified Plan
4045// PeerConnections.
4046class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004047 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004048 public ::testing::WithParamInterface<
4049 std::tuple<SdpSemantics, SdpSemantics>> {
4050 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004051 // Setting the SdpSemantics for the base test to kDefault does not matter
4052 // because we specify not to use the test semantics when creating
4053 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004054 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004055 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4056 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004057 callee_semantics_(std::get<1>(GetParam())) {}
4058
4059 bool CreatePeerConnectionWrappersWithSemantics() {
4060 RTCConfiguration caller_config;
4061 caller_config.sdp_semantics = caller_semantics_;
4062 RTCConfiguration callee_config;
4063 callee_config.sdp_semantics = callee_semantics_;
4064 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4065 }
4066
4067 const SdpSemantics caller_semantics_;
4068 const SdpSemantics callee_semantics_;
4069};
4070
4071TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4072 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4073 ConnectFakeSignaling();
4074
4075 caller()->CreateAndSetAndSignalOffer();
4076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4077}
4078
4079TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4080 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4081 ConnectFakeSignaling();
4082 auto audio_sender = caller()->AddAudioTrack();
4083
4084 caller()->CreateAndSetAndSignalOffer();
4085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4086
4087 // Verify that one audio receiver has been created on the remote and that it
4088 // has the same track ID as the sending track.
4089 auto receivers = callee()->pc()->GetReceivers();
4090 ASSERT_EQ(1u, receivers.size());
4091 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4092 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4093
Seth Hampson2f0d7022018-02-20 11:54:42 -08004094 MediaExpectations media_expectations;
4095 media_expectations.CalleeExpectsSomeAudio();
4096 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004097}
4098
4099TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4100 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4101 ConnectFakeSignaling();
4102 auto video_sender = caller()->AddVideoTrack();
4103 auto audio_sender = caller()->AddAudioTrack();
4104
4105 caller()->CreateAndSetAndSignalOffer();
4106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4107
4108 // Verify that one audio and one video receiver have been created on the
4109 // remote and that they have the same track IDs as the sending tracks.
4110 auto audio_receivers =
4111 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4112 ASSERT_EQ(1u, audio_receivers.size());
4113 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4114 auto video_receivers =
4115 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4116 ASSERT_EQ(1u, video_receivers.size());
4117 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4118
Seth Hampson2f0d7022018-02-20 11:54:42 -08004119 MediaExpectations media_expectations;
4120 media_expectations.CalleeExpectsSomeAudioAndVideo();
4121 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004122}
4123
4124TEST_P(PeerConnectionIntegrationInteropTest,
4125 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4126 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4127 ConnectFakeSignaling();
4128 caller()->AddAudioVideoTracks();
4129 callee()->AddAudioVideoTracks();
4130
4131 caller()->CreateAndSetAndSignalOffer();
4132 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4133
Seth Hampson2f0d7022018-02-20 11:54:42 -08004134 MediaExpectations media_expectations;
4135 media_expectations.ExpectBidirectionalAudioAndVideo();
4136 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004137}
4138
4139TEST_P(PeerConnectionIntegrationInteropTest,
4140 ReverseRolesOneAudioLocalToOneVideoRemote) {
4141 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4142 ConnectFakeSignaling();
4143 caller()->AddAudioTrack();
4144 callee()->AddVideoTrack();
4145
4146 caller()->CreateAndSetAndSignalOffer();
4147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4148
4149 // Verify that only the audio track has been negotiated.
4150 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4151 // Might also check that the callee's NegotiationNeeded flag is set.
4152
4153 // Reverse roles.
4154 callee()->CreateAndSetAndSignalOffer();
4155 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4156
Seth Hampson2f0d7022018-02-20 11:54:42 -08004157 MediaExpectations media_expectations;
4158 media_expectations.CallerExpectsSomeVideo();
4159 media_expectations.CalleeExpectsSomeAudio();
4160 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004161}
4162
4163// Test that if one side offers two video tracks then the other side will only
4164// see the first one and ignore the second.
4165TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToNoMediaRemote) {
4166 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4167 ConnectFakeSignaling();
4168 auto first_sender = caller()->AddVideoTrack();
4169 caller()->AddVideoTrack();
4170
4171 caller()->CreateAndSetAndSignalOffer();
4172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4173
4174 // Verify that there is only one receiver and it corresponds to the first
4175 // added track.
4176 auto receivers = callee()->pc()->GetReceivers();
4177 ASSERT_EQ(1u, receivers.size());
4178 EXPECT_TRUE(receivers[0]->track()->enabled());
4179 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4180
Seth Hampson2f0d7022018-02-20 11:54:42 -08004181 MediaExpectations media_expectations;
4182 media_expectations.CalleeExpectsSomeVideo();
4183 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004184}
4185
4186// Test that in the multi-track case each endpoint only looks at the first track
4187// and ignores the second one.
4188TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToTwoVideoRemote) {
4189 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4190 ConnectFakeSignaling();
4191 caller()->AddVideoTrack();
4192 caller()->AddVideoTrack();
4193 callee()->AddVideoTrack();
4194 callee()->AddVideoTrack();
4195
4196 caller()->CreateAndSetAndSignalOffer();
4197 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4198
4199 PeerConnectionWrapper* plan_b =
4200 (caller_semantics_ == SdpSemantics::kPlanB ? caller() : callee());
4201 PeerConnectionWrapper* unified_plan =
4202 (caller_semantics_ == SdpSemantics::kUnifiedPlan ? caller() : callee());
4203
4204 // Should have two senders each, one for each track.
4205 EXPECT_EQ(2u, plan_b->pc()->GetSenders().size());
4206 EXPECT_EQ(2u, unified_plan->pc()->GetSenders().size());
4207
4208 // Plan B will have one receiver since it only looks at the first video
4209 // section. The receiver should have the same track ID as the sender's first
4210 // track.
4211 ASSERT_EQ(1u, plan_b->pc()->GetReceivers().size());
4212 EXPECT_EQ(unified_plan->pc()->GetSenders()[0]->track()->id(),
4213 plan_b->pc()->GetReceivers()[0]->track()->id());
4214
4215 // Unified Plan will have two receivers since they were created with the
4216 // transceivers when the tracks were added.
4217 ASSERT_EQ(2u, unified_plan->pc()->GetReceivers().size());
4218
4219 if (unified_plan == caller()) {
4220 // If the Unified Plan endpoint was the caller, then the Plan B endpoint
4221 // would have rejected the second video media section so we would expect the
4222 // transceiver to be stopped.
4223 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[0]->stopped());
4224 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[1]->stopped());
4225 } else {
4226 // If the Unified Plan endpoint was the callee, then the Plan B endpoint
4227 // would have offered only one video section so we would expect the first
4228 // transceiver to map to the first track and the second transceiver to be
4229 // missing a mid.
4230 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[0]->mid());
4231 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[1]->mid());
4232 }
4233
4234 // Should be exchanging video frames for the first tracks on each endpoint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004235 MediaExpectations media_expectations;
4236 media_expectations.ExpectBidirectionalVideo();
4237 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004238}
4239
4240INSTANTIATE_TEST_CASE_P(
4241 PeerConnectionIntegrationTest,
4242 PeerConnectionIntegrationInteropTest,
4243 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4244 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4245
deadbeef1dcb1642017-03-29 21:08:16 -07004246} // namespace
4247
4248#endif // if !defined(THREAD_SANITIZER)