blob: c78dbc4da78ecf1d6e546614257e522c3bce6f3e [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"
deadbeef953c2ce2017-01-09 14:53:41 -080026#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000027#include "webrtc/p2p/base/portallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010028#include "webrtc/pc/channel.h"
29#include "webrtc/pc/channelmanager.h"
30#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080031#include "webrtc/pc/sctputils.h"
32#include "webrtc/pc/webrtcsessiondescriptionfactory.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020033#include "webrtc/rtc_base/basictypes.h"
34#include "webrtc/rtc_base/bind.h"
35#include "webrtc/rtc_base/checks.h"
36#include "webrtc/rtc_base/helpers.h"
37#include "webrtc/rtc_base/logging.h"
38#include "webrtc/rtc_base/stringencode.h"
39#include "webrtc/rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
zhihuang9763d562016-08-05 11:14:50 -070041#ifdef HAVE_QUIC
42#include "webrtc/p2p/quic/quictransportchannel.h"
43#endif // HAVE_QUIC
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045using cricket::ContentInfo;
46using cricket::ContentInfos;
47using cricket::MediaContentDescription;
48using cricket::SessionDescription;
49using cricket::TransportInfo;
50
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070051using cricket::LOCAL_PORT_TYPE;
52using cricket::STUN_PORT_TYPE;
53using cricket::RELAY_PORT_TYPE;
54using cricket::PRFLX_PORT_TYPE;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace webrtc {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000059const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
60 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062const char kInvalidCandidates[] = "Description contains invalid candidates.";
63const char kInvalidSdp[] = "Invalid session description.";
64const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
66const char kPushDownTDFailed[] =
67 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000068const char kSdpWithoutDtlsFingerprint[] =
69 "Called with SDP without DTLS fingerprint.";
70const char kSdpWithoutSdesCrypto[] =
71 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000072const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000073 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080076const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000077 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080078const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070080const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070082IceCandidatePairType GetIceCandidatePairCounter(
83 const cricket::Candidate& local,
84 const cricket::Candidate& remote) {
85 const auto& l = local.type();
86 const auto& r = remote.type();
87 const auto& host = LOCAL_PORT_TYPE;
88 const auto& srflx = STUN_PORT_TYPE;
89 const auto& relay = RELAY_PORT_TYPE;
90 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070091 if (l == host && r == host) {
92 bool local_private = IPIsPrivate(local.address().ipaddr());
93 bool remote_private = IPIsPrivate(remote.address().ipaddr());
94 if (local_private) {
95 if (remote_private) {
96 return kIceCandidatePairHostPrivateHostPrivate;
97 } else {
98 return kIceCandidatePairHostPrivateHostPublic;
99 }
100 } else {
101 if (remote_private) {
102 return kIceCandidatePairHostPublicHostPrivate;
103 } else {
104 return kIceCandidatePairHostPublicHostPublic;
105 }
106 }
107 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700108 if (l == host && r == srflx)
109 return kIceCandidatePairHostSrflx;
110 if (l == host && r == relay)
111 return kIceCandidatePairHostRelay;
112 if (l == host && r == prflx)
113 return kIceCandidatePairHostPrflx;
114 if (l == srflx && r == host)
115 return kIceCandidatePairSrflxHost;
116 if (l == srflx && r == srflx)
117 return kIceCandidatePairSrflxSrflx;
118 if (l == srflx && r == relay)
119 return kIceCandidatePairSrflxRelay;
120 if (l == srflx && r == prflx)
121 return kIceCandidatePairSrflxPrflx;
122 if (l == relay && r == host)
123 return kIceCandidatePairRelayHost;
124 if (l == relay && r == srflx)
125 return kIceCandidatePairRelaySrflx;
126 if (l == relay && r == relay)
127 return kIceCandidatePairRelayRelay;
128 if (l == relay && r == prflx)
129 return kIceCandidatePairRelayPrflx;
130 if (l == prflx && r == host)
131 return kIceCandidatePairPrflxHost;
132 if (l == prflx && r == srflx)
133 return kIceCandidatePairPrflxSrflx;
134 if (l == prflx && r == relay)
135 return kIceCandidatePairPrflxRelay;
136 return kIceCandidatePairMax;
137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139// Compares |answer| against |offer|. Comparision is done
140// for number of m-lines in answer against offer. If matches true will be
141// returned otherwise false.
142static bool VerifyMediaDescriptions(
143 const SessionDescription* answer, const SessionDescription* offer) {
144 if (offer->contents().size() != answer->contents().size())
145 return false;
146
147 for (size_t i = 0; i < offer->contents().size(); ++i) {
148 if ((offer->contents()[i].name) != answer->contents()[i].name) {
149 return false;
150 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000151 const MediaContentDescription* offer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 offer->contents()[i].description);
154 const MediaContentDescription* answer_mdesc =
155 static_cast<const MediaContentDescription*>(
156 answer->contents()[i].description);
157 if (offer_mdesc->type() != answer_mdesc->type()) {
158 return false;
159 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 }
161 return true;
162}
163
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800165// fingerprint, unless it's in a BUNDLE group, in which case only the
166// BUNDLE-tag section (first media section/description in the BUNDLE group)
167// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
168// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
169// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000170static bool VerifyCrypto(const SessionDescription* desc,
171 bool dtls_enabled,
172 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800173 const cricket::ContentGroup* bundle =
174 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 const ContentInfos& contents = desc->contents();
176 for (size_t index = 0; index < contents.size(); ++index) {
177 const ContentInfo* cinfo = &contents[index];
178 if (cinfo->rejected) {
179 continue;
180 }
deadbeefb7892532017-02-22 19:35:18 -0800181 if (bundle && bundle->HasContentName(cinfo->name) &&
182 cinfo->name != *(bundle->FirstContentName())) {
183 // This isn't the first media section in the BUNDLE group, so it's not
184 // required to have crypto attributes, since only the crypto attributes
185 // from the first section actually get used.
186 continue;
187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
deadbeefb7892532017-02-22 19:35:18 -0800189 // If the content isn't rejected or bundled into another m= section, crypto
190 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 const MediaContentDescription* media =
192 static_cast<const MediaContentDescription*>(cinfo->description);
193 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
194 if (!media || !tinfo) {
195 // Something is not right.
196 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return false;
199 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 LOG(LS_WARNING) <<
203 "Session description must have DTLS fingerprint if DTLS enabled.";
204 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000205 return false;
206 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000207 } else {
208 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 LOG(LS_WARNING) <<
210 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000211 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000212 return false;
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215 }
216
217 return true;
218}
219
deadbeefb7892532017-02-22 19:35:18 -0800220// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
221// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
222// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800224 const cricket::ContentGroup* bundle =
225 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000226 const ContentInfos& contents = desc->contents();
227 for (size_t index = 0; index < contents.size(); ++index) {
228 const ContentInfo* cinfo = &contents[index];
229 if (cinfo->rejected) {
230 continue;
231 }
deadbeefb7892532017-02-22 19:35:18 -0800232 if (bundle && bundle->HasContentName(cinfo->name) &&
233 cinfo->name != *(bundle->FirstContentName())) {
234 // This isn't the first media section in the BUNDLE group, so it's not
235 // required to have ufrag/password, since only the ufrag/password from
236 // the first section actually get used.
237 continue;
238 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000239
deadbeefb7892532017-02-22 19:35:18 -0800240 // If the content isn't rejected or bundled into another m= section,
241 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000242 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
243 if (!tinfo) {
244 // Something is not right.
245 LOG(LS_ERROR) << kInvalidSdp;
246 return false;
247 }
248 if (tinfo->description.ice_ufrag.empty() ||
249 tinfo->description.ice_pwd.empty()) {
250 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
251 return false;
252 }
253 }
254 return true;
255}
256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 uint32_t ssrc,
259 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800260 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 const cricket::ContentInfo* audio_info =
263 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000264 if (audio_info) {
265 const cricket::MediaContentDescription* audio_content =
266 static_cast<const cricket::MediaContentDescription*>(
267 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000269 const auto* found =
270 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
271 if (found) {
272 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000273 return true;
274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 }
276
277 const cricket::ContentInfo* video_info =
278 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000279 if (video_info) {
280 const cricket::MediaContentDescription* video_content =
281 static_cast<const cricket::MediaContentDescription*>(
282 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const auto* found =
285 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
286 if (found) {
287 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000288 return true;
289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 }
291 return false;
292}
293
deadbeef953c2ce2017-01-09 14:53:41 -0800294// Get the SCTP port out of a SessionDescription.
295// Return -1 if not found.
296static int GetSctpPort(const SessionDescription* session_description) {
297 const ContentInfo* content_info = GetFirstDataContent(session_description);
298 RTC_DCHECK(content_info);
299 if (!content_info) {
300 return -1;
301 }
302 const cricket::DataContentDescription* data =
303 static_cast<const cricket::DataContentDescription*>(
304 (content_info->description));
305 std::string value;
306 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
307 cricket::kGoogleSctpDataCodecName);
308 for (const cricket::DataCodec& codec : data->codecs()) {
309 if (!codec.Matches(match_pattern)) {
310 continue;
311 }
312 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
313 return rtc::FromString<int>(value);
314 }
315 }
316 return -1;
317}
318
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319static bool BadSdp(const std::string& source,
320 const std::string& type,
321 const std::string& reason,
322 std::string* err_desc) {
323 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700324 desc << "Failed to set " << source;
325 if (!type.empty()) {
326 desc << " " << type;
327 }
328 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000329
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 return false;
335}
336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338 const std::string& type,
339 const std::string& reason,
340 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
346}
347
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348static bool BadLocalSdp(const std::string& type,
349 const std::string& reason,
350 std::string* err_desc) {
351 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
352}
353
354static bool BadRemoteSdp(const std::string& type,
355 const std::string& reason,
356 std::string* err_desc) {
357 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
358}
359
360static bool BadOfferSdp(cricket::ContentSource source,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
364}
365
366static bool BadPranswerSdp(cricket::ContentSource source,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
370 reason, err_desc);
371}
372
373static bool BadAnswerSdp(cricket::ContentSource source,
374 const std::string& reason,
375 std::string* err_desc) {
376 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377}
378
deadbeefd59daf82015-10-14 15:02:44 -0700379#define GET_STRING_OF_STATE(state) \
380 case webrtc::WebRtcSession::state: \
381 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 break;
383
deadbeefd59daf82015-10-14 15:02:44 -0700384static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 std::string result;
386 switch (state) {
387 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_SENTOFFER)
389 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
390 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
391 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700393 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 default:
nissec80e7412017-01-11 05:56:46 -0800395 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 break;
397 }
398 return result;
399}
400
deadbeefd59daf82015-10-14 15:02:44 -0700401#define GET_STRING_OF_ERROR_CODE(err) \
402 case webrtc::WebRtcSession::err: \
403 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 break;
405
deadbeefd59daf82015-10-14 15:02:44 -0700406static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 std::string result;
408 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000409 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
411 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800413 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 break;
415 }
416 return result;
417}
418
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419static std::string MakeErrorString(const std::string& error,
420 const std::string& desc) {
421 std::ostringstream ret;
422 ret << error << " " << desc;
423 return ret.str();
424}
425
426static std::string MakeTdErrorString(const std::string& desc) {
427 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
deadbeef0ed85b22016-02-23 17:24:52 -0800430// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
431bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
432 const SessionDescriptionInterface* new_desc,
433 const std::string& content_name) {
434 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700435 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
deadbeef0ed85b22016-02-23 17:24:52 -0800437 const SessionDescription* new_sd = new_desc->description();
438 const SessionDescription* old_sd = old_desc->description();
439 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
440 if (!cinfo || cinfo->rejected) {
441 return false;
442 }
443 // If the content isn't rejected, check if ufrag and password has changed.
444 const cricket::TransportDescription* new_transport_desc =
445 new_sd->GetTransportDescriptionByName(content_name);
446 const cricket::TransportDescription* old_transport_desc =
447 old_sd->GetTransportDescriptionByName(content_name);
448 if (!new_transport_desc || !old_transport_desc) {
449 // No transport description exists. This is not an ICE restart.
450 return false;
451 }
452 if (cricket::IceCredentialsChanged(
453 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
454 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
455 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
456 << ".";
457 return true;
458 }
459 return false;
460}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
zhihuang29ff8442016-07-27 11:07:25 -0700462WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700463 Call* call,
464 cricket::ChannelManager* channel_manager,
465 const cricket::MediaConfig& media_config,
466 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700467 rtc::Thread* network_thread,
468 rtc::Thread* worker_thread,
469 rtc::Thread* signaling_thread,
470 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800471 std::unique_ptr<cricket::TransportController> transport_controller,
472 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700473 : network_thread_(network_thread),
474 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700475 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // RFC 3264: The numeric value of the session id and version in the
477 // o line MUST be representable with a "64 bit signed integer".
478 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700479 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700480 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800481 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700482 media_config_(media_config),
483 event_log_(event_log),
484 call_(call),
485 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 ice_observer_(NULL),
487 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700488 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000490 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000492 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700493 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
494 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700495 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700496 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700497 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700498 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700499 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700500 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700501 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700502 transport_controller_->SignalCandidatesRemoved.connect(
503 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700504 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800505 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506}
507
508WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800509 RTC_DCHECK(signaling_thread()->IsCurrent());
Steve Anton169629a2017-08-30 17:36:36 -0700510 // Destroy video channels first since they may have a pointer to a voice
511 // channel.
512 for (auto* channel : video_channels_) {
513 DestroyVideoChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 }
Steve Anton169629a2017-08-30 17:36:36 -0700515 for (auto* channel : voice_channels_) {
516 DestroyVoiceChannel(channel);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000517 }
deadbeef953c2ce2017-01-09 14:53:41 -0800518 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800519 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800520 }
521 if (sctp_transport_) {
522 SignalDataChannelDestroyed();
523 network_thread_->Invoke<void>(
524 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
zhihuang9763d562016-08-05 11:14:50 -0700526#ifdef HAVE_QUIC
527 if (quic_data_transport_) {
528 quic_data_transport_.reset();
529 }
530#endif
deadbeefd59daf82015-10-14 15:02:44 -0700531
532 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533}
534
wu@webrtc.org91053e72013-08-10 07:18:04 +0000535bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200537 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200538 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
539 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700540 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700541 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000542
Henrik Boström87713d02015-08-25 09:53:21 +0200543 // Obtain a certificate from RTCConfiguration if any were provided (optional).
544 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
545 if (!rtc_configuration.certificates.empty()) {
546 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
547 // just picking the first one. The decision should be made based on the DTLS
548 // handshake. The DTLS negotiations need to know about all certificates.
549 certificate = rtc_configuration.certificates[0];
550 }
551
honghaiz1f429e32015-09-28 07:57:34 -0700552 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700553
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000554 if (options.disable_encryption) {
555 dtls_enabled_ = false;
556 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200557 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200558 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800559 // |rtc_configuration| can override the default |dtls_enabled_| value.
560 if (rtc_configuration.enable_dtls_srtp) {
561 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000562 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000563 }
564
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000566 // It takes precendence over the disable_sctp_data_channels
567 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800568 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700570 }
571#ifdef HAVE_QUIC
572 else if (rtc_configuration.enable_quic) {
573 // Use QUIC instead of DTLS when |enable_quic| is true.
574 data_channel_type_ = cricket::DCT_QUIC;
575 transport_controller_->use_quic();
576 if (dtls_enabled_) {
577 LOG(LS_INFO) << "Using QUIC instead of DTLS";
578 }
579 quic_data_transport_.reset(
580 new QuicDataTransport(signaling_thread(), worker_thread(),
581 network_thread(), transport_controller_.get()));
582 }
583#endif // HAVE_QUIC
584 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000585 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000586 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000587 data_channel_type_ = cricket::DCT_SCTP;
588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
htaa2a49d92016-03-04 02:51:39 -0800591 video_options_.screencast_min_bitrate_kbps =
592 rtc_configuration.screencast_min_bitrate;
593 audio_options_.combined_audio_video_bwe =
594 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000595
kwiberg102c6a62015-10-30 02:47:38 -0700596 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100597 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200598
Karl Wibergbe579832015-11-10 22:34:18 +0100599 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
600 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200601
Henrik Boström87713d02015-08-25 09:53:21 +0200602 if (!dtls_enabled_) {
603 // Construct with DTLS disabled.
604 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200605 signaling_thread(), channel_manager_, this, id(),
606 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200607 } else {
608 // Construct with DTLS enabled.
609 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200610 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200611 signaling_thread(), channel_manager_, this, id(),
612 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200613 } else {
614 // Use the already generated certificate.
615 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200616 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200617 }
618 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000619
Henrik Boströmd8281982015-08-27 10:12:24 +0200620 webrtc_session_desc_factory_->SignalCertificateReady.connect(
621 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000622
wu@webrtc.org97077a32013-10-25 21:18:33 +0000623 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000625 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700626
jbauch5869f502017-06-29 12:31:36 -0700627 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
628 options.crypto_options.enable_encrypted_rtp_header_extensions);
629
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 return true;
631}
632
deadbeefd59daf82015-10-14 15:02:44 -0700633void WebRtcSession::Close() {
634 SetState(STATE_CLOSED);
635 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700636 call_ = nullptr;
Steve Anton169629a2017-08-30 17:36:36 -0700637 RTC_DCHECK(voice_channels_.empty());
638 RTC_DCHECK(video_channels_.empty());
deadbeef953c2ce2017-01-09 14:53:41 -0800639 RTC_DCHECK(!rtp_data_channel_);
640 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641}
642
deadbeef0ed85b22016-02-23 17:24:52 -0800643cricket::BaseChannel* WebRtcSession::GetChannel(
644 const std::string& content_name) {
645 if (voice_channel() && voice_channel()->content_name() == content_name) {
646 return voice_channel();
647 }
648 if (video_channel() && video_channel()->content_name() == content_name) {
649 return video_channel();
650 }
deadbeef953c2ce2017-01-09 14:53:41 -0800651 if (rtp_data_channel() &&
652 rtp_data_channel()->content_name() == content_name) {
653 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800654 }
655 return nullptr;
656}
657
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000658cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
659 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660}
661
deadbeef953c2ce2017-01-09 14:53:41 -0800662bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
663 if (!local_description() || !remote_description()) {
664 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
665 << "SSL Role of the SCTP transport.";
666 return false;
667 }
668 if (!sctp_transport_) {
669 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
670 << "SSL Role of the SCTP transport.";
671 return false;
672 }
673
674 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
675}
676
677bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800678 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800679 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800680 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000681 << "SSL Role of the session.";
682 return false;
683 }
684
deadbeef953c2ce2017-01-09 14:53:41 -0800685 return transport_controller_->GetSslRole(GetTransportName(content_name),
686 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000687}
688
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000689void WebRtcSession::CreateOffer(
690 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700691 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
692 const cricket::MediaSessionOptions& session_options) {
693 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000694}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695
deadbeefab9b2d12015-10-14 11:33:11 -0700696void WebRtcSession::CreateAnswer(
697 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700698 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800699 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700}
701
702bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
703 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800704 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700705
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000706 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700707 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000708
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000709 // Validate SDP.
710 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
711 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
deadbeefd59daf82015-10-14 15:02:44 -0700714 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000715 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700717 initial_offerer_ = true;
718 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
deadbeeffe4a8a42016-12-20 17:56:17 -0800721 if (action == kAnswer) {
722 current_local_description_.reset(desc_temp.release());
723 pending_local_description_.reset(nullptr);
724 current_remote_description_.reset(pending_remote_description_.release());
725 } else {
726 pending_local_description_.reset(desc_temp.release());
727 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728
729 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800730 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // TODO(mallinath) - Handle CreateChannel failure, as new local description
732 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000733 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 }
735
deadbeefcbecd352015-09-23 11:50:27 -0700736 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800737 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000739 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 return false;
741 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800742 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700743 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800744 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
deadbeef0ed85b22016-02-23 17:24:52 -0800747 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700748 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000749 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
751 return true;
752}
753
754bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
755 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800756 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700757
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000758 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700759 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000760
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000761 // Validate SDP.
762 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
763 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
deadbeeffe4a8a42016-12-20 17:56:17 -0800766 const SessionDescriptionInterface* old_remote_description =
767 remote_description();
768 // Grab ownership of the description being replaced for the remainder of this
769 // method, since it's used below.
770 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
771 Action action = GetAction(desc->type());
772 if (action == kAnswer) {
773 replaced_remote_description.reset(
774 pending_remote_description_ ? pending_remote_description_.release()
775 : current_remote_description_.release());
776 current_remote_description_.reset(desc_temp.release());
777 pending_remote_description_.reset(nullptr);
778 current_local_description_.reset(pending_local_description_.release());
779 } else {
780 replaced_remote_description.reset(pending_remote_description_.release());
781 pending_remote_description_.reset(desc_temp.release());
782 }
deadbeefd59daf82015-10-14 15:02:44 -0700783
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 // Transport and Media channels will be created only when offer is set.
785 if (action == kOffer && !CreateChannels(desc->description())) {
786 // TODO(mallinath) - Handle CreateChannel failure, as new local description
787 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000788 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 }
790
deadbeefcbecd352015-09-23 11:50:27 -0700791 // Remove unused channels if MediaContentDescription is rejected.
792 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
794 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
795 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000796 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return false;
798 }
799
deadbeeffe4a8a42016-12-20 17:56:17 -0800800 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000801 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
deadbeeffe4a8a42016-12-20 17:56:17 -0800804 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800805 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800806 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800807 // Check if this new SessionDescription contains new ICE ufrag and
808 // password that indicates the remote peer requests an ICE restart.
809 // TODO(deadbeef): When we start storing both the current and pending
810 // remote description, this should reset pending_ice_restarts and compare
811 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800812 if (CheckForRemoteIceRestart(old_remote_description, desc,
813 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800814 if (action == kOffer) {
815 pending_ice_restarts_.insert(content.name);
816 }
817 } else {
818 // We retain all received candidates only if ICE is not restarted.
819 // When ICE is restarted, all previous candidates belong to an old
820 // generation and should not be kept.
821 // TODO(deadbeef): This goes against the W3C spec which says the remote
822 // description should only contain candidates from the last set remote
823 // description plus any candidates added since then. We should remove
824 // this once we're sure it won't break anything.
825 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800826 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800827 }
828 }
honghaiz503726c2015-07-31 12:37:38 -0700829 }
830
deadbeefd59daf82015-10-14 15:02:44 -0700831 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000832 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000834
835 // Set the the ICE connection state to connecting since the connection may
836 // become writable with peer reflexive candidates before any remote candidate
837 // is signaled.
838 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
839 // is to have a new signal the indicates a change in checking state from the
840 // transport and expose a new checking() member from transport that can be
841 // read to determine the current checking state. The existing SignalConnecting
842 // actually means "gathering candidates", so cannot be be used here.
843 if (desc->type() != SessionDescriptionInterface::kOffer &&
844 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
845 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
846 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 return true;
848}
849
Steve Anton18ee1d52017-09-11 11:32:35 -0700850// TODO(steveanton): Eventually it'd be nice to store the channels as a single
851// vector of BaseChannel pointers instead of separate voice and video channel
852// vectors. At that point, this will become a simple getter.
853std::vector<cricket::BaseChannel*> WebRtcSession::Channels() const {
854 std::vector<cricket::BaseChannel*> channels;
855 channels.insert(channels.end(), voice_channels_.begin(),
856 voice_channels_.end());
857 channels.insert(channels.end(), video_channels_.begin(),
858 video_channels_.end());
859 if (rtp_data_channel_) {
860 channels.push_back(rtp_data_channel_.get());
861 }
862 return channels;
863}
864
deadbeefd59daf82015-10-14 15:02:44 -0700865void WebRtcSession::LogState(State old_state, State new_state) {
866 LOG(LS_INFO) << "Session:" << id()
867 << " Old state:" << GetStateString(old_state)
868 << " New state:" << GetStateString(new_state);
869}
870
871void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800872 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700873 if (state != state_) {
874 LogState(state_, state);
875 state_ = state;
876 SignalState(this, state_);
877 }
878}
879
880void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800881 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700882 if (error != error_) {
883 error_ = error;
884 error_desc_ = error_desc;
885 }
886}
887
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888bool WebRtcSession::UpdateSessionState(
889 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800891 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700892
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 // If there's already a pending error then no state transition should happen.
894 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800895 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000898 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
899 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
deadbeefd59daf82015-10-14 15:02:44 -0700901 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
902 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000903 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700904 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000905 }
deadbeefd59daf82015-10-14 15:02:44 -0700906 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000907 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000910 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
911 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
913 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700914 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
915 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000916 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700917 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000918 }
deadbeefd59daf82015-10-14 15:02:44 -0700919 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000920 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
922 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700923 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800924 local_description()->description()->GetGroupByName(
925 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700926 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800927 remote_description()->description()->GetGroupByName(
928 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700929 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800930 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700931 const cricket::ContentGroup* answer_bundle =
932 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
933 if (!EnableBundle(*answer_bundle)) {
934 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
935 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
936 }
937 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800938 // Only push down the transport description after enabling BUNDLE; we don't
939 // want to push down a description on a transport about to be destroyed.
940 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
941 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
942 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700944 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000945 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700946 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000947 }
deadbeefd59daf82015-10-14 15:02:44 -0700948 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000949 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 }
951 }
952 return true;
953}
954
955WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
956 if (type == SessionDescriptionInterface::kOffer) {
957 return WebRtcSession::kOffer;
958 } else if (type == SessionDescriptionInterface::kPrAnswer) {
959 return WebRtcSession::kPrAnswer;
960 } else if (type == SessionDescriptionInterface::kAnswer) {
961 return WebRtcSession::kAnswer;
962 }
nisseede5da42017-01-12 05:15:36 -0800963 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 return WebRtcSession::kOffer;
965}
966
deadbeefd59daf82015-10-14 15:02:44 -0700967bool WebRtcSession::PushdownMediaDescription(
968 cricket::ContentAction action,
969 cricket::ContentSource source,
970 std::string* err) {
Steve Anton18ee1d52017-09-11 11:32:35 -0700971 const SessionDescription* sdesc =
972 (source == cricket::CS_LOCAL ? local_description() : remote_description())
973 ->description();
974 RTC_DCHECK(sdesc);
975 bool all_success = true;
976 for (auto* channel : Channels()) {
977 // TODO(steveanton): Add support for multiple channels of the same type.
978 const ContentInfo* content_info =
979 cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type());
980 if (!content_info) {
981 continue;
deadbeefd59daf82015-10-14 15:02:44 -0700982 }
Steve Anton18ee1d52017-09-11 11:32:35 -0700983 const MediaContentDescription* content_desc =
984 static_cast<const MediaContentDescription*>(content_info->description);
985 if (content_desc && !content_info->rejected) {
986 bool success = (source == cricket::CS_LOCAL)
987 ? channel->SetLocalContent(content_desc, action, err)
988 : channel->SetRemoteContent(content_desc, action, err);
989 if (!success) {
990 all_success = false;
991 break;
992 }
993 }
994 }
deadbeef953c2ce2017-01-09 14:53:41 -0800995 // Need complete offer/answer with an SCTP m= section before starting SCTP,
996 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
997 if (sctp_transport_ && local_description() && remote_description() &&
998 cricket::GetFirstDataContent(local_description()->description()) &&
999 cricket::GetFirstDataContent(remote_description()->description())) {
Steve Anton18ee1d52017-09-11 11:32:35 -07001000 all_success &= network_thread_->Invoke<bool>(
deadbeef953c2ce2017-01-09 14:53:41 -08001001 RTC_FROM_HERE,
1002 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
1003 }
Steve Anton18ee1d52017-09-11 11:32:35 -07001004 return all_success;
deadbeef953c2ce2017-01-09 14:53:41 -08001005}
1006
1007bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
1008 RTC_DCHECK(network_thread_->IsCurrent());
1009 RTC_DCHECK(local_description());
1010 RTC_DCHECK(remote_description());
1011 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
1012 // When we support "max-message-size", that would also be pushed down here.
1013 return sctp_transport_->Start(
1014 GetSctpPort(local_description()->description()),
1015 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -07001016}
1017
1018bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1019 cricket::ContentAction action,
1020 std::string* error_desc) {
1021 RTC_DCHECK(signaling_thread()->IsCurrent());
1022
1023 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001024 return PushdownLocalTransportDescription(local_description()->description(),
1025 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001026 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001027 return PushdownRemoteTransportDescription(remote_description()->description(),
1028 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001029}
1030
1031bool WebRtcSession::PushdownLocalTransportDescription(
1032 const SessionDescription* sdesc,
1033 cricket::ContentAction action,
1034 std::string* err) {
1035 RTC_DCHECK(signaling_thread()->IsCurrent());
1036
1037 if (!sdesc) {
1038 return false;
1039 }
1040
1041 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1042 if (!transport_controller_->SetLocalTransportDescription(
1043 tinfo.content_name, tinfo.description, action, err)) {
1044 return false;
1045 }
1046 }
1047
1048 return true;
1049}
1050
1051bool WebRtcSession::PushdownRemoteTransportDescription(
1052 const SessionDescription* sdesc,
1053 cricket::ContentAction action,
1054 std::string* err) {
1055 RTC_DCHECK(signaling_thread()->IsCurrent());
1056
1057 if (!sdesc) {
1058 return false;
1059 }
1060
1061 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1062 if (!transport_controller_->SetRemoteTransportDescription(
1063 tinfo.content_name, tinfo.description, action, err)) {
1064 return false;
1065 }
1066 }
1067
1068 return true;
1069}
1070
1071bool WebRtcSession::GetTransportDescription(
1072 const SessionDescription* description,
1073 const std::string& content_name,
1074 cricket::TransportDescription* tdesc) {
1075 if (!description || !tdesc) {
1076 return false;
1077 }
1078 const TransportInfo* transport_info =
1079 description->GetTransportInfoByName(content_name);
1080 if (!transport_info) {
1081 return false;
1082 }
1083 *tdesc = transport_info->description;
1084 return true;
1085}
1086
deadbeefcbecd352015-09-23 11:50:27 -07001087bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1088 const std::string* first_content_name = bundle.FirstContentName();
1089 if (!first_content_name) {
1090 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1091 return false;
1092 }
1093 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001094
zhihuang9763d562016-08-05 11:14:50 -07001095#ifdef HAVE_QUIC
1096 if (quic_data_transport_ &&
1097 bundle.HasContentName(quic_data_transport_->content_name()) &&
1098 quic_data_transport_->transport_name() != transport_name) {
1099 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1100 << " on " << transport_name << "with QUIC.";
1101 }
1102#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001103 auto maybe_set_transport = [this, bundle,
1104 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001105 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001106 return true;
1107 }
1108
zhihuangf5b251b2017-01-12 19:37:48 -08001109 std::string old_transport_name = ch->transport_name();
1110 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001111 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1112 << " on " << transport_name << ".";
1113 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001114 }
torbjornga81a42f2015-09-23 02:16:58 -07001115
zhihuangb2cdd932017-01-19 16:54:25 -08001116 cricket::DtlsTransportInternal* rtp_dtls_transport =
1117 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001118 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001119 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1120 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001121 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001122 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001123 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1124 }
1125
zhihuangb2cdd932017-01-19 16:54:25 -08001126 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001127 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1128 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001129 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001130 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001131 // If the channel needs rtcp, it means that the channel used to have a
1132 // rtcp transport which needs to be deleted now.
1133 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001134 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001135 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001136 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001137 return true;
1138 };
1139
deadbeefcbecd352015-09-23 11:50:27 -07001140 if (!maybe_set_transport(voice_channel()) ||
1141 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001142 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001143 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001144 }
deadbeef953c2ce2017-01-09 14:53:41 -08001145 // For SCTP, transport creation/deletion happens here instead of in the
1146 // object itself.
1147 if (sctp_transport_) {
1148 RTC_DCHECK(sctp_transport_name_);
1149 RTC_DCHECK(sctp_content_name_);
1150 if (transport_name != *sctp_transport_name_ &&
1151 bundle.HasContentName(*sctp_content_name_)) {
1152 network_thread_->Invoke<void>(
1153 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1154 transport_name));
1155 }
1156 }
deadbeefcbecd352015-09-23 11:50:27 -07001157
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001158 return true;
1159}
1160
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001162 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001163 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1164 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001165 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 }
1167
1168 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001169 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 return false;
1171 }
1172
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001173 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001174 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1175 if (!valid) {
1176 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 }
1178
1179 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001180 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001181 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 return false;
1183 }
1184
deadbeefd59daf82015-10-14 15:02:44 -07001185 if (ready) {
1186 return UseCandidate(candidate);
1187 } else {
1188 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1189 return true;
1190 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191}
1192
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001193bool WebRtcSession::RemoveRemoteIceCandidates(
1194 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001195 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001196 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1197 << "removed without any remote session description.";
1198 return false;
1199 }
1200
1201 if (candidates.empty()) {
1202 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1203 return false;
1204 }
1205
deadbeeffe4a8a42016-12-20 17:56:17 -08001206 size_t number_removed =
1207 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001208 if (number_removed != candidates.size()) {
1209 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1210 << "Requested " << candidates.size() << " but only "
1211 << number_removed << " are removed.";
1212 }
1213
1214 // Remove the candidates from the transport controller.
1215 std::string error;
1216 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1217 if (!res && !error.empty()) {
1218 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1219 }
1220 return true;
1221}
1222
deadbeefd59daf82015-10-14 15:02:44 -07001223cricket::IceConfig WebRtcSession::ParseIceConfig(
1224 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001225 cricket::ContinualGatheringPolicy gathering_policy;
1226 // TODO(honghaiz): Add the third continual gathering policy in
1227 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1228 switch (config.continual_gathering_policy) {
1229 case PeerConnectionInterface::GATHER_ONCE:
1230 gathering_policy = cricket::GATHER_ONCE;
1231 break;
1232 case PeerConnectionInterface::GATHER_CONTINUALLY:
1233 gathering_policy = cricket::GATHER_CONTINUALLY;
1234 break;
1235 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001236 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001237 gathering_policy = cricket::GATHER_ONCE;
1238 }
deadbeefd59daf82015-10-14 15:02:44 -07001239 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001240 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001241 ice_config.prioritize_most_likely_candidate_pairs =
1242 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001243 ice_config.backup_connection_ping_interval =
1244 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001245 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001246 ice_config.presume_writable_when_fully_relayed =
1247 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001248 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001249 ice_config.regather_all_networks_interval_range =
1250 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001251 return ice_config;
1252}
1253
1254void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1255 transport_controller_->SetIceConfig(config);
1256}
1257
1258void WebRtcSession::MaybeStartGathering() {
1259 transport_controller_->MaybeStartGathering();
1260}
1261
Peter Boström0c4e06b2015-10-07 12:23:21 +02001262bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1263 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001264 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001266 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001267 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1268 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269}
1270
Peter Boström0c4e06b2015-10-07 12:23:21 +02001271bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1272 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001273 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001274 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001275 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001276 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1277 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278}
1279
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001280std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001282 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 return desc.str();
1284}
1285
wu@webrtc.org78187522013-10-07 23:32:02 +00001286bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001287 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001288 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001289 if (!rtp_data_channel_ && !sctp_transport_) {
1290 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1291 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001292 return false;
1293 }
deadbeef953c2ce2017-01-09 14:53:41 -08001294 return rtp_data_channel_
1295 ? rtp_data_channel_->SendData(params, payload, result)
1296 : network_thread_->Invoke<bool>(
1297 RTC_FROM_HERE,
1298 Bind(&cricket::SctpTransportInternal::SendData,
1299 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001300}
1301
1302bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001303 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001304 // Don't log an error here, because DataChannels are expected to call
1305 // ConnectDataChannel in this state. It's the only way to initially tell
1306 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001307 return false;
1308 }
deadbeef953c2ce2017-01-09 14:53:41 -08001309 if (rtp_data_channel_) {
1310 rtp_data_channel_->SignalReadyToSendData.connect(
1311 webrtc_data_channel, &DataChannel::OnChannelReady);
1312 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1313 &DataChannel::OnDataReceived);
1314 } else {
1315 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1316 &DataChannel::OnChannelReady);
1317 SignalSctpDataReceived.connect(webrtc_data_channel,
1318 &DataChannel::OnDataReceived);
1319 SignalSctpStreamClosedRemotely.connect(
1320 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1321 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001322 return true;
1323}
1324
1325void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001326 if (!rtp_data_channel_ && !sctp_transport_) {
1327 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1328 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001329 return;
1330 }
deadbeef953c2ce2017-01-09 14:53:41 -08001331 if (rtp_data_channel_) {
1332 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1333 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1334 } else {
1335 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1336 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1337 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1338 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001339}
1340
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001341void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001342 if (!sctp_transport_) {
1343 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001344 return;
1345 }
deadbeef953c2ce2017-01-09 14:53:41 -08001346 network_thread_->Invoke<void>(
1347 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1348 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001349}
1350
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001351void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001352 if (!sctp_transport_) {
1353 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001354 << "NULL.";
1355 return;
1356 }
deadbeef953c2ce2017-01-09 14:53:41 -08001357 network_thread_->Invoke<void>(
1358 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1359 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001360}
1361
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001362bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001363 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1364 sctp_ready_to_send_data_;
1365}
1366
1367std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001368 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001369 ChannelNamePairs channel_name_pairs;
1370 if (voice_channel()) {
1371 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1372 voice_channel()->content_name(), voice_channel()->transport_name()));
1373 }
1374 if (video_channel()) {
1375 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1376 video_channel()->content_name(), video_channel()->transport_name()));
1377 }
1378 if (rtp_data_channel()) {
1379 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1380 ChannelNamePair(rtp_data_channel()->content_name(),
1381 rtp_data_channel()->transport_name()));
1382 }
1383 if (sctp_transport_) {
1384 RTC_DCHECK(sctp_content_name_);
1385 RTC_DCHECK(sctp_transport_name_);
1386 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1387 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1388 }
1389 return GetStats(channel_name_pairs);
1390}
1391
1392std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1393 const ChannelNamePairs& channel_name_pairs) {
1394 if (network_thread()->IsCurrent()) {
1395 return GetStats_n(channel_name_pairs);
1396 }
1397 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1398 RTC_FROM_HERE,
1399 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1400}
1401
1402bool WebRtcSession::GetLocalCertificate(
1403 const std::string& transport_name,
1404 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1405 return transport_controller_->GetLocalCertificate(transport_name,
1406 certificate);
1407}
1408
1409std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1410 const std::string& transport_name) {
1411 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001412}
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414cricket::DataChannelType WebRtcSession::data_channel_type() const {
1415 return data_channel_type_;
1416}
1417
deadbeef0ed85b22016-02-23 17:24:52 -08001418bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1419 return pending_ice_restarts_.find(content_name) !=
1420 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001421}
1422
deadbeefd1a38b52016-12-10 13:15:33 -08001423void WebRtcSession::SetNeedsIceRestartFlag() {
1424 transport_controller_->SetNeedsIceRestartFlag();
1425}
1426
1427bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1428 return transport_controller_->NeedsIceRestart(content_name);
1429}
1430
Henrik Boströmd8281982015-08-27 10:12:24 +02001431void WebRtcSession::OnCertificateReady(
1432 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001433 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001434}
1435
deadbeef953c2ce2017-01-09 14:53:41 -08001436void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1437 SetError(ERROR_TRANSPORT,
1438 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1439}
1440
Henrik Boströmd8281982015-08-27 10:12:24 +02001441bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001442 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001443}
1444
deadbeefcbecd352015-09-23 11:50:27 -07001445const rtc::scoped_refptr<rtc::RTCCertificate>&
1446WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001447 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001448}
1449
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450void WebRtcSession::SetIceConnectionState(
1451 PeerConnectionInterface::IceConnectionState state) {
1452 if (ice_connection_state_ == state) {
1453 return;
1454 }
1455
deadbeefcbecd352015-09-23 11:50:27 -07001456 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1457 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001458 RTC_DCHECK(ice_connection_state_ !=
1459 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 ice_connection_state_ = state;
1461 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001462 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 }
1464}
1465
deadbeefcbecd352015-09-23 11:50:27 -07001466void WebRtcSession::OnTransportControllerConnectionState(
1467 cricket::IceConnectionState state) {
1468 switch (state) {
1469 case cricket::kIceConnectionConnecting:
1470 // If the current state is Connected or Completed, then there were
1471 // writable channels but now there are not, so the next state must
1472 // be Disconnected.
1473 // kIceConnectionConnecting is currently used as the default,
1474 // un-connected state by the TransportController, so its only use is
1475 // detecting disconnections.
1476 if (ice_connection_state_ ==
1477 PeerConnectionInterface::kIceConnectionConnected ||
1478 ice_connection_state_ ==
1479 PeerConnectionInterface::kIceConnectionCompleted) {
1480 SetIceConnectionState(
1481 PeerConnectionInterface::kIceConnectionDisconnected);
1482 }
torbjornga81a42f2015-09-23 02:16:58 -07001483 break;
deadbeefcbecd352015-09-23 11:50:27 -07001484 case cricket::kIceConnectionFailed:
1485 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1486 break;
1487 case cricket::kIceConnectionConnected:
1488 LOG(LS_INFO) << "Changing to ICE connected state because "
1489 << "all transports are writable.";
1490 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1491 break;
1492 case cricket::kIceConnectionCompleted:
1493 LOG(LS_INFO) << "Changing to ICE completed state because "
1494 << "all transports are complete.";
1495 if (ice_connection_state_ !=
1496 PeerConnectionInterface::kIceConnectionConnected) {
1497 // If jumping directly from "checking" to "connected",
1498 // signal "connected" first.
1499 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1500 }
1501 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1502 if (metrics_observer_) {
1503 ReportTransportStats();
1504 }
1505 break;
1506 default:
nissec80e7412017-01-11 05:56:46 -08001507 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001508 }
deadbeefcbecd352015-09-23 11:50:27 -07001509}
1510
1511void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001512 SetIceConnectionReceiving(receiving);
1513}
1514
1515void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1516 if (ice_connection_receiving_ == receiving) {
1517 return;
1518 }
1519 ice_connection_receiving_ = receiving;
1520 if (ice_observer_) {
1521 ice_observer_->OnIceConnectionReceivingChange(receiving);
1522 }
1523}
1524
deadbeefcbecd352015-09-23 11:50:27 -07001525void WebRtcSession::OnTransportControllerCandidatesGathered(
1526 const std::string& transport_name,
1527 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001528 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001529 int sdp_mline_index;
1530 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1531 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1532 << transport_name << " not found";
1533 return;
1534 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535
deadbeefcbecd352015-09-23 11:50:27 -07001536 for (cricket::Candidates::const_iterator citer = candidates.begin();
1537 citer != candidates.end(); ++citer) {
1538 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001539 std::unique_ptr<JsepIceCandidate> candidate(
1540 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001541 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001542 mutable_local_description()->AddCandidate(candidate.get());
1543 }
1544 if (ice_observer_) {
1545 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001546 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 }
1548}
1549
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001550void WebRtcSession::OnTransportControllerCandidatesRemoved(
1551 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001552 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001553 // Sanity check.
1554 for (const cricket::Candidate& candidate : candidates) {
1555 if (candidate.transport_name().empty()) {
1556 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1557 << "empty content name in candidate "
1558 << candidate.ToString();
1559 return;
1560 }
1561 }
1562
deadbeeffe4a8a42016-12-20 17:56:17 -08001563 if (local_description()) {
1564 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001565 }
1566 if (ice_observer_) {
1567 ice_observer_->OnIceCandidatesRemoved(candidates);
1568 }
1569}
1570
deadbeef953c2ce2017-01-09 14:53:41 -08001571void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1572 rtc::SSLHandshakeError error) {
1573 if (metrics_observer_) {
1574 metrics_observer_->IncrementEnumCounter(
1575 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1576 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1577 }
1578}
1579
Steve Anton169629a2017-08-30 17:36:36 -07001580// Enabling voice and video (and RTP data) channels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581void WebRtcSession::EnableChannels() {
Steve Anton169629a2017-08-30 17:36:36 -07001582 for (cricket::VoiceChannel* voice_channel : voice_channels_) {
1583 if (!voice_channel->enabled()) {
1584 voice_channel->Enable(true);
1585 }
1586 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
Steve Anton169629a2017-08-30 17:36:36 -07001588 for (cricket::VideoChannel* video_channel : video_channels_) {
1589 if (!video_channel->enabled()) {
1590 video_channel->Enable(true);
1591 }
1592 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593
deadbeef953c2ce2017-01-09 14:53:41 -08001594 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1595 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598// Returns the media index for a local ice candidate given the content name.
1599bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1600 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001601 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001603 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604
1605 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001606 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 for (size_t index = 0; index < contents.size(); ++index) {
1608 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001609 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 content_found = true;
1611 break;
1612 }
1613 }
1614 return content_found;
1615}
1616
1617bool WebRtcSession::UseCandidatesInSessionDescription(
1618 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001619 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001621 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001623
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1625 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001626 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001627 const IceCandidateInterface* candidate = candidates->at(n);
1628 bool valid = false;
1629 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1630 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001631 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1632 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001633 }
1634 continue;
1635 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001636 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001637 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 break;
deadbeefd59daf82015-10-14 15:02:44 -07001639 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 }
1641 }
1642 return ret;
1643}
1644
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001645bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001647 size_t remote_content_size =
1648 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001650 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 return false;
1652 }
1653
1654 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001655 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 std::vector<cricket::Candidate> candidates;
1657 candidates.push_back(candidate->candidate());
1658 // Invoking BaseSession method to handle remote candidates.
1659 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001660 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1661 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 // Candidates successfully submitted for checking.
1663 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1664 ice_connection_state_ ==
1665 PeerConnectionInterface::kIceConnectionDisconnected) {
1666 // If state is New, then the session has just gotten its first remote ICE
1667 // candidates, so go to Checking.
1668 // If state is Disconnected, the session is re-using old candidates or
1669 // receiving additional ones, so go to Checking.
1670 // If state is Connected, stay Connected.
1671 // TODO(bemasc): If state is Connected, and the new candidates are for a
1672 // newly added transport, then the state actually _should_ move to
1673 // checking. Add a way to distinguish that case.
1674 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1675 }
1676 // TODO(bemasc): If state is Completed, go back to Connected.
1677 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001678 if (!error.empty()) {
1679 LOG(LS_WARNING) << error;
1680 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 }
1682 return true;
1683}
1684
deadbeefcbecd352015-09-23 11:50:27 -07001685void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001686 // TODO(steveanton): Add support for multiple audio/video channels.
1687 // Destroy video channel first since it may have a pointer to the
1688 // voice channel.
1689 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
1690 if ((!video_info || video_info->rejected) && video_channel()) {
1691 RemoveAndDestroyVideoChannel(video_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 }
1693
Steve Anton169629a2017-08-30 17:36:36 -07001694 const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc);
1695 if ((!voice_info || voice_info->rejected) && voice_channel()) {
1696 RemoveAndDestroyVoiceChannel(voice_channel());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001697 }
1698
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 const cricket::ContentInfo* data_info =
1700 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001701 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001702 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001703 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001704 }
1705 if (sctp_transport_) {
1706 SignalDataChannelDestroyed();
1707 network_thread_->Invoke<void>(
1708 RTC_FROM_HERE,
1709 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001710 }
1711#ifdef HAVE_QUIC
1712 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1713 if (quic_data_transport_) {
1714 quic_data_transport_.reset();
1715 }
1716#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 }
1718}
1719
skvlad6c87a672016-05-17 17:49:52 -07001720// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1721// if the channel is not part of any bundle.
1722const std::string* WebRtcSession::GetBundleTransportName(
1723 const cricket::ContentInfo* content,
1724 const cricket::ContentGroup* bundle) {
1725 if (!bundle) {
1726 return nullptr;
1727 }
1728 const std::string* first_content_name = bundle->FirstContentName();
1729 if (!first_content_name) {
1730 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1731 return nullptr;
1732 }
1733 if (!bundle->HasContentName(content->name)) {
1734 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1735 return nullptr;
1736 }
1737 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1738 return first_content_name;
1739}
1740
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001742 // TODO(steveanton): Add support for multiple audio/video channels.
skvlad6c87a672016-05-17 17:49:52 -07001743 const cricket::ContentGroup* bundle_group = nullptr;
1744 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1745 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1746 if (!bundle_group) {
1747 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1748 return false;
1749 }
1750 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 // Creating the media channels and transport proxies.
1752 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001753 if (voice && !voice->rejected && !voice_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001754 if (!CreateVoiceChannel(voice,
1755 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 LOG(LS_ERROR) << "Failed to create voice channel.";
1757 return false;
1758 }
1759 }
1760
1761 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001762 if (video && !video->rejected && !video_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001763 if (!CreateVideoChannel(video,
1764 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 LOG(LS_ERROR) << "Failed to create video channel.";
1766 return false;
1767 }
1768 }
1769
1770 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001771 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1772 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001773 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 LOG(LS_ERROR) << "Failed to create data channel.";
1775 return false;
1776 }
1777 }
1778
1779 return true;
1780}
1781
skvlad6c87a672016-05-17 17:49:52 -07001782bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1783 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001784 // TODO(steveanton): Check to see if it's safe to create multiple voice
1785 // channels.
1786 RTC_DCHECK(voice_channels_.empty());
1787
skvlad6c87a672016-05-17 17:49:52 -07001788 bool require_rtcp_mux =
1789 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001790
1791 std::string transport_name =
1792 bundle_transport ? *bundle_transport : content->name;
1793
zhihuangb2cdd932017-01-19 16:54:25 -08001794 cricket::DtlsTransportInternal* rtp_dtls_transport =
1795 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001796 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001797 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001798 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001799 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001800 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1801 }
1802
Steve Anton169629a2017-08-30 17:36:36 -07001803 cricket::VoiceChannel* voice_channel = channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001804 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001805 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001806 audio_options_);
1807 if (!voice_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001808 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001809 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001810 if (rtcp_dtls_transport) {
1811 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001812 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001813 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001814 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001815 }
zhihuangf5b251b2017-01-12 19:37:48 -08001816
Steve Anton169629a2017-08-30 17:36:36 -07001817 voice_channels_.push_back(voice_channel);
1818
1819 voice_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001820 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001821 voice_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001822 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001823
Steve Anton169629a2017-08-30 17:36:36 -07001824 // TODO(steveanton): This should signal which voice channel was created since
1825 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001826 SignalVoiceChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001827 voice_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001828 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829}
1830
skvlad6c87a672016-05-17 17:49:52 -07001831bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1832 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001833 // TODO(steveanton): Check to see if it's safe to create multiple video
1834 // channels.
1835 RTC_DCHECK(video_channels_.empty());
1836
skvlad6c87a672016-05-17 17:49:52 -07001837 bool require_rtcp_mux =
1838 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001839
1840 std::string transport_name =
1841 bundle_transport ? *bundle_transport : content->name;
1842
zhihuangb2cdd932017-01-19 16:54:25 -08001843 cricket::DtlsTransportInternal* rtp_dtls_transport =
1844 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001845 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001846 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001847 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001848 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001849 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1850 }
1851
Steve Anton169629a2017-08-30 17:36:36 -07001852 cricket::VideoChannel* video_channel = channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001853 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001854 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001855 video_options_);
zhihuangf5b251b2017-01-12 19:37:48 -08001856
Steve Anton169629a2017-08-30 17:36:36 -07001857 if (!video_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001858 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001859 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001860 if (rtcp_dtls_transport) {
1861 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001862 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001863 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001864 return false;
1865 }
zhihuangf5b251b2017-01-12 19:37:48 -08001866
Steve Anton169629a2017-08-30 17:36:36 -07001867 video_channels_.push_back(video_channel);
1868
1869 video_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001870 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001871 video_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001872 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001873
Steve Anton169629a2017-08-30 17:36:36 -07001874 // TODO(steveanton): This should signal which video channel was created since
1875 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001876 SignalVideoChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001877 video_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001878 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879}
1880
skvlad6c87a672016-05-17 17:49:52 -07001881bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1882 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001883 const std::string transport_name =
1884 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001885#ifdef HAVE_QUIC
1886 if (data_channel_type_ == cricket::DCT_QUIC) {
1887 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001888 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001889 return true;
1890 }
1891#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001892 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001893 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001894 if (!sctp_factory_) {
1895 LOG(LS_ERROR)
1896 << "Trying to create SCTP transport, but didn't compile with "
1897 "SCTP support (HAVE_SCTP)";
1898 return false;
1899 }
1900 if (!network_thread_->Invoke<bool>(
1901 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1902 this, content->name, transport_name))) {
1903 return false;
1904 };
1905 } else {
1906 bool require_rtcp_mux =
1907 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001908
1909 std::string transport_name =
1910 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001911 cricket::DtlsTransportInternal* rtp_dtls_transport =
1912 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001913 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001914 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001915 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001916 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001917 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1918 }
1919
deadbeef953c2ce2017-01-09 14:53:41 -08001920 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001921 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001922 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001923 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001924
deadbeef953c2ce2017-01-09 14:53:41 -08001925 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001926 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001927 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001928 if (rtcp_dtls_transport) {
1929 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001930 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001931 }
deadbeef953c2ce2017-01-09 14:53:41 -08001932 return false;
1933 }
zhihuangf5b251b2017-01-12 19:37:48 -08001934
deadbeefac22f702017-01-12 21:59:29 -08001935 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1936 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001937 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1938 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1939 rtp_data_channel_->SignalSentPacket.connect(this,
1940 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001941 }
1942
deadbeefab9b2d12015-10-14 11:33:11 -07001943 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001944
wu@webrtc.org91053e72013-08-10 07:18:04 +00001945 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946}
1947
stefanf79ade12017-06-02 06:44:03 -07001948Call::Stats WebRtcSession::GetCallStats() {
1949 if (!worker_thread()->IsCurrent()) {
1950 return worker_thread()->Invoke<Call::Stats>(
1951 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1952 }
zhihuang38ede132017-06-15 12:52:32 -07001953 if (!call_)
1954 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001955 return call_->GetStats();
1956}
1957
hbosdf6075a2016-12-19 04:58:02 -08001958std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1959 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001960 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001961 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1962 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1963 &channel_name_pairs.video,
1964 &channel_name_pairs.data }) {
1965 if (*channel_name_pair) {
1966 cricket::TransportStats transport_stats;
1967 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1968 &transport_stats)) {
1969 return nullptr;
1970 }
1971 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1972 (*channel_name_pair)->transport_name;
1973 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1974 std::move(transport_stats);
1975 }
1976 }
1977 return session_stats;
1978}
1979
deadbeef953c2ce2017-01-09 14:53:41 -08001980bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1981 const std::string& transport_name) {
1982 RTC_DCHECK(network_thread_->IsCurrent());
1983 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001984 cricket::DtlsTransportInternal* tc =
1985 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001986 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1987 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1988 RTC_DCHECK(sctp_transport_);
1989 sctp_invoker_.reset(new rtc::AsyncInvoker());
1990 sctp_transport_->SignalReadyToSendData.connect(
1991 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1992 sctp_transport_->SignalDataReceived.connect(
1993 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1994 sctp_transport_->SignalStreamClosedRemotely.connect(
1995 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1996 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1997 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1998 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999}
2000
deadbeef953c2ce2017-01-09 14:53:41 -08002001void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
2002 RTC_DCHECK(network_thread_->IsCurrent());
2003 RTC_DCHECK(sctp_transport_);
2004 RTC_DCHECK(sctp_transport_name_);
2005 std::string old_sctp_transport_name = *sctp_transport_name_;
2006 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08002007 cricket::DtlsTransportInternal* tc =
2008 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002009 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2010 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08002011 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002012 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2013}
2014
2015void WebRtcSession::DestroySctpTransport_n() {
2016 RTC_DCHECK(network_thread_->IsCurrent());
2017 sctp_transport_.reset(nullptr);
2018 sctp_content_name_.reset();
2019 sctp_transport_name_.reset();
2020 sctp_invoker_.reset(nullptr);
2021 sctp_ready_to_send_data_ = false;
2022}
2023
2024void WebRtcSession::OnSctpTransportReadyToSendData_n() {
2025 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2026 RTC_DCHECK(network_thread_->IsCurrent());
2027 sctp_invoker_->AsyncInvoke<void>(
2028 RTC_FROM_HERE, signaling_thread_,
2029 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
2030}
2031
2032void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
2033 RTC_DCHECK(signaling_thread_->IsCurrent());
2034 sctp_ready_to_send_data_ = ready;
2035 SignalSctpReadyToSendData(ready);
2036}
2037
2038void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002039 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002040 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07002041 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08002042 RTC_DCHECK(network_thread_->IsCurrent());
2043 sctp_invoker_->AsyncInvoke<void>(
2044 RTC_FROM_HERE, signaling_thread_,
2045 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2046 payload));
2047}
2048
2049void WebRtcSession::OnSctpTransportDataReceived_s(
2050 const cricket::ReceiveDataParams& params,
2051 const rtc::CopyOnWriteBuffer& payload) {
2052 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002053 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2054 // Received OPEN message; parse and signal that a new data channel should
2055 // be created.
2056 std::string label;
2057 InternalDataChannelInit config;
2058 config.id = params.ssrc;
2059 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2060 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2061 << params.ssrc;
2062 return;
2063 }
2064 config.open_handshake_role = InternalDataChannelInit::kAcker;
2065 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002066 } else {
2067 // Otherwise just forward the signal.
2068 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 }
deadbeef953c2ce2017-01-09 14:53:41 -08002070}
2071
2072void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2073 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2074 RTC_DCHECK(network_thread_->IsCurrent());
2075 sctp_invoker_->AsyncInvoke<void>(
2076 RTC_FROM_HERE, signaling_thread_,
2077 rtc::Bind(&sigslot::signal1<int>::operator(),
2078 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079}
2080
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002081// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002082bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002083 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2084 if (!bundle_enabled)
2085 return true;
2086
2087 const cricket::ContentGroup* bundle_group =
2088 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002089 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002090
2091 const cricket::ContentInfos& contents = desc->contents();
2092 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2093 citer != contents.end(); ++citer) {
2094 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002095 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002096 if (bundle_group->HasContentName(content->name) &&
2097 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2098 if (!HasRtcpMuxEnabled(content))
2099 return false;
2100 }
2101 }
2102 // RTCP-MUX is enabled in all the contents.
2103 return true;
2104}
2105
2106bool WebRtcSession::HasRtcpMuxEnabled(
2107 const cricket::ContentInfo* content) {
2108 const cricket::MediaContentDescription* description =
2109 static_cast<cricket::MediaContentDescription*>(content->description);
2110 return description->rtcp_mux();
2111}
2112
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002113bool WebRtcSession::ValidateSessionDescription(
2114 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002115 cricket::ContentSource source, std::string* err_desc) {
2116 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002117 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002118 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002119 }
2120
2121 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002122 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002123 }
2124
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002125 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002126 Action action = GetAction(sdesc->type());
2127 if (source == cricket::CS_LOCAL) {
2128 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002129 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002130 } else {
2131 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002132 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002133 }
2134
2135 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002136 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002137 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2138 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002139 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002140 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002141 }
2142
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002143 // Verify ice-ufrag and ice-pwd.
2144 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002145 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002146 }
2147
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002148 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002150 }
2151
skvlad6c87a672016-05-17 17:49:52 -07002152 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2153 // m-lines that do not rtcp-mux enabled.
2154
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002155 // Verify m-lines in Answer when compared against Offer.
2156 if (action == kAnswer) {
2157 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002158 (source == cricket::CS_LOCAL) ? remote_description()->description()
2159 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002160 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002161 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002162 }
2163 }
2164
2165 return true;
2166}
2167
2168bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2169 return ((action == kOffer && state() == STATE_INIT) ||
2170 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002171 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002172 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002173 (action == kOffer && state() == STATE_INPROGRESS) ||
2174 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002175 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2176 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2177 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2178 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002179}
2180
2181bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2182 return ((action == kOffer && state() == STATE_INIT) ||
2183 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002184 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002185 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002186 (action == kOffer && state() == STATE_INPROGRESS) ||
2187 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002188 (action == kAnswer && state() == STATE_SENTOFFER) ||
2189 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2190 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2191 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002192}
2193
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002194std::string WebRtcSession::GetSessionErrorMsg() {
2195 std::ostringstream desc;
2196 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2197 desc << kSessionErrorDesc << error_desc() << ".";
2198 return desc.str();
2199}
2200
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002201// We need to check the local/remote description for the Transport instead of
2202// the session, because a new Transport added during renegotiation may have
2203// them unset while the session has them set from the previous negotiation.
2204// Not doing so may trigger the auto generation of transport description and
2205// mess up DTLS identity information, ICE credential, etc.
2206bool WebRtcSession::ReadyToUseRemoteCandidate(
2207 const IceCandidateInterface* candidate,
2208 const SessionDescriptionInterface* remote_desc,
2209 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002210 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002211
2212 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002213 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002214
deadbeefd59daf82015-10-14 15:02:44 -07002215 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002216 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002217 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002218
2219 size_t mediacontent_index =
2220 static_cast<size_t>(candidate->sdp_mline_index());
2221 size_t remote_content_size =
2222 current_remote_desc->description()->contents().size();
2223 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002224 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2225 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002226
2227 *valid = false;
2228 return false;
2229 }
2230
2231 cricket::ContentInfo content =
2232 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002233
2234 const std::string transport_name = GetTransportName(content.name);
2235 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002236 return false;
2237 }
zhihuang9763d562016-08-05 11:14:50 -07002238 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002239}
2240
deadbeef7af91dd2016-12-13 11:29:11 -08002241bool WebRtcSession::SrtpRequired() const {
2242 return dtls_enabled_ ||
2243 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2244}
2245
deadbeefcbecd352015-09-23 11:50:27 -07002246void WebRtcSession::OnTransportControllerGatheringState(
2247 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002248 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002249 if (state == cricket::kIceGatheringGathering) {
2250 if (ice_observer_) {
2251 ice_observer_->OnIceGatheringChange(
2252 PeerConnectionInterface::kIceGatheringGathering);
2253 }
2254 } else if (state == cricket::kIceGatheringComplete) {
2255 if (ice_observer_) {
2256 ice_observer_->OnIceGatheringChange(
2257 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002258 }
2259 }
2260}
2261
2262void WebRtcSession::ReportTransportStats() {
2263 // Use a set so we don't report the same stats twice if two channels share
2264 // a transport.
2265 std::set<std::string> transport_names;
2266 if (voice_channel()) {
2267 transport_names.insert(voice_channel()->transport_name());
2268 }
2269 if (video_channel()) {
2270 transport_names.insert(video_channel()->transport_name());
2271 }
deadbeef953c2ce2017-01-09 14:53:41 -08002272 if (rtp_data_channel()) {
2273 transport_names.insert(rtp_data_channel()->transport_name());
2274 }
2275 if (sctp_transport_name_) {
2276 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002277 }
2278 for (const auto& name : transport_names) {
2279 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002280 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002281 ReportBestConnectionState(stats);
2282 ReportNegotiatedCiphers(stats);
2283 }
2284 }
2285}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002286// Walk through the ConnectionInfos to gather best connection usage
2287// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002288void WebRtcSession::ReportBestConnectionState(
2289 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002290 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002291 for (cricket::TransportChannelStatsList::const_iterator it =
2292 stats.channel_stats.begin();
2293 it != stats.channel_stats.end(); ++it) {
2294 for (cricket::ConnectionInfos::const_iterator it_info =
2295 it->connection_infos.begin();
2296 it_info != it->connection_infos.end(); ++it_info) {
2297 if (!it_info->best_connection) {
2298 continue;
2299 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002300
2301 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2302 const cricket::Candidate& local = it_info->local_candidate;
2303 const cricket::Candidate& remote = it_info->remote_candidate;
2304
2305 // Increment the counter for IceCandidatePairType.
2306 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2307 (local.type() == RELAY_PORT_TYPE &&
2308 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2309 type = kEnumCounterIceCandidatePairTypeTcp;
2310 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2311 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002312 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002313 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002314 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002315 metrics_observer_->IncrementEnumCounter(
2316 type, GetIceCandidatePairCounter(local, remote),
2317 kIceCandidatePairMax);
2318
2319 // Increment the counter for IP type.
2320 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002321 metrics_observer_->IncrementEnumCounter(
2322 kEnumCounterAddressFamily, kBestConnections_IPv4,
2323 kPeerConnectionAddressFamilyCounter_Max);
2324
2325 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002326 metrics_observer_->IncrementEnumCounter(
2327 kEnumCounterAddressFamily, kBestConnections_IPv6,
2328 kPeerConnectionAddressFamilyCounter_Max);
2329 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002330 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002331 }
2332
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002333 return;
2334 }
2335 }
2336}
2337
jbauchac8869e2015-07-03 01:36:14 -07002338void WebRtcSession::ReportNegotiatedCiphers(
2339 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002340 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002341 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2342 return;
2343 }
2344
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002345 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2346 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2347 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2348 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002349 return;
2350 }
2351
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002352 PeerConnectionEnumCounterType srtp_counter_type;
2353 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002354 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002355 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2356 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002357 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002358 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2359 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002360 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002361 srtp_counter_type = kEnumCounterDataSrtpCipher;
2362 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002363 } else {
2364 RTC_NOTREACHED();
2365 return;
2366 }
2367
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002368 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2369 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2370 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002371 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002372 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2373 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2374 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002375 }
2376}
2377
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002378void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002379 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002380 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002381 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002382}
2383
zhihuang9763d562016-08-05 11:14:50 -07002384const std::string WebRtcSession::GetTransportName(
2385 const std::string& content_name) {
2386 cricket::BaseChannel* channel = GetChannel(content_name);
2387 if (!channel) {
2388#ifdef HAVE_QUIC
2389 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2390 content_name == quic_data_transport_->transport_name()) {
2391 return quic_data_transport_->transport_name();
2392 }
2393#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002394 if (sctp_transport_) {
2395 RTC_DCHECK(sctp_content_name_);
2396 RTC_DCHECK(sctp_transport_name_);
2397 if (content_name == *sctp_content_name_) {
2398 return *sctp_transport_name_;
2399 }
2400 }
zhihuang9763d562016-08-05 11:14:50 -07002401 // Return an empty string if failed to retrieve the transport name.
2402 return "";
2403 }
2404 return channel->transport_name();
2405}
zhihuangd82eee02016-08-26 11:25:05 -07002406
deadbeefac22f702017-01-12 21:59:29 -08002407void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002408 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002409 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002410 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2411}
2412
Steve Anton169629a2017-08-30 17:36:36 -07002413void WebRtcSession::RemoveAndDestroyVideoChannel(
2414 cricket::VideoChannel* video_channel) {
2415 auto it =
2416 std::find(video_channels_.begin(), video_channels_.end(), video_channel);
2417 RTC_DCHECK(it != video_channels_.end());
2418 if (it == video_channels_.end()) {
2419 return;
2420 }
2421 video_channels_.erase(it);
2422 DestroyVideoChannel(video_channel);
2423}
2424
2425void WebRtcSession::DestroyVideoChannel(cricket::VideoChannel* video_channel) {
2426 // TODO(steveanton): This should take an identifier for the video channel
2427 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002428 SignalVideoChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002429 RTC_DCHECK(video_channel->rtp_dtls_transport());
2430 const std::string transport_name =
2431 video_channel->rtp_dtls_transport()->transport_name();
2432 const bool need_to_delete_rtcp =
2433 (video_channel->rtcp_dtls_transport() != nullptr);
2434 // The above need to be cached before destroying the video channel so that we
2435 // do not access uninitialized memory.
2436 channel_manager_->DestroyVideoChannel(video_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002437 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002438 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002439 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002440 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002441 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002442 }
2443}
2444
Steve Anton169629a2017-08-30 17:36:36 -07002445void WebRtcSession::RemoveAndDestroyVoiceChannel(
2446 cricket::VoiceChannel* voice_channel) {
2447 auto it =
2448 std::find(voice_channels_.begin(), voice_channels_.end(), voice_channel);
2449 RTC_DCHECK(it != voice_channels_.end());
2450 if (it == voice_channels_.end()) {
2451 return;
2452 }
2453 voice_channels_.erase(it);
2454 DestroyVoiceChannel(voice_channel);
2455}
2456
2457void WebRtcSession::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) {
2458 // TODO(steveanton): This should take an identifier for the voice channel
2459 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002460 SignalVoiceChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002461 RTC_DCHECK(voice_channel->rtp_dtls_transport());
2462 const std::string transport_name =
2463 voice_channel->rtp_dtls_transport()->transport_name();
2464 const bool need_to_delete_rtcp =
2465 (voice_channel->rtcp_dtls_transport() != nullptr);
2466 // The above need to be cached before destroying the video channel so that we
2467 // do not access uninitialized memory.
2468 channel_manager_->DestroyVoiceChannel(voice_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002469 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002470 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002471 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002472 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002473 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002474 }
2475}
2476
2477void WebRtcSession::DestroyDataChannel() {
2478 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002479 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002480 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002481 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2482 bool need_to_delete_rtcp =
2483 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002484 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002485 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002486 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002487 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002488 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002489 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002490 }
2491}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492} // namespace webrtc