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