blob: a6fda21d27e33b08b533995c2f459b983f44e0ad [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
deadbeefcbecd352015-09-23 11:50:27 -070013#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/fakemetricsobserver.h"
16#include "api/jsepicecandidate.h"
17#include "api/jsepsessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "media/base/fakemediaengine.h"
19#include "media/base/fakevideorenderer.h"
20#include "media/base/mediachannel.h"
21#include "media/engine/fakewebrtccall.h"
22#include "media/sctp/sctptransportinternal.h"
23#include "p2p/base/packettransportinternal.h"
24#include "p2p/base/stunserver.h"
25#include "p2p/base/teststunserver.h"
26#include "p2p/base/testturnserver.h"
27#include "p2p/client/basicportallocator.h"
28#include "pc/audiotrack.h"
29#include "pc/channelmanager.h"
30#include "pc/mediasession.h"
31#include "pc/peerconnection.h"
32#include "pc/sctputils.h"
33#include "pc/test/fakertccertificategenerator.h"
34#include "pc/videotrack.h"
35#include "pc/webrtcsession.h"
36#include "pc/webrtcsessiondescriptionfactory.h"
37#include "rtc_base/checks.h"
38#include "rtc_base/fakenetwork.h"
39#include "rtc_base/firewallsocketserver.h"
40#include "rtc_base/gunit.h"
41#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045using cricket::FakeVoiceMediaChannel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046using cricket::TransportInfo;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047using rtc::SocketAddress;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048using rtc::Thread;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049using webrtc::CreateSessionDescription;
wu@webrtc.org91053e72013-08-10 07:18:04 +000050using webrtc::CreateSessionDescriptionObserver;
51using webrtc::CreateSessionDescriptionRequest;
deadbeefab9b2d12015-10-14 11:33:11 -070052using webrtc::DataChannel;
jbauchac8869e2015-07-03 01:36:14 -070053using webrtc::FakeMetricsObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054using webrtc::IceCandidateCollection;
deadbeefab9b2d12015-10-14 11:33:11 -070055using webrtc::InternalDataChannelInit;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056using webrtc::JsepIceCandidate;
57using webrtc::JsepSessionDescription;
wu@webrtc.org97077a32013-10-25 21:18:33 +000058using webrtc::PeerConnectionFactoryInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059using webrtc::PeerConnectionInterface;
60using webrtc::SessionDescriptionInterface;
deadbeefd59daf82015-10-14 15:02:44 -070061using webrtc::SessionStats;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062using webrtc::StreamCollection;
wu@webrtc.org91053e72013-08-10 07:18:04 +000063using webrtc::WebRtcSession;
wu@webrtc.org364f2042013-11-20 21:49:41 +000064using webrtc::kBundleWithoutRtcpMux;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065using webrtc::kCreateChannelFailed;
66using webrtc::kInvalidSdp;
Zhi Huang2a5e4262017-09-14 01:15:03 -070067using webrtc::kMlineMismatchInAnswer;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000068using webrtc::kPushDownTDFailed;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069using webrtc::kSdpWithoutIceUfragPwd;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070using webrtc::kSdpWithoutDtlsFingerprint;
71using webrtc::kSdpWithoutSdesCrypto;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072using webrtc::kSessionError;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000073using webrtc::kSessionErrorDesc;
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000074using webrtc::kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +000076typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
77
wu@webrtc.org364f2042013-11-20 21:49:41 +000078static const int kClientAddrPort = 0;
79static const char kClientAddrHost1[] = "11.11.11.11";
wu@webrtc.org364f2042013-11-20 21:49:41 +000080static const char kStunAddrHost[] = "99.99.99.1";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
82static const char kSessionVersion[] = "1";
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084// Media index of candidates belonging to the first media content.
85static const int kMediaContentIndex0 = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
87// Media index of candidates belonging to the second media content.
88static const int kMediaContentIndex1 = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
deadbeef953c2ce2017-01-09 14:53:41 -080090static const int kDefaultTimeout = 10000; // 10 seconds.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091static const int kIceCandidatesTimeout = 10000;
92
deadbeefab9b2d12015-10-14 11:33:11 -070093static const char kStream1[] = "stream1";
94static const char kVideoTrack1[] = "video1";
95static const char kAudioTrack1[] = "audio1";
96
97static const char kStream2[] = "stream2";
98static const char kVideoTrack2[] = "video2";
99static const char kAudioTrack2[] = "audio2";
100
zhihuang1c378ed2017-08-17 14:10:50 -0700101static constexpr bool kActive = false;
102
Henrik Boström87713d02015-08-25 09:53:21 +0200103enum RTCCertificateGenerationMethod { ALREADY_GENERATED, DTLS_IDENTITY_STORE };
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105class MockIceObserver : public webrtc::IceObserver {
106 public:
107 MockIceObserver()
108 : oncandidatesready_(false),
109 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
110 ice_gathering_state_(PeerConnectionInterface::kIceGatheringNew) {
111 }
112
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200113 virtual ~MockIceObserver() = default;
114
zstein6dfd53a2017-03-06 13:49:03 -0800115 void OnIceConnectionStateChange(
perkjdfb769d2016-02-09 03:09:43 -0800116 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 ice_connection_state_ = new_state;
skvlad6c87a672016-05-17 17:49:52 -0700118 ice_connection_state_history_.push_back(new_state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 }
perkjdfb769d2016-02-09 03:09:43 -0800120 void OnIceGatheringChange(
121 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 // We can never transition back to "new".
123 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, new_state);
124 ice_gathering_state_ = new_state;
perkjdfb769d2016-02-09 03:09:43 -0800125 oncandidatesready_ =
126 new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 }
128
129 // Found a new candidate.
jbauch81bf7b02017-03-25 08:31:12 -0700130 void OnIceCandidate(
131 std::unique_ptr<webrtc::IceCandidateInterface> candidate) override {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000132 switch (candidate->sdp_mline_index()) {
133 case kMediaContentIndex0:
134 mline_0_candidates_.push_back(candidate->candidate());
135 break;
136 case kMediaContentIndex1:
137 mline_1_candidates_.push_back(candidate->candidate());
138 break;
139 default:
nissec80e7412017-01-11 05:56:46 -0800140 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000142
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 // The ICE gathering state should always be Gathering when a candidate is
144 // received (or possibly Completed in the case of the final candidate).
145 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, ice_gathering_state_);
146 }
147
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700148 // Some local candidates are removed.
149 void OnIceCandidatesRemoved(
nisseef8b61e2016-04-29 06:09:15 -0700150 const std::vector<cricket::Candidate>& candidates) override {
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700151 num_candidates_removed_ += candidates.size();
152 }
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 bool oncandidatesready_;
155 std::vector<cricket::Candidate> mline_0_candidates_;
156 std::vector<cricket::Candidate> mline_1_candidates_;
157 PeerConnectionInterface::IceConnectionState ice_connection_state_;
158 PeerConnectionInterface::IceGatheringState ice_gathering_state_;
skvlad6c87a672016-05-17 17:49:52 -0700159 std::vector<PeerConnectionInterface::IceConnectionState>
160 ice_connection_state_history_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700161 size_t num_candidates_removed_ = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162};
163
deadbeef953c2ce2017-01-09 14:53:41 -0800164// Used for tests in this file to verify that WebRtcSession responds to signals
165// from the SctpTransport correctly, and calls Start with the correct
166// local/remote ports.
167class FakeSctpTransport : public cricket::SctpTransportInternal {
168 public:
deadbeef5bd5ca32017-02-10 11:31:50 -0800169 void SetTransportChannel(rtc::PacketTransportInternal* channel) override {}
deadbeef953c2ce2017-01-09 14:53:41 -0800170 bool Start(int local_port, int remote_port) override {
171 local_port_ = local_port;
172 remote_port_ = remote_port;
173 return true;
174 }
175 bool OpenStream(int sid) override { return true; }
176 bool ResetStream(int sid) override { return true; }
177 bool SendData(const cricket::SendDataParams& params,
178 const rtc::CopyOnWriteBuffer& payload,
179 cricket::SendDataResult* result = nullptr) override {
180 return true;
181 }
182 bool ReadyToSendData() override { return true; }
183 void set_debug_name_for_testing(const char* debug_name) override {}
184
185 int local_port() const { return local_port_; }
186 int remote_port() const { return remote_port_; }
187
188 private:
189 int local_port_ = -1;
190 int remote_port_ = -1;
191};
192
193class FakeSctpTransportFactory : public cricket::SctpTransportInternalFactory {
194 public:
195 std::unique_ptr<cricket::SctpTransportInternal> CreateSctpTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800196 rtc::PacketTransportInternal*) override {
deadbeef953c2ce2017-01-09 14:53:41 -0800197 last_fake_sctp_transport_ = new FakeSctpTransport();
198 return std::unique_ptr<cricket::SctpTransportInternal>(
199 last_fake_sctp_transport_);
200 }
201
202 FakeSctpTransport* last_fake_sctp_transport() {
203 return last_fake_sctp_transport_;
204 }
205
206 private:
207 FakeSctpTransport* last_fake_sctp_transport_ = nullptr;
208};
209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210class WebRtcSessionForTest : public webrtc::WebRtcSession {
211 public:
zhihuang29ff8442016-07-27 11:07:25 -0700212 WebRtcSessionForTest(
nisseeaabdf62017-05-05 02:23:02 -0700213 webrtc::Call* fake_call,
214 cricket::ChannelManager* channel_manager,
215 const cricket::MediaConfig& media_config,
216 webrtc::RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700217 rtc::Thread* network_thread,
218 rtc::Thread* worker_thread,
219 rtc::Thread* signaling_thread,
220 cricket::PortAllocator* port_allocator,
221 webrtc::IceObserver* ice_observer,
deadbeef953c2ce2017-01-09 14:53:41 -0800222 std::unique_ptr<cricket::TransportController> transport_controller,
223 std::unique_ptr<FakeSctpTransportFactory> sctp_factory)
nisseeaabdf62017-05-05 02:23:02 -0700224 : WebRtcSession(fake_call, channel_manager, media_config, event_log,
danilchape9021a32016-05-17 01:52:02 -0700225 network_thread,
stefanc1aeaf02015-10-15 07:26:07 -0700226 worker_thread,
danilchape9021a32016-05-17 01:52:02 -0700227 signaling_thread,
zhihuang29ff8442016-07-27 11:07:25 -0700228 port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800229 std::move(transport_controller),
230 std::move(sctp_factory)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 RegisterIceObserver(ice_observer);
232 }
233 virtual ~WebRtcSessionForTest() {}
234
deadbeefcbecd352015-09-23 11:50:27 -0700235 // Note that these methods are only safe to use if the signaling thread
236 // is the same as the worker thread
deadbeef5bd5ca32017-02-10 11:31:50 -0800237 rtc::PacketTransportInternal* voice_rtp_transport_channel() {
deadbeefcbecd352015-09-23 11:50:27 -0700238 return rtp_transport_channel(voice_channel());
239 }
240
deadbeef5bd5ca32017-02-10 11:31:50 -0800241 rtc::PacketTransportInternal* voice_rtcp_transport_channel() {
deadbeefcbecd352015-09-23 11:50:27 -0700242 return rtcp_transport_channel(voice_channel());
243 }
244
deadbeef5bd5ca32017-02-10 11:31:50 -0800245 rtc::PacketTransportInternal* video_rtp_transport_channel() {
deadbeefcbecd352015-09-23 11:50:27 -0700246 return rtp_transport_channel(video_channel());
247 }
248
deadbeef5bd5ca32017-02-10 11:31:50 -0800249 rtc::PacketTransportInternal* video_rtcp_transport_channel() {
deadbeefcbecd352015-09-23 11:50:27 -0700250 return rtcp_transport_channel(video_channel());
251 }
252
deadbeefcbecd352015-09-23 11:50:27 -0700253 private:
deadbeef5bd5ca32017-02-10 11:31:50 -0800254 rtc::PacketTransportInternal* rtp_transport_channel(
johan669d69b2016-11-08 14:14:08 -0800255 cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -0700256 if (!ch) {
257 return nullptr;
258 }
zhihuangb2cdd932017-01-19 16:54:25 -0800259 return ch->rtp_dtls_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700260 }
261
deadbeef5bd5ca32017-02-10 11:31:50 -0800262 rtc::PacketTransportInternal* rtcp_transport_channel(
johan669d69b2016-11-08 14:14:08 -0800263 cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -0700264 if (!ch) {
265 return nullptr;
266 }
zhihuangb2cdd932017-01-19 16:54:25 -0800267 return ch->rtcp_dtls_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269};
270
wu@webrtc.org91053e72013-08-10 07:18:04 +0000271class WebRtcSessionCreateSDPObserverForTest
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 : public rtc::RefCountedObject<CreateSessionDescriptionObserver> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +0000274 enum State {
275 kInit,
276 kFailed,
277 kSucceeded,
278 };
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000279 WebRtcSessionCreateSDPObserverForTest() : state_(kInit) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +0000280
281 // CreateSessionDescriptionObserver implementation.
282 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000283 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000284 state_ = kSucceeded;
285 }
286 virtual void OnFailure(const std::string& error) {
287 state_ = kFailed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 }
289
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000290 SessionDescriptionInterface* description() { return description_.get(); }
291
292 SessionDescriptionInterface* ReleaseDescription() {
293 return description_.release();
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
wu@webrtc.org91053e72013-08-10 07:18:04 +0000296 State state() const { return state_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297
wu@webrtc.org91053e72013-08-10 07:18:04 +0000298 protected:
299 ~WebRtcSessionCreateSDPObserverForTest() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300
301 private:
kwibergd1fe2812016-04-27 06:47:29 -0700302 std::unique_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000303 State state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304};
305
Henrik Boström87713d02015-08-25 09:53:21 +0200306class WebRtcSessionTest
deadbeefab9b2d12015-10-14 11:33:11 -0700307 : public testing::TestWithParam<RTCCertificateGenerationMethod>,
308 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 protected:
310 // TODO Investigate why ChannelManager crashes, if it's created
311 // after stun_server.
312 WebRtcSessionTest()
deadbeef98e186c2017-05-16 18:00:06 -0700313 : vss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -0700314 fss_(new rtc::FirewallSocketServer(vss_.get())),
315 thread_(fss_.get()),
316 media_engine_(new cricket::FakeMediaEngine()),
stefanc1aeaf02015-10-15 07:26:07 -0700317 data_engine_(new cricket::FakeDataEngine()),
deadbeef112b2e92017-02-10 20:13:37 -0800318 channel_manager_(new cricket::ChannelManager(
319 std::unique_ptr<cricket::MediaEngineInterface>(media_engine_),
320 std::unique_ptr<cricket::DataEngineInterface>(data_engine_),
321 rtc::Thread::Current())),
skvlad11a9cbf2016-10-07 11:53:05 -0700322 fake_call_(webrtc::Call::Config(&event_log_)),
stefanc1aeaf02015-10-15 07:26:07 -0700323 tdesc_factory_(new cricket::TransportDescriptionFactory()),
324 desc_factory_(
325 new cricket::MediaSessionDescriptionFactory(channel_manager_.get(),
326 tdesc_factory_.get())),
stefanc1aeaf02015-10-15 07:26:07 -0700327 stun_socket_addr_(
328 rtc::SocketAddress(kStunAddrHost, cricket::STUN_SERVER_PORT)),
329 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
330 stun_socket_addr_)),
stefanc1aeaf02015-10-15 07:26:07 -0700331 metrics_observer_(new rtc::RefCountedObject<FakeMetricsObserver>()) {
buildbot@webrtc.org51c55082014-07-28 22:26:15 +0000332 cricket::ServerAddresses stun_servers;
333 stun_servers.insert(stun_socket_addr_);
334 allocator_.reset(new cricket::BasicPortAllocator(
335 &network_manager_,
336 stun_servers,
337 SocketAddress(), SocketAddress(), SocketAddress()));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000338 allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700339 cricket::PORTALLOCATOR_DISABLE_RELAY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 EXPECT_TRUE(channel_manager_->Init());
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000341 allocator_->set_step_delay(cricket::kMinimumStepDelay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 }
343
344 void AddInterface(const SocketAddress& addr) {
345 network_manager_.AddInterface(addr);
346 }
347
Henrik Boströmd79599d2016-06-01 13:58:50 +0200348 // If |cert_generator| != null or |rtc_configuration| contains |certificates|
349 // then DTLS will be enabled unless explicitly disabled by |rtc_configuration|
350 // options. When DTLS is enabled a certificate will be used if provided,
351 // otherwise one will be generated using the |cert_generator|.
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000352 void Init(
zhihuang4dfb8ce2016-11-23 10:30:12 -0800353 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef7914b8c2017-04-21 03:23:33 -0700354 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy,
355 const rtc::CryptoOptions& crypto_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 ASSERT_TRUE(session_.get() == NULL);
deadbeef953c2ce2017-01-09 14:53:41 -0800357 fake_sctp_transport_factory_ = new FakeSctpTransportFactory();
nisseeaabdf62017-05-05 02:23:02 -0700358 session_.reset(new WebRtcSessionForTest(&fake_call_,
359 channel_manager_.get(), cricket::MediaConfig(), &event_log_,
360 rtc::Thread::Current(), rtc::Thread::Current(),
zhihuang29ff8442016-07-27 11:07:25 -0700361 rtc::Thread::Current(), allocator_.get(), &observer_,
362 std::unique_ptr<cricket::TransportController>(
deadbeef7914b8c2017-04-21 03:23:33 -0700363 new cricket::TransportController(
364 rtc::Thread::Current(), rtc::Thread::Current(),
365 allocator_.get(),
366 /*redetermine_role_on_ice_restart=*/true, crypto_options)),
deadbeef953c2ce2017-01-09 14:53:41 -0800367 std::unique_ptr<FakeSctpTransportFactory>(
368 fake_sctp_transport_factory_)));
deadbeefab9b2d12015-10-14 11:33:11 -0700369 session_->SignalDataChannelOpenMessage.connect(
370 this, &WebRtcSessionTest::OnDataChannelOpenMessage);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371
zhihuang4dfb8ce2016-11-23 10:30:12 -0800372 configuration_.rtcp_mux_policy = rtcp_mux_policy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
374 observer_.ice_connection_state_);
375 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
376 observer_.ice_gathering_state_);
377
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200378 EXPECT_TRUE(session_->Initialize(options_, std::move(cert_generator),
htaa2a49d92016-03-04 02:51:39 -0800379 configuration_));
jbauchac8869e2015-07-03 01:36:14 -0700380 session_->set_metrics_observer(metrics_observer_);
deadbeef7914b8c2017-04-21 03:23:33 -0700381 crypto_options_ = crypto_options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 }
383
deadbeefab9b2d12015-10-14 11:33:11 -0700384 void OnDataChannelOpenMessage(const std::string& label,
385 const InternalDataChannelInit& config) {
386 last_data_channel_label_ = label;
387 last_data_channel_config_ = config;
388 }
389
zhihuang4dfb8ce2016-11-23 10:30:12 -0800390 void Init() {
deadbeef7914b8c2017-04-21 03:23:33 -0700391 Init(nullptr, PeerConnectionInterface::kRtcpMuxPolicyNegotiate,
392 rtc::CryptoOptions());
zhihuang4dfb8ce2016-11-23 10:30:12 -0800393 }
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000394
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000395 void InitWithBundlePolicy(
396 PeerConnectionInterface::BundlePolicy bundle_policy) {
htaa2a49d92016-03-04 02:51:39 -0800397 configuration_.bundle_policy = bundle_policy;
398 Init();
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700399 }
400
Henrik Boström87713d02015-08-25 09:53:21 +0200401 // Successfully init with DTLS; with a certificate generated and supplied or
402 // with a store that generates it for us.
403 void InitWithDtls(RTCCertificateGenerationMethod cert_gen_method) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200404 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator;
Henrik Boström87713d02015-08-25 09:53:21 +0200405 if (cert_gen_method == ALREADY_GENERATED) {
htaa2a49d92016-03-04 02:51:39 -0800406 configuration_.certificates.push_back(
Henrik Boströmd79599d2016-06-01 13:58:50 +0200407 FakeRTCCertificateGenerator::GenerateCertificate());
Henrik Boström87713d02015-08-25 09:53:21 +0200408 } else if (cert_gen_method == DTLS_IDENTITY_STORE) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200409 cert_generator.reset(new FakeRTCCertificateGenerator());
410 cert_generator->set_should_fail(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200411 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700412 RTC_CHECK(false);
Henrik Boström87713d02015-08-25 09:53:21 +0200413 }
zhihuang4dfb8ce2016-11-23 10:30:12 -0800414 Init(std::move(cert_generator),
deadbeef7914b8c2017-04-21 03:23:33 -0700415 PeerConnectionInterface::kRtcpMuxPolicyNegotiate,
416 rtc::CryptoOptions());
Henrik Boström87713d02015-08-25 09:53:21 +0200417 }
418
zhihuang1c378ed2017-08-17 14:10:50 -0700419 // The following convenience functions can be applied for both local side and
420 // remote side. The flags can be overwritten for different use cases.
deadbeefab9b2d12015-10-14 11:33:11 -0700421 void SendAudioVideoStream1() {
422 send_stream_1_ = true;
423 send_stream_2_ = false;
zhihuang1c378ed2017-08-17 14:10:50 -0700424 local_send_audio_ = true;
425 local_send_video_ = true;
426 remote_send_audio_ = true;
427 remote_send_video_ = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700428 }
429
430 void SendAudioVideoStream2() {
431 send_stream_1_ = false;
432 send_stream_2_ = true;
zhihuang1c378ed2017-08-17 14:10:50 -0700433 local_send_audio_ = true;
434 local_send_video_ = true;
435 remote_send_audio_ = true;
436 remote_send_video_ = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700437 }
438
deadbeefab9b2d12015-10-14 11:33:11 -0700439 void SendAudioOnlyStream2() {
440 send_stream_1_ = false;
441 send_stream_2_ = true;
zhihuang1c378ed2017-08-17 14:10:50 -0700442 local_send_audio_ = true;
443 local_send_video_ = false;
444 remote_send_audio_ = true;
445 remote_send_video_ = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700446 }
447
448 void SendVideoOnlyStream2() {
449 send_stream_1_ = false;
450 send_stream_2_ = true;
zhihuang1c378ed2017-08-17 14:10:50 -0700451 local_send_audio_ = false;
452 local_send_video_ = true;
453 remote_send_audio_ = false;
454 remote_send_video_ = true;
deadbeefab9b2d12015-10-14 11:33:11 -0700455 }
456
zhihuang1c378ed2017-08-17 14:10:50 -0700457 // Add the media sections to the options from |offered_media_sections_| when
458 // creating an answer or a new offer.
459 // This duplicates a lot of logic from PeerConnection but this can be fixed
460 // when PeerConnection and WebRtcSession are merged.
461 void AddExistingMediaSectionsAndSendersToOptions(
462 cricket::MediaSessionOptions* session_options,
463 bool send_audio,
464 bool recv_audio,
465 bool send_video,
466 bool recv_video) {
467 int num_sim_layer = 1;
468 for (auto media_description_options : offered_media_sections_) {
469 if (media_description_options.type == cricket::MEDIA_TYPE_AUDIO) {
470 bool stopped = !send_audio && !recv_audio;
471 auto media_desc_options = cricket::MediaDescriptionOptions(
472 cricket::MEDIA_TYPE_AUDIO, media_description_options.mid,
473 cricket::RtpTransceiverDirection(send_audio, recv_audio), stopped);
474 if (send_stream_1_ && send_audio) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700475 media_desc_options.AddAudioSender(kAudioTrack1, {kStream1});
zhihuang1c378ed2017-08-17 14:10:50 -0700476 }
477 if (send_stream_2_ && send_audio) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700478 media_desc_options.AddAudioSender(kAudioTrack2, {kStream2});
zhihuang1c378ed2017-08-17 14:10:50 -0700479 }
480 session_options->media_description_options.push_back(
481 media_desc_options);
482 } else if (media_description_options.type == cricket::MEDIA_TYPE_VIDEO) {
483 bool stopped = !send_video && !recv_video;
484 auto media_desc_options = cricket::MediaDescriptionOptions(
485 cricket::MEDIA_TYPE_VIDEO, media_description_options.mid,
486 cricket::RtpTransceiverDirection(send_video, recv_video), stopped);
487 if (send_stream_1_ && send_video) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700488 media_desc_options.AddVideoSender(kVideoTrack1, {kStream1},
zhihuang1c378ed2017-08-17 14:10:50 -0700489 num_sim_layer);
490 }
491 if (send_stream_2_ && send_video) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700492 media_desc_options.AddVideoSender(kVideoTrack2, {kStream2},
zhihuang1c378ed2017-08-17 14:10:50 -0700493 num_sim_layer);
494 }
495 session_options->media_description_options.push_back(
496 media_desc_options);
497 } else if (media_description_options.type == cricket::MEDIA_TYPE_DATA) {
498 session_options->media_description_options.push_back(
499 cricket::MediaDescriptionOptions(
500 cricket::MEDIA_TYPE_DATA, media_description_options.mid,
501 // Direction for data sections is meaningless, but legacy
502 // endpoints might expect sendrecv.
503 cricket::RtpTransceiverDirection(true, true), false));
504 } else {
505 RTC_NOTREACHED();
506 }
deadbeefab9b2d12015-10-14 11:33:11 -0700507 }
zhihuang1c378ed2017-08-17 14:10:50 -0700508 }
509
510 // Add the existing media sections first and then add new media sections if
511 // needed.
512 void AddMediaSectionsAndSendersToOptions(
513 cricket::MediaSessionOptions* session_options,
514 bool send_audio,
515 bool recv_audio,
516 bool send_video,
517 bool recv_video) {
518 AddExistingMediaSectionsAndSendersToOptions(
519 session_options, send_audio, recv_audio, send_video, recv_video);
520
521 if (!session_options->has_audio() && (send_audio || recv_audio)) {
522 cricket::MediaDescriptionOptions media_desc_options =
523 cricket::MediaDescriptionOptions(
524 cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
525 cricket::RtpTransceiverDirection(send_audio, recv_audio),
526 kActive);
527 if (send_stream_1_ && send_audio) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700528 media_desc_options.AddAudioSender(kAudioTrack1, {kStream1});
zhihuang1c378ed2017-08-17 14:10:50 -0700529 }
530 if (send_stream_2_ && send_audio) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700531 media_desc_options.AddAudioSender(kAudioTrack2, {kStream2});
zhihuang1c378ed2017-08-17 14:10:50 -0700532 }
533 session_options->media_description_options.push_back(media_desc_options);
534 offered_media_sections_.push_back(media_desc_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700535 }
zhihuang1c378ed2017-08-17 14:10:50 -0700536
537 if (!session_options->has_video() && (send_video || recv_video)) {
538 cricket::MediaDescriptionOptions media_desc_options =
539 cricket::MediaDescriptionOptions(
540 cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
541 cricket::RtpTransceiverDirection(send_video, recv_video),
542 kActive);
543 int num_sim_layer = 1;
544 if (send_stream_1_ && send_video) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700545 media_desc_options.AddVideoSender(kVideoTrack1, {kStream1},
zhihuang1c378ed2017-08-17 14:10:50 -0700546 num_sim_layer);
547 }
548 if (send_stream_2_ && send_video) {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700549 media_desc_options.AddVideoSender(kVideoTrack2, {kStream2},
zhihuang1c378ed2017-08-17 14:10:50 -0700550 num_sim_layer);
551 }
552 session_options->media_description_options.push_back(media_desc_options);
553 offered_media_sections_.push_back(media_desc_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700554 }
zhihuang1c378ed2017-08-17 14:10:50 -0700555
556 if (!session_options->has_data() &&
557 (data_channel_ ||
558 session_options->data_channel_type != cricket::DCT_NONE)) {
559 cricket::MediaDescriptionOptions media_desc_options =
560 cricket::MediaDescriptionOptions(
561 cricket::MEDIA_TYPE_DATA, cricket::CN_DATA,
562 cricket::RtpTransceiverDirection(true, true), kActive);
563 if (session_options->data_channel_type == cricket::DCT_RTP) {
564 media_desc_options.AddRtpDataChannel(data_channel_->label(),
565 data_channel_->label());
566 }
567 session_options->media_description_options.push_back(media_desc_options);
568 offered_media_sections_.push_back(media_desc_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700569 }
570 }
571
572 void GetOptionsForOffer(
573 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
574 cricket::MediaSessionOptions* session_options) {
zhihuang1c378ed2017-08-17 14:10:50 -0700575 ExtractSharedMediaSessionOptions(rtc_options, session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700576
zhihuang1c378ed2017-08-17 14:10:50 -0700577 // |recv_X| is true by default if |offer_to_receive_X| is undefined.
578 bool recv_audio = rtc_options.offer_to_receive_audio != 0;
579 bool recv_video = rtc_options.offer_to_receive_video != 0;
580
581 AddMediaSectionsAndSendersToOptions(session_options, local_send_audio_,
582 recv_audio, local_send_video_,
583 recv_video);
deadbeefc80741f2015-10-22 13:14:45 -0700584 session_options->bundle_enabled =
585 session_options->bundle_enabled &&
586 (session_options->has_audio() || session_options->has_video() ||
587 session_options->has_data());
588
zhihuang1c378ed2017-08-17 14:10:50 -0700589 session_options->crypto_options = crypto_options_;
590 }
591
592 void GetOptionsForAnswer(cricket::MediaSessionOptions* session_options) {
593 AddExistingMediaSectionsAndSendersToOptions(
594 session_options, local_send_audio_, local_recv_audio_,
595 local_send_video_, local_recv_video_);
596
597 session_options->bundle_enabled =
598 session_options->bundle_enabled &&
599 (session_options->has_audio() || session_options->has_video() ||
600 session_options->has_data());
601
602 if (session_->data_channel_type() != cricket::DCT_RTP) {
603 session_options->data_channel_type = session_->data_channel_type();
olka3c747662017-08-17 06:50:32 -0700604 }
605
zhihuanga77e6bb2017-08-14 18:17:48 -0700606 session_options->crypto_options = crypto_options_;
607 }
608
zhihuang1c378ed2017-08-17 14:10:50 -0700609 void GetOptionsForRemoteAnswer(
610 cricket::MediaSessionOptions* session_options) {
611 bool recv_audio = local_send_audio_ || remote_recv_audio_;
612 bool recv_video = local_send_video_ || remote_recv_video_;
613 bool send_audio = false;
614 bool send_video = false;
zhihuanga77e6bb2017-08-14 18:17:48 -0700615
zhihuang1c378ed2017-08-17 14:10:50 -0700616 AddExistingMediaSectionsAndSendersToOptions(
617 session_options, send_audio, recv_audio, send_video, recv_video);
618
zhihuanga77e6bb2017-08-14 18:17:48 -0700619 session_options->bundle_enabled =
620 session_options->bundle_enabled &&
621 (session_options->has_audio() || session_options->has_video() ||
622 session_options->has_data());
623
624 if (session_->data_channel_type() != cricket::DCT_RTP) {
625 session_options->data_channel_type = session_->data_channel_type();
deadbeefab9b2d12015-10-14 11:33:11 -0700626 }
jbauchcb560652016-08-04 05:20:32 -0700627
deadbeef7914b8c2017-04-21 03:23:33 -0700628 session_options->crypto_options = crypto_options_;
deadbeefab9b2d12015-10-14 11:33:11 -0700629 }
630
zhihuang1c378ed2017-08-17 14:10:50 -0700631 void GetOptionsForRemoteOffer(cricket::MediaSessionOptions* session_options) {
632 AddMediaSectionsAndSendersToOptions(session_options, remote_send_audio_,
633 remote_recv_audio_, remote_send_video_,
634 remote_recv_video_);
635 session_options->bundle_enabled =
636 (session_options->has_audio() || session_options->has_video() ||
637 session_options->has_data());
638
639 if (session_->data_channel_type() != cricket::DCT_RTP) {
640 session_options->data_channel_type = session_->data_channel_type();
641 }
642
643 session_options->crypto_options = crypto_options_;
644 }
645
deadbeefab9b2d12015-10-14 11:33:11 -0700646 // Creates a local offer and applies it. Starts ICE.
647 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 // to decide which streams to create.
649 void InitiateCall() {
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000650 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 SetLocalDescriptionWithoutError(offer);
652 EXPECT_TRUE_WAIT(PeerConnectionInterface::kIceGatheringNew !=
653 observer_.ice_gathering_state_,
654 kIceCandidatesTimeout);
655 }
656
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000657 SessionDescriptionInterface* CreateOffer() {
658 PeerConnectionInterface::RTCOfferAnswerOptions options;
659 options.offer_to_receive_audio =
660 RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000661 return CreateOffer(options);
662 }
663
wu@webrtc.org91053e72013-08-10 07:18:04 +0000664 SessionDescriptionInterface* CreateOffer(
htaa2a49d92016-03-04 02:51:39 -0800665 const PeerConnectionInterface::RTCOfferAnswerOptions options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000666 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest>
wu@webrtc.org91053e72013-08-10 07:18:04 +0000667 observer = new WebRtcSessionCreateSDPObserverForTest();
deadbeefab9b2d12015-10-14 11:33:11 -0700668 cricket::MediaSessionOptions session_options;
669 GetOptionsForOffer(options, &session_options);
670 session_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000671 EXPECT_TRUE_WAIT(
672 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000673 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000674 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000675 }
676
677 SessionDescriptionInterface* CreateAnswer(
htaa2a49d92016-03-04 02:51:39 -0800678 const cricket::MediaSessionOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000679 rtc::scoped_refptr<WebRtcSessionCreateSDPObserverForTest> observer
wu@webrtc.org91053e72013-08-10 07:18:04 +0000680 = new WebRtcSessionCreateSDPObserverForTest();
htaa2a49d92016-03-04 02:51:39 -0800681 cricket::MediaSessionOptions session_options = options;
682 GetOptionsForAnswer(&session_options);
htaa2a49d92016-03-04 02:51:39 -0800683 session_->CreateAnswer(observer, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000684 EXPECT_TRUE_WAIT(
685 observer->state() != WebRtcSessionCreateSDPObserverForTest::kInit,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000686 2000);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000687 return observer->ReleaseDescription();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000688 }
689
htaa2a49d92016-03-04 02:51:39 -0800690 SessionDescriptionInterface* CreateAnswer() {
691 cricket::MediaSessionOptions options;
zhihuang1c378ed2017-08-17 14:10:50 -0700692 options.bundle_enabled = true;
htaa2a49d92016-03-04 02:51:39 -0800693 return CreateAnswer(options);
694 }
695
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 // Set the internal fake description factories to do DTLS-SRTP.
697 void SetFactoryDtlsSrtp() {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000698 desc_factory_->set_secure(cricket::SEC_DISABLED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 std::string identity_name = "WebRTC" +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700 rtc::ToString(rtc::CreateRandomId());
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200701 // Confirmed to work with KT_RSA and KT_ECDSA.
kwiberg0eb15ed2015-12-17 03:04:15 -0800702 tdesc_factory_->set_certificate(
jbauch555604a2016-04-26 03:13:22 -0700703 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>(
kwiberg0eb15ed2015-12-17 03:04:15 -0800704 rtc::SSLIdentity::Generate(identity_name, rtc::KT_DEFAULT))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 tdesc_factory_->set_secure(cricket::SEC_REQUIRED);
706 }
707
deadbeef0ed85b22016-02-23 17:24:52 -0800708 // Compares ufrag/password only for the specified |media_type|.
709 bool IceUfragPwdEqual(const cricket::SessionDescription* desc1,
710 const cricket::SessionDescription* desc2,
711 cricket::MediaType media_type) {
712 if (desc1->contents().size() != desc2->contents().size()) {
713 return false;
714 }
715
716 const cricket::ContentInfo* cinfo =
717 cricket::GetFirstMediaContent(desc1->contents(), media_type);
718 const cricket::TransportDescription* transport_desc1 =
719 desc1->GetTransportDescriptionByName(cinfo->name);
720 const cricket::TransportDescription* transport_desc2 =
721 desc2->GetTransportDescriptionByName(cinfo->name);
722 if (!transport_desc1 || !transport_desc2) {
723 return false;
724 }
725 if (transport_desc1->ice_pwd != transport_desc2->ice_pwd ||
726 transport_desc1->ice_ufrag != transport_desc2->ice_ufrag) {
727 return false;
728 }
729 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000731
deadbeef0ed85b22016-02-23 17:24:52 -0800732 // Sets ufrag/pwd for specified |media_type|.
733 void SetIceUfragPwd(SessionDescriptionInterface* current_desc,
734 cricket::MediaType media_type,
735 const std::string& ufrag,
736 const std::string& pwd) {
737 cricket::SessionDescription* desc = current_desc->description();
738 const cricket::ContentInfo* cinfo =
739 cricket::GetFirstMediaContent(desc->contents(), media_type);
740 TransportInfo* transport_info = desc->GetTransportInfoByName(cinfo->name);
741 cricket::TransportDescription* transport_desc =
742 &transport_info->description;
743 transport_desc->ice_ufrag = ufrag;
744 transport_desc->ice_pwd = pwd;
745 }
746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 void SetLocalDescriptionWithoutError(SessionDescriptionInterface* desc) {
Steve Anton3df5dca2017-10-19 16:11:30 -0700748 ASSERT_TRUE(session_->SetLocalDescription(rtc::WrapUnique(desc), nullptr));
deadbeefcbecd352015-09-23 11:50:27 -0700749 session_->MaybeStartGathering();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000751 void SetLocalDescriptionExpectError(const std::string& action,
752 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 SessionDescriptionInterface* desc) {
754 std::string error;
Steve Anton3df5dca2017-10-19 16:11:30 -0700755 EXPECT_FALSE(session_->SetLocalDescription(rtc::WrapUnique(desc), &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000756 std::string sdp_type = "local ";
757 sdp_type.append(action);
758 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 EXPECT_NE(std::string::npos, error.find(expected_error));
760 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000761 void SetLocalDescriptionOfferExpectError(const std::string& expected_error,
762 SessionDescriptionInterface* desc) {
763 SetLocalDescriptionExpectError(SessionDescriptionInterface::kOffer,
764 expected_error, desc);
765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 void SetRemoteDescriptionWithoutError(SessionDescriptionInterface* desc) {
Steve Anton3df5dca2017-10-19 16:11:30 -0700767 ASSERT_TRUE(session_->SetRemoteDescription(rtc::WrapUnique(desc), nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000769 void SetRemoteDescriptionExpectError(const std::string& action,
770 const std::string& expected_error,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 SessionDescriptionInterface* desc) {
772 std::string error;
Steve Anton3df5dca2017-10-19 16:11:30 -0700773 EXPECT_FALSE(session_->SetRemoteDescription(rtc::WrapUnique(desc), &error));
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000774 std::string sdp_type = "remote ";
775 sdp_type.append(action);
776 EXPECT_NE(std::string::npos, error.find(sdp_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 EXPECT_NE(std::string::npos, error.find(expected_error));
778 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000779 void SetRemoteDescriptionOfferExpectError(
780 const std::string& expected_error, SessionDescriptionInterface* desc) {
781 SetRemoteDescriptionExpectError(SessionDescriptionInterface::kOffer,
782 expected_error, desc);
783 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 JsepSessionDescription* CreateRemoteOfferWithVersion(
786 cricket::MediaSessionOptions options,
787 cricket::SecurePolicy secure_policy,
788 const std::string& session_version,
789 const SessionDescriptionInterface* current_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000790 std::string session_id = rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 const cricket::SessionDescription* cricket_desc = NULL;
792 if (current_desc) {
793 cricket_desc = current_desc->description();
794 session_id = current_desc->session_id();
795 }
796
797 desc_factory_->set_secure(secure_policy);
798 JsepSessionDescription* offer(
799 new JsepSessionDescription(JsepSessionDescription::kOffer));
800 if (!offer->Initialize(desc_factory_->CreateOffer(options, cricket_desc),
801 session_id, session_version)) {
802 delete offer;
803 offer = NULL;
804 }
805 return offer;
806 }
807 JsepSessionDescription* CreateRemoteOffer(
808 cricket::MediaSessionOptions options) {
809 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
810 kSessionVersion, NULL);
811 }
812 JsepSessionDescription* CreateRemoteOffer(
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000813 cricket::MediaSessionOptions options, cricket::SecurePolicy sdes_policy) {
814 return CreateRemoteOfferWithVersion(
815 options, sdes_policy, kSessionVersion, NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817 JsepSessionDescription* CreateRemoteOffer(
818 cricket::MediaSessionOptions options,
819 const SessionDescriptionInterface* current_desc) {
820 return CreateRemoteOfferWithVersion(options, cricket::SEC_ENABLED,
821 kSessionVersion, current_desc);
822 }
823
Steve Anton6d64e9a2017-09-12 09:44:05 -0700824 SessionDescriptionInterface* CreateRemoteOfferWithSctpPort(
825 const char* sctp_stream_name,
826 int new_port,
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000827 cricket::MediaSessionOptions options) {
828 options.data_channel_type = cricket::DCT_SCTP;
zhihuang1c378ed2017-08-17 14:10:50 -0700829 GetOptionsForRemoteOffer(&options);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000830 return ChangeSDPSctpPort(new_port, CreateRemoteOffer(options));
831 }
832
833 // Takes ownership of offer_basis (and deletes it).
Steve Anton6d64e9a2017-09-12 09:44:05 -0700834 SessionDescriptionInterface* ChangeSDPSctpPort(
835 int new_port,
836 webrtc::SessionDescriptionInterface* offer_basis) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000837 // Stringify the input SDP, swap the 5000 for 'new_port' and create a new
838 // SessionDescription from the mutated string.
839 const char* default_port_str = "5000";
840 char new_port_str[16];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 rtc::sprintfn(new_port_str, sizeof(new_port_str), "%d", new_port);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000842 std::string offer_str;
843 offer_basis->ToString(&offer_str);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000844 rtc::replace_substrs(default_port_str, strlen(default_port_str),
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000845 new_port_str, strlen(new_port_str),
846 &offer_str);
Steve Anton6d64e9a2017-09-12 09:44:05 -0700847 SessionDescriptionInterface* offer =
848 CreateSessionDescription(offer_basis->type(), offer_str, nullptr);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000849 delete offer_basis;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000850 return offer;
851 }
852
deadbeefab9b2d12015-10-14 11:33:11 -0700853 // Create a remote offer. Call SendAudioVideoStreamX()
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 // before this function to decide which streams to create.
855 JsepSessionDescription* CreateRemoteOffer() {
856 cricket::MediaSessionOptions options;
zhihuang1c378ed2017-08-17 14:10:50 -0700857 GetOptionsForRemoteOffer(&options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return CreateRemoteOffer(options, session_->remote_description());
859 }
860
861 JsepSessionDescription* CreateRemoteAnswer(
862 const SessionDescriptionInterface* offer,
863 cricket::MediaSessionOptions options,
864 cricket::SecurePolicy policy) {
865 desc_factory_->set_secure(policy);
866 const std::string session_id =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000867 rtc::ToString(rtc::CreateRandomId64());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 JsepSessionDescription* answer(
869 new JsepSessionDescription(JsepSessionDescription::kAnswer));
870 if (!answer->Initialize(desc_factory_->CreateAnswer(offer->description(),
871 options, NULL),
872 session_id, kSessionVersion)) {
873 delete answer;
874 answer = NULL;
875 }
876 return answer;
877 }
878
879 JsepSessionDescription* CreateRemoteAnswer(
880 const SessionDescriptionInterface* offer,
881 cricket::MediaSessionOptions options) {
882 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
883 }
884
deadbeefab9b2d12015-10-14 11:33:11 -0700885 // Creates an answer session description.
886 // Call SendAudioVideoStreamX() before this function
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 // to decide which streams to create.
888 JsepSessionDescription* CreateRemoteAnswer(
889 const SessionDescriptionInterface* offer) {
890 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -0800891 GetOptionsForAnswer(&options);
zhihuang1c378ed2017-08-17 14:10:50 -0700892 options.bundle_enabled = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 return CreateRemoteAnswer(offer, options, cricket::SEC_REQUIRED);
894 }
895
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 // The method sets up a call from the session to itself, in a loopback
897 // arrangement. It also uses a firewall rule to create a temporary
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000898 // disconnection, and then a permanent disconnection.
899 // This code is placed in a method so that it can be invoked
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 // by multiple tests with different allocators (e.g. with and without BUNDLE).
901 // While running the call, this method also checks if the session goes through
902 // the correct sequence of ICE states when a connection is established,
903 // broken, and re-established.
904 // The Connection state should go:
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000905 // New -> Checking -> (Connected) -> Completed -> Disconnected -> Completed
906 // -> Failed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 // The Gathering state should go: New -> Gathering -> Completed.
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000908
stefanc1aeaf02015-10-15 07:26:07 -0700909 void SetupLoopbackCall() {
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000910 Init();
deadbeefab9b2d12015-10-14 11:33:11 -0700911 SendAudioVideoStream1();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000912 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913
914 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
915 observer_.ice_gathering_state_);
916 SetLocalDescriptionWithoutError(offer);
917 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
918 observer_.ice_connection_state_);
919 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringGathering,
stefanc1aeaf02015-10-15 07:26:07 -0700920 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
922 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
stefanc1aeaf02015-10-15 07:26:07 -0700923 observer_.ice_gathering_state_, kIceCandidatesTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924
925 std::string sdp;
926 offer->ToString(&sdp);
stefanc1aeaf02015-10-15 07:26:07 -0700927 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
928 JsepSessionDescription::kAnswer, sdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 ASSERT_TRUE(desc != NULL);
930 SetRemoteDescriptionWithoutError(desc);
931
932 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionChecking,
stefanc1aeaf02015-10-15 07:26:07 -0700933 observer_.ice_connection_state_, kIceCandidatesTimeout);
mallinath@webrtc.orgd3dc4242014-03-01 00:05:52 +0000934
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000935 // The ice connection state is "Connected" too briefly to catch in a test.
936 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
stefanc1aeaf02015-10-15 07:26:07 -0700937 observer_.ice_connection_state_, kIceCandidatesTimeout);
938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939
stefanc1aeaf02015-10-15 07:26:07 -0700940 void TestPacketOptions() {
Steve Antonede9ca52017-10-16 13:04:27 -0700941 AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
stefanc1aeaf02015-10-15 07:26:07 -0700942
943 SetupLoopbackCall();
944
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200945 // Wait for channel to be ready for sending.
946 EXPECT_TRUE_WAIT(media_engine_->GetVideoChannel(0)->sending(), 100);
stefanc1aeaf02015-10-15 07:26:07 -0700947 uint8_t test_packet[15] = {0};
948 rtc::PacketOptions options;
949 options.packet_id = 10;
950 media_engine_->GetVideoChannel(0)
951 ->SendRtp(test_packet, sizeof(test_packet), options);
952
953 const int kPacketTimeout = 2000;
deadbeef14461d42016-06-15 11:06:57 -0700954 EXPECT_EQ_WAIT(10, fake_call_.last_sent_nonnegative_packet_id(),
955 kPacketTimeout);
stefanc1aeaf02015-10-15 07:26:07 -0700956 EXPECT_GT(fake_call_.last_sent_packet().send_time_ms, -1);
957 }
958
deadbeefab9b2d12015-10-14 11:33:11 -0700959 void CreateDataChannel() {
deadbeeffc648b62015-10-13 16:42:33 -0700960 webrtc::InternalDataChannelInit dci;
nissec8ee8822017-01-18 07:20:55 -0800961 RTC_CHECK(session_.get());
deadbeefab9b2d12015-10-14 11:33:11 -0700962 dci.reliable = session_->data_channel_type() == cricket::DCT_SCTP;
963 data_channel_ = DataChannel::Create(
964 session_.get(), session_->data_channel_type(), "datachannel", dci);
965 }
966
967 void SetLocalDescriptionWithDataChannel() {
968 CreateDataChannel();
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000969 SessionDescriptionInterface* offer = CreateOffer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 SetLocalDescriptionWithoutError(offer);
971 }
972
skvlad11a9cbf2016-10-07 11:53:05 -0700973 webrtc::RtcEventLogNullImpl event_log_;
nisse7eaa4ea2017-05-08 05:25:41 -0700974 std::unique_ptr<rtc::VirtualSocketServer> vss_;
975 std::unique_ptr<rtc::FirewallSocketServer> fss_;
976 rtc::AutoSocketServerThread thread_;
deadbeef112b2e92017-02-10 20:13:37 -0800977 // |media_engine_| and |data_engine_| are actually owned by
978 // |channel_manager_|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 cricket::FakeMediaEngine* media_engine_;
980 cricket::FakeDataEngine* data_engine_;
deadbeef953c2ce2017-01-09 14:53:41 -0800981 // Actually owned by session_.
982 FakeSctpTransportFactory* fake_sctp_transport_factory_ = nullptr;
kwibergd1fe2812016-04-27 06:47:29 -0700983 std::unique_ptr<cricket::ChannelManager> channel_manager_;
stefanc1aeaf02015-10-15 07:26:07 -0700984 cricket::FakeCall fake_call_;
kwibergd1fe2812016-04-27 06:47:29 -0700985 std::unique_ptr<cricket::TransportDescriptionFactory> tdesc_factory_;
986 std::unique_ptr<cricket::MediaSessionDescriptionFactory> desc_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000987 rtc::SocketAddress stun_socket_addr_;
kwibergd1fe2812016-04-27 06:47:29 -0700988 std::unique_ptr<cricket::TestStunServer> stun_server_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000989 rtc::FakeNetworkManager network_manager_;
kwibergd1fe2812016-04-27 06:47:29 -0700990 std::unique_ptr<cricket::BasicPortAllocator> allocator_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000991 PeerConnectionFactoryInterface::Options options_;
htaa2a49d92016-03-04 02:51:39 -0800992 PeerConnectionInterface::RTCConfiguration configuration_;
kwibergd1fe2812016-04-27 06:47:29 -0700993 std::unique_ptr<WebRtcSessionForTest> session_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 MockIceObserver observer_;
995 cricket::FakeVideoMediaChannel* video_channel_;
996 cricket::FakeVoiceMediaChannel* voice_channel_;
jbauchac8869e2015-07-03 01:36:14 -0700997 rtc::scoped_refptr<FakeMetricsObserver> metrics_observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700998 // The following flags affect options created for CreateOffer/CreateAnswer.
999 bool send_stream_1_ = false;
1000 bool send_stream_2_ = false;
zhihuang1c378ed2017-08-17 14:10:50 -07001001 bool local_send_audio_ = false;
1002 bool local_send_video_ = false;
1003 bool local_recv_audio_ = true;
1004 bool local_recv_video_ = true;
1005 bool remote_send_audio_ = false;
1006 bool remote_send_video_ = false;
1007 bool remote_recv_audio_ = true;
1008 bool remote_recv_video_ = true;
1009 std::vector<cricket::MediaDescriptionOptions> offered_media_sections_;
deadbeefab9b2d12015-10-14 11:33:11 -07001010 rtc::scoped_refptr<DataChannel> data_channel_;
1011 // Last values received from data channel creation signal.
1012 std::string last_data_channel_label_;
1013 InternalDataChannelInit last_data_channel_config_;
deadbeef7914b8c2017-04-21 03:23:33 -07001014 rtc::CryptoOptions crypto_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015};
1016
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001017// Test that we can create and set an answer correctly when different
1018// SSL roles have been negotiated for different transports.
1019// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4525
1020TEST_P(WebRtcSessionTest, TestCreateAnswerWithDifferentSslRoles) {
1021 SendAudioVideoStream1();
1022 InitWithDtls(GetParam());
1023 SetFactoryDtlsSrtp();
1024
1025 SessionDescriptionInterface* offer = CreateOffer();
1026 SetLocalDescriptionWithoutError(offer);
1027
1028 cricket::MediaSessionOptions options;
zhihuang1c378ed2017-08-17 14:10:50 -07001029 GetOptionsForAnswer(&options);
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001030
1031 // First, negotiate different SSL roles.
1032 SessionDescriptionInterface* answer =
1033 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1034 TransportInfo* audio_transport_info =
1035 answer->description()->GetTransportInfoByName("audio");
1036 audio_transport_info->description.connection_role =
1037 cricket::CONNECTIONROLE_ACTIVE;
1038 TransportInfo* video_transport_info =
1039 answer->description()->GetTransportInfoByName("video");
1040 video_transport_info->description.connection_role =
1041 cricket::CONNECTIONROLE_PASSIVE;
1042 SetRemoteDescriptionWithoutError(answer);
1043
1044 // Now create an offer in the reverse direction, and ensure the initial
1045 // offerer responds with an answer with correct SSL roles.
1046 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
1047 kSessionVersion,
1048 session_->remote_description());
1049 SetRemoteDescriptionWithoutError(offer);
1050
zhihuang1c378ed2017-08-17 14:10:50 -07001051 cricket::MediaSessionOptions answer_options;
1052 answer_options.bundle_enabled = true;
1053 answer = CreateAnswer(answer_options);
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001054 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
1055 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1056 audio_transport_info->description.connection_role);
1057 video_transport_info = answer->description()->GetTransportInfoByName("video");
1058 EXPECT_EQ(cricket::CONNECTIONROLE_ACTIVE,
1059 video_transport_info->description.connection_role);
1060 SetLocalDescriptionWithoutError(answer);
1061
1062 // Lastly, start BUNDLE-ing on "audio", expecting that the "passive" role of
1063 // audio is transferred over to video in the answer that completes the BUNDLE
1064 // negotiation.
1065 options.bundle_enabled = true;
1066 offer = CreateRemoteOfferWithVersion(options, cricket::SEC_DISABLED,
1067 kSessionVersion,
1068 session_->remote_description());
1069 SetRemoteDescriptionWithoutError(offer);
zhihuang1c378ed2017-08-17 14:10:50 -07001070 answer = CreateAnswer(answer_options);
Taylor Brandstetterf475d362016-01-08 15:35:57 -08001071 audio_transport_info = answer->description()->GetTransportInfoByName("audio");
1072 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1073 audio_transport_info->description.connection_role);
1074 video_transport_info = answer->description()->GetTransportInfoByName("video");
1075 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
1076 video_transport_info->description.connection_role);
1077 SetLocalDescriptionWithoutError(answer);
1078}
1079
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080TEST_F(WebRtcSessionTest, TestRtpDataChannel) {
htaa2a49d92016-03-04 02:51:39 -08001081 configuration_.enable_rtp_data_channel = true;
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +00001082 Init();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 SetLocalDescriptionWithDataChannel();
htaa2a49d92016-03-04 02:51:39 -08001084 ASSERT_TRUE(data_engine_);
deadbeef953c2ce2017-01-09 14:53:41 -08001085 EXPECT_NE(nullptr, data_engine_->GetChannel(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086}
1087
Henrik Boström87713d02015-08-25 09:53:21 +02001088TEST_P(WebRtcSessionTest, TestRtpDataChannelConstraintTakesPrecedence) {
htaa2a49d92016-03-04 02:51:39 -08001089 configuration_.enable_rtp_data_channel = true;
wu@webrtc.org97077a32013-10-25 21:18:33 +00001090 options_.disable_sctp_data_channels = false;
1091
Henrik Boström87713d02015-08-25 09:53:21 +02001092 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093
1094 SetLocalDescriptionWithDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001095 EXPECT_NE(nullptr, data_engine_->GetChannel(0));
1096}
1097
1098// Test that sctp_content_name/sctp_transport_name (used for stats) are correct
1099// before and after BUNDLE is negotiated.
1100TEST_P(WebRtcSessionTest, SctpContentAndTransportName) {
deadbeef953c2ce2017-01-09 14:53:41 -08001101 SetFactoryDtlsSrtp();
1102 InitWithDtls(GetParam());
1103
1104 // Initially these fields should be empty.
1105 EXPECT_FALSE(session_->sctp_content_name());
1106 EXPECT_FALSE(session_->sctp_transport_name());
1107
1108 // Create offer with audio/video/data.
1109 // Default bundle policy is "balanced", so data should be using its own
1110 // transport.
1111 SendAudioVideoStream1();
1112 CreateDataChannel();
1113 InitiateCall();
1114 ASSERT_TRUE(session_->sctp_content_name());
1115 ASSERT_TRUE(session_->sctp_transport_name());
1116 EXPECT_EQ("data", *session_->sctp_content_name());
1117 EXPECT_EQ("data", *session_->sctp_transport_name());
1118
1119 // Create answer that finishes BUNDLE negotiation, which means everything
1120 // should be bundled on the first transport (audio).
1121 cricket::MediaSessionOptions answer_options;
deadbeef953c2ce2017-01-09 14:53:41 -08001122 answer_options.bundle_enabled = true;
1123 answer_options.data_channel_type = cricket::DCT_SCTP;
zhihuang1c378ed2017-08-17 14:10:50 -07001124 GetOptionsForAnswer(&answer_options);
deadbeef953c2ce2017-01-09 14:53:41 -08001125 SetRemoteDescriptionWithoutError(CreateRemoteAnswer(
1126 session_->local_description(), answer_options, cricket::SEC_DISABLED));
1127 ASSERT_TRUE(session_->sctp_content_name());
1128 ASSERT_TRUE(session_->sctp_transport_name());
1129 EXPECT_EQ("data", *session_->sctp_content_name());
1130 EXPECT_EQ("audio", *session_->sctp_transport_name());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131}
1132
Henrik Boström87713d02015-08-25 09:53:21 +02001133TEST_P(WebRtcSessionTest, TestCreateOfferWithSctpEnabledWithoutStreams) {
Henrik Boström87713d02015-08-25 09:53:21 +02001134 InitWithDtls(GetParam());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001135
kwibergd1fe2812016-04-27 06:47:29 -07001136 std::unique_ptr<SessionDescriptionInterface> offer(CreateOffer());
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001137 EXPECT_TRUE(offer->description()->GetContentByName("data") == NULL);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001138 EXPECT_TRUE(offer->description()->GetTransportInfoByName("data") == NULL);
1139}
1140
Henrik Boström87713d02015-08-25 09:53:21 +02001141TEST_P(WebRtcSessionTest, TestCreateAnswerWithSctpInOfferAndNoStreams) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001142 SetFactoryDtlsSrtp();
Henrik Boström87713d02015-08-25 09:53:21 +02001143 InitWithDtls(GetParam());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001144
1145 // Create remote offer with SCTP.
1146 cricket::MediaSessionOptions options;
1147 options.data_channel_type = cricket::DCT_SCTP;
zhihuang1c378ed2017-08-17 14:10:50 -07001148 GetOptionsForRemoteOffer(&options);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001149 JsepSessionDescription* offer =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001150 CreateRemoteOffer(options, cricket::SEC_DISABLED);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001151 SetRemoteDescriptionWithoutError(offer);
1152
1153 // Verifies the answer contains SCTP.
kwibergd1fe2812016-04-27 06:47:29 -07001154 std::unique_ptr<SessionDescriptionInterface> answer(CreateAnswer());
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001155 EXPECT_TRUE(answer != NULL);
1156 EXPECT_TRUE(answer->description()->GetContentByName("data") != NULL);
1157 EXPECT_TRUE(answer->description()->GetTransportInfoByName("data") != NULL);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001158}
1159
deadbeef953c2ce2017-01-09 14:53:41 -08001160// Test that if DTLS is disabled, we don't end up with an SctpTransport
1161// created (or an RtpDataChannel).
Henrik Boström87713d02015-08-25 09:53:21 +02001162TEST_P(WebRtcSessionTest, TestSctpDataChannelWithoutDtls) {
htaa2a49d92016-03-04 02:51:39 -08001163 configuration_.enable_dtls_srtp = rtc::Optional<bool>(false);
Henrik Boström87713d02015-08-25 09:53:21 +02001164 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165
1166 SetLocalDescriptionWithDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001167 EXPECT_EQ(nullptr, data_engine_->GetChannel(0));
1168 EXPECT_EQ(nullptr, fake_sctp_transport_factory_->last_fake_sctp_transport());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169}
1170
deadbeef953c2ce2017-01-09 14:53:41 -08001171// Test that if DTLS is enabled, we end up with an SctpTransport created
1172// (and not an RtpDataChannel).
Henrik Boström87713d02015-08-25 09:53:21 +02001173TEST_P(WebRtcSessionTest, TestSctpDataChannelWithDtls) {
Henrik Boström87713d02015-08-25 09:53:21 +02001174 InitWithDtls(GetParam());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175
1176 SetLocalDescriptionWithDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001177 EXPECT_EQ(nullptr, data_engine_->GetChannel(0));
1178 EXPECT_NE(nullptr, fake_sctp_transport_factory_->last_fake_sctp_transport());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179}
wu@webrtc.org91053e72013-08-10 07:18:04 +00001180
deadbeef953c2ce2017-01-09 14:53:41 -08001181// Test that if SCTP is disabled, we don't end up with an SctpTransport
1182// created (or an RtpDataChannel).
Henrik Boström87713d02015-08-25 09:53:21 +02001183TEST_P(WebRtcSessionTest, TestDisableSctpDataChannels) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00001184 options_.disable_sctp_data_channels = true;
Henrik Boström87713d02015-08-25 09:53:21 +02001185 InitWithDtls(GetParam());
wu@webrtc.org97077a32013-10-25 21:18:33 +00001186
1187 SetLocalDescriptionWithDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001188 EXPECT_EQ(nullptr, data_engine_->GetChannel(0));
1189 EXPECT_EQ(nullptr, fake_sctp_transport_factory_->last_fake_sctp_transport());
wu@webrtc.org97077a32013-10-25 21:18:33 +00001190}
1191
Henrik Boström87713d02015-08-25 09:53:21 +02001192TEST_P(WebRtcSessionTest, TestSctpDataChannelSendPortParsing) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001193 const int new_send_port = 9998;
1194 const int new_recv_port = 7775;
1195
Henrik Boström87713d02015-08-25 09:53:21 +02001196 InitWithDtls(GetParam());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001197 SetFactoryDtlsSrtp();
1198
1199 // By default, don't actually add the codecs to desc_factory_; they don't
1200 // actually get serialized for SCTP in BuildMediaDescription(). Instead,
1201 // let the session description get parsed. That'll get the proper codecs
1202 // into the stream.
1203 cricket::MediaSessionOptions options;
Steve Anton6d64e9a2017-09-12 09:44:05 -07001204 SessionDescriptionInterface* offer =
1205 CreateRemoteOfferWithSctpPort("stream1", new_send_port, options);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001206
1207 // SetRemoteDescription will take the ownership of the offer.
1208 SetRemoteDescriptionWithoutError(offer);
1209
htaa2a49d92016-03-04 02:51:39 -08001210 SessionDescriptionInterface* answer =
1211 ChangeSDPSctpPort(new_recv_port, CreateAnswer());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001212 ASSERT_TRUE(answer != NULL);
1213
1214 // Now set the local description, which'll take ownership of the answer.
1215 SetLocalDescriptionWithoutError(answer);
1216
1217 // TEST PLAN: Set the port number to something new, set it in the SDP,
1218 // and pass it all the way down.
deadbeef953c2ce2017-01-09 14:53:41 -08001219 EXPECT_EQ(nullptr, data_engine_->GetChannel(0));
deadbeefab9b2d12015-10-14 11:33:11 -07001220 CreateDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001221 ASSERT_NE(nullptr, fake_sctp_transport_factory_->last_fake_sctp_transport());
1222 EXPECT_EQ(
1223 new_recv_port,
1224 fake_sctp_transport_factory_->last_fake_sctp_transport()->local_port());
1225 EXPECT_EQ(
1226 new_send_port,
1227 fake_sctp_transport_factory_->last_fake_sctp_transport()->remote_port());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001228}
1229
deadbeef953c2ce2017-01-09 14:53:41 -08001230// Verifies that when a session's SctpTransport receives an OPEN message,
1231// WebRtcSession signals the SctpTransport creation request with the expected
deadbeefab9b2d12015-10-14 11:33:11 -07001232// config.
1233TEST_P(WebRtcSessionTest, TestSctpDataChannelOpenMessage) {
deadbeefab9b2d12015-10-14 11:33:11 -07001234 InitWithDtls(GetParam());
1235
1236 SetLocalDescriptionWithDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001237 EXPECT_EQ(nullptr, data_engine_->GetChannel(0));
1238 ASSERT_NE(nullptr, fake_sctp_transport_factory_->last_fake_sctp_transport());
deadbeefab9b2d12015-10-14 11:33:11 -07001239
deadbeef953c2ce2017-01-09 14:53:41 -08001240 // Make the fake SCTP transport pretend it received an OPEN message.
deadbeefab9b2d12015-10-14 11:33:11 -07001241 webrtc::DataChannelInit config;
1242 config.id = 1;
jbaucheec21bd2016-03-20 06:15:43 -07001243 rtc::CopyOnWriteBuffer payload;
deadbeefab9b2d12015-10-14 11:33:11 -07001244 webrtc::WriteDataChannelOpenMessage("a", config, &payload);
1245 cricket::ReceiveDataParams params;
1246 params.ssrc = config.id;
1247 params.type = cricket::DMT_CONTROL;
deadbeef953c2ce2017-01-09 14:53:41 -08001248 fake_sctp_transport_factory_->last_fake_sctp_transport()->SignalDataReceived(
1249 params, payload);
deadbeefab9b2d12015-10-14 11:33:11 -07001250
deadbeef953c2ce2017-01-09 14:53:41 -08001251 EXPECT_EQ_WAIT("a", last_data_channel_label_, kDefaultTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07001252 EXPECT_EQ(config.id, last_data_channel_config_.id);
1253 EXPECT_FALSE(last_data_channel_config_.negotiated);
1254 EXPECT_EQ(webrtc::InternalDataChannelInit::kAcker,
1255 last_data_channel_config_.open_handshake_role);
1256}
1257
zhihuang9763d562016-08-05 11:14:50 -07001258#ifdef HAVE_QUIC
1259TEST_P(WebRtcSessionTest, TestNegotiateQuic) {
1260 configuration_.enable_quic = true;
1261 InitWithDtls(GetParam());
1262 EXPECT_TRUE(session_->data_channel_type() == cricket::DCT_QUIC);
1263 SessionDescriptionInterface* offer = CreateOffer();
1264 ASSERT_TRUE(offer);
1265 ASSERT_TRUE(offer->description());
1266 SetLocalDescriptionWithoutError(offer);
1267 cricket::MediaSessionOptions options;
zhihuang1c378ed2017-08-17 14:10:50 -07001268 GetOptionsForAnswer(&options);
zhihuang9763d562016-08-05 11:14:50 -07001269 SessionDescriptionInterface* answer =
1270 CreateRemoteAnswer(offer, options, cricket::SEC_DISABLED);
1271 ASSERT_TRUE(answer);
1272 ASSERT_TRUE(answer->description());
1273 SetRemoteDescriptionWithoutError(answer);
1274}
1275#endif // HAVE_QUIC
1276
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001277// This verifies that the voice channel after bundle has both options from video
1278// and voice channels.
1279TEST_F(WebRtcSessionTest, TestSetSocketOptionBeforeBundle) {
1280 InitWithBundlePolicy(PeerConnectionInterface::kBundlePolicyBalanced);
deadbeefab9b2d12015-10-14 11:33:11 -07001281 SendAudioVideoStream1();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001282
1283 PeerConnectionInterface::RTCOfferAnswerOptions options;
1284 options.use_rtp_mux = true;
1285
1286 SessionDescriptionInterface* offer = CreateOffer(options);
1287 SetLocalDescriptionWithoutError(offer);
1288
1289 session_->video_channel()->SetOption(cricket::BaseChannel::ST_RTP,
1290 rtc::Socket::Option::OPT_SNDBUF, 4000);
1291
1292 session_->voice_channel()->SetOption(cricket::BaseChannel::ST_RTP,
1293 rtc::Socket::Option::OPT_RCVBUF, 8000);
1294
1295 int option_val;
deadbeefcbecd352015-09-23 11:50:27 -07001296 EXPECT_TRUE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001297 rtc::Socket::Option::OPT_SNDBUF, &option_val));
1298 EXPECT_EQ(4000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07001299 EXPECT_FALSE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001300 rtc::Socket::Option::OPT_SNDBUF, &option_val));
1301
deadbeefcbecd352015-09-23 11:50:27 -07001302 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001303 rtc::Socket::Option::OPT_RCVBUF, &option_val));
1304 EXPECT_EQ(8000, option_val);
deadbeefcbecd352015-09-23 11:50:27 -07001305 EXPECT_FALSE(session_->video_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001306 rtc::Socket::Option::OPT_RCVBUF, &option_val));
1307
deadbeefcbecd352015-09-23 11:50:27 -07001308 EXPECT_NE(session_->voice_rtp_transport_channel(),
1309 session_->video_rtp_transport_channel());
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001310
deadbeefab9b2d12015-10-14 11:33:11 -07001311 SendAudioVideoStream2();
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001312 SessionDescriptionInterface* answer =
1313 CreateRemoteAnswer(session_->local_description());
1314 SetRemoteDescriptionWithoutError(answer);
1315
deadbeefcbecd352015-09-23 11:50:27 -07001316 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001317 rtc::Socket::Option::OPT_SNDBUF, &option_val));
1318 EXPECT_EQ(4000, option_val);
1319
deadbeefcbecd352015-09-23 11:50:27 -07001320 EXPECT_TRUE(session_->voice_rtp_transport_channel()->GetOption(
guoweis@webrtc.org4f852882015-03-12 20:09:44 +00001321 rtc::Socket::Option::OPT_RCVBUF, &option_val));
1322 EXPECT_EQ(8000, option_val);
1323}
1324
stefanc1aeaf02015-10-15 07:26:07 -07001325TEST_F(WebRtcSessionTest, TestPacketOptionsAndOnPacketSent) {
1326 TestPacketOptions();
1327}
1328
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329// TODO(bemasc): Add a TestIceStatesBundle with BUNDLE enabled. That test
1330// currently fails because upon disconnection and reconnection OnIceComplete is
1331// called more than once without returning to IceGatheringGathering.
Henrik Boström87713d02015-08-25 09:53:21 +02001332
deadbeefcbecd352015-09-23 11:50:27 -07001333INSTANTIATE_TEST_CASE_P(WebRtcSessionTests,
1334 WebRtcSessionTest,
1335 testing::Values(ALREADY_GENERATED,
1336 DTLS_IDENTITY_STORE));