blob: b4b30a0813b367b7d863cc249e3f523425929423 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/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
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_sink.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/peerconnectioninterface.h"
ossuf515ab82016-12-07 04:52:58 -080024#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/videocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080027#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000028#include "webrtc/p2p/base/portallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010029#include "webrtc/pc/channel.h"
30#include "webrtc/pc/channelmanager.h"
31#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080032#include "webrtc/pc/sctputils.h"
33#include "webrtc/pc/webrtcsessiondescriptionfactory.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020034#include "webrtc/rtc_base/basictypes.h"
35#include "webrtc/rtc_base/bind.h"
36#include "webrtc/rtc_base/checks.h"
37#include "webrtc/rtc_base/helpers.h"
38#include "webrtc/rtc_base/logging.h"
39#include "webrtc/rtc_base/stringencode.h"
40#include "webrtc/rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
zhihuang9763d562016-08-05 11:14:50 -070042#ifdef HAVE_QUIC
43#include "webrtc/p2p/quic/quictransportchannel.h"
44#endif // HAVE_QUIC
45
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046using cricket::ContentInfo;
47using cricket::ContentInfos;
48using cricket::MediaContentDescription;
49using cricket::SessionDescription;
50using cricket::TransportInfo;
51
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070052using cricket::LOCAL_PORT_TYPE;
53using cricket::STUN_PORT_TYPE;
54using cricket::RELAY_PORT_TYPE;
55using cricket::PRFLX_PORT_TYPE;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace webrtc {
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060const char kBundleWithoutRtcpMux[] = "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.";
65const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
67const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080077const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000078 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080079const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000080 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070081const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070083IceCandidatePairType GetIceCandidatePairCounter(
84 const cricket::Candidate& local,
85 const cricket::Candidate& remote) {
86 const auto& l = local.type();
87 const auto& r = remote.type();
88 const auto& host = LOCAL_PORT_TYPE;
89 const auto& srflx = STUN_PORT_TYPE;
90 const auto& relay = RELAY_PORT_TYPE;
91 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070092 if (l == host && r == host) {
93 bool local_private = IPIsPrivate(local.address().ipaddr());
94 bool remote_private = IPIsPrivate(remote.address().ipaddr());
95 if (local_private) {
96 if (remote_private) {
97 return kIceCandidatePairHostPrivateHostPrivate;
98 } else {
99 return kIceCandidatePairHostPrivateHostPublic;
100 }
101 } else {
102 if (remote_private) {
103 return kIceCandidatePairHostPublicHostPrivate;
104 } else {
105 return kIceCandidatePairHostPublicHostPublic;
106 }
107 }
108 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700109 if (l == host && r == srflx)
110 return kIceCandidatePairHostSrflx;
111 if (l == host && r == relay)
112 return kIceCandidatePairHostRelay;
113 if (l == host && r == prflx)
114 return kIceCandidatePairHostPrflx;
115 if (l == srflx && r == host)
116 return kIceCandidatePairSrflxHost;
117 if (l == srflx && r == srflx)
118 return kIceCandidatePairSrflxSrflx;
119 if (l == srflx && r == relay)
120 return kIceCandidatePairSrflxRelay;
121 if (l == srflx && r == prflx)
122 return kIceCandidatePairSrflxPrflx;
123 if (l == relay && r == host)
124 return kIceCandidatePairRelayHost;
125 if (l == relay && r == srflx)
126 return kIceCandidatePairRelaySrflx;
127 if (l == relay && r == relay)
128 return kIceCandidatePairRelayRelay;
129 if (l == relay && r == prflx)
130 return kIceCandidatePairRelayPrflx;
131 if (l == prflx && r == host)
132 return kIceCandidatePairPrflxHost;
133 if (l == prflx && r == srflx)
134 return kIceCandidatePairPrflxSrflx;
135 if (l == prflx && r == relay)
136 return kIceCandidatePairPrflxRelay;
137 return kIceCandidatePairMax;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140// Compares |answer| against |offer|. Comparision is done
141// for number of m-lines in answer against offer. If matches true will be
142// returned otherwise false.
143static bool VerifyMediaDescriptions(
144 const SessionDescription* answer, const SessionDescription* offer) {
145 if (offer->contents().size() != answer->contents().size())
146 return false;
147
148 for (size_t i = 0; i < offer->contents().size(); ++i) {
149 if ((offer->contents()[i].name) != answer->contents()[i].name) {
150 return false;
151 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000152 const MediaContentDescription* offer_mdesc =
153 static_cast<const MediaContentDescription*>(
154 offer->contents()[i].description);
155 const MediaContentDescription* answer_mdesc =
156 static_cast<const MediaContentDescription*>(
157 answer->contents()[i].description);
158 if (offer_mdesc->type() != answer_mdesc->type()) {
159 return false;
160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162 return true;
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800166// fingerprint, unless it's in a BUNDLE group, in which case only the
167// BUNDLE-tag section (first media section/description in the BUNDLE group)
168// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
169// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
170// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000171static bool VerifyCrypto(const SessionDescription* desc,
172 bool dtls_enabled,
173 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800174 const cricket::ContentGroup* bundle =
175 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 const ContentInfos& contents = desc->contents();
177 for (size_t index = 0; index < contents.size(); ++index) {
178 const ContentInfo* cinfo = &contents[index];
179 if (cinfo->rejected) {
180 continue;
181 }
deadbeefb7892532017-02-22 19:35:18 -0800182 if (bundle && bundle->HasContentName(cinfo->name) &&
183 cinfo->name != *(bundle->FirstContentName())) {
184 // This isn't the first media section in the BUNDLE group, so it's not
185 // required to have crypto attributes, since only the crypto attributes
186 // from the first section actually get used.
187 continue;
188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
deadbeefb7892532017-02-22 19:35:18 -0800190 // If the content isn't rejected or bundled into another m= section, crypto
191 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 const MediaContentDescription* media =
193 static_cast<const MediaContentDescription*>(cinfo->description);
194 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
195 if (!media || !tinfo) {
196 // Something is not right.
197 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return false;
200 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000203 LOG(LS_WARNING) <<
204 "Session description must have DTLS fingerprint if DTLS enabled.";
205 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000206 return false;
207 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000208 } else {
209 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 LOG(LS_WARNING) <<
211 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000212 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 return false;
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 }
216 }
217
218 return true;
219}
220
deadbeefb7892532017-02-22 19:35:18 -0800221// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
222// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
223// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000224static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800225 const cricket::ContentGroup* bundle =
226 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000227 const ContentInfos& contents = desc->contents();
228 for (size_t index = 0; index < contents.size(); ++index) {
229 const ContentInfo* cinfo = &contents[index];
230 if (cinfo->rejected) {
231 continue;
232 }
deadbeefb7892532017-02-22 19:35:18 -0800233 if (bundle && bundle->HasContentName(cinfo->name) &&
234 cinfo->name != *(bundle->FirstContentName())) {
235 // This isn't the first media section in the BUNDLE group, so it's not
236 // required to have ufrag/password, since only the ufrag/password from
237 // the first section actually get used.
238 continue;
239 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000240
deadbeefb7892532017-02-22 19:35:18 -0800241 // If the content isn't rejected or bundled into another m= section,
242 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000243 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
244 if (!tinfo) {
245 // Something is not right.
246 LOG(LS_ERROR) << kInvalidSdp;
247 return false;
248 }
249 if (tinfo->description.ice_ufrag.empty() ||
250 tinfo->description.ice_pwd.empty()) {
251 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
252 return false;
253 }
254 }
255 return true;
256}
257
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 uint32_t ssrc,
260 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800261 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 const cricket::ContentInfo* audio_info =
264 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000265 if (audio_info) {
266 const cricket::MediaContentDescription* audio_content =
267 static_cast<const cricket::MediaContentDescription*>(
268 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000270 const auto* found =
271 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
272 if (found) {
273 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000274 return true;
275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 const cricket::ContentInfo* video_info =
279 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000280 if (video_info) {
281 const cricket::MediaContentDescription* video_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000285 const auto* found =
286 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
287 if (found) {
288 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000289 return true;
290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292 return false;
293}
294
deadbeef953c2ce2017-01-09 14:53:41 -0800295// Get the SCTP port out of a SessionDescription.
296// Return -1 if not found.
297static int GetSctpPort(const SessionDescription* session_description) {
298 const ContentInfo* content_info = GetFirstDataContent(session_description);
299 RTC_DCHECK(content_info);
300 if (!content_info) {
301 return -1;
302 }
303 const cricket::DataContentDescription* data =
304 static_cast<const cricket::DataContentDescription*>(
305 (content_info->description));
306 std::string value;
307 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
308 cricket::kGoogleSctpDataCodecName);
309 for (const cricket::DataCodec& codec : data->codecs()) {
310 if (!codec.Matches(match_pattern)) {
311 continue;
312 }
313 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
314 return rtc::FromString<int>(value);
315 }
316 }
317 return -1;
318}
319
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320static bool BadSdp(const std::string& source,
321 const std::string& type,
322 const std::string& reason,
323 std::string* err_desc) {
324 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700325 desc << "Failed to set " << source;
326 if (!type.empty()) {
327 desc << " " << type;
328 }
329 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000334 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 return false;
336}
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 const std::string& type,
340 const std::string& reason,
341 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 }
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349static bool BadLocalSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
353}
354
355static bool BadRemoteSdp(const std::string& type,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
359}
360
361static bool BadOfferSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
365}
366
367static bool BadPranswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
371 reason, err_desc);
372}
373
374static bool BadAnswerSdp(cricket::ContentSource source,
375 const std::string& reason,
376 std::string* err_desc) {
377 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378}
379
deadbeefd59daf82015-10-14 15:02:44 -0700380#define GET_STRING_OF_STATE(state) \
381 case webrtc::WebRtcSession::state: \
382 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 break;
384
deadbeefd59daf82015-10-14 15:02:44 -0700385static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 std::string result;
387 switch (state) {
388 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700389 GET_STRING_OF_STATE(STATE_SENTOFFER)
390 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
391 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
392 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700394 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 default:
nissec80e7412017-01-11 05:56:46 -0800396 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 break;
398 }
399 return result;
400}
401
deadbeefd59daf82015-10-14 15:02:44 -0700402#define GET_STRING_OF_ERROR_CODE(err) \
403 case webrtc::WebRtcSession::err: \
404 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 break;
406
deadbeefd59daf82015-10-14 15:02:44 -0700407static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 std::string result;
409 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000411 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
412 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800414 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 break;
416 }
417 return result;
418}
419
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420static std::string MakeErrorString(const std::string& error,
421 const std::string& desc) {
422 std::ostringstream ret;
423 ret << error << " " << desc;
424 return ret.str();
425}
426
427static std::string MakeTdErrorString(const std::string& desc) {
428 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429}
430
deadbeef0ed85b22016-02-23 17:24:52 -0800431// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
432bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
433 const SessionDescriptionInterface* new_desc,
434 const std::string& content_name) {
435 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700436 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
deadbeef0ed85b22016-02-23 17:24:52 -0800438 const SessionDescription* new_sd = new_desc->description();
439 const SessionDescription* old_sd = old_desc->description();
440 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
441 if (!cinfo || cinfo->rejected) {
442 return false;
443 }
444 // If the content isn't rejected, check if ufrag and password has changed.
445 const cricket::TransportDescription* new_transport_desc =
446 new_sd->GetTransportDescriptionByName(content_name);
447 const cricket::TransportDescription* old_transport_desc =
448 old_sd->GetTransportDescriptionByName(content_name);
449 if (!new_transport_desc || !old_transport_desc) {
450 // No transport description exists. This is not an ICE restart.
451 return false;
452 }
453 if (cricket::IceCredentialsChanged(
454 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
455 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
456 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
457 << ".";
458 return true;
459 }
460 return false;
461}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
zhihuang29ff8442016-07-27 11:07:25 -0700463WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700464 Call* call,
465 cricket::ChannelManager* channel_manager,
466 const cricket::MediaConfig& media_config,
467 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700468 rtc::Thread* network_thread,
469 rtc::Thread* worker_thread,
470 rtc::Thread* signaling_thread,
471 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800472 std::unique_ptr<cricket::TransportController> transport_controller,
473 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700474 : network_thread_(network_thread),
475 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700476 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 // RFC 3264: The numeric value of the session id and version in the
478 // o line MUST be representable with a "64 bit signed integer".
479 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700480 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700481 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800482 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700483 media_config_(media_config),
484 event_log_(event_log),
485 call_(call),
486 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 ice_observer_(NULL),
488 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700489 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000491 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000493 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700494 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
495 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700496 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700497 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700498 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700499 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700500 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700501 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700502 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700503 transport_controller_->SignalCandidatesRemoved.connect(
504 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700505 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800506 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507}
508
509WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800510 RTC_DCHECK(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000511 // Destroy video_channel_ first since it may have a pointer to the
512 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000513 if (video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800514 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000516 if (voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800517 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000518 }
deadbeef953c2ce2017-01-09 14:53:41 -0800519 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800520 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800521 }
522 if (sctp_transport_) {
523 SignalDataChannelDestroyed();
524 network_thread_->Invoke<void>(
525 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
zhihuang9763d562016-08-05 11:14:50 -0700527#ifdef HAVE_QUIC
528 if (quic_data_transport_) {
529 quic_data_transport_.reset();
530 }
531#endif
deadbeefd59daf82015-10-14 15:02:44 -0700532
533 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534}
535
wu@webrtc.org91053e72013-08-10 07:18:04 +0000536bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000537 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200538 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200539 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
540 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700541 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700542 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000543
Henrik Boström87713d02015-08-25 09:53:21 +0200544 // Obtain a certificate from RTCConfiguration if any were provided (optional).
545 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
546 if (!rtc_configuration.certificates.empty()) {
547 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
548 // just picking the first one. The decision should be made based on the DTLS
549 // handshake. The DTLS negotiations need to know about all certificates.
550 certificate = rtc_configuration.certificates[0];
551 }
552
honghaiz1f429e32015-09-28 07:57:34 -0700553 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700554
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000555 if (options.disable_encryption) {
556 dtls_enabled_ = false;
557 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200558 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200559 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800560 // |rtc_configuration| can override the default |dtls_enabled_| value.
561 if (rtc_configuration.enable_dtls_srtp) {
562 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000563 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000564 }
565
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000567 // It takes precendence over the disable_sctp_data_channels
568 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800569 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700571 }
572#ifdef HAVE_QUIC
573 else if (rtc_configuration.enable_quic) {
574 // Use QUIC instead of DTLS when |enable_quic| is true.
575 data_channel_type_ = cricket::DCT_QUIC;
576 transport_controller_->use_quic();
577 if (dtls_enabled_) {
578 LOG(LS_INFO) << "Using QUIC instead of DTLS";
579 }
580 quic_data_transport_.reset(
581 new QuicDataTransport(signaling_thread(), worker_thread(),
582 network_thread(), transport_controller_.get()));
583 }
584#endif // HAVE_QUIC
585 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000586 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000587 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000588 data_channel_type_ = cricket::DCT_SCTP;
589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
htaa2a49d92016-03-04 02:51:39 -0800592 video_options_.screencast_min_bitrate_kbps =
593 rtc_configuration.screencast_min_bitrate;
594 audio_options_.combined_audio_video_bwe =
595 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000596
kwiberg102c6a62015-10-30 02:47:38 -0700597 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100598 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200599
Karl Wibergbe579832015-11-10 22:34:18 +0100600 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
601 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200602
Henrik Boström87713d02015-08-25 09:53:21 +0200603 if (!dtls_enabled_) {
604 // Construct with DTLS disabled.
605 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200606 signaling_thread(), channel_manager_, this, id(),
607 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200608 } else {
609 // Construct with DTLS enabled.
610 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200611 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200612 signaling_thread(), channel_manager_, this, id(),
613 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200614 } else {
615 // Use the already generated certificate.
616 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200617 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200618 }
619 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000620
Henrik Boströmd8281982015-08-27 10:12:24 +0200621 webrtc_session_desc_factory_->SignalCertificateReady.connect(
622 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000623
wu@webrtc.org97077a32013-10-25 21:18:33 +0000624 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000625 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000626 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700627
jbauch5869f502017-06-29 12:31:36 -0700628 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
629 options.crypto_options.enable_encrypted_rtp_header_extensions);
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 return true;
632}
633
deadbeefd59daf82015-10-14 15:02:44 -0700634void WebRtcSession::Close() {
635 SetState(STATE_CLOSED);
636 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700637 call_ = nullptr;
deadbeef953c2ce2017-01-09 14:53:41 -0800638 RTC_DCHECK(!voice_channel_);
639 RTC_DCHECK(!video_channel_);
640 RTC_DCHECK(!rtp_data_channel_);
641 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642}
643
deadbeef0ed85b22016-02-23 17:24:52 -0800644cricket::BaseChannel* WebRtcSession::GetChannel(
645 const std::string& content_name) {
646 if (voice_channel() && voice_channel()->content_name() == content_name) {
647 return voice_channel();
648 }
649 if (video_channel() && video_channel()->content_name() == content_name) {
650 return video_channel();
651 }
deadbeef953c2ce2017-01-09 14:53:41 -0800652 if (rtp_data_channel() &&
653 rtp_data_channel()->content_name() == content_name) {
654 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800655 }
656 return nullptr;
657}
658
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000659cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
660 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661}
662
deadbeef953c2ce2017-01-09 14:53:41 -0800663bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
664 if (!local_description() || !remote_description()) {
665 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
666 << "SSL Role of the SCTP transport.";
667 return false;
668 }
669 if (!sctp_transport_) {
670 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
671 << "SSL Role of the SCTP transport.";
672 return false;
673 }
674
675 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
676}
677
678bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800679 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800680 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800681 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000682 << "SSL Role of the session.";
683 return false;
684 }
685
deadbeef953c2ce2017-01-09 14:53:41 -0800686 return transport_controller_->GetSslRole(GetTransportName(content_name),
687 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000688}
689
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000690void WebRtcSession::CreateOffer(
691 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700692 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
693 const cricket::MediaSessionOptions& session_options) {
694 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000695}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696
deadbeefab9b2d12015-10-14 11:33:11 -0700697void WebRtcSession::CreateAnswer(
698 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700699 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800700 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701}
702
703bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
704 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800705 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700706
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000707 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700708 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000709
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000710 // Validate SDP.
711 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
712 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
deadbeefd59daf82015-10-14 15:02:44 -0700715 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000716 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700718 initial_offerer_ = true;
719 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 }
721
deadbeeffe4a8a42016-12-20 17:56:17 -0800722 if (action == kAnswer) {
723 current_local_description_.reset(desc_temp.release());
724 pending_local_description_.reset(nullptr);
725 current_remote_description_.reset(pending_remote_description_.release());
726 } else {
727 pending_local_description_.reset(desc_temp.release());
728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
730 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800731 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 // TODO(mallinath) - Handle CreateChannel failure, as new local description
733 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000734 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736
deadbeefcbecd352015-09-23 11:50:27 -0700737 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800738 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000740 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 return false;
742 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800743 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700744 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800745 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747
deadbeef0ed85b22016-02-23 17:24:52 -0800748 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700749 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000750 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752 return true;
753}
754
755bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
756 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800757 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700758
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700760 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000761
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000762 // Validate SDP.
763 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
764 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
766
deadbeeffe4a8a42016-12-20 17:56:17 -0800767 const SessionDescriptionInterface* old_remote_description =
768 remote_description();
769 // Grab ownership of the description being replaced for the remainder of this
770 // method, since it's used below.
771 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
772 Action action = GetAction(desc->type());
773 if (action == kAnswer) {
774 replaced_remote_description.reset(
775 pending_remote_description_ ? pending_remote_description_.release()
776 : current_remote_description_.release());
777 current_remote_description_.reset(desc_temp.release());
778 pending_remote_description_.reset(nullptr);
779 current_local_description_.reset(pending_local_description_.release());
780 } else {
781 replaced_remote_description.reset(pending_remote_description_.release());
782 pending_remote_description_.reset(desc_temp.release());
783 }
deadbeefd59daf82015-10-14 15:02:44 -0700784
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 // Transport and Media channels will be created only when offer is set.
786 if (action == kOffer && !CreateChannels(desc->description())) {
787 // TODO(mallinath) - Handle CreateChannel failure, as new local description
788 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000789 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 }
791
deadbeefcbecd352015-09-23 11:50:27 -0700792 // Remove unused channels if MediaContentDescription is rejected.
793 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794
795 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
796 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000797 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 return false;
799 }
800
deadbeeffe4a8a42016-12-20 17:56:17 -0800801 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000802 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 }
804
deadbeeffe4a8a42016-12-20 17:56:17 -0800805 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800806 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800807 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800808 // Check if this new SessionDescription contains new ICE ufrag and
809 // password that indicates the remote peer requests an ICE restart.
810 // TODO(deadbeef): When we start storing both the current and pending
811 // remote description, this should reset pending_ice_restarts and compare
812 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800813 if (CheckForRemoteIceRestart(old_remote_description, desc,
814 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800815 if (action == kOffer) {
816 pending_ice_restarts_.insert(content.name);
817 }
818 } else {
819 // We retain all received candidates only if ICE is not restarted.
820 // When ICE is restarted, all previous candidates belong to an old
821 // generation and should not be kept.
822 // TODO(deadbeef): This goes against the W3C spec which says the remote
823 // description should only contain candidates from the last set remote
824 // description plus any candidates added since then. We should remove
825 // this once we're sure it won't break anything.
826 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800827 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800828 }
829 }
honghaiz503726c2015-07-31 12:37:38 -0700830 }
831
deadbeefd59daf82015-10-14 15:02:44 -0700832 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000833 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000835
836 // Set the the ICE connection state to connecting since the connection may
837 // become writable with peer reflexive candidates before any remote candidate
838 // is signaled.
839 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
840 // is to have a new signal the indicates a change in checking state from the
841 // transport and expose a new checking() member from transport that can be
842 // read to determine the current checking state. The existing SignalConnecting
843 // actually means "gathering candidates", so cannot be be used here.
844 if (desc->type() != SessionDescriptionInterface::kOffer &&
845 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
846 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
847 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 return true;
849}
850
deadbeefd59daf82015-10-14 15:02:44 -0700851void WebRtcSession::LogState(State old_state, State new_state) {
852 LOG(LS_INFO) << "Session:" << id()
853 << " Old state:" << GetStateString(old_state)
854 << " New state:" << GetStateString(new_state);
855}
856
857void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800858 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700859 if (state != state_) {
860 LogState(state_, state);
861 state_ = state;
862 SignalState(this, state_);
863 }
864}
865
866void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800867 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700868 if (error != error_) {
869 error_ = error;
870 error_desc_ = error_desc;
871 }
872}
873
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874bool WebRtcSession::UpdateSessionState(
875 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800877 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700878
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 // If there's already a pending error then no state transition should happen.
880 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800881 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000882 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
885 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
deadbeefd59daf82015-10-14 15:02:44 -0700887 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
888 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000889 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700890 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000891 }
deadbeefd59daf82015-10-14 15:02:44 -0700892 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000893 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
897 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 }
899 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700900 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
901 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000902 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700903 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000904 }
deadbeefd59daf82015-10-14 15:02:44 -0700905 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000906 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 }
908 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700909 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800910 local_description()->description()->GetGroupByName(
911 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700912 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800913 remote_description()->description()->GetGroupByName(
914 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700915 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800916 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700917 const cricket::ContentGroup* answer_bundle =
918 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
919 if (!EnableBundle(*answer_bundle)) {
920 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
921 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
922 }
923 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800924 // Only push down the transport description after enabling BUNDLE; we don't
925 // want to push down a description on a transport about to be destroyed.
926 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
927 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700930 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 if (!PushdownMediaDescription(cricket::CA_ANSWER, 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 BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937 }
938 return true;
939}
940
941WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
942 if (type == SessionDescriptionInterface::kOffer) {
943 return WebRtcSession::kOffer;
944 } else if (type == SessionDescriptionInterface::kPrAnswer) {
945 return WebRtcSession::kPrAnswer;
946 } else if (type == SessionDescriptionInterface::kAnswer) {
947 return WebRtcSession::kAnswer;
948 }
nisseede5da42017-01-12 05:15:36 -0800949 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 return WebRtcSession::kOffer;
951}
952
deadbeefd59daf82015-10-14 15:02:44 -0700953bool WebRtcSession::PushdownMediaDescription(
954 cricket::ContentAction action,
955 cricket::ContentSource source,
956 std::string* err) {
957 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
958 if (!ch) {
959 return true;
960 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800961 return ch->PushdownLocalDescription(local_description()->description(),
962 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700963 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800964 return ch->PushdownRemoteDescription(remote_description()->description(),
965 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700966 }
967 };
968
deadbeef953c2ce2017-01-09 14:53:41 -0800969 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
970 set_content(rtp_data_channel()));
971 // Need complete offer/answer with an SCTP m= section before starting SCTP,
972 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
973 if (sctp_transport_ && local_description() && remote_description() &&
974 cricket::GetFirstDataContent(local_description()->description()) &&
975 cricket::GetFirstDataContent(remote_description()->description())) {
976 ret &= network_thread_->Invoke<bool>(
977 RTC_FROM_HERE,
978 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
979 }
980 return ret;
981}
982
983bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
984 RTC_DCHECK(network_thread_->IsCurrent());
985 RTC_DCHECK(local_description());
986 RTC_DCHECK(remote_description());
987 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
988 // When we support "max-message-size", that would also be pushed down here.
989 return sctp_transport_->Start(
990 GetSctpPort(local_description()->description()),
991 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700992}
993
994bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
995 cricket::ContentAction action,
996 std::string* error_desc) {
997 RTC_DCHECK(signaling_thread()->IsCurrent());
998
999 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001000 return PushdownLocalTransportDescription(local_description()->description(),
1001 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001002 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001003 return PushdownRemoteTransportDescription(remote_description()->description(),
1004 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001005}
1006
1007bool WebRtcSession::PushdownLocalTransportDescription(
1008 const SessionDescription* sdesc,
1009 cricket::ContentAction action,
1010 std::string* err) {
1011 RTC_DCHECK(signaling_thread()->IsCurrent());
1012
1013 if (!sdesc) {
1014 return false;
1015 }
1016
1017 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1018 if (!transport_controller_->SetLocalTransportDescription(
1019 tinfo.content_name, tinfo.description, action, err)) {
1020 return false;
1021 }
1022 }
1023
1024 return true;
1025}
1026
1027bool WebRtcSession::PushdownRemoteTransportDescription(
1028 const SessionDescription* sdesc,
1029 cricket::ContentAction action,
1030 std::string* err) {
1031 RTC_DCHECK(signaling_thread()->IsCurrent());
1032
1033 if (!sdesc) {
1034 return false;
1035 }
1036
1037 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1038 if (!transport_controller_->SetRemoteTransportDescription(
1039 tinfo.content_name, tinfo.description, action, err)) {
1040 return false;
1041 }
1042 }
1043
1044 return true;
1045}
1046
1047bool WebRtcSession::GetTransportDescription(
1048 const SessionDescription* description,
1049 const std::string& content_name,
1050 cricket::TransportDescription* tdesc) {
1051 if (!description || !tdesc) {
1052 return false;
1053 }
1054 const TransportInfo* transport_info =
1055 description->GetTransportInfoByName(content_name);
1056 if (!transport_info) {
1057 return false;
1058 }
1059 *tdesc = transport_info->description;
1060 return true;
1061}
1062
deadbeefcbecd352015-09-23 11:50:27 -07001063bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1064 const std::string* first_content_name = bundle.FirstContentName();
1065 if (!first_content_name) {
1066 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1067 return false;
1068 }
1069 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001070
zhihuang9763d562016-08-05 11:14:50 -07001071#ifdef HAVE_QUIC
1072 if (quic_data_transport_ &&
1073 bundle.HasContentName(quic_data_transport_->content_name()) &&
1074 quic_data_transport_->transport_name() != transport_name) {
1075 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1076 << " on " << transport_name << "with QUIC.";
1077 }
1078#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001079 auto maybe_set_transport = [this, bundle,
1080 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001081 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001082 return true;
1083 }
1084
zhihuangf5b251b2017-01-12 19:37:48 -08001085 std::string old_transport_name = ch->transport_name();
1086 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001087 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1088 << " on " << transport_name << ".";
1089 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001090 }
torbjornga81a42f2015-09-23 02:16:58 -07001091
zhihuangb2cdd932017-01-19 16:54:25 -08001092 cricket::DtlsTransportInternal* rtp_dtls_transport =
1093 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001094 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001095 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1096 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001097 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001098 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001099 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1100 }
1101
zhihuangb2cdd932017-01-19 16:54:25 -08001102 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001103 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1104 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001105 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001106 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001107 // If the channel needs rtcp, it means that the channel used to have a
1108 // rtcp transport which needs to be deleted now.
1109 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001110 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001111 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001112 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001113 return true;
1114 };
1115
deadbeefcbecd352015-09-23 11:50:27 -07001116 if (!maybe_set_transport(voice_channel()) ||
1117 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001118 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001119 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001120 }
deadbeef953c2ce2017-01-09 14:53:41 -08001121 // For SCTP, transport creation/deletion happens here instead of in the
1122 // object itself.
1123 if (sctp_transport_) {
1124 RTC_DCHECK(sctp_transport_name_);
1125 RTC_DCHECK(sctp_content_name_);
1126 if (transport_name != *sctp_transport_name_ &&
1127 bundle.HasContentName(*sctp_content_name_)) {
1128 network_thread_->Invoke<void>(
1129 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1130 transport_name));
1131 }
1132 }
deadbeefcbecd352015-09-23 11:50:27 -07001133
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001134 return true;
1135}
1136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001138 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001139 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1140 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001141 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 }
1143
1144 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001145 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 return false;
1147 }
1148
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001149 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001150 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1151 if (!valid) {
1152 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 }
1154
1155 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001156 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001157 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 return false;
1159 }
1160
deadbeefd59daf82015-10-14 15:02:44 -07001161 if (ready) {
1162 return UseCandidate(candidate);
1163 } else {
1164 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1165 return true;
1166 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167}
1168
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001169bool WebRtcSession::RemoveRemoteIceCandidates(
1170 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001171 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001172 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1173 << "removed without any remote session description.";
1174 return false;
1175 }
1176
1177 if (candidates.empty()) {
1178 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1179 return false;
1180 }
1181
deadbeeffe4a8a42016-12-20 17:56:17 -08001182 size_t number_removed =
1183 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001184 if (number_removed != candidates.size()) {
1185 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1186 << "Requested " << candidates.size() << " but only "
1187 << number_removed << " are removed.";
1188 }
1189
1190 // Remove the candidates from the transport controller.
1191 std::string error;
1192 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1193 if (!res && !error.empty()) {
1194 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1195 }
1196 return true;
1197}
1198
deadbeefd59daf82015-10-14 15:02:44 -07001199cricket::IceConfig WebRtcSession::ParseIceConfig(
1200 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001201 cricket::ContinualGatheringPolicy gathering_policy;
1202 // TODO(honghaiz): Add the third continual gathering policy in
1203 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1204 switch (config.continual_gathering_policy) {
1205 case PeerConnectionInterface::GATHER_ONCE:
1206 gathering_policy = cricket::GATHER_ONCE;
1207 break;
1208 case PeerConnectionInterface::GATHER_CONTINUALLY:
1209 gathering_policy = cricket::GATHER_CONTINUALLY;
1210 break;
1211 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001212 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001213 gathering_policy = cricket::GATHER_ONCE;
1214 }
deadbeefd59daf82015-10-14 15:02:44 -07001215 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001216 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001217 ice_config.prioritize_most_likely_candidate_pairs =
1218 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001219 ice_config.backup_connection_ping_interval =
1220 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001221 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001222 ice_config.presume_writable_when_fully_relayed =
1223 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001224 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001225 ice_config.regather_all_networks_interval_range =
1226 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001227 return ice_config;
1228}
1229
1230void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1231 transport_controller_->SetIceConfig(config);
1232}
1233
1234void WebRtcSession::MaybeStartGathering() {
1235 transport_controller_->MaybeStartGathering();
1236}
1237
Peter Boström0c4e06b2015-10-07 12:23:21 +02001238bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1239 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001240 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001242 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001243 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1244 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245}
1246
Peter Boström0c4e06b2015-10-07 12:23:21 +02001247bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1248 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001249 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001250 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001251 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001252 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1253 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254}
1255
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001258 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 return desc.str();
1260}
1261
wu@webrtc.org78187522013-10-07 23:32:02 +00001262bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001263 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001264 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001265 if (!rtp_data_channel_ && !sctp_transport_) {
1266 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1267 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001268 return false;
1269 }
deadbeef953c2ce2017-01-09 14:53:41 -08001270 return rtp_data_channel_
1271 ? rtp_data_channel_->SendData(params, payload, result)
1272 : network_thread_->Invoke<bool>(
1273 RTC_FROM_HERE,
1274 Bind(&cricket::SctpTransportInternal::SendData,
1275 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001276}
1277
1278bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001279 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001280 // Don't log an error here, because DataChannels are expected to call
1281 // ConnectDataChannel in this state. It's the only way to initially tell
1282 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001283 return false;
1284 }
deadbeef953c2ce2017-01-09 14:53:41 -08001285 if (rtp_data_channel_) {
1286 rtp_data_channel_->SignalReadyToSendData.connect(
1287 webrtc_data_channel, &DataChannel::OnChannelReady);
1288 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1289 &DataChannel::OnDataReceived);
1290 } else {
1291 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1292 &DataChannel::OnChannelReady);
1293 SignalSctpDataReceived.connect(webrtc_data_channel,
1294 &DataChannel::OnDataReceived);
1295 SignalSctpStreamClosedRemotely.connect(
1296 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1297 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001298 return true;
1299}
1300
1301void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001302 if (!rtp_data_channel_ && !sctp_transport_) {
1303 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1304 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001305 return;
1306 }
deadbeef953c2ce2017-01-09 14:53:41 -08001307 if (rtp_data_channel_) {
1308 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1309 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1310 } else {
1311 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1312 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1313 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1314 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001315}
1316
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001317void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001318 if (!sctp_transport_) {
1319 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001320 return;
1321 }
deadbeef953c2ce2017-01-09 14:53:41 -08001322 network_thread_->Invoke<void>(
1323 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1324 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001325}
1326
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001327void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001328 if (!sctp_transport_) {
1329 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001330 << "NULL.";
1331 return;
1332 }
deadbeef953c2ce2017-01-09 14:53:41 -08001333 network_thread_->Invoke<void>(
1334 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1335 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001336}
1337
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001338bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001339 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1340 sctp_ready_to_send_data_;
1341}
1342
1343std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001344 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001345 ChannelNamePairs channel_name_pairs;
1346 if (voice_channel()) {
1347 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1348 voice_channel()->content_name(), voice_channel()->transport_name()));
1349 }
1350 if (video_channel()) {
1351 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1352 video_channel()->content_name(), video_channel()->transport_name()));
1353 }
1354 if (rtp_data_channel()) {
1355 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1356 ChannelNamePair(rtp_data_channel()->content_name(),
1357 rtp_data_channel()->transport_name()));
1358 }
1359 if (sctp_transport_) {
1360 RTC_DCHECK(sctp_content_name_);
1361 RTC_DCHECK(sctp_transport_name_);
1362 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1363 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1364 }
1365 return GetStats(channel_name_pairs);
1366}
1367
1368std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1369 const ChannelNamePairs& channel_name_pairs) {
1370 if (network_thread()->IsCurrent()) {
1371 return GetStats_n(channel_name_pairs);
1372 }
1373 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1374 RTC_FROM_HERE,
1375 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1376}
1377
1378bool WebRtcSession::GetLocalCertificate(
1379 const std::string& transport_name,
1380 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1381 return transport_controller_->GetLocalCertificate(transport_name,
1382 certificate);
1383}
1384
1385std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1386 const std::string& transport_name) {
1387 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001388}
1389
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390cricket::DataChannelType WebRtcSession::data_channel_type() const {
1391 return data_channel_type_;
1392}
1393
deadbeef0ed85b22016-02-23 17:24:52 -08001394bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1395 return pending_ice_restarts_.find(content_name) !=
1396 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001397}
1398
deadbeefd1a38b52016-12-10 13:15:33 -08001399void WebRtcSession::SetNeedsIceRestartFlag() {
1400 transport_controller_->SetNeedsIceRestartFlag();
1401}
1402
1403bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1404 return transport_controller_->NeedsIceRestart(content_name);
1405}
1406
Henrik Boströmd8281982015-08-27 10:12:24 +02001407void WebRtcSession::OnCertificateReady(
1408 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001409 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001410}
1411
deadbeef953c2ce2017-01-09 14:53:41 -08001412void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1413 SetError(ERROR_TRANSPORT,
1414 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1415}
1416
Henrik Boströmd8281982015-08-27 10:12:24 +02001417bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001418 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001419}
1420
deadbeefcbecd352015-09-23 11:50:27 -07001421const rtc::scoped_refptr<rtc::RTCCertificate>&
1422WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001423 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001424}
1425
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426void WebRtcSession::SetIceConnectionState(
1427 PeerConnectionInterface::IceConnectionState state) {
1428 if (ice_connection_state_ == state) {
1429 return;
1430 }
1431
deadbeefcbecd352015-09-23 11:50:27 -07001432 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1433 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001434 RTC_DCHECK(ice_connection_state_ !=
1435 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 ice_connection_state_ = state;
1437 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001438 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 }
1440}
1441
deadbeefcbecd352015-09-23 11:50:27 -07001442void WebRtcSession::OnTransportControllerConnectionState(
1443 cricket::IceConnectionState state) {
1444 switch (state) {
1445 case cricket::kIceConnectionConnecting:
1446 // If the current state is Connected or Completed, then there were
1447 // writable channels but now there are not, so the next state must
1448 // be Disconnected.
1449 // kIceConnectionConnecting is currently used as the default,
1450 // un-connected state by the TransportController, so its only use is
1451 // detecting disconnections.
1452 if (ice_connection_state_ ==
1453 PeerConnectionInterface::kIceConnectionConnected ||
1454 ice_connection_state_ ==
1455 PeerConnectionInterface::kIceConnectionCompleted) {
1456 SetIceConnectionState(
1457 PeerConnectionInterface::kIceConnectionDisconnected);
1458 }
torbjornga81a42f2015-09-23 02:16:58 -07001459 break;
deadbeefcbecd352015-09-23 11:50:27 -07001460 case cricket::kIceConnectionFailed:
1461 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1462 break;
1463 case cricket::kIceConnectionConnected:
1464 LOG(LS_INFO) << "Changing to ICE connected state because "
1465 << "all transports are writable.";
1466 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1467 break;
1468 case cricket::kIceConnectionCompleted:
1469 LOG(LS_INFO) << "Changing to ICE completed state because "
1470 << "all transports are complete.";
1471 if (ice_connection_state_ !=
1472 PeerConnectionInterface::kIceConnectionConnected) {
1473 // If jumping directly from "checking" to "connected",
1474 // signal "connected" first.
1475 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1476 }
1477 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1478 if (metrics_observer_) {
1479 ReportTransportStats();
1480 }
1481 break;
1482 default:
nissec80e7412017-01-11 05:56:46 -08001483 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001484 }
deadbeefcbecd352015-09-23 11:50:27 -07001485}
1486
1487void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001488 SetIceConnectionReceiving(receiving);
1489}
1490
1491void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1492 if (ice_connection_receiving_ == receiving) {
1493 return;
1494 }
1495 ice_connection_receiving_ = receiving;
1496 if (ice_observer_) {
1497 ice_observer_->OnIceConnectionReceivingChange(receiving);
1498 }
1499}
1500
deadbeefcbecd352015-09-23 11:50:27 -07001501void WebRtcSession::OnTransportControllerCandidatesGathered(
1502 const std::string& transport_name,
1503 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001504 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001505 int sdp_mline_index;
1506 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1507 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1508 << transport_name << " not found";
1509 return;
1510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511
deadbeefcbecd352015-09-23 11:50:27 -07001512 for (cricket::Candidates::const_iterator citer = candidates.begin();
1513 citer != candidates.end(); ++citer) {
1514 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001515 std::unique_ptr<JsepIceCandidate> candidate(
1516 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001517 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001518 mutable_local_description()->AddCandidate(candidate.get());
1519 }
1520 if (ice_observer_) {
1521 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001522 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 }
1524}
1525
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001526void WebRtcSession::OnTransportControllerCandidatesRemoved(
1527 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001528 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001529 // Sanity check.
1530 for (const cricket::Candidate& candidate : candidates) {
1531 if (candidate.transport_name().empty()) {
1532 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1533 << "empty content name in candidate "
1534 << candidate.ToString();
1535 return;
1536 }
1537 }
1538
deadbeeffe4a8a42016-12-20 17:56:17 -08001539 if (local_description()) {
1540 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001541 }
1542 if (ice_observer_) {
1543 ice_observer_->OnIceCandidatesRemoved(candidates);
1544 }
1545}
1546
deadbeef953c2ce2017-01-09 14:53:41 -08001547void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1548 rtc::SSLHandshakeError error) {
1549 if (metrics_observer_) {
1550 metrics_observer_->IncrementEnumCounter(
1551 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1552 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1553 }
1554}
1555
1556// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557void WebRtcSession::EnableChannels() {
1558 if (voice_channel_ && !voice_channel_->enabled())
1559 voice_channel_->Enable(true);
1560
1561 if (video_channel_ && !video_channel_->enabled())
1562 video_channel_->Enable(true);
1563
deadbeef953c2ce2017-01-09 14:53:41 -08001564 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1565 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566}
1567
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568// Returns the media index for a local ice candidate given the content name.
1569bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1570 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001571 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001573 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574
1575 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001576 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 for (size_t index = 0; index < contents.size(); ++index) {
1578 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001579 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 content_found = true;
1581 break;
1582 }
1583 }
1584 return content_found;
1585}
1586
1587bool WebRtcSession::UseCandidatesInSessionDescription(
1588 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001589 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001591 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001593
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1595 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001596 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001597 const IceCandidateInterface* candidate = candidates->at(n);
1598 bool valid = false;
1599 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1600 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001601 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1602 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001603 }
1604 continue;
1605 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001606 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001607 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 break;
deadbeefd59daf82015-10-14 15:02:44 -07001609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 }
1611 }
1612 return ret;
1613}
1614
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001615bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001617 size_t remote_content_size =
1618 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001620 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 return false;
1622 }
1623
1624 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001625 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 std::vector<cricket::Candidate> candidates;
1627 candidates.push_back(candidate->candidate());
1628 // Invoking BaseSession method to handle remote candidates.
1629 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001630 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1631 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 // Candidates successfully submitted for checking.
1633 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1634 ice_connection_state_ ==
1635 PeerConnectionInterface::kIceConnectionDisconnected) {
1636 // If state is New, then the session has just gotten its first remote ICE
1637 // candidates, so go to Checking.
1638 // If state is Disconnected, the session is re-using old candidates or
1639 // receiving additional ones, so go to Checking.
1640 // If state is Connected, stay Connected.
1641 // TODO(bemasc): If state is Connected, and the new candidates are for a
1642 // newly added transport, then the state actually _should_ move to
1643 // checking. Add a way to distinguish that case.
1644 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1645 }
1646 // TODO(bemasc): If state is Completed, go back to Connected.
1647 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001648 if (!error.empty()) {
1649 LOG(LS_WARNING) << error;
1650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 }
1652 return true;
1653}
1654
deadbeefcbecd352015-09-23 11:50:27 -07001655void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001656 // Destroy video_channel_ first since it may have a pointer to the
1657 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 const cricket::ContentInfo* video_info =
1659 cricket::GetFirstVideoContent(desc);
1660 if ((!video_info || video_info->rejected) && video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001661 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 }
1663
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001664 const cricket::ContentInfo* voice_info =
1665 cricket::GetFirstAudioContent(desc);
1666 if ((!voice_info || voice_info->rejected) && voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001667 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001668 }
1669
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 const cricket::ContentInfo* data_info =
1671 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001672 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001673 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001674 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001675 }
1676 if (sctp_transport_) {
1677 SignalDataChannelDestroyed();
1678 network_thread_->Invoke<void>(
1679 RTC_FROM_HERE,
1680 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001681 }
1682#ifdef HAVE_QUIC
1683 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1684 if (quic_data_transport_) {
1685 quic_data_transport_.reset();
1686 }
1687#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 }
1689}
1690
skvlad6c87a672016-05-17 17:49:52 -07001691// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1692// if the channel is not part of any bundle.
1693const std::string* WebRtcSession::GetBundleTransportName(
1694 const cricket::ContentInfo* content,
1695 const cricket::ContentGroup* bundle) {
1696 if (!bundle) {
1697 return nullptr;
1698 }
1699 const std::string* first_content_name = bundle->FirstContentName();
1700 if (!first_content_name) {
1701 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1702 return nullptr;
1703 }
1704 if (!bundle->HasContentName(content->name)) {
1705 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1706 return nullptr;
1707 }
1708 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1709 return first_content_name;
1710}
1711
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001713 const cricket::ContentGroup* bundle_group = nullptr;
1714 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1715 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1716 if (!bundle_group) {
1717 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1718 return false;
1719 }
1720 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 // Creating the media channels and transport proxies.
1722 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1723 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001724 if (!CreateVoiceChannel(voice,
1725 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 LOG(LS_ERROR) << "Failed to create voice channel.";
1727 return false;
1728 }
1729 }
1730
1731 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1732 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001733 if (!CreateVideoChannel(video,
1734 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 LOG(LS_ERROR) << "Failed to create video channel.";
1736 return false;
1737 }
1738 }
1739
1740 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001741 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1742 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001743 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 LOG(LS_ERROR) << "Failed to create data channel.";
1745 return false;
1746 }
1747 }
1748
1749 return true;
1750}
1751
skvlad6c87a672016-05-17 17:49:52 -07001752bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1753 const std::string* bundle_transport) {
1754 bool require_rtcp_mux =
1755 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001756
1757 std::string transport_name =
1758 bundle_transport ? *bundle_transport : content->name;
1759
zhihuangb2cdd932017-01-19 16:54:25 -08001760 cricket::DtlsTransportInternal* rtp_dtls_transport =
1761 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001762 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001763 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001764 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001765 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001766 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1767 }
1768
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001770 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001771 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1772 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001773 if (!voice_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001774 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001775 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001776 if (rtcp_dtls_transport) {
1777 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001778 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1779 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001780 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001781 }
zhihuangf5b251b2017-01-12 19:37:48 -08001782
deadbeefac22f702017-01-12 21:59:29 -08001783 voice_channel_->SignalRtcpMuxFullyActive.connect(
1784 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001785 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1786 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001787
1788 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001789 voice_channel_->SignalSentPacket.connect(this,
1790 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001791 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792}
1793
skvlad6c87a672016-05-17 17:49:52 -07001794bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1795 const std::string* bundle_transport) {
1796 bool require_rtcp_mux =
1797 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001798
1799 std::string transport_name =
1800 bundle_transport ? *bundle_transport : content->name;
1801
zhihuangb2cdd932017-01-19 16:54:25 -08001802 cricket::DtlsTransportInternal* rtp_dtls_transport =
1803 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001804 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001805 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001806 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001807 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001808 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1809 }
1810
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 video_channel_.reset(channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001812 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001813 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1814 video_options_));
zhihuangf5b251b2017-01-12 19:37:48 -08001815
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001816 if (!video_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001817 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001818 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001819 if (rtcp_dtls_transport) {
1820 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001821 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1822 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001823 return false;
1824 }
zhihuangf5b251b2017-01-12 19:37:48 -08001825
deadbeefac22f702017-01-12 21:59:29 -08001826 video_channel_->SignalRtcpMuxFullyActive.connect(
1827 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001828 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1829 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001830
1831 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001832 video_channel_->SignalSentPacket.connect(this,
1833 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001834 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835}
1836
skvlad6c87a672016-05-17 17:49:52 -07001837bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1838 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001839 const std::string transport_name =
1840 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001841#ifdef HAVE_QUIC
1842 if (data_channel_type_ == cricket::DCT_QUIC) {
1843 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001844 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001845 return true;
1846 }
1847#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001848 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001849 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001850 if (!sctp_factory_) {
1851 LOG(LS_ERROR)
1852 << "Trying to create SCTP transport, but didn't compile with "
1853 "SCTP support (HAVE_SCTP)";
1854 return false;
1855 }
1856 if (!network_thread_->Invoke<bool>(
1857 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1858 this, content->name, transport_name))) {
1859 return false;
1860 };
1861 } else {
1862 bool require_rtcp_mux =
1863 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001864
1865 std::string transport_name =
1866 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001867 cricket::DtlsTransportInternal* rtp_dtls_transport =
1868 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001869 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001870 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001871 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001872 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001873 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1874 }
1875
deadbeef953c2ce2017-01-09 14:53:41 -08001876 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001877 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001878 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001879 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001880
deadbeef953c2ce2017-01-09 14:53:41 -08001881 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001882 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001883 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001884 if (rtcp_dtls_transport) {
1885 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001886 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1887 }
deadbeef953c2ce2017-01-09 14:53:41 -08001888 return false;
1889 }
zhihuangf5b251b2017-01-12 19:37:48 -08001890
deadbeefac22f702017-01-12 21:59:29 -08001891 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1892 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001893 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1894 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1895 rtp_data_channel_->SignalSentPacket.connect(this,
1896 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001897 }
1898
deadbeefab9b2d12015-10-14 11:33:11 -07001899 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001900
wu@webrtc.org91053e72013-08-10 07:18:04 +00001901 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902}
1903
stefanf79ade12017-06-02 06:44:03 -07001904Call::Stats WebRtcSession::GetCallStats() {
1905 if (!worker_thread()->IsCurrent()) {
1906 return worker_thread()->Invoke<Call::Stats>(
1907 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1908 }
zhihuang38ede132017-06-15 12:52:32 -07001909 if (!call_)
1910 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001911 return call_->GetStats();
1912}
1913
hbosdf6075a2016-12-19 04:58:02 -08001914std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1915 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001916 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001917 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1918 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1919 &channel_name_pairs.video,
1920 &channel_name_pairs.data }) {
1921 if (*channel_name_pair) {
1922 cricket::TransportStats transport_stats;
1923 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1924 &transport_stats)) {
1925 return nullptr;
1926 }
1927 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1928 (*channel_name_pair)->transport_name;
1929 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1930 std::move(transport_stats);
1931 }
1932 }
1933 return session_stats;
1934}
1935
deadbeef953c2ce2017-01-09 14:53:41 -08001936bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1937 const std::string& transport_name) {
1938 RTC_DCHECK(network_thread_->IsCurrent());
1939 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001940 cricket::DtlsTransportInternal* tc =
1941 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001942 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1943 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1944 RTC_DCHECK(sctp_transport_);
1945 sctp_invoker_.reset(new rtc::AsyncInvoker());
1946 sctp_transport_->SignalReadyToSendData.connect(
1947 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1948 sctp_transport_->SignalDataReceived.connect(
1949 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1950 sctp_transport_->SignalStreamClosedRemotely.connect(
1951 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1952 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1953 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1954 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955}
1956
deadbeef953c2ce2017-01-09 14:53:41 -08001957void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1958 RTC_DCHECK(network_thread_->IsCurrent());
1959 RTC_DCHECK(sctp_transport_);
1960 RTC_DCHECK(sctp_transport_name_);
1961 std::string old_sctp_transport_name = *sctp_transport_name_;
1962 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08001963 cricket::DtlsTransportInternal* tc =
1964 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001965 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1966 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08001967 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001968 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1969}
1970
1971void WebRtcSession::DestroySctpTransport_n() {
1972 RTC_DCHECK(network_thread_->IsCurrent());
1973 sctp_transport_.reset(nullptr);
1974 sctp_content_name_.reset();
1975 sctp_transport_name_.reset();
1976 sctp_invoker_.reset(nullptr);
1977 sctp_ready_to_send_data_ = false;
1978}
1979
1980void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1981 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1982 RTC_DCHECK(network_thread_->IsCurrent());
1983 sctp_invoker_->AsyncInvoke<void>(
1984 RTC_FROM_HERE, signaling_thread_,
1985 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1986}
1987
1988void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1989 RTC_DCHECK(signaling_thread_->IsCurrent());
1990 sctp_ready_to_send_data_ = ready;
1991 SignalSctpReadyToSendData(ready);
1992}
1993
1994void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001995 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001996 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001997 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08001998 RTC_DCHECK(network_thread_->IsCurrent());
1999 sctp_invoker_->AsyncInvoke<void>(
2000 RTC_FROM_HERE, signaling_thread_,
2001 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2002 payload));
2003}
2004
2005void WebRtcSession::OnSctpTransportDataReceived_s(
2006 const cricket::ReceiveDataParams& params,
2007 const rtc::CopyOnWriteBuffer& payload) {
2008 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002009 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2010 // Received OPEN message; parse and signal that a new data channel should
2011 // be created.
2012 std::string label;
2013 InternalDataChannelInit config;
2014 config.id = params.ssrc;
2015 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2016 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2017 << params.ssrc;
2018 return;
2019 }
2020 config.open_handshake_role = InternalDataChannelInit::kAcker;
2021 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002022 } else {
2023 // Otherwise just forward the signal.
2024 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 }
deadbeef953c2ce2017-01-09 14:53:41 -08002026}
2027
2028void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2029 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2030 RTC_DCHECK(network_thread_->IsCurrent());
2031 sctp_invoker_->AsyncInvoke<void>(
2032 RTC_FROM_HERE, signaling_thread_,
2033 rtc::Bind(&sigslot::signal1<int>::operator(),
2034 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035}
2036
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002037// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002038bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002039 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2040 if (!bundle_enabled)
2041 return true;
2042
2043 const cricket::ContentGroup* bundle_group =
2044 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002045 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002046
2047 const cricket::ContentInfos& contents = desc->contents();
2048 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2049 citer != contents.end(); ++citer) {
2050 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002051 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002052 if (bundle_group->HasContentName(content->name) &&
2053 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2054 if (!HasRtcpMuxEnabled(content))
2055 return false;
2056 }
2057 }
2058 // RTCP-MUX is enabled in all the contents.
2059 return true;
2060}
2061
2062bool WebRtcSession::HasRtcpMuxEnabled(
2063 const cricket::ContentInfo* content) {
2064 const cricket::MediaContentDescription* description =
2065 static_cast<cricket::MediaContentDescription*>(content->description);
2066 return description->rtcp_mux();
2067}
2068
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002069bool WebRtcSession::ValidateSessionDescription(
2070 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002071 cricket::ContentSource source, std::string* err_desc) {
2072 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002073 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002074 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002075 }
2076
2077 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002078 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002079 }
2080
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002081 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002082 Action action = GetAction(sdesc->type());
2083 if (source == cricket::CS_LOCAL) {
2084 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002085 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002086 } else {
2087 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002088 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002089 }
2090
2091 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002092 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002093 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2094 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002095 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002096 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002097 }
2098
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002099 // Verify ice-ufrag and ice-pwd.
2100 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002101 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002102 }
2103
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002104 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002105 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002106 }
2107
skvlad6c87a672016-05-17 17:49:52 -07002108 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2109 // m-lines that do not rtcp-mux enabled.
2110
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002111 // Verify m-lines in Answer when compared against Offer.
2112 if (action == kAnswer) {
2113 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002114 (source == cricket::CS_LOCAL) ? remote_description()->description()
2115 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002116 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002117 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002118 }
2119 }
2120
2121 return true;
2122}
2123
2124bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2125 return ((action == kOffer && state() == STATE_INIT) ||
2126 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002127 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002128 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002129 (action == kOffer && state() == STATE_INPROGRESS) ||
2130 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002131 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2132 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2133 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2134 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002135}
2136
2137bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2138 return ((action == kOffer && state() == STATE_INIT) ||
2139 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002140 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002141 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002142 (action == kOffer && state() == STATE_INPROGRESS) ||
2143 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002144 (action == kAnswer && state() == STATE_SENTOFFER) ||
2145 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2146 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2147 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002148}
2149
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002150std::string WebRtcSession::GetSessionErrorMsg() {
2151 std::ostringstream desc;
2152 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2153 desc << kSessionErrorDesc << error_desc() << ".";
2154 return desc.str();
2155}
2156
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002157// We need to check the local/remote description for the Transport instead of
2158// the session, because a new Transport added during renegotiation may have
2159// them unset while the session has them set from the previous negotiation.
2160// Not doing so may trigger the auto generation of transport description and
2161// mess up DTLS identity information, ICE credential, etc.
2162bool WebRtcSession::ReadyToUseRemoteCandidate(
2163 const IceCandidateInterface* candidate,
2164 const SessionDescriptionInterface* remote_desc,
2165 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002166 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002167
2168 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002169 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002170
deadbeefd59daf82015-10-14 15:02:44 -07002171 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002172 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002173 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002174
2175 size_t mediacontent_index =
2176 static_cast<size_t>(candidate->sdp_mline_index());
2177 size_t remote_content_size =
2178 current_remote_desc->description()->contents().size();
2179 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002180 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2181 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002182
2183 *valid = false;
2184 return false;
2185 }
2186
2187 cricket::ContentInfo content =
2188 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002189
2190 const std::string transport_name = GetTransportName(content.name);
2191 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002192 return false;
2193 }
zhihuang9763d562016-08-05 11:14:50 -07002194 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002195}
2196
deadbeef7af91dd2016-12-13 11:29:11 -08002197bool WebRtcSession::SrtpRequired() const {
2198 return dtls_enabled_ ||
2199 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2200}
2201
deadbeefcbecd352015-09-23 11:50:27 -07002202void WebRtcSession::OnTransportControllerGatheringState(
2203 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002204 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002205 if (state == cricket::kIceGatheringGathering) {
2206 if (ice_observer_) {
2207 ice_observer_->OnIceGatheringChange(
2208 PeerConnectionInterface::kIceGatheringGathering);
2209 }
2210 } else if (state == cricket::kIceGatheringComplete) {
2211 if (ice_observer_) {
2212 ice_observer_->OnIceGatheringChange(
2213 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002214 }
2215 }
2216}
2217
2218void WebRtcSession::ReportTransportStats() {
2219 // Use a set so we don't report the same stats twice if two channels share
2220 // a transport.
2221 std::set<std::string> transport_names;
2222 if (voice_channel()) {
2223 transport_names.insert(voice_channel()->transport_name());
2224 }
2225 if (video_channel()) {
2226 transport_names.insert(video_channel()->transport_name());
2227 }
deadbeef953c2ce2017-01-09 14:53:41 -08002228 if (rtp_data_channel()) {
2229 transport_names.insert(rtp_data_channel()->transport_name());
2230 }
2231 if (sctp_transport_name_) {
2232 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002233 }
2234 for (const auto& name : transport_names) {
2235 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002236 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002237 ReportBestConnectionState(stats);
2238 ReportNegotiatedCiphers(stats);
2239 }
2240 }
2241}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002242// Walk through the ConnectionInfos to gather best connection usage
2243// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002244void WebRtcSession::ReportBestConnectionState(
2245 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002246 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002247 for (cricket::TransportChannelStatsList::const_iterator it =
2248 stats.channel_stats.begin();
2249 it != stats.channel_stats.end(); ++it) {
2250 for (cricket::ConnectionInfos::const_iterator it_info =
2251 it->connection_infos.begin();
2252 it_info != it->connection_infos.end(); ++it_info) {
2253 if (!it_info->best_connection) {
2254 continue;
2255 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002256
2257 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2258 const cricket::Candidate& local = it_info->local_candidate;
2259 const cricket::Candidate& remote = it_info->remote_candidate;
2260
2261 // Increment the counter for IceCandidatePairType.
2262 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2263 (local.type() == RELAY_PORT_TYPE &&
2264 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2265 type = kEnumCounterIceCandidatePairTypeTcp;
2266 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2267 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002268 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002269 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002270 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002271 metrics_observer_->IncrementEnumCounter(
2272 type, GetIceCandidatePairCounter(local, remote),
2273 kIceCandidatePairMax);
2274
2275 // Increment the counter for IP type.
2276 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002277 metrics_observer_->IncrementEnumCounter(
2278 kEnumCounterAddressFamily, kBestConnections_IPv4,
2279 kPeerConnectionAddressFamilyCounter_Max);
2280
2281 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002282 metrics_observer_->IncrementEnumCounter(
2283 kEnumCounterAddressFamily, kBestConnections_IPv6,
2284 kPeerConnectionAddressFamilyCounter_Max);
2285 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002286 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002287 }
2288
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002289 return;
2290 }
2291 }
2292}
2293
jbauchac8869e2015-07-03 01:36:14 -07002294void WebRtcSession::ReportNegotiatedCiphers(
2295 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002296 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002297 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2298 return;
2299 }
2300
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002301 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2302 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2303 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2304 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002305 return;
2306 }
2307
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002308 PeerConnectionEnumCounterType srtp_counter_type;
2309 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002310 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002311 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2312 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002313 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002314 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2315 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002316 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002317 srtp_counter_type = kEnumCounterDataSrtpCipher;
2318 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002319 } else {
2320 RTC_NOTREACHED();
2321 return;
2322 }
2323
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002324 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2325 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2326 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002327 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002328 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2329 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2330 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002331 }
2332}
2333
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002334void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002335 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002336 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002337 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002338}
2339
zhihuang9763d562016-08-05 11:14:50 -07002340const std::string WebRtcSession::GetTransportName(
2341 const std::string& content_name) {
2342 cricket::BaseChannel* channel = GetChannel(content_name);
2343 if (!channel) {
2344#ifdef HAVE_QUIC
2345 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2346 content_name == quic_data_transport_->transport_name()) {
2347 return quic_data_transport_->transport_name();
2348 }
2349#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002350 if (sctp_transport_) {
2351 RTC_DCHECK(sctp_content_name_);
2352 RTC_DCHECK(sctp_transport_name_);
2353 if (content_name == *sctp_content_name_) {
2354 return *sctp_transport_name_;
2355 }
2356 }
zhihuang9763d562016-08-05 11:14:50 -07002357 // Return an empty string if failed to retrieve the transport name.
2358 return "";
2359 }
2360 return channel->transport_name();
2361}
zhihuangd82eee02016-08-26 11:25:05 -07002362
deadbeefac22f702017-01-12 21:59:29 -08002363void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002364 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002365 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002366 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2367}
2368
2369void WebRtcSession::DestroyVideoChannel() {
2370 SignalVideoChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002371 RTC_DCHECK(video_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002372 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002373 transport_name = video_channel_->rtp_dtls_transport()->transport_name();
2374 bool need_to_delete_rtcp = (video_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002375 channel_manager_->DestroyVideoChannel(video_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002376 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002377 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002378 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002379 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002380 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002381 }
2382}
2383
2384void WebRtcSession::DestroyVoiceChannel() {
2385 SignalVoiceChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002386 RTC_DCHECK(voice_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002387 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002388 transport_name = voice_channel_->rtp_dtls_transport()->transport_name();
2389 bool need_to_delete_rtcp = (voice_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002390 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002391 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002392 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002393 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002394 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002395 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002396 }
2397}
2398
2399void WebRtcSession::DestroyDataChannel() {
2400 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002401 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002402 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002403 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2404 bool need_to_delete_rtcp =
2405 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002406 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002407 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002408 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002409 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002410 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002411 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002412 }
2413}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414} // namespace webrtc