blob: d03f7568327fcaabafcaaa8452da44a1bf7bbef6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_sink.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/peerconnectioninterface.h"
ossuf515ab82016-12-07 04:52:58 -080024#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
deadbeef953c2ce2017-01-09 14:53:41 -080026#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000027#include "webrtc/p2p/base/portallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010028#include "webrtc/pc/channel.h"
29#include "webrtc/pc/channelmanager.h"
30#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080031#include "webrtc/pc/sctputils.h"
32#include "webrtc/pc/webrtcsessiondescriptionfactory.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020033#include "webrtc/rtc_base/basictypes.h"
34#include "webrtc/rtc_base/bind.h"
35#include "webrtc/rtc_base/checks.h"
36#include "webrtc/rtc_base/helpers.h"
37#include "webrtc/rtc_base/logging.h"
38#include "webrtc/rtc_base/stringencode.h"
39#include "webrtc/rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
zhihuang9763d562016-08-05 11:14:50 -070041#ifdef HAVE_QUIC
42#include "webrtc/p2p/quic/quictransportchannel.h"
43#endif // HAVE_QUIC
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045using cricket::ContentInfo;
46using cricket::ContentInfos;
47using cricket::MediaContentDescription;
48using cricket::SessionDescription;
49using cricket::TransportInfo;
50
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070051using cricket::LOCAL_PORT_TYPE;
52using cricket::STUN_PORT_TYPE;
53using cricket::RELAY_PORT_TYPE;
54using cricket::PRFLX_PORT_TYPE;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace webrtc {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000059const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
60 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062const char kInvalidCandidates[] = "Description contains invalid candidates.";
63const char kInvalidSdp[] = "Invalid session description.";
64const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
66const char kPushDownTDFailed[] =
67 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000068const char kSdpWithoutDtlsFingerprint[] =
69 "Called with SDP without DTLS fingerprint.";
70const char kSdpWithoutSdesCrypto[] =
71 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000072const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000073 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080076const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000077 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080078const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070080const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070082IceCandidatePairType GetIceCandidatePairCounter(
83 const cricket::Candidate& local,
84 const cricket::Candidate& remote) {
85 const auto& l = local.type();
86 const auto& r = remote.type();
87 const auto& host = LOCAL_PORT_TYPE;
88 const auto& srflx = STUN_PORT_TYPE;
89 const auto& relay = RELAY_PORT_TYPE;
90 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070091 if (l == host && r == host) {
92 bool local_private = IPIsPrivate(local.address().ipaddr());
93 bool remote_private = IPIsPrivate(remote.address().ipaddr());
94 if (local_private) {
95 if (remote_private) {
96 return kIceCandidatePairHostPrivateHostPrivate;
97 } else {
98 return kIceCandidatePairHostPrivateHostPublic;
99 }
100 } else {
101 if (remote_private) {
102 return kIceCandidatePairHostPublicHostPrivate;
103 } else {
104 return kIceCandidatePairHostPublicHostPublic;
105 }
106 }
107 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700108 if (l == host && r == srflx)
109 return kIceCandidatePairHostSrflx;
110 if (l == host && r == relay)
111 return kIceCandidatePairHostRelay;
112 if (l == host && r == prflx)
113 return kIceCandidatePairHostPrflx;
114 if (l == srflx && r == host)
115 return kIceCandidatePairSrflxHost;
116 if (l == srflx && r == srflx)
117 return kIceCandidatePairSrflxSrflx;
118 if (l == srflx && r == relay)
119 return kIceCandidatePairSrflxRelay;
120 if (l == srflx && r == prflx)
121 return kIceCandidatePairSrflxPrflx;
122 if (l == relay && r == host)
123 return kIceCandidatePairRelayHost;
124 if (l == relay && r == srflx)
125 return kIceCandidatePairRelaySrflx;
126 if (l == relay && r == relay)
127 return kIceCandidatePairRelayRelay;
128 if (l == relay && r == prflx)
129 return kIceCandidatePairRelayPrflx;
130 if (l == prflx && r == host)
131 return kIceCandidatePairPrflxHost;
132 if (l == prflx && r == srflx)
133 return kIceCandidatePairPrflxSrflx;
134 if (l == prflx && r == relay)
135 return kIceCandidatePairPrflxRelay;
136 return kIceCandidatePairMax;
137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139// Compares |answer| against |offer|. Comparision is done
140// for number of m-lines in answer against offer. If matches true will be
141// returned otherwise false.
142static bool VerifyMediaDescriptions(
143 const SessionDescription* answer, const SessionDescription* offer) {
144 if (offer->contents().size() != answer->contents().size())
145 return false;
146
147 for (size_t i = 0; i < offer->contents().size(); ++i) {
148 if ((offer->contents()[i].name) != answer->contents()[i].name) {
149 return false;
150 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000151 const MediaContentDescription* offer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 offer->contents()[i].description);
154 const MediaContentDescription* answer_mdesc =
155 static_cast<const MediaContentDescription*>(
156 answer->contents()[i].description);
157 if (offer_mdesc->type() != answer_mdesc->type()) {
158 return false;
159 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 }
161 return true;
162}
163
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800165// fingerprint, unless it's in a BUNDLE group, in which case only the
166// BUNDLE-tag section (first media section/description in the BUNDLE group)
167// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
168// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
169// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000170static bool VerifyCrypto(const SessionDescription* desc,
171 bool dtls_enabled,
172 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800173 const cricket::ContentGroup* bundle =
174 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 const ContentInfos& contents = desc->contents();
176 for (size_t index = 0; index < contents.size(); ++index) {
177 const ContentInfo* cinfo = &contents[index];
178 if (cinfo->rejected) {
179 continue;
180 }
deadbeefb7892532017-02-22 19:35:18 -0800181 if (bundle && bundle->HasContentName(cinfo->name) &&
182 cinfo->name != *(bundle->FirstContentName())) {
183 // This isn't the first media section in the BUNDLE group, so it's not
184 // required to have crypto attributes, since only the crypto attributes
185 // from the first section actually get used.
186 continue;
187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
deadbeefb7892532017-02-22 19:35:18 -0800189 // If the content isn't rejected or bundled into another m= section, crypto
190 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 const MediaContentDescription* media =
192 static_cast<const MediaContentDescription*>(cinfo->description);
193 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
194 if (!media || !tinfo) {
195 // Something is not right.
196 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return false;
199 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 LOG(LS_WARNING) <<
203 "Session description must have DTLS fingerprint if DTLS enabled.";
204 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000205 return false;
206 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000207 } else {
208 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 LOG(LS_WARNING) <<
210 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000211 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000212 return false;
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215 }
216
217 return true;
218}
219
deadbeefb7892532017-02-22 19:35:18 -0800220// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
221// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
222// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800224 const cricket::ContentGroup* bundle =
225 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000226 const ContentInfos& contents = desc->contents();
227 for (size_t index = 0; index < contents.size(); ++index) {
228 const ContentInfo* cinfo = &contents[index];
229 if (cinfo->rejected) {
230 continue;
231 }
deadbeefb7892532017-02-22 19:35:18 -0800232 if (bundle && bundle->HasContentName(cinfo->name) &&
233 cinfo->name != *(bundle->FirstContentName())) {
234 // This isn't the first media section in the BUNDLE group, so it's not
235 // required to have ufrag/password, since only the ufrag/password from
236 // the first section actually get used.
237 continue;
238 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000239
deadbeefb7892532017-02-22 19:35:18 -0800240 // If the content isn't rejected or bundled into another m= section,
241 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000242 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
243 if (!tinfo) {
244 // Something is not right.
245 LOG(LS_ERROR) << kInvalidSdp;
246 return false;
247 }
248 if (tinfo->description.ice_ufrag.empty() ||
249 tinfo->description.ice_pwd.empty()) {
250 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
251 return false;
252 }
253 }
254 return true;
255}
256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 uint32_t ssrc,
259 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800260 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 const cricket::ContentInfo* audio_info =
263 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000264 if (audio_info) {
265 const cricket::MediaContentDescription* audio_content =
266 static_cast<const cricket::MediaContentDescription*>(
267 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000269 const auto* found =
270 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
271 if (found) {
272 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000273 return true;
274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 }
276
277 const cricket::ContentInfo* video_info =
278 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000279 if (video_info) {
280 const cricket::MediaContentDescription* video_content =
281 static_cast<const cricket::MediaContentDescription*>(
282 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const auto* found =
285 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
286 if (found) {
287 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000288 return true;
289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 }
291 return false;
292}
293
deadbeef953c2ce2017-01-09 14:53:41 -0800294// Get the SCTP port out of a SessionDescription.
295// Return -1 if not found.
296static int GetSctpPort(const SessionDescription* session_description) {
297 const ContentInfo* content_info = GetFirstDataContent(session_description);
298 RTC_DCHECK(content_info);
299 if (!content_info) {
300 return -1;
301 }
302 const cricket::DataContentDescription* data =
303 static_cast<const cricket::DataContentDescription*>(
304 (content_info->description));
305 std::string value;
306 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
307 cricket::kGoogleSctpDataCodecName);
308 for (const cricket::DataCodec& codec : data->codecs()) {
309 if (!codec.Matches(match_pattern)) {
310 continue;
311 }
312 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
313 return rtc::FromString<int>(value);
314 }
315 }
316 return -1;
317}
318
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319static bool BadSdp(const std::string& source,
320 const std::string& type,
321 const std::string& reason,
322 std::string* err_desc) {
323 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700324 desc << "Failed to set " << source;
325 if (!type.empty()) {
326 desc << " " << type;
327 }
328 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000329
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 return false;
335}
336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338 const std::string& type,
339 const std::string& reason,
340 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
346}
347
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348static bool BadLocalSdp(const std::string& type,
349 const std::string& reason,
350 std::string* err_desc) {
351 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
352}
353
354static bool BadRemoteSdp(const std::string& type,
355 const std::string& reason,
356 std::string* err_desc) {
357 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
358}
359
360static bool BadOfferSdp(cricket::ContentSource source,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
364}
365
366static bool BadPranswerSdp(cricket::ContentSource source,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
370 reason, err_desc);
371}
372
373static bool BadAnswerSdp(cricket::ContentSource source,
374 const std::string& reason,
375 std::string* err_desc) {
376 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377}
378
deadbeefd59daf82015-10-14 15:02:44 -0700379#define GET_STRING_OF_STATE(state) \
380 case webrtc::WebRtcSession::state: \
381 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 break;
383
deadbeefd59daf82015-10-14 15:02:44 -0700384static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 std::string result;
386 switch (state) {
387 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_SENTOFFER)
389 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
390 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
391 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700393 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 default:
nissec80e7412017-01-11 05:56:46 -0800395 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 break;
397 }
398 return result;
399}
400
deadbeefd59daf82015-10-14 15:02:44 -0700401#define GET_STRING_OF_ERROR_CODE(err) \
402 case webrtc::WebRtcSession::err: \
403 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 break;
405
deadbeefd59daf82015-10-14 15:02:44 -0700406static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 std::string result;
408 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000409 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
411 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800413 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 break;
415 }
416 return result;
417}
418
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419static std::string MakeErrorString(const std::string& error,
420 const std::string& desc) {
421 std::ostringstream ret;
422 ret << error << " " << desc;
423 return ret.str();
424}
425
426static std::string MakeTdErrorString(const std::string& desc) {
427 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
deadbeef0ed85b22016-02-23 17:24:52 -0800430// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
431bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
432 const SessionDescriptionInterface* new_desc,
433 const std::string& content_name) {
434 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700435 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
deadbeef0ed85b22016-02-23 17:24:52 -0800437 const SessionDescription* new_sd = new_desc->description();
438 const SessionDescription* old_sd = old_desc->description();
439 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
440 if (!cinfo || cinfo->rejected) {
441 return false;
442 }
443 // If the content isn't rejected, check if ufrag and password has changed.
444 const cricket::TransportDescription* new_transport_desc =
445 new_sd->GetTransportDescriptionByName(content_name);
446 const cricket::TransportDescription* old_transport_desc =
447 old_sd->GetTransportDescriptionByName(content_name);
448 if (!new_transport_desc || !old_transport_desc) {
449 // No transport description exists. This is not an ICE restart.
450 return false;
451 }
452 if (cricket::IceCredentialsChanged(
453 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
454 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
455 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
456 << ".";
457 return true;
458 }
459 return false;
460}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
zhihuang29ff8442016-07-27 11:07:25 -0700462WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700463 Call* call,
464 cricket::ChannelManager* channel_manager,
465 const cricket::MediaConfig& media_config,
466 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700467 rtc::Thread* network_thread,
468 rtc::Thread* worker_thread,
469 rtc::Thread* signaling_thread,
470 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800471 std::unique_ptr<cricket::TransportController> transport_controller,
472 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700473 : network_thread_(network_thread),
474 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700475 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // RFC 3264: The numeric value of the session id and version in the
477 // o line MUST be representable with a "64 bit signed integer".
478 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700479 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700480 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800481 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700482 media_config_(media_config),
483 event_log_(event_log),
484 call_(call),
485 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 ice_observer_(NULL),
487 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700488 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000490 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000492 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700493 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
494 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700495 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700496 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700497 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700498 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700499 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700500 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700501 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700502 transport_controller_->SignalCandidatesRemoved.connect(
503 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700504 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800505 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506}
507
508WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800509 RTC_DCHECK(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000510 // Destroy video_channel_ first since it may have a pointer to the
511 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000512 if (video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800513 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000515 if (voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800516 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000517 }
deadbeef953c2ce2017-01-09 14:53:41 -0800518 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800519 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800520 }
521 if (sctp_transport_) {
522 SignalDataChannelDestroyed();
523 network_thread_->Invoke<void>(
524 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
zhihuang9763d562016-08-05 11:14:50 -0700526#ifdef HAVE_QUIC
527 if (quic_data_transport_) {
528 quic_data_transport_.reset();
529 }
530#endif
deadbeefd59daf82015-10-14 15:02:44 -0700531
532 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533}
534
wu@webrtc.org91053e72013-08-10 07:18:04 +0000535bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200537 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200538 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
539 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700540 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700541 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000542
Henrik Boström87713d02015-08-25 09:53:21 +0200543 // Obtain a certificate from RTCConfiguration if any were provided (optional).
544 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
545 if (!rtc_configuration.certificates.empty()) {
546 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
547 // just picking the first one. The decision should be made based on the DTLS
548 // handshake. The DTLS negotiations need to know about all certificates.
549 certificate = rtc_configuration.certificates[0];
550 }
551
honghaiz1f429e32015-09-28 07:57:34 -0700552 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700553
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000554 if (options.disable_encryption) {
555 dtls_enabled_ = false;
556 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200557 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200558 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800559 // |rtc_configuration| can override the default |dtls_enabled_| value.
560 if (rtc_configuration.enable_dtls_srtp) {
561 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000562 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000563 }
564
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000566 // It takes precendence over the disable_sctp_data_channels
567 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800568 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700570 }
571#ifdef HAVE_QUIC
572 else if (rtc_configuration.enable_quic) {
573 // Use QUIC instead of DTLS when |enable_quic| is true.
574 data_channel_type_ = cricket::DCT_QUIC;
575 transport_controller_->use_quic();
576 if (dtls_enabled_) {
577 LOG(LS_INFO) << "Using QUIC instead of DTLS";
578 }
579 quic_data_transport_.reset(
580 new QuicDataTransport(signaling_thread(), worker_thread(),
581 network_thread(), transport_controller_.get()));
582 }
583#endif // HAVE_QUIC
584 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000585 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000586 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000587 data_channel_type_ = cricket::DCT_SCTP;
588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
htaa2a49d92016-03-04 02:51:39 -0800591 video_options_.screencast_min_bitrate_kbps =
592 rtc_configuration.screencast_min_bitrate;
593 audio_options_.combined_audio_video_bwe =
594 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000595
kwiberg102c6a62015-10-30 02:47:38 -0700596 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100597 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200598
Karl Wibergbe579832015-11-10 22:34:18 +0100599 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
600 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200601
Henrik Boström87713d02015-08-25 09:53:21 +0200602 if (!dtls_enabled_) {
603 // Construct with DTLS disabled.
604 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200605 signaling_thread(), channel_manager_, this, id(),
606 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200607 } else {
608 // Construct with DTLS enabled.
609 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200610 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200611 signaling_thread(), channel_manager_, this, id(),
612 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200613 } else {
614 // Use the already generated certificate.
615 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200616 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200617 }
618 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000619
Henrik Boströmd8281982015-08-27 10:12:24 +0200620 webrtc_session_desc_factory_->SignalCertificateReady.connect(
621 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000622
wu@webrtc.org97077a32013-10-25 21:18:33 +0000623 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000625 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700626
jbauch5869f502017-06-29 12:31:36 -0700627 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
628 options.crypto_options.enable_encrypted_rtp_header_extensions);
629
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 return true;
631}
632
deadbeefd59daf82015-10-14 15:02:44 -0700633void WebRtcSession::Close() {
634 SetState(STATE_CLOSED);
635 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700636 call_ = nullptr;
deadbeef953c2ce2017-01-09 14:53:41 -0800637 RTC_DCHECK(!voice_channel_);
638 RTC_DCHECK(!video_channel_);
639 RTC_DCHECK(!rtp_data_channel_);
640 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641}
642
deadbeef0ed85b22016-02-23 17:24:52 -0800643cricket::BaseChannel* WebRtcSession::GetChannel(
644 const std::string& content_name) {
645 if (voice_channel() && voice_channel()->content_name() == content_name) {
646 return voice_channel();
647 }
648 if (video_channel() && video_channel()->content_name() == content_name) {
649 return video_channel();
650 }
deadbeef953c2ce2017-01-09 14:53:41 -0800651 if (rtp_data_channel() &&
652 rtp_data_channel()->content_name() == content_name) {
653 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800654 }
655 return nullptr;
656}
657
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000658cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
659 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660}
661
deadbeef953c2ce2017-01-09 14:53:41 -0800662bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
663 if (!local_description() || !remote_description()) {
664 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
665 << "SSL Role of the SCTP transport.";
666 return false;
667 }
668 if (!sctp_transport_) {
669 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
670 << "SSL Role of the SCTP transport.";
671 return false;
672 }
673
674 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
675}
676
677bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800678 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800679 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800680 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000681 << "SSL Role of the session.";
682 return false;
683 }
684
deadbeef953c2ce2017-01-09 14:53:41 -0800685 return transport_controller_->GetSslRole(GetTransportName(content_name),
686 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000687}
688
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000689void WebRtcSession::CreateOffer(
690 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700691 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
692 const cricket::MediaSessionOptions& session_options) {
693 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000694}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695
deadbeefab9b2d12015-10-14 11:33:11 -0700696void WebRtcSession::CreateAnswer(
697 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700698 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800699 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700}
701
702bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
703 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800704 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700705
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000706 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700707 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000708
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000709 // Validate SDP.
710 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
711 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
deadbeefd59daf82015-10-14 15:02:44 -0700714 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000715 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700717 initial_offerer_ = true;
718 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
deadbeeffe4a8a42016-12-20 17:56:17 -0800721 if (action == kAnswer) {
722 current_local_description_.reset(desc_temp.release());
723 pending_local_description_.reset(nullptr);
724 current_remote_description_.reset(pending_remote_description_.release());
725 } else {
726 pending_local_description_.reset(desc_temp.release());
727 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728
729 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800730 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // TODO(mallinath) - Handle CreateChannel failure, as new local description
732 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000733 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 }
735
deadbeefcbecd352015-09-23 11:50:27 -0700736 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800737 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000739 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 return false;
741 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800742 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700743 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800744 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
deadbeef0ed85b22016-02-23 17:24:52 -0800747 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700748 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000749 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
751 return true;
752}
753
754bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
755 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800756 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700757
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000758 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700759 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000760
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000761 // Validate SDP.
762 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
763 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
deadbeeffe4a8a42016-12-20 17:56:17 -0800766 const SessionDescriptionInterface* old_remote_description =
767 remote_description();
768 // Grab ownership of the description being replaced for the remainder of this
769 // method, since it's used below.
770 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
771 Action action = GetAction(desc->type());
772 if (action == kAnswer) {
773 replaced_remote_description.reset(
774 pending_remote_description_ ? pending_remote_description_.release()
775 : current_remote_description_.release());
776 current_remote_description_.reset(desc_temp.release());
777 pending_remote_description_.reset(nullptr);
778 current_local_description_.reset(pending_local_description_.release());
779 } else {
780 replaced_remote_description.reset(pending_remote_description_.release());
781 pending_remote_description_.reset(desc_temp.release());
782 }
deadbeefd59daf82015-10-14 15:02:44 -0700783
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 // Transport and Media channels will be created only when offer is set.
785 if (action == kOffer && !CreateChannels(desc->description())) {
786 // TODO(mallinath) - Handle CreateChannel failure, as new local description
787 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000788 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 }
790
deadbeefcbecd352015-09-23 11:50:27 -0700791 // Remove unused channels if MediaContentDescription is rejected.
792 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
794 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
795 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000796 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return false;
798 }
799
deadbeeffe4a8a42016-12-20 17:56:17 -0800800 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000801 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
deadbeeffe4a8a42016-12-20 17:56:17 -0800804 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800805 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800806 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800807 // Check if this new SessionDescription contains new ICE ufrag and
808 // password that indicates the remote peer requests an ICE restart.
809 // TODO(deadbeef): When we start storing both the current and pending
810 // remote description, this should reset pending_ice_restarts and compare
811 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800812 if (CheckForRemoteIceRestart(old_remote_description, desc,
813 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800814 if (action == kOffer) {
815 pending_ice_restarts_.insert(content.name);
816 }
817 } else {
818 // We retain all received candidates only if ICE is not restarted.
819 // When ICE is restarted, all previous candidates belong to an old
820 // generation and should not be kept.
821 // TODO(deadbeef): This goes against the W3C spec which says the remote
822 // description should only contain candidates from the last set remote
823 // description plus any candidates added since then. We should remove
824 // this once we're sure it won't break anything.
825 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800826 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800827 }
828 }
honghaiz503726c2015-07-31 12:37:38 -0700829 }
830
deadbeefd59daf82015-10-14 15:02:44 -0700831 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000832 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000834
835 // Set the the ICE connection state to connecting since the connection may
836 // become writable with peer reflexive candidates before any remote candidate
837 // is signaled.
838 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
839 // is to have a new signal the indicates a change in checking state from the
840 // transport and expose a new checking() member from transport that can be
841 // read to determine the current checking state. The existing SignalConnecting
842 // actually means "gathering candidates", so cannot be be used here.
843 if (desc->type() != SessionDescriptionInterface::kOffer &&
844 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
845 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
846 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 return true;
848}
849
deadbeefd59daf82015-10-14 15:02:44 -0700850void WebRtcSession::LogState(State old_state, State new_state) {
851 LOG(LS_INFO) << "Session:" << id()
852 << " Old state:" << GetStateString(old_state)
853 << " New state:" << GetStateString(new_state);
854}
855
856void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800857 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700858 if (state != state_) {
859 LogState(state_, state);
860 state_ = state;
861 SignalState(this, state_);
862 }
863}
864
865void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800866 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700867 if (error != error_) {
868 error_ = error;
869 error_desc_ = error_desc;
870 }
871}
872
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873bool WebRtcSession::UpdateSessionState(
874 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800876 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700877
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 // If there's already a pending error then no state transition should happen.
879 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800880 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000881 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000883 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
884 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
deadbeefd59daf82015-10-14 15:02:44 -0700886 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
887 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000888 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700889 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000890 }
deadbeefd59daf82015-10-14 15:02:44 -0700891 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000892 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 }
894 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000895 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
896 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 }
898 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700899 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
900 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000901 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700902 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000903 }
deadbeefd59daf82015-10-14 15:02:44 -0700904 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000905 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 }
907 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700908 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800909 local_description()->description()->GetGroupByName(
910 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700911 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800912 remote_description()->description()->GetGroupByName(
913 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700914 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800915 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700916 const cricket::ContentGroup* answer_bundle =
917 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
918 if (!EnableBundle(*answer_bundle)) {
919 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
920 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
921 }
922 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800923 // Only push down the transport description after enabling BUNDLE; we don't
924 // want to push down a description on a transport about to be destroyed.
925 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
926 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
927 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700929 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000930 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700931 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000932 }
deadbeefd59daf82015-10-14 15:02:44 -0700933 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000934 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 }
936 }
937 return true;
938}
939
940WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
941 if (type == SessionDescriptionInterface::kOffer) {
942 return WebRtcSession::kOffer;
943 } else if (type == SessionDescriptionInterface::kPrAnswer) {
944 return WebRtcSession::kPrAnswer;
945 } else if (type == SessionDescriptionInterface::kAnswer) {
946 return WebRtcSession::kAnswer;
947 }
nisseede5da42017-01-12 05:15:36 -0800948 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 return WebRtcSession::kOffer;
950}
951
deadbeefd59daf82015-10-14 15:02:44 -0700952bool WebRtcSession::PushdownMediaDescription(
953 cricket::ContentAction action,
954 cricket::ContentSource source,
955 std::string* err) {
956 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
957 if (!ch) {
958 return true;
959 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800960 return ch->PushdownLocalDescription(local_description()->description(),
961 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700962 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800963 return ch->PushdownRemoteDescription(remote_description()->description(),
964 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700965 }
966 };
967
deadbeef953c2ce2017-01-09 14:53:41 -0800968 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
969 set_content(rtp_data_channel()));
970 // Need complete offer/answer with an SCTP m= section before starting SCTP,
971 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
972 if (sctp_transport_ && local_description() && remote_description() &&
973 cricket::GetFirstDataContent(local_description()->description()) &&
974 cricket::GetFirstDataContent(remote_description()->description())) {
975 ret &= network_thread_->Invoke<bool>(
976 RTC_FROM_HERE,
977 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
978 }
979 return ret;
980}
981
982bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
983 RTC_DCHECK(network_thread_->IsCurrent());
984 RTC_DCHECK(local_description());
985 RTC_DCHECK(remote_description());
986 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
987 // When we support "max-message-size", that would also be pushed down here.
988 return sctp_transport_->Start(
989 GetSctpPort(local_description()->description()),
990 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700991}
992
993bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
994 cricket::ContentAction action,
995 std::string* error_desc) {
996 RTC_DCHECK(signaling_thread()->IsCurrent());
997
998 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800999 return PushdownLocalTransportDescription(local_description()->description(),
1000 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001001 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001002 return PushdownRemoteTransportDescription(remote_description()->description(),
1003 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001004}
1005
1006bool WebRtcSession::PushdownLocalTransportDescription(
1007 const SessionDescription* sdesc,
1008 cricket::ContentAction action,
1009 std::string* err) {
1010 RTC_DCHECK(signaling_thread()->IsCurrent());
1011
1012 if (!sdesc) {
1013 return false;
1014 }
1015
1016 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1017 if (!transport_controller_->SetLocalTransportDescription(
1018 tinfo.content_name, tinfo.description, action, err)) {
1019 return false;
1020 }
1021 }
1022
1023 return true;
1024}
1025
1026bool WebRtcSession::PushdownRemoteTransportDescription(
1027 const SessionDescription* sdesc,
1028 cricket::ContentAction action,
1029 std::string* err) {
1030 RTC_DCHECK(signaling_thread()->IsCurrent());
1031
1032 if (!sdesc) {
1033 return false;
1034 }
1035
1036 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1037 if (!transport_controller_->SetRemoteTransportDescription(
1038 tinfo.content_name, tinfo.description, action, err)) {
1039 return false;
1040 }
1041 }
1042
1043 return true;
1044}
1045
1046bool WebRtcSession::GetTransportDescription(
1047 const SessionDescription* description,
1048 const std::string& content_name,
1049 cricket::TransportDescription* tdesc) {
1050 if (!description || !tdesc) {
1051 return false;
1052 }
1053 const TransportInfo* transport_info =
1054 description->GetTransportInfoByName(content_name);
1055 if (!transport_info) {
1056 return false;
1057 }
1058 *tdesc = transport_info->description;
1059 return true;
1060}
1061
deadbeefcbecd352015-09-23 11:50:27 -07001062bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1063 const std::string* first_content_name = bundle.FirstContentName();
1064 if (!first_content_name) {
1065 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1066 return false;
1067 }
1068 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001069
zhihuang9763d562016-08-05 11:14:50 -07001070#ifdef HAVE_QUIC
1071 if (quic_data_transport_ &&
1072 bundle.HasContentName(quic_data_transport_->content_name()) &&
1073 quic_data_transport_->transport_name() != transport_name) {
1074 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1075 << " on " << transport_name << "with QUIC.";
1076 }
1077#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001078 auto maybe_set_transport = [this, bundle,
1079 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001080 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001081 return true;
1082 }
1083
zhihuangf5b251b2017-01-12 19:37:48 -08001084 std::string old_transport_name = ch->transport_name();
1085 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001086 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1087 << " on " << transport_name << ".";
1088 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001089 }
torbjornga81a42f2015-09-23 02:16:58 -07001090
zhihuangb2cdd932017-01-19 16:54:25 -08001091 cricket::DtlsTransportInternal* rtp_dtls_transport =
1092 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001093 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001094 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1095 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001096 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001097 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001098 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1099 }
1100
zhihuangb2cdd932017-01-19 16:54:25 -08001101 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001102 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1103 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001104 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001105 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001106 // If the channel needs rtcp, it means that the channel used to have a
1107 // rtcp transport which needs to be deleted now.
1108 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001109 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001110 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001111 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001112 return true;
1113 };
1114
deadbeefcbecd352015-09-23 11:50:27 -07001115 if (!maybe_set_transport(voice_channel()) ||
1116 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001117 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001118 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001119 }
deadbeef953c2ce2017-01-09 14:53:41 -08001120 // For SCTP, transport creation/deletion happens here instead of in the
1121 // object itself.
1122 if (sctp_transport_) {
1123 RTC_DCHECK(sctp_transport_name_);
1124 RTC_DCHECK(sctp_content_name_);
1125 if (transport_name != *sctp_transport_name_ &&
1126 bundle.HasContentName(*sctp_content_name_)) {
1127 network_thread_->Invoke<void>(
1128 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1129 transport_name));
1130 }
1131 }
deadbeefcbecd352015-09-23 11:50:27 -07001132
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001133 return true;
1134}
1135
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001137 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001138 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1139 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001140 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 }
1142
1143 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001144 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 return false;
1146 }
1147
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001148 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001149 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1150 if (!valid) {
1151 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 }
1153
1154 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001155 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001156 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 return false;
1158 }
1159
deadbeefd59daf82015-10-14 15:02:44 -07001160 if (ready) {
1161 return UseCandidate(candidate);
1162 } else {
1163 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1164 return true;
1165 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166}
1167
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001168bool WebRtcSession::RemoveRemoteIceCandidates(
1169 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001170 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001171 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1172 << "removed without any remote session description.";
1173 return false;
1174 }
1175
1176 if (candidates.empty()) {
1177 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1178 return false;
1179 }
1180
deadbeeffe4a8a42016-12-20 17:56:17 -08001181 size_t number_removed =
1182 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001183 if (number_removed != candidates.size()) {
1184 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1185 << "Requested " << candidates.size() << " but only "
1186 << number_removed << " are removed.";
1187 }
1188
1189 // Remove the candidates from the transport controller.
1190 std::string error;
1191 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1192 if (!res && !error.empty()) {
1193 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1194 }
1195 return true;
1196}
1197
deadbeefd59daf82015-10-14 15:02:44 -07001198cricket::IceConfig WebRtcSession::ParseIceConfig(
1199 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001200 cricket::ContinualGatheringPolicy gathering_policy;
1201 // TODO(honghaiz): Add the third continual gathering policy in
1202 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1203 switch (config.continual_gathering_policy) {
1204 case PeerConnectionInterface::GATHER_ONCE:
1205 gathering_policy = cricket::GATHER_ONCE;
1206 break;
1207 case PeerConnectionInterface::GATHER_CONTINUALLY:
1208 gathering_policy = cricket::GATHER_CONTINUALLY;
1209 break;
1210 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001211 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001212 gathering_policy = cricket::GATHER_ONCE;
1213 }
deadbeefd59daf82015-10-14 15:02:44 -07001214 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001215 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001216 ice_config.prioritize_most_likely_candidate_pairs =
1217 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001218 ice_config.backup_connection_ping_interval =
1219 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001220 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001221 ice_config.presume_writable_when_fully_relayed =
1222 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001223 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001224 ice_config.regather_all_networks_interval_range =
1225 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001226 return ice_config;
1227}
1228
1229void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1230 transport_controller_->SetIceConfig(config);
1231}
1232
1233void WebRtcSession::MaybeStartGathering() {
1234 transport_controller_->MaybeStartGathering();
1235}
1236
Peter Boström0c4e06b2015-10-07 12:23:21 +02001237bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1238 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001239 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001241 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001242 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1243 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244}
1245
Peter Boström0c4e06b2015-10-07 12:23:21 +02001246bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1247 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001248 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001249 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001250 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001251 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1252 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253}
1254
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001255std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001257 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 return desc.str();
1259}
1260
wu@webrtc.org78187522013-10-07 23:32:02 +00001261bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001262 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001263 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001264 if (!rtp_data_channel_ && !sctp_transport_) {
1265 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1266 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001267 return false;
1268 }
deadbeef953c2ce2017-01-09 14:53:41 -08001269 return rtp_data_channel_
1270 ? rtp_data_channel_->SendData(params, payload, result)
1271 : network_thread_->Invoke<bool>(
1272 RTC_FROM_HERE,
1273 Bind(&cricket::SctpTransportInternal::SendData,
1274 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001275}
1276
1277bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001278 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001279 // Don't log an error here, because DataChannels are expected to call
1280 // ConnectDataChannel in this state. It's the only way to initially tell
1281 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001282 return false;
1283 }
deadbeef953c2ce2017-01-09 14:53:41 -08001284 if (rtp_data_channel_) {
1285 rtp_data_channel_->SignalReadyToSendData.connect(
1286 webrtc_data_channel, &DataChannel::OnChannelReady);
1287 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1288 &DataChannel::OnDataReceived);
1289 } else {
1290 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1291 &DataChannel::OnChannelReady);
1292 SignalSctpDataReceived.connect(webrtc_data_channel,
1293 &DataChannel::OnDataReceived);
1294 SignalSctpStreamClosedRemotely.connect(
1295 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1296 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001297 return true;
1298}
1299
1300void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001301 if (!rtp_data_channel_ && !sctp_transport_) {
1302 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1303 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001304 return;
1305 }
deadbeef953c2ce2017-01-09 14:53:41 -08001306 if (rtp_data_channel_) {
1307 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1308 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1309 } else {
1310 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1311 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1312 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1313 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001314}
1315
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001316void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001317 if (!sctp_transport_) {
1318 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001319 return;
1320 }
deadbeef953c2ce2017-01-09 14:53:41 -08001321 network_thread_->Invoke<void>(
1322 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1323 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001324}
1325
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001326void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001327 if (!sctp_transport_) {
1328 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001329 << "NULL.";
1330 return;
1331 }
deadbeef953c2ce2017-01-09 14:53:41 -08001332 network_thread_->Invoke<void>(
1333 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1334 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001335}
1336
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001337bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001338 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1339 sctp_ready_to_send_data_;
1340}
1341
1342std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001343 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001344 ChannelNamePairs channel_name_pairs;
1345 if (voice_channel()) {
1346 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1347 voice_channel()->content_name(), voice_channel()->transport_name()));
1348 }
1349 if (video_channel()) {
1350 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1351 video_channel()->content_name(), video_channel()->transport_name()));
1352 }
1353 if (rtp_data_channel()) {
1354 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1355 ChannelNamePair(rtp_data_channel()->content_name(),
1356 rtp_data_channel()->transport_name()));
1357 }
1358 if (sctp_transport_) {
1359 RTC_DCHECK(sctp_content_name_);
1360 RTC_DCHECK(sctp_transport_name_);
1361 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1362 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1363 }
1364 return GetStats(channel_name_pairs);
1365}
1366
1367std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1368 const ChannelNamePairs& channel_name_pairs) {
1369 if (network_thread()->IsCurrent()) {
1370 return GetStats_n(channel_name_pairs);
1371 }
1372 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1373 RTC_FROM_HERE,
1374 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1375}
1376
1377bool WebRtcSession::GetLocalCertificate(
1378 const std::string& transport_name,
1379 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1380 return transport_controller_->GetLocalCertificate(transport_name,
1381 certificate);
1382}
1383
1384std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1385 const std::string& transport_name) {
1386 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001387}
1388
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389cricket::DataChannelType WebRtcSession::data_channel_type() const {
1390 return data_channel_type_;
1391}
1392
deadbeef0ed85b22016-02-23 17:24:52 -08001393bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1394 return pending_ice_restarts_.find(content_name) !=
1395 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001396}
1397
deadbeefd1a38b52016-12-10 13:15:33 -08001398void WebRtcSession::SetNeedsIceRestartFlag() {
1399 transport_controller_->SetNeedsIceRestartFlag();
1400}
1401
1402bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1403 return transport_controller_->NeedsIceRestart(content_name);
1404}
1405
Henrik Boströmd8281982015-08-27 10:12:24 +02001406void WebRtcSession::OnCertificateReady(
1407 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001408 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001409}
1410
deadbeef953c2ce2017-01-09 14:53:41 -08001411void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1412 SetError(ERROR_TRANSPORT,
1413 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1414}
1415
Henrik Boströmd8281982015-08-27 10:12:24 +02001416bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001417 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001418}
1419
deadbeefcbecd352015-09-23 11:50:27 -07001420const rtc::scoped_refptr<rtc::RTCCertificate>&
1421WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001422 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001423}
1424
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425void WebRtcSession::SetIceConnectionState(
1426 PeerConnectionInterface::IceConnectionState state) {
1427 if (ice_connection_state_ == state) {
1428 return;
1429 }
1430
deadbeefcbecd352015-09-23 11:50:27 -07001431 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1432 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001433 RTC_DCHECK(ice_connection_state_ !=
1434 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 ice_connection_state_ = state;
1436 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001437 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 }
1439}
1440
deadbeefcbecd352015-09-23 11:50:27 -07001441void WebRtcSession::OnTransportControllerConnectionState(
1442 cricket::IceConnectionState state) {
1443 switch (state) {
1444 case cricket::kIceConnectionConnecting:
1445 // If the current state is Connected or Completed, then there were
1446 // writable channels but now there are not, so the next state must
1447 // be Disconnected.
1448 // kIceConnectionConnecting is currently used as the default,
1449 // un-connected state by the TransportController, so its only use is
1450 // detecting disconnections.
1451 if (ice_connection_state_ ==
1452 PeerConnectionInterface::kIceConnectionConnected ||
1453 ice_connection_state_ ==
1454 PeerConnectionInterface::kIceConnectionCompleted) {
1455 SetIceConnectionState(
1456 PeerConnectionInterface::kIceConnectionDisconnected);
1457 }
torbjornga81a42f2015-09-23 02:16:58 -07001458 break;
deadbeefcbecd352015-09-23 11:50:27 -07001459 case cricket::kIceConnectionFailed:
1460 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1461 break;
1462 case cricket::kIceConnectionConnected:
1463 LOG(LS_INFO) << "Changing to ICE connected state because "
1464 << "all transports are writable.";
1465 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1466 break;
1467 case cricket::kIceConnectionCompleted:
1468 LOG(LS_INFO) << "Changing to ICE completed state because "
1469 << "all transports are complete.";
1470 if (ice_connection_state_ !=
1471 PeerConnectionInterface::kIceConnectionConnected) {
1472 // If jumping directly from "checking" to "connected",
1473 // signal "connected" first.
1474 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1475 }
1476 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1477 if (metrics_observer_) {
1478 ReportTransportStats();
1479 }
1480 break;
1481 default:
nissec80e7412017-01-11 05:56:46 -08001482 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001483 }
deadbeefcbecd352015-09-23 11:50:27 -07001484}
1485
1486void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001487 SetIceConnectionReceiving(receiving);
1488}
1489
1490void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1491 if (ice_connection_receiving_ == receiving) {
1492 return;
1493 }
1494 ice_connection_receiving_ = receiving;
1495 if (ice_observer_) {
1496 ice_observer_->OnIceConnectionReceivingChange(receiving);
1497 }
1498}
1499
deadbeefcbecd352015-09-23 11:50:27 -07001500void WebRtcSession::OnTransportControllerCandidatesGathered(
1501 const std::string& transport_name,
1502 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001503 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001504 int sdp_mline_index;
1505 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1506 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1507 << transport_name << " not found";
1508 return;
1509 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510
deadbeefcbecd352015-09-23 11:50:27 -07001511 for (cricket::Candidates::const_iterator citer = candidates.begin();
1512 citer != candidates.end(); ++citer) {
1513 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001514 std::unique_ptr<JsepIceCandidate> candidate(
1515 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001516 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001517 mutable_local_description()->AddCandidate(candidate.get());
1518 }
1519 if (ice_observer_) {
1520 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001521 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 }
1523}
1524
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001525void WebRtcSession::OnTransportControllerCandidatesRemoved(
1526 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001527 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001528 // Sanity check.
1529 for (const cricket::Candidate& candidate : candidates) {
1530 if (candidate.transport_name().empty()) {
1531 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1532 << "empty content name in candidate "
1533 << candidate.ToString();
1534 return;
1535 }
1536 }
1537
deadbeeffe4a8a42016-12-20 17:56:17 -08001538 if (local_description()) {
1539 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001540 }
1541 if (ice_observer_) {
1542 ice_observer_->OnIceCandidatesRemoved(candidates);
1543 }
1544}
1545
deadbeef953c2ce2017-01-09 14:53:41 -08001546void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1547 rtc::SSLHandshakeError error) {
1548 if (metrics_observer_) {
1549 metrics_observer_->IncrementEnumCounter(
1550 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1551 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1552 }
1553}
1554
1555// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556void WebRtcSession::EnableChannels() {
1557 if (voice_channel_ && !voice_channel_->enabled())
1558 voice_channel_->Enable(true);
1559
1560 if (video_channel_ && !video_channel_->enabled())
1561 video_channel_->Enable(true);
1562
deadbeef953c2ce2017-01-09 14:53:41 -08001563 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1564 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565}
1566
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567// Returns the media index for a local ice candidate given the content name.
1568bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1569 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001570 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001572 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573
1574 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001575 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 for (size_t index = 0; index < contents.size(); ++index) {
1577 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001578 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579 content_found = true;
1580 break;
1581 }
1582 }
1583 return content_found;
1584}
1585
1586bool WebRtcSession::UseCandidatesInSessionDescription(
1587 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001588 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001590 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001592
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1594 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001595 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001596 const IceCandidateInterface* candidate = candidates->at(n);
1597 bool valid = false;
1598 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1599 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001600 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1601 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001602 }
1603 continue;
1604 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001605 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001606 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 break;
deadbeefd59daf82015-10-14 15:02:44 -07001608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 }
1610 }
1611 return ret;
1612}
1613
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001614bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001616 size_t remote_content_size =
1617 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001619 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 return false;
1621 }
1622
1623 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001624 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 std::vector<cricket::Candidate> candidates;
1626 candidates.push_back(candidate->candidate());
1627 // Invoking BaseSession method to handle remote candidates.
1628 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001629 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1630 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 // Candidates successfully submitted for checking.
1632 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1633 ice_connection_state_ ==
1634 PeerConnectionInterface::kIceConnectionDisconnected) {
1635 // If state is New, then the session has just gotten its first remote ICE
1636 // candidates, so go to Checking.
1637 // If state is Disconnected, the session is re-using old candidates or
1638 // receiving additional ones, so go to Checking.
1639 // If state is Connected, stay Connected.
1640 // TODO(bemasc): If state is Connected, and the new candidates are for a
1641 // newly added transport, then the state actually _should_ move to
1642 // checking. Add a way to distinguish that case.
1643 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1644 }
1645 // TODO(bemasc): If state is Completed, go back to Connected.
1646 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001647 if (!error.empty()) {
1648 LOG(LS_WARNING) << error;
1649 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 }
1651 return true;
1652}
1653
deadbeefcbecd352015-09-23 11:50:27 -07001654void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001655 // Destroy video_channel_ first since it may have a pointer to the
1656 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 const cricket::ContentInfo* video_info =
1658 cricket::GetFirstVideoContent(desc);
1659 if ((!video_info || video_info->rejected) && video_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001660 DestroyVideoChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 }
1662
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001663 const cricket::ContentInfo* voice_info =
1664 cricket::GetFirstAudioContent(desc);
1665 if ((!voice_info || voice_info->rejected) && voice_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001666 DestroyVoiceChannel();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001667 }
1668
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 const cricket::ContentInfo* data_info =
1670 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001671 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001672 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001673 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001674 }
1675 if (sctp_transport_) {
1676 SignalDataChannelDestroyed();
1677 network_thread_->Invoke<void>(
1678 RTC_FROM_HERE,
1679 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001680 }
1681#ifdef HAVE_QUIC
1682 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1683 if (quic_data_transport_) {
1684 quic_data_transport_.reset();
1685 }
1686#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 }
1688}
1689
skvlad6c87a672016-05-17 17:49:52 -07001690// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1691// if the channel is not part of any bundle.
1692const std::string* WebRtcSession::GetBundleTransportName(
1693 const cricket::ContentInfo* content,
1694 const cricket::ContentGroup* bundle) {
1695 if (!bundle) {
1696 return nullptr;
1697 }
1698 const std::string* first_content_name = bundle->FirstContentName();
1699 if (!first_content_name) {
1700 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1701 return nullptr;
1702 }
1703 if (!bundle->HasContentName(content->name)) {
1704 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1705 return nullptr;
1706 }
1707 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1708 return first_content_name;
1709}
1710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001712 const cricket::ContentGroup* bundle_group = nullptr;
1713 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1714 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1715 if (!bundle_group) {
1716 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1717 return false;
1718 }
1719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 // Creating the media channels and transport proxies.
1721 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1722 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001723 if (!CreateVoiceChannel(voice,
1724 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 LOG(LS_ERROR) << "Failed to create voice channel.";
1726 return false;
1727 }
1728 }
1729
1730 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1731 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001732 if (!CreateVideoChannel(video,
1733 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 LOG(LS_ERROR) << "Failed to create video channel.";
1735 return false;
1736 }
1737 }
1738
1739 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001740 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1741 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001742 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 LOG(LS_ERROR) << "Failed to create data channel.";
1744 return false;
1745 }
1746 }
1747
1748 return true;
1749}
1750
skvlad6c87a672016-05-17 17:49:52 -07001751bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1752 const std::string* bundle_transport) {
1753 bool require_rtcp_mux =
1754 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001755
1756 std::string transport_name =
1757 bundle_transport ? *bundle_transport : content->name;
1758
zhihuangb2cdd932017-01-19 16:54:25 -08001759 cricket::DtlsTransportInternal* rtp_dtls_transport =
1760 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001761 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001762 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001763 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001764 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001765 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1766 }
1767
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001769 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001770 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1771 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001772 if (!voice_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001773 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001774 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001775 if (rtcp_dtls_transport) {
1776 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001777 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1778 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001779 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001780 }
zhihuangf5b251b2017-01-12 19:37:48 -08001781
deadbeefac22f702017-01-12 21:59:29 -08001782 voice_channel_->SignalRtcpMuxFullyActive.connect(
1783 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001784 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1785 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001786
1787 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001788 voice_channel_->SignalSentPacket.connect(this,
1789 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001790 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
skvlad6c87a672016-05-17 17:49:52 -07001793bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1794 const std::string* bundle_transport) {
1795 bool require_rtcp_mux =
1796 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001797
1798 std::string transport_name =
1799 bundle_transport ? *bundle_transport : content->name;
1800
zhihuangb2cdd932017-01-19 16:54:25 -08001801 cricket::DtlsTransportInternal* rtp_dtls_transport =
1802 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001803 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001804 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001805 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001806 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001807 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1808 }
1809
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 video_channel_.reset(channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001811 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001812 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
1813 video_options_));
zhihuangf5b251b2017-01-12 19:37:48 -08001814
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001815 if (!video_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001816 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001817 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001818 if (rtcp_dtls_transport) {
1819 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001820 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1821 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001822 return false;
1823 }
zhihuangf5b251b2017-01-12 19:37:48 -08001824
deadbeefac22f702017-01-12 21:59:29 -08001825 video_channel_->SignalRtcpMuxFullyActive.connect(
1826 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001827 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1828 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001829
1830 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001831 video_channel_->SignalSentPacket.connect(this,
1832 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001833 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834}
1835
skvlad6c87a672016-05-17 17:49:52 -07001836bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1837 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001838 const std::string transport_name =
1839 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001840#ifdef HAVE_QUIC
1841 if (data_channel_type_ == cricket::DCT_QUIC) {
1842 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001843 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001844 return true;
1845 }
1846#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001847 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001848 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001849 if (!sctp_factory_) {
1850 LOG(LS_ERROR)
1851 << "Trying to create SCTP transport, but didn't compile with "
1852 "SCTP support (HAVE_SCTP)";
1853 return false;
1854 }
1855 if (!network_thread_->Invoke<bool>(
1856 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1857 this, content->name, transport_name))) {
1858 return false;
1859 };
1860 } else {
1861 bool require_rtcp_mux =
1862 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001863
1864 std::string transport_name =
1865 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001866 cricket::DtlsTransportInternal* rtp_dtls_transport =
1867 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001868 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001869 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001870 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001871 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001872 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1873 }
1874
deadbeef953c2ce2017-01-09 14:53:41 -08001875 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001876 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001877 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001878 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001879
deadbeef953c2ce2017-01-09 14:53:41 -08001880 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001881 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001882 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001883 if (rtcp_dtls_transport) {
1884 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001885 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1886 }
deadbeef953c2ce2017-01-09 14:53:41 -08001887 return false;
1888 }
zhihuangf5b251b2017-01-12 19:37:48 -08001889
deadbeefac22f702017-01-12 21:59:29 -08001890 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1891 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001892 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1893 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1894 rtp_data_channel_->SignalSentPacket.connect(this,
1895 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001896 }
1897
deadbeefab9b2d12015-10-14 11:33:11 -07001898 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001899
wu@webrtc.org91053e72013-08-10 07:18:04 +00001900 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901}
1902
stefanf79ade12017-06-02 06:44:03 -07001903Call::Stats WebRtcSession::GetCallStats() {
1904 if (!worker_thread()->IsCurrent()) {
1905 return worker_thread()->Invoke<Call::Stats>(
1906 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1907 }
zhihuang38ede132017-06-15 12:52:32 -07001908 if (!call_)
1909 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001910 return call_->GetStats();
1911}
1912
hbosdf6075a2016-12-19 04:58:02 -08001913std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1914 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001915 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001916 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1917 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1918 &channel_name_pairs.video,
1919 &channel_name_pairs.data }) {
1920 if (*channel_name_pair) {
1921 cricket::TransportStats transport_stats;
1922 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1923 &transport_stats)) {
1924 return nullptr;
1925 }
1926 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1927 (*channel_name_pair)->transport_name;
1928 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1929 std::move(transport_stats);
1930 }
1931 }
1932 return session_stats;
1933}
1934
deadbeef953c2ce2017-01-09 14:53:41 -08001935bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1936 const std::string& transport_name) {
1937 RTC_DCHECK(network_thread_->IsCurrent());
1938 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001939 cricket::DtlsTransportInternal* tc =
1940 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001941 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1942 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1943 RTC_DCHECK(sctp_transport_);
1944 sctp_invoker_.reset(new rtc::AsyncInvoker());
1945 sctp_transport_->SignalReadyToSendData.connect(
1946 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1947 sctp_transport_->SignalDataReceived.connect(
1948 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1949 sctp_transport_->SignalStreamClosedRemotely.connect(
1950 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1951 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1952 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1953 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954}
1955
deadbeef953c2ce2017-01-09 14:53:41 -08001956void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1957 RTC_DCHECK(network_thread_->IsCurrent());
1958 RTC_DCHECK(sctp_transport_);
1959 RTC_DCHECK(sctp_transport_name_);
1960 std::string old_sctp_transport_name = *sctp_transport_name_;
1961 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08001962 cricket::DtlsTransportInternal* tc =
1963 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001964 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1965 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08001966 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001967 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1968}
1969
1970void WebRtcSession::DestroySctpTransport_n() {
1971 RTC_DCHECK(network_thread_->IsCurrent());
1972 sctp_transport_.reset(nullptr);
1973 sctp_content_name_.reset();
1974 sctp_transport_name_.reset();
1975 sctp_invoker_.reset(nullptr);
1976 sctp_ready_to_send_data_ = false;
1977}
1978
1979void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1980 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1981 RTC_DCHECK(network_thread_->IsCurrent());
1982 sctp_invoker_->AsyncInvoke<void>(
1983 RTC_FROM_HERE, signaling_thread_,
1984 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1985}
1986
1987void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1988 RTC_DCHECK(signaling_thread_->IsCurrent());
1989 sctp_ready_to_send_data_ = ready;
1990 SignalSctpReadyToSendData(ready);
1991}
1992
1993void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001994 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001995 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001996 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08001997 RTC_DCHECK(network_thread_->IsCurrent());
1998 sctp_invoker_->AsyncInvoke<void>(
1999 RTC_FROM_HERE, signaling_thread_,
2000 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2001 payload));
2002}
2003
2004void WebRtcSession::OnSctpTransportDataReceived_s(
2005 const cricket::ReceiveDataParams& params,
2006 const rtc::CopyOnWriteBuffer& payload) {
2007 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002008 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2009 // Received OPEN message; parse and signal that a new data channel should
2010 // be created.
2011 std::string label;
2012 InternalDataChannelInit config;
2013 config.id = params.ssrc;
2014 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2015 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2016 << params.ssrc;
2017 return;
2018 }
2019 config.open_handshake_role = InternalDataChannelInit::kAcker;
2020 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002021 } else {
2022 // Otherwise just forward the signal.
2023 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 }
deadbeef953c2ce2017-01-09 14:53:41 -08002025}
2026
2027void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2028 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2029 RTC_DCHECK(network_thread_->IsCurrent());
2030 sctp_invoker_->AsyncInvoke<void>(
2031 RTC_FROM_HERE, signaling_thread_,
2032 rtc::Bind(&sigslot::signal1<int>::operator(),
2033 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034}
2035
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002036// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002037bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002038 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2039 if (!bundle_enabled)
2040 return true;
2041
2042 const cricket::ContentGroup* bundle_group =
2043 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002044 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002045
2046 const cricket::ContentInfos& contents = desc->contents();
2047 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2048 citer != contents.end(); ++citer) {
2049 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002050 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002051 if (bundle_group->HasContentName(content->name) &&
2052 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2053 if (!HasRtcpMuxEnabled(content))
2054 return false;
2055 }
2056 }
2057 // RTCP-MUX is enabled in all the contents.
2058 return true;
2059}
2060
2061bool WebRtcSession::HasRtcpMuxEnabled(
2062 const cricket::ContentInfo* content) {
2063 const cricket::MediaContentDescription* description =
2064 static_cast<cricket::MediaContentDescription*>(content->description);
2065 return description->rtcp_mux();
2066}
2067
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002068bool WebRtcSession::ValidateSessionDescription(
2069 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002070 cricket::ContentSource source, std::string* err_desc) {
2071 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002072 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002073 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002074 }
2075
2076 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002077 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002078 }
2079
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002081 Action action = GetAction(sdesc->type());
2082 if (source == cricket::CS_LOCAL) {
2083 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002084 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002085 } else {
2086 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002087 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002088 }
2089
2090 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002091 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002092 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2093 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002094 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002095 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096 }
2097
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002098 // Verify ice-ufrag and ice-pwd.
2099 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002100 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002101 }
2102
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002103 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002104 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002105 }
2106
skvlad6c87a672016-05-17 17:49:52 -07002107 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2108 // m-lines that do not rtcp-mux enabled.
2109
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002110 // Verify m-lines in Answer when compared against Offer.
2111 if (action == kAnswer) {
2112 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002113 (source == cricket::CS_LOCAL) ? remote_description()->description()
2114 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002115 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002116 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002117 }
2118 }
2119
2120 return true;
2121}
2122
2123bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2124 return ((action == kOffer && state() == STATE_INIT) ||
2125 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002126 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002127 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002128 (action == kOffer && state() == STATE_INPROGRESS) ||
2129 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002130 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2131 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2132 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2133 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002134}
2135
2136bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2137 return ((action == kOffer && state() == STATE_INIT) ||
2138 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002139 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002140 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002141 (action == kOffer && state() == STATE_INPROGRESS) ||
2142 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002143 (action == kAnswer && state() == STATE_SENTOFFER) ||
2144 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2145 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2146 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002147}
2148
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149std::string WebRtcSession::GetSessionErrorMsg() {
2150 std::ostringstream desc;
2151 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2152 desc << kSessionErrorDesc << error_desc() << ".";
2153 return desc.str();
2154}
2155
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002156// We need to check the local/remote description for the Transport instead of
2157// the session, because a new Transport added during renegotiation may have
2158// them unset while the session has them set from the previous negotiation.
2159// Not doing so may trigger the auto generation of transport description and
2160// mess up DTLS identity information, ICE credential, etc.
2161bool WebRtcSession::ReadyToUseRemoteCandidate(
2162 const IceCandidateInterface* candidate,
2163 const SessionDescriptionInterface* remote_desc,
2164 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002165 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002166
2167 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002168 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002169
deadbeefd59daf82015-10-14 15:02:44 -07002170 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002171 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002172 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002173
2174 size_t mediacontent_index =
2175 static_cast<size_t>(candidate->sdp_mline_index());
2176 size_t remote_content_size =
2177 current_remote_desc->description()->contents().size();
2178 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002179 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2180 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002181
2182 *valid = false;
2183 return false;
2184 }
2185
2186 cricket::ContentInfo content =
2187 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002188
2189 const std::string transport_name = GetTransportName(content.name);
2190 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002191 return false;
2192 }
zhihuang9763d562016-08-05 11:14:50 -07002193 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002194}
2195
deadbeef7af91dd2016-12-13 11:29:11 -08002196bool WebRtcSession::SrtpRequired() const {
2197 return dtls_enabled_ ||
2198 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2199}
2200
deadbeefcbecd352015-09-23 11:50:27 -07002201void WebRtcSession::OnTransportControllerGatheringState(
2202 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002203 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002204 if (state == cricket::kIceGatheringGathering) {
2205 if (ice_observer_) {
2206 ice_observer_->OnIceGatheringChange(
2207 PeerConnectionInterface::kIceGatheringGathering);
2208 }
2209 } else if (state == cricket::kIceGatheringComplete) {
2210 if (ice_observer_) {
2211 ice_observer_->OnIceGatheringChange(
2212 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002213 }
2214 }
2215}
2216
2217void WebRtcSession::ReportTransportStats() {
2218 // Use a set so we don't report the same stats twice if two channels share
2219 // a transport.
2220 std::set<std::string> transport_names;
2221 if (voice_channel()) {
2222 transport_names.insert(voice_channel()->transport_name());
2223 }
2224 if (video_channel()) {
2225 transport_names.insert(video_channel()->transport_name());
2226 }
deadbeef953c2ce2017-01-09 14:53:41 -08002227 if (rtp_data_channel()) {
2228 transport_names.insert(rtp_data_channel()->transport_name());
2229 }
2230 if (sctp_transport_name_) {
2231 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002232 }
2233 for (const auto& name : transport_names) {
2234 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002235 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002236 ReportBestConnectionState(stats);
2237 ReportNegotiatedCiphers(stats);
2238 }
2239 }
2240}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002241// Walk through the ConnectionInfos to gather best connection usage
2242// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002243void WebRtcSession::ReportBestConnectionState(
2244 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002245 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002246 for (cricket::TransportChannelStatsList::const_iterator it =
2247 stats.channel_stats.begin();
2248 it != stats.channel_stats.end(); ++it) {
2249 for (cricket::ConnectionInfos::const_iterator it_info =
2250 it->connection_infos.begin();
2251 it_info != it->connection_infos.end(); ++it_info) {
2252 if (!it_info->best_connection) {
2253 continue;
2254 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002255
2256 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2257 const cricket::Candidate& local = it_info->local_candidate;
2258 const cricket::Candidate& remote = it_info->remote_candidate;
2259
2260 // Increment the counter for IceCandidatePairType.
2261 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2262 (local.type() == RELAY_PORT_TYPE &&
2263 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2264 type = kEnumCounterIceCandidatePairTypeTcp;
2265 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2266 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002267 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002268 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002269 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002270 metrics_observer_->IncrementEnumCounter(
2271 type, GetIceCandidatePairCounter(local, remote),
2272 kIceCandidatePairMax);
2273
2274 // Increment the counter for IP type.
2275 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002276 metrics_observer_->IncrementEnumCounter(
2277 kEnumCounterAddressFamily, kBestConnections_IPv4,
2278 kPeerConnectionAddressFamilyCounter_Max);
2279
2280 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002281 metrics_observer_->IncrementEnumCounter(
2282 kEnumCounterAddressFamily, kBestConnections_IPv6,
2283 kPeerConnectionAddressFamilyCounter_Max);
2284 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002285 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002286 }
2287
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002288 return;
2289 }
2290 }
2291}
2292
jbauchac8869e2015-07-03 01:36:14 -07002293void WebRtcSession::ReportNegotiatedCiphers(
2294 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002295 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002296 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2297 return;
2298 }
2299
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002300 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2301 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2302 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2303 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002304 return;
2305 }
2306
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002307 PeerConnectionEnumCounterType srtp_counter_type;
2308 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002309 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002310 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2311 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002312 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002313 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2314 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002315 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002316 srtp_counter_type = kEnumCounterDataSrtpCipher;
2317 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002318 } else {
2319 RTC_NOTREACHED();
2320 return;
2321 }
2322
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002323 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2324 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2325 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002326 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002327 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2328 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2329 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002330 }
2331}
2332
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002333void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002334 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002335 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002336 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002337}
2338
zhihuang9763d562016-08-05 11:14:50 -07002339const std::string WebRtcSession::GetTransportName(
2340 const std::string& content_name) {
2341 cricket::BaseChannel* channel = GetChannel(content_name);
2342 if (!channel) {
2343#ifdef HAVE_QUIC
2344 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2345 content_name == quic_data_transport_->transport_name()) {
2346 return quic_data_transport_->transport_name();
2347 }
2348#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002349 if (sctp_transport_) {
2350 RTC_DCHECK(sctp_content_name_);
2351 RTC_DCHECK(sctp_transport_name_);
2352 if (content_name == *sctp_content_name_) {
2353 return *sctp_transport_name_;
2354 }
2355 }
zhihuang9763d562016-08-05 11:14:50 -07002356 // Return an empty string if failed to retrieve the transport name.
2357 return "";
2358 }
2359 return channel->transport_name();
2360}
zhihuangd82eee02016-08-26 11:25:05 -07002361
deadbeefac22f702017-01-12 21:59:29 -08002362void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002363 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002364 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002365 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2366}
2367
2368void WebRtcSession::DestroyVideoChannel() {
2369 SignalVideoChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002370 RTC_DCHECK(video_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002371 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002372 transport_name = video_channel_->rtp_dtls_transport()->transport_name();
2373 bool need_to_delete_rtcp = (video_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002374 channel_manager_->DestroyVideoChannel(video_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002375 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002376 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002377 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002378 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002379 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002380 }
2381}
2382
2383void WebRtcSession::DestroyVoiceChannel() {
2384 SignalVoiceChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002385 RTC_DCHECK(voice_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002386 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002387 transport_name = voice_channel_->rtp_dtls_transport()->transport_name();
2388 bool need_to_delete_rtcp = (voice_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002389 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002390 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002391 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002392 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002393 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002394 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002395 }
2396}
2397
2398void WebRtcSession::DestroyDataChannel() {
2399 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002400 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002401 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002402 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2403 bool need_to_delete_rtcp =
2404 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002405 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002406 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002407 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002408 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002409 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002410 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002411 }
2412}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413} // namespace webrtc