blob: 6dbf32080db119e33e25c847214a0e54bb07e430 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070036#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080045#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080046#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "pc/test/fakeaudiocapturemodule.h"
48#include "pc/test/fakeperiodicvideocapturer.h"
49#include "pc/test/fakertccertificategenerator.h"
50#include "pc/test/fakevideotrackrenderer.h"
51#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070053#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020056#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070057
58using cricket::ContentInfo;
59using cricket::FakeWebRtcVideoDecoder;
60using cricket::FakeWebRtcVideoDecoderFactory;
61using cricket::FakeWebRtcVideoEncoder;
62using cricket::FakeWebRtcVideoEncoderFactory;
63using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070064using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080065using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070066using ::testing::ElementsAre;
67using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070068using webrtc::DataBuffer;
69using webrtc::DataChannelInterface;
70using webrtc::DtmfSender;
71using webrtc::DtmfSenderInterface;
72using webrtc::DtmfSenderObserverInterface;
73using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080074using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070075using webrtc::MediaConstraintsInterface;
76using webrtc::MediaStreamInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070083using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070084using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080085using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070086using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070087using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080088using webrtc::RTCErrorType;
Steve Anton74255ff2018-01-24 18:32:57 -080089using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010090using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080091using webrtc::RtpSenderInterface;
92using webrtc::RtpTransceiverDirection;
93using webrtc::RtpTransceiverInit;
94using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080095using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080096using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070097using webrtc::SessionDescriptionInterface;
98using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -080099using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700100
101namespace {
102
103static const int kDefaultTimeout = 10000;
104static const int kMaxWaitForStatsMs = 3000;
105static const int kMaxWaitForActivationMs = 5000;
106static const int kMaxWaitForFramesMs = 10000;
107// Default number of audio/video frames to wait for before considering a test
108// successful.
109static const int kDefaultExpectedAudioFrameCount = 3;
110static const int kDefaultExpectedVideoFrameCount = 3;
111
deadbeef1dcb1642017-03-29 21:08:16 -0700112static const char kDataChannelLabel[] = "data_channel";
113
114// SRTP cipher name negotiated by the tests. This must be updated if the
115// default changes.
Tommi8e545ee2018-02-08 16:25:20 +0000116static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
deadbeef1dcb1642017-03-29 21:08:16 -0700117static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
118
Steve Antonede9ca52017-10-16 13:04:27 -0700119static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
120
deadbeef1dcb1642017-03-29 21:08:16 -0700121// Helper function for constructing offer/answer options to initiate an ICE
122// restart.
123PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
124 PeerConnectionInterface::RTCOfferAnswerOptions options;
125 options.ice_restart = true;
126 return options;
127}
128
deadbeefd8ad7882017-04-18 16:01:17 -0700129// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
130// attribute from received SDP, simulating a legacy endpoint.
131void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
132 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800133 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700134 }
135 desc->set_msid_supported(false);
136}
137
zhihuangf8164932017-05-19 13:09:47 -0700138int FindFirstMediaStatsIndexByKind(
139 const std::string& kind,
140 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
141 media_stats_vec) {
142 for (size_t i = 0; i < media_stats_vec.size(); i++) {
143 if (media_stats_vec[i]->kind.ValueToString() == kind) {
144 return i;
145 }
146 }
147 return -1;
148}
149
deadbeef1dcb1642017-03-29 21:08:16 -0700150class SignalingMessageReceiver {
151 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800152 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700153 virtual void ReceiveIceMessage(const std::string& sdp_mid,
154 int sdp_mline_index,
155 const std::string& msg) = 0;
156
157 protected:
158 SignalingMessageReceiver() {}
159 virtual ~SignalingMessageReceiver() {}
160};
161
162class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
163 public:
164 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
165 : expected_media_type_(media_type) {}
166
167 void OnFirstPacketReceived(cricket::MediaType media_type) override {
168 ASSERT_EQ(expected_media_type_, media_type);
169 first_packet_received_ = true;
170 }
171
172 bool first_packet_received() const { return first_packet_received_; }
173
174 virtual ~MockRtpReceiverObserver() {}
175
176 private:
177 bool first_packet_received_ = false;
178 cricket::MediaType expected_media_type_;
179};
180
181// Helper class that wraps a peer connection, observes it, and can accept
182// signaling messages from another wrapper.
183//
184// Uses a fake network, fake A/V capture, and optionally fake
185// encoders/decoders, though they aren't used by default since they don't
186// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700187// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800188// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700189class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800190 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700191 public:
192 // Different factory methods for convenience.
193 // TODO(deadbeef): Could use the pattern of:
194 //
195 // PeerConnectionWrapper =
196 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
197 //
198 // To reduce some code duplication.
199 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
200 const std::string& debug_name,
201 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
202 rtc::Thread* network_thread,
203 rtc::Thread* worker_thread) {
204 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
205 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
206 network_thread, worker_thread)) {
207 delete client;
208 return nullptr;
209 }
210 return client;
211 }
212
deadbeef2f425aa2017-04-14 10:41:32 -0700213 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
214 return peer_connection_factory_.get();
215 }
216
deadbeef1dcb1642017-03-29 21:08:16 -0700217 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
218
219 // If a signaling message receiver is set (via ConnectFakeSignaling), this
220 // will set the whole offer/answer exchange in motion. Just need to wait for
221 // the signaling state to reach "stable".
222 void CreateAndSetAndSignalOffer() {
223 auto offer = CreateOffer();
224 ASSERT_NE(nullptr, offer);
225 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
226 }
227
228 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
229 // when a remote offer is received (via fake signaling) and an answer is
230 // generated. By default, uses default options.
231 void SetOfferAnswerOptions(
232 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
233 offer_answer_options_ = options;
234 }
235
236 // Set a callback to be invoked when SDP is received via the fake signaling
237 // channel, which provides an opportunity to munge (modify) the SDP. This is
238 // used to test SDP being applied that a PeerConnection would normally not
239 // generate, but a non-JSEP endpoint might.
240 void SetReceivedSdpMunger(
241 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100242 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700243 }
244
deadbeefc964d0b2017-04-03 10:03:35 -0700245 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700246 // generated.
247 void SetGeneratedSdpMunger(
248 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100249 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700250 }
251
Seth Hampson2f0d7022018-02-20 11:54:42 -0800252 // Set a callback to be invoked when a remote offer is received via the fake
253 // signaling channel. This provides an opportunity to change the
254 // PeerConnection state before an answer is created and sent to the caller.
255 void SetRemoteOfferHandler(std::function<void()> handler) {
256 remote_offer_handler_ = std::move(handler);
257 }
258
Steve Antonede9ca52017-10-16 13:04:27 -0700259 // Every ICE connection state in order that has been seen by the observer.
260 std::vector<PeerConnectionInterface::IceConnectionState>
261 ice_connection_state_history() const {
262 return ice_connection_state_history_;
263 }
Steve Anton6f25b092017-10-23 09:39:20 -0700264 void clear_ice_connection_state_history() {
265 ice_connection_state_history_.clear();
266 }
Steve Antonede9ca52017-10-16 13:04:27 -0700267
268 // Every ICE gathering state in order that has been seen by the observer.
269 std::vector<PeerConnectionInterface::IceGatheringState>
270 ice_gathering_state_history() const {
271 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700272 }
273
Steve Anton15324772018-01-16 10:26:49 -0800274 void AddAudioVideoTracks() {
275 AddAudioTrack();
276 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700277 }
278
Steve Anton74255ff2018-01-24 18:32:57 -0800279 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
280 return AddTrack(CreateLocalAudioTrack());
281 }
deadbeef1dcb1642017-03-29 21:08:16 -0700282
Steve Anton74255ff2018-01-24 18:32:57 -0800283 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
284 return AddTrack(CreateLocalVideoTrack());
285 }
deadbeef1dcb1642017-03-29 21:08:16 -0700286
287 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
288 FakeConstraints constraints;
289 // Disable highpass filter so that we can get all the test audio frames.
290 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
291 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
292 peer_connection_factory_->CreateAudioSource(&constraints);
293 // TODO(perkj): Test audio source when it is implemented. Currently audio
294 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700295 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700296 source);
297 }
298
299 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700300 return CreateLocalVideoTrackInternal(FakeConstraints(),
301 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
303
304 rtc::scoped_refptr<webrtc::VideoTrackInterface>
305 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700306 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700307 }
308
309 rtc::scoped_refptr<webrtc::VideoTrackInterface>
310 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700311 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700312 }
313
Steve Anton74255ff2018-01-24 18:32:57 -0800314 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
315 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800316 const std::vector<std::string>& stream_ids = {}) {
317 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800318 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800319 return result.MoveValue();
320 }
321
322 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
323 cricket::MediaType media_type) {
324 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
325 for (auto receiver : pc()->GetReceivers()) {
326 if (receiver->media_type() == media_type) {
327 receivers.push_back(receiver);
328 }
329 }
330 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
Seth Hampson2f0d7022018-02-20 11:54:42 -0800333 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
334 cricket::MediaType media_type) {
335 for (auto transceiver : pc()->GetTransceivers()) {
336 if (transceiver->receiver()->media_type() == media_type) {
337 return transceiver;
338 }
339 }
340 return nullptr;
341 }
342
deadbeef1dcb1642017-03-29 21:08:16 -0700343 bool SignalingStateStable() {
344 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
345 }
346
347 void CreateDataChannel() { CreateDataChannel(nullptr); }
348
349 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700350 CreateDataChannel(kDataChannelLabel, init);
351 }
352
353 void CreateDataChannel(const std::string& label,
354 const webrtc::DataChannelInit* init) {
355 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700356 ASSERT_TRUE(data_channel_.get() != nullptr);
357 data_observer_.reset(new MockDataChannelObserver(data_channel_));
358 }
359
360 DataChannelInterface* data_channel() { return data_channel_; }
361 const MockDataChannelObserver* data_observer() const {
362 return data_observer_.get();
363 }
364
365 int audio_frames_received() const {
366 return fake_audio_capture_module_->frames_received();
367 }
368
369 // Takes minimum of video frames received for each track.
370 //
371 // Can be used like:
372 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
373 //
374 // To ensure that all video tracks received at least a certain number of
375 // frames.
376 int min_video_frames_received_per_track() const {
377 int min_frames = INT_MAX;
378 if (video_decoder_factory_enabled_) {
379 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
380 fake_video_decoder_factory_->decoders();
381 if (decoders.empty()) {
382 return 0;
383 }
384 for (FakeWebRtcVideoDecoder* decoder : decoders) {
385 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
386 }
387 return min_frames;
388 } else {
389 if (fake_video_renderers_.empty()) {
390 return 0;
391 }
392
393 for (const auto& pair : fake_video_renderers_) {
394 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
395 }
396 return min_frames;
397 }
398 }
399
400 // In contrast to the above, sums the video frames received for all tracks.
401 // Can be used to verify that no video frames were received, or that the
402 // counts didn't increase.
403 int total_video_frames_received() const {
404 int total = 0;
405 if (video_decoder_factory_enabled_) {
406 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
407 fake_video_decoder_factory_->decoders();
408 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
409 total += decoder->GetNumFramesReceived();
410 }
411 } else {
412 for (const auto& pair : fake_video_renderers_) {
413 total += pair.second->num_rendered_frames();
414 }
415 for (const auto& renderer : removed_fake_video_renderers_) {
416 total += renderer->num_rendered_frames();
417 }
418 }
419 return total;
420 }
421
422 // Returns a MockStatsObserver in a state after stats gathering finished,
423 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700424 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700425 webrtc::MediaStreamTrackInterface* track) {
426 rtc::scoped_refptr<MockStatsObserver> observer(
427 new rtc::RefCountedObject<MockStatsObserver>());
428 EXPECT_TRUE(peer_connection_->GetStats(
429 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
430 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
431 return observer;
432 }
433
434 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
436 return OldGetStatsForTrack(nullptr);
437 }
438
439 // Synchronously gets stats and returns them. If it times out, fails the test
440 // and returns null.
441 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
442 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
443 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
444 peer_connection_->GetStats(callback);
445 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
446 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700447 }
448
449 int rendered_width() {
450 EXPECT_FALSE(fake_video_renderers_.empty());
451 return fake_video_renderers_.empty()
452 ? 0
453 : fake_video_renderers_.begin()->second->width();
454 }
455
456 int rendered_height() {
457 EXPECT_FALSE(fake_video_renderers_.empty());
458 return fake_video_renderers_.empty()
459 ? 0
460 : fake_video_renderers_.begin()->second->height();
461 }
462
463 double rendered_aspect_ratio() {
464 if (rendered_height() == 0) {
465 return 0.0;
466 }
467 return static_cast<double>(rendered_width()) / rendered_height();
468 }
469
470 webrtc::VideoRotation rendered_rotation() {
471 EXPECT_FALSE(fake_video_renderers_.empty());
472 return fake_video_renderers_.empty()
473 ? webrtc::kVideoRotation_0
474 : fake_video_renderers_.begin()->second->rotation();
475 }
476
477 int local_rendered_width() {
478 return local_video_renderer_ ? local_video_renderer_->width() : 0;
479 }
480
481 int local_rendered_height() {
482 return local_video_renderer_ ? local_video_renderer_->height() : 0;
483 }
484
485 double local_rendered_aspect_ratio() {
486 if (local_rendered_height() == 0) {
487 return 0.0;
488 }
489 return static_cast<double>(local_rendered_width()) /
490 local_rendered_height();
491 }
492
493 size_t number_of_remote_streams() {
494 if (!pc()) {
495 return 0;
496 }
497 return pc()->remote_streams()->count();
498 }
499
500 StreamCollectionInterface* remote_streams() const {
501 if (!pc()) {
502 ADD_FAILURE();
503 return nullptr;
504 }
505 return pc()->remote_streams();
506 }
507
508 StreamCollectionInterface* local_streams() {
509 if (!pc()) {
510 ADD_FAILURE();
511 return nullptr;
512 }
513 return pc()->local_streams();
514 }
515
516 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
517 return pc()->signaling_state();
518 }
519
520 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
521 return pc()->ice_connection_state();
522 }
523
524 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
525 return pc()->ice_gathering_state();
526 }
527
528 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
529 // GetReceivers. They're updated automatically when a remote offer/answer
530 // from the fake signaling channel is applied, or when
531 // ResetRtpReceiverObservers below is called.
532 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
533 rtp_receiver_observers() {
534 return rtp_receiver_observers_;
535 }
536
537 void ResetRtpReceiverObservers() {
538 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100539 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
540 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700541 std::unique_ptr<MockRtpReceiverObserver> observer(
542 new MockRtpReceiverObserver(receiver->media_type()));
543 receiver->SetObserver(observer.get());
544 rtp_receiver_observers_.push_back(std::move(observer));
545 }
546 }
547
Steve Antonede9ca52017-10-16 13:04:27 -0700548 rtc::FakeNetworkManager* network() const {
549 return fake_network_manager_.get();
550 }
551 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
552
deadbeef1dcb1642017-03-29 21:08:16 -0700553 private:
554 explicit PeerConnectionWrapper(const std::string& debug_name)
555 : debug_name_(debug_name) {}
556
557 bool Init(
558 const MediaConstraintsInterface* constraints,
559 const PeerConnectionFactory::Options* options,
560 const PeerConnectionInterface::RTCConfiguration* config,
561 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
562 rtc::Thread* network_thread,
563 rtc::Thread* worker_thread) {
564 // There's an error in this test code if Init ends up being called twice.
565 RTC_DCHECK(!peer_connection_);
566 RTC_DCHECK(!peer_connection_factory_);
567
568 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700569 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700570
571 std::unique_ptr<cricket::PortAllocator> port_allocator(
572 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700573 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700574 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
575 if (!fake_audio_capture_module_) {
576 return false;
577 }
578 // Note that these factories don't end up getting used unless supported
579 // codecs are added to them.
580 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
581 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
582 rtc::Thread* const signaling_thread = rtc::Thread::Current();
583 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
584 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200585 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
586 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700587 fake_video_decoder_factory_);
588 if (!peer_connection_factory_) {
589 return false;
590 }
591 if (options) {
592 peer_connection_factory_->SetOptions(*options);
593 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800594 if (config) {
595 sdp_semantics_ = config->sdp_semantics;
596 }
deadbeef1dcb1642017-03-29 21:08:16 -0700597 peer_connection_ =
598 CreatePeerConnection(std::move(port_allocator), constraints, config,
599 std::move(cert_generator));
600 return peer_connection_.get() != nullptr;
601 }
602
603 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
604 std::unique_ptr<cricket::PortAllocator> port_allocator,
605 const MediaConstraintsInterface* constraints,
606 const PeerConnectionInterface::RTCConfiguration* config,
607 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
608 PeerConnectionInterface::RTCConfiguration modified_config;
609 // If |config| is null, this will result in a default configuration being
610 // used.
611 if (config) {
612 modified_config = *config;
613 }
614 // Disable resolution adaptation; we don't want it interfering with the
615 // test results.
616 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
617 // ratios and not specific resolutions, is this even necessary?
618 modified_config.set_cpu_adaptation(false);
619
620 return peer_connection_factory_->CreatePeerConnection(
621 modified_config, constraints, std::move(port_allocator),
622 std::move(cert_generator), this);
623 }
624
625 void set_signaling_message_receiver(
626 SignalingMessageReceiver* signaling_message_receiver) {
627 signaling_message_receiver_ = signaling_message_receiver;
628 }
629
630 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
631
Steve Antonede9ca52017-10-16 13:04:27 -0700632 void set_signal_ice_candidates(bool signal) {
633 signal_ice_candidates_ = signal;
634 }
635
deadbeef1dcb1642017-03-29 21:08:16 -0700636 void EnableVideoDecoderFactory() {
637 video_decoder_factory_enabled_ = true;
638 fake_video_decoder_factory_->AddSupportedVideoCodecType(
639 webrtc::kVideoCodecVP8);
640 }
641
642 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700643 const FakeConstraints& constraints,
644 webrtc::VideoRotation rotation) {
645 // Set max frame rate to 10fps to reduce the risk of test flakiness.
646 // TODO(deadbeef): Do something more robust.
647 FakeConstraints source_constraints = constraints;
648 source_constraints.SetMandatoryMaxFrameRate(10);
649
650 cricket::FakeVideoCapturer* fake_capturer =
651 new webrtc::FakePeriodicVideoCapturer();
652 fake_capturer->SetRotation(rotation);
653 video_capturers_.push_back(fake_capturer);
654 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
655 peer_connection_factory_->CreateVideoSource(fake_capturer,
656 &source_constraints);
657 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700658 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
659 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700660 if (!local_video_renderer_) {
661 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
662 }
663 return track;
664 }
665
666 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100667 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800668 std::unique_ptr<SessionDescriptionInterface> desc =
669 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700670 if (received_sdp_munger_) {
671 received_sdp_munger_(desc->description());
672 }
673
674 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
675 // Setting a remote description may have changed the number of receivers,
676 // so reset the receiver observers.
677 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800678 if (remote_offer_handler_) {
679 remote_offer_handler_();
680 }
deadbeef1dcb1642017-03-29 21:08:16 -0700681 auto answer = CreateAnswer();
682 ASSERT_NE(nullptr, answer);
683 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
684 }
685
686 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800688 std::unique_ptr<SessionDescriptionInterface> desc =
689 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700690 if (received_sdp_munger_) {
691 received_sdp_munger_(desc->description());
692 }
693
694 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
695 // Set the RtpReceiverObserver after receivers are created.
696 ResetRtpReceiverObservers();
697 }
698
699 // Returns null on failure.
700 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
702 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
703 pc()->CreateOffer(observer, offer_answer_options_);
704 return WaitForDescriptionFromObserver(observer);
705 }
706
707 // Returns null on failure.
708 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
709 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
710 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
711 pc()->CreateAnswer(observer, offer_answer_options_);
712 return WaitForDescriptionFromObserver(observer);
713 }
714
715 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100716 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700717 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
718 if (!observer->result()) {
719 return nullptr;
720 }
721 auto description = observer->MoveDescription();
722 if (generated_sdp_munger_) {
723 generated_sdp_munger_(description->description());
724 }
725 return description;
726 }
727
728 // Setting the local description and sending the SDP message over the fake
729 // signaling channel are combined into the same method because the SDP
730 // message needs to be sent as soon as SetLocalDescription finishes, without
731 // waiting for the observer to be called. This ensures that ICE candidates
732 // don't outrace the description.
733 bool SetLocalDescriptionAndSendSdpMessage(
734 std::unique_ptr<SessionDescriptionInterface> desc) {
735 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
736 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800738 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700739 std::string sdp;
740 EXPECT_TRUE(desc->ToString(&sdp));
741 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800742 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
743 RemoveUnusedVideoRenderers();
744 }
deadbeef1dcb1642017-03-29 21:08:16 -0700745 // As mentioned above, we need to send the message immediately after
746 // SetLocalDescription.
747 SendSdpMessage(type, sdp);
748 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
749 return true;
750 }
751
752 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
753 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
754 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100755 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700756 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800757 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
758 RemoveUnusedVideoRenderers();
759 }
deadbeef1dcb1642017-03-29 21:08:16 -0700760 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
761 return observer->result();
762 }
763
Seth Hampson2f0d7022018-02-20 11:54:42 -0800764 // This is a work around to remove unused fake_video_renderers from
765 // transceivers that have either stopped or are no longer receiving.
766 void RemoveUnusedVideoRenderers() {
767 auto transceivers = pc()->GetTransceivers();
768 for (auto& transceiver : transceivers) {
769 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
770 continue;
771 }
772 // Remove fake video renderers from any stopped transceivers.
773 if (transceiver->stopped()) {
774 auto it =
775 fake_video_renderers_.find(transceiver->receiver()->track()->id());
776 if (it != fake_video_renderers_.end()) {
777 fake_video_renderers_.erase(it);
778 }
779 }
780 // Remove fake video renderers from any transceivers that are no longer
781 // receiving.
782 if ((transceiver->current_direction() &&
783 !webrtc::RtpTransceiverDirectionHasRecv(
784 *transceiver->current_direction()))) {
785 auto it =
786 fake_video_renderers_.find(transceiver->receiver()->track()->id());
787 if (it != fake_video_renderers_.end()) {
788 fake_video_renderers_.erase(it);
789 }
790 }
791 }
792 }
793
deadbeef1dcb1642017-03-29 21:08:16 -0700794 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
795 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800796 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700797 if (signaling_delay_ms_ == 0) {
798 RelaySdpMessageIfReceiverExists(type, msg);
799 } else {
800 invoker_.AsyncInvokeDelayed<void>(
801 RTC_FROM_HERE, rtc::Thread::Current(),
802 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
803 this, type, msg),
804 signaling_delay_ms_);
805 }
806 }
807
Steve Antona3a92c22017-12-07 10:27:41 -0800808 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700809 if (signaling_message_receiver_) {
810 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
811 }
812 }
813
814 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
815 // default).
816 void SendIceMessage(const std::string& sdp_mid,
817 int sdp_mline_index,
818 const std::string& msg) {
819 if (signaling_delay_ms_ == 0) {
820 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
821 } else {
822 invoker_.AsyncInvokeDelayed<void>(
823 RTC_FROM_HERE, rtc::Thread::Current(),
824 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
825 this, sdp_mid, sdp_mline_index, msg),
826 signaling_delay_ms_);
827 }
828 }
829
830 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
831 int sdp_mline_index,
832 const std::string& msg) {
833 if (signaling_message_receiver_) {
834 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
835 msg);
836 }
837 }
838
839 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800840 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
841 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700842 HandleIncomingOffer(msg);
843 } else {
844 HandleIncomingAnswer(msg);
845 }
846 }
847
848 void ReceiveIceMessage(const std::string& sdp_mid,
849 int sdp_mline_index,
850 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100851 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700852 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
853 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
854 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
855 }
856
857 // PeerConnectionObserver callbacks.
858 void OnSignalingChange(
859 webrtc::PeerConnectionInterface::SignalingState new_state) override {
860 EXPECT_EQ(pc()->signaling_state(), new_state);
861 }
Steve Anton15324772018-01-16 10:26:49 -0800862 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
863 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
864 streams) override {
865 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
866 rtc::scoped_refptr<VideoTrackInterface> video_track(
867 static_cast<VideoTrackInterface*>(receiver->track().get()));
868 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700869 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800870 fake_video_renderers_[video_track->id()] =
871 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700872 }
873 }
Steve Anton15324772018-01-16 10:26:49 -0800874 void OnRemoveTrack(
875 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
876 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
877 auto it = fake_video_renderers_.find(receiver->track()->id());
878 RTC_DCHECK(it != fake_video_renderers_.end());
879 fake_video_renderers_.erase(it);
880 }
881 }
deadbeef1dcb1642017-03-29 21:08:16 -0700882 void OnRenegotiationNeeded() override {}
883 void OnIceConnectionChange(
884 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
885 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700886 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700887 }
888 void OnIceGatheringChange(
889 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700890 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700891 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700892 }
893 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700895
896 std::string ice_sdp;
897 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700898 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700899 // Remote party may be deleted.
900 return;
901 }
902 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
903 }
904 void OnDataChannel(
905 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700907 data_channel_ = data_channel;
908 data_observer_.reset(new MockDataChannelObserver(data_channel));
909 }
910
deadbeef1dcb1642017-03-29 21:08:16 -0700911 std::string debug_name_;
912
913 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
914
915 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
916 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
917 peer_connection_factory_;
918
Steve Antonede9ca52017-10-16 13:04:27 -0700919 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700920 // Needed to keep track of number of frames sent.
921 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
922 // Needed to keep track of number of frames received.
923 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
924 fake_video_renderers_;
925 // Needed to ensure frames aren't received for removed tracks.
926 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
927 removed_fake_video_renderers_;
928 // Needed to keep track of number of frames received when external decoder
929 // used.
930 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
931 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
932 bool video_decoder_factory_enabled_ = false;
933
934 // For remote peer communication.
935 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
936 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700937 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700938
939 // Store references to the video capturers we've created, so that we can stop
940 // them, if required.
941 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
942 // |local_video_renderer_| attached to the first created local video track.
943 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
944
Seth Hampson2f0d7022018-02-20 11:54:42 -0800945 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700946 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
947 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
948 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800949 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700950
951 rtc::scoped_refptr<DataChannelInterface> data_channel_;
952 std::unique_ptr<MockDataChannelObserver> data_observer_;
953
954 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
955
Steve Antonede9ca52017-10-16 13:04:27 -0700956 std::vector<PeerConnectionInterface::IceConnectionState>
957 ice_connection_state_history_;
958 std::vector<PeerConnectionInterface::IceGatheringState>
959 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700960
961 rtc::AsyncInvoker invoker_;
962
Seth Hampson2f0d7022018-02-20 11:54:42 -0800963 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700964};
965
Elad Alon99c3fe52017-10-13 16:29:40 +0200966class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
967 public:
968 virtual ~MockRtcEventLogOutput() = default;
969 MOCK_CONST_METHOD0(IsActive, bool());
970 MOCK_METHOD1(Write, bool(const std::string&));
971};
972
Seth Hampson2f0d7022018-02-20 11:54:42 -0800973// This helper object is used for both specifying how many audio/video frames
974// are expected to be received for a caller/callee. It provides helper functions
975// to specify these expectations. The object initially starts in a state of no
976// expectations.
977class MediaExpectations {
978 public:
979 enum ExpectFrames {
980 kExpectSomeFrames,
981 kExpectNoFrames,
982 kNoExpectation,
983 };
984
985 void ExpectBidirectionalAudioAndVideo() {
986 ExpectBidirectionalAudio();
987 ExpectBidirectionalVideo();
988 }
989
990 void ExpectBidirectionalAudio() {
991 CallerExpectsSomeAudio();
992 CalleeExpectsSomeAudio();
993 }
994
995 void ExpectNoAudio() {
996 CallerExpectsNoAudio();
997 CalleeExpectsNoAudio();
998 }
999
1000 void ExpectBidirectionalVideo() {
1001 CallerExpectsSomeVideo();
1002 CalleeExpectsSomeVideo();
1003 }
1004
1005 void ExpectNoVideo() {
1006 CallerExpectsNoVideo();
1007 CalleeExpectsNoVideo();
1008 }
1009
1010 void CallerExpectsSomeAudioAndVideo() {
1011 CallerExpectsSomeAudio();
1012 CallerExpectsSomeVideo();
1013 }
1014
1015 void CalleeExpectsSomeAudioAndVideo() {
1016 CalleeExpectsSomeAudio();
1017 CalleeExpectsSomeVideo();
1018 }
1019
1020 // Caller's audio functions.
1021 void CallerExpectsSomeAudio(
1022 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1023 caller_audio_expectation_ = kExpectSomeFrames;
1024 caller_audio_frames_expected_ = expected_audio_frames;
1025 }
1026
1027 void CallerExpectsNoAudio() {
1028 caller_audio_expectation_ = kExpectNoFrames;
1029 caller_audio_frames_expected_ = 0;
1030 }
1031
1032 // Caller's video functions.
1033 void CallerExpectsSomeVideo(
1034 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1035 caller_video_expectation_ = kExpectSomeFrames;
1036 caller_video_frames_expected_ = expected_video_frames;
1037 }
1038
1039 void CallerExpectsNoVideo() {
1040 caller_video_expectation_ = kExpectNoFrames;
1041 caller_video_frames_expected_ = 0;
1042 }
1043
1044 // Callee's audio functions.
1045 void CalleeExpectsSomeAudio(
1046 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1047 callee_audio_expectation_ = kExpectSomeFrames;
1048 callee_audio_frames_expected_ = expected_audio_frames;
1049 }
1050
1051 void CalleeExpectsNoAudio() {
1052 callee_audio_expectation_ = kExpectNoFrames;
1053 callee_audio_frames_expected_ = 0;
1054 }
1055
1056 // Callee's video functions.
1057 void CalleeExpectsSomeVideo(
1058 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1059 callee_video_expectation_ = kExpectSomeFrames;
1060 callee_video_frames_expected_ = expected_video_frames;
1061 }
1062
1063 void CalleeExpectsNoVideo() {
1064 callee_video_expectation_ = kExpectNoFrames;
1065 callee_video_frames_expected_ = 0;
1066 }
1067
1068 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1069 ExpectFrames caller_video_expectation_ = kNoExpectation;
1070 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1071 ExpectFrames callee_video_expectation_ = kNoExpectation;
1072 int caller_audio_frames_expected_ = 0;
1073 int caller_video_frames_expected_ = 0;
1074 int callee_audio_frames_expected_ = 0;
1075 int callee_video_frames_expected_ = 0;
1076};
1077
deadbeef1dcb1642017-03-29 21:08:16 -07001078// Tests two PeerConnections connecting to each other end-to-end, using a
1079// virtual network, fake A/V capture and fake encoder/decoders. The
1080// PeerConnections share the threads/socket servers, but use separate versions
1081// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001082class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001083 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001084 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1085 : sdp_semantics_(sdp_semantics),
1086 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001087 fss_(new rtc::FirewallSocketServer(ss_.get())),
1088 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001089 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001090 network_thread_->SetName("PCNetworkThread", this);
1091 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001092 RTC_CHECK(network_thread_->Start());
1093 RTC_CHECK(worker_thread_->Start());
1094 }
1095
Seth Hampson2f0d7022018-02-20 11:54:42 -08001096 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001097 if (caller_) {
1098 caller_->set_signaling_message_receiver(nullptr);
1099 }
1100 if (callee_) {
1101 callee_->set_signaling_message_receiver(nullptr);
1102 }
1103 }
1104
1105 bool SignalingStateStable() {
1106 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1107 }
1108
deadbeef71452802017-05-07 17:21:01 -07001109 bool DtlsConnected() {
1110 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1111 // are connected. This is an important distinction. Once we have separate
1112 // ICE and DTLS state, this check needs to use the DTLS state.
1113 return (callee()->ice_connection_state() ==
1114 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1115 callee()->ice_connection_state() ==
1116 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1117 (caller()->ice_connection_state() ==
1118 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1119 caller()->ice_connection_state() ==
1120 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1121 }
1122
Seth Hampson2f0d7022018-02-20 11:54:42 -08001123 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1124 const std::string& debug_name,
1125 const MediaConstraintsInterface* constraints,
1126 const PeerConnectionFactory::Options* options,
1127 const RTCConfiguration* config,
1128 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
1129 RTCConfiguration modified_config;
1130 if (config) {
1131 modified_config = *config;
1132 }
1133 if (modified_config.sdp_semantics == SdpSemantics::kDefault) {
1134 modified_config.sdp_semantics = sdp_semantics_;
1135 }
1136 if (!cert_generator) {
1137 cert_generator = rtc::MakeUnique<FakeRTCCertificateGenerator>();
1138 }
1139 std::unique_ptr<PeerConnectionWrapper> client(
1140 new PeerConnectionWrapper(debug_name));
1141 if (!client->Init(constraints, options, &modified_config,
1142 std::move(cert_generator), network_thread_.get(),
1143 worker_thread_.get())) {
1144 return nullptr;
1145 }
1146 return client;
1147 }
1148
deadbeef1dcb1642017-03-29 21:08:16 -07001149 bool CreatePeerConnectionWrappers() {
1150 return CreatePeerConnectionWrappersWithConfig(
1151 PeerConnectionInterface::RTCConfiguration(),
1152 PeerConnectionInterface::RTCConfiguration());
1153 }
1154
1155 bool CreatePeerConnectionWrappersWithConstraints(
1156 MediaConstraintsInterface* caller_constraints,
1157 MediaConstraintsInterface* callee_constraints) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001158 caller_ = CreatePeerConnectionWrapper("Caller", caller_constraints, nullptr,
1159 nullptr, nullptr);
1160 callee_ = CreatePeerConnectionWrapper("Callee", callee_constraints, nullptr,
1161 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001162 return caller_ && callee_;
1163 }
1164
1165 bool CreatePeerConnectionWrappersWithConfig(
1166 const PeerConnectionInterface::RTCConfiguration& caller_config,
1167 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001168 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, nullptr,
1169 &caller_config, nullptr);
1170 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, nullptr,
1171 &callee_config, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001172 return caller_ && callee_;
1173 }
1174
1175 bool CreatePeerConnectionWrappersWithOptions(
1176 const PeerConnectionFactory::Options& caller_options,
1177 const PeerConnectionFactory::Options& callee_options) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001178 caller_ = CreatePeerConnectionWrapper("Caller", nullptr, &caller_options,
1179 nullptr, nullptr);
1180 callee_ = CreatePeerConnectionWrapper("Callee", nullptr, &callee_options,
1181 nullptr, nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001182 return caller_ && callee_;
1183 }
1184
Seth Hampson2f0d7022018-02-20 11:54:42 -08001185 std::unique_ptr<PeerConnectionWrapper>
1186 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001187 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1188 new FakeRTCCertificateGenerator());
1189 cert_generator->use_alternate_key();
1190
Seth Hampson2f0d7022018-02-20 11:54:42 -08001191 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
1192 std::move(cert_generator));
deadbeef1dcb1642017-03-29 21:08:16 -07001193 }
1194
1195 // Once called, SDP blobs and ICE candidates will be automatically signaled
1196 // between PeerConnections.
1197 void ConnectFakeSignaling() {
1198 caller_->set_signaling_message_receiver(callee_.get());
1199 callee_->set_signaling_message_receiver(caller_.get());
1200 }
1201
Steve Antonede9ca52017-10-16 13:04:27 -07001202 // Once called, SDP blobs will be automatically signaled between
1203 // PeerConnections. Note that ICE candidates will not be signaled unless they
1204 // are in the exchanged SDP blobs.
1205 void ConnectFakeSignalingForSdpOnly() {
1206 ConnectFakeSignaling();
1207 SetSignalIceCandidates(false);
1208 }
1209
deadbeef1dcb1642017-03-29 21:08:16 -07001210 void SetSignalingDelayMs(int delay_ms) {
1211 caller_->set_signaling_delay_ms(delay_ms);
1212 callee_->set_signaling_delay_ms(delay_ms);
1213 }
1214
Steve Antonede9ca52017-10-16 13:04:27 -07001215 void SetSignalIceCandidates(bool signal) {
1216 caller_->set_signal_ice_candidates(signal);
1217 callee_->set_signal_ice_candidates(signal);
1218 }
1219
deadbeef1dcb1642017-03-29 21:08:16 -07001220 void EnableVideoDecoderFactory() {
1221 caller_->EnableVideoDecoderFactory();
1222 callee_->EnableVideoDecoderFactory();
1223 }
1224
1225 // Messages may get lost on the unreliable DataChannel, so we send multiple
1226 // times to avoid test flakiness.
1227 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1228 const std::string& data,
1229 int retries) {
1230 for (int i = 0; i < retries; ++i) {
1231 dc->Send(DataBuffer(data));
1232 }
1233 }
1234
1235 rtc::Thread* network_thread() { return network_thread_.get(); }
1236
1237 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1238
1239 PeerConnectionWrapper* caller() { return caller_.get(); }
1240
1241 // Set the |caller_| to the |wrapper| passed in and return the
1242 // original |caller_|.
1243 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1244 PeerConnectionWrapper* wrapper) {
1245 PeerConnectionWrapper* old = caller_.release();
1246 caller_.reset(wrapper);
1247 return old;
1248 }
1249
1250 PeerConnectionWrapper* callee() { return callee_.get(); }
1251
1252 // Set the |callee_| to the |wrapper| passed in and return the
1253 // original |callee_|.
1254 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1255 PeerConnectionWrapper* wrapper) {
1256 PeerConnectionWrapper* old = callee_.release();
1257 callee_.reset(wrapper);
1258 return old;
1259 }
1260
Steve Antonede9ca52017-10-16 13:04:27 -07001261 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1262
Seth Hampson2f0d7022018-02-20 11:54:42 -08001263 // Expects the provided number of new frames to be received within
1264 // kMaxWaitForFramesMs. The new expected frames are specified in
1265 // |media_expectations|. Returns false if any of the expectations were
1266 // not met.
1267 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1268 // First initialize the expected frame counts based upon the current
1269 // frame count.
1270 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1271 if (media_expectations.caller_audio_expectation_ ==
1272 MediaExpectations::kExpectSomeFrames) {
1273 total_caller_audio_frames_expected +=
1274 media_expectations.caller_audio_frames_expected_;
1275 }
1276 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001277 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001278 if (media_expectations.caller_video_expectation_ ==
1279 MediaExpectations::kExpectSomeFrames) {
1280 total_caller_video_frames_expected +=
1281 media_expectations.caller_video_frames_expected_;
1282 }
1283 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1284 if (media_expectations.callee_audio_expectation_ ==
1285 MediaExpectations::kExpectSomeFrames) {
1286 total_callee_audio_frames_expected +=
1287 media_expectations.callee_audio_frames_expected_;
1288 }
1289 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001290 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 if (media_expectations.callee_video_expectation_ ==
1292 MediaExpectations::kExpectSomeFrames) {
1293 total_callee_video_frames_expected +=
1294 media_expectations.callee_video_frames_expected_;
1295 }
deadbeef1dcb1642017-03-29 21:08:16 -07001296
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001298 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001299 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001300 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001301 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001302 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001303 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001304 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001305 total_callee_video_frames_expected,
1306 kMaxWaitForFramesMs);
1307 bool expectations_correct =
1308 caller()->audio_frames_received() >=
1309 total_caller_audio_frames_expected &&
1310 caller()->min_video_frames_received_per_track() >=
1311 total_caller_video_frames_expected &&
1312 callee()->audio_frames_received() >=
1313 total_callee_audio_frames_expected &&
1314 callee()->min_video_frames_received_per_track() >=
1315 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001316
Seth Hampson2f0d7022018-02-20 11:54:42 -08001317 // After the combined wait, print out a more detailed message upon
1318 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001319 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001320 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001321 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001322 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001323 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001324 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001325 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001326 total_callee_video_frames_expected);
1327
1328 // We want to make sure nothing unexpected was received.
1329 if (media_expectations.caller_audio_expectation_ ==
1330 MediaExpectations::kExpectNoFrames) {
1331 EXPECT_EQ(caller()->audio_frames_received(),
1332 total_caller_audio_frames_expected);
1333 if (caller()->audio_frames_received() !=
1334 total_caller_audio_frames_expected) {
1335 expectations_correct = false;
1336 }
1337 }
1338 if (media_expectations.caller_video_expectation_ ==
1339 MediaExpectations::kExpectNoFrames) {
1340 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1341 total_caller_video_frames_expected);
1342 if (caller()->min_video_frames_received_per_track() !=
1343 total_caller_video_frames_expected) {
1344 expectations_correct = false;
1345 }
1346 }
1347 if (media_expectations.callee_audio_expectation_ ==
1348 MediaExpectations::kExpectNoFrames) {
1349 EXPECT_EQ(callee()->audio_frames_received(),
1350 total_callee_audio_frames_expected);
1351 if (callee()->audio_frames_received() !=
1352 total_callee_audio_frames_expected) {
1353 expectations_correct = false;
1354 }
1355 }
1356 if (media_expectations.callee_video_expectation_ ==
1357 MediaExpectations::kExpectNoFrames) {
1358 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1359 total_callee_video_frames_expected);
1360 if (callee()->min_video_frames_received_per_track() !=
1361 total_callee_video_frames_expected) {
1362 expectations_correct = false;
1363 }
1364 }
1365 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001366 }
1367
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001368 void TestNegotiatedCipherSuite(
1369 const PeerConnectionFactory::Options& caller_options,
1370 const PeerConnectionFactory::Options& callee_options,
1371 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001372 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1373 callee_options));
1374 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1375 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1376 caller()->pc()->RegisterUMAObserver(caller_observer);
1377 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001378 caller()->AddAudioVideoTracks();
1379 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001380 caller()->CreateAndSetAndSignalOffer();
1381 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1382 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001383 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001384 EXPECT_EQ(
1385 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1386 expected_cipher_suite));
1387 caller()->pc()->RegisterUMAObserver(nullptr);
1388 }
1389
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001390 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1391 bool remote_gcm_enabled,
1392 int expected_cipher_suite) {
1393 PeerConnectionFactory::Options caller_options;
1394 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1395 PeerConnectionFactory::Options callee_options;
1396 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1397 TestNegotiatedCipherSuite(caller_options, callee_options,
1398 expected_cipher_suite);
1399 }
1400
Seth Hampson2f0d7022018-02-20 11:54:42 -08001401 protected:
1402 const SdpSemantics sdp_semantics_;
1403
deadbeef1dcb1642017-03-29 21:08:16 -07001404 private:
1405 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001406 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001407 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001408 // |network_thread_| and |worker_thread_| are used by both
1409 // |caller_| and |callee_| so they must be destroyed
1410 // later.
1411 std::unique_ptr<rtc::Thread> network_thread_;
1412 std::unique_ptr<rtc::Thread> worker_thread_;
1413 std::unique_ptr<PeerConnectionWrapper> caller_;
1414 std::unique_ptr<PeerConnectionWrapper> callee_;
1415};
1416
Seth Hampson2f0d7022018-02-20 11:54:42 -08001417class PeerConnectionIntegrationTest
1418 : public PeerConnectionIntegrationBaseTest,
1419 public ::testing::WithParamInterface<SdpSemantics> {
1420 protected:
1421 PeerConnectionIntegrationTest()
1422 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1423};
1424
1425class PeerConnectionIntegrationTestPlanB
1426 : public PeerConnectionIntegrationBaseTest {
1427 protected:
1428 PeerConnectionIntegrationTestPlanB()
1429 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1430};
1431
1432class PeerConnectionIntegrationTestUnifiedPlan
1433 : public PeerConnectionIntegrationBaseTest {
1434 protected:
1435 PeerConnectionIntegrationTestUnifiedPlan()
1436 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1437};
1438
deadbeef1dcb1642017-03-29 21:08:16 -07001439// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1440// includes testing that the callback is invoked if an observer is connected
1441// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001442TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001443 RtpReceiverObserverOnFirstPacketReceived) {
1444 ASSERT_TRUE(CreatePeerConnectionWrappers());
1445 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001446 caller()->AddAudioVideoTracks();
1447 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001448 // Start offer/answer exchange and wait for it to complete.
1449 caller()->CreateAndSetAndSignalOffer();
1450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1451 // Should be one receiver each for audio/video.
1452 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1453 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1454 // Wait for all "first packet received" callbacks to be fired.
1455 EXPECT_TRUE_WAIT(
1456 std::all_of(caller()->rtp_receiver_observers().begin(),
1457 caller()->rtp_receiver_observers().end(),
1458 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1459 return o->first_packet_received();
1460 }),
1461 kMaxWaitForFramesMs);
1462 EXPECT_TRUE_WAIT(
1463 std::all_of(callee()->rtp_receiver_observers().begin(),
1464 callee()->rtp_receiver_observers().end(),
1465 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1466 return o->first_packet_received();
1467 }),
1468 kMaxWaitForFramesMs);
1469 // If new observers are set after the first packet was already received, the
1470 // callback should still be invoked.
1471 caller()->ResetRtpReceiverObservers();
1472 callee()->ResetRtpReceiverObservers();
1473 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1474 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1475 EXPECT_TRUE(
1476 std::all_of(caller()->rtp_receiver_observers().begin(),
1477 caller()->rtp_receiver_observers().end(),
1478 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1479 return o->first_packet_received();
1480 }));
1481 EXPECT_TRUE(
1482 std::all_of(callee()->rtp_receiver_observers().begin(),
1483 callee()->rtp_receiver_observers().end(),
1484 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1485 return o->first_packet_received();
1486 }));
1487}
1488
1489class DummyDtmfObserver : public DtmfSenderObserverInterface {
1490 public:
1491 DummyDtmfObserver() : completed_(false) {}
1492
1493 // Implements DtmfSenderObserverInterface.
1494 void OnToneChange(const std::string& tone) override {
1495 tones_.push_back(tone);
1496 if (tone.empty()) {
1497 completed_ = true;
1498 }
1499 }
1500
1501 const std::vector<std::string>& tones() const { return tones_; }
1502 bool completed() const { return completed_; }
1503
1504 private:
1505 bool completed_;
1506 std::vector<std::string> tones_;
1507};
1508
1509// Assumes |sender| already has an audio track added and the offer/answer
1510// exchange is done.
1511void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1512 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001513 // We should be able to get a DTMF sender from the local sender.
1514 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1515 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1516 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001517 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001518 dtmf_sender->RegisterObserver(&observer);
1519
1520 // Test the DtmfSender object just created.
1521 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1522 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1523
1524 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1525 std::vector<std::string> tones = {"1", "a", ""};
1526 EXPECT_EQ(tones, observer.tones());
1527 dtmf_sender->UnregisterObserver();
1528 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1529}
1530
1531// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1532// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001533TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001534 ASSERT_TRUE(CreatePeerConnectionWrappers());
1535 ConnectFakeSignaling();
1536 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001537 caller()->AddAudioTrack();
1538 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001539 caller()->CreateAndSetAndSignalOffer();
1540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001541 // DTLS must finish before the DTMF sender can be used reliably.
1542 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001543 TestDtmfFromSenderToReceiver(caller(), callee());
1544 TestDtmfFromSenderToReceiver(callee(), caller());
1545}
1546
1547// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1548// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001549TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001550 ASSERT_TRUE(CreatePeerConnectionWrappers());
1551 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001552 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1553 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1554 caller()->pc()->RegisterUMAObserver(caller_observer);
1555
deadbeef1dcb1642017-03-29 21:08:16 -07001556 // Do normal offer/answer and wait for some frames to be received in each
1557 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001558 caller()->AddAudioVideoTracks();
1559 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001560 caller()->CreateAndSetAndSignalOffer();
1561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001562 MediaExpectations media_expectations;
1563 media_expectations.ExpectBidirectionalAudioAndVideo();
1564 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001565 EXPECT_LE(
1566 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1567 webrtc::kEnumCounterKeyProtocolDtls));
1568 EXPECT_EQ(
1569 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1570 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001571}
1572
1573// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001574TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001575 PeerConnectionInterface::RTCConfiguration sdes_config;
1576 sdes_config.enable_dtls_srtp.emplace(false);
1577 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1578 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001579 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1580 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1581 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001582
1583 // Do normal offer/answer and wait for some frames to be received in each
1584 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001585 caller()->AddAudioVideoTracks();
1586 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001587 caller()->CreateAndSetAndSignalOffer();
1588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001589 MediaExpectations media_expectations;
1590 media_expectations.ExpectBidirectionalAudioAndVideo();
1591 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001592 EXPECT_LE(
1593 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1594 webrtc::kEnumCounterKeyProtocolSdes));
1595 EXPECT_EQ(
1596 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1597 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001598}
1599
Steve Anton8c0f7a72017-10-03 10:03:10 -07001600// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1601// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001602TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001603 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1604 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1605 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1606 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1607 return pc->GetRemoteAudioSSLCertificate();
1608 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001609 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1610 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1611 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1612 return pc->GetRemoteAudioSSLCertChain();
1613 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001614
1615 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1616 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1617
1618 // Configure each side with a known certificate so they can be compared later.
1619 PeerConnectionInterface::RTCConfiguration caller_config;
1620 caller_config.enable_dtls_srtp.emplace(true);
1621 caller_config.certificates.push_back(caller_cert);
1622 PeerConnectionInterface::RTCConfiguration callee_config;
1623 callee_config.enable_dtls_srtp.emplace(true);
1624 callee_config.certificates.push_back(callee_cert);
1625 ASSERT_TRUE(
1626 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1627 ConnectFakeSignaling();
1628
1629 // When first initialized, there should not be a remote SSL certificate (and
1630 // calling this method should not crash).
1631 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1632 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001633 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1634 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001635
Steve Anton15324772018-01-16 10:26:49 -08001636 caller()->AddAudioTrack();
1637 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001638 caller()->CreateAndSetAndSignalOffer();
1639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1640 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1641
1642 // Once DTLS has been connected, each side should return the other's SSL
1643 // certificate when calling GetRemoteAudioSSLCertificate.
1644
1645 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1646 ASSERT_TRUE(caller_remote_cert);
1647 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1648 caller_remote_cert->ToPEMString());
1649
1650 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1651 ASSERT_TRUE(callee_remote_cert);
1652 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1653 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001654
1655 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1656 ASSERT_TRUE(caller_remote_cert_chain);
1657 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1658 auto remote_cert = &caller_remote_cert_chain->Get(0);
1659 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1660 remote_cert->ToPEMString());
1661
1662 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1663 ASSERT_TRUE(callee_remote_cert_chain);
1664 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1665 remote_cert = &callee_remote_cert_chain->Get(0);
1666 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1667 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001668}
1669
deadbeef1dcb1642017-03-29 21:08:16 -07001670// This test sets up a call between two parties (using DTLS) and tests that we
1671// can get a video aspect ratio of 16:9.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001672TEST_P(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
deadbeef1dcb1642017-03-29 21:08:16 -07001673 ASSERT_TRUE(CreatePeerConnectionWrappers());
1674 ConnectFakeSignaling();
1675
1676 // Add video tracks with 16:9 constraint.
1677 FakeConstraints constraints;
1678 double requested_ratio = 16.0 / 9;
1679 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001680 caller()->AddTrack(
1681 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1682 callee()->AddTrack(
1683 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001684
1685 // Do normal offer/answer and wait for at least one frame to be received in
1686 // each direction.
1687 caller()->CreateAndSetAndSignalOffer();
1688 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1689 callee()->min_video_frames_received_per_track() > 0,
1690 kMaxWaitForFramesMs);
1691
1692 // Check rendered aspect ratio.
1693 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1694 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1695 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1696 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1697}
1698
1699// This test sets up a call between two parties with a source resolution of
1700// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001701TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001702 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1703 ASSERT_TRUE(CreatePeerConnectionWrappers());
1704 ConnectFakeSignaling();
1705
1706 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1707 // instead of aspect ratio constraint.
1708 FakeConstraints constraints;
1709 constraints.SetMandatoryMinWidth(1280);
1710 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001711 caller()->AddTrack(
1712 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1713 callee()->AddTrack(
1714 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001715
1716 // Do normal offer/answer and wait for at least one frame to be received in
1717 // each direction.
1718 caller()->CreateAndSetAndSignalOffer();
1719 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1720 callee()->min_video_frames_received_per_track() > 0,
1721 kMaxWaitForFramesMs);
1722
1723 // Check rendered aspect ratio.
1724 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1725 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1726 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1727 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1728}
1729
1730// This test sets up an one-way call, with media only from caller to
1731// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001732TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001733 ASSERT_TRUE(CreatePeerConnectionWrappers());
1734 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001735 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001736 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001737 MediaExpectations media_expectations;
1738 media_expectations.CalleeExpectsSomeAudioAndVideo();
1739 media_expectations.CallerExpectsNoAudio();
1740 media_expectations.CallerExpectsNoVideo();
1741 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001742}
1743
1744// This test sets up a audio call initially, with the callee rejecting video
1745// initially. Then later the callee decides to upgrade to audio/video, and
1746// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001747TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001748 ASSERT_TRUE(CreatePeerConnectionWrappers());
1749 ConnectFakeSignaling();
1750 // Initially, offer an audio/video stream from the caller, but refuse to
1751 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001752 caller()->AddAudioVideoTracks();
1753 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001754 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1755 PeerConnectionInterface::RTCOfferAnswerOptions options;
1756 options.offer_to_receive_video = 0;
1757 callee()->SetOfferAnswerOptions(options);
1758 } else {
1759 callee()->SetRemoteOfferHandler([this] {
1760 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1761 });
1762 }
deadbeef1dcb1642017-03-29 21:08:16 -07001763 // Do offer/answer and make sure audio is still received end-to-end.
1764 caller()->CreateAndSetAndSignalOffer();
1765 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001766 {
1767 MediaExpectations media_expectations;
1768 media_expectations.ExpectBidirectionalAudio();
1769 media_expectations.ExpectNoVideo();
1770 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1771 }
deadbeef1dcb1642017-03-29 21:08:16 -07001772 // Sanity check that the callee's description has a rejected video section.
1773 ASSERT_NE(nullptr, callee()->pc()->local_description());
1774 const ContentInfo* callee_video_content =
1775 GetFirstVideoContent(callee()->pc()->local_description()->description());
1776 ASSERT_NE(nullptr, callee_video_content);
1777 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001778
deadbeef1dcb1642017-03-29 21:08:16 -07001779 // Now negotiate with video and ensure negotiation succeeds, with video
1780 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001781 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001782 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1783 PeerConnectionInterface::RTCOfferAnswerOptions options;
1784 options.offer_to_receive_video = 1;
1785 callee()->SetOfferAnswerOptions(options);
1786 } else {
1787 callee()->SetRemoteOfferHandler(nullptr);
1788 caller()->SetRemoteOfferHandler([this] {
1789 // The caller creates a new transceiver to receive video on when receiving
1790 // the offer, but by default it is send only.
1791 auto transceivers = caller()->pc()->GetTransceivers();
1792 ASSERT_EQ(3, transceivers.size());
1793 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1794 transceivers[2]->receiver()->media_type());
1795 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1796 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1797 });
1798 }
deadbeef1dcb1642017-03-29 21:08:16 -07001799 callee()->CreateAndSetAndSignalOffer();
1800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001801 {
1802 // Expect additional audio frames to be received after the upgrade.
1803 MediaExpectations media_expectations;
1804 media_expectations.ExpectBidirectionalAudioAndVideo();
1805 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1806 }
deadbeef1dcb1642017-03-29 21:08:16 -07001807}
1808
deadbeef4389b4d2017-09-07 09:07:36 -07001809// Simpler than the above test; just add an audio track to an established
1810// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001811TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001812 ASSERT_TRUE(CreatePeerConnectionWrappers());
1813 ConnectFakeSignaling();
1814 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001815 caller()->AddVideoTrack();
1816 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001817 caller()->CreateAndSetAndSignalOffer();
1818 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1819 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001820 caller()->AddAudioTrack();
1821 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001822 caller()->CreateAndSetAndSignalOffer();
1823 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1824 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001825 MediaExpectations media_expectations;
1826 media_expectations.ExpectBidirectionalAudioAndVideo();
1827 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001828}
1829
deadbeef1dcb1642017-03-29 21:08:16 -07001830// This test sets up a call that's transferred to a new caller with a different
1831// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001832TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001833 ASSERT_TRUE(CreatePeerConnectionWrappers());
1834 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001835 caller()->AddAudioVideoTracks();
1836 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001837 caller()->CreateAndSetAndSignalOffer();
1838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1839
1840 // Keep the original peer around which will still send packets to the
1841 // receiving client. These SRTP packets will be dropped.
1842 std::unique_ptr<PeerConnectionWrapper> original_peer(
1843 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001844 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001845 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1846 // directly above.
1847 original_peer->pc()->Close();
1848
1849 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001850 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001851 caller()->CreateAndSetAndSignalOffer();
1852 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1853 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001854 MediaExpectations media_expectations;
1855 media_expectations.ExpectBidirectionalAudioAndVideo();
1856 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001857}
1858
1859// This test sets up a call that's transferred to a new callee with a different
1860// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001861TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001862 ASSERT_TRUE(CreatePeerConnectionWrappers());
1863 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001864 caller()->AddAudioVideoTracks();
1865 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001866 caller()->CreateAndSetAndSignalOffer();
1867 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1868
1869 // Keep the original peer around which will still send packets to the
1870 // receiving client. These SRTP packets will be dropped.
1871 std::unique_ptr<PeerConnectionWrapper> original_peer(
1872 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001873 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001874 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1875 // directly above.
1876 original_peer->pc()->Close();
1877
1878 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001879 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001880 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1881 caller()->CreateAndSetAndSignalOffer();
1882 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1883 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001884 MediaExpectations media_expectations;
1885 media_expectations.ExpectBidirectionalAudioAndVideo();
1886 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001887}
1888
1889// This test sets up a non-bundled call and negotiates bundling at the same
1890// time as starting an ICE restart. When bundling is in effect in the restart,
1891// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001892TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001893 ASSERT_TRUE(CreatePeerConnectionWrappers());
1894 ConnectFakeSignaling();
1895
Steve Anton15324772018-01-16 10:26:49 -08001896 caller()->AddAudioVideoTracks();
1897 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001898 // Remove the bundle group from the SDP received by the callee.
1899 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1900 desc->RemoveGroupByName("BUNDLE");
1901 });
1902 caller()->CreateAndSetAndSignalOffer();
1903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001904 {
1905 MediaExpectations media_expectations;
1906 media_expectations.ExpectBidirectionalAudioAndVideo();
1907 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1908 }
deadbeef1dcb1642017-03-29 21:08:16 -07001909 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1910 callee()->SetReceivedSdpMunger(nullptr);
1911 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1912 caller()->CreateAndSetAndSignalOffer();
1913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1914
1915 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001916 {
1917 MediaExpectations media_expectations;
1918 media_expectations.ExpectBidirectionalAudioAndVideo();
1919 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1920 }
deadbeef1dcb1642017-03-29 21:08:16 -07001921}
1922
1923// Test CVO (Coordination of Video Orientation). If a video source is rotated
1924// and both peers support the CVO RTP header extension, the actual video frames
1925// don't need to be encoded in different resolutions, since the rotation is
1926// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001927TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001928 ASSERT_TRUE(CreatePeerConnectionWrappers());
1929 ConnectFakeSignaling();
1930 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001931 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001932 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001933 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001934 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1935
1936 // Wait for video frames to be received by both sides.
1937 caller()->CreateAndSetAndSignalOffer();
1938 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1939 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1940 callee()->min_video_frames_received_per_track() > 0,
1941 kMaxWaitForFramesMs);
1942
1943 // Ensure that the aspect ratio is unmodified.
1944 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1945 // not just assumed.
1946 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1947 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1948 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1949 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1950 // Ensure that the CVO bits were surfaced to the renderer.
1951 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1952 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1953}
1954
1955// Test that when the CVO extension isn't supported, video is rotated the
1956// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001957TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001958 ASSERT_TRUE(CreatePeerConnectionWrappers());
1959 ConnectFakeSignaling();
1960 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001961 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001962 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001963 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001964 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1965
1966 // Remove the CVO extension from the offered SDP.
1967 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1968 cricket::VideoContentDescription* video =
1969 GetFirstVideoContentDescription(desc);
1970 video->ClearRtpHeaderExtensions();
1971 });
1972 // Wait for video frames to be received by both sides.
1973 caller()->CreateAndSetAndSignalOffer();
1974 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1975 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1976 callee()->min_video_frames_received_per_track() > 0,
1977 kMaxWaitForFramesMs);
1978
1979 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1980 // rotation.
1981 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1982 // not just assumed.
1983 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1984 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1985 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1986 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1987 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1988 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1989 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1990}
1991
deadbeef1dcb1642017-03-29 21:08:16 -07001992// Test that if the answerer rejects the audio m= section, no audio is sent or
1993// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001994TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07001995 ASSERT_TRUE(CreatePeerConnectionWrappers());
1996 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001997 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001998 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1999 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2000 // it will reject the audio m= section completely.
2001 PeerConnectionInterface::RTCOfferAnswerOptions options;
2002 options.offer_to_receive_audio = 0;
2003 callee()->SetOfferAnswerOptions(options);
2004 } else {
2005 // Stopping the audio RtpTransceiver will cause the media section to be
2006 // rejected in the answer.
2007 callee()->SetRemoteOfferHandler([this] {
2008 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2009 });
2010 }
Steve Anton15324772018-01-16 10:26:49 -08002011 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002012 // Do offer/answer and wait for successful end-to-end video frames.
2013 caller()->CreateAndSetAndSignalOffer();
2014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002015 MediaExpectations media_expectations;
2016 media_expectations.ExpectBidirectionalVideo();
2017 media_expectations.ExpectNoAudio();
2018 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2019
deadbeef1dcb1642017-03-29 21:08:16 -07002020 // Sanity check that the callee's description has a rejected audio section.
2021 ASSERT_NE(nullptr, callee()->pc()->local_description());
2022 const ContentInfo* callee_audio_content =
2023 GetFirstAudioContent(callee()->pc()->local_description()->description());
2024 ASSERT_NE(nullptr, callee_audio_content);
2025 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002026 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2027 // The caller's transceiver should have stopped after receiving the answer.
2028 EXPECT_TRUE(caller()
2029 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2030 ->stopped());
2031 }
deadbeef1dcb1642017-03-29 21:08:16 -07002032}
2033
2034// Test that if the answerer rejects the video m= section, no video is sent or
2035// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002036TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002037 ASSERT_TRUE(CreatePeerConnectionWrappers());
2038 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002039 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002040 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2041 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2042 // it will reject the video m= section completely.
2043 PeerConnectionInterface::RTCOfferAnswerOptions options;
2044 options.offer_to_receive_video = 0;
2045 callee()->SetOfferAnswerOptions(options);
2046 } else {
2047 // Stopping the video RtpTransceiver will cause the media section to be
2048 // rejected in the answer.
2049 callee()->SetRemoteOfferHandler([this] {
2050 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2051 });
2052 }
Steve Anton15324772018-01-16 10:26:49 -08002053 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002054 // Do offer/answer and wait for successful end-to-end audio frames.
2055 caller()->CreateAndSetAndSignalOffer();
2056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002057 MediaExpectations media_expectations;
2058 media_expectations.ExpectBidirectionalAudio();
2059 media_expectations.ExpectNoVideo();
2060 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2061
deadbeef1dcb1642017-03-29 21:08:16 -07002062 // Sanity check that the callee's description has a rejected video section.
2063 ASSERT_NE(nullptr, callee()->pc()->local_description());
2064 const ContentInfo* callee_video_content =
2065 GetFirstVideoContent(callee()->pc()->local_description()->description());
2066 ASSERT_NE(nullptr, callee_video_content);
2067 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002068 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2069 // The caller's transceiver should have stopped after receiving the answer.
2070 EXPECT_TRUE(caller()
2071 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2072 ->stopped());
2073 }
deadbeef1dcb1642017-03-29 21:08:16 -07002074}
2075
2076// Test that if the answerer rejects both audio and video m= sections, nothing
2077// bad happens.
2078// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2079// test anything but the fact that negotiation succeeds, which doesn't mean
2080// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002081TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002082 ASSERT_TRUE(CreatePeerConnectionWrappers());
2083 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002084 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002085 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2086 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2087 // will reject both audio and video m= sections.
2088 PeerConnectionInterface::RTCOfferAnswerOptions options;
2089 options.offer_to_receive_audio = 0;
2090 options.offer_to_receive_video = 0;
2091 callee()->SetOfferAnswerOptions(options);
2092 } else {
2093 callee()->SetRemoteOfferHandler([this] {
2094 // Stopping all transceivers will cause all media sections to be rejected.
2095 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2096 transceiver->Stop();
2097 }
2098 });
2099 }
deadbeef1dcb1642017-03-29 21:08:16 -07002100 // Do offer/answer and wait for stable signaling state.
2101 caller()->CreateAndSetAndSignalOffer();
2102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002103
deadbeef1dcb1642017-03-29 21:08:16 -07002104 // Sanity check that the callee's description has rejected m= sections.
2105 ASSERT_NE(nullptr, callee()->pc()->local_description());
2106 const ContentInfo* callee_audio_content =
2107 GetFirstAudioContent(callee()->pc()->local_description()->description());
2108 ASSERT_NE(nullptr, callee_audio_content);
2109 EXPECT_TRUE(callee_audio_content->rejected);
2110 const ContentInfo* callee_video_content =
2111 GetFirstVideoContent(callee()->pc()->local_description()->description());
2112 ASSERT_NE(nullptr, callee_video_content);
2113 EXPECT_TRUE(callee_video_content->rejected);
2114}
2115
2116// This test sets up an audio and video call between two parties. After the
2117// call runs for a while, the caller sends an updated offer with video being
2118// rejected. Once the re-negotiation is done, the video flow should stop and
2119// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002120TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002121 ASSERT_TRUE(CreatePeerConnectionWrappers());
2122 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002123 caller()->AddAudioVideoTracks();
2124 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002125 caller()->CreateAndSetAndSignalOffer();
2126 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002127 {
2128 MediaExpectations media_expectations;
2129 media_expectations.ExpectBidirectionalAudioAndVideo();
2130 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2131 }
deadbeef1dcb1642017-03-29 21:08:16 -07002132 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002133 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2134 caller()->SetGeneratedSdpMunger(
2135 [](cricket::SessionDescription* description) {
2136 for (cricket::ContentInfo& content : description->contents()) {
2137 if (cricket::IsVideoContent(&content)) {
2138 content.rejected = true;
2139 }
2140 }
2141 });
2142 } else {
2143 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2144 }
deadbeef1dcb1642017-03-29 21:08:16 -07002145 caller()->CreateAndSetAndSignalOffer();
2146 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2147
2148 // Sanity check that the caller's description has a rejected video section.
2149 ASSERT_NE(nullptr, caller()->pc()->local_description());
2150 const ContentInfo* caller_video_content =
2151 GetFirstVideoContent(caller()->pc()->local_description()->description());
2152 ASSERT_NE(nullptr, caller_video_content);
2153 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002154 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002155 {
2156 MediaExpectations media_expectations;
2157 media_expectations.ExpectBidirectionalAudio();
2158 media_expectations.ExpectNoVideo();
2159 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2160 }
deadbeef1dcb1642017-03-29 21:08:16 -07002161}
2162
2163// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2164// is needed to support legacy endpoints.
2165// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2166// add a test for an end-to-end test without MID signaling either (basically,
2167// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002168TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002169 ASSERT_TRUE(CreatePeerConnectionWrappers());
2170 ConnectFakeSignaling();
2171 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002172 caller()->AddAudioVideoTracks();
2173 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002174 // Remove SSRCs and MSIDs from the received offer SDP.
2175 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002176 caller()->CreateAndSetAndSignalOffer();
2177 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002178 MediaExpectations media_expectations;
2179 media_expectations.ExpectBidirectionalAudioAndVideo();
2180 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002181}
2182
2183// Test that if two video tracks are sent (from caller to callee, in this test),
2184// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002185TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002186 ASSERT_TRUE(CreatePeerConnectionWrappers());
2187 ConnectFakeSignaling();
2188 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002189 caller()->AddAudioVideoTracks();
2190 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002191 caller()->CreateAndSetAndSignalOffer();
2192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002193 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002194
2195 MediaExpectations media_expectations;
2196 media_expectations.CalleeExpectsSomeAudioAndVideo();
2197 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002198}
2199
2200static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2201 bool first = true;
2202 for (cricket::ContentInfo& content : desc->contents()) {
2203 if (first) {
2204 first = false;
2205 continue;
2206 }
2207 content.bundle_only = true;
2208 }
2209 first = true;
2210 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2211 if (first) {
2212 first = false;
2213 continue;
2214 }
2215 transport.description.ice_ufrag.clear();
2216 transport.description.ice_pwd.clear();
2217 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2218 transport.description.identity_fingerprint.reset(nullptr);
2219 }
2220}
2221
2222// Test that if applying a true "max bundle" offer, which uses ports of 0,
2223// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2224// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2225// successfully and media flows.
2226// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2227// TODO(deadbeef): Won't need this test once we start generating actual
2228// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002229TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002230 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2231 ASSERT_TRUE(CreatePeerConnectionWrappers());
2232 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002233 caller()->AddAudioVideoTracks();
2234 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002235 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2236 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2237 // but the first m= section.
2238 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2239 caller()->CreateAndSetAndSignalOffer();
2240 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002241 MediaExpectations media_expectations;
2242 media_expectations.ExpectBidirectionalAudioAndVideo();
2243 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002244}
2245
2246// Test that we can receive the audio output level from a remote audio track.
2247// TODO(deadbeef): Use a fake audio source and verify that the output level is
2248// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002250 ASSERT_TRUE(CreatePeerConnectionWrappers());
2251 ConnectFakeSignaling();
2252 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002253 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002254 caller()->CreateAndSetAndSignalOffer();
2255 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2256
2257 // Get the audio output level stats. Note that the level is not available
2258 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002259 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002260 kMaxWaitForFramesMs);
2261}
2262
2263// Test that an audio input level is reported.
2264// TODO(deadbeef): Use a fake audio source and verify that the input level is
2265// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002266TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002267 ASSERT_TRUE(CreatePeerConnectionWrappers());
2268 ConnectFakeSignaling();
2269 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002270 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002271 caller()->CreateAndSetAndSignalOffer();
2272 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2273
2274 // Get the audio input level stats. The level should be available very
2275 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002276 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002277 kMaxWaitForStatsMs);
2278}
2279
2280// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002281TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002282 ASSERT_TRUE(CreatePeerConnectionWrappers());
2283 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002284 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002285 // Do offer/answer, wait for the callee to receive some frames.
2286 caller()->CreateAndSetAndSignalOffer();
2287 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002288
2289 MediaExpectations media_expectations;
2290 media_expectations.CalleeExpectsSomeAudioAndVideo();
2291 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002292
2293 // Get a handle to the remote tracks created, so they can be used as GetStats
2294 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002295 for (auto receiver : callee()->pc()->GetReceivers()) {
2296 // We received frames, so we definitely should have nonzero "received bytes"
2297 // stats at this point.
2298 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2299 0);
2300 }
deadbeef1dcb1642017-03-29 21:08:16 -07002301}
2302
2303// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002304TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002305 ASSERT_TRUE(CreatePeerConnectionWrappers());
2306 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002307 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002308 auto audio_track = caller()->CreateLocalAudioTrack();
2309 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002310 caller()->AddTrack(audio_track);
2311 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002312 // Do offer/answer, wait for the callee to receive some frames.
2313 caller()->CreateAndSetAndSignalOffer();
2314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002315 MediaExpectations media_expectations;
2316 media_expectations.CalleeExpectsSomeAudioAndVideo();
2317 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002318
2319 // The callee received frames, so we definitely should have nonzero "sent
2320 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002321 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2322 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2323}
2324
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002325// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002326TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002327 ASSERT_TRUE(CreatePeerConnectionWrappers());
2328 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002329 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002330
Steve Anton15324772018-01-16 10:26:49 -08002331 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002332
2333 // Do offer/answer, wait for the callee to receive some frames.
2334 caller()->CreateAndSetAndSignalOffer();
2335 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2336
2337 // Get the remote audio track created on the receiver, so they can be used as
2338 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002339 auto receivers = callee()->pc()->GetReceivers();
2340 ASSERT_EQ(1u, receivers.size());
2341 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002342
2343 // Get the audio output level stats. Note that the level is not available
2344 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002345 EXPECT_TRUE_WAIT(
2346 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2347 0,
2348 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002349}
2350
deadbeefd8ad7882017-04-18 16:01:17 -07002351// Test that we can get stats (using the new stats implemnetation) for
2352// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2353// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002354TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002355 GetStatsForUnsignaledStreamWithNewStatsApi) {
2356 ASSERT_TRUE(CreatePeerConnectionWrappers());
2357 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002358 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002359 // Remove SSRCs and MSIDs from the received offer SDP.
2360 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2361 caller()->CreateAndSetAndSignalOffer();
2362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002363 MediaExpectations media_expectations;
2364 media_expectations.CalleeExpectsSomeAudio(1);
2365 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002366
2367 // We received a frame, so we should have nonzero "bytes received" stats for
2368 // the unsignaled stream, if stats are working for it.
2369 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2370 callee()->NewGetStats();
2371 ASSERT_NE(nullptr, report);
2372 auto inbound_stream_stats =
2373 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2374 ASSERT_EQ(1U, inbound_stream_stats.size());
2375 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2376 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002377 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2378}
2379
2380// Test that we can successfully get the media related stats (audio level
2381// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002382TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002383 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2384 ASSERT_TRUE(CreatePeerConnectionWrappers());
2385 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002386 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002387 // Remove SSRCs and MSIDs from the received offer SDP.
2388 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2389 caller()->CreateAndSetAndSignalOffer();
2390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002391 MediaExpectations media_expectations;
2392 media_expectations.CalleeExpectsSomeAudio(1);
2393 media_expectations.CalleeExpectsSomeVideo(1);
2394 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002395
2396 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2397 callee()->NewGetStats();
2398 ASSERT_NE(nullptr, report);
2399
2400 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2401 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2402 ASSERT_GE(audio_index, 0);
2403 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002404}
2405
deadbeef4e2deab2017-09-20 13:56:21 -07002406// Helper for test below.
2407void ModifySsrcs(cricket::SessionDescription* desc) {
2408 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002409 for (cricket::StreamParams& stream :
2410 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002411 for (uint32_t& ssrc : stream.ssrcs) {
2412 ssrc = rtc::CreateRandomId();
2413 }
2414 }
2415 }
2416}
2417
2418// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2419// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2420// This should result in two "RTCInboundRTPStreamStats", but only one
2421// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2422// being reset to 0 once the SSRC change occurs.
2423//
2424// Regression test for this bug:
2425// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2426//
2427// The bug causes the track stats to only represent one of the two streams:
2428// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2429// that the track stat counters would reset to 0 when the new stream is
2430// received, and a 50% chance that they'll stop updating (while
2431// "concealed_samples" continues increasing, due to silence being generated for
2432// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002433TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002434 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002435 ASSERT_TRUE(CreatePeerConnectionWrappers());
2436 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002437 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002438 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2439 // that doesn't signal SSRCs (from the callee's perspective).
2440 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2441 caller()->CreateAndSetAndSignalOffer();
2442 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2443 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002444 {
2445 MediaExpectations media_expectations;
2446 media_expectations.CalleeExpectsSomeAudio(50);
2447 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2448 }
deadbeef4e2deab2017-09-20 13:56:21 -07002449 // Some audio frames were received, so we should have nonzero "samples
2450 // received" for the track.
2451 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2452 callee()->NewGetStats();
2453 ASSERT_NE(nullptr, report);
2454 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2455 ASSERT_EQ(1U, track_stats.size());
2456 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2457 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2458 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2459
2460 // Create a new offer and munge it to cause the caller to use a new SSRC.
2461 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2462 caller()->CreateAndSetAndSignalOffer();
2463 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2464 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2465 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002466 {
2467 MediaExpectations media_expectations;
2468 media_expectations.CalleeExpectsSomeAudio(25);
2469 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2470 }
deadbeef4e2deab2017-09-20 13:56:21 -07002471
2472 report = callee()->NewGetStats();
2473 ASSERT_NE(nullptr, report);
2474 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2475 ASSERT_EQ(1U, track_stats.size());
2476 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2477 // The "total samples received" stat should only be greater than it was
2478 // before.
2479 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2480 // Right now, the new SSRC will cause the counters to reset to 0.
2481 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2482
2483 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002484 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002485 // good sign that we're seeing stats from the old stream that's no longer
2486 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002487 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002488 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2489 EXPECT_LT(*track_stats[0]->concealed_samples,
2490 *track_stats[0]->total_samples_received *
2491 kAcceptableConcealedSamplesPercentage);
2492
2493 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2494 // sanity check that the SSRC really changed.
2495 // TODO(deadbeef): This isn't working right now, because we're not returning
2496 // *any* stats for the inactive stream. Uncomment when the bug is completely
2497 // fixed.
2498 // auto inbound_stream_stats =
2499 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2500 // ASSERT_EQ(2U, inbound_stream_stats.size());
2501}
2502
deadbeef1dcb1642017-03-29 21:08:16 -07002503// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002504TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002505 PeerConnectionFactory::Options dtls_10_options;
2506 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2507 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2508 dtls_10_options));
2509 ConnectFakeSignaling();
2510 // Do normal offer/answer and wait for some frames to be received in each
2511 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002512 caller()->AddAudioVideoTracks();
2513 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002514 caller()->CreateAndSetAndSignalOffer();
2515 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002516 MediaExpectations media_expectations;
2517 media_expectations.ExpectBidirectionalAudioAndVideo();
2518 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002519}
2520
2521// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002522TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002523 PeerConnectionFactory::Options dtls_10_options;
2524 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2525 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2526 dtls_10_options));
2527 ConnectFakeSignaling();
2528 // Register UMA observer before signaling begins.
2529 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2530 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2531 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002532 caller()->AddAudioVideoTracks();
2533 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002534 caller()->CreateAndSetAndSignalOffer();
2535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2536 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002537 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002538 kDefaultTimeout);
2539 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002540 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002541 EXPECT_EQ(1,
2542 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2543 kDefaultSrtpCryptoSuite));
2544}
2545
2546// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002547TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002548 PeerConnectionFactory::Options dtls_12_options;
2549 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2550 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2551 dtls_12_options));
2552 ConnectFakeSignaling();
2553 // Register UMA observer before signaling begins.
2554 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2555 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2556 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002557 caller()->AddAudioVideoTracks();
2558 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002559 caller()->CreateAndSetAndSignalOffer();
2560 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2561 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002562 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002563 kDefaultTimeout);
2564 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002565 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002566 EXPECT_EQ(1,
2567 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2568 kDefaultSrtpCryptoSuite));
2569}
2570
2571// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2572// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002573TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002574 PeerConnectionFactory::Options caller_options;
2575 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2576 PeerConnectionFactory::Options callee_options;
2577 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2578 ASSERT_TRUE(
2579 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2580 ConnectFakeSignaling();
2581 // Do normal offer/answer and wait for some frames to be received in each
2582 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002583 caller()->AddAudioVideoTracks();
2584 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002585 caller()->CreateAndSetAndSignalOffer();
2586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002587 MediaExpectations media_expectations;
2588 media_expectations.ExpectBidirectionalAudioAndVideo();
2589 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002590}
2591
2592// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2593// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002595 PeerConnectionFactory::Options caller_options;
2596 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2597 PeerConnectionFactory::Options callee_options;
2598 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2599 ASSERT_TRUE(
2600 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2601 ConnectFakeSignaling();
2602 // Do normal offer/answer and wait for some frames to be received in each
2603 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002604 caller()->AddAudioVideoTracks();
2605 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002606 caller()->CreateAndSetAndSignalOffer();
2607 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002608 MediaExpectations media_expectations;
2609 media_expectations.ExpectBidirectionalAudioAndVideo();
2610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002611}
2612
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002613// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2614// works as expected; the cipher should only be used if enabled by both sides.
2615TEST_P(PeerConnectionIntegrationTest,
2616 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2617 PeerConnectionFactory::Options caller_options;
2618 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2619 PeerConnectionFactory::Options callee_options;
2620 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2621 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2622 TestNegotiatedCipherSuite(caller_options, callee_options,
2623 expected_cipher_suite);
2624}
2625
2626TEST_P(PeerConnectionIntegrationTest,
2627 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2628 PeerConnectionFactory::Options caller_options;
2629 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2630 PeerConnectionFactory::Options callee_options;
2631 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2632 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2633 TestNegotiatedCipherSuite(caller_options, callee_options,
2634 expected_cipher_suite);
2635}
2636
2637TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2638 PeerConnectionFactory::Options caller_options;
2639 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2640 PeerConnectionFactory::Options callee_options;
2641 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2642 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2643 TestNegotiatedCipherSuite(caller_options, callee_options,
2644 expected_cipher_suite);
2645}
2646
deadbeef1dcb1642017-03-29 21:08:16 -07002647// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002648TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002649 bool local_gcm_enabled = false;
2650 bool remote_gcm_enabled = false;
2651 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2652 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2653 expected_cipher_suite);
2654}
2655
2656// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002657TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002658 bool local_gcm_enabled = true;
2659 bool remote_gcm_enabled = true;
2660 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2661 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2662 expected_cipher_suite);
2663}
2664
2665// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002666TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002667 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2668 bool local_gcm_enabled = true;
2669 bool remote_gcm_enabled = false;
2670 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2671 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2672 expected_cipher_suite);
2673}
2674
2675// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002676TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002677 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2678 bool local_gcm_enabled = false;
2679 bool remote_gcm_enabled = true;
2680 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2681 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2682 expected_cipher_suite);
2683}
2684
deadbeef7914b8c2017-04-21 03:23:33 -07002685// Verify that media can be transmitted end-to-end when GCM crypto suites are
2686// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2687// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2688// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002689TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002690 PeerConnectionFactory::Options gcm_options;
2691 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2692 ASSERT_TRUE(
2693 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2694 ConnectFakeSignaling();
2695 // Do normal offer/answer and wait for some frames to be received in each
2696 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002697 caller()->AddAudioVideoTracks();
2698 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002699 caller()->CreateAndSetAndSignalOffer();
2700 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002701 MediaExpectations media_expectations;
2702 media_expectations.ExpectBidirectionalAudioAndVideo();
2703 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002704}
2705
deadbeef1dcb1642017-03-29 21:08:16 -07002706// This test sets up a call between two parties with audio, video and an RTP
2707// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002708TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002709 FakeConstraints setup_constraints;
2710 setup_constraints.SetAllowRtpDataChannels();
2711 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2712 &setup_constraints));
2713 ConnectFakeSignaling();
2714 // Expect that data channel created on caller side will show up for callee as
2715 // well.
2716 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002717 caller()->AddAudioVideoTracks();
2718 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002719 caller()->CreateAndSetAndSignalOffer();
2720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2721 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002722 MediaExpectations media_expectations;
2723 media_expectations.ExpectBidirectionalAudioAndVideo();
2724 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002725 ASSERT_NE(nullptr, caller()->data_channel());
2726 ASSERT_NE(nullptr, callee()->data_channel());
2727 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2728 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2729
2730 // Ensure data can be sent in both directions.
2731 std::string data = "hello world";
2732 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2733 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2734 kDefaultTimeout);
2735 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2736 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2737 kDefaultTimeout);
2738}
2739
2740// Ensure that an RTP data channel is signaled as closed for the caller when
2741// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002742TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002743 RtpDataChannelSignaledClosedInCalleeOffer) {
2744 // Same procedure as above test.
2745 FakeConstraints setup_constraints;
2746 setup_constraints.SetAllowRtpDataChannels();
2747 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2748 &setup_constraints));
2749 ConnectFakeSignaling();
2750 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002751 caller()->AddAudioVideoTracks();
2752 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002753 caller()->CreateAndSetAndSignalOffer();
2754 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2755 ASSERT_NE(nullptr, caller()->data_channel());
2756 ASSERT_NE(nullptr, callee()->data_channel());
2757 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2758 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2759
2760 // Close the data channel on the callee, and do an updated offer/answer.
2761 callee()->data_channel()->Close();
2762 callee()->CreateAndSetAndSignalOffer();
2763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2764 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2765 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2766}
2767
2768// Tests that data is buffered in an RTP data channel until an observer is
2769// registered for it.
2770//
2771// NOTE: RTP data channels can receive data before the underlying
2772// transport has detected that a channel is writable and thus data can be
2773// received before the data channel state changes to open. That is hard to test
2774// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002775TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002776 DataBufferedUntilRtpDataChannelObserverRegistered) {
2777 // Use fake clock and simulated network delay so that we predictably can wait
2778 // until an SCTP message has been delivered without "sleep()"ing.
2779 rtc::ScopedFakeClock fake_clock;
2780 // Some things use a time of "0" as a special value, so we need to start out
2781 // the fake clock at a nonzero time.
2782 // TODO(deadbeef): Fix this.
2783 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2784 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2785 virtual_socket_server()->UpdateDelayDistribution();
2786
2787 FakeConstraints constraints;
2788 constraints.SetAllowRtpDataChannels();
2789 ASSERT_TRUE(
2790 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2791 ConnectFakeSignaling();
2792 caller()->CreateDataChannel();
2793 caller()->CreateAndSetAndSignalOffer();
2794 ASSERT_TRUE(caller()->data_channel() != nullptr);
2795 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2796 kDefaultTimeout, fake_clock);
2797 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2798 kDefaultTimeout, fake_clock);
2799 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2800 callee()->data_channel()->state(), kDefaultTimeout,
2801 fake_clock);
2802
2803 // Unregister the observer which is normally automatically registered.
2804 callee()->data_channel()->UnregisterObserver();
2805 // Send data and advance fake clock until it should have been received.
2806 std::string data = "hello world";
2807 caller()->data_channel()->Send(DataBuffer(data));
2808 SIMULATED_WAIT(false, 50, fake_clock);
2809
2810 // Attach data channel and expect data to be received immediately. Note that
2811 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2812 // further, but data can be received even if the callback is asynchronous.
2813 MockDataChannelObserver new_observer(callee()->data_channel());
2814 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2815 fake_clock);
2816}
2817
2818// This test sets up a call between two parties with audio, video and but only
2819// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002820TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002821 FakeConstraints setup_constraints_1;
2822 setup_constraints_1.SetAllowRtpDataChannels();
2823 // Must disable DTLS to make negotiation succeed.
2824 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2825 false);
2826 FakeConstraints setup_constraints_2;
2827 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2828 false);
2829 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2830 &setup_constraints_1, &setup_constraints_2));
2831 ConnectFakeSignaling();
2832 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002833 caller()->AddAudioVideoTracks();
2834 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002835 caller()->CreateAndSetAndSignalOffer();
2836 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2837 // The caller should still have a data channel, but it should be closed, and
2838 // one should ever have been created for the callee.
2839 EXPECT_TRUE(caller()->data_channel() != nullptr);
2840 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2841 EXPECT_EQ(nullptr, callee()->data_channel());
2842}
2843
2844// This test sets up a call between two parties with audio, and video. When
2845// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002846TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002847 FakeConstraints setup_constraints;
2848 setup_constraints.SetAllowRtpDataChannels();
2849 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2850 &setup_constraints));
2851 ConnectFakeSignaling();
2852 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002853 caller()->AddAudioVideoTracks();
2854 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002855 caller()->CreateAndSetAndSignalOffer();
2856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2857 // Create data channel and do new offer and answer.
2858 caller()->CreateDataChannel();
2859 caller()->CreateAndSetAndSignalOffer();
2860 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2861 ASSERT_NE(nullptr, caller()->data_channel());
2862 ASSERT_NE(nullptr, callee()->data_channel());
2863 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2864 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2865 // Ensure data can be sent in both directions.
2866 std::string data = "hello world";
2867 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2868 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2869 kDefaultTimeout);
2870 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2871 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2872 kDefaultTimeout);
2873}
2874
2875#ifdef HAVE_SCTP
2876
2877// This test sets up a call between two parties with audio, video and an SCTP
2878// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002879TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002880 ASSERT_TRUE(CreatePeerConnectionWrappers());
2881 ConnectFakeSignaling();
2882 // Expect that data channel created on caller side will show up for callee as
2883 // well.
2884 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002885 caller()->AddAudioVideoTracks();
2886 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002887 caller()->CreateAndSetAndSignalOffer();
2888 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2889 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002890 MediaExpectations media_expectations;
2891 media_expectations.ExpectBidirectionalAudioAndVideo();
2892 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002893 // Caller data channel should already exist (it created one). Callee data
2894 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2895 ASSERT_NE(nullptr, caller()->data_channel());
2896 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2897 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2898 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2899
2900 // Ensure data can be sent in both directions.
2901 std::string data = "hello world";
2902 caller()->data_channel()->Send(DataBuffer(data));
2903 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2904 kDefaultTimeout);
2905 callee()->data_channel()->Send(DataBuffer(data));
2906 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2907 kDefaultTimeout);
2908}
2909
2910// Ensure that when the callee closes an SCTP data channel, the closing
2911// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002912TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002913 // Same procedure as above test.
2914 ASSERT_TRUE(CreatePeerConnectionWrappers());
2915 ConnectFakeSignaling();
2916 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002917 caller()->AddAudioVideoTracks();
2918 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002919 caller()->CreateAndSetAndSignalOffer();
2920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2921 ASSERT_NE(nullptr, caller()->data_channel());
2922 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2923 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2924 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2925
2926 // Close the data channel on the callee side, and wait for it to reach the
2927 // "closed" state on both sides.
2928 callee()->data_channel()->Close();
2929 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2930 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2931}
2932
Seth Hampson2f0d7022018-02-20 11:54:42 -08002933TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07002934 ASSERT_TRUE(CreatePeerConnectionWrappers());
2935 ConnectFakeSignaling();
2936 webrtc::DataChannelInit init;
2937 init.id = 53;
2938 init.maxRetransmits = 52;
2939 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002940 caller()->AddAudioVideoTracks();
2941 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002942 caller()->CreateAndSetAndSignalOffer();
2943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002944 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2945 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002946 EXPECT_EQ(init.id, callee()->data_channel()->id());
2947 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2948 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2949 EXPECT_FALSE(callee()->data_channel()->negotiated());
2950}
2951
deadbeef1dcb1642017-03-29 21:08:16 -07002952// Test usrsctp's ability to process unordered data stream, where data actually
2953// arrives out of order using simulated delays. Previously there have been some
2954// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002955TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002956 // Introduce random network delays.
2957 // Otherwise it's not a true "unordered" test.
2958 virtual_socket_server()->set_delay_mean(20);
2959 virtual_socket_server()->set_delay_stddev(5);
2960 virtual_socket_server()->UpdateDelayDistribution();
2961 // Normal procedure, but with unordered data channel config.
2962 ASSERT_TRUE(CreatePeerConnectionWrappers());
2963 ConnectFakeSignaling();
2964 webrtc::DataChannelInit init;
2965 init.ordered = false;
2966 caller()->CreateDataChannel(&init);
2967 caller()->CreateAndSetAndSignalOffer();
2968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2969 ASSERT_NE(nullptr, caller()->data_channel());
2970 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2971 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2972 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2973
2974 static constexpr int kNumMessages = 100;
2975 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2976 static constexpr size_t kMaxMessageSize = 4096;
2977 // Create and send random messages.
2978 std::vector<std::string> sent_messages;
2979 for (int i = 0; i < kNumMessages; ++i) {
2980 size_t length =
2981 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2982 std::string message;
2983 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2984 caller()->data_channel()->Send(DataBuffer(message));
2985 callee()->data_channel()->Send(DataBuffer(message));
2986 sent_messages.push_back(message);
2987 }
2988
2989 // Wait for all messages to be received.
2990 EXPECT_EQ_WAIT(kNumMessages,
2991 caller()->data_observer()->received_message_count(),
2992 kDefaultTimeout);
2993 EXPECT_EQ_WAIT(kNumMessages,
2994 callee()->data_observer()->received_message_count(),
2995 kDefaultTimeout);
2996
2997 // Sort and compare to make sure none of the messages were corrupted.
2998 std::vector<std::string> caller_received_messages =
2999 caller()->data_observer()->messages();
3000 std::vector<std::string> callee_received_messages =
3001 callee()->data_observer()->messages();
3002 std::sort(sent_messages.begin(), sent_messages.end());
3003 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3004 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3005 EXPECT_EQ(sent_messages, caller_received_messages);
3006 EXPECT_EQ(sent_messages, callee_received_messages);
3007}
3008
3009// This test sets up a call between two parties with audio, and video. When
3010// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003011TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003012 ASSERT_TRUE(CreatePeerConnectionWrappers());
3013 ConnectFakeSignaling();
3014 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003015 caller()->AddAudioVideoTracks();
3016 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003017 caller()->CreateAndSetAndSignalOffer();
3018 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3019 // Create data channel and do new offer and answer.
3020 caller()->CreateDataChannel();
3021 caller()->CreateAndSetAndSignalOffer();
3022 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3023 // Caller data channel should already exist (it created one). Callee data
3024 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3025 ASSERT_NE(nullptr, caller()->data_channel());
3026 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3027 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3028 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3029 // Ensure data can be sent in both directions.
3030 std::string data = "hello world";
3031 caller()->data_channel()->Send(DataBuffer(data));
3032 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3033 kDefaultTimeout);
3034 callee()->data_channel()->Send(DataBuffer(data));
3035 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3036 kDefaultTimeout);
3037}
3038
deadbeef7914b8c2017-04-21 03:23:33 -07003039// Set up a connection initially just using SCTP data channels, later upgrading
3040// to audio/video, ensuring frames are received end-to-end. Effectively the
3041// inverse of the test above.
3042// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003043TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003044 ASSERT_TRUE(CreatePeerConnectionWrappers());
3045 ConnectFakeSignaling();
3046 // Do initial offer/answer with just data channel.
3047 caller()->CreateDataChannel();
3048 caller()->CreateAndSetAndSignalOffer();
3049 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3050 // Wait until data can be sent over the data channel.
3051 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3052 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3053 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3054
3055 // Do subsequent offer/answer with two-way audio and video. Audio and video
3056 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003057 caller()->AddAudioVideoTracks();
3058 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003059 caller()->CreateAndSetAndSignalOffer();
3060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003061 MediaExpectations media_expectations;
3062 media_expectations.ExpectBidirectionalAudioAndVideo();
3063 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003064}
3065
deadbeef8b7e9ad2017-05-25 09:38:55 -07003066static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003067 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003068 GetFirstDataContentDescription(desc);
3069 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003070 dcd_offer->set_use_sctpmap(false);
3071 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3072}
3073
3074// Test that the data channel works when a spec-compliant SCTP m= section is
3075// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3076// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003077TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003078 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3079 ASSERT_TRUE(CreatePeerConnectionWrappers());
3080 ConnectFakeSignaling();
3081 caller()->CreateDataChannel();
3082 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3083 caller()->CreateAndSetAndSignalOffer();
3084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3085 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3086 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3087 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3088
3089 // Ensure data can be sent in both directions.
3090 std::string data = "hello world";
3091 caller()->data_channel()->Send(DataBuffer(data));
3092 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3093 kDefaultTimeout);
3094 callee()->data_channel()->Send(DataBuffer(data));
3095 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3096 kDefaultTimeout);
3097}
3098
deadbeef1dcb1642017-03-29 21:08:16 -07003099#endif // HAVE_SCTP
3100
3101// Test that the ICE connection and gathering states eventually reach
3102// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003103TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003104 ASSERT_TRUE(CreatePeerConnectionWrappers());
3105 ConnectFakeSignaling();
3106 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003107 caller()->AddAudioVideoTracks();
3108 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003109 caller()->CreateAndSetAndSignalOffer();
3110 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3111 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3112 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3113 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3114 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3115 // After the best candidate pair is selected and all candidates are signaled,
3116 // the ICE connection state should reach "complete".
3117 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3118 // answerer/"callee" by default) only reaches "connected". When this is
3119 // fixed, this test should be updated.
3120 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3121 caller()->ice_connection_state(), kDefaultTimeout);
3122 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3123 callee()->ice_connection_state(), kDefaultTimeout);
3124}
3125
Steve Antonede9ca52017-10-16 13:04:27 -07003126// Test that firewalling the ICE connection causes the clients to identify the
3127// disconnected state and then removing the firewall causes them to reconnect.
3128class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003129 : public PeerConnectionIntegrationBaseTest,
3130 public ::testing::WithParamInterface<
3131 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003132 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003133 PeerConnectionIntegrationIceStatesTest()
3134 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3135 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003136 }
3137
3138 void StartStunServer(const SocketAddress& server_address) {
3139 stun_server_.reset(
3140 cricket::TestStunServer::Create(network_thread(), server_address));
3141 }
3142
3143 bool TestIPv6() {
3144 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3145 }
3146
3147 void SetPortAllocatorFlags() {
3148 caller()->port_allocator()->set_flags(port_allocator_flags_);
3149 callee()->port_allocator()->set_flags(port_allocator_flags_);
3150 }
3151
3152 std::vector<SocketAddress> CallerAddresses() {
3153 std::vector<SocketAddress> addresses;
3154 addresses.push_back(SocketAddress("1.1.1.1", 0));
3155 if (TestIPv6()) {
3156 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3157 }
3158 return addresses;
3159 }
3160
3161 std::vector<SocketAddress> CalleeAddresses() {
3162 std::vector<SocketAddress> addresses;
3163 addresses.push_back(SocketAddress("2.2.2.2", 0));
3164 if (TestIPv6()) {
3165 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3166 }
3167 return addresses;
3168 }
3169
3170 void SetUpNetworkInterfaces() {
3171 // Remove the default interfaces added by the test infrastructure.
3172 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3173 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3174
3175 // Add network addresses for test.
3176 for (const auto& caller_address : CallerAddresses()) {
3177 caller()->network()->AddInterface(caller_address);
3178 }
3179 for (const auto& callee_address : CalleeAddresses()) {
3180 callee()->network()->AddInterface(callee_address);
3181 }
3182 }
3183
3184 private:
3185 uint32_t port_allocator_flags_;
3186 std::unique_ptr<cricket::TestStunServer> stun_server_;
3187};
3188
3189// Tests that the PeerConnection goes through all the ICE gathering/connection
3190// states over the duration of the call. This includes Disconnected and Failed
3191// states, induced by putting a firewall between the peers and waiting for them
3192// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003193TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3194 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3195 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3196 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003197
3198 const SocketAddress kStunServerAddress =
3199 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3200 StartStunServer(kStunServerAddress);
3201
3202 PeerConnectionInterface::RTCConfiguration config;
3203 PeerConnectionInterface::IceServer ice_stun_server;
3204 ice_stun_server.urls.push_back(
3205 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3206 kStunServerAddress.PortAsString());
3207 config.servers.push_back(ice_stun_server);
3208
3209 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3210 ConnectFakeSignaling();
3211 SetPortAllocatorFlags();
3212 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003213 caller()->AddAudioVideoTracks();
3214 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003215
3216 // Initial state before anything happens.
3217 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3218 caller()->ice_gathering_state());
3219 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3220 caller()->ice_connection_state());
3221
3222 // Start the call by creating the offer, setting it as the local description,
3223 // then sending it to the peer who will respond with an answer. This happens
3224 // asynchronously so that we can watch the states as it runs in the
3225 // background.
3226 caller()->CreateAndSetAndSignalOffer();
3227
Steve Anton83119dd2017-11-10 16:19:52 -08003228 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3229 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003230
3231 // Verify that the observer was notified of the intermediate transitions.
3232 EXPECT_THAT(caller()->ice_connection_state_history(),
3233 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3234 PeerConnectionInterface::kIceConnectionConnected,
3235 PeerConnectionInterface::kIceConnectionCompleted));
3236 EXPECT_THAT(caller()->ice_gathering_state_history(),
3237 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3238 PeerConnectionInterface::kIceGatheringComplete));
3239
3240 // Block connections to/from the caller and wait for ICE to become
3241 // disconnected.
3242 for (const auto& caller_address : CallerAddresses()) {
3243 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3244 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003245 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003246 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3247 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003248
3249 // Let ICE re-establish by removing the firewall rules.
3250 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003251 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003252 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3253 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003254
3255 // According to RFC7675, if there is no response within 30 seconds then the
3256 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003257 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003258 constexpr int kConsentTimeout = 30000;
3259 for (const auto& caller_address : CallerAddresses()) {
3260 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3261 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003262 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003263 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3264 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003265}
3266
3267// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3268// and that the statistics in the metric observers are updated correctly.
3269TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3270 ASSERT_TRUE(CreatePeerConnectionWrappers());
3271 ConnectFakeSignaling();
3272 SetPortAllocatorFlags();
3273 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003274 caller()->AddAudioVideoTracks();
3275 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003276
3277 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3278 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3279 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3280
3281 caller()->CreateAndSetAndSignalOffer();
3282
3283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3284
3285 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3286 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3287 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3288 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3289 if (TestIPv6()) {
3290 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3291 // connection.
3292 EXPECT_EQ(0u, num_best_ipv4);
3293 EXPECT_EQ(1u, num_best_ipv6);
3294 } else {
3295 EXPECT_EQ(1u, num_best_ipv4);
3296 EXPECT_EQ(0u, num_best_ipv6);
3297 }
3298
3299 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3300 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3301 webrtc::kIceCandidatePairHostHost));
3302 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3303 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3304 webrtc::kIceCandidatePairHostPublicHostPublic));
3305}
3306
3307constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3308 cricket::PORTALLOCATOR_DISABLE_STUN |
3309 cricket::PORTALLOCATOR_DISABLE_RELAY;
3310constexpr uint32_t kFlagsIPv6NoStun =
3311 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3312 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3313constexpr uint32_t kFlagsIPv4Stun =
3314 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3315
Seth Hampson2f0d7022018-02-20 11:54:42 -08003316INSTANTIATE_TEST_CASE_P(
3317 PeerConnectionIntegrationTest,
3318 PeerConnectionIntegrationIceStatesTest,
3319 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3320 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3321 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3322 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003323
deadbeef1dcb1642017-03-29 21:08:16 -07003324// This test sets up a call between two parties with audio and video.
3325// During the call, the caller restarts ICE and the test verifies that
3326// new ICE candidates are generated and audio and video still can flow, and the
3327// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003328TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003329 ASSERT_TRUE(CreatePeerConnectionWrappers());
3330 ConnectFakeSignaling();
3331 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003332 caller()->AddAudioVideoTracks();
3333 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003334 caller()->CreateAndSetAndSignalOffer();
3335 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3336 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3337 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3338 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3339 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3340
3341 // To verify that the ICE restart actually occurs, get
3342 // ufrag/password/candidates before and after restart.
3343 // Create an SDP string of the first audio candidate for both clients.
3344 const webrtc::IceCandidateCollection* audio_candidates_caller =
3345 caller()->pc()->local_description()->candidates(0);
3346 const webrtc::IceCandidateCollection* audio_candidates_callee =
3347 callee()->pc()->local_description()->candidates(0);
3348 ASSERT_GT(audio_candidates_caller->count(), 0u);
3349 ASSERT_GT(audio_candidates_callee->count(), 0u);
3350 std::string caller_candidate_pre_restart;
3351 ASSERT_TRUE(
3352 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3353 std::string callee_candidate_pre_restart;
3354 ASSERT_TRUE(
3355 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3356 const cricket::SessionDescription* desc =
3357 caller()->pc()->local_description()->description();
3358 std::string caller_ufrag_pre_restart =
3359 desc->transport_infos()[0].description.ice_ufrag;
3360 desc = callee()->pc()->local_description()->description();
3361 std::string callee_ufrag_pre_restart =
3362 desc->transport_infos()[0].description.ice_ufrag;
3363
3364 // Have the caller initiate an ICE restart.
3365 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3366 caller()->CreateAndSetAndSignalOffer();
3367 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3368 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3369 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3370 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3371 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3372
3373 // Grab the ufrags/candidates again.
3374 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3375 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3376 ASSERT_GT(audio_candidates_caller->count(), 0u);
3377 ASSERT_GT(audio_candidates_callee->count(), 0u);
3378 std::string caller_candidate_post_restart;
3379 ASSERT_TRUE(
3380 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3381 std::string callee_candidate_post_restart;
3382 ASSERT_TRUE(
3383 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3384 desc = caller()->pc()->local_description()->description();
3385 std::string caller_ufrag_post_restart =
3386 desc->transport_infos()[0].description.ice_ufrag;
3387 desc = callee()->pc()->local_description()->description();
3388 std::string callee_ufrag_post_restart =
3389 desc->transport_infos()[0].description.ice_ufrag;
3390 // Sanity check that an ICE restart was actually negotiated in SDP.
3391 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3392 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3393 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3394 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3395
3396 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003397 MediaExpectations media_expectations;
3398 media_expectations.ExpectBidirectionalAudioAndVideo();
3399 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003400}
3401
3402// Verify that audio/video can be received end-to-end when ICE renomination is
3403// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003404TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003405 PeerConnectionInterface::RTCConfiguration config;
3406 config.enable_ice_renomination = true;
3407 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3408 ConnectFakeSignaling();
3409 // Do normal offer/answer and wait for some frames to be received in each
3410 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003411 caller()->AddAudioVideoTracks();
3412 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003413 caller()->CreateAndSetAndSignalOffer();
3414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3415 // Sanity check that ICE renomination was actually negotiated.
3416 const cricket::SessionDescription* desc =
3417 caller()->pc()->local_description()->description();
3418 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003419 ASSERT_NE(
3420 info.description.transport_options.end(),
3421 std::find(info.description.transport_options.begin(),
3422 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003423 }
3424 desc = callee()->pc()->local_description()->description();
3425 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003426 ASSERT_NE(
3427 info.description.transport_options.end(),
3428 std::find(info.description.transport_options.begin(),
3429 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003430 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003431 MediaExpectations media_expectations;
3432 media_expectations.ExpectBidirectionalAudioAndVideo();
3433 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003434}
3435
Steve Anton6f25b092017-10-23 09:39:20 -07003436// With a max bundle policy and RTCP muxing, adding a new media description to
3437// the connection should not affect ICE at all because the new media will use
3438// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003439TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003440 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003441 PeerConnectionInterface::RTCConfiguration config;
3442 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3443 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3444 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3445 config, PeerConnectionInterface::RTCConfiguration()));
3446 ConnectFakeSignaling();
3447
Steve Anton15324772018-01-16 10:26:49 -08003448 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003449 caller()->CreateAndSetAndSignalOffer();
3450 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003451 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3452 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003453
3454 caller()->clear_ice_connection_state_history();
3455
Steve Anton15324772018-01-16 10:26:49 -08003456 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003457 caller()->CreateAndSetAndSignalOffer();
3458 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3459
3460 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3461}
3462
deadbeef1dcb1642017-03-29 21:08:16 -07003463// This test sets up a call between two parties with audio and video. It then
3464// renegotiates setting the video m-line to "port 0", then later renegotiates
3465// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003466TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003467 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3468 ASSERT_TRUE(CreatePeerConnectionWrappers());
3469 ConnectFakeSignaling();
3470
3471 // Do initial negotiation, only sending media from the caller. Will result in
3472 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003473 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003474 caller()->CreateAndSetAndSignalOffer();
3475 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3476
3477 // Negotiate again, disabling the video "m=" section (the callee will set the
3478 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003479 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3480 PeerConnectionInterface::RTCOfferAnswerOptions options;
3481 options.offer_to_receive_video = 0;
3482 callee()->SetOfferAnswerOptions(options);
3483 } else {
3484 callee()->SetRemoteOfferHandler([this] {
3485 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3486 });
3487 }
deadbeef1dcb1642017-03-29 21:08:16 -07003488 caller()->CreateAndSetAndSignalOffer();
3489 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3490 // Sanity check that video "m=" section was actually rejected.
3491 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3492 callee()->pc()->local_description()->description());
3493 ASSERT_NE(nullptr, answer_video_content);
3494 ASSERT_TRUE(answer_video_content->rejected);
3495
3496 // Enable video and do negotiation again, making sure video is received
3497 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003498 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3499 PeerConnectionInterface::RTCOfferAnswerOptions options;
3500 options.offer_to_receive_video = 1;
3501 callee()->SetOfferAnswerOptions(options);
3502 } else {
3503 // The caller's transceiver is stopped, so we need to add another track.
3504 auto caller_transceiver =
3505 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3506 EXPECT_TRUE(caller_transceiver->stopped());
3507 caller()->AddVideoTrack();
3508 }
3509 callee()->AddVideoTrack();
3510 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003511 caller()->CreateAndSetAndSignalOffer();
3512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003513
deadbeef1dcb1642017-03-29 21:08:16 -07003514 // Verify the caller receives frames from the newly added stream, and the
3515 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003516 MediaExpectations media_expectations;
3517 media_expectations.CalleeExpectsSomeAudio();
3518 media_expectations.ExpectBidirectionalVideo();
3519 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003520}
3521
3522// This test sets up a Jsep call between two parties with external
3523// VideoDecoderFactory.
3524// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3525// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003526TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003527 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3528 ASSERT_TRUE(CreatePeerConnectionWrappers());
3529 EnableVideoDecoderFactory();
3530 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003531 caller()->AddAudioVideoTracks();
3532 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003533 caller()->CreateAndSetAndSignalOffer();
3534 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003535 MediaExpectations media_expectations;
3536 media_expectations.ExpectBidirectionalAudioAndVideo();
3537 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003538}
3539
3540// This tests that if we negotiate after calling CreateSender but before we
3541// have a track, then set a track later, frames from the newly-set track are
3542// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003543TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003544 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3545 ASSERT_TRUE(CreatePeerConnectionWrappers());
3546 ConnectFakeSignaling();
3547 auto caller_audio_sender =
3548 caller()->pc()->CreateSender("audio", "caller_stream");
3549 auto caller_video_sender =
3550 caller()->pc()->CreateSender("video", "caller_stream");
3551 auto callee_audio_sender =
3552 callee()->pc()->CreateSender("audio", "callee_stream");
3553 auto callee_video_sender =
3554 callee()->pc()->CreateSender("video", "callee_stream");
3555 caller()->CreateAndSetAndSignalOffer();
3556 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3557 // Wait for ICE to complete, without any tracks being set.
3558 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3559 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3560 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3561 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3562 // Now set the tracks, and expect frames to immediately start flowing.
3563 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3564 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3565 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3566 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003567 MediaExpectations media_expectations;
3568 media_expectations.ExpectBidirectionalAudioAndVideo();
3569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3570}
3571
3572// This tests that if we negotiate after calling AddTransceiver but before we
3573// have a track, then set a track later, frames from the newly-set tracks are
3574// received end-to-end.
3575TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3576 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3577 ASSERT_TRUE(CreatePeerConnectionWrappers());
3578 ConnectFakeSignaling();
3579 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3580 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3581 auto caller_audio_sender = audio_result.MoveValue()->sender();
3582 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3583 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3584 auto caller_video_sender = video_result.MoveValue()->sender();
3585 callee()->SetRemoteOfferHandler([this] {
3586 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3587 callee()->pc()->GetTransceivers()[0]->SetDirection(
3588 RtpTransceiverDirection::kSendRecv);
3589 callee()->pc()->GetTransceivers()[1]->SetDirection(
3590 RtpTransceiverDirection::kSendRecv);
3591 });
3592 caller()->CreateAndSetAndSignalOffer();
3593 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3594 // Wait for ICE to complete, without any tracks being set.
3595 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3596 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3597 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3598 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3599 // Now set the tracks, and expect frames to immediately start flowing.
3600 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3601 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3602 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3603 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3604 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3605 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3606 MediaExpectations media_expectations;
3607 media_expectations.ExpectBidirectionalAudioAndVideo();
3608 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003609}
3610
3611// This test verifies that a remote video track can be added via AddStream,
3612// and sent end-to-end. For this particular test, it's simply echoed back
3613// from the caller to the callee, rather than being forwarded to a third
3614// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003615TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003616 ASSERT_TRUE(CreatePeerConnectionWrappers());
3617 ConnectFakeSignaling();
3618 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003619 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003620 caller()->CreateAndSetAndSignalOffer();
3621 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3622 ASSERT_EQ(1, callee()->remote_streams()->count());
3623
3624 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3625 // time).
3626 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3627 callee()->CreateAndSetAndSignalOffer();
3628 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3629
Seth Hampson2f0d7022018-02-20 11:54:42 -08003630 MediaExpectations media_expectations;
3631 media_expectations.ExpectBidirectionalVideo();
3632 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003633}
3634
3635// Test that we achieve the expected end-to-end connection time, using a
3636// fake clock and simulated latency on the media and signaling paths.
3637// We use a TURN<->TURN connection because this is usually the quickest to
3638// set up initially, especially when we're confident the connection will work
3639// and can start sending media before we get a STUN response.
3640//
3641// With various optimizations enabled, here are the network delays we expect to
3642// be on the critical path:
3643// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3644// signaling answer (with DTLS fingerprint).
3645// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3646// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3647// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003648TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003649 rtc::ScopedFakeClock fake_clock;
3650 // Some things use a time of "0" as a special value, so we need to start out
3651 // the fake clock at a nonzero time.
3652 // TODO(deadbeef): Fix this.
3653 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3654
3655 static constexpr int media_hop_delay_ms = 50;
3656 static constexpr int signaling_trip_delay_ms = 500;
3657 // For explanation of these values, see comment above.
3658 static constexpr int required_media_hops = 9;
3659 static constexpr int required_signaling_trips = 2;
3660 // For internal delays (such as posting an event asychronously).
3661 static constexpr int allowed_internal_delay_ms = 20;
3662 static constexpr int total_connection_time_ms =
3663 media_hop_delay_ms * required_media_hops +
3664 signaling_trip_delay_ms * required_signaling_trips +
3665 allowed_internal_delay_ms;
3666
3667 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3668 3478};
3669 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3670 0};
3671 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3672 3478};
3673 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3674 0};
3675 cricket::TestTurnServer turn_server_1(network_thread(),
3676 turn_server_1_internal_address,
3677 turn_server_1_external_address);
3678 cricket::TestTurnServer turn_server_2(network_thread(),
3679 turn_server_2_internal_address,
3680 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003681
deadbeef1dcb1642017-03-29 21:08:16 -07003682 // Bypass permission check on received packets so media can be sent before
3683 // the candidate is signaled.
3684 turn_server_1.set_enable_permission_checks(false);
3685 turn_server_2.set_enable_permission_checks(false);
3686
3687 PeerConnectionInterface::RTCConfiguration client_1_config;
3688 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3689 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3690 ice_server_1.username = "test";
3691 ice_server_1.password = "test";
3692 client_1_config.servers.push_back(ice_server_1);
3693 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3694 client_1_config.presume_writable_when_fully_relayed = true;
3695
3696 PeerConnectionInterface::RTCConfiguration client_2_config;
3697 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3698 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3699 ice_server_2.username = "test";
3700 ice_server_2.password = "test";
3701 client_2_config.servers.push_back(ice_server_2);
3702 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3703 client_2_config.presume_writable_when_fully_relayed = true;
3704
3705 ASSERT_TRUE(
3706 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3707 // Set up the simulated delays.
3708 SetSignalingDelayMs(signaling_trip_delay_ms);
3709 ConnectFakeSignaling();
3710 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3711 virtual_socket_server()->UpdateDelayDistribution();
3712
3713 // Set "offer to receive audio/video" without adding any tracks, so we just
3714 // set up ICE/DTLS with no media.
3715 PeerConnectionInterface::RTCOfferAnswerOptions options;
3716 options.offer_to_receive_audio = 1;
3717 options.offer_to_receive_video = 1;
3718 caller()->SetOfferAnswerOptions(options);
3719 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003720 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3721 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003722 // Need to free the clients here since they're using things we created on
3723 // the stack.
3724 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3725 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3726}
3727
Jonas Orelandbdcee282017-10-10 14:01:40 +02003728// Verify that a TurnCustomizer passed in through RTCConfiguration
3729// is actually used by the underlying TURN candidate pair.
3730// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003731TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003732 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3733 3478};
3734 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3735 0};
3736 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3737 3478};
3738 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3739 0};
3740 cricket::TestTurnServer turn_server_1(network_thread(),
3741 turn_server_1_internal_address,
3742 turn_server_1_external_address);
3743 cricket::TestTurnServer turn_server_2(network_thread(),
3744 turn_server_2_internal_address,
3745 turn_server_2_external_address);
3746
3747 PeerConnectionInterface::RTCConfiguration client_1_config;
3748 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3749 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3750 ice_server_1.username = "test";
3751 ice_server_1.password = "test";
3752 client_1_config.servers.push_back(ice_server_1);
3753 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3754 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3755 client_1_config.turn_customizer = customizer1.get();
3756
3757 PeerConnectionInterface::RTCConfiguration client_2_config;
3758 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3759 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3760 ice_server_2.username = "test";
3761 ice_server_2.password = "test";
3762 client_2_config.servers.push_back(ice_server_2);
3763 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3764 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3765 client_2_config.turn_customizer = customizer2.get();
3766
3767 ASSERT_TRUE(
3768 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3769 ConnectFakeSignaling();
3770
3771 // Set "offer to receive audio/video" without adding any tracks, so we just
3772 // set up ICE/DTLS with no media.
3773 PeerConnectionInterface::RTCOfferAnswerOptions options;
3774 options.offer_to_receive_audio = 1;
3775 options.offer_to_receive_video = 1;
3776 caller()->SetOfferAnswerOptions(options);
3777 caller()->CreateAndSetAndSignalOffer();
3778 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3779
3780 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3781 EXPECT_GT(customizer1->modify_cnt_, 0u);
3782
3783 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3784 EXPECT_GT(customizer2->modify_cnt_, 0u);
3785
3786 // Need to free the clients here since they're using things we created on
3787 // the stack.
3788 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3789 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3790}
3791
deadbeefc964d0b2017-04-03 10:03:35 -07003792// Test that audio and video flow end-to-end when codec names don't use the
3793// expected casing, given that they're supposed to be case insensitive. To test
3794// this, all but one codec is removed from each media description, and its
3795// casing is changed.
3796//
3797// In the past, this has regressed and caused crashes/black video, due to the
3798// fact that code at some layers was doing case-insensitive comparisons and
3799// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003800TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07003801 ASSERT_TRUE(CreatePeerConnectionWrappers());
3802 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003803 caller()->AddAudioVideoTracks();
3804 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003805
3806 // Remove all but one audio/video codec (opus and VP8), and change the
3807 // casing of the caller's generated offer.
3808 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3809 cricket::AudioContentDescription* audio =
3810 GetFirstAudioContentDescription(description);
3811 ASSERT_NE(nullptr, audio);
3812 auto audio_codecs = audio->codecs();
3813 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3814 [](const cricket::AudioCodec& codec) {
3815 return codec.name != "opus";
3816 }),
3817 audio_codecs.end());
3818 ASSERT_EQ(1u, audio_codecs.size());
3819 audio_codecs[0].name = "OpUs";
3820 audio->set_codecs(audio_codecs);
3821
3822 cricket::VideoContentDescription* video =
3823 GetFirstVideoContentDescription(description);
3824 ASSERT_NE(nullptr, video);
3825 auto video_codecs = video->codecs();
3826 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3827 [](const cricket::VideoCodec& codec) {
3828 return codec.name != "VP8";
3829 }),
3830 video_codecs.end());
3831 ASSERT_EQ(1u, video_codecs.size());
3832 video_codecs[0].name = "vP8";
3833 video->set_codecs(video_codecs);
3834 });
3835
3836 caller()->CreateAndSetAndSignalOffer();
3837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3838
3839 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003840 MediaExpectations media_expectations;
3841 media_expectations.ExpectBidirectionalAudioAndVideo();
3842 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07003843}
3844
Seth Hampson2f0d7022018-02-20 11:54:42 -08003845TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07003846 ASSERT_TRUE(CreatePeerConnectionWrappers());
3847 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003848 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003849 caller()->CreateAndSetAndSignalOffer();
3850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003851 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003852 MediaExpectations media_expectations;
3853 media_expectations.CalleeExpectsSomeAudio(1);
3854 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07003855 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3856 auto receiver = callee()->pc()->GetReceivers()[0];
3857 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3858
3859 auto contributing_sources = receiver->GetSources();
3860 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3861 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3862 contributing_sources[0].source_id());
3863}
3864
deadbeef2f425aa2017-04-14 10:41:32 -07003865// Test that if a track is removed and added again with a different stream ID,
3866// the new stream ID is successfully communicated in SDP and media continues to
3867// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003868// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
3869// it will not reuse a transceiver that has already been sending. After creating
3870// a new transceiver it tries to create an offer with two senders of the same
3871// track ids and it fails.
3872TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07003873 ASSERT_TRUE(CreatePeerConnectionWrappers());
3874 ConnectFakeSignaling();
3875
3876 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3877 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3878 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3879 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3880
3881 // Add track using stream 1, do offer/answer.
3882 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3883 caller()->CreateLocalAudioTrack();
3884 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3885 caller()->pc()->AddTrack(track, {stream_1.get()});
3886 caller()->CreateAndSetAndSignalOffer();
3887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003888 {
3889 MediaExpectations media_expectations;
3890 media_expectations.CalleeExpectsSomeAudio(1);
3891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3892 }
deadbeef2f425aa2017-04-14 10:41:32 -07003893 // Remove the sender, and create a new one with the new stream.
3894 caller()->pc()->RemoveTrack(sender);
3895 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3896 caller()->CreateAndSetAndSignalOffer();
3897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3898 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003899 {
3900 MediaExpectations media_expectations;
3901 media_expectations.CalleeExpectsSomeAudio();
3902 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3903 }
deadbeef2f425aa2017-04-14 10:41:32 -07003904}
3905
Seth Hampson2f0d7022018-02-20 11:54:42 -08003906TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02003907 ASSERT_TRUE(CreatePeerConnectionWrappers());
3908 ConnectFakeSignaling();
3909
3910 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3911 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3912 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3913 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003914 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3915 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003916
Steve Anton15324772018-01-16 10:26:49 -08003917 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003918 caller()->CreateAndSetAndSignalOffer();
3919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3920}
3921
Steve Antonede9ca52017-10-16 13:04:27 -07003922// Test that if candidates are only signaled by applying full session
3923// descriptions (instead of using AddIceCandidate), the peers can connect to
3924// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003925TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07003926 ASSERT_TRUE(CreatePeerConnectionWrappers());
3927 // Each side will signal the session descriptions but not candidates.
3928 ConnectFakeSignalingForSdpOnly();
3929
3930 // Add audio video track and exchange the initial offer/answer with media
3931 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003932 caller()->AddAudioVideoTracks();
3933 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003934 caller()->CreateAndSetAndSignalOffer();
3935
3936 // Wait for all candidates to be gathered on both the caller and callee.
3937 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3938 caller()->ice_gathering_state(), kDefaultTimeout);
3939 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3940 callee()->ice_gathering_state(), kDefaultTimeout);
3941
3942 // The candidates will now be included in the session description, so
3943 // signaling them will start the ICE connection.
3944 caller()->CreateAndSetAndSignalOffer();
3945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3946
3947 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003948 MediaExpectations media_expectations;
3949 media_expectations.ExpectBidirectionalAudioAndVideo();
3950 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07003951}
3952
henrika5f6bf242017-11-01 11:06:56 +01003953// Test that SetAudioPlayout can be used to disable audio playout from the
3954// start, then later enable it. This may be useful, for example, if the caller
3955// needs to play a local ringtone until some event occurs, after which it
3956// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003957TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01003958 ASSERT_TRUE(CreatePeerConnectionWrappers());
3959 ConnectFakeSignaling();
3960
3961 // Set up audio-only call where audio playout is disabled on caller's side.
3962 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003963 caller()->AddAudioTrack();
3964 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003965 caller()->CreateAndSetAndSignalOffer();
3966 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3967
3968 // Pump messages for a second.
3969 WAIT(false, 1000);
3970 // Since audio playout is disabled, the caller shouldn't have received
3971 // anything (at the playout level, at least).
3972 EXPECT_EQ(0, caller()->audio_frames_received());
3973 // As a sanity check, make sure the callee (for which playout isn't disabled)
3974 // did still see frames on its audio level.
3975 ASSERT_GT(callee()->audio_frames_received(), 0);
3976
3977 // Enable playout again, and ensure audio starts flowing.
3978 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003979 MediaExpectations media_expectations;
3980 media_expectations.ExpectBidirectionalAudio();
3981 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01003982}
3983
3984double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3985 auto report = pc->NewGetStats();
3986 auto track_stats_list =
3987 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3988 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3989 for (const auto* track_stats : track_stats_list) {
3990 if (track_stats->remote_source.is_defined() &&
3991 *track_stats->remote_source) {
3992 remote_track_stats = track_stats;
3993 break;
3994 }
3995 }
3996
3997 if (!remote_track_stats->total_audio_energy.is_defined()) {
3998 return 0.0;
3999 }
4000 return *remote_track_stats->total_audio_energy;
4001}
4002
4003// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4004// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004005TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004006 DisableAudioPlayoutStillGeneratesAudioStats) {
4007 ASSERT_TRUE(CreatePeerConnectionWrappers());
4008 ConnectFakeSignaling();
4009
4010 // Set up audio-only call where playout is disabled but audio-processing is
4011 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004012 caller()->AddAudioTrack();
4013 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004014 caller()->pc()->SetAudioPlayout(false);
4015
4016 caller()->CreateAndSetAndSignalOffer();
4017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4018
4019 // Wait for the callee to receive audio stats.
4020 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4021}
4022
henrika4f167df2017-11-01 14:45:55 +01004023// Test that SetAudioRecording can be used to disable audio recording from the
4024// start, then later enable it. This may be useful, for example, if the caller
4025// wants to ensure that no audio resources are active before a certain state
4026// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004027TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004028 ASSERT_TRUE(CreatePeerConnectionWrappers());
4029 ConnectFakeSignaling();
4030
4031 // Set up audio-only call where audio recording is disabled on caller's side.
4032 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004033 caller()->AddAudioTrack();
4034 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004035 caller()->CreateAndSetAndSignalOffer();
4036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4037
4038 // Pump messages for a second.
4039 WAIT(false, 1000);
4040 // Since caller has disabled audio recording, the callee shouldn't have
4041 // received anything.
4042 EXPECT_EQ(0, callee()->audio_frames_received());
4043 // As a sanity check, make sure the caller did still see frames on its
4044 // audio level since audio recording is enabled on the calle side.
4045 ASSERT_GT(caller()->audio_frames_received(), 0);
4046
4047 // Enable audio recording again, and ensure audio starts flowing.
4048 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004049 MediaExpectations media_expectations;
4050 media_expectations.ExpectBidirectionalAudio();
4051 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004052}
4053
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004054// Test that after closing PeerConnections, they stop sending any packets (ICE,
4055// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004056TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004057 // Set up audio/video/data, wait for some frames to be received.
4058 ASSERT_TRUE(CreatePeerConnectionWrappers());
4059 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004060 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004061#ifdef HAVE_SCTP
4062 caller()->CreateDataChannel();
4063#endif
4064 caller()->CreateAndSetAndSignalOffer();
4065 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004066 MediaExpectations media_expectations;
4067 media_expectations.CalleeExpectsSomeAudioAndVideo();
4068 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004069 // Close PeerConnections.
4070 caller()->pc()->Close();
4071 callee()->pc()->Close();
4072 // Pump messages for a second, and ensure no new packets end up sent.
4073 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4074 WAIT(false, 1000);
4075 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4076 EXPECT_EQ(sent_packets_a, sent_packets_b);
4077}
4078
Seth Hampson2f0d7022018-02-20 11:54:42 -08004079INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4080 PeerConnectionIntegrationTest,
4081 Values(SdpSemantics::kPlanB,
4082 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004083
Steve Anton74255ff2018-01-24 18:32:57 -08004084// Tests that verify interoperability between Plan B and Unified Plan
4085// PeerConnections.
4086class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004087 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004088 public ::testing::WithParamInterface<
4089 std::tuple<SdpSemantics, SdpSemantics>> {
4090 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004091 // Setting the SdpSemantics for the base test to kDefault does not matter
4092 // because we specify not to use the test semantics when creating
4093 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004094 PeerConnectionIntegrationInteropTest()
Seth Hampson2f0d7022018-02-20 11:54:42 -08004095 : PeerConnectionIntegrationBaseTest(SdpSemantics::kDefault),
4096 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004097 callee_semantics_(std::get<1>(GetParam())) {}
4098
4099 bool CreatePeerConnectionWrappersWithSemantics() {
4100 RTCConfiguration caller_config;
4101 caller_config.sdp_semantics = caller_semantics_;
4102 RTCConfiguration callee_config;
4103 callee_config.sdp_semantics = callee_semantics_;
4104 return CreatePeerConnectionWrappersWithConfig(caller_config, callee_config);
4105 }
4106
4107 const SdpSemantics caller_semantics_;
4108 const SdpSemantics callee_semantics_;
4109};
4110
4111TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4112 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4113 ConnectFakeSignaling();
4114
4115 caller()->CreateAndSetAndSignalOffer();
4116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4117}
4118
4119TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4120 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4121 ConnectFakeSignaling();
4122 auto audio_sender = caller()->AddAudioTrack();
4123
4124 caller()->CreateAndSetAndSignalOffer();
4125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4126
4127 // Verify that one audio receiver has been created on the remote and that it
4128 // has the same track ID as the sending track.
4129 auto receivers = callee()->pc()->GetReceivers();
4130 ASSERT_EQ(1u, receivers.size());
4131 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4132 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4133
Seth Hampson2f0d7022018-02-20 11:54:42 -08004134 MediaExpectations media_expectations;
4135 media_expectations.CalleeExpectsSomeAudio();
4136 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004137}
4138
4139TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4140 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4141 ConnectFakeSignaling();
4142 auto video_sender = caller()->AddVideoTrack();
4143 auto audio_sender = caller()->AddAudioTrack();
4144
4145 caller()->CreateAndSetAndSignalOffer();
4146 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4147
4148 // Verify that one audio and one video receiver have been created on the
4149 // remote and that they have the same track IDs as the sending tracks.
4150 auto audio_receivers =
4151 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4152 ASSERT_EQ(1u, audio_receivers.size());
4153 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4154 auto video_receivers =
4155 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4156 ASSERT_EQ(1u, video_receivers.size());
4157 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4158
Seth Hampson2f0d7022018-02-20 11:54:42 -08004159 MediaExpectations media_expectations;
4160 media_expectations.CalleeExpectsSomeAudioAndVideo();
4161 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004162}
4163
4164TEST_P(PeerConnectionIntegrationInteropTest,
4165 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4166 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4167 ConnectFakeSignaling();
4168 caller()->AddAudioVideoTracks();
4169 callee()->AddAudioVideoTracks();
4170
4171 caller()->CreateAndSetAndSignalOffer();
4172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4173
Seth Hampson2f0d7022018-02-20 11:54:42 -08004174 MediaExpectations media_expectations;
4175 media_expectations.ExpectBidirectionalAudioAndVideo();
4176 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004177}
4178
4179TEST_P(PeerConnectionIntegrationInteropTest,
4180 ReverseRolesOneAudioLocalToOneVideoRemote) {
4181 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4182 ConnectFakeSignaling();
4183 caller()->AddAudioTrack();
4184 callee()->AddVideoTrack();
4185
4186 caller()->CreateAndSetAndSignalOffer();
4187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4188
4189 // Verify that only the audio track has been negotiated.
4190 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4191 // Might also check that the callee's NegotiationNeeded flag is set.
4192
4193 // Reverse roles.
4194 callee()->CreateAndSetAndSignalOffer();
4195 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4196
Seth Hampson2f0d7022018-02-20 11:54:42 -08004197 MediaExpectations media_expectations;
4198 media_expectations.CallerExpectsSomeVideo();
4199 media_expectations.CalleeExpectsSomeAudio();
4200 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004201}
4202
4203// Test that if one side offers two video tracks then the other side will only
4204// see the first one and ignore the second.
4205TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToNoMediaRemote) {
4206 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4207 ConnectFakeSignaling();
4208 auto first_sender = caller()->AddVideoTrack();
4209 caller()->AddVideoTrack();
4210
4211 caller()->CreateAndSetAndSignalOffer();
4212 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4213
4214 // Verify that there is only one receiver and it corresponds to the first
4215 // added track.
4216 auto receivers = callee()->pc()->GetReceivers();
4217 ASSERT_EQ(1u, receivers.size());
4218 EXPECT_TRUE(receivers[0]->track()->enabled());
4219 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4220
Seth Hampson2f0d7022018-02-20 11:54:42 -08004221 MediaExpectations media_expectations;
4222 media_expectations.CalleeExpectsSomeVideo();
4223 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004224}
4225
4226// Test that in the multi-track case each endpoint only looks at the first track
4227// and ignores the second one.
4228TEST_P(PeerConnectionIntegrationInteropTest, TwoVideoLocalToTwoVideoRemote) {
4229 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4230 ConnectFakeSignaling();
4231 caller()->AddVideoTrack();
4232 caller()->AddVideoTrack();
4233 callee()->AddVideoTrack();
4234 callee()->AddVideoTrack();
4235
4236 caller()->CreateAndSetAndSignalOffer();
4237 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4238
4239 PeerConnectionWrapper* plan_b =
4240 (caller_semantics_ == SdpSemantics::kPlanB ? caller() : callee());
4241 PeerConnectionWrapper* unified_plan =
4242 (caller_semantics_ == SdpSemantics::kUnifiedPlan ? caller() : callee());
4243
4244 // Should have two senders each, one for each track.
4245 EXPECT_EQ(2u, plan_b->pc()->GetSenders().size());
4246 EXPECT_EQ(2u, unified_plan->pc()->GetSenders().size());
4247
4248 // Plan B will have one receiver since it only looks at the first video
4249 // section. The receiver should have the same track ID as the sender's first
4250 // track.
4251 ASSERT_EQ(1u, plan_b->pc()->GetReceivers().size());
4252 EXPECT_EQ(unified_plan->pc()->GetSenders()[0]->track()->id(),
4253 plan_b->pc()->GetReceivers()[0]->track()->id());
4254
4255 // Unified Plan will have two receivers since they were created with the
4256 // transceivers when the tracks were added.
4257 ASSERT_EQ(2u, unified_plan->pc()->GetReceivers().size());
4258
4259 if (unified_plan == caller()) {
4260 // If the Unified Plan endpoint was the caller, then the Plan B endpoint
4261 // would have rejected the second video media section so we would expect the
4262 // transceiver to be stopped.
4263 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[0]->stopped());
4264 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[1]->stopped());
4265 } else {
4266 // If the Unified Plan endpoint was the callee, then the Plan B endpoint
4267 // would have offered only one video section so we would expect the first
4268 // transceiver to map to the first track and the second transceiver to be
4269 // missing a mid.
4270 EXPECT_TRUE(unified_plan->pc()->GetTransceivers()[0]->mid());
4271 EXPECT_FALSE(unified_plan->pc()->GetTransceivers()[1]->mid());
4272 }
4273
4274 // Should be exchanging video frames for the first tracks on each endpoint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004275 MediaExpectations media_expectations;
4276 media_expectations.ExpectBidirectionalVideo();
4277 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004278}
4279
4280INSTANTIATE_TEST_CASE_P(
4281 PeerConnectionIntegrationTest,
4282 PeerConnectionIntegrationInteropTest,
4283 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4284 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4285
deadbeef1dcb1642017-03-29 21:08:16 -07004286} // namespace
4287
4288#endif // if !defined(THREAD_SANITIZER)