blob: abff2778ccef540073a35803b0faf5f20061b6aa [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/call/audio_sink.h"
21#include "api/jsepicecandidate.h"
22#include "api/jsepsessiondescription.h"
23#include "api/peerconnectioninterface.h"
24#include "call/call.h"
25#include "media/base/mediaconstants.h"
26#include "media/sctp/sctptransportinternal.h"
27#include "p2p/base/portallocator.h"
28#include "pc/channel.h"
29#include "pc/channelmanager.h"
30#include "pc/mediasession.h"
31#include "pc/sctputils.h"
32#include "pc/webrtcsessiondescriptionfactory.h"
33#include "rtc_base/basictypes.h"
34#include "rtc_base/bind.h"
35#include "rtc_base/checks.h"
36#include "rtc_base/helpers.h"
37#include "rtc_base/logging.h"
38#include "rtc_base/stringencode.h"
39#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
zhihuang9763d562016-08-05 11:14:50 -070041#ifdef HAVE_QUIC
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "p2p/quic/quictransportchannel.h"
zhihuang9763d562016-08-05 11:14:50 -070043#endif // HAVE_QUIC
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045using cricket::ContentInfo;
46using cricket::ContentInfos;
47using cricket::MediaContentDescription;
48using cricket::SessionDescription;
49using cricket::TransportInfo;
50
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070051using cricket::LOCAL_PORT_TYPE;
52using cricket::STUN_PORT_TYPE;
53using cricket::RELAY_PORT_TYPE;
54using cricket::PRFLX_PORT_TYPE;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace webrtc {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// Error messages
Steve Anton6f25b092017-10-23 09:39:20 -070059const char kBundleWithoutRtcpMux[] =
60 "rtcp-mux must be enabled when BUNDLE "
61 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
Zhi Huang2a5e4262017-09-14 01:15:03 -070065const char kMlineMismatchInAnswer[] =
66 "The order of m-lines in answer doesn't match order in offer. Rejecting "
67 "answer.";
68const char kMlineMismatchInSubsequentOffer[] =
69 "The order of m-lines in subsequent offer doesn't match order from "
70 "previous offer/answer.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000071const char kPushDownTDFailed[] =
72 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000073const char kSdpWithoutDtlsFingerprint[] =
74 "Called with SDP without DTLS fingerprint.";
75const char kSdpWithoutSdesCrypto[] =
76 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000077const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000078 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000080const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080081const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000082 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080083const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000084 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070085const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070087IceCandidatePairType GetIceCandidatePairCounter(
88 const cricket::Candidate& local,
89 const cricket::Candidate& remote) {
90 const auto& l = local.type();
91 const auto& r = remote.type();
92 const auto& host = LOCAL_PORT_TYPE;
93 const auto& srflx = STUN_PORT_TYPE;
94 const auto& relay = RELAY_PORT_TYPE;
95 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070096 if (l == host && r == host) {
97 bool local_private = IPIsPrivate(local.address().ipaddr());
98 bool remote_private = IPIsPrivate(remote.address().ipaddr());
99 if (local_private) {
100 if (remote_private) {
101 return kIceCandidatePairHostPrivateHostPrivate;
102 } else {
103 return kIceCandidatePairHostPrivateHostPublic;
104 }
105 } else {
106 if (remote_private) {
107 return kIceCandidatePairHostPublicHostPrivate;
108 } else {
109 return kIceCandidatePairHostPublicHostPublic;
110 }
111 }
112 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700113 if (l == host && r == srflx)
114 return kIceCandidatePairHostSrflx;
115 if (l == host && r == relay)
116 return kIceCandidatePairHostRelay;
117 if (l == host && r == prflx)
118 return kIceCandidatePairHostPrflx;
119 if (l == srflx && r == host)
120 return kIceCandidatePairSrflxHost;
121 if (l == srflx && r == srflx)
122 return kIceCandidatePairSrflxSrflx;
123 if (l == srflx && r == relay)
124 return kIceCandidatePairSrflxRelay;
125 if (l == srflx && r == prflx)
126 return kIceCandidatePairSrflxPrflx;
127 if (l == relay && r == host)
128 return kIceCandidatePairRelayHost;
129 if (l == relay && r == srflx)
130 return kIceCandidatePairRelaySrflx;
131 if (l == relay && r == relay)
132 return kIceCandidatePairRelayRelay;
133 if (l == relay && r == prflx)
134 return kIceCandidatePairRelayPrflx;
135 if (l == prflx && r == host)
136 return kIceCandidatePairPrflxHost;
137 if (l == prflx && r == srflx)
138 return kIceCandidatePairPrflxSrflx;
139 if (l == prflx && r == relay)
140 return kIceCandidatePairPrflxRelay;
141 return kIceCandidatePairMax;
142}
143
Tommi589ae452017-10-15 21:20:46 +0000144// Verify that the order of media sections in |desc1| matches |desc2|. The
145// number of m= sections could be different.
146static bool MediaSectionsInSameOrder(const SessionDescription* desc1,
147 const SessionDescription* desc2) {
148 if (!desc1 || !desc2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 return false;
Zhi Huang2a5e4262017-09-14 01:15:03 -0700150 }
Tommi589ae452017-10-15 21:20:46 +0000151 for (size_t i = 0;
152 i < desc1->contents().size() && i < desc2->contents().size(); ++i) {
153 if ((desc2->contents()[i].name) != desc1->contents()[i].name) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 return false;
155 }
Tommi589ae452017-10-15 21:20:46 +0000156 const MediaContentDescription* desc2_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000157 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000158 desc2->contents()[i].description);
159 const MediaContentDescription* desc1_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000160 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000161 desc1->contents()[i].description);
162 if (desc2_mdesc->type() != desc1_mdesc->type()) {
wu@webrtc.org4e393072014-04-07 17:04:35 +0000163 return false;
164 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
166 return true;
167}
168
Zhi Huang2a5e4262017-09-14 01:15:03 -0700169static bool MediaSectionsHaveSameCount(const SessionDescription* desc1,
170 const SessionDescription* desc2) {
171 if (!desc1 || !desc2) {
172 return false;
173 }
174 return desc1->contents().size() == desc2->contents().size();
175}
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800178// fingerprint, unless it's in a BUNDLE group, in which case only the
179// BUNDLE-tag section (first media section/description in the BUNDLE group)
180// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
181// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
182// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000183static bool VerifyCrypto(const SessionDescription* desc,
184 bool dtls_enabled,
185 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800186 const cricket::ContentGroup* bundle =
187 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 const ContentInfos& contents = desc->contents();
189 for (size_t index = 0; index < contents.size(); ++index) {
190 const ContentInfo* cinfo = &contents[index];
191 if (cinfo->rejected) {
192 continue;
193 }
deadbeefb7892532017-02-22 19:35:18 -0800194 if (bundle && bundle->HasContentName(cinfo->name) &&
195 cinfo->name != *(bundle->FirstContentName())) {
196 // This isn't the first media section in the BUNDLE group, so it's not
197 // required to have crypto attributes, since only the crypto attributes
198 // from the first section actually get used.
199 continue;
200 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
deadbeefb7892532017-02-22 19:35:18 -0800202 // If the content isn't rejected or bundled into another m= section, crypto
203 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 const MediaContentDescription* media =
205 static_cast<const MediaContentDescription*>(cinfo->description);
206 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
207 if (!media || !tinfo) {
208 // Something is not right.
209 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 return false;
212 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000213 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000215 LOG(LS_WARNING) <<
216 "Session description must have DTLS fingerprint if DTLS enabled.";
217 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000218 return false;
219 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000220 } else {
221 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000222 LOG(LS_WARNING) <<
223 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000224 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000225 return false;
226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 }
228 }
229
230 return true;
231}
232
deadbeefb7892532017-02-22 19:35:18 -0800233// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
234// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
235// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000236static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800237 const cricket::ContentGroup* bundle =
238 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000239 const ContentInfos& contents = desc->contents();
240 for (size_t index = 0; index < contents.size(); ++index) {
241 const ContentInfo* cinfo = &contents[index];
242 if (cinfo->rejected) {
243 continue;
244 }
deadbeefb7892532017-02-22 19:35:18 -0800245 if (bundle && bundle->HasContentName(cinfo->name) &&
246 cinfo->name != *(bundle->FirstContentName())) {
247 // This isn't the first media section in the BUNDLE group, so it's not
248 // required to have ufrag/password, since only the ufrag/password from
249 // the first section actually get used.
250 continue;
251 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000252
deadbeefb7892532017-02-22 19:35:18 -0800253 // If the content isn't rejected or bundled into another m= section,
254 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000255 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
256 if (!tinfo) {
257 // Something is not right.
258 LOG(LS_ERROR) << kInvalidSdp;
259 return false;
260 }
261 if (tinfo->description.ice_ufrag.empty() ||
262 tinfo->description.ice_pwd.empty()) {
263 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
264 return false;
265 }
266 }
267 return true;
268}
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271 uint32_t ssrc,
272 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800273 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 const cricket::ContentInfo* audio_info =
276 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000277 if (audio_info) {
278 const cricket::MediaContentDescription* audio_content =
279 static_cast<const cricket::MediaContentDescription*>(
280 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000282 const auto* found =
283 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
284 if (found) {
285 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000286 return true;
287 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 }
289
290 const cricket::ContentInfo* video_info =
291 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000292 if (video_info) {
293 const cricket::MediaContentDescription* video_content =
294 static_cast<const cricket::MediaContentDescription*>(
295 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000297 const auto* found =
298 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
299 if (found) {
300 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000301 return true;
302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 }
304 return false;
305}
306
deadbeef953c2ce2017-01-09 14:53:41 -0800307// Get the SCTP port out of a SessionDescription.
308// Return -1 if not found.
309static int GetSctpPort(const SessionDescription* session_description) {
310 const ContentInfo* content_info = GetFirstDataContent(session_description);
311 RTC_DCHECK(content_info);
312 if (!content_info) {
313 return -1;
314 }
315 const cricket::DataContentDescription* data =
316 static_cast<const cricket::DataContentDescription*>(
317 (content_info->description));
318 std::string value;
319 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
320 cricket::kGoogleSctpDataCodecName);
321 for (const cricket::DataCodec& codec : data->codecs()) {
322 if (!codec.Matches(match_pattern)) {
323 continue;
324 }
325 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
326 return rtc::FromString<int>(value);
327 }
328 }
329 return -1;
330}
331
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332static bool BadSdp(const std::string& source,
333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
336 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700337 desc << "Failed to set " << source;
338 if (!type.empty()) {
339 desc << " " << type;
340 }
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000341 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 return false;
348}
349
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 const std::string& type,
352 const std::string& reason,
353 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000355 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000357 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 }
359}
360
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000361static bool BadLocalSdp(const std::string& type,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
365}
366
367static bool BadRemoteSdp(const std::string& type,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
371}
372
373static bool BadOfferSdp(cricket::ContentSource source,
374 const std::string& reason,
375 std::string* err_desc) {
376 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
377}
378
379static bool BadPranswerSdp(cricket::ContentSource source,
380 const std::string& reason,
381 std::string* err_desc) {
382 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
383 reason, err_desc);
384}
385
386static bool BadAnswerSdp(cricket::ContentSource source,
387 const std::string& reason,
388 std::string* err_desc) {
389 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390}
391
deadbeefd59daf82015-10-14 15:02:44 -0700392#define GET_STRING_OF_STATE(state) \
393 case webrtc::WebRtcSession::state: \
394 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 break;
396
deadbeefd59daf82015-10-14 15:02:44 -0700397static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 std::string result;
399 switch (state) {
400 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700401 GET_STRING_OF_STATE(STATE_SENTOFFER)
402 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
403 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
404 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700406 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
nissec80e7412017-01-11 05:56:46 -0800408 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
deadbeefd59daf82015-10-14 15:02:44 -0700414#define GET_STRING_OF_ERROR_CODE(err) \
415 case webrtc::WebRtcSession::err: \
416 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 break;
418
deadbeefd59daf82015-10-14 15:02:44 -0700419static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 std::string result;
421 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000423 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
424 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800426 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 break;
428 }
429 return result;
430}
431
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000432static std::string MakeErrorString(const std::string& error,
433 const std::string& desc) {
434 std::ostringstream ret;
435 ret << error << " " << desc;
436 return ret.str();
437}
438
439static std::string MakeTdErrorString(const std::string& desc) {
440 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
deadbeef0ed85b22016-02-23 17:24:52 -0800443// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
444bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
445 const SessionDescriptionInterface* new_desc,
446 const std::string& content_name) {
447 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700448 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
deadbeef0ed85b22016-02-23 17:24:52 -0800450 const SessionDescription* new_sd = new_desc->description();
451 const SessionDescription* old_sd = old_desc->description();
452 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
453 if (!cinfo || cinfo->rejected) {
454 return false;
455 }
456 // If the content isn't rejected, check if ufrag and password has changed.
457 const cricket::TransportDescription* new_transport_desc =
458 new_sd->GetTransportDescriptionByName(content_name);
459 const cricket::TransportDescription* old_transport_desc =
460 old_sd->GetTransportDescriptionByName(content_name);
461 if (!new_transport_desc || !old_transport_desc) {
462 // No transport description exists. This is not an ICE restart.
463 return false;
464 }
465 if (cricket::IceCredentialsChanged(
466 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
467 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
468 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
469 << ".";
470 return true;
471 }
472 return false;
473}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474
zhihuang29ff8442016-07-27 11:07:25 -0700475WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700476 Call* call,
477 cricket::ChannelManager* channel_manager,
478 const cricket::MediaConfig& media_config,
479 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700480 rtc::Thread* network_thread,
481 rtc::Thread* worker_thread,
482 rtc::Thread* signaling_thread,
483 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800484 std::unique_ptr<cricket::TransportController> transport_controller,
485 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700486 : network_thread_(network_thread),
487 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700488 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 // RFC 3264: The numeric value of the session id and version in the
490 // o line MUST be representable with a "64 bit signed integer".
491 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700492 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700493 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800494 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700495 media_config_(media_config),
496 event_log_(event_log),
497 call_(call),
498 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 ice_observer_(NULL),
500 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700501 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000503 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000505 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700506 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
507 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700508 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700509 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700510 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700511 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700512 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700513 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700514 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700515 transport_controller_->SignalCandidatesRemoved.connect(
516 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700517 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800518 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519}
520
521WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800522 RTC_DCHECK(signaling_thread()->IsCurrent());
Steve Anton169629a2017-08-30 17:36:36 -0700523 // Destroy video channels first since they may have a pointer to a voice
524 // channel.
525 for (auto* channel : video_channels_) {
526 DestroyVideoChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
Steve Anton169629a2017-08-30 17:36:36 -0700528 for (auto* channel : voice_channels_) {
529 DestroyVoiceChannel(channel);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000530 }
deadbeef953c2ce2017-01-09 14:53:41 -0800531 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800532 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800533 }
534 if (sctp_transport_) {
535 SignalDataChannelDestroyed();
536 network_thread_->Invoke<void>(
537 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 }
zhihuang9763d562016-08-05 11:14:50 -0700539#ifdef HAVE_QUIC
540 if (quic_data_transport_) {
541 quic_data_transport_.reset();
542 }
543#endif
deadbeefd59daf82015-10-14 15:02:44 -0700544
545 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546}
547
wu@webrtc.org91053e72013-08-10 07:18:04 +0000548bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000549 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200550 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200551 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
552 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700553 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700554 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000555
Henrik Boström87713d02015-08-25 09:53:21 +0200556 // Obtain a certificate from RTCConfiguration if any were provided (optional).
557 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
558 if (!rtc_configuration.certificates.empty()) {
559 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
560 // just picking the first one. The decision should be made based on the DTLS
561 // handshake. The DTLS negotiations need to know about all certificates.
562 certificate = rtc_configuration.certificates[0];
563 }
564
honghaiz1f429e32015-09-28 07:57:34 -0700565 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700566
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000567 if (options.disable_encryption) {
568 dtls_enabled_ = false;
569 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200570 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200571 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800572 // |rtc_configuration| can override the default |dtls_enabled_| value.
573 if (rtc_configuration.enable_dtls_srtp) {
574 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000575 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000576 }
577
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000579 // It takes precendence over the disable_sctp_data_channels
580 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800581 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700583 }
584#ifdef HAVE_QUIC
585 else if (rtc_configuration.enable_quic) {
586 // Use QUIC instead of DTLS when |enable_quic| is true.
587 data_channel_type_ = cricket::DCT_QUIC;
588 transport_controller_->use_quic();
589 if (dtls_enabled_) {
590 LOG(LS_INFO) << "Using QUIC instead of DTLS";
591 }
592 quic_data_transport_.reset(
593 new QuicDataTransport(signaling_thread(), worker_thread(),
594 network_thread(), transport_controller_.get()));
595 }
596#endif // HAVE_QUIC
597 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000598 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000599 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000600 data_channel_type_ = cricket::DCT_SCTP;
601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
htaa2a49d92016-03-04 02:51:39 -0800604 video_options_.screencast_min_bitrate_kbps =
605 rtc_configuration.screencast_min_bitrate;
606 audio_options_.combined_audio_video_bwe =
607 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000608
kwiberg102c6a62015-10-30 02:47:38 -0700609 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100610 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200611
Karl Wibergbe579832015-11-10 22:34:18 +0100612 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
613 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200614
Henrik Boström87713d02015-08-25 09:53:21 +0200615 if (!dtls_enabled_) {
616 // Construct with DTLS disabled.
617 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200618 signaling_thread(), channel_manager_, this, id(),
619 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200620 } else {
621 // Construct with DTLS enabled.
622 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200623 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200624 signaling_thread(), channel_manager_, this, id(),
625 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200626 } else {
627 // Use the already generated certificate.
628 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200629 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200630 }
631 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632
Henrik Boströmd8281982015-08-27 10:12:24 +0200633 webrtc_session_desc_factory_->SignalCertificateReady.connect(
634 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000635
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000637 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000638 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700639
jbauch5869f502017-06-29 12:31:36 -0700640 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
641 options.crypto_options.enable_encrypted_rtp_header_extensions);
642
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 return true;
644}
645
deadbeefd59daf82015-10-14 15:02:44 -0700646void WebRtcSession::Close() {
647 SetState(STATE_CLOSED);
648 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700649 call_ = nullptr;
Steve Anton169629a2017-08-30 17:36:36 -0700650 RTC_DCHECK(voice_channels_.empty());
651 RTC_DCHECK(video_channels_.empty());
deadbeef953c2ce2017-01-09 14:53:41 -0800652 RTC_DCHECK(!rtp_data_channel_);
653 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654}
655
deadbeef0ed85b22016-02-23 17:24:52 -0800656cricket::BaseChannel* WebRtcSession::GetChannel(
657 const std::string& content_name) {
658 if (voice_channel() && voice_channel()->content_name() == content_name) {
659 return voice_channel();
660 }
661 if (video_channel() && video_channel()->content_name() == content_name) {
662 return video_channel();
663 }
deadbeef953c2ce2017-01-09 14:53:41 -0800664 if (rtp_data_channel() &&
665 rtp_data_channel()->content_name() == content_name) {
666 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800667 }
668 return nullptr;
669}
670
deadbeef953c2ce2017-01-09 14:53:41 -0800671bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
672 if (!local_description() || !remote_description()) {
673 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
674 << "SSL Role of the SCTP transport.";
675 return false;
676 }
677 if (!sctp_transport_) {
678 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
679 << "SSL Role of the SCTP transport.";
680 return false;
681 }
682
683 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
684}
685
686bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800687 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800688 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800689 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000690 << "SSL Role of the session.";
691 return false;
692 }
693
deadbeef953c2ce2017-01-09 14:53:41 -0800694 return transport_controller_->GetSslRole(GetTransportName(content_name),
695 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000696}
697
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000698void WebRtcSession::CreateOffer(
699 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700700 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
701 const cricket::MediaSessionOptions& session_options) {
702 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000703}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704
deadbeefab9b2d12015-10-14 11:33:11 -0700705void WebRtcSession::CreateAnswer(
706 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700707 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800708 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709}
710
Steve Anton8d3444d2017-10-20 15:30:51 -0700711bool WebRtcSession::SetLocalDescription(
712 std::unique_ptr<SessionDescriptionInterface> desc,
713 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800714 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700715
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000716 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700717 if (!ValidateSessionDescription(desc.get(), cricket::CS_LOCAL, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000718 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
deadbeefd59daf82015-10-14 15:02:44 -0700721 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000722 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700724 initial_offerer_ = true;
725 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 }
727
deadbeeffe4a8a42016-12-20 17:56:17 -0800728 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700729 current_local_description_ = std::move(desc);
730 pending_local_description_ = nullptr;
731 current_remote_description_ = std::move(pending_remote_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800732 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700733 pending_local_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800734 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
736 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800737 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 // TODO(mallinath) - Handle CreateChannel failure, as new local description
739 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700740 return BadLocalSdp(local_description()->type(), kCreateChannelFailed,
741 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 }
743
deadbeefcbecd352015-09-23 11:50:27 -0700744 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800745 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000747 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 return false;
749 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800750 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700751 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800752 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700753 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
deadbeef0ed85b22016-02-23 17:24:52 -0800755 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700756 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700757 return BadLocalSdp(local_description()->type(), GetSessionErrorMsg(),
758 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 return true;
761}
762
Steve Anton8d3444d2017-10-20 15:30:51 -0700763bool WebRtcSession::SetRemoteDescription(
764 std::unique_ptr<SessionDescriptionInterface> desc,
765 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800766 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700767
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000768 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700769 if (!ValidateSessionDescription(desc.get(), cricket::CS_REMOTE, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000770 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 }
772
Steve Anton8d3444d2017-10-20 15:30:51 -0700773 // Hold this pointer so candidates can be copied to it later in the method.
774 SessionDescriptionInterface* desc_ptr = desc.get();
775
deadbeeffe4a8a42016-12-20 17:56:17 -0800776 const SessionDescriptionInterface* old_remote_description =
777 remote_description();
778 // Grab ownership of the description being replaced for the remainder of this
Steve Anton8d3444d2017-10-20 15:30:51 -0700779 // method, since it's used below as |old_remote_description|.
deadbeeffe4a8a42016-12-20 17:56:17 -0800780 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
781 Action action = GetAction(desc->type());
782 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700783 replaced_remote_description = pending_remote_description_
784 ? std::move(pending_remote_description_)
785 : std::move(current_remote_description_);
786 current_remote_description_ = std::move(desc);
787 pending_remote_description_ = nullptr;
788 current_local_description_ = std::move(pending_local_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800789 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700790 replaced_remote_description = std::move(pending_remote_description_);
791 pending_remote_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800792 }
deadbeefd59daf82015-10-14 15:02:44 -0700793
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 // Transport and Media channels will be created only when offer is set.
Steve Anton8d3444d2017-10-20 15:30:51 -0700795 if (action == kOffer &&
796 !CreateChannels(remote_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 // TODO(mallinath) - Handle CreateChannel failure, as new local description
798 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700799 return BadRemoteSdp(remote_description()->type(), kCreateChannelFailed,
800 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
deadbeefcbecd352015-09-23 11:50:27 -0700803 // Remove unused channels if MediaContentDescription is rejected.
Steve Anton8d3444d2017-10-20 15:30:51 -0700804 RemoveUnusedChannels(remote_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805
806 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
807 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000808 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 return false;
810 }
811
Steve Anton8d3444d2017-10-20 15:30:51 -0700812 if (local_description() &&
813 !UseCandidatesInSessionDescription(remote_description())) {
814 return BadRemoteSdp(remote_description()->type(), kInvalidCandidates,
815 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817
deadbeeffe4a8a42016-12-20 17:56:17 -0800818 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800819 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800820 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800821 // Check if this new SessionDescription contains new ICE ufrag and
822 // password that indicates the remote peer requests an ICE restart.
823 // TODO(deadbeef): When we start storing both the current and pending
824 // remote description, this should reset pending_ice_restarts and compare
825 // against the current description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700826 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
deadbeeffe4a8a42016-12-20 17:56:17 -0800827 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800828 if (action == kOffer) {
829 pending_ice_restarts_.insert(content.name);
830 }
831 } else {
832 // We retain all received candidates only if ICE is not restarted.
833 // When ICE is restarted, all previous candidates belong to an old
834 // generation and should not be kept.
835 // TODO(deadbeef): This goes against the W3C spec which says the remote
836 // description should only contain candidates from the last set remote
837 // description plus any candidates added since then. We should remove
838 // this once we're sure it won't break anything.
839 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700840 old_remote_description, content.name, desc_ptr);
deadbeef0ed85b22016-02-23 17:24:52 -0800841 }
842 }
honghaiz503726c2015-07-31 12:37:38 -0700843 }
844
deadbeefd59daf82015-10-14 15:02:44 -0700845 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700846 return BadRemoteSdp(remote_description()->type(), GetSessionErrorMsg(),
847 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000849
850 // Set the the ICE connection state to connecting since the connection may
851 // become writable with peer reflexive candidates before any remote candidate
852 // is signaled.
853 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
854 // is to have a new signal the indicates a change in checking state from the
855 // transport and expose a new checking() member from transport that can be
856 // read to determine the current checking state. The existing SignalConnecting
857 // actually means "gathering candidates", so cannot be be used here.
Steve Anton8d3444d2017-10-20 15:30:51 -0700858 if (remote_description()->type() != SessionDescriptionInterface::kOffer &&
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000859 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
860 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 return true;
863}
864
Steve Anton18ee1d52017-09-11 11:32:35 -0700865// TODO(steveanton): Eventually it'd be nice to store the channels as a single
866// vector of BaseChannel pointers instead of separate voice and video channel
867// vectors. At that point, this will become a simple getter.
868std::vector<cricket::BaseChannel*> WebRtcSession::Channels() const {
869 std::vector<cricket::BaseChannel*> channels;
870 channels.insert(channels.end(), voice_channels_.begin(),
871 voice_channels_.end());
872 channels.insert(channels.end(), video_channels_.begin(),
873 video_channels_.end());
874 if (rtp_data_channel_) {
875 channels.push_back(rtp_data_channel_.get());
876 }
877 return channels;
878}
879
deadbeefd59daf82015-10-14 15:02:44 -0700880void WebRtcSession::LogState(State old_state, State new_state) {
881 LOG(LS_INFO) << "Session:" << id()
882 << " Old state:" << GetStateString(old_state)
883 << " New state:" << GetStateString(new_state);
884}
885
886void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800887 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700888 if (state != state_) {
889 LogState(state_, state);
890 state_ = state;
891 SignalState(this, state_);
892 }
893}
894
895void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800896 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700897 if (error != error_) {
898 error_ = error;
899 error_desc_ = error_desc;
900 }
901}
902
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903bool WebRtcSession::UpdateSessionState(
904 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800906 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700907
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 // If there's already a pending error then no state transition should happen.
909 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800910 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000911 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000913 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
914 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
deadbeefd59daf82015-10-14 15:02:44 -0700916 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
917 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000918 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700919 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000920 }
deadbeefd59daf82015-10-14 15:02:44 -0700921 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000922 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
924 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000925 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
926 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700929 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
930 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700932 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000933 }
deadbeefd59daf82015-10-14 15:02:44 -0700934 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000935 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700938 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800939 local_description()->description()->GetGroupByName(
940 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700941 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800942 remote_description()->description()->GetGroupByName(
943 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700944 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800945 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700946 const cricket::ContentGroup* answer_bundle =
947 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
948 if (!EnableBundle(*answer_bundle)) {
949 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
950 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
951 }
952 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800953 // Only push down the transport description after enabling BUNDLE; we don't
954 // want to push down a description on a transport about to be destroyed.
955 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
956 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700959 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000960 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700961 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000962 }
deadbeefd59daf82015-10-14 15:02:44 -0700963 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000964 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 }
966 }
967 return true;
968}
969
970WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
971 if (type == SessionDescriptionInterface::kOffer) {
972 return WebRtcSession::kOffer;
973 } else if (type == SessionDescriptionInterface::kPrAnswer) {
974 return WebRtcSession::kPrAnswer;
975 } else if (type == SessionDescriptionInterface::kAnswer) {
976 return WebRtcSession::kAnswer;
977 }
nisseede5da42017-01-12 05:15:36 -0800978 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 return WebRtcSession::kOffer;
980}
981
deadbeefd59daf82015-10-14 15:02:44 -0700982bool WebRtcSession::PushdownMediaDescription(
983 cricket::ContentAction action,
984 cricket::ContentSource source,
985 std::string* err) {
Steve Anton18ee1d52017-09-11 11:32:35 -0700986 const SessionDescription* sdesc =
987 (source == cricket::CS_LOCAL ? local_description() : remote_description())
988 ->description();
989 RTC_DCHECK(sdesc);
990 bool all_success = true;
991 for (auto* channel : Channels()) {
992 // TODO(steveanton): Add support for multiple channels of the same type.
993 const ContentInfo* content_info =
994 cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type());
995 if (!content_info) {
996 continue;
deadbeefd59daf82015-10-14 15:02:44 -0700997 }
Steve Anton18ee1d52017-09-11 11:32:35 -0700998 const MediaContentDescription* content_desc =
999 static_cast<const MediaContentDescription*>(content_info->description);
1000 if (content_desc && !content_info->rejected) {
1001 bool success = (source == cricket::CS_LOCAL)
1002 ? channel->SetLocalContent(content_desc, action, err)
1003 : channel->SetRemoteContent(content_desc, action, err);
1004 if (!success) {
1005 all_success = false;
1006 break;
1007 }
1008 }
1009 }
deadbeef953c2ce2017-01-09 14:53:41 -08001010 // Need complete offer/answer with an SCTP m= section before starting SCTP,
1011 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
1012 if (sctp_transport_ && local_description() && remote_description() &&
1013 cricket::GetFirstDataContent(local_description()->description()) &&
1014 cricket::GetFirstDataContent(remote_description()->description())) {
Steve Anton18ee1d52017-09-11 11:32:35 -07001015 all_success &= network_thread_->Invoke<bool>(
deadbeef953c2ce2017-01-09 14:53:41 -08001016 RTC_FROM_HERE,
1017 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
1018 }
Steve Anton18ee1d52017-09-11 11:32:35 -07001019 return all_success;
deadbeef953c2ce2017-01-09 14:53:41 -08001020}
1021
1022bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
1023 RTC_DCHECK(network_thread_->IsCurrent());
1024 RTC_DCHECK(local_description());
1025 RTC_DCHECK(remote_description());
1026 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
1027 // When we support "max-message-size", that would also be pushed down here.
1028 return sctp_transport_->Start(
1029 GetSctpPort(local_description()->description()),
1030 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -07001031}
1032
1033bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1034 cricket::ContentAction action,
1035 std::string* error_desc) {
1036 RTC_DCHECK(signaling_thread()->IsCurrent());
1037
1038 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001039 return PushdownLocalTransportDescription(local_description()->description(),
1040 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001041 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001042 return PushdownRemoteTransportDescription(remote_description()->description(),
1043 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001044}
1045
1046bool WebRtcSession::PushdownLocalTransportDescription(
1047 const SessionDescription* sdesc,
1048 cricket::ContentAction action,
1049 std::string* err) {
1050 RTC_DCHECK(signaling_thread()->IsCurrent());
1051
1052 if (!sdesc) {
1053 return false;
1054 }
1055
1056 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1057 if (!transport_controller_->SetLocalTransportDescription(
1058 tinfo.content_name, tinfo.description, action, err)) {
1059 return false;
1060 }
1061 }
1062
1063 return true;
1064}
1065
1066bool WebRtcSession::PushdownRemoteTransportDescription(
1067 const SessionDescription* sdesc,
1068 cricket::ContentAction action,
1069 std::string* err) {
1070 RTC_DCHECK(signaling_thread()->IsCurrent());
1071
1072 if (!sdesc) {
1073 return false;
1074 }
1075
1076 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1077 if (!transport_controller_->SetRemoteTransportDescription(
1078 tinfo.content_name, tinfo.description, action, err)) {
1079 return false;
1080 }
1081 }
1082
1083 return true;
1084}
1085
1086bool WebRtcSession::GetTransportDescription(
1087 const SessionDescription* description,
1088 const std::string& content_name,
1089 cricket::TransportDescription* tdesc) {
1090 if (!description || !tdesc) {
1091 return false;
1092 }
1093 const TransportInfo* transport_info =
1094 description->GetTransportInfoByName(content_name);
1095 if (!transport_info) {
1096 return false;
1097 }
1098 *tdesc = transport_info->description;
1099 return true;
1100}
1101
deadbeefcbecd352015-09-23 11:50:27 -07001102bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1103 const std::string* first_content_name = bundle.FirstContentName();
1104 if (!first_content_name) {
1105 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1106 return false;
1107 }
1108 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001109
zhihuang9763d562016-08-05 11:14:50 -07001110#ifdef HAVE_QUIC
1111 if (quic_data_transport_ &&
1112 bundle.HasContentName(quic_data_transport_->content_name()) &&
1113 quic_data_transport_->transport_name() != transport_name) {
1114 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1115 << " on " << transport_name << "with QUIC.";
1116 }
1117#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001118 auto maybe_set_transport = [this, bundle,
1119 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001120 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001121 return true;
1122 }
1123
zhihuangf5b251b2017-01-12 19:37:48 -08001124 std::string old_transport_name = ch->transport_name();
1125 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001126 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1127 << " on " << transport_name << ".";
1128 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001129 }
torbjornga81a42f2015-09-23 02:16:58 -07001130
zhihuangb2cdd932017-01-19 16:54:25 -08001131 cricket::DtlsTransportInternal* rtp_dtls_transport =
1132 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001133 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001134 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1135 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001136 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001137 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001138 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1139 }
1140
zhihuangb2cdd932017-01-19 16:54:25 -08001141 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001142 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1143 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001144 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001145 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001146 // If the channel needs rtcp, it means that the channel used to have a
1147 // rtcp transport which needs to be deleted now.
1148 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001149 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001150 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001151 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001152 return true;
1153 };
1154
deadbeefcbecd352015-09-23 11:50:27 -07001155 if (!maybe_set_transport(voice_channel()) ||
1156 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001157 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001158 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001159 }
deadbeef953c2ce2017-01-09 14:53:41 -08001160 // For SCTP, transport creation/deletion happens here instead of in the
1161 // object itself.
1162 if (sctp_transport_) {
1163 RTC_DCHECK(sctp_transport_name_);
1164 RTC_DCHECK(sctp_content_name_);
1165 if (transport_name != *sctp_transport_name_ &&
1166 bundle.HasContentName(*sctp_content_name_)) {
1167 network_thread_->Invoke<void>(
1168 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1169 transport_name));
1170 }
1171 }
deadbeefcbecd352015-09-23 11:50:27 -07001172
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001173 return true;
1174}
1175
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001177 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001178 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1179 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001180 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 }
1182
1183 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001184 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 return false;
1186 }
1187
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001188 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001189 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1190 if (!valid) {
1191 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 }
1193
1194 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001195 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001196 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return false;
1198 }
1199
deadbeefd59daf82015-10-14 15:02:44 -07001200 if (ready) {
1201 return UseCandidate(candidate);
1202 } else {
1203 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1204 return true;
1205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206}
1207
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001208bool WebRtcSession::RemoveRemoteIceCandidates(
1209 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001210 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001211 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1212 << "removed without any remote session description.";
1213 return false;
1214 }
1215
1216 if (candidates.empty()) {
1217 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1218 return false;
1219 }
1220
deadbeeffe4a8a42016-12-20 17:56:17 -08001221 size_t number_removed =
1222 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001223 if (number_removed != candidates.size()) {
1224 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1225 << "Requested " << candidates.size() << " but only "
1226 << number_removed << " are removed.";
1227 }
1228
1229 // Remove the candidates from the transport controller.
1230 std::string error;
1231 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1232 if (!res && !error.empty()) {
1233 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1234 }
1235 return true;
1236}
1237
deadbeefd59daf82015-10-14 15:02:44 -07001238cricket::IceConfig WebRtcSession::ParseIceConfig(
1239 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001240 cricket::ContinualGatheringPolicy gathering_policy;
1241 // TODO(honghaiz): Add the third continual gathering policy in
1242 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1243 switch (config.continual_gathering_policy) {
1244 case PeerConnectionInterface::GATHER_ONCE:
1245 gathering_policy = cricket::GATHER_ONCE;
1246 break;
1247 case PeerConnectionInterface::GATHER_CONTINUALLY:
1248 gathering_policy = cricket::GATHER_CONTINUALLY;
1249 break;
1250 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001251 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001252 gathering_policy = cricket::GATHER_ONCE;
1253 }
deadbeefd59daf82015-10-14 15:02:44 -07001254 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001255 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001256 ice_config.prioritize_most_likely_candidate_pairs =
1257 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001258 ice_config.backup_connection_ping_interval =
1259 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001260 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001261 ice_config.presume_writable_when_fully_relayed =
1262 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001263 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001264 ice_config.regather_all_networks_interval_range =
1265 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001266 return ice_config;
1267}
1268
1269void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1270 transport_controller_->SetIceConfig(config);
1271}
1272
1273void WebRtcSession::MaybeStartGathering() {
1274 transport_controller_->MaybeStartGathering();
1275}
1276
Peter Boström0c4e06b2015-10-07 12:23:21 +02001277bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1278 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001279 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001281 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001282 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1283 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284}
1285
Peter Boström0c4e06b2015-10-07 12:23:21 +02001286bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1287 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001288 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001289 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001290 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001291 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1292 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293}
1294
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001295std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001297 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 return desc.str();
1299}
1300
wu@webrtc.org78187522013-10-07 23:32:02 +00001301bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001302 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001303 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001304 if (!rtp_data_channel_ && !sctp_transport_) {
1305 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1306 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001307 return false;
1308 }
deadbeef953c2ce2017-01-09 14:53:41 -08001309 return rtp_data_channel_
1310 ? rtp_data_channel_->SendData(params, payload, result)
1311 : network_thread_->Invoke<bool>(
1312 RTC_FROM_HERE,
1313 Bind(&cricket::SctpTransportInternal::SendData,
1314 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001315}
1316
1317bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001318 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001319 // Don't log an error here, because DataChannels are expected to call
1320 // ConnectDataChannel in this state. It's the only way to initially tell
1321 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001322 return false;
1323 }
deadbeef953c2ce2017-01-09 14:53:41 -08001324 if (rtp_data_channel_) {
1325 rtp_data_channel_->SignalReadyToSendData.connect(
1326 webrtc_data_channel, &DataChannel::OnChannelReady);
1327 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1328 &DataChannel::OnDataReceived);
1329 } else {
1330 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1331 &DataChannel::OnChannelReady);
1332 SignalSctpDataReceived.connect(webrtc_data_channel,
1333 &DataChannel::OnDataReceived);
1334 SignalSctpStreamClosedRemotely.connect(
1335 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1336 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001337 return true;
1338}
1339
1340void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001341 if (!rtp_data_channel_ && !sctp_transport_) {
1342 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1343 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001344 return;
1345 }
deadbeef953c2ce2017-01-09 14:53:41 -08001346 if (rtp_data_channel_) {
1347 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1348 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1349 } else {
1350 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1351 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1352 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1353 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001354}
1355
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001356void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001357 if (!sctp_transport_) {
1358 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001359 return;
1360 }
deadbeef953c2ce2017-01-09 14:53:41 -08001361 network_thread_->Invoke<void>(
1362 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1363 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001364}
1365
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001366void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001367 if (!sctp_transport_) {
1368 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001369 << "NULL.";
1370 return;
1371 }
deadbeef953c2ce2017-01-09 14:53:41 -08001372 network_thread_->Invoke<void>(
1373 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1374 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001375}
1376
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001377bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001378 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1379 sctp_ready_to_send_data_;
1380}
1381
1382std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001383 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001384 ChannelNamePairs channel_name_pairs;
1385 if (voice_channel()) {
1386 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1387 voice_channel()->content_name(), voice_channel()->transport_name()));
1388 }
1389 if (video_channel()) {
1390 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1391 video_channel()->content_name(), video_channel()->transport_name()));
1392 }
1393 if (rtp_data_channel()) {
1394 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1395 ChannelNamePair(rtp_data_channel()->content_name(),
1396 rtp_data_channel()->transport_name()));
1397 }
1398 if (sctp_transport_) {
1399 RTC_DCHECK(sctp_content_name_);
1400 RTC_DCHECK(sctp_transport_name_);
1401 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1402 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1403 }
1404 return GetStats(channel_name_pairs);
1405}
1406
1407std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1408 const ChannelNamePairs& channel_name_pairs) {
1409 if (network_thread()->IsCurrent()) {
1410 return GetStats_n(channel_name_pairs);
1411 }
1412 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1413 RTC_FROM_HERE,
1414 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1415}
1416
1417bool WebRtcSession::GetLocalCertificate(
1418 const std::string& transport_name,
1419 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1420 return transport_controller_->GetLocalCertificate(transport_name,
1421 certificate);
1422}
1423
1424std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1425 const std::string& transport_name) {
1426 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001427}
1428
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429cricket::DataChannelType WebRtcSession::data_channel_type() const {
1430 return data_channel_type_;
1431}
1432
deadbeef0ed85b22016-02-23 17:24:52 -08001433bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1434 return pending_ice_restarts_.find(content_name) !=
1435 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001436}
1437
deadbeefd1a38b52016-12-10 13:15:33 -08001438void WebRtcSession::SetNeedsIceRestartFlag() {
1439 transport_controller_->SetNeedsIceRestartFlag();
1440}
1441
1442bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1443 return transport_controller_->NeedsIceRestart(content_name);
1444}
1445
Henrik Boströmd8281982015-08-27 10:12:24 +02001446void WebRtcSession::OnCertificateReady(
1447 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001448 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001449}
1450
deadbeef953c2ce2017-01-09 14:53:41 -08001451void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1452 SetError(ERROR_TRANSPORT,
1453 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1454}
1455
Henrik Boströmd8281982015-08-27 10:12:24 +02001456bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001457 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001458}
1459
deadbeefcbecd352015-09-23 11:50:27 -07001460const rtc::scoped_refptr<rtc::RTCCertificate>&
1461WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001462 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001463}
1464
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465void WebRtcSession::SetIceConnectionState(
1466 PeerConnectionInterface::IceConnectionState state) {
1467 if (ice_connection_state_ == state) {
1468 return;
1469 }
1470
deadbeefcbecd352015-09-23 11:50:27 -07001471 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1472 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001473 RTC_DCHECK(ice_connection_state_ !=
1474 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 ice_connection_state_ = state;
1476 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001477 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
1479}
1480
deadbeefcbecd352015-09-23 11:50:27 -07001481void WebRtcSession::OnTransportControllerConnectionState(
1482 cricket::IceConnectionState state) {
1483 switch (state) {
1484 case cricket::kIceConnectionConnecting:
1485 // If the current state is Connected or Completed, then there were
1486 // writable channels but now there are not, so the next state must
1487 // be Disconnected.
1488 // kIceConnectionConnecting is currently used as the default,
1489 // un-connected state by the TransportController, so its only use is
1490 // detecting disconnections.
1491 if (ice_connection_state_ ==
1492 PeerConnectionInterface::kIceConnectionConnected ||
1493 ice_connection_state_ ==
1494 PeerConnectionInterface::kIceConnectionCompleted) {
1495 SetIceConnectionState(
1496 PeerConnectionInterface::kIceConnectionDisconnected);
1497 }
torbjornga81a42f2015-09-23 02:16:58 -07001498 break;
deadbeefcbecd352015-09-23 11:50:27 -07001499 case cricket::kIceConnectionFailed:
1500 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1501 break;
1502 case cricket::kIceConnectionConnected:
1503 LOG(LS_INFO) << "Changing to ICE connected state because "
1504 << "all transports are writable.";
1505 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1506 break;
1507 case cricket::kIceConnectionCompleted:
1508 LOG(LS_INFO) << "Changing to ICE completed state because "
1509 << "all transports are complete.";
1510 if (ice_connection_state_ !=
1511 PeerConnectionInterface::kIceConnectionConnected) {
1512 // If jumping directly from "checking" to "connected",
1513 // signal "connected" first.
1514 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1515 }
1516 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1517 if (metrics_observer_) {
1518 ReportTransportStats();
1519 }
1520 break;
1521 default:
nissec80e7412017-01-11 05:56:46 -08001522 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001523 }
deadbeefcbecd352015-09-23 11:50:27 -07001524}
1525
1526void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001527 SetIceConnectionReceiving(receiving);
1528}
1529
1530void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1531 if (ice_connection_receiving_ == receiving) {
1532 return;
1533 }
1534 ice_connection_receiving_ = receiving;
1535 if (ice_observer_) {
1536 ice_observer_->OnIceConnectionReceivingChange(receiving);
1537 }
1538}
1539
deadbeefcbecd352015-09-23 11:50:27 -07001540void WebRtcSession::OnTransportControllerCandidatesGathered(
1541 const std::string& transport_name,
1542 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001543 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001544 int sdp_mline_index;
1545 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1546 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1547 << transport_name << " not found";
1548 return;
1549 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550
deadbeefcbecd352015-09-23 11:50:27 -07001551 for (cricket::Candidates::const_iterator citer = candidates.begin();
1552 citer != candidates.end(); ++citer) {
1553 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001554 std::unique_ptr<JsepIceCandidate> candidate(
1555 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001556 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001557 mutable_local_description()->AddCandidate(candidate.get());
1558 }
1559 if (ice_observer_) {
1560 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 }
1563}
1564
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001565void WebRtcSession::OnTransportControllerCandidatesRemoved(
1566 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001567 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001568 // Sanity check.
1569 for (const cricket::Candidate& candidate : candidates) {
1570 if (candidate.transport_name().empty()) {
1571 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1572 << "empty content name in candidate "
1573 << candidate.ToString();
1574 return;
1575 }
1576 }
1577
deadbeeffe4a8a42016-12-20 17:56:17 -08001578 if (local_description()) {
1579 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001580 }
1581 if (ice_observer_) {
1582 ice_observer_->OnIceCandidatesRemoved(candidates);
1583 }
1584}
1585
deadbeef953c2ce2017-01-09 14:53:41 -08001586void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1587 rtc::SSLHandshakeError error) {
1588 if (metrics_observer_) {
1589 metrics_observer_->IncrementEnumCounter(
1590 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1591 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1592 }
1593}
1594
Steve Anton169629a2017-08-30 17:36:36 -07001595// Enabling voice and video (and RTP data) channels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596void WebRtcSession::EnableChannels() {
Steve Anton169629a2017-08-30 17:36:36 -07001597 for (cricket::VoiceChannel* voice_channel : voice_channels_) {
1598 if (!voice_channel->enabled()) {
1599 voice_channel->Enable(true);
1600 }
1601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
Steve Anton169629a2017-08-30 17:36:36 -07001603 for (cricket::VideoChannel* video_channel : video_channels_) {
1604 if (!video_channel->enabled()) {
1605 video_channel->Enable(true);
1606 }
1607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
deadbeef953c2ce2017-01-09 14:53:41 -08001609 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1610 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611}
1612
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613// Returns the media index for a local ice candidate given the content name.
1614bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1615 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001616 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001618 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619
1620 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001621 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 for (size_t index = 0; index < contents.size(); ++index) {
1623 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001624 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 content_found = true;
1626 break;
1627 }
1628 }
1629 return content_found;
1630}
1631
1632bool WebRtcSession::UseCandidatesInSessionDescription(
1633 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001634 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001638
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1640 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001641 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001642 const IceCandidateInterface* candidate = candidates->at(n);
1643 bool valid = false;
1644 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1645 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001646 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1647 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001648 }
1649 continue;
1650 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001651 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001652 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 break;
deadbeefd59daf82015-10-14 15:02:44 -07001654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 }
1656 }
1657 return ret;
1658}
1659
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001660bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001662 size_t remote_content_size =
1663 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001665 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 return false;
1667 }
1668
1669 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001670 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 std::vector<cricket::Candidate> candidates;
1672 candidates.push_back(candidate->candidate());
1673 // Invoking BaseSession method to handle remote candidates.
1674 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001675 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1676 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 // Candidates successfully submitted for checking.
1678 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1679 ice_connection_state_ ==
1680 PeerConnectionInterface::kIceConnectionDisconnected) {
1681 // If state is New, then the session has just gotten its first remote ICE
1682 // candidates, so go to Checking.
1683 // If state is Disconnected, the session is re-using old candidates or
1684 // receiving additional ones, so go to Checking.
1685 // If state is Connected, stay Connected.
1686 // TODO(bemasc): If state is Connected, and the new candidates are for a
1687 // newly added transport, then the state actually _should_ move to
1688 // checking. Add a way to distinguish that case.
1689 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1690 }
1691 // TODO(bemasc): If state is Completed, go back to Connected.
1692 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001693 if (!error.empty()) {
1694 LOG(LS_WARNING) << error;
1695 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 }
1697 return true;
1698}
1699
deadbeefcbecd352015-09-23 11:50:27 -07001700void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001701 // TODO(steveanton): Add support for multiple audio/video channels.
1702 // Destroy video channel first since it may have a pointer to the
1703 // voice channel.
1704 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
1705 if ((!video_info || video_info->rejected) && video_channel()) {
1706 RemoveAndDestroyVideoChannel(video_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 }
1708
Steve Anton169629a2017-08-30 17:36:36 -07001709 const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc);
1710 if ((!voice_info || voice_info->rejected) && voice_channel()) {
1711 RemoveAndDestroyVoiceChannel(voice_channel());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001712 }
1713
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 const cricket::ContentInfo* data_info =
1715 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001716 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001717 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001718 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001719 }
1720 if (sctp_transport_) {
1721 SignalDataChannelDestroyed();
1722 network_thread_->Invoke<void>(
1723 RTC_FROM_HERE,
1724 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001725 }
1726#ifdef HAVE_QUIC
1727 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1728 if (quic_data_transport_) {
1729 quic_data_transport_.reset();
1730 }
1731#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 }
1733}
1734
skvlad6c87a672016-05-17 17:49:52 -07001735// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1736// if the channel is not part of any bundle.
1737const std::string* WebRtcSession::GetBundleTransportName(
1738 const cricket::ContentInfo* content,
1739 const cricket::ContentGroup* bundle) {
1740 if (!bundle) {
1741 return nullptr;
1742 }
1743 const std::string* first_content_name = bundle->FirstContentName();
1744 if (!first_content_name) {
1745 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1746 return nullptr;
1747 }
1748 if (!bundle->HasContentName(content->name)) {
1749 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1750 return nullptr;
1751 }
1752 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1753 return first_content_name;
1754}
1755
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001757 // TODO(steveanton): Add support for multiple audio/video channels.
skvlad6c87a672016-05-17 17:49:52 -07001758 const cricket::ContentGroup* bundle_group = nullptr;
1759 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1760 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1761 if (!bundle_group) {
1762 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1763 return false;
1764 }
1765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 // Creating the media channels and transport proxies.
1767 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001768 if (voice && !voice->rejected && !voice_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001769 if (!CreateVoiceChannel(voice,
1770 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 LOG(LS_ERROR) << "Failed to create voice channel.";
1772 return false;
1773 }
1774 }
1775
1776 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001777 if (video && !video->rejected && !video_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001778 if (!CreateVideoChannel(video,
1779 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 LOG(LS_ERROR) << "Failed to create video channel.";
1781 return false;
1782 }
1783 }
1784
1785 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001786 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1787 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001788 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 LOG(LS_ERROR) << "Failed to create data channel.";
1790 return false;
1791 }
1792 }
1793
1794 return true;
1795}
1796
skvlad6c87a672016-05-17 17:49:52 -07001797bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1798 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001799 // TODO(steveanton): Check to see if it's safe to create multiple voice
1800 // channels.
1801 RTC_DCHECK(voice_channels_.empty());
1802
skvlad6c87a672016-05-17 17:49:52 -07001803 bool require_rtcp_mux =
1804 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001805
1806 std::string transport_name =
1807 bundle_transport ? *bundle_transport : content->name;
1808
zhihuangb2cdd932017-01-19 16:54:25 -08001809 cricket::DtlsTransportInternal* rtp_dtls_transport =
1810 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001811 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001812 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001813 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001814 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001815 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1816 }
1817
Steve Anton169629a2017-08-30 17:36:36 -07001818 cricket::VoiceChannel* voice_channel = channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001819 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001820 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001821 audio_options_);
1822 if (!voice_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001823 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001824 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001825 if (rtcp_dtls_transport) {
1826 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001827 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001828 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001829 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001830 }
zhihuangf5b251b2017-01-12 19:37:48 -08001831
Steve Anton169629a2017-08-30 17:36:36 -07001832 voice_channels_.push_back(voice_channel);
1833
1834 voice_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001835 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001836 voice_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001837 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001838
Steve Anton169629a2017-08-30 17:36:36 -07001839 // TODO(steveanton): This should signal which voice channel was created since
1840 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001841 SignalVoiceChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001842 voice_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001843 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844}
1845
skvlad6c87a672016-05-17 17:49:52 -07001846bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1847 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001848 // TODO(steveanton): Check to see if it's safe to create multiple video
1849 // channels.
1850 RTC_DCHECK(video_channels_.empty());
1851
skvlad6c87a672016-05-17 17:49:52 -07001852 bool require_rtcp_mux =
1853 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001854
1855 std::string transport_name =
1856 bundle_transport ? *bundle_transport : content->name;
1857
zhihuangb2cdd932017-01-19 16:54:25 -08001858 cricket::DtlsTransportInternal* rtp_dtls_transport =
1859 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001860 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001861 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001862 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001863 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001864 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1865 }
1866
Steve Anton169629a2017-08-30 17:36:36 -07001867 cricket::VideoChannel* video_channel = channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001868 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001869 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001870 video_options_);
zhihuangf5b251b2017-01-12 19:37:48 -08001871
Steve Anton169629a2017-08-30 17:36:36 -07001872 if (!video_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001873 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001874 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001875 if (rtcp_dtls_transport) {
1876 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001877 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001878 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001879 return false;
1880 }
zhihuangf5b251b2017-01-12 19:37:48 -08001881
Steve Anton169629a2017-08-30 17:36:36 -07001882 video_channels_.push_back(video_channel);
1883
1884 video_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001885 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001886 video_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001887 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001888
Steve Anton169629a2017-08-30 17:36:36 -07001889 // TODO(steveanton): This should signal which video channel was created since
1890 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001891 SignalVideoChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001892 video_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001893 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894}
1895
skvlad6c87a672016-05-17 17:49:52 -07001896bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1897 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001898 const std::string transport_name =
1899 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001900#ifdef HAVE_QUIC
1901 if (data_channel_type_ == cricket::DCT_QUIC) {
1902 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001903 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001904 return true;
1905 }
1906#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001907 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001908 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001909 if (!sctp_factory_) {
1910 LOG(LS_ERROR)
1911 << "Trying to create SCTP transport, but didn't compile with "
1912 "SCTP support (HAVE_SCTP)";
1913 return false;
1914 }
1915 if (!network_thread_->Invoke<bool>(
1916 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1917 this, content->name, transport_name))) {
1918 return false;
1919 };
1920 } else {
1921 bool require_rtcp_mux =
1922 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001923
1924 std::string transport_name =
1925 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001926 cricket::DtlsTransportInternal* rtp_dtls_transport =
1927 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001928 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001929 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001930 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001931 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001932 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1933 }
1934
deadbeef953c2ce2017-01-09 14:53:41 -08001935 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001936 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001937 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001938 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001939
deadbeef953c2ce2017-01-09 14:53:41 -08001940 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001941 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001942 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001943 if (rtcp_dtls_transport) {
1944 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001945 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001946 }
deadbeef953c2ce2017-01-09 14:53:41 -08001947 return false;
1948 }
zhihuangf5b251b2017-01-12 19:37:48 -08001949
deadbeefac22f702017-01-12 21:59:29 -08001950 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1951 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001952 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1953 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1954 rtp_data_channel_->SignalSentPacket.connect(this,
1955 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001956 }
1957
deadbeefab9b2d12015-10-14 11:33:11 -07001958 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001959
wu@webrtc.org91053e72013-08-10 07:18:04 +00001960 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961}
1962
stefanf79ade12017-06-02 06:44:03 -07001963Call::Stats WebRtcSession::GetCallStats() {
1964 if (!worker_thread()->IsCurrent()) {
1965 return worker_thread()->Invoke<Call::Stats>(
1966 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1967 }
zhihuang38ede132017-06-15 12:52:32 -07001968 if (!call_)
1969 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001970 return call_->GetStats();
1971}
1972
hbosdf6075a2016-12-19 04:58:02 -08001973std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1974 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001975 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001976 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1977 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1978 &channel_name_pairs.video,
1979 &channel_name_pairs.data }) {
1980 if (*channel_name_pair) {
1981 cricket::TransportStats transport_stats;
1982 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1983 &transport_stats)) {
1984 return nullptr;
1985 }
1986 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1987 (*channel_name_pair)->transport_name;
1988 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1989 std::move(transport_stats);
1990 }
1991 }
1992 return session_stats;
1993}
1994
deadbeef953c2ce2017-01-09 14:53:41 -08001995bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1996 const std::string& transport_name) {
1997 RTC_DCHECK(network_thread_->IsCurrent());
1998 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001999 cricket::DtlsTransportInternal* tc =
2000 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002001 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2002 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
2003 RTC_DCHECK(sctp_transport_);
2004 sctp_invoker_.reset(new rtc::AsyncInvoker());
2005 sctp_transport_->SignalReadyToSendData.connect(
2006 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
2007 sctp_transport_->SignalDataReceived.connect(
2008 this, &WebRtcSession::OnSctpTransportDataReceived_n);
2009 sctp_transport_->SignalStreamClosedRemotely.connect(
2010 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
2011 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
2012 sctp_content_name_ = rtc::Optional<std::string>(content_name);
2013 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014}
2015
deadbeef953c2ce2017-01-09 14:53:41 -08002016void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
2017 RTC_DCHECK(network_thread_->IsCurrent());
2018 RTC_DCHECK(sctp_transport_);
2019 RTC_DCHECK(sctp_transport_name_);
2020 std::string old_sctp_transport_name = *sctp_transport_name_;
2021 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08002022 cricket::DtlsTransportInternal* tc =
2023 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002024 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2025 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08002026 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002027 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2028}
2029
2030void WebRtcSession::DestroySctpTransport_n() {
2031 RTC_DCHECK(network_thread_->IsCurrent());
2032 sctp_transport_.reset(nullptr);
2033 sctp_content_name_.reset();
2034 sctp_transport_name_.reset();
2035 sctp_invoker_.reset(nullptr);
2036 sctp_ready_to_send_data_ = false;
2037}
2038
2039void WebRtcSession::OnSctpTransportReadyToSendData_n() {
2040 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2041 RTC_DCHECK(network_thread_->IsCurrent());
2042 sctp_invoker_->AsyncInvoke<void>(
2043 RTC_FROM_HERE, signaling_thread_,
2044 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
2045}
2046
2047void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
2048 RTC_DCHECK(signaling_thread_->IsCurrent());
2049 sctp_ready_to_send_data_ = ready;
2050 SignalSctpReadyToSendData(ready);
2051}
2052
2053void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002054 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002055 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07002056 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08002057 RTC_DCHECK(network_thread_->IsCurrent());
2058 sctp_invoker_->AsyncInvoke<void>(
2059 RTC_FROM_HERE, signaling_thread_,
2060 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2061 payload));
2062}
2063
2064void WebRtcSession::OnSctpTransportDataReceived_s(
2065 const cricket::ReceiveDataParams& params,
2066 const rtc::CopyOnWriteBuffer& payload) {
2067 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002068 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2069 // Received OPEN message; parse and signal that a new data channel should
2070 // be created.
2071 std::string label;
2072 InternalDataChannelInit config;
2073 config.id = params.ssrc;
2074 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2075 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2076 << params.ssrc;
2077 return;
2078 }
2079 config.open_handshake_role = InternalDataChannelInit::kAcker;
2080 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002081 } else {
2082 // Otherwise just forward the signal.
2083 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 }
deadbeef953c2ce2017-01-09 14:53:41 -08002085}
2086
2087void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2088 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2089 RTC_DCHECK(network_thread_->IsCurrent());
2090 sctp_invoker_->AsyncInvoke<void>(
2091 RTC_FROM_HERE, signaling_thread_,
2092 rtc::Bind(&sigslot::signal1<int>::operator(),
2093 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094}
2095
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002096// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002097bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002098 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2099 if (!bundle_enabled)
2100 return true;
2101
2102 const cricket::ContentGroup* bundle_group =
2103 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002104 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002105
2106 const cricket::ContentInfos& contents = desc->contents();
2107 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2108 citer != contents.end(); ++citer) {
2109 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002110 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002111 if (bundle_group->HasContentName(content->name) &&
2112 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2113 if (!HasRtcpMuxEnabled(content))
2114 return false;
2115 }
2116 }
2117 // RTCP-MUX is enabled in all the contents.
2118 return true;
2119}
2120
2121bool WebRtcSession::HasRtcpMuxEnabled(
2122 const cricket::ContentInfo* content) {
2123 const cricket::MediaContentDescription* description =
2124 static_cast<cricket::MediaContentDescription*>(content->description);
2125 return description->rtcp_mux();
2126}
2127
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002128bool WebRtcSession::ValidateSessionDescription(
2129 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002130 cricket::ContentSource source, std::string* err_desc) {
2131 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002132 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002133 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002134 }
2135
2136 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002137 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002138 }
2139
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002140 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002141 Action action = GetAction(sdesc->type());
2142 if (source == cricket::CS_LOCAL) {
2143 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002144 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002145 } else {
2146 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002147 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002148 }
2149
2150 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002151 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002152 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2153 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002154 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002155 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002156 }
2157
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002158 // Verify ice-ufrag and ice-pwd.
2159 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002160 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002161 }
2162
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002163 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002164 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002165 }
2166
skvlad6c87a672016-05-17 17:49:52 -07002167 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2168 // m-lines that do not rtcp-mux enabled.
2169
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002170 // Verify m-lines in Answer when compared against Offer.
Zhi Huang2a5e4262017-09-14 01:15:03 -07002171 if (action == kAnswer || action == kPrAnswer) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002172 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002173 (source == cricket::CS_LOCAL) ? remote_description()->description()
2174 : local_description()->description();
Tommi589ae452017-10-15 21:20:46 +00002175 if (!MediaSectionsHaveSameCount(sdesc->description(), offer_desc) ||
2176 !MediaSectionsInSameOrder(sdesc->description(), offer_desc)) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002177 return BadAnswerSdp(source, kMlineMismatchInAnswer, err_desc);
2178 }
2179 } else {
Tommi589ae452017-10-15 21:20:46 +00002180 // The re-offers should respect the order of m= sections in current local
Zhi Huang2a5e4262017-09-14 01:15:03 -07002181 // description. See RFC3264 Section 8 paragraph 4 for more details.
Tommi589ae452017-10-15 21:20:46 +00002182 if (local_description() &&
2183 !MediaSectionsInSameOrder(sdesc->description(),
2184 local_description()->description())) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002185 return BadOfferSdp(source, kMlineMismatchInSubsequentOffer, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002186 }
2187 }
2188
2189 return true;
2190}
2191
2192bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2193 return ((action == kOffer && state() == STATE_INIT) ||
2194 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002195 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002196 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002197 (action == kOffer && state() == STATE_INPROGRESS) ||
2198 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002199 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2200 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2201 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2202 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002203}
2204
2205bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2206 return ((action == kOffer && state() == STATE_INIT) ||
2207 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002208 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002209 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002210 (action == kOffer && state() == STATE_INPROGRESS) ||
2211 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002212 (action == kAnswer && state() == STATE_SENTOFFER) ||
2213 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2214 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2215 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002216}
2217
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002218std::string WebRtcSession::GetSessionErrorMsg() {
2219 std::ostringstream desc;
2220 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2221 desc << kSessionErrorDesc << error_desc() << ".";
2222 return desc.str();
2223}
2224
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002225// We need to check the local/remote description for the Transport instead of
2226// the session, because a new Transport added during renegotiation may have
2227// them unset while the session has them set from the previous negotiation.
2228// Not doing so may trigger the auto generation of transport description and
2229// mess up DTLS identity information, ICE credential, etc.
2230bool WebRtcSession::ReadyToUseRemoteCandidate(
2231 const IceCandidateInterface* candidate,
2232 const SessionDescriptionInterface* remote_desc,
2233 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002234 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002235
2236 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002237 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002238
deadbeefd59daf82015-10-14 15:02:44 -07002239 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002240 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002241 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002242
2243 size_t mediacontent_index =
2244 static_cast<size_t>(candidate->sdp_mline_index());
2245 size_t remote_content_size =
2246 current_remote_desc->description()->contents().size();
2247 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002248 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2249 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002250
2251 *valid = false;
2252 return false;
2253 }
2254
2255 cricket::ContentInfo content =
2256 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002257
2258 const std::string transport_name = GetTransportName(content.name);
2259 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002260 return false;
2261 }
zhihuang9763d562016-08-05 11:14:50 -07002262 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002263}
2264
deadbeef7af91dd2016-12-13 11:29:11 -08002265bool WebRtcSession::SrtpRequired() const {
2266 return dtls_enabled_ ||
2267 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2268}
2269
deadbeefcbecd352015-09-23 11:50:27 -07002270void WebRtcSession::OnTransportControllerGatheringState(
2271 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002272 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002273 if (state == cricket::kIceGatheringGathering) {
2274 if (ice_observer_) {
2275 ice_observer_->OnIceGatheringChange(
2276 PeerConnectionInterface::kIceGatheringGathering);
2277 }
2278 } else if (state == cricket::kIceGatheringComplete) {
2279 if (ice_observer_) {
2280 ice_observer_->OnIceGatheringChange(
2281 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002282 }
2283 }
2284}
2285
2286void WebRtcSession::ReportTransportStats() {
2287 // Use a set so we don't report the same stats twice if two channels share
2288 // a transport.
2289 std::set<std::string> transport_names;
2290 if (voice_channel()) {
2291 transport_names.insert(voice_channel()->transport_name());
2292 }
2293 if (video_channel()) {
2294 transport_names.insert(video_channel()->transport_name());
2295 }
deadbeef953c2ce2017-01-09 14:53:41 -08002296 if (rtp_data_channel()) {
2297 transport_names.insert(rtp_data_channel()->transport_name());
2298 }
2299 if (sctp_transport_name_) {
2300 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002301 }
2302 for (const auto& name : transport_names) {
2303 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002304 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002305 ReportBestConnectionState(stats);
2306 ReportNegotiatedCiphers(stats);
2307 }
2308 }
2309}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002310// Walk through the ConnectionInfos to gather best connection usage
2311// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002312void WebRtcSession::ReportBestConnectionState(
2313 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002314 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002315 for (cricket::TransportChannelStatsList::const_iterator it =
2316 stats.channel_stats.begin();
2317 it != stats.channel_stats.end(); ++it) {
2318 for (cricket::ConnectionInfos::const_iterator it_info =
2319 it->connection_infos.begin();
2320 it_info != it->connection_infos.end(); ++it_info) {
2321 if (!it_info->best_connection) {
2322 continue;
2323 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002324
2325 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2326 const cricket::Candidate& local = it_info->local_candidate;
2327 const cricket::Candidate& remote = it_info->remote_candidate;
2328
2329 // Increment the counter for IceCandidatePairType.
2330 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2331 (local.type() == RELAY_PORT_TYPE &&
2332 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2333 type = kEnumCounterIceCandidatePairTypeTcp;
2334 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2335 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002336 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002337 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002338 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002339 metrics_observer_->IncrementEnumCounter(
2340 type, GetIceCandidatePairCounter(local, remote),
2341 kIceCandidatePairMax);
2342
2343 // Increment the counter for IP type.
2344 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002345 metrics_observer_->IncrementEnumCounter(
2346 kEnumCounterAddressFamily, kBestConnections_IPv4,
2347 kPeerConnectionAddressFamilyCounter_Max);
2348
2349 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002350 metrics_observer_->IncrementEnumCounter(
2351 kEnumCounterAddressFamily, kBestConnections_IPv6,
2352 kPeerConnectionAddressFamilyCounter_Max);
2353 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002354 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002355 }
2356
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002357 return;
2358 }
2359 }
2360}
2361
jbauchac8869e2015-07-03 01:36:14 -07002362void WebRtcSession::ReportNegotiatedCiphers(
2363 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002364 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002365 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2366 return;
2367 }
2368
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002369 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2370 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2371 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2372 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002373 return;
2374 }
2375
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002376 PeerConnectionEnumCounterType srtp_counter_type;
2377 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002378 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002379 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2380 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002381 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002382 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2383 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002384 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002385 srtp_counter_type = kEnumCounterDataSrtpCipher;
2386 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002387 } else {
2388 RTC_NOTREACHED();
2389 return;
2390 }
2391
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002392 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2393 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2394 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002395 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002396 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2397 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2398 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002399 }
2400}
2401
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002402void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002403 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002404 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002405 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002406}
2407
zhihuang9763d562016-08-05 11:14:50 -07002408const std::string WebRtcSession::GetTransportName(
2409 const std::string& content_name) {
2410 cricket::BaseChannel* channel = GetChannel(content_name);
2411 if (!channel) {
2412#ifdef HAVE_QUIC
2413 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2414 content_name == quic_data_transport_->transport_name()) {
2415 return quic_data_transport_->transport_name();
2416 }
2417#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002418 if (sctp_transport_) {
2419 RTC_DCHECK(sctp_content_name_);
2420 RTC_DCHECK(sctp_transport_name_);
2421 if (content_name == *sctp_content_name_) {
2422 return *sctp_transport_name_;
2423 }
2424 }
zhihuang9763d562016-08-05 11:14:50 -07002425 // Return an empty string if failed to retrieve the transport name.
2426 return "";
2427 }
2428 return channel->transport_name();
2429}
zhihuangd82eee02016-08-26 11:25:05 -07002430
deadbeefac22f702017-01-12 21:59:29 -08002431void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002432 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002433 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002434 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2435}
2436
Steve Anton169629a2017-08-30 17:36:36 -07002437void WebRtcSession::RemoveAndDestroyVideoChannel(
2438 cricket::VideoChannel* video_channel) {
2439 auto it =
2440 std::find(video_channels_.begin(), video_channels_.end(), video_channel);
2441 RTC_DCHECK(it != video_channels_.end());
2442 if (it == video_channels_.end()) {
2443 return;
2444 }
2445 video_channels_.erase(it);
2446 DestroyVideoChannel(video_channel);
2447}
2448
2449void WebRtcSession::DestroyVideoChannel(cricket::VideoChannel* video_channel) {
2450 // TODO(steveanton): This should take an identifier for the video channel
2451 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002452 SignalVideoChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002453 RTC_DCHECK(video_channel->rtp_dtls_transport());
2454 const std::string transport_name =
2455 video_channel->rtp_dtls_transport()->transport_name();
2456 const bool need_to_delete_rtcp =
2457 (video_channel->rtcp_dtls_transport() != nullptr);
2458 // The above need to be cached before destroying the video channel so that we
2459 // do not access uninitialized memory.
2460 channel_manager_->DestroyVideoChannel(video_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002461 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002462 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002463 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002464 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002465 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002466 }
2467}
2468
Steve Anton169629a2017-08-30 17:36:36 -07002469void WebRtcSession::RemoveAndDestroyVoiceChannel(
2470 cricket::VoiceChannel* voice_channel) {
2471 auto it =
2472 std::find(voice_channels_.begin(), voice_channels_.end(), voice_channel);
2473 RTC_DCHECK(it != voice_channels_.end());
2474 if (it == voice_channels_.end()) {
2475 return;
2476 }
2477 voice_channels_.erase(it);
2478 DestroyVoiceChannel(voice_channel);
2479}
2480
2481void WebRtcSession::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) {
2482 // TODO(steveanton): This should take an identifier for the voice channel
2483 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002484 SignalVoiceChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002485 RTC_DCHECK(voice_channel->rtp_dtls_transport());
2486 const std::string transport_name =
2487 voice_channel->rtp_dtls_transport()->transport_name();
2488 const bool need_to_delete_rtcp =
2489 (voice_channel->rtcp_dtls_transport() != nullptr);
2490 // The above need to be cached before destroying the video channel so that we
2491 // do not access uninitialized memory.
2492 channel_manager_->DestroyVoiceChannel(voice_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002493 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002494 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002495 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002496 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002497 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002498 }
2499}
2500
2501void WebRtcSession::DestroyDataChannel() {
2502 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002503 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002504 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002505 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2506 bool need_to_delete_rtcp =
2507 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002508 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002509 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002510 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002511 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002512 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002513 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002514 }
2515}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516} // namespace webrtc