blob: 763c19cc160296d99311338694634e7e5faa9c1d [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"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000024#include "webrtc/base/basictypes.h"
zhihuang9763d562016-08-05 11:14:50 -070025#include "webrtc/base/bind.h"
jbauchac8869e2015-07-03 01:36:14 -070026#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/helpers.h"
28#include "webrtc/base/logging.h"
29#include "webrtc/base/stringencode.h"
30#include "webrtc/base/stringutils.h"
ossuf515ab82016-12-07 04:52:58 -080031#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080032#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/videocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080034#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000035#include "webrtc/p2p/base/portallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010036#include "webrtc/pc/channel.h"
37#include "webrtc/pc/channelmanager.h"
38#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080039#include "webrtc/pc/sctputils.h"
40#include "webrtc/pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
zhihuang9763d562016-08-05 11:14:50 -070042#ifdef HAVE_QUIC
43#include "webrtc/p2p/quic/quictransportchannel.h"
44#endif // HAVE_QUIC
45
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046using cricket::ContentInfo;
47using cricket::ContentInfos;
48using cricket::MediaContentDescription;
49using cricket::SessionDescription;
50using cricket::TransportInfo;
51
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070052using cricket::LOCAL_PORT_TYPE;
53using cricket::STUN_PORT_TYPE;
54using cricket::RELAY_PORT_TYPE;
55using cricket::PRFLX_PORT_TYPE;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace webrtc {
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
61 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
65const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
67const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080077const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000078 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080079const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000080 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070081const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070083IceCandidatePairType GetIceCandidatePairCounter(
84 const cricket::Candidate& local,
85 const cricket::Candidate& remote) {
86 const auto& l = local.type();
87 const auto& r = remote.type();
88 const auto& host = LOCAL_PORT_TYPE;
89 const auto& srflx = STUN_PORT_TYPE;
90 const auto& relay = RELAY_PORT_TYPE;
91 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070092 if (l == host && r == host) {
93 bool local_private = IPIsPrivate(local.address().ipaddr());
94 bool remote_private = IPIsPrivate(remote.address().ipaddr());
95 if (local_private) {
96 if (remote_private) {
97 return kIceCandidatePairHostPrivateHostPrivate;
98 } else {
99 return kIceCandidatePairHostPrivateHostPublic;
100 }
101 } else {
102 if (remote_private) {
103 return kIceCandidatePairHostPublicHostPrivate;
104 } else {
105 return kIceCandidatePairHostPublicHostPublic;
106 }
107 }
108 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700109 if (l == host && r == srflx)
110 return kIceCandidatePairHostSrflx;
111 if (l == host && r == relay)
112 return kIceCandidatePairHostRelay;
113 if (l == host && r == prflx)
114 return kIceCandidatePairHostPrflx;
115 if (l == srflx && r == host)
116 return kIceCandidatePairSrflxHost;
117 if (l == srflx && r == srflx)
118 return kIceCandidatePairSrflxSrflx;
119 if (l == srflx && r == relay)
120 return kIceCandidatePairSrflxRelay;
121 if (l == srflx && r == prflx)
122 return kIceCandidatePairSrflxPrflx;
123 if (l == relay && r == host)
124 return kIceCandidatePairRelayHost;
125 if (l == relay && r == srflx)
126 return kIceCandidatePairRelaySrflx;
127 if (l == relay && r == relay)
128 return kIceCandidatePairRelayRelay;
129 if (l == relay && r == prflx)
130 return kIceCandidatePairRelayPrflx;
131 if (l == prflx && r == host)
132 return kIceCandidatePairPrflxHost;
133 if (l == prflx && r == srflx)
134 return kIceCandidatePairPrflxSrflx;
135 if (l == prflx && r == relay)
136 return kIceCandidatePairPrflxRelay;
137 return kIceCandidatePairMax;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140// Compares |answer| against |offer|. Comparision is done
141// for number of m-lines in answer against offer. If matches true will be
142// returned otherwise false.
143static bool VerifyMediaDescriptions(
144 const SessionDescription* answer, const SessionDescription* offer) {
145 if (offer->contents().size() != answer->contents().size())
146 return false;
147
148 for (size_t i = 0; i < offer->contents().size(); ++i) {
149 if ((offer->contents()[i].name) != answer->contents()[i].name) {
150 return false;
151 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000152 const MediaContentDescription* offer_mdesc =
153 static_cast<const MediaContentDescription*>(
154 offer->contents()[i].description);
155 const MediaContentDescription* answer_mdesc =
156 static_cast<const MediaContentDescription*>(
157 answer->contents()[i].description);
158 if (offer_mdesc->type() != answer_mdesc->type()) {
159 return false;
160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162 return true;
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Checks that each non-rejected content has SDES crypto keys or a DTLS
166// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
167// keys, will be caught in Transport negotiation, and backstopped by Channel's
deadbeef7af91dd2016-12-13 11:29:11 -0800168// |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000169static bool VerifyCrypto(const SessionDescription* desc,
170 bool dtls_enabled,
171 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 const ContentInfos& contents = desc->contents();
173 for (size_t index = 0; index < contents.size(); ++index) {
174 const ContentInfo* cinfo = &contents[index];
175 if (cinfo->rejected) {
176 continue;
177 }
178
179 // If the content isn't rejected, crypto must be present.
180 const MediaContentDescription* media =
181 static_cast<const MediaContentDescription*>(cinfo->description);
182 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
183 if (!media || !tinfo) {
184 // Something is not right.
185 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 return false;
188 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000189 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000191 LOG(LS_WARNING) <<
192 "Session description must have DTLS fingerprint if DTLS enabled.";
193 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 return false;
195 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 } else {
197 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 LOG(LS_WARNING) <<
199 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 return false;
202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 }
204 }
205
206 return true;
207}
208
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000209// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
210static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
211 const ContentInfos& contents = desc->contents();
212 for (size_t index = 0; index < contents.size(); ++index) {
213 const ContentInfo* cinfo = &contents[index];
214 if (cinfo->rejected) {
215 continue;
216 }
217
218 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
219 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
220 if (!tinfo) {
221 // Something is not right.
222 LOG(LS_ERROR) << kInvalidSdp;
223 return false;
224 }
225 if (tinfo->description.ice_ufrag.empty() ||
226 tinfo->description.ice_pwd.empty()) {
227 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
228 return false;
229 }
230 }
231 return true;
232}
233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200235 uint32_t ssrc,
236 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800237 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 const cricket::ContentInfo* audio_info =
240 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000241 if (audio_info) {
242 const cricket::MediaContentDescription* audio_content =
243 static_cast<const cricket::MediaContentDescription*>(
244 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000246 const auto* found =
247 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
248 if (found) {
249 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000250 return true;
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 }
253
254 const cricket::ContentInfo* video_info =
255 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000256 if (video_info) {
257 const cricket::MediaContentDescription* video_content =
258 static_cast<const cricket::MediaContentDescription*>(
259 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000261 const auto* found =
262 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
263 if (found) {
264 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000265 return true;
266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 }
268 return false;
269}
270
deadbeef953c2ce2017-01-09 14:53:41 -0800271// Get the SCTP port out of a SessionDescription.
272// Return -1 if not found.
273static int GetSctpPort(const SessionDescription* session_description) {
274 const ContentInfo* content_info = GetFirstDataContent(session_description);
275 RTC_DCHECK(content_info);
276 if (!content_info) {
277 return -1;
278 }
279 const cricket::DataContentDescription* data =
280 static_cast<const cricket::DataContentDescription*>(
281 (content_info->description));
282 std::string value;
283 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
284 cricket::kGoogleSctpDataCodecName);
285 for (const cricket::DataCodec& codec : data->codecs()) {
286 if (!codec.Matches(match_pattern)) {
287 continue;
288 }
289 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
290 return rtc::FromString<int>(value);
291 }
292 }
293 return -1;
294}
295
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000296static bool BadSdp(const std::string& source,
297 const std::string& type,
298 const std::string& reason,
299 std::string* err_desc) {
300 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700301 desc << "Failed to set " << source;
302 if (!type.empty()) {
303 desc << " " << type;
304 }
305 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000308 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000310 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 return false;
312}
313
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000321 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 }
323}
324
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325static bool BadLocalSdp(const std::string& type,
326 const std::string& reason,
327 std::string* err_desc) {
328 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
329}
330
331static bool BadRemoteSdp(const std::string& type,
332 const std::string& reason,
333 std::string* err_desc) {
334 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
335}
336
337static bool BadOfferSdp(cricket::ContentSource source,
338 const std::string& reason,
339 std::string* err_desc) {
340 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
341}
342
343static bool BadPranswerSdp(cricket::ContentSource source,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
347 reason, err_desc);
348}
349
350static bool BadAnswerSdp(cricket::ContentSource source,
351 const std::string& reason,
352 std::string* err_desc) {
353 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354}
355
deadbeefd59daf82015-10-14 15:02:44 -0700356#define GET_STRING_OF_STATE(state) \
357 case webrtc::WebRtcSession::state: \
358 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 break;
360
deadbeefd59daf82015-10-14 15:02:44 -0700361static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 std::string result;
363 switch (state) {
364 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700365 GET_STRING_OF_STATE(STATE_SENTOFFER)
366 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
367 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
368 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700370 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 default:
nissec80e7412017-01-11 05:56:46 -0800372 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 break;
374 }
375 return result;
376}
377
deadbeefd59daf82015-10-14 15:02:44 -0700378#define GET_STRING_OF_ERROR_CODE(err) \
379 case webrtc::WebRtcSession::err: \
380 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 break;
382
deadbeefd59daf82015-10-14 15:02:44 -0700383static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 std::string result;
385 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000386 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000387 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
388 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800390 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 break;
392 }
393 return result;
394}
395
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000396static std::string MakeErrorString(const std::string& error,
397 const std::string& desc) {
398 std::ostringstream ret;
399 ret << error << " " << desc;
400 return ret.str();
401}
402
403static std::string MakeTdErrorString(const std::string& desc) {
404 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
deadbeef0ed85b22016-02-23 17:24:52 -0800407// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
408bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
409 const SessionDescriptionInterface* new_desc,
410 const std::string& content_name) {
411 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700412 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 }
deadbeef0ed85b22016-02-23 17:24:52 -0800414 const SessionDescription* new_sd = new_desc->description();
415 const SessionDescription* old_sd = old_desc->description();
416 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
417 if (!cinfo || cinfo->rejected) {
418 return false;
419 }
420 // If the content isn't rejected, check if ufrag and password has changed.
421 const cricket::TransportDescription* new_transport_desc =
422 new_sd->GetTransportDescriptionByName(content_name);
423 const cricket::TransportDescription* old_transport_desc =
424 old_sd->GetTransportDescriptionByName(content_name);
425 if (!new_transport_desc || !old_transport_desc) {
426 // No transport description exists. This is not an ICE restart.
427 return false;
428 }
429 if (cricket::IceCredentialsChanged(
430 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
431 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
432 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
433 << ".";
434 return true;
435 }
436 return false;
437}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438
zhihuang29ff8442016-07-27 11:07:25 -0700439WebRtcSession::WebRtcSession(
440 webrtc::MediaControllerInterface* media_controller,
441 rtc::Thread* network_thread,
442 rtc::Thread* worker_thread,
443 rtc::Thread* signaling_thread,
444 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800445 std::unique_ptr<cricket::TransportController> transport_controller,
446 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700447 : network_thread_(network_thread),
448 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700449 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 // RFC 3264: The numeric value of the session id and version in the
451 // o line MUST be representable with a "64 bit signed integer".
452 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700453 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700454 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800455 sctp_factory_(std::move(sctp_factory)),
stefanc1aeaf02015-10-15 07:26:07 -0700456 media_controller_(media_controller),
457 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 ice_observer_(NULL),
459 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700460 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000462 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000464 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700465 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
466 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700467 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700468 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700469 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700470 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700471 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700472 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700473 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700474 transport_controller_->SignalCandidatesRemoved.connect(
475 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700476 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800477 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478}
479
480WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800481 RTC_DCHECK(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000482 // Destroy video_channel_ first since it may have a pointer to the
483 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000484 if (video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800485 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000487 if (voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800488 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000489 }
deadbeef953c2ce2017-01-09 14:53:41 -0800490 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800491 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800492 }
493 if (sctp_transport_) {
494 SignalDataChannelDestroyed();
495 network_thread_->Invoke<void>(
496 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
zhihuang9763d562016-08-05 11:14:50 -0700498#ifdef HAVE_QUIC
499 if (quic_data_transport_) {
500 quic_data_transport_.reset();
501 }
502#endif
deadbeefd59daf82015-10-14 15:02:44 -0700503
504 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505}
506
wu@webrtc.org91053e72013-08-10 07:18:04 +0000507bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000508 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200509 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200510 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
511 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700512 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700513 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000514
Henrik Boström87713d02015-08-25 09:53:21 +0200515 // Obtain a certificate from RTCConfiguration if any were provided (optional).
516 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
517 if (!rtc_configuration.certificates.empty()) {
518 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
519 // just picking the first one. The decision should be made based on the DTLS
520 // handshake. The DTLS negotiations need to know about all certificates.
521 certificate = rtc_configuration.certificates[0];
522 }
523
honghaiz1f429e32015-09-28 07:57:34 -0700524 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700525
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000526 if (options.disable_encryption) {
527 dtls_enabled_ = false;
528 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200529 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200530 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800531 // |rtc_configuration| can override the default |dtls_enabled_| value.
532 if (rtc_configuration.enable_dtls_srtp) {
533 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000534 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000535 }
536
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000538 // It takes precendence over the disable_sctp_data_channels
539 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800540 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700542 }
543#ifdef HAVE_QUIC
544 else if (rtc_configuration.enable_quic) {
545 // Use QUIC instead of DTLS when |enable_quic| is true.
546 data_channel_type_ = cricket::DCT_QUIC;
547 transport_controller_->use_quic();
548 if (dtls_enabled_) {
549 LOG(LS_INFO) << "Using QUIC instead of DTLS";
550 }
551 quic_data_transport_.reset(
552 new QuicDataTransport(signaling_thread(), worker_thread(),
553 network_thread(), transport_controller_.get()));
554 }
555#endif // HAVE_QUIC
556 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000557 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000558 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000559 data_channel_type_ = cricket::DCT_SCTP;
560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562
htaa2a49d92016-03-04 02:51:39 -0800563 video_options_.screencast_min_bitrate_kbps =
564 rtc_configuration.screencast_min_bitrate;
565 audio_options_.combined_audio_video_bwe =
566 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000567
kwiberg102c6a62015-10-30 02:47:38 -0700568 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100569 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200570
Karl Wibergbe579832015-11-10 22:34:18 +0100571 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
572 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200573
Henrik Boström87713d02015-08-25 09:53:21 +0200574 if (!dtls_enabled_) {
575 // Construct with DTLS disabled.
576 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200577 signaling_thread(), channel_manager_, this, id(),
578 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200579 } else {
580 // Construct with DTLS enabled.
581 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200582 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200583 signaling_thread(), channel_manager_, this, id(),
584 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200585 } else {
586 // Use the already generated certificate.
587 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200588 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200589 }
590 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000591
Henrik Boströmd8281982015-08-27 10:12:24 +0200592 webrtc_session_desc_factory_->SignalCertificateReady.connect(
593 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000594
wu@webrtc.org97077a32013-10-25 21:18:33 +0000595 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000596 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000597 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700598
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 return true;
600}
601
deadbeefd59daf82015-10-14 15:02:44 -0700602void WebRtcSession::Close() {
603 SetState(STATE_CLOSED);
604 RemoveUnusedChannels(nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800605 RTC_DCHECK(!voice_channel_);
606 RTC_DCHECK(!video_channel_);
607 RTC_DCHECK(!rtp_data_channel_);
608 RTC_DCHECK(!sctp_transport_);
solenberg03d6d572016-03-01 12:42:03 -0800609 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610}
611
deadbeef0ed85b22016-02-23 17:24:52 -0800612cricket::BaseChannel* WebRtcSession::GetChannel(
613 const std::string& content_name) {
614 if (voice_channel() && voice_channel()->content_name() == content_name) {
615 return voice_channel();
616 }
617 if (video_channel() && video_channel()->content_name() == content_name) {
618 return video_channel();
619 }
deadbeef953c2ce2017-01-09 14:53:41 -0800620 if (rtp_data_channel() &&
621 rtp_data_channel()->content_name() == content_name) {
622 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800623 }
624 return nullptr;
625}
626
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000627cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
628 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629}
630
deadbeef953c2ce2017-01-09 14:53:41 -0800631bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
632 if (!local_description() || !remote_description()) {
633 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
634 << "SSL Role of the SCTP transport.";
635 return false;
636 }
637 if (!sctp_transport_) {
638 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
639 << "SSL Role of the SCTP transport.";
640 return false;
641 }
642
643 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
644}
645
646bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800647 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800648 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800649 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000650 << "SSL Role of the session.";
651 return false;
652 }
653
deadbeef953c2ce2017-01-09 14:53:41 -0800654 return transport_controller_->GetSslRole(GetTransportName(content_name),
655 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000656}
657
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000658void WebRtcSession::CreateOffer(
659 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700660 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
661 const cricket::MediaSessionOptions& session_options) {
662 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000663}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664
deadbeefab9b2d12015-10-14 11:33:11 -0700665void WebRtcSession::CreateAnswer(
666 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700667 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800668 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669}
670
671bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
672 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800673 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700674
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000675 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700676 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000677
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000678 // Validate SDP.
679 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
680 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 }
682
deadbeefd59daf82015-10-14 15:02:44 -0700683 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000684 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700686 initial_offerer_ = true;
687 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 }
689
deadbeeffe4a8a42016-12-20 17:56:17 -0800690 if (action == kAnswer) {
691 current_local_description_.reset(desc_temp.release());
692 pending_local_description_.reset(nullptr);
693 current_remote_description_.reset(pending_remote_description_.release());
694 } else {
695 pending_local_description_.reset(desc_temp.release());
696 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697
698 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800699 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 // TODO(mallinath) - Handle CreateChannel failure, as new local description
701 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000702 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 }
704
deadbeefcbecd352015-09-23 11:50:27 -0700705 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800706 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000708 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 return false;
710 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800711 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700712 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800713 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700714 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715
deadbeef0ed85b22016-02-23 17:24:52 -0800716 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700717 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000718 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720 return true;
721}
722
723bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
724 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800725 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700726
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000727 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700728 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000729
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000730 // Validate SDP.
731 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
732 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 }
734
deadbeeffe4a8a42016-12-20 17:56:17 -0800735 const SessionDescriptionInterface* old_remote_description =
736 remote_description();
737 // Grab ownership of the description being replaced for the remainder of this
738 // method, since it's used below.
739 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
740 Action action = GetAction(desc->type());
741 if (action == kAnswer) {
742 replaced_remote_description.reset(
743 pending_remote_description_ ? pending_remote_description_.release()
744 : current_remote_description_.release());
745 current_remote_description_.reset(desc_temp.release());
746 pending_remote_description_.reset(nullptr);
747 current_local_description_.reset(pending_local_description_.release());
748 } else {
749 replaced_remote_description.reset(pending_remote_description_.release());
750 pending_remote_description_.reset(desc_temp.release());
751 }
deadbeefd59daf82015-10-14 15:02:44 -0700752
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 // Transport and Media channels will be created only when offer is set.
754 if (action == kOffer && !CreateChannels(desc->description())) {
755 // TODO(mallinath) - Handle CreateChannel failure, as new local description
756 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000757 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759
deadbeefcbecd352015-09-23 11:50:27 -0700760 // Remove unused channels if MediaContentDescription is rejected.
761 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
763 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
764 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000765 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 return false;
767 }
768
deadbeeffe4a8a42016-12-20 17:56:17 -0800769 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000770 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 }
772
deadbeeffe4a8a42016-12-20 17:56:17 -0800773 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800774 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800775 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800776 // Check if this new SessionDescription contains new ICE ufrag and
777 // password that indicates the remote peer requests an ICE restart.
778 // TODO(deadbeef): When we start storing both the current and pending
779 // remote description, this should reset pending_ice_restarts and compare
780 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800781 if (CheckForRemoteIceRestart(old_remote_description, desc,
782 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800783 if (action == kOffer) {
784 pending_ice_restarts_.insert(content.name);
785 }
786 } else {
787 // We retain all received candidates only if ICE is not restarted.
788 // When ICE is restarted, all previous candidates belong to an old
789 // generation and should not be kept.
790 // TODO(deadbeef): This goes against the W3C spec which says the remote
791 // description should only contain candidates from the last set remote
792 // description plus any candidates added since then. We should remove
793 // this once we're sure it won't break anything.
794 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800795 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800796 }
797 }
honghaiz503726c2015-07-31 12:37:38 -0700798 }
799
deadbeefd59daf82015-10-14 15:02:44 -0700800 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000801 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000803
804 // Set the the ICE connection state to connecting since the connection may
805 // become writable with peer reflexive candidates before any remote candidate
806 // is signaled.
807 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
808 // is to have a new signal the indicates a change in checking state from the
809 // transport and expose a new checking() member from transport that can be
810 // read to determine the current checking state. The existing SignalConnecting
811 // actually means "gathering candidates", so cannot be be used here.
812 if (desc->type() != SessionDescriptionInterface::kOffer &&
813 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
814 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
815 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 return true;
817}
818
deadbeefd59daf82015-10-14 15:02:44 -0700819void WebRtcSession::LogState(State old_state, State new_state) {
820 LOG(LS_INFO) << "Session:" << id()
821 << " Old state:" << GetStateString(old_state)
822 << " New state:" << GetStateString(new_state);
823}
824
825void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800826 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700827 if (state != state_) {
828 LogState(state_, state);
829 state_ = state;
830 SignalState(this, state_);
831 }
832}
833
834void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800835 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700836 if (error != error_) {
837 error_ = error;
838 error_desc_ = error_desc;
839 }
840}
841
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842bool WebRtcSession::UpdateSessionState(
843 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800845 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700846
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 // If there's already a pending error then no state transition should happen.
848 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800849 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000850 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000852 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
853 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
deadbeefd59daf82015-10-14 15:02:44 -0700855 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
856 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000857 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700858 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000859 }
deadbeefd59daf82015-10-14 15:02:44 -0700860 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000861 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000864 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
865 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 }
867 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700868 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
869 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000870 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700871 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000872 }
deadbeefd59daf82015-10-14 15:02:44 -0700873 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 }
876 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700877 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800878 local_description()->description()->GetGroupByName(
879 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700880 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800881 remote_description()->description()->GetGroupByName(
882 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700883 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800884 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700885 const cricket::ContentGroup* answer_bundle =
886 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
887 if (!EnableBundle(*answer_bundle)) {
888 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
889 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
890 }
891 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800892 // Only push down the transport description after enabling BUNDLE; we don't
893 // want to push down a description on a transport about to be destroyed.
894 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
895 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
896 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700898 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000899 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700900 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000901 }
deadbeefd59daf82015-10-14 15:02:44 -0700902 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000903 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
905 }
906 return true;
907}
908
909WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
910 if (type == SessionDescriptionInterface::kOffer) {
911 return WebRtcSession::kOffer;
912 } else if (type == SessionDescriptionInterface::kPrAnswer) {
913 return WebRtcSession::kPrAnswer;
914 } else if (type == SessionDescriptionInterface::kAnswer) {
915 return WebRtcSession::kAnswer;
916 }
nisseede5da42017-01-12 05:15:36 -0800917 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 return WebRtcSession::kOffer;
919}
920
deadbeefd59daf82015-10-14 15:02:44 -0700921bool WebRtcSession::PushdownMediaDescription(
922 cricket::ContentAction action,
923 cricket::ContentSource source,
924 std::string* err) {
925 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
926 if (!ch) {
927 return true;
928 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800929 return ch->PushdownLocalDescription(local_description()->description(),
930 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700931 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800932 return ch->PushdownRemoteDescription(remote_description()->description(),
933 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700934 }
935 };
936
deadbeef953c2ce2017-01-09 14:53:41 -0800937 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
938 set_content(rtp_data_channel()));
939 // Need complete offer/answer with an SCTP m= section before starting SCTP,
940 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
941 if (sctp_transport_ && local_description() && remote_description() &&
942 cricket::GetFirstDataContent(local_description()->description()) &&
943 cricket::GetFirstDataContent(remote_description()->description())) {
944 ret &= network_thread_->Invoke<bool>(
945 RTC_FROM_HERE,
946 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
947 }
948 return ret;
949}
950
951bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
952 RTC_DCHECK(network_thread_->IsCurrent());
953 RTC_DCHECK(local_description());
954 RTC_DCHECK(remote_description());
955 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
956 // When we support "max-message-size", that would also be pushed down here.
957 return sctp_transport_->Start(
958 GetSctpPort(local_description()->description()),
959 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700960}
961
962bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
963 cricket::ContentAction action,
964 std::string* error_desc) {
965 RTC_DCHECK(signaling_thread()->IsCurrent());
966
967 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800968 return PushdownLocalTransportDescription(local_description()->description(),
969 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700970 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800971 return PushdownRemoteTransportDescription(remote_description()->description(),
972 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700973}
974
975bool WebRtcSession::PushdownLocalTransportDescription(
976 const SessionDescription* sdesc,
977 cricket::ContentAction action,
978 std::string* err) {
979 RTC_DCHECK(signaling_thread()->IsCurrent());
980
981 if (!sdesc) {
982 return false;
983 }
984
985 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
986 if (!transport_controller_->SetLocalTransportDescription(
987 tinfo.content_name, tinfo.description, action, err)) {
988 return false;
989 }
990 }
991
992 return true;
993}
994
995bool WebRtcSession::PushdownRemoteTransportDescription(
996 const SessionDescription* sdesc,
997 cricket::ContentAction action,
998 std::string* err) {
999 RTC_DCHECK(signaling_thread()->IsCurrent());
1000
1001 if (!sdesc) {
1002 return false;
1003 }
1004
1005 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1006 if (!transport_controller_->SetRemoteTransportDescription(
1007 tinfo.content_name, tinfo.description, action, err)) {
1008 return false;
1009 }
1010 }
1011
1012 return true;
1013}
1014
1015bool WebRtcSession::GetTransportDescription(
1016 const SessionDescription* description,
1017 const std::string& content_name,
1018 cricket::TransportDescription* tdesc) {
1019 if (!description || !tdesc) {
1020 return false;
1021 }
1022 const TransportInfo* transport_info =
1023 description->GetTransportInfoByName(content_name);
1024 if (!transport_info) {
1025 return false;
1026 }
1027 *tdesc = transport_info->description;
1028 return true;
1029}
1030
deadbeefcbecd352015-09-23 11:50:27 -07001031bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1032 const std::string* first_content_name = bundle.FirstContentName();
1033 if (!first_content_name) {
1034 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1035 return false;
1036 }
1037 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001038
zhihuang9763d562016-08-05 11:14:50 -07001039#ifdef HAVE_QUIC
1040 if (quic_data_transport_ &&
1041 bundle.HasContentName(quic_data_transport_->content_name()) &&
1042 quic_data_transport_->transport_name() != transport_name) {
1043 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1044 << " on " << transport_name << "with QUIC.";
1045 }
1046#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001047 auto maybe_set_transport = [this, bundle,
1048 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001049 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001050 return true;
1051 }
1052
zhihuangf5b251b2017-01-12 19:37:48 -08001053 std::string old_transport_name = ch->transport_name();
1054 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001055 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1056 << " on " << transport_name << ".";
1057 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001058 }
torbjornga81a42f2015-09-23 02:16:58 -07001059
zhihuangb2cdd932017-01-19 16:54:25 -08001060 cricket::DtlsTransportInternal* rtp_dtls_transport =
1061 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001062 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001063 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1064 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001065 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001066 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08001067 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1068 }
1069
zhihuangb2cdd932017-01-19 16:54:25 -08001070 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001071 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1072 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001073 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001074 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001075 // If the channel needs rtcp, it means that the channel used to have a
1076 // rtcp transport which needs to be deleted now.
1077 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001078 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001079 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001080 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001081 return true;
1082 };
1083
deadbeefcbecd352015-09-23 11:50:27 -07001084 if (!maybe_set_transport(voice_channel()) ||
1085 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001086 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001087 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001088 }
deadbeef953c2ce2017-01-09 14:53:41 -08001089 // For SCTP, transport creation/deletion happens here instead of in the
1090 // object itself.
1091 if (sctp_transport_) {
1092 RTC_DCHECK(sctp_transport_name_);
1093 RTC_DCHECK(sctp_content_name_);
1094 if (transport_name != *sctp_transport_name_ &&
1095 bundle.HasContentName(*sctp_content_name_)) {
1096 network_thread_->Invoke<void>(
1097 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1098 transport_name));
1099 }
1100 }
deadbeefcbecd352015-09-23 11:50:27 -07001101
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001102 return true;
1103}
1104
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001106 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001107 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1108 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001109 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 }
1111
1112 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001113 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 return false;
1115 }
1116
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001117 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001118 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1119 if (!valid) {
1120 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 }
1122
1123 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001124 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001125 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 return false;
1127 }
1128
deadbeefd59daf82015-10-14 15:02:44 -07001129 if (ready) {
1130 return UseCandidate(candidate);
1131 } else {
1132 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1133 return true;
1134 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135}
1136
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001137bool WebRtcSession::RemoveRemoteIceCandidates(
1138 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001139 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001140 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1141 << "removed without any remote session description.";
1142 return false;
1143 }
1144
1145 if (candidates.empty()) {
1146 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1147 return false;
1148 }
1149
deadbeeffe4a8a42016-12-20 17:56:17 -08001150 size_t number_removed =
1151 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001152 if (number_removed != candidates.size()) {
1153 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1154 << "Requested " << candidates.size() << " but only "
1155 << number_removed << " are removed.";
1156 }
1157
1158 // Remove the candidates from the transport controller.
1159 std::string error;
1160 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1161 if (!res && !error.empty()) {
1162 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1163 }
1164 return true;
1165}
1166
deadbeefd59daf82015-10-14 15:02:44 -07001167cricket::IceConfig WebRtcSession::ParseIceConfig(
1168 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001169 cricket::ContinualGatheringPolicy gathering_policy;
1170 // TODO(honghaiz): Add the third continual gathering policy in
1171 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1172 switch (config.continual_gathering_policy) {
1173 case PeerConnectionInterface::GATHER_ONCE:
1174 gathering_policy = cricket::GATHER_ONCE;
1175 break;
1176 case PeerConnectionInterface::GATHER_CONTINUALLY:
1177 gathering_policy = cricket::GATHER_CONTINUALLY;
1178 break;
1179 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001180 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001181 gathering_policy = cricket::GATHER_ONCE;
1182 }
deadbeefd59daf82015-10-14 15:02:44 -07001183 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001184 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001185 ice_config.prioritize_most_likely_candidate_pairs =
1186 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001187 ice_config.backup_connection_ping_interval =
1188 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001189 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001190 ice_config.presume_writable_when_fully_relayed =
1191 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001192 ice_config.ice_check_min_interval = config.ice_check_min_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001193 return ice_config;
1194}
1195
1196void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1197 transport_controller_->SetIceConfig(config);
1198}
1199
1200void WebRtcSession::MaybeStartGathering() {
1201 transport_controller_->MaybeStartGathering();
1202}
1203
Peter Boström0c4e06b2015-10-07 12:23:21 +02001204bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1205 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001206 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001208 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001209 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1210 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211}
1212
Peter Boström0c4e06b2015-10-07 12:23:21 +02001213bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1214 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001215 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001216 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001217 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001218 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1219 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220}
1221
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001222std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001224 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 return desc.str();
1226}
1227
wu@webrtc.org78187522013-10-07 23:32:02 +00001228bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001229 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001230 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001231 if (!rtp_data_channel_ && !sctp_transport_) {
1232 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1233 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001234 return false;
1235 }
deadbeef953c2ce2017-01-09 14:53:41 -08001236 return rtp_data_channel_
1237 ? rtp_data_channel_->SendData(params, payload, result)
1238 : network_thread_->Invoke<bool>(
1239 RTC_FROM_HERE,
1240 Bind(&cricket::SctpTransportInternal::SendData,
1241 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001242}
1243
1244bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001245 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001246 // Don't log an error here, because DataChannels are expected to call
1247 // ConnectDataChannel in this state. It's the only way to initially tell
1248 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001249 return false;
1250 }
deadbeef953c2ce2017-01-09 14:53:41 -08001251 if (rtp_data_channel_) {
1252 rtp_data_channel_->SignalReadyToSendData.connect(
1253 webrtc_data_channel, &DataChannel::OnChannelReady);
1254 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1255 &DataChannel::OnDataReceived);
1256 } else {
1257 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1258 &DataChannel::OnChannelReady);
1259 SignalSctpDataReceived.connect(webrtc_data_channel,
1260 &DataChannel::OnDataReceived);
1261 SignalSctpStreamClosedRemotely.connect(
1262 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1263 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001264 return true;
1265}
1266
1267void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001268 if (!rtp_data_channel_ && !sctp_transport_) {
1269 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1270 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001271 return;
1272 }
deadbeef953c2ce2017-01-09 14:53:41 -08001273 if (rtp_data_channel_) {
1274 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1275 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1276 } else {
1277 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1278 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1279 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1280 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001281}
1282
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001283void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001284 if (!sctp_transport_) {
1285 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001286 return;
1287 }
deadbeef953c2ce2017-01-09 14:53:41 -08001288 network_thread_->Invoke<void>(
1289 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1290 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001291}
1292
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001293void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001294 if (!sctp_transport_) {
1295 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001296 << "NULL.";
1297 return;
1298 }
deadbeef953c2ce2017-01-09 14:53:41 -08001299 network_thread_->Invoke<void>(
1300 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1301 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001302}
1303
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001304bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001305 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1306 sctp_ready_to_send_data_;
1307}
1308
1309std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001310 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001311 ChannelNamePairs channel_name_pairs;
1312 if (voice_channel()) {
1313 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1314 voice_channel()->content_name(), voice_channel()->transport_name()));
1315 }
1316 if (video_channel()) {
1317 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1318 video_channel()->content_name(), video_channel()->transport_name()));
1319 }
1320 if (rtp_data_channel()) {
1321 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1322 ChannelNamePair(rtp_data_channel()->content_name(),
1323 rtp_data_channel()->transport_name()));
1324 }
1325 if (sctp_transport_) {
1326 RTC_DCHECK(sctp_content_name_);
1327 RTC_DCHECK(sctp_transport_name_);
1328 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1329 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1330 }
1331 return GetStats(channel_name_pairs);
1332}
1333
1334std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1335 const ChannelNamePairs& channel_name_pairs) {
1336 if (network_thread()->IsCurrent()) {
1337 return GetStats_n(channel_name_pairs);
1338 }
1339 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1340 RTC_FROM_HERE,
1341 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1342}
1343
1344bool WebRtcSession::GetLocalCertificate(
1345 const std::string& transport_name,
1346 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1347 return transport_controller_->GetLocalCertificate(transport_name,
1348 certificate);
1349}
1350
1351std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1352 const std::string& transport_name) {
1353 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001354}
1355
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356cricket::DataChannelType WebRtcSession::data_channel_type() const {
1357 return data_channel_type_;
1358}
1359
deadbeef0ed85b22016-02-23 17:24:52 -08001360bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1361 return pending_ice_restarts_.find(content_name) !=
1362 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001363}
1364
deadbeefd1a38b52016-12-10 13:15:33 -08001365void WebRtcSession::SetNeedsIceRestartFlag() {
1366 transport_controller_->SetNeedsIceRestartFlag();
1367}
1368
1369bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1370 return transport_controller_->NeedsIceRestart(content_name);
1371}
1372
Henrik Boströmd8281982015-08-27 10:12:24 +02001373void WebRtcSession::OnCertificateReady(
1374 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001375 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001376}
1377
deadbeef953c2ce2017-01-09 14:53:41 -08001378void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1379 SetError(ERROR_TRANSPORT,
1380 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1381}
1382
Henrik Boströmd8281982015-08-27 10:12:24 +02001383bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001384 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001385}
1386
deadbeefcbecd352015-09-23 11:50:27 -07001387const rtc::scoped_refptr<rtc::RTCCertificate>&
1388WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001389 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001390}
1391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392void WebRtcSession::SetIceConnectionState(
1393 PeerConnectionInterface::IceConnectionState state) {
1394 if (ice_connection_state_ == state) {
1395 return;
1396 }
1397
deadbeefcbecd352015-09-23 11:50:27 -07001398 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1399 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001400 RTC_DCHECK(ice_connection_state_ !=
1401 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 ice_connection_state_ = state;
1403 if (ice_observer_) {
1404 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1405 }
1406}
1407
deadbeefcbecd352015-09-23 11:50:27 -07001408void WebRtcSession::OnTransportControllerConnectionState(
1409 cricket::IceConnectionState state) {
1410 switch (state) {
1411 case cricket::kIceConnectionConnecting:
1412 // If the current state is Connected or Completed, then there were
1413 // writable channels but now there are not, so the next state must
1414 // be Disconnected.
1415 // kIceConnectionConnecting is currently used as the default,
1416 // un-connected state by the TransportController, so its only use is
1417 // detecting disconnections.
1418 if (ice_connection_state_ ==
1419 PeerConnectionInterface::kIceConnectionConnected ||
1420 ice_connection_state_ ==
1421 PeerConnectionInterface::kIceConnectionCompleted) {
1422 SetIceConnectionState(
1423 PeerConnectionInterface::kIceConnectionDisconnected);
1424 }
torbjornga81a42f2015-09-23 02:16:58 -07001425 break;
deadbeefcbecd352015-09-23 11:50:27 -07001426 case cricket::kIceConnectionFailed:
1427 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1428 break;
1429 case cricket::kIceConnectionConnected:
1430 LOG(LS_INFO) << "Changing to ICE connected state because "
1431 << "all transports are writable.";
1432 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1433 break;
1434 case cricket::kIceConnectionCompleted:
1435 LOG(LS_INFO) << "Changing to ICE completed state because "
1436 << "all transports are complete.";
1437 if (ice_connection_state_ !=
1438 PeerConnectionInterface::kIceConnectionConnected) {
1439 // If jumping directly from "checking" to "connected",
1440 // signal "connected" first.
1441 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1442 }
1443 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1444 if (metrics_observer_) {
1445 ReportTransportStats();
1446 }
1447 break;
1448 default:
nissec80e7412017-01-11 05:56:46 -08001449 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001450 }
deadbeefcbecd352015-09-23 11:50:27 -07001451}
1452
1453void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001454 SetIceConnectionReceiving(receiving);
1455}
1456
1457void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1458 if (ice_connection_receiving_ == receiving) {
1459 return;
1460 }
1461 ice_connection_receiving_ = receiving;
1462 if (ice_observer_) {
1463 ice_observer_->OnIceConnectionReceivingChange(receiving);
1464 }
1465}
1466
deadbeefcbecd352015-09-23 11:50:27 -07001467void WebRtcSession::OnTransportControllerCandidatesGathered(
1468 const std::string& transport_name,
1469 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001470 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001471 int sdp_mline_index;
1472 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1473 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1474 << transport_name << " not found";
1475 return;
1476 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477
deadbeefcbecd352015-09-23 11:50:27 -07001478 for (cricket::Candidates::const_iterator citer = candidates.begin();
1479 citer != candidates.end(); ++citer) {
1480 // Use transport_name as the candidate media id.
1481 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1482 if (ice_observer_) {
1483 ice_observer_->OnIceCandidate(&candidate);
1484 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001485 if (local_description()) {
1486 mutable_local_description()->AddCandidate(&candidate);
deadbeefcbecd352015-09-23 11:50:27 -07001487 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 }
1489}
1490
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001491void WebRtcSession::OnTransportControllerCandidatesRemoved(
1492 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001493 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001494 // Sanity check.
1495 for (const cricket::Candidate& candidate : candidates) {
1496 if (candidate.transport_name().empty()) {
1497 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1498 << "empty content name in candidate "
1499 << candidate.ToString();
1500 return;
1501 }
1502 }
1503
deadbeeffe4a8a42016-12-20 17:56:17 -08001504 if (local_description()) {
1505 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001506 }
1507 if (ice_observer_) {
1508 ice_observer_->OnIceCandidatesRemoved(candidates);
1509 }
1510}
1511
deadbeef953c2ce2017-01-09 14:53:41 -08001512void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1513 rtc::SSLHandshakeError error) {
1514 if (metrics_observer_) {
1515 metrics_observer_->IncrementEnumCounter(
1516 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1517 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1518 }
1519}
1520
1521// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522void WebRtcSession::EnableChannels() {
1523 if (voice_channel_ && !voice_channel_->enabled())
1524 voice_channel_->Enable(true);
1525
1526 if (video_channel_ && !video_channel_->enabled())
1527 video_channel_->Enable(true);
1528
deadbeef953c2ce2017-01-09 14:53:41 -08001529 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1530 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531}
1532
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533// Returns the media index for a local ice candidate given the content name.
1534bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1535 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001536 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001541 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 for (size_t index = 0; index < contents.size(); ++index) {
1543 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001544 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 content_found = true;
1546 break;
1547 }
1548 }
1549 return content_found;
1550}
1551
1552bool WebRtcSession::UseCandidatesInSessionDescription(
1553 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001554 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001558
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1560 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001561 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001562 const IceCandidateInterface* candidate = candidates->at(n);
1563 bool valid = false;
1564 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1565 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001566 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1567 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001568 }
1569 continue;
1570 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001571 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001572 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 break;
deadbeefd59daf82015-10-14 15:02:44 -07001574 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 }
1576 }
1577 return ret;
1578}
1579
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001580bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001582 size_t remote_content_size =
1583 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001585 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 return false;
1587 }
1588
1589 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001590 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 std::vector<cricket::Candidate> candidates;
1592 candidates.push_back(candidate->candidate());
1593 // Invoking BaseSession method to handle remote candidates.
1594 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001595 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1596 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 // Candidates successfully submitted for checking.
1598 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1599 ice_connection_state_ ==
1600 PeerConnectionInterface::kIceConnectionDisconnected) {
1601 // If state is New, then the session has just gotten its first remote ICE
1602 // candidates, so go to Checking.
1603 // If state is Disconnected, the session is re-using old candidates or
1604 // receiving additional ones, so go to Checking.
1605 // If state is Connected, stay Connected.
1606 // TODO(bemasc): If state is Connected, and the new candidates are for a
1607 // newly added transport, then the state actually _should_ move to
1608 // checking. Add a way to distinguish that case.
1609 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1610 }
1611 // TODO(bemasc): If state is Completed, go back to Connected.
1612 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001613 if (!error.empty()) {
1614 LOG(LS_WARNING) << error;
1615 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 }
1617 return true;
1618}
1619
deadbeefcbecd352015-09-23 11:50:27 -07001620void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001621 // Destroy video_channel_ first since it may have a pointer to the
1622 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 const cricket::ContentInfo* video_info =
1624 cricket::GetFirstVideoContent(desc);
1625 if ((!video_info || video_info->rejected) && video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001626 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 }
1628
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001629 const cricket::ContentInfo* voice_info =
1630 cricket::GetFirstAudioContent(desc);
1631 if ((!voice_info || voice_info->rejected) && voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001632 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001633 }
1634
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 const cricket::ContentInfo* data_info =
1636 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001637 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001638 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001639 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001640 }
1641 if (sctp_transport_) {
1642 SignalDataChannelDestroyed();
1643 network_thread_->Invoke<void>(
1644 RTC_FROM_HERE,
1645 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001646 }
1647#ifdef HAVE_QUIC
1648 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1649 if (quic_data_transport_) {
1650 quic_data_transport_.reset();
1651 }
1652#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 }
1654}
1655
skvlad6c87a672016-05-17 17:49:52 -07001656// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1657// if the channel is not part of any bundle.
1658const std::string* WebRtcSession::GetBundleTransportName(
1659 const cricket::ContentInfo* content,
1660 const cricket::ContentGroup* bundle) {
1661 if (!bundle) {
1662 return nullptr;
1663 }
1664 const std::string* first_content_name = bundle->FirstContentName();
1665 if (!first_content_name) {
1666 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1667 return nullptr;
1668 }
1669 if (!bundle->HasContentName(content->name)) {
1670 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1671 return nullptr;
1672 }
1673 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1674 return first_content_name;
1675}
1676
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001678 const cricket::ContentGroup* bundle_group = nullptr;
1679 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1680 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1681 if (!bundle_group) {
1682 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1683 return false;
1684 }
1685 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 // Creating the media channels and transport proxies.
1687 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1688 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001689 if (!CreateVoiceChannel(voice,
1690 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 LOG(LS_ERROR) << "Failed to create voice channel.";
1692 return false;
1693 }
1694 }
1695
1696 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1697 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001698 if (!CreateVideoChannel(video,
1699 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 LOG(LS_ERROR) << "Failed to create video channel.";
1701 return false;
1702 }
1703 }
1704
1705 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001706 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1707 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001708 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 LOG(LS_ERROR) << "Failed to create data channel.";
1710 return false;
1711 }
1712 }
1713
1714 return true;
1715}
1716
skvlad6c87a672016-05-17 17:49:52 -07001717bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1718 const std::string* bundle_transport) {
1719 bool require_rtcp_mux =
1720 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001721
1722 std::string transport_name =
1723 bundle_transport ? *bundle_transport : content->name;
1724
zhihuangb2cdd932017-01-19 16:54:25 -08001725 cricket::DtlsTransportInternal* rtp_dtls_transport =
1726 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001727 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001728 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001729 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001730 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001731 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1732 }
1733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001735 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001736 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1737 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001738 if (!voice_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001739 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001740 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001741 if (rtcp_dtls_transport) {
1742 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001743 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1744 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001745 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001746 }
zhihuangf5b251b2017-01-12 19:37:48 -08001747
deadbeefac22f702017-01-12 21:59:29 -08001748 voice_channel_->SignalRtcpMuxFullyActive.connect(
1749 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001750 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1751 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001752
1753 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001754 voice_channel_->SignalSentPacket.connect(this,
1755 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001756 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757}
1758
skvlad6c87a672016-05-17 17:49:52 -07001759bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1760 const std::string* bundle_transport) {
1761 bool require_rtcp_mux =
1762 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001763
1764 std::string transport_name =
1765 bundle_transport ? *bundle_transport : content->name;
1766
zhihuangb2cdd932017-01-19 16:54:25 -08001767 cricket::DtlsTransportInternal* rtp_dtls_transport =
1768 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001769 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001770 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001771 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001772 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001773 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1774 }
1775
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 video_channel_.reset(channel_manager_->CreateVideoChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001777 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001778 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1779 video_options_));
zhihuangf5b251b2017-01-12 19:37:48 -08001780
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001781 if (!video_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001782 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001783 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001784 if (rtcp_dtls_transport) {
1785 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001786 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1787 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001788 return false;
1789 }
zhihuangf5b251b2017-01-12 19:37:48 -08001790
deadbeefac22f702017-01-12 21:59:29 -08001791 video_channel_->SignalRtcpMuxFullyActive.connect(
1792 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001793 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1794 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001795
1796 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001797 video_channel_->SignalSentPacket.connect(this,
1798 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001799 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800}
1801
skvlad6c87a672016-05-17 17:49:52 -07001802bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1803 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001804 const std::string transport_name =
1805 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001806#ifdef HAVE_QUIC
1807 if (data_channel_type_ == cricket::DCT_QUIC) {
1808 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001809 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001810 return true;
1811 }
1812#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001813 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001814 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001815 if (!sctp_factory_) {
1816 LOG(LS_ERROR)
1817 << "Trying to create SCTP transport, but didn't compile with "
1818 "SCTP support (HAVE_SCTP)";
1819 return false;
1820 }
1821 if (!network_thread_->Invoke<bool>(
1822 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1823 this, content->name, transport_name))) {
1824 return false;
1825 };
1826 } else {
1827 bool require_rtcp_mux =
1828 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001829
1830 std::string transport_name =
1831 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001832 cricket::DtlsTransportInternal* rtp_dtls_transport =
1833 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001834 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001835 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001836 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001837 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001838 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1839 }
1840
deadbeef953c2ce2017-01-09 14:53:41 -08001841 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001842 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001843 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001844 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001845
deadbeef953c2ce2017-01-09 14:53:41 -08001846 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001847 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001848 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001849 if (rtcp_dtls_transport) {
1850 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001851 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1852 }
deadbeef953c2ce2017-01-09 14:53:41 -08001853 return false;
1854 }
zhihuangf5b251b2017-01-12 19:37:48 -08001855
deadbeefac22f702017-01-12 21:59:29 -08001856 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1857 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001858 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1859 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1860 rtp_data_channel_->SignalSentPacket.connect(this,
1861 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001862 }
1863
deadbeefab9b2d12015-10-14 11:33:11 -07001864 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001865
wu@webrtc.org91053e72013-08-10 07:18:04 +00001866 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867}
1868
hbosdf6075a2016-12-19 04:58:02 -08001869std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1870 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001871 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001872 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1873 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1874 &channel_name_pairs.video,
1875 &channel_name_pairs.data }) {
1876 if (*channel_name_pair) {
1877 cricket::TransportStats transport_stats;
1878 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1879 &transport_stats)) {
1880 return nullptr;
1881 }
1882 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1883 (*channel_name_pair)->transport_name;
1884 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1885 std::move(transport_stats);
1886 }
1887 }
1888 return session_stats;
1889}
1890
deadbeef953c2ce2017-01-09 14:53:41 -08001891bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1892 const std::string& transport_name) {
1893 RTC_DCHECK(network_thread_->IsCurrent());
1894 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001895 cricket::DtlsTransportInternal* tc =
1896 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001897 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1898 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1899 RTC_DCHECK(sctp_transport_);
1900 sctp_invoker_.reset(new rtc::AsyncInvoker());
1901 sctp_transport_->SignalReadyToSendData.connect(
1902 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1903 sctp_transport_->SignalDataReceived.connect(
1904 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1905 sctp_transport_->SignalStreamClosedRemotely.connect(
1906 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1907 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1908 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1909 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910}
1911
deadbeef953c2ce2017-01-09 14:53:41 -08001912void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1913 RTC_DCHECK(network_thread_->IsCurrent());
1914 RTC_DCHECK(sctp_transport_);
1915 RTC_DCHECK(sctp_transport_name_);
1916 std::string old_sctp_transport_name = *sctp_transport_name_;
1917 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08001918 cricket::DtlsTransportInternal* tc =
1919 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001920 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1921 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08001922 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001923 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1924}
1925
1926void WebRtcSession::DestroySctpTransport_n() {
1927 RTC_DCHECK(network_thread_->IsCurrent());
1928 sctp_transport_.reset(nullptr);
1929 sctp_content_name_.reset();
1930 sctp_transport_name_.reset();
1931 sctp_invoker_.reset(nullptr);
1932 sctp_ready_to_send_data_ = false;
1933}
1934
1935void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1936 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1937 RTC_DCHECK(network_thread_->IsCurrent());
1938 sctp_invoker_->AsyncInvoke<void>(
1939 RTC_FROM_HERE, signaling_thread_,
1940 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1941}
1942
1943void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1944 RTC_DCHECK(signaling_thread_->IsCurrent());
1945 sctp_ready_to_send_data_ = ready;
1946 SignalSctpReadyToSendData(ready);
1947}
1948
1949void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001950 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001951 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001952 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08001953 RTC_DCHECK(network_thread_->IsCurrent());
1954 sctp_invoker_->AsyncInvoke<void>(
1955 RTC_FROM_HERE, signaling_thread_,
1956 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
1957 payload));
1958}
1959
1960void WebRtcSession::OnSctpTransportDataReceived_s(
1961 const cricket::ReceiveDataParams& params,
1962 const rtc::CopyOnWriteBuffer& payload) {
1963 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07001964 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1965 // Received OPEN message; parse and signal that a new data channel should
1966 // be created.
1967 std::string label;
1968 InternalDataChannelInit config;
1969 config.id = params.ssrc;
1970 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1971 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1972 << params.ssrc;
1973 return;
1974 }
1975 config.open_handshake_role = InternalDataChannelInit::kAcker;
1976 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08001977 } else {
1978 // Otherwise just forward the signal.
1979 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 }
deadbeef953c2ce2017-01-09 14:53:41 -08001981}
1982
1983void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
1984 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1985 RTC_DCHECK(network_thread_->IsCurrent());
1986 sctp_invoker_->AsyncInvoke<void>(
1987 RTC_FROM_HERE, signaling_thread_,
1988 rtc::Bind(&sigslot::signal1<int>::operator(),
1989 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990}
1991
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001992// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001993bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001994 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1995 if (!bundle_enabled)
1996 return true;
1997
1998 const cricket::ContentGroup* bundle_group =
1999 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002000 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002001
2002 const cricket::ContentInfos& contents = desc->contents();
2003 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2004 citer != contents.end(); ++citer) {
2005 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002006 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002007 if (bundle_group->HasContentName(content->name) &&
2008 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2009 if (!HasRtcpMuxEnabled(content))
2010 return false;
2011 }
2012 }
2013 // RTCP-MUX is enabled in all the contents.
2014 return true;
2015}
2016
2017bool WebRtcSession::HasRtcpMuxEnabled(
2018 const cricket::ContentInfo* content) {
2019 const cricket::MediaContentDescription* description =
2020 static_cast<cricket::MediaContentDescription*>(content->description);
2021 return description->rtcp_mux();
2022}
2023
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002024bool WebRtcSession::ValidateSessionDescription(
2025 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002026 cricket::ContentSource source, std::string* err_desc) {
2027 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002028 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002029 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002030 }
2031
2032 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002033 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002034 }
2035
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002036 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002037 Action action = GetAction(sdesc->type());
2038 if (source == cricket::CS_LOCAL) {
2039 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002040 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002041 } else {
2042 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002043 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002044 }
2045
2046 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002047 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002048 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2049 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002050 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002051 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002052 }
2053
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002054 // Verify ice-ufrag and ice-pwd.
2055 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002056 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002057 }
2058
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002059 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002060 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002061 }
2062
skvlad6c87a672016-05-17 17:49:52 -07002063 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2064 // m-lines that do not rtcp-mux enabled.
2065
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002066 // Verify m-lines in Answer when compared against Offer.
2067 if (action == kAnswer) {
2068 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002069 (source == cricket::CS_LOCAL) ? remote_description()->description()
2070 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002071 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002072 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002073 }
2074 }
2075
2076 return true;
2077}
2078
2079bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2080 return ((action == kOffer && state() == STATE_INIT) ||
2081 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002082 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002083 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002084 (action == kOffer && state() == STATE_INPROGRESS) ||
2085 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002086 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2087 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2088 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2089 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002090}
2091
2092bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2093 return ((action == kOffer && state() == STATE_INIT) ||
2094 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002095 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002097 (action == kOffer && state() == STATE_INPROGRESS) ||
2098 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002099 (action == kAnswer && state() == STATE_SENTOFFER) ||
2100 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2101 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2102 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002103}
2104
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002105std::string WebRtcSession::GetSessionErrorMsg() {
2106 std::ostringstream desc;
2107 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2108 desc << kSessionErrorDesc << error_desc() << ".";
2109 return desc.str();
2110}
2111
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002112// We need to check the local/remote description for the Transport instead of
2113// the session, because a new Transport added during renegotiation may have
2114// them unset while the session has them set from the previous negotiation.
2115// Not doing so may trigger the auto generation of transport description and
2116// mess up DTLS identity information, ICE credential, etc.
2117bool WebRtcSession::ReadyToUseRemoteCandidate(
2118 const IceCandidateInterface* candidate,
2119 const SessionDescriptionInterface* remote_desc,
2120 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002121 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002122
2123 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002124 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002125
deadbeefd59daf82015-10-14 15:02:44 -07002126 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002127 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002128 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002129
2130 size_t mediacontent_index =
2131 static_cast<size_t>(candidate->sdp_mline_index());
2132 size_t remote_content_size =
2133 current_remote_desc->description()->contents().size();
2134 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002135 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2136 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002137
2138 *valid = false;
2139 return false;
2140 }
2141
2142 cricket::ContentInfo content =
2143 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002144
2145 const std::string transport_name = GetTransportName(content.name);
2146 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002147 return false;
2148 }
zhihuang9763d562016-08-05 11:14:50 -07002149 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002150}
2151
deadbeef7af91dd2016-12-13 11:29:11 -08002152bool WebRtcSession::SrtpRequired() const {
2153 return dtls_enabled_ ||
2154 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2155}
2156
deadbeefcbecd352015-09-23 11:50:27 -07002157void WebRtcSession::OnTransportControllerGatheringState(
2158 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002159 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002160 if (state == cricket::kIceGatheringGathering) {
2161 if (ice_observer_) {
2162 ice_observer_->OnIceGatheringChange(
2163 PeerConnectionInterface::kIceGatheringGathering);
2164 }
2165 } else if (state == cricket::kIceGatheringComplete) {
2166 if (ice_observer_) {
2167 ice_observer_->OnIceGatheringChange(
2168 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002169 }
2170 }
2171}
2172
2173void WebRtcSession::ReportTransportStats() {
2174 // Use a set so we don't report the same stats twice if two channels share
2175 // a transport.
2176 std::set<std::string> transport_names;
2177 if (voice_channel()) {
2178 transport_names.insert(voice_channel()->transport_name());
2179 }
2180 if (video_channel()) {
2181 transport_names.insert(video_channel()->transport_name());
2182 }
deadbeef953c2ce2017-01-09 14:53:41 -08002183 if (rtp_data_channel()) {
2184 transport_names.insert(rtp_data_channel()->transport_name());
2185 }
2186 if (sctp_transport_name_) {
2187 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002188 }
2189 for (const auto& name : transport_names) {
2190 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002191 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002192 ReportBestConnectionState(stats);
2193 ReportNegotiatedCiphers(stats);
2194 }
2195 }
2196}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002197// Walk through the ConnectionInfos to gather best connection usage
2198// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002199void WebRtcSession::ReportBestConnectionState(
2200 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002201 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002202 for (cricket::TransportChannelStatsList::const_iterator it =
2203 stats.channel_stats.begin();
2204 it != stats.channel_stats.end(); ++it) {
2205 for (cricket::ConnectionInfos::const_iterator it_info =
2206 it->connection_infos.begin();
2207 it_info != it->connection_infos.end(); ++it_info) {
2208 if (!it_info->best_connection) {
2209 continue;
2210 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002211
2212 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2213 const cricket::Candidate& local = it_info->local_candidate;
2214 const cricket::Candidate& remote = it_info->remote_candidate;
2215
2216 // Increment the counter for IceCandidatePairType.
2217 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2218 (local.type() == RELAY_PORT_TYPE &&
2219 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2220 type = kEnumCounterIceCandidatePairTypeTcp;
2221 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2222 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002223 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002224 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002225 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002226 metrics_observer_->IncrementEnumCounter(
2227 type, GetIceCandidatePairCounter(local, remote),
2228 kIceCandidatePairMax);
2229
2230 // Increment the counter for IP type.
2231 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002232 metrics_observer_->IncrementEnumCounter(
2233 kEnumCounterAddressFamily, kBestConnections_IPv4,
2234 kPeerConnectionAddressFamilyCounter_Max);
2235
2236 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002237 metrics_observer_->IncrementEnumCounter(
2238 kEnumCounterAddressFamily, kBestConnections_IPv6,
2239 kPeerConnectionAddressFamilyCounter_Max);
2240 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002241 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002242 }
2243
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002244 return;
2245 }
2246 }
2247}
2248
jbauchac8869e2015-07-03 01:36:14 -07002249void WebRtcSession::ReportNegotiatedCiphers(
2250 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002251 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002252 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2253 return;
2254 }
2255
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002256 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2257 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2258 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2259 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002260 return;
2261 }
2262
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002263 PeerConnectionEnumCounterType srtp_counter_type;
2264 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002265 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002266 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2267 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002268 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002269 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2270 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002271 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002272 srtp_counter_type = kEnumCounterDataSrtpCipher;
2273 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002274 } else {
2275 RTC_NOTREACHED();
2276 return;
2277 }
2278
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002279 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2280 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2281 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002282 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002283 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2284 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2285 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002286 }
2287}
2288
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002289void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002290 RTC_DCHECK(worker_thread()->IsCurrent());
2291 media_controller_->call_w()->OnSentPacket(sent_packet);
2292}
2293
zhihuang9763d562016-08-05 11:14:50 -07002294const std::string WebRtcSession::GetTransportName(
2295 const std::string& content_name) {
2296 cricket::BaseChannel* channel = GetChannel(content_name);
2297 if (!channel) {
2298#ifdef HAVE_QUIC
2299 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2300 content_name == quic_data_transport_->transport_name()) {
2301 return quic_data_transport_->transport_name();
2302 }
2303#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002304 if (sctp_transport_) {
2305 RTC_DCHECK(sctp_content_name_);
2306 RTC_DCHECK(sctp_transport_name_);
2307 if (content_name == *sctp_content_name_) {
2308 return *sctp_transport_name_;
2309 }
2310 }
zhihuang9763d562016-08-05 11:14:50 -07002311 // Return an empty string if failed to retrieve the transport name.
2312 return "";
2313 }
2314 return channel->transport_name();
2315}
zhihuangd82eee02016-08-26 11:25:05 -07002316
deadbeefac22f702017-01-12 21:59:29 -08002317void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002318 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002319 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002320 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2321}
2322
2323void WebRtcSession::DestroyVideoChannel() {
2324 SignalVideoChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002325 RTC_DCHECK(video_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002326 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002327 transport_name = video_channel_->rtp_dtls_transport()->transport_name();
2328 bool need_to_delete_rtcp = (video_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002329 channel_manager_->DestroyVideoChannel(video_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002330 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002331 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002332 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002333 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002334 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002335 }
2336}
2337
2338void WebRtcSession::DestroyVoiceChannel() {
2339 SignalVoiceChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002340 RTC_DCHECK(voice_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002341 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002342 transport_name = voice_channel_->rtp_dtls_transport()->transport_name();
2343 bool need_to_delete_rtcp = (voice_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002344 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002345 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002346 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002347 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002348 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002349 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002350 }
2351}
2352
2353void WebRtcSession::DestroyDataChannel() {
2354 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002355 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002356 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002357 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2358 bool need_to_delete_rtcp =
2359 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002360 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002361 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002362 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002363 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002364 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002365 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002366 }
2367}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368} // namespace webrtc