blob: a0c72c853ec6a54a4e92e052ec4e15e1c560b634 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/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"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000026#include "webrtc/base/basictypes.h"
zhihuang9763d562016-08-05 11:14:50 -070027#include "webrtc/base/bind.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
ossuf515ab82016-12-07 04:52:58 -080033#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/videocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080036#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000037#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070038#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010039#include "webrtc/pc/channel.h"
40#include "webrtc/pc/channelmanager.h"
41#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
zhihuang9763d562016-08-05 11:14:50 -070043#ifdef HAVE_QUIC
44#include "webrtc/p2p/quic/quictransportchannel.h"
45#endif // HAVE_QUIC
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047using cricket::ContentInfo;
48using cricket::ContentInfos;
49using cricket::MediaContentDescription;
50using cricket::SessionDescription;
51using cricket::TransportInfo;
52
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070053using cricket::LOCAL_PORT_TYPE;
54using cricket::STUN_PORT_TYPE;
55using cricket::RELAY_PORT_TYPE;
56using cricket::PRFLX_PORT_TYPE;
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058namespace webrtc {
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000061const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
62 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064const char kInvalidCandidates[] = "Description contains invalid candidates.";
65const char kInvalidSdp[] = "Invalid session description.";
66const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000067 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
68const char kPushDownTDFailed[] =
69 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070const char kSdpWithoutDtlsFingerprint[] =
71 "Called with SDP without DTLS fingerprint.";
72const char kSdpWithoutSdesCrypto[] =
73 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000074const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000075 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080078const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080080const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000081 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070082const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070084IceCandidatePairType GetIceCandidatePairCounter(
85 const cricket::Candidate& local,
86 const cricket::Candidate& remote) {
87 const auto& l = local.type();
88 const auto& r = remote.type();
89 const auto& host = LOCAL_PORT_TYPE;
90 const auto& srflx = STUN_PORT_TYPE;
91 const auto& relay = RELAY_PORT_TYPE;
92 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070093 if (l == host && r == host) {
94 bool local_private = IPIsPrivate(local.address().ipaddr());
95 bool remote_private = IPIsPrivate(remote.address().ipaddr());
96 if (local_private) {
97 if (remote_private) {
98 return kIceCandidatePairHostPrivateHostPrivate;
99 } else {
100 return kIceCandidatePairHostPrivateHostPublic;
101 }
102 } else {
103 if (remote_private) {
104 return kIceCandidatePairHostPublicHostPrivate;
105 } else {
106 return kIceCandidatePairHostPublicHostPublic;
107 }
108 }
109 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700110 if (l == host && r == srflx)
111 return kIceCandidatePairHostSrflx;
112 if (l == host && r == relay)
113 return kIceCandidatePairHostRelay;
114 if (l == host && r == prflx)
115 return kIceCandidatePairHostPrflx;
116 if (l == srflx && r == host)
117 return kIceCandidatePairSrflxHost;
118 if (l == srflx && r == srflx)
119 return kIceCandidatePairSrflxSrflx;
120 if (l == srflx && r == relay)
121 return kIceCandidatePairSrflxRelay;
122 if (l == srflx && r == prflx)
123 return kIceCandidatePairSrflxPrflx;
124 if (l == relay && r == host)
125 return kIceCandidatePairRelayHost;
126 if (l == relay && r == srflx)
127 return kIceCandidatePairRelaySrflx;
128 if (l == relay && r == relay)
129 return kIceCandidatePairRelayRelay;
130 if (l == relay && r == prflx)
131 return kIceCandidatePairRelayPrflx;
132 if (l == prflx && r == host)
133 return kIceCandidatePairPrflxHost;
134 if (l == prflx && r == srflx)
135 return kIceCandidatePairPrflxSrflx;
136 if (l == prflx && r == relay)
137 return kIceCandidatePairPrflxRelay;
138 return kIceCandidatePairMax;
139}
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141// Compares |answer| against |offer|. Comparision is done
142// for number of m-lines in answer against offer. If matches true will be
143// returned otherwise false.
144static bool VerifyMediaDescriptions(
145 const SessionDescription* answer, const SessionDescription* offer) {
146 if (offer->contents().size() != answer->contents().size())
147 return false;
148
149 for (size_t i = 0; i < offer->contents().size(); ++i) {
150 if ((offer->contents()[i].name) != answer->contents()[i].name) {
151 return false;
152 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000153 const MediaContentDescription* offer_mdesc =
154 static_cast<const MediaContentDescription*>(
155 offer->contents()[i].description);
156 const MediaContentDescription* answer_mdesc =
157 static_cast<const MediaContentDescription*>(
158 answer->contents()[i].description);
159 if (offer_mdesc->type() != answer_mdesc->type()) {
160 return false;
161 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 }
163 return true;
164}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166// Checks that each non-rejected content has SDES crypto keys or a DTLS
167// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
168// keys, will be caught in Transport negotiation, and backstopped by Channel's
deadbeef7af91dd2016-12-13 11:29:11 -0800169// |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000170static bool VerifyCrypto(const SessionDescription* desc,
171 bool dtls_enabled,
172 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 const ContentInfos& contents = desc->contents();
174 for (size_t index = 0; index < contents.size(); ++index) {
175 const ContentInfo* cinfo = &contents[index];
176 if (cinfo->rejected) {
177 continue;
178 }
179
180 // If the content isn't rejected, crypto must be present.
181 const MediaContentDescription* media =
182 static_cast<const MediaContentDescription*>(cinfo->description);
183 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
184 if (!media || !tinfo) {
185 // Something is not right.
186 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000187 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 return false;
189 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000190 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000191 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 LOG(LS_WARNING) <<
193 "Session description must have DTLS fingerprint if DTLS enabled.";
194 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000195 return false;
196 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000197 } else {
198 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 LOG(LS_WARNING) <<
200 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 return false;
203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 }
205 }
206
207 return true;
208}
209
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000210// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
211static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
212 const ContentInfos& contents = desc->contents();
213 for (size_t index = 0; index < contents.size(); ++index) {
214 const ContentInfo* cinfo = &contents[index];
215 if (cinfo->rejected) {
216 continue;
217 }
218
219 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
220 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
221 if (!tinfo) {
222 // Something is not right.
223 LOG(LS_ERROR) << kInvalidSdp;
224 return false;
225 }
226 if (tinfo->description.ice_ufrag.empty() ||
227 tinfo->description.ice_pwd.empty()) {
228 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
229 return false;
230 }
231 }
232 return true;
233}
234
Peter Boström0c4e06b2015-10-07 12:23:21 +0200235static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
236 const std::string& track_id,
237 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 const cricket::ContentInfo* audio_info =
239 cricket::GetFirstAudioContent(session_description);
240 if (!audio_info) {
241 LOG(LS_ERROR) << "Audio not used in this call";
242 return false;
243 }
244
245 const cricket::MediaContentDescription* audio_content =
246 static_cast<const cricket::MediaContentDescription*>(
247 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000248 const cricket::StreamParams* stream =
249 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
250 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 return false;
252 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000253
254 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 return true;
256}
257
258static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 uint32_t ssrc,
260 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800261 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 const cricket::ContentInfo* audio_info =
264 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000265 if (audio_info) {
266 const cricket::MediaContentDescription* audio_content =
267 static_cast<const cricket::MediaContentDescription*>(
268 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000270 const auto* found =
271 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
272 if (found) {
273 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000274 return true;
275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 const cricket::ContentInfo* video_info =
279 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000280 if (video_info) {
281 const cricket::MediaContentDescription* video_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000285 const auto* found =
286 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
287 if (found) {
288 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000289 return true;
290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292 return false;
293}
294
deadbeef953c2ce2017-01-09 14:53:41 -0800295// Get the SCTP port out of a SessionDescription.
296// Return -1 if not found.
297static int GetSctpPort(const SessionDescription* session_description) {
298 const ContentInfo* content_info = GetFirstDataContent(session_description);
299 RTC_DCHECK(content_info);
300 if (!content_info) {
301 return -1;
302 }
303 const cricket::DataContentDescription* data =
304 static_cast<const cricket::DataContentDescription*>(
305 (content_info->description));
306 std::string value;
307 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
308 cricket::kGoogleSctpDataCodecName);
309 for (const cricket::DataCodec& codec : data->codecs()) {
310 if (!codec.Matches(match_pattern)) {
311 continue;
312 }
313 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
314 return rtc::FromString<int>(value);
315 }
316 }
317 return -1;
318}
319
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320static bool BadSdp(const std::string& source,
321 const std::string& type,
322 const std::string& reason,
323 std::string* err_desc) {
324 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700325 desc << "Failed to set " << source;
326 if (!type.empty()) {
327 desc << " " << type;
328 }
329 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000334 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 return false;
336}
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 const std::string& type,
340 const std::string& reason,
341 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 }
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349static bool BadLocalSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
353}
354
355static bool BadRemoteSdp(const std::string& type,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
359}
360
361static bool BadOfferSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
365}
366
367static bool BadPranswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
371 reason, err_desc);
372}
373
374static bool BadAnswerSdp(cricket::ContentSource source,
375 const std::string& reason,
376 std::string* err_desc) {
377 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378}
379
deadbeefd59daf82015-10-14 15:02:44 -0700380#define GET_STRING_OF_STATE(state) \
381 case webrtc::WebRtcSession::state: \
382 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 break;
384
deadbeefd59daf82015-10-14 15:02:44 -0700385static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 std::string result;
387 switch (state) {
388 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700389 GET_STRING_OF_STATE(STATE_SENTOFFER)
390 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
391 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
392 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700394 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 default:
nissec80e7412017-01-11 05:56:46 -0800396 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 break;
398 }
399 return result;
400}
401
deadbeefd59daf82015-10-14 15:02:44 -0700402#define GET_STRING_OF_ERROR_CODE(err) \
403 case webrtc::WebRtcSession::err: \
404 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 break;
406
deadbeefd59daf82015-10-14 15:02:44 -0700407static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 std::string result;
409 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000411 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
412 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800414 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 break;
416 }
417 return result;
418}
419
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420static std::string MakeErrorString(const std::string& error,
421 const std::string& desc) {
422 std::ostringstream ret;
423 ret << error << " " << desc;
424 return ret.str();
425}
426
427static std::string MakeTdErrorString(const std::string& desc) {
428 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429}
430
deadbeef0ed85b22016-02-23 17:24:52 -0800431// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
432bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
433 const SessionDescriptionInterface* new_desc,
434 const std::string& content_name) {
435 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700436 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
deadbeef0ed85b22016-02-23 17:24:52 -0800438 const SessionDescription* new_sd = new_desc->description();
439 const SessionDescription* old_sd = old_desc->description();
440 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
441 if (!cinfo || cinfo->rejected) {
442 return false;
443 }
444 // If the content isn't rejected, check if ufrag and password has changed.
445 const cricket::TransportDescription* new_transport_desc =
446 new_sd->GetTransportDescriptionByName(content_name);
447 const cricket::TransportDescription* old_transport_desc =
448 old_sd->GetTransportDescriptionByName(content_name);
449 if (!new_transport_desc || !old_transport_desc) {
450 // No transport description exists. This is not an ICE restart.
451 return false;
452 }
453 if (cricket::IceCredentialsChanged(
454 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
455 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
456 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
457 << ".";
458 return true;
459 }
460 return false;
461}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
zhihuang29ff8442016-07-27 11:07:25 -0700463WebRtcSession::WebRtcSession(
464 webrtc::MediaControllerInterface* media_controller,
465 rtc::Thread* network_thread,
466 rtc::Thread* worker_thread,
467 rtc::Thread* signaling_thread,
468 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800469 std::unique_ptr<cricket::TransportController> transport_controller,
470 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700471 : network_thread_(network_thread),
472 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700473 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 // RFC 3264: The numeric value of the session id and version in the
475 // o line MUST be representable with a "64 bit signed integer".
476 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700477 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700478 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800479 sctp_factory_(std::move(sctp_factory)),
stefanc1aeaf02015-10-15 07:26:07 -0700480 media_controller_(media_controller),
481 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 ice_observer_(NULL),
483 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700484 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000486 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000488 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700489 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
490 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700491 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700492 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700493 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700494 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700495 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700496 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700497 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700498 transport_controller_->SignalCandidatesRemoved.connect(
499 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700500 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800501 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502}
503
504WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800505 RTC_DCHECK(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000506 // Destroy video_channel_ first since it may have a pointer to the
507 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000508 if (video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800509 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000511 if (voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800512 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000513 }
deadbeef953c2ce2017-01-09 14:53:41 -0800514 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800515 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800516 }
517 if (sctp_transport_) {
518 SignalDataChannelDestroyed();
519 network_thread_->Invoke<void>(
520 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 }
zhihuang9763d562016-08-05 11:14:50 -0700522#ifdef HAVE_QUIC
523 if (quic_data_transport_) {
524 quic_data_transport_.reset();
525 }
526#endif
deadbeef057ecf02016-01-20 14:30:43 -0800527 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700528
529 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530}
531
wu@webrtc.org91053e72013-08-10 07:18:04 +0000532bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000533 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200534 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200535 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
536 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700537 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700538 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000539
Henrik Boström87713d02015-08-25 09:53:21 +0200540 // Obtain a certificate from RTCConfiguration if any were provided (optional).
541 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
542 if (!rtc_configuration.certificates.empty()) {
543 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
544 // just picking the first one. The decision should be made based on the DTLS
545 // handshake. The DTLS negotiations need to know about all certificates.
546 certificate = rtc_configuration.certificates[0];
547 }
548
honghaiz1f429e32015-09-28 07:57:34 -0700549 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700550
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000551 if (options.disable_encryption) {
552 dtls_enabled_ = false;
553 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200554 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200555 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800556 // |rtc_configuration| can override the default |dtls_enabled_| value.
557 if (rtc_configuration.enable_dtls_srtp) {
558 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000559 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000560 }
561
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000563 // It takes precendence over the disable_sctp_data_channels
564 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800565 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700567 }
568#ifdef HAVE_QUIC
569 else if (rtc_configuration.enable_quic) {
570 // Use QUIC instead of DTLS when |enable_quic| is true.
571 data_channel_type_ = cricket::DCT_QUIC;
572 transport_controller_->use_quic();
573 if (dtls_enabled_) {
574 LOG(LS_INFO) << "Using QUIC instead of DTLS";
575 }
576 quic_data_transport_.reset(
577 new QuicDataTransport(signaling_thread(), worker_thread(),
578 network_thread(), transport_controller_.get()));
579 }
580#endif // HAVE_QUIC
581 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000582 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000583 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000584 data_channel_type_ = cricket::DCT_SCTP;
585 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
htaa2a49d92016-03-04 02:51:39 -0800588 video_options_.screencast_min_bitrate_kbps =
589 rtc_configuration.screencast_min_bitrate;
590 audio_options_.combined_audio_video_bwe =
591 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000592
kwiberg102c6a62015-10-30 02:47:38 -0700593 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100594 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200595
Karl Wibergbe579832015-11-10 22:34:18 +0100596 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
597 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200598
Henrik Boström87713d02015-08-25 09:53:21 +0200599 if (!dtls_enabled_) {
600 // Construct with DTLS disabled.
601 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200602 signaling_thread(), channel_manager_, this, id(),
603 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200604 } else {
605 // Construct with DTLS enabled.
606 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200607 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200608 signaling_thread(), channel_manager_, this, id(),
609 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200610 } else {
611 // Use the already generated certificate.
612 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200613 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200614 }
615 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000616
Henrik Boströmd8281982015-08-27 10:12:24 +0200617 webrtc_session_desc_factory_->SignalCertificateReady.connect(
618 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000619
wu@webrtc.org97077a32013-10-25 21:18:33 +0000620 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000621 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000622 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 return true;
625}
626
deadbeefd59daf82015-10-14 15:02:44 -0700627void WebRtcSession::Close() {
628 SetState(STATE_CLOSED);
629 RemoveUnusedChannels(nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800630 RTC_DCHECK(!voice_channel_);
631 RTC_DCHECK(!video_channel_);
632 RTC_DCHECK(!rtp_data_channel_);
633 RTC_DCHECK(!sctp_transport_);
solenberg03d6d572016-03-01 12:42:03 -0800634 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635}
636
deadbeef0ed85b22016-02-23 17:24:52 -0800637cricket::BaseChannel* WebRtcSession::GetChannel(
638 const std::string& content_name) {
639 if (voice_channel() && voice_channel()->content_name() == content_name) {
640 return voice_channel();
641 }
642 if (video_channel() && video_channel()->content_name() == content_name) {
643 return video_channel();
644 }
deadbeef953c2ce2017-01-09 14:53:41 -0800645 if (rtp_data_channel() &&
646 rtp_data_channel()->content_name() == content_name) {
647 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800648 }
649 return nullptr;
650}
651
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000652cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
653 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654}
655
deadbeef953c2ce2017-01-09 14:53:41 -0800656bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
657 if (!local_description() || !remote_description()) {
658 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
659 << "SSL Role of the SCTP transport.";
660 return false;
661 }
662 if (!sctp_transport_) {
663 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
664 << "SSL Role of the SCTP transport.";
665 return false;
666 }
667
668 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
669}
670
671bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800672 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800673 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800674 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000675 << "SSL Role of the session.";
676 return false;
677 }
678
deadbeef953c2ce2017-01-09 14:53:41 -0800679 return transport_controller_->GetSslRole(GetTransportName(content_name),
680 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000681}
682
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000683void WebRtcSession::CreateOffer(
684 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700685 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
686 const cricket::MediaSessionOptions& session_options) {
687 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000688}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689
deadbeefab9b2d12015-10-14 11:33:11 -0700690void WebRtcSession::CreateAnswer(
691 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700692 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800693 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694}
695
696bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
697 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800698 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700699
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000700 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700701 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000702
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000703 // Validate SDP.
704 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
705 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
707
deadbeefd59daf82015-10-14 15:02:44 -0700708 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000709 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700711 initial_offerer_ = true;
712 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
deadbeeffe4a8a42016-12-20 17:56:17 -0800715 if (action == kAnswer) {
716 current_local_description_.reset(desc_temp.release());
717 pending_local_description_.reset(nullptr);
718 current_remote_description_.reset(pending_remote_description_.release());
719 } else {
720 pending_local_description_.reset(desc_temp.release());
721 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722
723 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800724 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 // TODO(mallinath) - Handle CreateChannel failure, as new local description
726 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000727 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
deadbeefcbecd352015-09-23 11:50:27 -0700730 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800731 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000733 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 return false;
735 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800736 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700737 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800738 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740
deadbeef0ed85b22016-02-23 17:24:52 -0800741 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700742 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000743 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 return true;
746}
747
748bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
749 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800750 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700751
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000752 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700753 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000754
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000755 // Validate SDP.
756 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
757 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759
deadbeeffe4a8a42016-12-20 17:56:17 -0800760 const SessionDescriptionInterface* old_remote_description =
761 remote_description();
762 // Grab ownership of the description being replaced for the remainder of this
763 // method, since it's used below.
764 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
765 Action action = GetAction(desc->type());
766 if (action == kAnswer) {
767 replaced_remote_description.reset(
768 pending_remote_description_ ? pending_remote_description_.release()
769 : current_remote_description_.release());
770 current_remote_description_.reset(desc_temp.release());
771 pending_remote_description_.reset(nullptr);
772 current_local_description_.reset(pending_local_description_.release());
773 } else {
774 replaced_remote_description.reset(pending_remote_description_.release());
775 pending_remote_description_.reset(desc_temp.release());
776 }
deadbeefd59daf82015-10-14 15:02:44 -0700777
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 // Transport and Media channels will be created only when offer is set.
779 if (action == kOffer && !CreateChannels(desc->description())) {
780 // TODO(mallinath) - Handle CreateChannel failure, as new local description
781 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000782 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 }
784
deadbeefcbecd352015-09-23 11:50:27 -0700785 // Remove unused channels if MediaContentDescription is rejected.
786 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787
788 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
789 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000790 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 return false;
792 }
793
deadbeeffe4a8a42016-12-20 17:56:17 -0800794 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000795 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 }
797
deadbeeffe4a8a42016-12-20 17:56:17 -0800798 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800799 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800800 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800801 // Check if this new SessionDescription contains new ICE ufrag and
802 // password that indicates the remote peer requests an ICE restart.
803 // TODO(deadbeef): When we start storing both the current and pending
804 // remote description, this should reset pending_ice_restarts and compare
805 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800806 if (CheckForRemoteIceRestart(old_remote_description, desc,
807 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800808 if (action == kOffer) {
809 pending_ice_restarts_.insert(content.name);
810 }
811 } else {
812 // We retain all received candidates only if ICE is not restarted.
813 // When ICE is restarted, all previous candidates belong to an old
814 // generation and should not be kept.
815 // TODO(deadbeef): This goes against the W3C spec which says the remote
816 // description should only contain candidates from the last set remote
817 // description plus any candidates added since then. We should remove
818 // this once we're sure it won't break anything.
819 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800820 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800821 }
822 }
honghaiz503726c2015-07-31 12:37:38 -0700823 }
824
deadbeefd59daf82015-10-14 15:02:44 -0700825 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000826 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000828
829 // Set the the ICE connection state to connecting since the connection may
830 // become writable with peer reflexive candidates before any remote candidate
831 // is signaled.
832 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
833 // is to have a new signal the indicates a change in checking state from the
834 // transport and expose a new checking() member from transport that can be
835 // read to determine the current checking state. The existing SignalConnecting
836 // actually means "gathering candidates", so cannot be be used here.
837 if (desc->type() != SessionDescriptionInterface::kOffer &&
838 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
839 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
840 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return true;
842}
843
deadbeefd59daf82015-10-14 15:02:44 -0700844void WebRtcSession::LogState(State old_state, State new_state) {
845 LOG(LS_INFO) << "Session:" << id()
846 << " Old state:" << GetStateString(old_state)
847 << " New state:" << GetStateString(new_state);
848}
849
850void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800851 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700852 if (state != state_) {
853 LogState(state_, state);
854 state_ = state;
855 SignalState(this, state_);
856 }
857}
858
859void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800860 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700861 if (error != error_) {
862 error_ = error;
863 error_desc_ = error_desc;
864 }
865}
866
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867bool WebRtcSession::UpdateSessionState(
868 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800870 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700871
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 // If there's already a pending error then no state transition should happen.
873 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800874 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000875 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000877 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
878 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
deadbeefd59daf82015-10-14 15:02:44 -0700880 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
881 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000882 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700883 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000884 }
deadbeefd59daf82015-10-14 15:02:44 -0700885 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000886 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
888 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000889 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
890 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700893 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
894 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000895 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700896 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000897 }
deadbeefd59daf82015-10-14 15:02:44 -0700898 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000899 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700902 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800903 local_description()->description()->GetGroupByName(
904 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700905 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800906 remote_description()->description()->GetGroupByName(
907 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700908 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800909 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700910 const cricket::ContentGroup* answer_bundle =
911 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
912 if (!EnableBundle(*answer_bundle)) {
913 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
914 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
915 }
916 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800917 // Only push down the transport description after enabling BUNDLE; we don't
918 // want to push down a description on a transport about to be destroyed.
919 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
920 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
921 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700923 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000924 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700925 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000926 }
deadbeefd59daf82015-10-14 15:02:44 -0700927 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000928 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 }
930 }
931 return true;
932}
933
934WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
935 if (type == SessionDescriptionInterface::kOffer) {
936 return WebRtcSession::kOffer;
937 } else if (type == SessionDescriptionInterface::kPrAnswer) {
938 return WebRtcSession::kPrAnswer;
939 } else if (type == SessionDescriptionInterface::kAnswer) {
940 return WebRtcSession::kAnswer;
941 }
nisseede5da42017-01-12 05:15:36 -0800942 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 return WebRtcSession::kOffer;
944}
945
deadbeefd59daf82015-10-14 15:02:44 -0700946bool WebRtcSession::PushdownMediaDescription(
947 cricket::ContentAction action,
948 cricket::ContentSource source,
949 std::string* err) {
950 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
951 if (!ch) {
952 return true;
953 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800954 return ch->PushdownLocalDescription(local_description()->description(),
955 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700956 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800957 return ch->PushdownRemoteDescription(remote_description()->description(),
958 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700959 }
960 };
961
deadbeef953c2ce2017-01-09 14:53:41 -0800962 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
963 set_content(rtp_data_channel()));
964 // Need complete offer/answer with an SCTP m= section before starting SCTP,
965 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
966 if (sctp_transport_ && local_description() && remote_description() &&
967 cricket::GetFirstDataContent(local_description()->description()) &&
968 cricket::GetFirstDataContent(remote_description()->description())) {
969 ret &= network_thread_->Invoke<bool>(
970 RTC_FROM_HERE,
971 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
972 }
973 return ret;
974}
975
976bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
977 RTC_DCHECK(network_thread_->IsCurrent());
978 RTC_DCHECK(local_description());
979 RTC_DCHECK(remote_description());
980 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
981 // When we support "max-message-size", that would also be pushed down here.
982 return sctp_transport_->Start(
983 GetSctpPort(local_description()->description()),
984 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700985}
986
987bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
988 cricket::ContentAction action,
989 std::string* error_desc) {
990 RTC_DCHECK(signaling_thread()->IsCurrent());
991
992 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800993 return PushdownLocalTransportDescription(local_description()->description(),
994 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700995 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800996 return PushdownRemoteTransportDescription(remote_description()->description(),
997 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700998}
999
1000bool WebRtcSession::PushdownLocalTransportDescription(
1001 const SessionDescription* sdesc,
1002 cricket::ContentAction action,
1003 std::string* err) {
1004 RTC_DCHECK(signaling_thread()->IsCurrent());
1005
1006 if (!sdesc) {
1007 return false;
1008 }
1009
1010 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1011 if (!transport_controller_->SetLocalTransportDescription(
1012 tinfo.content_name, tinfo.description, action, err)) {
1013 return false;
1014 }
1015 }
1016
1017 return true;
1018}
1019
1020bool WebRtcSession::PushdownRemoteTransportDescription(
1021 const SessionDescription* sdesc,
1022 cricket::ContentAction action,
1023 std::string* err) {
1024 RTC_DCHECK(signaling_thread()->IsCurrent());
1025
1026 if (!sdesc) {
1027 return false;
1028 }
1029
1030 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1031 if (!transport_controller_->SetRemoteTransportDescription(
1032 tinfo.content_name, tinfo.description, action, err)) {
1033 return false;
1034 }
1035 }
1036
1037 return true;
1038}
1039
1040bool WebRtcSession::GetTransportDescription(
1041 const SessionDescription* description,
1042 const std::string& content_name,
1043 cricket::TransportDescription* tdesc) {
1044 if (!description || !tdesc) {
1045 return false;
1046 }
1047 const TransportInfo* transport_info =
1048 description->GetTransportInfoByName(content_name);
1049 if (!transport_info) {
1050 return false;
1051 }
1052 *tdesc = transport_info->description;
1053 return true;
1054}
1055
deadbeefcbecd352015-09-23 11:50:27 -07001056bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1057 const std::string* first_content_name = bundle.FirstContentName();
1058 if (!first_content_name) {
1059 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1060 return false;
1061 }
1062 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001063
zhihuang9763d562016-08-05 11:14:50 -07001064#ifdef HAVE_QUIC
1065 if (quic_data_transport_ &&
1066 bundle.HasContentName(quic_data_transport_->content_name()) &&
1067 quic_data_transport_->transport_name() != transport_name) {
1068 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1069 << " on " << transport_name << "with QUIC.";
1070 }
1071#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001072 auto maybe_set_transport = [this, bundle,
1073 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001074 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001075 return true;
1076 }
1077
zhihuangf5b251b2017-01-12 19:37:48 -08001078 std::string old_transport_name = ch->transport_name();
1079 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001080 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1081 << " on " << transport_name << ".";
1082 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001083 }
torbjornga81a42f2015-09-23 02:16:58 -07001084
zhihuangf5b251b2017-01-12 19:37:48 -08001085 cricket::TransportChannel* rtp_transport =
1086 transport_controller_->CreateTransportChannel(
1087 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1088 bool need_rtcp = (ch->rtcp_transport() != nullptr);
1089 cricket::TransportChannel* rtcp_transport = nullptr;
1090 if (need_rtcp) {
1091 rtcp_transport = transport_controller_->CreateTransportChannel_n(
1092 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1093 }
1094
1095 if (!ch->SetTransport(rtp_transport, rtcp_transport)) {
deadbeefcbecd352015-09-23 11:50:27 -07001096 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1097 return false;
1098 }
1099 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1100 << transport_name << ".";
zhihuangf5b251b2017-01-12 19:37:48 -08001101 DestroyTransport(old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1102 // If the channel needs rtcp, it means that the channel used to have a
1103 // rtcp transport which needs to be deleted now.
1104 if (need_rtcp) {
1105 DestroyTransport(old_transport_name,
1106 cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1107 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001108 return true;
1109 };
1110
deadbeefcbecd352015-09-23 11:50:27 -07001111 if (!maybe_set_transport(voice_channel()) ||
1112 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001113 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001114 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001115 }
deadbeef953c2ce2017-01-09 14:53:41 -08001116 // For SCTP, transport creation/deletion happens here instead of in the
1117 // object itself.
1118 if (sctp_transport_) {
1119 RTC_DCHECK(sctp_transport_name_);
1120 RTC_DCHECK(sctp_content_name_);
1121 if (transport_name != *sctp_transport_name_ &&
1122 bundle.HasContentName(*sctp_content_name_)) {
1123 network_thread_->Invoke<void>(
1124 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1125 transport_name));
1126 }
1127 }
deadbeefcbecd352015-09-23 11:50:27 -07001128
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001129 return true;
1130}
1131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001133 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001134 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1135 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001136 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 }
1138
1139 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001140 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 return false;
1142 }
1143
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001144 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001145 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1146 if (!valid) {
1147 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 }
1149
1150 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001151 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001152 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 return false;
1154 }
1155
deadbeefd59daf82015-10-14 15:02:44 -07001156 if (ready) {
1157 return UseCandidate(candidate);
1158 } else {
1159 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1160 return true;
1161 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162}
1163
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001164bool WebRtcSession::RemoveRemoteIceCandidates(
1165 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001166 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001167 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1168 << "removed without any remote session description.";
1169 return false;
1170 }
1171
1172 if (candidates.empty()) {
1173 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1174 return false;
1175 }
1176
deadbeeffe4a8a42016-12-20 17:56:17 -08001177 size_t number_removed =
1178 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001179 if (number_removed != candidates.size()) {
1180 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1181 << "Requested " << candidates.size() << " but only "
1182 << number_removed << " are removed.";
1183 }
1184
1185 // Remove the candidates from the transport controller.
1186 std::string error;
1187 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1188 if (!res && !error.empty()) {
1189 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1190 }
1191 return true;
1192}
1193
deadbeefd59daf82015-10-14 15:02:44 -07001194cricket::IceConfig WebRtcSession::ParseIceConfig(
1195 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001196 cricket::ContinualGatheringPolicy gathering_policy;
1197 // TODO(honghaiz): Add the third continual gathering policy in
1198 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1199 switch (config.continual_gathering_policy) {
1200 case PeerConnectionInterface::GATHER_ONCE:
1201 gathering_policy = cricket::GATHER_ONCE;
1202 break;
1203 case PeerConnectionInterface::GATHER_CONTINUALLY:
1204 gathering_policy = cricket::GATHER_CONTINUALLY;
1205 break;
1206 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001207 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001208 gathering_policy = cricket::GATHER_ONCE;
1209 }
deadbeefd59daf82015-10-14 15:02:44 -07001210 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001211 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001212 ice_config.prioritize_most_likely_candidate_pairs =
1213 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001214 ice_config.backup_connection_ping_interval =
1215 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001216 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001217 ice_config.presume_writable_when_fully_relayed =
1218 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001219 return ice_config;
1220}
1221
1222void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1223 transport_controller_->SetIceConfig(config);
1224}
1225
1226void WebRtcSession::MaybeStartGathering() {
1227 transport_controller_->MaybeStartGathering();
1228}
1229
Peter Boström0c4e06b2015-10-07 12:23:21 +02001230bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1231 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001232 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001234 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001235 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1236 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237}
1238
Peter Boström0c4e06b2015-10-07 12:23:21 +02001239bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1240 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001241 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001242 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001243 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001244 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1245 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246}
1247
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001248std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001250 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 return desc.str();
1252}
1253
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
nisseede5da42017-01-12 05:15:36 -08001255 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 if (!voice_channel_) {
1257 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1258 return false;
1259 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001260 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1262 // exists.
deadbeeffe4a8a42016-12-20 17:56:17 -08001263 if (!local_description() ||
1264 !GetAudioSsrcByTrackId(local_description()->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 &send_ssrc)) {
1266 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1267 return false;
1268 }
1269 return voice_channel_->CanInsertDtmf();
1270}
1271
1272bool WebRtcSession::InsertDtmf(const std::string& track_id,
1273 int code, int duration) {
nisseede5da42017-01-12 05:15:36 -08001274 RTC_DCHECK(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 if (!voice_channel_) {
1276 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1277 return false;
1278 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001279 uint32_t send_ssrc = 0;
deadbeeffe4a8a42016-12-20 17:56:17 -08001280 if (!VERIFY(local_description() &&
1281 GetAudioSsrcByTrackId(local_description()->description(),
1282 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1284 return false;
1285 }
solenberg1d63dd02015-12-02 12:35:09 -08001286 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1288 return false;
1289 }
1290 return true;
1291}
1292
1293sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001294 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295}
1296
wu@webrtc.org78187522013-10-07 23:32:02 +00001297bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001298 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001299 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001300 if (!rtp_data_channel_ && !sctp_transport_) {
1301 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1302 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001303 return false;
1304 }
deadbeef953c2ce2017-01-09 14:53:41 -08001305 return rtp_data_channel_
1306 ? rtp_data_channel_->SendData(params, payload, result)
1307 : network_thread_->Invoke<bool>(
1308 RTC_FROM_HERE,
1309 Bind(&cricket::SctpTransportInternal::SendData,
1310 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001311}
1312
1313bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001314 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001315 // Don't log an error here, because DataChannels are expected to call
1316 // ConnectDataChannel in this state. It's the only way to initially tell
1317 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001318 return false;
1319 }
deadbeef953c2ce2017-01-09 14:53:41 -08001320 if (rtp_data_channel_) {
1321 rtp_data_channel_->SignalReadyToSendData.connect(
1322 webrtc_data_channel, &DataChannel::OnChannelReady);
1323 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1324 &DataChannel::OnDataReceived);
1325 } else {
1326 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1327 &DataChannel::OnChannelReady);
1328 SignalSctpDataReceived.connect(webrtc_data_channel,
1329 &DataChannel::OnDataReceived);
1330 SignalSctpStreamClosedRemotely.connect(
1331 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1332 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001333 return true;
1334}
1335
1336void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001337 if (!rtp_data_channel_ && !sctp_transport_) {
1338 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1339 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001340 return;
1341 }
deadbeef953c2ce2017-01-09 14:53:41 -08001342 if (rtp_data_channel_) {
1343 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1344 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1345 } else {
1346 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1347 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1348 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1349 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001350}
1351
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001352void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001353 if (!sctp_transport_) {
1354 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001355 return;
1356 }
deadbeef953c2ce2017-01-09 14:53:41 -08001357 network_thread_->Invoke<void>(
1358 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1359 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001360}
1361
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001362void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001363 if (!sctp_transport_) {
1364 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001365 << "NULL.";
1366 return;
1367 }
deadbeef953c2ce2017-01-09 14:53:41 -08001368 network_thread_->Invoke<void>(
1369 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1370 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001371}
1372
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001373bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001374 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1375 sctp_ready_to_send_data_;
1376}
1377
1378std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001379 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001380 ChannelNamePairs channel_name_pairs;
1381 if (voice_channel()) {
1382 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1383 voice_channel()->content_name(), voice_channel()->transport_name()));
1384 }
1385 if (video_channel()) {
1386 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1387 video_channel()->content_name(), video_channel()->transport_name()));
1388 }
1389 if (rtp_data_channel()) {
1390 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1391 ChannelNamePair(rtp_data_channel()->content_name(),
1392 rtp_data_channel()->transport_name()));
1393 }
1394 if (sctp_transport_) {
1395 RTC_DCHECK(sctp_content_name_);
1396 RTC_DCHECK(sctp_transport_name_);
1397 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1398 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1399 }
1400 return GetStats(channel_name_pairs);
1401}
1402
1403std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1404 const ChannelNamePairs& channel_name_pairs) {
1405 if (network_thread()->IsCurrent()) {
1406 return GetStats_n(channel_name_pairs);
1407 }
1408 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1409 RTC_FROM_HERE,
1410 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1411}
1412
1413bool WebRtcSession::GetLocalCertificate(
1414 const std::string& transport_name,
1415 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1416 return transport_controller_->GetLocalCertificate(transport_name,
1417 certificate);
1418}
1419
1420std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1421 const std::string& transport_name) {
1422 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001423}
1424
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425cricket::DataChannelType WebRtcSession::data_channel_type() const {
1426 return data_channel_type_;
1427}
1428
deadbeef0ed85b22016-02-23 17:24:52 -08001429bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1430 return pending_ice_restarts_.find(content_name) !=
1431 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001432}
1433
deadbeefd1a38b52016-12-10 13:15:33 -08001434void WebRtcSession::SetNeedsIceRestartFlag() {
1435 transport_controller_->SetNeedsIceRestartFlag();
1436}
1437
1438bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1439 return transport_controller_->NeedsIceRestart(content_name);
1440}
1441
Henrik Boströmd8281982015-08-27 10:12:24 +02001442void WebRtcSession::OnCertificateReady(
1443 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001444 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001445}
1446
deadbeef953c2ce2017-01-09 14:53:41 -08001447void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1448 SetError(ERROR_TRANSPORT,
1449 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1450}
1451
Henrik Boströmd8281982015-08-27 10:12:24 +02001452bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001453 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001454}
1455
deadbeefcbecd352015-09-23 11:50:27 -07001456const rtc::scoped_refptr<rtc::RTCCertificate>&
1457WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001458 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001459}
1460
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461void WebRtcSession::SetIceConnectionState(
1462 PeerConnectionInterface::IceConnectionState state) {
1463 if (ice_connection_state_ == state) {
1464 return;
1465 }
1466
deadbeefcbecd352015-09-23 11:50:27 -07001467 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1468 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001469 RTC_DCHECK(ice_connection_state_ !=
1470 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 ice_connection_state_ = state;
1472 if (ice_observer_) {
1473 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1474 }
1475}
1476
deadbeefcbecd352015-09-23 11:50:27 -07001477void WebRtcSession::OnTransportControllerConnectionState(
1478 cricket::IceConnectionState state) {
1479 switch (state) {
1480 case cricket::kIceConnectionConnecting:
1481 // If the current state is Connected or Completed, then there were
1482 // writable channels but now there are not, so the next state must
1483 // be Disconnected.
1484 // kIceConnectionConnecting is currently used as the default,
1485 // un-connected state by the TransportController, so its only use is
1486 // detecting disconnections.
1487 if (ice_connection_state_ ==
1488 PeerConnectionInterface::kIceConnectionConnected ||
1489 ice_connection_state_ ==
1490 PeerConnectionInterface::kIceConnectionCompleted) {
1491 SetIceConnectionState(
1492 PeerConnectionInterface::kIceConnectionDisconnected);
1493 }
torbjornga81a42f2015-09-23 02:16:58 -07001494 break;
deadbeefcbecd352015-09-23 11:50:27 -07001495 case cricket::kIceConnectionFailed:
1496 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1497 break;
1498 case cricket::kIceConnectionConnected:
1499 LOG(LS_INFO) << "Changing to ICE connected state because "
1500 << "all transports are writable.";
1501 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1502 break;
1503 case cricket::kIceConnectionCompleted:
1504 LOG(LS_INFO) << "Changing to ICE completed state because "
1505 << "all transports are complete.";
1506 if (ice_connection_state_ !=
1507 PeerConnectionInterface::kIceConnectionConnected) {
1508 // If jumping directly from "checking" to "connected",
1509 // signal "connected" first.
1510 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1511 }
1512 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1513 if (metrics_observer_) {
1514 ReportTransportStats();
1515 }
1516 break;
1517 default:
nissec80e7412017-01-11 05:56:46 -08001518 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001519 }
deadbeefcbecd352015-09-23 11:50:27 -07001520}
1521
1522void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001523 SetIceConnectionReceiving(receiving);
1524}
1525
1526void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1527 if (ice_connection_receiving_ == receiving) {
1528 return;
1529 }
1530 ice_connection_receiving_ = receiving;
1531 if (ice_observer_) {
1532 ice_observer_->OnIceConnectionReceivingChange(receiving);
1533 }
1534}
1535
deadbeefcbecd352015-09-23 11:50:27 -07001536void WebRtcSession::OnTransportControllerCandidatesGathered(
1537 const std::string& transport_name,
1538 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001539 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001540 int sdp_mline_index;
1541 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1542 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1543 << transport_name << " not found";
1544 return;
1545 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546
deadbeefcbecd352015-09-23 11:50:27 -07001547 for (cricket::Candidates::const_iterator citer = candidates.begin();
1548 citer != candidates.end(); ++citer) {
1549 // Use transport_name as the candidate media id.
1550 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1551 if (ice_observer_) {
1552 ice_observer_->OnIceCandidate(&candidate);
1553 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001554 if (local_description()) {
1555 mutable_local_description()->AddCandidate(&candidate);
deadbeefcbecd352015-09-23 11:50:27 -07001556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 }
1558}
1559
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001560void WebRtcSession::OnTransportControllerCandidatesRemoved(
1561 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001562 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001563 // Sanity check.
1564 for (const cricket::Candidate& candidate : candidates) {
1565 if (candidate.transport_name().empty()) {
1566 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1567 << "empty content name in candidate "
1568 << candidate.ToString();
1569 return;
1570 }
1571 }
1572
deadbeeffe4a8a42016-12-20 17:56:17 -08001573 if (local_description()) {
1574 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001575 }
1576 if (ice_observer_) {
1577 ice_observer_->OnIceCandidatesRemoved(candidates);
1578 }
1579}
1580
deadbeef953c2ce2017-01-09 14:53:41 -08001581void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1582 rtc::SSLHandshakeError error) {
1583 if (metrics_observer_) {
1584 metrics_observer_->IncrementEnumCounter(
1585 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1586 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1587 }
1588}
1589
1590// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591void WebRtcSession::EnableChannels() {
1592 if (voice_channel_ && !voice_channel_->enabled())
1593 voice_channel_->Enable(true);
1594
1595 if (video_channel_ && !video_channel_->enabled())
1596 video_channel_->Enable(true);
1597
deadbeef953c2ce2017-01-09 14:53:41 -08001598 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1599 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600}
1601
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602// Returns the media index for a local ice candidate given the content name.
1603bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1604 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001605 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
1609 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001610 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 for (size_t index = 0; index < contents.size(); ++index) {
1612 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001613 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 content_found = true;
1615 break;
1616 }
1617 }
1618 return content_found;
1619}
1620
1621bool WebRtcSession::UseCandidatesInSessionDescription(
1622 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001623 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001627
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1629 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001630 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001631 const IceCandidateInterface* candidate = candidates->at(n);
1632 bool valid = false;
1633 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1634 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001635 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1636 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001637 }
1638 continue;
1639 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001640 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001641 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 break;
deadbeefd59daf82015-10-14 15:02:44 -07001643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 }
1645 }
1646 return ret;
1647}
1648
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001649bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001651 size_t remote_content_size =
1652 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001654 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 return false;
1656 }
1657
1658 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001659 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 std::vector<cricket::Candidate> candidates;
1661 candidates.push_back(candidate->candidate());
1662 // Invoking BaseSession method to handle remote candidates.
1663 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001664 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1665 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 // Candidates successfully submitted for checking.
1667 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1668 ice_connection_state_ ==
1669 PeerConnectionInterface::kIceConnectionDisconnected) {
1670 // If state is New, then the session has just gotten its first remote ICE
1671 // candidates, so go to Checking.
1672 // If state is Disconnected, the session is re-using old candidates or
1673 // receiving additional ones, so go to Checking.
1674 // If state is Connected, stay Connected.
1675 // TODO(bemasc): If state is Connected, and the new candidates are for a
1676 // newly added transport, then the state actually _should_ move to
1677 // checking. Add a way to distinguish that case.
1678 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1679 }
1680 // TODO(bemasc): If state is Completed, go back to Connected.
1681 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001682 if (!error.empty()) {
1683 LOG(LS_WARNING) << error;
1684 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 }
1686 return true;
1687}
1688
deadbeefcbecd352015-09-23 11:50:27 -07001689void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001690 // Destroy video_channel_ first since it may have a pointer to the
1691 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 const cricket::ContentInfo* video_info =
1693 cricket::GetFirstVideoContent(desc);
1694 if ((!video_info || video_info->rejected) && video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001695 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 }
1697
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001698 const cricket::ContentInfo* voice_info =
1699 cricket::GetFirstAudioContent(desc);
1700 if ((!voice_info || voice_info->rejected) && voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001701 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001702 }
1703
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 const cricket::ContentInfo* data_info =
1705 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001706 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001707 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001708 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001709 }
1710 if (sctp_transport_) {
1711 SignalDataChannelDestroyed();
1712 network_thread_->Invoke<void>(
1713 RTC_FROM_HERE,
1714 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001715 }
1716#ifdef HAVE_QUIC
1717 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1718 if (quic_data_transport_) {
1719 quic_data_transport_.reset();
1720 }
1721#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 }
1723}
1724
skvlad6c87a672016-05-17 17:49:52 -07001725// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1726// if the channel is not part of any bundle.
1727const std::string* WebRtcSession::GetBundleTransportName(
1728 const cricket::ContentInfo* content,
1729 const cricket::ContentGroup* bundle) {
1730 if (!bundle) {
1731 return nullptr;
1732 }
1733 const std::string* first_content_name = bundle->FirstContentName();
1734 if (!first_content_name) {
1735 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1736 return nullptr;
1737 }
1738 if (!bundle->HasContentName(content->name)) {
1739 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1740 return nullptr;
1741 }
1742 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1743 return first_content_name;
1744}
1745
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001747 const cricket::ContentGroup* bundle_group = nullptr;
1748 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1749 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1750 if (!bundle_group) {
1751 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1752 return false;
1753 }
1754 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 // Creating the media channels and transport proxies.
1756 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1757 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001758 if (!CreateVoiceChannel(voice,
1759 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 LOG(LS_ERROR) << "Failed to create voice channel.";
1761 return false;
1762 }
1763 }
1764
1765 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1766 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001767 if (!CreateVideoChannel(video,
1768 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 LOG(LS_ERROR) << "Failed to create video channel.";
1770 return false;
1771 }
1772 }
1773
1774 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001775 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1776 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001777 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 LOG(LS_ERROR) << "Failed to create data channel.";
1779 return false;
1780 }
1781 }
1782
1783 return true;
1784}
1785
skvlad6c87a672016-05-17 17:49:52 -07001786bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1787 const std::string* bundle_transport) {
1788 bool require_rtcp_mux =
1789 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001790
1791 std::string transport_name =
1792 bundle_transport ? *bundle_transport : content->name;
1793
1794 cricket::TransportChannel* rtp_transport =
1795 transport_controller_->CreateTransportChannel(
1796 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1797 cricket::TransportChannel* rtcp_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001798 if (!require_rtcp_mux) {
zhihuangf5b251b2017-01-12 19:37:48 -08001799 rtcp_transport = transport_controller_->CreateTransportChannel(
1800 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1801 }
1802
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
zhihuangf5b251b2017-01-12 19:37:48 -08001804 media_controller_, rtp_transport, rtcp_transport,
1805 transport_controller_->signaling_thread(), content->name,
deadbeefac22f702017-01-12 21:59:29 -08001806 bundle_transport, require_rtcp_mux, SrtpRequired(), audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001807 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001808 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001809 }
zhihuangf5b251b2017-01-12 19:37:48 -08001810
deadbeefac22f702017-01-12 21:59:29 -08001811 voice_channel_->SignalRtcpMuxFullyActive.connect(
1812 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001813 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1814 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001815
1816 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001817 voice_channel_->SignalSentPacket.connect(this,
1818 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001819 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820}
1821
skvlad6c87a672016-05-17 17:49:52 -07001822bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1823 const std::string* bundle_transport) {
1824 bool require_rtcp_mux =
1825 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001826
1827 std::string transport_name =
1828 bundle_transport ? *bundle_transport : content->name;
1829
1830 cricket::TransportChannel* rtp_transport =
1831 transport_controller_->CreateTransportChannel(
1832 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1833 cricket::TransportChannel* rtcp_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001834 if (!require_rtcp_mux) {
zhihuangf5b251b2017-01-12 19:37:48 -08001835 rtcp_transport = transport_controller_->CreateTransportChannel(
1836 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1837 }
1838
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 video_channel_.reset(channel_manager_->CreateVideoChannel(
zhihuangf5b251b2017-01-12 19:37:48 -08001840 media_controller_, rtp_transport, rtcp_transport,
1841 transport_controller_->signaling_thread(), content->name,
deadbeefac22f702017-01-12 21:59:29 -08001842 bundle_transport, require_rtcp_mux, SrtpRequired(), video_options_));
zhihuangf5b251b2017-01-12 19:37:48 -08001843
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001844 if (!video_channel_) {
1845 return false;
1846 }
zhihuangf5b251b2017-01-12 19:37:48 -08001847
deadbeefac22f702017-01-12 21:59:29 -08001848 video_channel_->SignalRtcpMuxFullyActive.connect(
1849 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001850 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1851 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001852
1853 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001854 video_channel_->SignalSentPacket.connect(this,
1855 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001856 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857}
1858
skvlad6c87a672016-05-17 17:49:52 -07001859bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1860 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001861 const std::string transport_name =
1862 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001863#ifdef HAVE_QUIC
1864 if (data_channel_type_ == cricket::DCT_QUIC) {
1865 RTC_DCHECK(transport_controller_->quic());
zhihuang9763d562016-08-05 11:14:50 -07001866 quic_data_transport_->SetTransport(transport_name);
1867 return true;
1868 }
1869#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001870 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001871 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001872 if (!sctp_factory_) {
1873 LOG(LS_ERROR)
1874 << "Trying to create SCTP transport, but didn't compile with "
1875 "SCTP support (HAVE_SCTP)";
1876 return false;
1877 }
1878 if (!network_thread_->Invoke<bool>(
1879 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1880 this, content->name, transport_name))) {
1881 return false;
1882 };
1883 } else {
1884 bool require_rtcp_mux =
1885 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001886
1887 std::string transport_name =
1888 bundle_transport ? *bundle_transport : content->name;
1889 cricket::TransportChannel* rtp_transport =
1890 transport_controller_->CreateTransportChannel(
1891 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1892 cricket::TransportChannel* rtcp_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001893 if (!require_rtcp_mux) {
zhihuangf5b251b2017-01-12 19:37:48 -08001894 rtcp_transport = transport_controller_->CreateTransportChannel(
1895 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1896 }
1897
deadbeef953c2ce2017-01-09 14:53:41 -08001898 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
zhihuangf5b251b2017-01-12 19:37:48 -08001899 media_controller_, rtp_transport, rtcp_transport,
1900 transport_controller_->signaling_thread(), content->name,
deadbeefac22f702017-01-12 21:59:29 -08001901 bundle_transport, require_rtcp_mux, SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001902
deadbeef953c2ce2017-01-09 14:53:41 -08001903 if (!rtp_data_channel_) {
1904 return false;
1905 }
zhihuangf5b251b2017-01-12 19:37:48 -08001906
deadbeefac22f702017-01-12 21:59:29 -08001907 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1908 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001909 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1910 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1911 rtp_data_channel_->SignalSentPacket.connect(this,
1912 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001913 }
1914
deadbeefab9b2d12015-10-14 11:33:11 -07001915 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001916
wu@webrtc.org91053e72013-08-10 07:18:04 +00001917 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918}
1919
hbosdf6075a2016-12-19 04:58:02 -08001920std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1921 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001922 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001923 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1924 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1925 &channel_name_pairs.video,
1926 &channel_name_pairs.data }) {
1927 if (*channel_name_pair) {
1928 cricket::TransportStats transport_stats;
1929 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1930 &transport_stats)) {
1931 return nullptr;
1932 }
1933 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1934 (*channel_name_pair)->transport_name;
1935 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1936 std::move(transport_stats);
1937 }
1938 }
1939 return session_stats;
1940}
1941
deadbeef953c2ce2017-01-09 14:53:41 -08001942bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1943 const std::string& transport_name) {
1944 RTC_DCHECK(network_thread_->IsCurrent());
1945 RTC_DCHECK(sctp_factory_);
1946 cricket::TransportChannel* tc =
1947 transport_controller_->CreateTransportChannel_n(
1948 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1949 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1950 RTC_DCHECK(sctp_transport_);
1951 sctp_invoker_.reset(new rtc::AsyncInvoker());
1952 sctp_transport_->SignalReadyToSendData.connect(
1953 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1954 sctp_transport_->SignalDataReceived.connect(
1955 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1956 sctp_transport_->SignalStreamClosedRemotely.connect(
1957 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1958 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1959 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1960 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961}
1962
deadbeef953c2ce2017-01-09 14:53:41 -08001963void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1964 RTC_DCHECK(network_thread_->IsCurrent());
1965 RTC_DCHECK(sctp_transport_);
1966 RTC_DCHECK(sctp_transport_name_);
1967 std::string old_sctp_transport_name = *sctp_transport_name_;
1968 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1969 cricket::TransportChannel* tc =
1970 transport_controller_->CreateTransportChannel_n(
1971 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1972 sctp_transport_->SetTransportChannel(tc);
1973 transport_controller_->DestroyTransportChannel_n(
1974 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1975}
1976
1977void WebRtcSession::DestroySctpTransport_n() {
1978 RTC_DCHECK(network_thread_->IsCurrent());
1979 sctp_transport_.reset(nullptr);
1980 sctp_content_name_.reset();
1981 sctp_transport_name_.reset();
1982 sctp_invoker_.reset(nullptr);
1983 sctp_ready_to_send_data_ = false;
1984}
1985
1986void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1987 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1988 RTC_DCHECK(network_thread_->IsCurrent());
1989 sctp_invoker_->AsyncInvoke<void>(
1990 RTC_FROM_HERE, signaling_thread_,
1991 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1992}
1993
1994void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1995 RTC_DCHECK(signaling_thread_->IsCurrent());
1996 sctp_ready_to_send_data_ = ready;
1997 SignalSctpReadyToSendData(ready);
1998}
1999
2000void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002001 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002002 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07002003 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08002004 RTC_DCHECK(network_thread_->IsCurrent());
2005 sctp_invoker_->AsyncInvoke<void>(
2006 RTC_FROM_HERE, signaling_thread_,
2007 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2008 payload));
2009}
2010
2011void WebRtcSession::OnSctpTransportDataReceived_s(
2012 const cricket::ReceiveDataParams& params,
2013 const rtc::CopyOnWriteBuffer& payload) {
2014 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002015 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2016 // Received OPEN message; parse and signal that a new data channel should
2017 // be created.
2018 std::string label;
2019 InternalDataChannelInit config;
2020 config.id = params.ssrc;
2021 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2022 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2023 << params.ssrc;
2024 return;
2025 }
2026 config.open_handshake_role = InternalDataChannelInit::kAcker;
2027 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002028 } else {
2029 // Otherwise just forward the signal.
2030 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 }
deadbeef953c2ce2017-01-09 14:53:41 -08002032}
2033
2034void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2035 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2036 RTC_DCHECK(network_thread_->IsCurrent());
2037 sctp_invoker_->AsyncInvoke<void>(
2038 RTC_FROM_HERE, signaling_thread_,
2039 rtc::Bind(&sigslot::signal1<int>::operator(),
2040 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041}
2042
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002043// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002044bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002045 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2046 if (!bundle_enabled)
2047 return true;
2048
2049 const cricket::ContentGroup* bundle_group =
2050 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002051 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002052
2053 const cricket::ContentInfos& contents = desc->contents();
2054 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2055 citer != contents.end(); ++citer) {
2056 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002057 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002058 if (bundle_group->HasContentName(content->name) &&
2059 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2060 if (!HasRtcpMuxEnabled(content))
2061 return false;
2062 }
2063 }
2064 // RTCP-MUX is enabled in all the contents.
2065 return true;
2066}
2067
2068bool WebRtcSession::HasRtcpMuxEnabled(
2069 const cricket::ContentInfo* content) {
2070 const cricket::MediaContentDescription* description =
2071 static_cast<cricket::MediaContentDescription*>(content->description);
2072 return description->rtcp_mux();
2073}
2074
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002075bool WebRtcSession::ValidateSessionDescription(
2076 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002077 cricket::ContentSource source, std::string* err_desc) {
2078 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002079 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002081 }
2082
2083 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002084 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002085 }
2086
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002087 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002088 Action action = GetAction(sdesc->type());
2089 if (source == cricket::CS_LOCAL) {
2090 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002091 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002092 } else {
2093 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002094 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002095 }
2096
2097 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002098 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002099 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2100 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002101 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002102 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002103 }
2104
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002105 // Verify ice-ufrag and ice-pwd.
2106 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002107 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002108 }
2109
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002110 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002111 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002112 }
2113
skvlad6c87a672016-05-17 17:49:52 -07002114 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2115 // m-lines that do not rtcp-mux enabled.
2116
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002117 // Verify m-lines in Answer when compared against Offer.
2118 if (action == kAnswer) {
2119 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002120 (source == cricket::CS_LOCAL) ? remote_description()->description()
2121 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002122 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002123 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002124 }
2125 }
2126
2127 return true;
2128}
2129
2130bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2131 return ((action == kOffer && state() == STATE_INIT) ||
2132 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002133 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002134 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002135 (action == kOffer && state() == STATE_INPROGRESS) ||
2136 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002137 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2138 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2139 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2140 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002141}
2142
2143bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2144 return ((action == kOffer && state() == STATE_INIT) ||
2145 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002146 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002147 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002148 (action == kOffer && state() == STATE_INPROGRESS) ||
2149 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002150 (action == kAnswer && state() == STATE_SENTOFFER) ||
2151 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2152 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2153 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002154}
2155
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002156std::string WebRtcSession::GetSessionErrorMsg() {
2157 std::ostringstream desc;
2158 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2159 desc << kSessionErrorDesc << error_desc() << ".";
2160 return desc.str();
2161}
2162
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002163// We need to check the local/remote description for the Transport instead of
2164// the session, because a new Transport added during renegotiation may have
2165// them unset while the session has them set from the previous negotiation.
2166// Not doing so may trigger the auto generation of transport description and
2167// mess up DTLS identity information, ICE credential, etc.
2168bool WebRtcSession::ReadyToUseRemoteCandidate(
2169 const IceCandidateInterface* candidate,
2170 const SessionDescriptionInterface* remote_desc,
2171 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002172 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002173
2174 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002175 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002176
deadbeefd59daf82015-10-14 15:02:44 -07002177 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002178 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002179 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002180
2181 size_t mediacontent_index =
2182 static_cast<size_t>(candidate->sdp_mline_index());
2183 size_t remote_content_size =
2184 current_remote_desc->description()->contents().size();
2185 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002186 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2187 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002188
2189 *valid = false;
2190 return false;
2191 }
2192
2193 cricket::ContentInfo content =
2194 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002195
2196 const std::string transport_name = GetTransportName(content.name);
2197 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002198 return false;
2199 }
zhihuang9763d562016-08-05 11:14:50 -07002200 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002201}
2202
deadbeef7af91dd2016-12-13 11:29:11 -08002203bool WebRtcSession::SrtpRequired() const {
2204 return dtls_enabled_ ||
2205 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2206}
2207
deadbeefcbecd352015-09-23 11:50:27 -07002208void WebRtcSession::OnTransportControllerGatheringState(
2209 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002210 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002211 if (state == cricket::kIceGatheringGathering) {
2212 if (ice_observer_) {
2213 ice_observer_->OnIceGatheringChange(
2214 PeerConnectionInterface::kIceGatheringGathering);
2215 }
2216 } else if (state == cricket::kIceGatheringComplete) {
2217 if (ice_observer_) {
2218 ice_observer_->OnIceGatheringChange(
2219 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002220 }
2221 }
2222}
2223
2224void WebRtcSession::ReportTransportStats() {
2225 // Use a set so we don't report the same stats twice if two channels share
2226 // a transport.
2227 std::set<std::string> transport_names;
2228 if (voice_channel()) {
2229 transport_names.insert(voice_channel()->transport_name());
2230 }
2231 if (video_channel()) {
2232 transport_names.insert(video_channel()->transport_name());
2233 }
deadbeef953c2ce2017-01-09 14:53:41 -08002234 if (rtp_data_channel()) {
2235 transport_names.insert(rtp_data_channel()->transport_name());
2236 }
2237 if (sctp_transport_name_) {
2238 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002239 }
2240 for (const auto& name : transport_names) {
2241 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002242 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002243 ReportBestConnectionState(stats);
2244 ReportNegotiatedCiphers(stats);
2245 }
2246 }
2247}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002248// Walk through the ConnectionInfos to gather best connection usage
2249// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002250void WebRtcSession::ReportBestConnectionState(
2251 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002252 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002253 for (cricket::TransportChannelStatsList::const_iterator it =
2254 stats.channel_stats.begin();
2255 it != stats.channel_stats.end(); ++it) {
2256 for (cricket::ConnectionInfos::const_iterator it_info =
2257 it->connection_infos.begin();
2258 it_info != it->connection_infos.end(); ++it_info) {
2259 if (!it_info->best_connection) {
2260 continue;
2261 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002262
2263 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2264 const cricket::Candidate& local = it_info->local_candidate;
2265 const cricket::Candidate& remote = it_info->remote_candidate;
2266
2267 // Increment the counter for IceCandidatePairType.
2268 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2269 (local.type() == RELAY_PORT_TYPE &&
2270 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2271 type = kEnumCounterIceCandidatePairTypeTcp;
2272 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2273 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002274 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002275 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002276 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002277 metrics_observer_->IncrementEnumCounter(
2278 type, GetIceCandidatePairCounter(local, remote),
2279 kIceCandidatePairMax);
2280
2281 // Increment the counter for IP type.
2282 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002283 metrics_observer_->IncrementEnumCounter(
2284 kEnumCounterAddressFamily, kBestConnections_IPv4,
2285 kPeerConnectionAddressFamilyCounter_Max);
2286
2287 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002288 metrics_observer_->IncrementEnumCounter(
2289 kEnumCounterAddressFamily, kBestConnections_IPv6,
2290 kPeerConnectionAddressFamilyCounter_Max);
2291 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002292 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002293 }
2294
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002295 return;
2296 }
2297 }
2298}
2299
jbauchac8869e2015-07-03 01:36:14 -07002300void WebRtcSession::ReportNegotiatedCiphers(
2301 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002302 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002303 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2304 return;
2305 }
2306
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002307 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2308 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2309 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2310 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002311 return;
2312 }
2313
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002314 PeerConnectionEnumCounterType srtp_counter_type;
2315 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002316 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002317 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2318 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002319 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002320 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2321 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002322 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002323 srtp_counter_type = kEnumCounterDataSrtpCipher;
2324 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002325 } else {
2326 RTC_NOTREACHED();
2327 return;
2328 }
2329
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002330 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2331 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2332 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002333 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002334 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2335 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2336 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002337 }
2338}
2339
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002340void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002341 RTC_DCHECK(worker_thread()->IsCurrent());
2342 media_controller_->call_w()->OnSentPacket(sent_packet);
2343}
2344
zhihuang9763d562016-08-05 11:14:50 -07002345const std::string WebRtcSession::GetTransportName(
2346 const std::string& content_name) {
2347 cricket::BaseChannel* channel = GetChannel(content_name);
2348 if (!channel) {
2349#ifdef HAVE_QUIC
2350 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2351 content_name == quic_data_transport_->transport_name()) {
2352 return quic_data_transport_->transport_name();
2353 }
2354#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002355 if (sctp_transport_) {
2356 RTC_DCHECK(sctp_content_name_);
2357 RTC_DCHECK(sctp_transport_name_);
2358 if (content_name == *sctp_content_name_) {
2359 return *sctp_transport_name_;
2360 }
2361 }
zhihuang9763d562016-08-05 11:14:50 -07002362 // Return an empty string if failed to retrieve the transport name.
2363 return "";
2364 }
2365 return channel->transport_name();
2366}
zhihuangd82eee02016-08-26 11:25:05 -07002367
zhihuangf5b251b2017-01-12 19:37:48 -08002368void WebRtcSession::DestroyTransport(const std::string& transport_name,
2369 int component) {
2370 network_thread_->Invoke<void>(
2371 RTC_FROM_HERE,
2372 rtc::Bind(&cricket::TransportController::DestroyTransportChannel_n,
2373 transport_controller_.get(), transport_name, component));
2374}
2375
deadbeefac22f702017-01-12 21:59:29 -08002376void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002377 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangf5b251b2017-01-12 19:37:48 -08002378 transport_controller_->DestroyTransportChannel_n(
2379 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2380}
2381
2382void WebRtcSession::DestroyVideoChannel() {
2383 SignalVideoChannelDestroyed();
2384 RTC_DCHECK(video_channel_->rtp_transport());
2385 std::string transport_name;
2386 transport_name = video_channel_->rtp_transport()->transport_name();
2387 bool need_to_delete_rtcp = (video_channel_->rtcp_transport() != nullptr);
2388 channel_manager_->DestroyVideoChannel(video_channel_.release());
2389 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2390 if (need_to_delete_rtcp) {
2391 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2392 }
2393}
2394
2395void WebRtcSession::DestroyVoiceChannel() {
2396 SignalVoiceChannelDestroyed();
2397 RTC_DCHECK(voice_channel_->rtp_transport());
2398 std::string transport_name;
2399 transport_name = voice_channel_->rtp_transport()->transport_name();
2400 bool need_to_delete_rtcp = (voice_channel_->rtcp_transport() != nullptr);
2401 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
2402 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2403 if (need_to_delete_rtcp) {
2404 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2405 }
2406}
2407
2408void WebRtcSession::DestroyDataChannel() {
2409 SignalDataChannelDestroyed();
2410 RTC_DCHECK(rtp_data_channel_->rtp_transport());
2411 std::string transport_name;
2412 transport_name = rtp_data_channel_->rtp_transport()->transport_name();
2413 bool need_to_delete_rtcp = (rtp_data_channel_->rtcp_transport() != nullptr);
2414 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
2415 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2416 if (need_to_delete_rtcp) {
2417 DestroyTransport(transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2418 }
2419}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420} // namespace webrtc