blob: c6046e905fd48e3a57b98315d49bebe091b56dfd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_sink.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/peerconnectioninterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000024#include "webrtc/base/basictypes.h"
zhihuang9763d562016-08-05 11:14:50 -070025#include "webrtc/base/bind.h"
jbauchac8869e2015-07-03 01:36:14 -070026#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/helpers.h"
28#include "webrtc/base/logging.h"
29#include "webrtc/base/stringencode.h"
30#include "webrtc/base/stringutils.h"
ossuf515ab82016-12-07 04:52:58 -080031#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080032#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/videocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080034#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000035#include "webrtc/p2p/base/portallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010036#include "webrtc/pc/channel.h"
37#include "webrtc/pc/channelmanager.h"
38#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080039#include "webrtc/pc/sctputils.h"
40#include "webrtc/pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
zhihuang9763d562016-08-05 11:14:50 -070042#ifdef HAVE_QUIC
43#include "webrtc/p2p/quic/quictransportchannel.h"
44#endif // HAVE_QUIC
45
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046using cricket::ContentInfo;
47using cricket::ContentInfos;
48using cricket::MediaContentDescription;
49using cricket::SessionDescription;
50using cricket::TransportInfo;
51
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070052using cricket::LOCAL_PORT_TYPE;
53using cricket::STUN_PORT_TYPE;
54using cricket::RELAY_PORT_TYPE;
55using cricket::PRFLX_PORT_TYPE;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace webrtc {
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
61 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
65const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
67const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080077const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000078 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080079const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000080 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070081const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070083IceCandidatePairType GetIceCandidatePairCounter(
84 const cricket::Candidate& local,
85 const cricket::Candidate& remote) {
86 const auto& l = local.type();
87 const auto& r = remote.type();
88 const auto& host = LOCAL_PORT_TYPE;
89 const auto& srflx = STUN_PORT_TYPE;
90 const auto& relay = RELAY_PORT_TYPE;
91 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070092 if (l == host && r == host) {
93 bool local_private = IPIsPrivate(local.address().ipaddr());
94 bool remote_private = IPIsPrivate(remote.address().ipaddr());
95 if (local_private) {
96 if (remote_private) {
97 return kIceCandidatePairHostPrivateHostPrivate;
98 } else {
99 return kIceCandidatePairHostPrivateHostPublic;
100 }
101 } else {
102 if (remote_private) {
103 return kIceCandidatePairHostPublicHostPrivate;
104 } else {
105 return kIceCandidatePairHostPublicHostPublic;
106 }
107 }
108 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700109 if (l == host && r == srflx)
110 return kIceCandidatePairHostSrflx;
111 if (l == host && r == relay)
112 return kIceCandidatePairHostRelay;
113 if (l == host && r == prflx)
114 return kIceCandidatePairHostPrflx;
115 if (l == srflx && r == host)
116 return kIceCandidatePairSrflxHost;
117 if (l == srflx && r == srflx)
118 return kIceCandidatePairSrflxSrflx;
119 if (l == srflx && r == relay)
120 return kIceCandidatePairSrflxRelay;
121 if (l == srflx && r == prflx)
122 return kIceCandidatePairSrflxPrflx;
123 if (l == relay && r == host)
124 return kIceCandidatePairRelayHost;
125 if (l == relay && r == srflx)
126 return kIceCandidatePairRelaySrflx;
127 if (l == relay && r == relay)
128 return kIceCandidatePairRelayRelay;
129 if (l == relay && r == prflx)
130 return kIceCandidatePairRelayPrflx;
131 if (l == prflx && r == host)
132 return kIceCandidatePairPrflxHost;
133 if (l == prflx && r == srflx)
134 return kIceCandidatePairPrflxSrflx;
135 if (l == prflx && r == relay)
136 return kIceCandidatePairPrflxRelay;
137 return kIceCandidatePairMax;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140// Compares |answer| against |offer|. Comparision is done
141// for number of m-lines in answer against offer. If matches true will be
142// returned otherwise false.
143static bool VerifyMediaDescriptions(
144 const SessionDescription* answer, const SessionDescription* offer) {
145 if (offer->contents().size() != answer->contents().size())
146 return false;
147
148 for (size_t i = 0; i < offer->contents().size(); ++i) {
149 if ((offer->contents()[i].name) != answer->contents()[i].name) {
150 return false;
151 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000152 const MediaContentDescription* offer_mdesc =
153 static_cast<const MediaContentDescription*>(
154 offer->contents()[i].description);
155 const MediaContentDescription* answer_mdesc =
156 static_cast<const MediaContentDescription*>(
157 answer->contents()[i].description);
158 if (offer_mdesc->type() != answer_mdesc->type()) {
159 return false;
160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162 return true;
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800166// fingerprint, unless it's in a BUNDLE group, in which case only the
167// BUNDLE-tag section (first media section/description in the BUNDLE group)
168// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
169// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
170// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000171static bool VerifyCrypto(const SessionDescription* desc,
172 bool dtls_enabled,
173 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800174 const cricket::ContentGroup* bundle =
175 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 const ContentInfos& contents = desc->contents();
177 for (size_t index = 0; index < contents.size(); ++index) {
178 const ContentInfo* cinfo = &contents[index];
179 if (cinfo->rejected) {
180 continue;
181 }
deadbeefb7892532017-02-22 19:35:18 -0800182 if (bundle && bundle->HasContentName(cinfo->name) &&
183 cinfo->name != *(bundle->FirstContentName())) {
184 // This isn't the first media section in the BUNDLE group, so it's not
185 // required to have crypto attributes, since only the crypto attributes
186 // from the first section actually get used.
187 continue;
188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
deadbeefb7892532017-02-22 19:35:18 -0800190 // If the content isn't rejected or bundled into another m= section, crypto
191 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 const MediaContentDescription* media =
193 static_cast<const MediaContentDescription*>(cinfo->description);
194 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
195 if (!media || !tinfo) {
196 // Something is not right.
197 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return false;
200 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000203 LOG(LS_WARNING) <<
204 "Session description must have DTLS fingerprint if DTLS enabled.";
205 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000206 return false;
207 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000208 } else {
209 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 LOG(LS_WARNING) <<
211 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000212 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 return false;
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 }
216 }
217
218 return true;
219}
220
deadbeefb7892532017-02-22 19:35:18 -0800221// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
222// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
223// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000224static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800225 const cricket::ContentGroup* bundle =
226 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000227 const ContentInfos& contents = desc->contents();
228 for (size_t index = 0; index < contents.size(); ++index) {
229 const ContentInfo* cinfo = &contents[index];
230 if (cinfo->rejected) {
231 continue;
232 }
deadbeefb7892532017-02-22 19:35:18 -0800233 if (bundle && bundle->HasContentName(cinfo->name) &&
234 cinfo->name != *(bundle->FirstContentName())) {
235 // This isn't the first media section in the BUNDLE group, so it's not
236 // required to have ufrag/password, since only the ufrag/password from
237 // the first section actually get used.
238 continue;
239 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000240
deadbeefb7892532017-02-22 19:35:18 -0800241 // If the content isn't rejected or bundled into another m= section,
242 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000243 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
244 if (!tinfo) {
245 // Something is not right.
246 LOG(LS_ERROR) << kInvalidSdp;
247 return false;
248 }
249 if (tinfo->description.ice_ufrag.empty() ||
250 tinfo->description.ice_pwd.empty()) {
251 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
252 return false;
253 }
254 }
255 return true;
256}
257
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 uint32_t ssrc,
260 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800261 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 const cricket::ContentInfo* audio_info =
264 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000265 if (audio_info) {
266 const cricket::MediaContentDescription* audio_content =
267 static_cast<const cricket::MediaContentDescription*>(
268 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000270 const auto* found =
271 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
272 if (found) {
273 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000274 return true;
275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 const cricket::ContentInfo* video_info =
279 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000280 if (video_info) {
281 const cricket::MediaContentDescription* video_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000285 const auto* found =
286 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
287 if (found) {
288 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000289 return true;
290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292 return false;
293}
294
deadbeef953c2ce2017-01-09 14:53:41 -0800295// Get the SCTP port out of a SessionDescription.
296// Return -1 if not found.
297static int GetSctpPort(const SessionDescription* session_description) {
298 const ContentInfo* content_info = GetFirstDataContent(session_description);
299 RTC_DCHECK(content_info);
300 if (!content_info) {
301 return -1;
302 }
303 const cricket::DataContentDescription* data =
304 static_cast<const cricket::DataContentDescription*>(
305 (content_info->description));
306 std::string value;
307 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
308 cricket::kGoogleSctpDataCodecName);
309 for (const cricket::DataCodec& codec : data->codecs()) {
310 if (!codec.Matches(match_pattern)) {
311 continue;
312 }
313 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
314 return rtc::FromString<int>(value);
315 }
316 }
317 return -1;
318}
319
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320static bool BadSdp(const std::string& source,
321 const std::string& type,
322 const std::string& reason,
323 std::string* err_desc) {
324 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700325 desc << "Failed to set " << source;
326 if (!type.empty()) {
327 desc << " " << type;
328 }
329 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000334 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 return false;
336}
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 const std::string& type,
340 const std::string& reason,
341 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 }
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349static bool BadLocalSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
353}
354
355static bool BadRemoteSdp(const std::string& type,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
359}
360
361static bool BadOfferSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
365}
366
367static bool BadPranswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
371 reason, err_desc);
372}
373
374static bool BadAnswerSdp(cricket::ContentSource source,
375 const std::string& reason,
376 std::string* err_desc) {
377 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378}
379
deadbeefd59daf82015-10-14 15:02:44 -0700380#define GET_STRING_OF_STATE(state) \
381 case webrtc::WebRtcSession::state: \
382 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 break;
384
deadbeefd59daf82015-10-14 15:02:44 -0700385static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 std::string result;
387 switch (state) {
388 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700389 GET_STRING_OF_STATE(STATE_SENTOFFER)
390 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
391 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
392 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700394 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 default:
nissec80e7412017-01-11 05:56:46 -0800396 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 break;
398 }
399 return result;
400}
401
deadbeefd59daf82015-10-14 15:02:44 -0700402#define GET_STRING_OF_ERROR_CODE(err) \
403 case webrtc::WebRtcSession::err: \
404 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 break;
406
deadbeefd59daf82015-10-14 15:02:44 -0700407static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 std::string result;
409 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000411 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
412 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800414 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 break;
416 }
417 return result;
418}
419
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420static std::string MakeErrorString(const std::string& error,
421 const std::string& desc) {
422 std::ostringstream ret;
423 ret << error << " " << desc;
424 return ret.str();
425}
426
427static std::string MakeTdErrorString(const std::string& desc) {
428 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429}
430
deadbeef0ed85b22016-02-23 17:24:52 -0800431// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
432bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
433 const SessionDescriptionInterface* new_desc,
434 const std::string& content_name) {
435 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700436 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
deadbeef0ed85b22016-02-23 17:24:52 -0800438 const SessionDescription* new_sd = new_desc->description();
439 const SessionDescription* old_sd = old_desc->description();
440 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
441 if (!cinfo || cinfo->rejected) {
442 return false;
443 }
444 // If the content isn't rejected, check if ufrag and password has changed.
445 const cricket::TransportDescription* new_transport_desc =
446 new_sd->GetTransportDescriptionByName(content_name);
447 const cricket::TransportDescription* old_transport_desc =
448 old_sd->GetTransportDescriptionByName(content_name);
449 if (!new_transport_desc || !old_transport_desc) {
450 // No transport description exists. This is not an ICE restart.
451 return false;
452 }
453 if (cricket::IceCredentialsChanged(
454 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
455 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
456 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
457 << ".";
458 return true;
459 }
460 return false;
461}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
zhihuang29ff8442016-07-27 11:07:25 -0700463WebRtcSession::WebRtcSession(
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
deadbeefd59daf82015-10-14 15:02:44 -0700527
528 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}
530
wu@webrtc.org91053e72013-08-10 07:18:04 +0000531bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000532 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200533 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200534 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
535 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700536 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700537 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000538
Henrik Boström87713d02015-08-25 09:53:21 +0200539 // Obtain a certificate from RTCConfiguration if any were provided (optional).
540 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
541 if (!rtc_configuration.certificates.empty()) {
542 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
543 // just picking the first one. The decision should be made based on the DTLS
544 // handshake. The DTLS negotiations need to know about all certificates.
545 certificate = rtc_configuration.certificates[0];
546 }
547
honghaiz1f429e32015-09-28 07:57:34 -0700548 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700549
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000550 if (options.disable_encryption) {
551 dtls_enabled_ = false;
552 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200553 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200554 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800555 // |rtc_configuration| can override the default |dtls_enabled_| value.
556 if (rtc_configuration.enable_dtls_srtp) {
557 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000558 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000559 }
560
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000562 // It takes precendence over the disable_sctp_data_channels
563 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800564 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700566 }
567#ifdef HAVE_QUIC
568 else if (rtc_configuration.enable_quic) {
569 // Use QUIC instead of DTLS when |enable_quic| is true.
570 data_channel_type_ = cricket::DCT_QUIC;
571 transport_controller_->use_quic();
572 if (dtls_enabled_) {
573 LOG(LS_INFO) << "Using QUIC instead of DTLS";
574 }
575 quic_data_transport_.reset(
576 new QuicDataTransport(signaling_thread(), worker_thread(),
577 network_thread(), transport_controller_.get()));
578 }
579#endif // HAVE_QUIC
580 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000581 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000582 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000583 data_channel_type_ = cricket::DCT_SCTP;
584 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586
htaa2a49d92016-03-04 02:51:39 -0800587 video_options_.screencast_min_bitrate_kbps =
588 rtc_configuration.screencast_min_bitrate;
589 audio_options_.combined_audio_video_bwe =
590 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000591
kwiberg102c6a62015-10-30 02:47:38 -0700592 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100593 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200594
Karl Wibergbe579832015-11-10 22:34:18 +0100595 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
596 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200597
Henrik Boström87713d02015-08-25 09:53:21 +0200598 if (!dtls_enabled_) {
599 // Construct with DTLS disabled.
600 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200601 signaling_thread(), channel_manager_, this, id(),
602 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200603 } else {
604 // Construct with DTLS enabled.
605 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200606 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200607 signaling_thread(), channel_manager_, this, id(),
608 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200609 } else {
610 // Use the already generated certificate.
611 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200612 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200613 }
614 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000615
Henrik Boströmd8281982015-08-27 10:12:24 +0200616 webrtc_session_desc_factory_->SignalCertificateReady.connect(
617 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000618
wu@webrtc.org97077a32013-10-25 21:18:33 +0000619 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000620 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000621 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700622
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 return true;
624}
625
deadbeefd59daf82015-10-14 15:02:44 -0700626void WebRtcSession::Close() {
627 SetState(STATE_CLOSED);
628 RemoveUnusedChannels(nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800629 RTC_DCHECK(!voice_channel_);
630 RTC_DCHECK(!video_channel_);
631 RTC_DCHECK(!rtp_data_channel_);
632 RTC_DCHECK(!sctp_transport_);
solenberg03d6d572016-03-01 12:42:03 -0800633 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634}
635
deadbeef0ed85b22016-02-23 17:24:52 -0800636cricket::BaseChannel* WebRtcSession::GetChannel(
637 const std::string& content_name) {
638 if (voice_channel() && voice_channel()->content_name() == content_name) {
639 return voice_channel();
640 }
641 if (video_channel() && video_channel()->content_name() == content_name) {
642 return video_channel();
643 }
deadbeef953c2ce2017-01-09 14:53:41 -0800644 if (rtp_data_channel() &&
645 rtp_data_channel()->content_name() == content_name) {
646 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800647 }
648 return nullptr;
649}
650
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000651cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
652 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653}
654
deadbeef953c2ce2017-01-09 14:53:41 -0800655bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
656 if (!local_description() || !remote_description()) {
657 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
658 << "SSL Role of the SCTP transport.";
659 return false;
660 }
661 if (!sctp_transport_) {
662 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
663 << "SSL Role of the SCTP transport.";
664 return false;
665 }
666
667 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
668}
669
670bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800671 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800672 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800673 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000674 << "SSL Role of the session.";
675 return false;
676 }
677
deadbeef953c2ce2017-01-09 14:53:41 -0800678 return transport_controller_->GetSslRole(GetTransportName(content_name),
679 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000680}
681
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000682void WebRtcSession::CreateOffer(
683 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700684 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
685 const cricket::MediaSessionOptions& session_options) {
686 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000687}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688
deadbeefab9b2d12015-10-14 11:33:11 -0700689void WebRtcSession::CreateAnswer(
690 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700691 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800692 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693}
694
695bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
696 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800697 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700698
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000699 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700700 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000701
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000702 // Validate SDP.
703 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
704 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 }
706
deadbeefd59daf82015-10-14 15:02:44 -0700707 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000708 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700710 initial_offerer_ = true;
711 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
deadbeeffe4a8a42016-12-20 17:56:17 -0800714 if (action == kAnswer) {
715 current_local_description_.reset(desc_temp.release());
716 pending_local_description_.reset(nullptr);
717 current_remote_description_.reset(pending_remote_description_.release());
718 } else {
719 pending_local_description_.reset(desc_temp.release());
720 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721
722 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800723 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 // TODO(mallinath) - Handle CreateChannel failure, as new local description
725 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000726 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 }
728
deadbeefcbecd352015-09-23 11:50:27 -0700729 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800730 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000732 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 return false;
734 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800735 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700736 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800737 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700738 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
deadbeef0ed85b22016-02-23 17:24:52 -0800740 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700741 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000742 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744 return true;
745}
746
747bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
748 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800749 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700750
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000751 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700752 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000753
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000754 // Validate SDP.
755 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
756 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758
deadbeeffe4a8a42016-12-20 17:56:17 -0800759 const SessionDescriptionInterface* old_remote_description =
760 remote_description();
761 // Grab ownership of the description being replaced for the remainder of this
762 // method, since it's used below.
763 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
764 Action action = GetAction(desc->type());
765 if (action == kAnswer) {
766 replaced_remote_description.reset(
767 pending_remote_description_ ? pending_remote_description_.release()
768 : current_remote_description_.release());
769 current_remote_description_.reset(desc_temp.release());
770 pending_remote_description_.reset(nullptr);
771 current_local_description_.reset(pending_local_description_.release());
772 } else {
773 replaced_remote_description.reset(pending_remote_description_.release());
774 pending_remote_description_.reset(desc_temp.release());
775 }
deadbeefd59daf82015-10-14 15:02:44 -0700776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 // Transport and Media channels will be created only when offer is set.
778 if (action == kOffer && !CreateChannels(desc->description())) {
779 // TODO(mallinath) - Handle CreateChannel failure, as new local description
780 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000781 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 }
783
deadbeefcbecd352015-09-23 11:50:27 -0700784 // Remove unused channels if MediaContentDescription is rejected.
785 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786
787 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
788 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000789 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 return false;
791 }
792
deadbeeffe4a8a42016-12-20 17:56:17 -0800793 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000794 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 }
796
deadbeeffe4a8a42016-12-20 17:56:17 -0800797 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800798 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800799 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800800 // Check if this new SessionDescription contains new ICE ufrag and
801 // password that indicates the remote peer requests an ICE restart.
802 // TODO(deadbeef): When we start storing both the current and pending
803 // remote description, this should reset pending_ice_restarts and compare
804 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800805 if (CheckForRemoteIceRestart(old_remote_description, desc,
806 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800807 if (action == kOffer) {
808 pending_ice_restarts_.insert(content.name);
809 }
810 } else {
811 // We retain all received candidates only if ICE is not restarted.
812 // When ICE is restarted, all previous candidates belong to an old
813 // generation and should not be kept.
814 // TODO(deadbeef): This goes against the W3C spec which says the remote
815 // description should only contain candidates from the last set remote
816 // description plus any candidates added since then. We should remove
817 // this once we're sure it won't break anything.
818 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800819 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800820 }
821 }
honghaiz503726c2015-07-31 12:37:38 -0700822 }
823
deadbeefd59daf82015-10-14 15:02:44 -0700824 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000825 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000827
828 // Set the the ICE connection state to connecting since the connection may
829 // become writable with peer reflexive candidates before any remote candidate
830 // is signaled.
831 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
832 // is to have a new signal the indicates a change in checking state from the
833 // transport and expose a new checking() member from transport that can be
834 // read to determine the current checking state. The existing SignalConnecting
835 // actually means "gathering candidates", so cannot be be used here.
836 if (desc->type() != SessionDescriptionInterface::kOffer &&
837 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
838 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 return true;
841}
842
deadbeefd59daf82015-10-14 15:02:44 -0700843void WebRtcSession::LogState(State old_state, State new_state) {
844 LOG(LS_INFO) << "Session:" << id()
845 << " Old state:" << GetStateString(old_state)
846 << " New state:" << GetStateString(new_state);
847}
848
849void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800850 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700851 if (state != state_) {
852 LogState(state_, state);
853 state_ = state;
854 SignalState(this, state_);
855 }
856}
857
858void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800859 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700860 if (error != error_) {
861 error_ = error;
862 error_desc_ = error_desc;
863 }
864}
865
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866bool WebRtcSession::UpdateSessionState(
867 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800869 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700870
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 // If there's already a pending error then no state transition should happen.
872 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800873 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000876 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
877 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 }
deadbeefd59daf82015-10-14 15:02:44 -0700879 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
880 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000881 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700882 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000883 }
deadbeefd59daf82015-10-14 15:02:44 -0700884 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000885 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
887 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
889 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 }
891 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700892 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
893 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000894 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700895 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000896 }
deadbeefd59daf82015-10-14 15:02:44 -0700897 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000898 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700901 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800902 local_description()->description()->GetGroupByName(
903 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700904 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800905 remote_description()->description()->GetGroupByName(
906 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700907 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800908 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700909 const cricket::ContentGroup* answer_bundle =
910 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
911 if (!EnableBundle(*answer_bundle)) {
912 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
913 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
914 }
915 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800916 // Only push down the transport description after enabling BUNDLE; we don't
917 // want to push down a description on a transport about to be destroyed.
918 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
919 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700922 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000923 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700924 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000925 }
deadbeefd59daf82015-10-14 15:02:44 -0700926 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000927 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929 }
930 return true;
931}
932
933WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
934 if (type == SessionDescriptionInterface::kOffer) {
935 return WebRtcSession::kOffer;
936 } else if (type == SessionDescriptionInterface::kPrAnswer) {
937 return WebRtcSession::kPrAnswer;
938 } else if (type == SessionDescriptionInterface::kAnswer) {
939 return WebRtcSession::kAnswer;
940 }
nisseede5da42017-01-12 05:15:36 -0800941 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 return WebRtcSession::kOffer;
943}
944
deadbeefd59daf82015-10-14 15:02:44 -0700945bool WebRtcSession::PushdownMediaDescription(
946 cricket::ContentAction action,
947 cricket::ContentSource source,
948 std::string* err) {
949 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
950 if (!ch) {
951 return true;
952 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800953 return ch->PushdownLocalDescription(local_description()->description(),
954 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700955 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800956 return ch->PushdownRemoteDescription(remote_description()->description(),
957 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700958 }
959 };
960
deadbeef953c2ce2017-01-09 14:53:41 -0800961 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
962 set_content(rtp_data_channel()));
963 // Need complete offer/answer with an SCTP m= section before starting SCTP,
964 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
965 if (sctp_transport_ && local_description() && remote_description() &&
966 cricket::GetFirstDataContent(local_description()->description()) &&
967 cricket::GetFirstDataContent(remote_description()->description())) {
968 ret &= network_thread_->Invoke<bool>(
969 RTC_FROM_HERE,
970 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
971 }
972 return ret;
973}
974
975bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
976 RTC_DCHECK(network_thread_->IsCurrent());
977 RTC_DCHECK(local_description());
978 RTC_DCHECK(remote_description());
979 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
980 // When we support "max-message-size", that would also be pushed down here.
981 return sctp_transport_->Start(
982 GetSctpPort(local_description()->description()),
983 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700984}
985
986bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
987 cricket::ContentAction action,
988 std::string* error_desc) {
989 RTC_DCHECK(signaling_thread()->IsCurrent());
990
991 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800992 return PushdownLocalTransportDescription(local_description()->description(),
993 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700994 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800995 return PushdownRemoteTransportDescription(remote_description()->description(),
996 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700997}
998
999bool WebRtcSession::PushdownLocalTransportDescription(
1000 const SessionDescription* sdesc,
1001 cricket::ContentAction action,
1002 std::string* err) {
1003 RTC_DCHECK(signaling_thread()->IsCurrent());
1004
1005 if (!sdesc) {
1006 return false;
1007 }
1008
1009 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1010 if (!transport_controller_->SetLocalTransportDescription(
1011 tinfo.content_name, tinfo.description, action, err)) {
1012 return false;
1013 }
1014 }
1015
1016 return true;
1017}
1018
1019bool WebRtcSession::PushdownRemoteTransportDescription(
1020 const SessionDescription* sdesc,
1021 cricket::ContentAction action,
1022 std::string* err) {
1023 RTC_DCHECK(signaling_thread()->IsCurrent());
1024
1025 if (!sdesc) {
1026 return false;
1027 }
1028
1029 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1030 if (!transport_controller_->SetRemoteTransportDescription(
1031 tinfo.content_name, tinfo.description, action, err)) {
1032 return false;
1033 }
1034 }
1035
1036 return true;
1037}
1038
1039bool WebRtcSession::GetTransportDescription(
1040 const SessionDescription* description,
1041 const std::string& content_name,
1042 cricket::TransportDescription* tdesc) {
1043 if (!description || !tdesc) {
1044 return false;
1045 }
1046 const TransportInfo* transport_info =
1047 description->GetTransportInfoByName(content_name);
1048 if (!transport_info) {
1049 return false;
1050 }
1051 *tdesc = transport_info->description;
1052 return true;
1053}
1054
deadbeefcbecd352015-09-23 11:50:27 -07001055bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1056 const std::string* first_content_name = bundle.FirstContentName();
1057 if (!first_content_name) {
1058 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1059 return false;
1060 }
1061 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001062
zhihuang9763d562016-08-05 11:14:50 -07001063#ifdef HAVE_QUIC
1064 if (quic_data_transport_ &&
1065 bundle.HasContentName(quic_data_transport_->content_name()) &&
1066 quic_data_transport_->transport_name() != transport_name) {
1067 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1068 << " on " << transport_name << "with QUIC.";
1069 }
1070#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001071 auto maybe_set_transport = [this, bundle,
1072 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001073 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001074 return true;
1075 }
1076
zhihuangf5b251b2017-01-12 19:37:48 -08001077 std::string old_transport_name = ch->transport_name();
1078 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001079 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1080 << " on " << transport_name << ".";
1081 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001082 }
torbjornga81a42f2015-09-23 02:16:58 -07001083
zhihuangb2cdd932017-01-19 16:54:25 -08001084 cricket::DtlsTransportInternal* rtp_dtls_transport =
1085 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001086 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001087 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1088 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001089 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001090 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08001091 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1092 }
1093
zhihuangb2cdd932017-01-19 16:54:25 -08001094 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001095 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1096 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001097 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001098 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001099 // If the channel needs rtcp, it means that the channel used to have a
1100 // rtcp transport which needs to be deleted now.
1101 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001102 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001103 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001104 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001105 return true;
1106 };
1107
deadbeefcbecd352015-09-23 11:50:27 -07001108 if (!maybe_set_transport(voice_channel()) ||
1109 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001110 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001111 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001112 }
deadbeef953c2ce2017-01-09 14:53:41 -08001113 // For SCTP, transport creation/deletion happens here instead of in the
1114 // object itself.
1115 if (sctp_transport_) {
1116 RTC_DCHECK(sctp_transport_name_);
1117 RTC_DCHECK(sctp_content_name_);
1118 if (transport_name != *sctp_transport_name_ &&
1119 bundle.HasContentName(*sctp_content_name_)) {
1120 network_thread_->Invoke<void>(
1121 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1122 transport_name));
1123 }
1124 }
deadbeefcbecd352015-09-23 11:50:27 -07001125
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001126 return true;
1127}
1128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001130 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001131 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1132 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001133 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 }
1135
1136 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001137 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 return false;
1139 }
1140
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001141 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001142 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1143 if (!valid) {
1144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 }
1146
1147 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001148 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001149 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 return false;
1151 }
1152
deadbeefd59daf82015-10-14 15:02:44 -07001153 if (ready) {
1154 return UseCandidate(candidate);
1155 } else {
1156 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1157 return true;
1158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159}
1160
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001161bool WebRtcSession::RemoveRemoteIceCandidates(
1162 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001163 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001164 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1165 << "removed without any remote session description.";
1166 return false;
1167 }
1168
1169 if (candidates.empty()) {
1170 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1171 return false;
1172 }
1173
deadbeeffe4a8a42016-12-20 17:56:17 -08001174 size_t number_removed =
1175 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001176 if (number_removed != candidates.size()) {
1177 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1178 << "Requested " << candidates.size() << " but only "
1179 << number_removed << " are removed.";
1180 }
1181
1182 // Remove the candidates from the transport controller.
1183 std::string error;
1184 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1185 if (!res && !error.empty()) {
1186 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1187 }
1188 return true;
1189}
1190
deadbeefd59daf82015-10-14 15:02:44 -07001191cricket::IceConfig WebRtcSession::ParseIceConfig(
1192 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001193 cricket::ContinualGatheringPolicy gathering_policy;
1194 // TODO(honghaiz): Add the third continual gathering policy in
1195 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1196 switch (config.continual_gathering_policy) {
1197 case PeerConnectionInterface::GATHER_ONCE:
1198 gathering_policy = cricket::GATHER_ONCE;
1199 break;
1200 case PeerConnectionInterface::GATHER_CONTINUALLY:
1201 gathering_policy = cricket::GATHER_CONTINUALLY;
1202 break;
1203 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001204 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001205 gathering_policy = cricket::GATHER_ONCE;
1206 }
deadbeefd59daf82015-10-14 15:02:44 -07001207 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001208 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001209 ice_config.prioritize_most_likely_candidate_pairs =
1210 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001211 ice_config.backup_connection_ping_interval =
1212 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001213 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001214 ice_config.presume_writable_when_fully_relayed =
1215 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001216 ice_config.ice_check_min_interval = config.ice_check_min_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001217 return ice_config;
1218}
1219
1220void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1221 transport_controller_->SetIceConfig(config);
1222}
1223
1224void WebRtcSession::MaybeStartGathering() {
1225 transport_controller_->MaybeStartGathering();
1226}
1227
Peter Boström0c4e06b2015-10-07 12:23:21 +02001228bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1229 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001230 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001232 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001233 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1234 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235}
1236
Peter Boström0c4e06b2015-10-07 12:23:21 +02001237bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1238 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001239 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001240 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001241 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001242 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1243 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244}
1245
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001246std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001248 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 return desc.str();
1250}
1251
wu@webrtc.org78187522013-10-07 23:32:02 +00001252bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001253 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001254 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001255 if (!rtp_data_channel_ && !sctp_transport_) {
1256 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1257 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001258 return false;
1259 }
deadbeef953c2ce2017-01-09 14:53:41 -08001260 return rtp_data_channel_
1261 ? rtp_data_channel_->SendData(params, payload, result)
1262 : network_thread_->Invoke<bool>(
1263 RTC_FROM_HERE,
1264 Bind(&cricket::SctpTransportInternal::SendData,
1265 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001266}
1267
1268bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001269 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001270 // Don't log an error here, because DataChannels are expected to call
1271 // ConnectDataChannel in this state. It's the only way to initially tell
1272 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001273 return false;
1274 }
deadbeef953c2ce2017-01-09 14:53:41 -08001275 if (rtp_data_channel_) {
1276 rtp_data_channel_->SignalReadyToSendData.connect(
1277 webrtc_data_channel, &DataChannel::OnChannelReady);
1278 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1279 &DataChannel::OnDataReceived);
1280 } else {
1281 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1282 &DataChannel::OnChannelReady);
1283 SignalSctpDataReceived.connect(webrtc_data_channel,
1284 &DataChannel::OnDataReceived);
1285 SignalSctpStreamClosedRemotely.connect(
1286 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1287 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001288 return true;
1289}
1290
1291void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001292 if (!rtp_data_channel_ && !sctp_transport_) {
1293 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1294 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001295 return;
1296 }
deadbeef953c2ce2017-01-09 14:53:41 -08001297 if (rtp_data_channel_) {
1298 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1299 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1300 } else {
1301 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1302 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1303 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1304 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001305}
1306
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001307void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001308 if (!sctp_transport_) {
1309 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001310 return;
1311 }
deadbeef953c2ce2017-01-09 14:53:41 -08001312 network_thread_->Invoke<void>(
1313 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1314 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001315}
1316
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001317void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001318 if (!sctp_transport_) {
1319 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001320 << "NULL.";
1321 return;
1322 }
deadbeef953c2ce2017-01-09 14:53:41 -08001323 network_thread_->Invoke<void>(
1324 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1325 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001326}
1327
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001328bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001329 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1330 sctp_ready_to_send_data_;
1331}
1332
1333std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001334 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001335 ChannelNamePairs channel_name_pairs;
1336 if (voice_channel()) {
1337 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1338 voice_channel()->content_name(), voice_channel()->transport_name()));
1339 }
1340 if (video_channel()) {
1341 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1342 video_channel()->content_name(), video_channel()->transport_name()));
1343 }
1344 if (rtp_data_channel()) {
1345 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1346 ChannelNamePair(rtp_data_channel()->content_name(),
1347 rtp_data_channel()->transport_name()));
1348 }
1349 if (sctp_transport_) {
1350 RTC_DCHECK(sctp_content_name_);
1351 RTC_DCHECK(sctp_transport_name_);
1352 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1353 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1354 }
1355 return GetStats(channel_name_pairs);
1356}
1357
1358std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1359 const ChannelNamePairs& channel_name_pairs) {
1360 if (network_thread()->IsCurrent()) {
1361 return GetStats_n(channel_name_pairs);
1362 }
1363 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1364 RTC_FROM_HERE,
1365 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1366}
1367
1368bool WebRtcSession::GetLocalCertificate(
1369 const std::string& transport_name,
1370 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1371 return transport_controller_->GetLocalCertificate(transport_name,
1372 certificate);
1373}
1374
1375std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1376 const std::string& transport_name) {
1377 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001378}
1379
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380cricket::DataChannelType WebRtcSession::data_channel_type() const {
1381 return data_channel_type_;
1382}
1383
deadbeef0ed85b22016-02-23 17:24:52 -08001384bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1385 return pending_ice_restarts_.find(content_name) !=
1386 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001387}
1388
deadbeefd1a38b52016-12-10 13:15:33 -08001389void WebRtcSession::SetNeedsIceRestartFlag() {
1390 transport_controller_->SetNeedsIceRestartFlag();
1391}
1392
1393bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1394 return transport_controller_->NeedsIceRestart(content_name);
1395}
1396
Henrik Boströmd8281982015-08-27 10:12:24 +02001397void WebRtcSession::OnCertificateReady(
1398 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001399 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001400}
1401
deadbeef953c2ce2017-01-09 14:53:41 -08001402void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1403 SetError(ERROR_TRANSPORT,
1404 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1405}
1406
Henrik Boströmd8281982015-08-27 10:12:24 +02001407bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001408 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001409}
1410
deadbeefcbecd352015-09-23 11:50:27 -07001411const rtc::scoped_refptr<rtc::RTCCertificate>&
1412WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001413 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001414}
1415
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416void WebRtcSession::SetIceConnectionState(
1417 PeerConnectionInterface::IceConnectionState state) {
1418 if (ice_connection_state_ == state) {
1419 return;
1420 }
1421
deadbeefcbecd352015-09-23 11:50:27 -07001422 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1423 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001424 RTC_DCHECK(ice_connection_state_ !=
1425 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 ice_connection_state_ = state;
1427 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001428 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 }
1430}
1431
deadbeefcbecd352015-09-23 11:50:27 -07001432void WebRtcSession::OnTransportControllerConnectionState(
1433 cricket::IceConnectionState state) {
1434 switch (state) {
1435 case cricket::kIceConnectionConnecting:
1436 // If the current state is Connected or Completed, then there were
1437 // writable channels but now there are not, so the next state must
1438 // be Disconnected.
1439 // kIceConnectionConnecting is currently used as the default,
1440 // un-connected state by the TransportController, so its only use is
1441 // detecting disconnections.
1442 if (ice_connection_state_ ==
1443 PeerConnectionInterface::kIceConnectionConnected ||
1444 ice_connection_state_ ==
1445 PeerConnectionInterface::kIceConnectionCompleted) {
1446 SetIceConnectionState(
1447 PeerConnectionInterface::kIceConnectionDisconnected);
1448 }
torbjornga81a42f2015-09-23 02:16:58 -07001449 break;
deadbeefcbecd352015-09-23 11:50:27 -07001450 case cricket::kIceConnectionFailed:
1451 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1452 break;
1453 case cricket::kIceConnectionConnected:
1454 LOG(LS_INFO) << "Changing to ICE connected state because "
1455 << "all transports are writable.";
1456 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1457 break;
1458 case cricket::kIceConnectionCompleted:
1459 LOG(LS_INFO) << "Changing to ICE completed state because "
1460 << "all transports are complete.";
1461 if (ice_connection_state_ !=
1462 PeerConnectionInterface::kIceConnectionConnected) {
1463 // If jumping directly from "checking" to "connected",
1464 // signal "connected" first.
1465 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1466 }
1467 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1468 if (metrics_observer_) {
1469 ReportTransportStats();
1470 }
1471 break;
1472 default:
nissec80e7412017-01-11 05:56:46 -08001473 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001474 }
deadbeefcbecd352015-09-23 11:50:27 -07001475}
1476
1477void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001478 SetIceConnectionReceiving(receiving);
1479}
1480
1481void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1482 if (ice_connection_receiving_ == receiving) {
1483 return;
1484 }
1485 ice_connection_receiving_ = receiving;
1486 if (ice_observer_) {
1487 ice_observer_->OnIceConnectionReceivingChange(receiving);
1488 }
1489}
1490
deadbeefcbecd352015-09-23 11:50:27 -07001491void WebRtcSession::OnTransportControllerCandidatesGathered(
1492 const std::string& transport_name,
1493 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001494 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001495 int sdp_mline_index;
1496 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1497 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1498 << transport_name << " not found";
1499 return;
1500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501
deadbeefcbecd352015-09-23 11:50:27 -07001502 for (cricket::Candidates::const_iterator citer = candidates.begin();
1503 citer != candidates.end(); ++citer) {
1504 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001505 std::unique_ptr<JsepIceCandidate> candidate(
1506 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001507 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001508 mutable_local_description()->AddCandidate(candidate.get());
1509 }
1510 if (ice_observer_) {
1511 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 }
1514}
1515
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001516void WebRtcSession::OnTransportControllerCandidatesRemoved(
1517 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001518 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001519 // Sanity check.
1520 for (const cricket::Candidate& candidate : candidates) {
1521 if (candidate.transport_name().empty()) {
1522 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1523 << "empty content name in candidate "
1524 << candidate.ToString();
1525 return;
1526 }
1527 }
1528
deadbeeffe4a8a42016-12-20 17:56:17 -08001529 if (local_description()) {
1530 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001531 }
1532 if (ice_observer_) {
1533 ice_observer_->OnIceCandidatesRemoved(candidates);
1534 }
1535}
1536
deadbeef953c2ce2017-01-09 14:53:41 -08001537void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1538 rtc::SSLHandshakeError error) {
1539 if (metrics_observer_) {
1540 metrics_observer_->IncrementEnumCounter(
1541 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1542 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1543 }
1544}
1545
1546// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547void WebRtcSession::EnableChannels() {
1548 if (voice_channel_ && !voice_channel_->enabled())
1549 voice_channel_->Enable(true);
1550
1551 if (video_channel_ && !video_channel_->enabled())
1552 video_channel_->Enable(true);
1553
deadbeef953c2ce2017-01-09 14:53:41 -08001554 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1555 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556}
1557
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558// Returns the media index for a local ice candidate given the content name.
1559bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1560 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001561 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001563 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564
1565 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001566 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 for (size_t index = 0; index < contents.size(); ++index) {
1568 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001569 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 content_found = true;
1571 break;
1572 }
1573 }
1574 return content_found;
1575}
1576
1577bool WebRtcSession::UseCandidatesInSessionDescription(
1578 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001579 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001581 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1585 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001586 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001587 const IceCandidateInterface* candidate = candidates->at(n);
1588 bool valid = false;
1589 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1590 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001591 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1592 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001593 }
1594 continue;
1595 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001596 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001597 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 break;
deadbeefd59daf82015-10-14 15:02:44 -07001599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 }
1601 }
1602 return ret;
1603}
1604
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001605bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001607 size_t remote_content_size =
1608 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001610 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 return false;
1612 }
1613
1614 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001615 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 std::vector<cricket::Candidate> candidates;
1617 candidates.push_back(candidate->candidate());
1618 // Invoking BaseSession method to handle remote candidates.
1619 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001620 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1621 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 // Candidates successfully submitted for checking.
1623 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1624 ice_connection_state_ ==
1625 PeerConnectionInterface::kIceConnectionDisconnected) {
1626 // If state is New, then the session has just gotten its first remote ICE
1627 // candidates, so go to Checking.
1628 // If state is Disconnected, the session is re-using old candidates or
1629 // receiving additional ones, so go to Checking.
1630 // If state is Connected, stay Connected.
1631 // TODO(bemasc): If state is Connected, and the new candidates are for a
1632 // newly added transport, then the state actually _should_ move to
1633 // checking. Add a way to distinguish that case.
1634 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1635 }
1636 // TODO(bemasc): If state is Completed, go back to Connected.
1637 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001638 if (!error.empty()) {
1639 LOG(LS_WARNING) << error;
1640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 }
1642 return true;
1643}
1644
deadbeefcbecd352015-09-23 11:50:27 -07001645void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001646 // Destroy video_channel_ first since it may have a pointer to the
1647 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 const cricket::ContentInfo* video_info =
1649 cricket::GetFirstVideoContent(desc);
1650 if ((!video_info || video_info->rejected) && video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001651 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 }
1653
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001654 const cricket::ContentInfo* voice_info =
1655 cricket::GetFirstAudioContent(desc);
1656 if ((!voice_info || voice_info->rejected) && voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001657 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001658 }
1659
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 const cricket::ContentInfo* data_info =
1661 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001662 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001663 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001664 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001665 }
1666 if (sctp_transport_) {
1667 SignalDataChannelDestroyed();
1668 network_thread_->Invoke<void>(
1669 RTC_FROM_HERE,
1670 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001671 }
1672#ifdef HAVE_QUIC
1673 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1674 if (quic_data_transport_) {
1675 quic_data_transport_.reset();
1676 }
1677#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 }
1679}
1680
skvlad6c87a672016-05-17 17:49:52 -07001681// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1682// if the channel is not part of any bundle.
1683const std::string* WebRtcSession::GetBundleTransportName(
1684 const cricket::ContentInfo* content,
1685 const cricket::ContentGroup* bundle) {
1686 if (!bundle) {
1687 return nullptr;
1688 }
1689 const std::string* first_content_name = bundle->FirstContentName();
1690 if (!first_content_name) {
1691 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1692 return nullptr;
1693 }
1694 if (!bundle->HasContentName(content->name)) {
1695 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1696 return nullptr;
1697 }
1698 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1699 return first_content_name;
1700}
1701
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001703 const cricket::ContentGroup* bundle_group = nullptr;
1704 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1705 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1706 if (!bundle_group) {
1707 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1708 return false;
1709 }
1710 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 // Creating the media channels and transport proxies.
1712 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1713 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001714 if (!CreateVoiceChannel(voice,
1715 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 LOG(LS_ERROR) << "Failed to create voice channel.";
1717 return false;
1718 }
1719 }
1720
1721 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1722 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001723 if (!CreateVideoChannel(video,
1724 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 LOG(LS_ERROR) << "Failed to create video channel.";
1726 return false;
1727 }
1728 }
1729
1730 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001731 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1732 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001733 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 LOG(LS_ERROR) << "Failed to create data channel.";
1735 return false;
1736 }
1737 }
1738
1739 return true;
1740}
1741
skvlad6c87a672016-05-17 17:49:52 -07001742bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1743 const std::string* bundle_transport) {
1744 bool require_rtcp_mux =
1745 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001746
1747 std::string transport_name =
1748 bundle_transport ? *bundle_transport : content->name;
1749
zhihuangb2cdd932017-01-19 16:54:25 -08001750 cricket::DtlsTransportInternal* rtp_dtls_transport =
1751 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001752 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001753 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001754 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001755 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001756 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1757 }
1758
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001760 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001761 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1762 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001763 if (!voice_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001764 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001765 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001766 if (rtcp_dtls_transport) {
1767 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001768 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1769 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001770 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001771 }
zhihuangf5b251b2017-01-12 19:37:48 -08001772
deadbeefac22f702017-01-12 21:59:29 -08001773 voice_channel_->SignalRtcpMuxFullyActive.connect(
1774 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001775 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1776 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001777
1778 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001779 voice_channel_->SignalSentPacket.connect(this,
1780 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001781 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782}
1783
skvlad6c87a672016-05-17 17:49:52 -07001784bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1785 const std::string* bundle_transport) {
1786 bool require_rtcp_mux =
1787 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001788
1789 std::string transport_name =
1790 bundle_transport ? *bundle_transport : content->name;
1791
zhihuangb2cdd932017-01-19 16:54:25 -08001792 cricket::DtlsTransportInternal* rtp_dtls_transport =
1793 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001794 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001795 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001796 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001797 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001798 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1799 }
1800
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 video_channel_.reset(channel_manager_->CreateVideoChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001802 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001803 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1804 video_options_));
zhihuangf5b251b2017-01-12 19:37:48 -08001805
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001806 if (!video_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001807 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001808 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001809 if (rtcp_dtls_transport) {
1810 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001811 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1812 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001813 return false;
1814 }
zhihuangf5b251b2017-01-12 19:37:48 -08001815
deadbeefac22f702017-01-12 21:59:29 -08001816 video_channel_->SignalRtcpMuxFullyActive.connect(
1817 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001818 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1819 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001820
1821 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001822 video_channel_->SignalSentPacket.connect(this,
1823 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001824 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
skvlad6c87a672016-05-17 17:49:52 -07001827bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1828 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001829 const std::string transport_name =
1830 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001831#ifdef HAVE_QUIC
1832 if (data_channel_type_ == cricket::DCT_QUIC) {
1833 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001834 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001835 return true;
1836 }
1837#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001838 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001839 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001840 if (!sctp_factory_) {
1841 LOG(LS_ERROR)
1842 << "Trying to create SCTP transport, but didn't compile with "
1843 "SCTP support (HAVE_SCTP)";
1844 return false;
1845 }
1846 if (!network_thread_->Invoke<bool>(
1847 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1848 this, content->name, transport_name))) {
1849 return false;
1850 };
1851 } else {
1852 bool require_rtcp_mux =
1853 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001854
1855 std::string transport_name =
1856 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001857 cricket::DtlsTransportInternal* rtp_dtls_transport =
1858 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001859 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001860 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001861 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001862 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001863 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1864 }
1865
deadbeef953c2ce2017-01-09 14:53:41 -08001866 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
zhihuangb2cdd932017-01-19 16:54:25 -08001867 media_controller_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001868 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001869 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001870
deadbeef953c2ce2017-01-09 14:53:41 -08001871 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001872 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001873 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001874 if (rtcp_dtls_transport) {
1875 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001876 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1877 }
deadbeef953c2ce2017-01-09 14:53:41 -08001878 return false;
1879 }
zhihuangf5b251b2017-01-12 19:37:48 -08001880
deadbeefac22f702017-01-12 21:59:29 -08001881 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1882 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001883 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1884 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1885 rtp_data_channel_->SignalSentPacket.connect(this,
1886 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001887 }
1888
deadbeefab9b2d12015-10-14 11:33:11 -07001889 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001890
wu@webrtc.org91053e72013-08-10 07:18:04 +00001891 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892}
1893
hbosdf6075a2016-12-19 04:58:02 -08001894std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1895 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001896 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001897 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1898 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1899 &channel_name_pairs.video,
1900 &channel_name_pairs.data }) {
1901 if (*channel_name_pair) {
1902 cricket::TransportStats transport_stats;
1903 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1904 &transport_stats)) {
1905 return nullptr;
1906 }
1907 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1908 (*channel_name_pair)->transport_name;
1909 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1910 std::move(transport_stats);
1911 }
1912 }
1913 return session_stats;
1914}
1915
deadbeef953c2ce2017-01-09 14:53:41 -08001916bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1917 const std::string& transport_name) {
1918 RTC_DCHECK(network_thread_->IsCurrent());
1919 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001920 cricket::DtlsTransportInternal* tc =
1921 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001922 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1923 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1924 RTC_DCHECK(sctp_transport_);
1925 sctp_invoker_.reset(new rtc::AsyncInvoker());
1926 sctp_transport_->SignalReadyToSendData.connect(
1927 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1928 sctp_transport_->SignalDataReceived.connect(
1929 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1930 sctp_transport_->SignalStreamClosedRemotely.connect(
1931 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1932 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1933 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1934 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935}
1936
deadbeef953c2ce2017-01-09 14:53:41 -08001937void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1938 RTC_DCHECK(network_thread_->IsCurrent());
1939 RTC_DCHECK(sctp_transport_);
1940 RTC_DCHECK(sctp_transport_name_);
1941 std::string old_sctp_transport_name = *sctp_transport_name_;
1942 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08001943 cricket::DtlsTransportInternal* tc =
1944 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001945 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1946 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08001947 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001948 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1949}
1950
1951void WebRtcSession::DestroySctpTransport_n() {
1952 RTC_DCHECK(network_thread_->IsCurrent());
1953 sctp_transport_.reset(nullptr);
1954 sctp_content_name_.reset();
1955 sctp_transport_name_.reset();
1956 sctp_invoker_.reset(nullptr);
1957 sctp_ready_to_send_data_ = false;
1958}
1959
1960void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1961 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1962 RTC_DCHECK(network_thread_->IsCurrent());
1963 sctp_invoker_->AsyncInvoke<void>(
1964 RTC_FROM_HERE, signaling_thread_,
1965 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1966}
1967
1968void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1969 RTC_DCHECK(signaling_thread_->IsCurrent());
1970 sctp_ready_to_send_data_ = ready;
1971 SignalSctpReadyToSendData(ready);
1972}
1973
1974void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001975 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001976 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001977 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08001978 RTC_DCHECK(network_thread_->IsCurrent());
1979 sctp_invoker_->AsyncInvoke<void>(
1980 RTC_FROM_HERE, signaling_thread_,
1981 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
1982 payload));
1983}
1984
1985void WebRtcSession::OnSctpTransportDataReceived_s(
1986 const cricket::ReceiveDataParams& params,
1987 const rtc::CopyOnWriteBuffer& payload) {
1988 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07001989 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1990 // Received OPEN message; parse and signal that a new data channel should
1991 // be created.
1992 std::string label;
1993 InternalDataChannelInit config;
1994 config.id = params.ssrc;
1995 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1996 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1997 << params.ssrc;
1998 return;
1999 }
2000 config.open_handshake_role = InternalDataChannelInit::kAcker;
2001 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002002 } else {
2003 // Otherwise just forward the signal.
2004 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 }
deadbeef953c2ce2017-01-09 14:53:41 -08002006}
2007
2008void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2009 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2010 RTC_DCHECK(network_thread_->IsCurrent());
2011 sctp_invoker_->AsyncInvoke<void>(
2012 RTC_FROM_HERE, signaling_thread_,
2013 rtc::Bind(&sigslot::signal1<int>::operator(),
2014 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015}
2016
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002017// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002018bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002019 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2020 if (!bundle_enabled)
2021 return true;
2022
2023 const cricket::ContentGroup* bundle_group =
2024 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002025 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002026
2027 const cricket::ContentInfos& contents = desc->contents();
2028 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2029 citer != contents.end(); ++citer) {
2030 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002031 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002032 if (bundle_group->HasContentName(content->name) &&
2033 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2034 if (!HasRtcpMuxEnabled(content))
2035 return false;
2036 }
2037 }
2038 // RTCP-MUX is enabled in all the contents.
2039 return true;
2040}
2041
2042bool WebRtcSession::HasRtcpMuxEnabled(
2043 const cricket::ContentInfo* content) {
2044 const cricket::MediaContentDescription* description =
2045 static_cast<cricket::MediaContentDescription*>(content->description);
2046 return description->rtcp_mux();
2047}
2048
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002049bool WebRtcSession::ValidateSessionDescription(
2050 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002051 cricket::ContentSource source, std::string* err_desc) {
2052 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002053 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002054 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002055 }
2056
2057 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002058 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002059 }
2060
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002061 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002062 Action action = GetAction(sdesc->type());
2063 if (source == cricket::CS_LOCAL) {
2064 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002065 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002066 } else {
2067 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002068 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002069 }
2070
2071 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002072 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002073 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2074 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002075 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002076 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002077 }
2078
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002079 // Verify ice-ufrag and ice-pwd.
2080 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002081 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002082 }
2083
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002084 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002085 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002086 }
2087
skvlad6c87a672016-05-17 17:49:52 -07002088 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2089 // m-lines that do not rtcp-mux enabled.
2090
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002091 // Verify m-lines in Answer when compared against Offer.
2092 if (action == kAnswer) {
2093 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002094 (source == cricket::CS_LOCAL) ? remote_description()->description()
2095 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002097 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002098 }
2099 }
2100
2101 return true;
2102}
2103
2104bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2105 return ((action == kOffer && state() == STATE_INIT) ||
2106 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002107 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002108 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002109 (action == kOffer && state() == STATE_INPROGRESS) ||
2110 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002111 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2112 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2113 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2114 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002115}
2116
2117bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2118 return ((action == kOffer && state() == STATE_INIT) ||
2119 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002120 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002121 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002122 (action == kOffer && state() == STATE_INPROGRESS) ||
2123 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002124 (action == kAnswer && state() == STATE_SENTOFFER) ||
2125 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2126 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2127 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002128}
2129
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002130std::string WebRtcSession::GetSessionErrorMsg() {
2131 std::ostringstream desc;
2132 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2133 desc << kSessionErrorDesc << error_desc() << ".";
2134 return desc.str();
2135}
2136
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002137// We need to check the local/remote description for the Transport instead of
2138// the session, because a new Transport added during renegotiation may have
2139// them unset while the session has them set from the previous negotiation.
2140// Not doing so may trigger the auto generation of transport description and
2141// mess up DTLS identity information, ICE credential, etc.
2142bool WebRtcSession::ReadyToUseRemoteCandidate(
2143 const IceCandidateInterface* candidate,
2144 const SessionDescriptionInterface* remote_desc,
2145 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002146 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002147
2148 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002149 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002150
deadbeefd59daf82015-10-14 15:02:44 -07002151 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002152 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002153 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002154
2155 size_t mediacontent_index =
2156 static_cast<size_t>(candidate->sdp_mline_index());
2157 size_t remote_content_size =
2158 current_remote_desc->description()->contents().size();
2159 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002160 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2161 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002162
2163 *valid = false;
2164 return false;
2165 }
2166
2167 cricket::ContentInfo content =
2168 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002169
2170 const std::string transport_name = GetTransportName(content.name);
2171 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002172 return false;
2173 }
zhihuang9763d562016-08-05 11:14:50 -07002174 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002175}
2176
deadbeef7af91dd2016-12-13 11:29:11 -08002177bool WebRtcSession::SrtpRequired() const {
2178 return dtls_enabled_ ||
2179 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2180}
2181
deadbeefcbecd352015-09-23 11:50:27 -07002182void WebRtcSession::OnTransportControllerGatheringState(
2183 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002184 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002185 if (state == cricket::kIceGatheringGathering) {
2186 if (ice_observer_) {
2187 ice_observer_->OnIceGatheringChange(
2188 PeerConnectionInterface::kIceGatheringGathering);
2189 }
2190 } else if (state == cricket::kIceGatheringComplete) {
2191 if (ice_observer_) {
2192 ice_observer_->OnIceGatheringChange(
2193 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002194 }
2195 }
2196}
2197
2198void WebRtcSession::ReportTransportStats() {
2199 // Use a set so we don't report the same stats twice if two channels share
2200 // a transport.
2201 std::set<std::string> transport_names;
2202 if (voice_channel()) {
2203 transport_names.insert(voice_channel()->transport_name());
2204 }
2205 if (video_channel()) {
2206 transport_names.insert(video_channel()->transport_name());
2207 }
deadbeef953c2ce2017-01-09 14:53:41 -08002208 if (rtp_data_channel()) {
2209 transport_names.insert(rtp_data_channel()->transport_name());
2210 }
2211 if (sctp_transport_name_) {
2212 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002213 }
2214 for (const auto& name : transport_names) {
2215 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002216 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002217 ReportBestConnectionState(stats);
2218 ReportNegotiatedCiphers(stats);
2219 }
2220 }
2221}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002222// Walk through the ConnectionInfos to gather best connection usage
2223// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002224void WebRtcSession::ReportBestConnectionState(
2225 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002226 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002227 for (cricket::TransportChannelStatsList::const_iterator it =
2228 stats.channel_stats.begin();
2229 it != stats.channel_stats.end(); ++it) {
2230 for (cricket::ConnectionInfos::const_iterator it_info =
2231 it->connection_infos.begin();
2232 it_info != it->connection_infos.end(); ++it_info) {
2233 if (!it_info->best_connection) {
2234 continue;
2235 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002236
2237 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2238 const cricket::Candidate& local = it_info->local_candidate;
2239 const cricket::Candidate& remote = it_info->remote_candidate;
2240
2241 // Increment the counter for IceCandidatePairType.
2242 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2243 (local.type() == RELAY_PORT_TYPE &&
2244 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2245 type = kEnumCounterIceCandidatePairTypeTcp;
2246 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2247 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002248 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002249 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002250 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002251 metrics_observer_->IncrementEnumCounter(
2252 type, GetIceCandidatePairCounter(local, remote),
2253 kIceCandidatePairMax);
2254
2255 // Increment the counter for IP type.
2256 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002257 metrics_observer_->IncrementEnumCounter(
2258 kEnumCounterAddressFamily, kBestConnections_IPv4,
2259 kPeerConnectionAddressFamilyCounter_Max);
2260
2261 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002262 metrics_observer_->IncrementEnumCounter(
2263 kEnumCounterAddressFamily, kBestConnections_IPv6,
2264 kPeerConnectionAddressFamilyCounter_Max);
2265 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002266 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002267 }
2268
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002269 return;
2270 }
2271 }
2272}
2273
jbauchac8869e2015-07-03 01:36:14 -07002274void WebRtcSession::ReportNegotiatedCiphers(
2275 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002276 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002277 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2278 return;
2279 }
2280
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002281 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2282 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2283 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2284 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002285 return;
2286 }
2287
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002288 PeerConnectionEnumCounterType srtp_counter_type;
2289 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002290 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002291 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2292 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002293 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002294 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2295 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002296 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002297 srtp_counter_type = kEnumCounterDataSrtpCipher;
2298 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002299 } else {
2300 RTC_NOTREACHED();
2301 return;
2302 }
2303
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002304 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2305 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2306 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002307 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002308 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2309 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2310 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002311 }
2312}
2313
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002314void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002315 RTC_DCHECK(worker_thread()->IsCurrent());
2316 media_controller_->call_w()->OnSentPacket(sent_packet);
2317}
2318
zhihuang9763d562016-08-05 11:14:50 -07002319const std::string WebRtcSession::GetTransportName(
2320 const std::string& content_name) {
2321 cricket::BaseChannel* channel = GetChannel(content_name);
2322 if (!channel) {
2323#ifdef HAVE_QUIC
2324 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2325 content_name == quic_data_transport_->transport_name()) {
2326 return quic_data_transport_->transport_name();
2327 }
2328#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002329 if (sctp_transport_) {
2330 RTC_DCHECK(sctp_content_name_);
2331 RTC_DCHECK(sctp_transport_name_);
2332 if (content_name == *sctp_content_name_) {
2333 return *sctp_transport_name_;
2334 }
2335 }
zhihuang9763d562016-08-05 11:14:50 -07002336 // Return an empty string if failed to retrieve the transport name.
2337 return "";
2338 }
2339 return channel->transport_name();
2340}
zhihuangd82eee02016-08-26 11:25:05 -07002341
deadbeefac22f702017-01-12 21:59:29 -08002342void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002343 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002344 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002345 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2346}
2347
2348void WebRtcSession::DestroyVideoChannel() {
2349 SignalVideoChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002350 RTC_DCHECK(video_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002351 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002352 transport_name = video_channel_->rtp_dtls_transport()->transport_name();
2353 bool need_to_delete_rtcp = (video_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002354 channel_manager_->DestroyVideoChannel(video_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002355 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002356 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002357 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002358 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002359 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002360 }
2361}
2362
2363void WebRtcSession::DestroyVoiceChannel() {
2364 SignalVoiceChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002365 RTC_DCHECK(voice_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002366 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002367 transport_name = voice_channel_->rtp_dtls_transport()->transport_name();
2368 bool need_to_delete_rtcp = (voice_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002369 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002370 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002371 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002372 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002373 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002374 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002375 }
2376}
2377
2378void WebRtcSession::DestroyDataChannel() {
2379 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002380 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002381 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002382 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2383 bool need_to_delete_rtcp =
2384 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002385 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002386 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002387 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002388 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002389 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002390 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002391 }
2392}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393} // namespace webrtc