blob: 314ba52095fffaa52ecaf5d5dd0cd7a8c696e759 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/call/audio_sink.h"
21#include "api/jsepicecandidate.h"
22#include "api/jsepsessiondescription.h"
23#include "api/peerconnectioninterface.h"
24#include "call/call.h"
25#include "media/base/mediaconstants.h"
26#include "media/sctp/sctptransportinternal.h"
27#include "p2p/base/portallocator.h"
28#include "pc/channel.h"
29#include "pc/channelmanager.h"
30#include "pc/mediasession.h"
31#include "pc/sctputils.h"
32#include "pc/webrtcsessiondescriptionfactory.h"
33#include "rtc_base/basictypes.h"
34#include "rtc_base/bind.h"
35#include "rtc_base/checks.h"
36#include "rtc_base/helpers.h"
37#include "rtc_base/logging.h"
38#include "rtc_base/stringencode.h"
39#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
zhihuang9763d562016-08-05 11:14:50 -070041#ifdef HAVE_QUIC
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "p2p/quic/quictransportchannel.h"
zhihuang9763d562016-08-05 11:14:50 -070043#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
Olga Sharonovab49b6612017-10-20 12:56:51 +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.";
Zhi Huang2a5e4262017-09-14 01:15:03 -070064const char kMlineMismatchInAnswer[] =
65 "The order of m-lines in answer doesn't match order in offer. Rejecting "
66 "answer.";
67const char kMlineMismatchInSubsequentOffer[] =
68 "The order of m-lines in subsequent offer doesn't match order from "
69 "previous offer/answer.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000070const char kPushDownTDFailed[] =
71 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000072const char kSdpWithoutDtlsFingerprint[] =
73 "Called with SDP without DTLS fingerprint.";
74const char kSdpWithoutSdesCrypto[] =
75 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000076const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000077 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080080const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000081 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080082const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000083 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070084const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070086IceCandidatePairType GetIceCandidatePairCounter(
87 const cricket::Candidate& local,
88 const cricket::Candidate& remote) {
89 const auto& l = local.type();
90 const auto& r = remote.type();
91 const auto& host = LOCAL_PORT_TYPE;
92 const auto& srflx = STUN_PORT_TYPE;
93 const auto& relay = RELAY_PORT_TYPE;
94 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070095 if (l == host && r == host) {
96 bool local_private = IPIsPrivate(local.address().ipaddr());
97 bool remote_private = IPIsPrivate(remote.address().ipaddr());
98 if (local_private) {
99 if (remote_private) {
100 return kIceCandidatePairHostPrivateHostPrivate;
101 } else {
102 return kIceCandidatePairHostPrivateHostPublic;
103 }
104 } else {
105 if (remote_private) {
106 return kIceCandidatePairHostPublicHostPrivate;
107 } else {
108 return kIceCandidatePairHostPublicHostPublic;
109 }
110 }
111 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700112 if (l == host && r == srflx)
113 return kIceCandidatePairHostSrflx;
114 if (l == host && r == relay)
115 return kIceCandidatePairHostRelay;
116 if (l == host && r == prflx)
117 return kIceCandidatePairHostPrflx;
118 if (l == srflx && r == host)
119 return kIceCandidatePairSrflxHost;
120 if (l == srflx && r == srflx)
121 return kIceCandidatePairSrflxSrflx;
122 if (l == srflx && r == relay)
123 return kIceCandidatePairSrflxRelay;
124 if (l == srflx && r == prflx)
125 return kIceCandidatePairSrflxPrflx;
126 if (l == relay && r == host)
127 return kIceCandidatePairRelayHost;
128 if (l == relay && r == srflx)
129 return kIceCandidatePairRelaySrflx;
130 if (l == relay && r == relay)
131 return kIceCandidatePairRelayRelay;
132 if (l == relay && r == prflx)
133 return kIceCandidatePairRelayPrflx;
134 if (l == prflx && r == host)
135 return kIceCandidatePairPrflxHost;
136 if (l == prflx && r == srflx)
137 return kIceCandidatePairPrflxSrflx;
138 if (l == prflx && r == relay)
139 return kIceCandidatePairPrflxRelay;
140 return kIceCandidatePairMax;
141}
142
Tommi589ae452017-10-15 21:20:46 +0000143// Verify that the order of media sections in |desc1| matches |desc2|. The
144// number of m= sections could be different.
145static bool MediaSectionsInSameOrder(const SessionDescription* desc1,
146 const SessionDescription* desc2) {
147 if (!desc1 || !desc2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 return false;
Zhi Huang2a5e4262017-09-14 01:15:03 -0700149 }
Tommi589ae452017-10-15 21:20:46 +0000150 for (size_t i = 0;
151 i < desc1->contents().size() && i < desc2->contents().size(); ++i) {
152 if ((desc2->contents()[i].name) != desc1->contents()[i].name) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 return false;
154 }
Tommi589ae452017-10-15 21:20:46 +0000155 const MediaContentDescription* desc2_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000156 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000157 desc2->contents()[i].description);
158 const MediaContentDescription* desc1_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000159 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000160 desc1->contents()[i].description);
161 if (desc2_mdesc->type() != desc1_mdesc->type()) {
wu@webrtc.org4e393072014-04-07 17:04:35 +0000162 return false;
163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 }
165 return true;
166}
167
Zhi Huang2a5e4262017-09-14 01:15:03 -0700168static bool MediaSectionsHaveSameCount(const SessionDescription* desc1,
169 const SessionDescription* desc2) {
170 if (!desc1 || !desc2) {
171 return false;
172 }
173 return desc1->contents().size() == desc2->contents().size();
174}
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800177// fingerprint, unless it's in a BUNDLE group, in which case only the
178// BUNDLE-tag section (first media section/description in the BUNDLE group)
179// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
180// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
181// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182static bool VerifyCrypto(const SessionDescription* desc,
183 bool dtls_enabled,
184 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800185 const cricket::ContentGroup* bundle =
186 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 const ContentInfos& contents = desc->contents();
188 for (size_t index = 0; index < contents.size(); ++index) {
189 const ContentInfo* cinfo = &contents[index];
190 if (cinfo->rejected) {
191 continue;
192 }
deadbeefb7892532017-02-22 19:35:18 -0800193 if (bundle && bundle->HasContentName(cinfo->name) &&
194 cinfo->name != *(bundle->FirstContentName())) {
195 // This isn't the first media section in the BUNDLE group, so it's not
196 // required to have crypto attributes, since only the crypto attributes
197 // from the first section actually get used.
198 continue;
199 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
deadbeefb7892532017-02-22 19:35:18 -0800201 // If the content isn't rejected or bundled into another m= section, crypto
202 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 const MediaContentDescription* media =
204 static_cast<const MediaContentDescription*>(cinfo->description);
205 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
206 if (!media || !tinfo) {
207 // Something is not right.
208 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 return false;
211 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000212 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000213 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000214 LOG(LS_WARNING) <<
215 "Session description must have DTLS fingerprint if DTLS enabled.";
216 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000217 return false;
218 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000219 } else {
220 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000221 LOG(LS_WARNING) <<
222 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000223 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000224 return false;
225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 }
227 }
228
229 return true;
230}
231
deadbeefb7892532017-02-22 19:35:18 -0800232// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
233// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
234// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000235static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800236 const cricket::ContentGroup* bundle =
237 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000238 const ContentInfos& contents = desc->contents();
239 for (size_t index = 0; index < contents.size(); ++index) {
240 const ContentInfo* cinfo = &contents[index];
241 if (cinfo->rejected) {
242 continue;
243 }
deadbeefb7892532017-02-22 19:35:18 -0800244 if (bundle && bundle->HasContentName(cinfo->name) &&
245 cinfo->name != *(bundle->FirstContentName())) {
246 // This isn't the first media section in the BUNDLE group, so it's not
247 // required to have ufrag/password, since only the ufrag/password from
248 // the first section actually get used.
249 continue;
250 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000251
deadbeefb7892532017-02-22 19:35:18 -0800252 // If the content isn't rejected or bundled into another m= section,
253 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000254 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
255 if (!tinfo) {
256 // Something is not right.
257 LOG(LS_ERROR) << kInvalidSdp;
258 return false;
259 }
260 if (tinfo->description.ice_ufrag.empty() ||
261 tinfo->description.ice_pwd.empty()) {
262 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
263 return false;
264 }
265 }
266 return true;
267}
268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270 uint32_t ssrc,
271 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800272 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 const cricket::ContentInfo* audio_info =
275 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000276 if (audio_info) {
277 const cricket::MediaContentDescription* audio_content =
278 static_cast<const cricket::MediaContentDescription*>(
279 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000281 const auto* found =
282 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
283 if (found) {
284 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000285 return true;
286 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 }
288
289 const cricket::ContentInfo* video_info =
290 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000291 if (video_info) {
292 const cricket::MediaContentDescription* video_content =
293 static_cast<const cricket::MediaContentDescription*>(
294 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000296 const auto* found =
297 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
298 if (found) {
299 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000300 return true;
301 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 }
303 return false;
304}
305
deadbeef953c2ce2017-01-09 14:53:41 -0800306// Get the SCTP port out of a SessionDescription.
307// Return -1 if not found.
308static int GetSctpPort(const SessionDescription* session_description) {
309 const ContentInfo* content_info = GetFirstDataContent(session_description);
310 RTC_DCHECK(content_info);
311 if (!content_info) {
312 return -1;
313 }
314 const cricket::DataContentDescription* data =
315 static_cast<const cricket::DataContentDescription*>(
316 (content_info->description));
317 std::string value;
318 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
319 cricket::kGoogleSctpDataCodecName);
320 for (const cricket::DataCodec& codec : data->codecs()) {
321 if (!codec.Matches(match_pattern)) {
322 continue;
323 }
324 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
325 return rtc::FromString<int>(value);
326 }
327 }
328 return -1;
329}
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331static bool BadSdp(const std::string& source,
332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
335 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700336 desc << "Failed to set " << source;
337 if (!type.empty()) {
338 desc << " " << type;
339 }
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000340 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return false;
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000356 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360static bool BadLocalSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
364}
365
366static bool BadRemoteSdp(const std::string& type,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
370}
371
372static bool BadOfferSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
376}
377
378static bool BadPranswerSdp(cricket::ContentSource source,
379 const std::string& reason,
380 std::string* err_desc) {
381 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
382 reason, err_desc);
383}
384
385static bool BadAnswerSdp(cricket::ContentSource source,
386 const std::string& reason,
387 std::string* err_desc) {
388 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389}
390
deadbeefd59daf82015-10-14 15:02:44 -0700391#define GET_STRING_OF_STATE(state) \
392 case webrtc::WebRtcSession::state: \
393 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 break;
395
deadbeefd59daf82015-10-14 15:02:44 -0700396static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 std::string result;
398 switch (state) {
399 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700400 GET_STRING_OF_STATE(STATE_SENTOFFER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
402 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
403 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700405 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
nissec80e7412017-01-11 05:56:46 -0800407 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 break;
409 }
410 return result;
411}
412
deadbeefd59daf82015-10-14 15:02:44 -0700413#define GET_STRING_OF_ERROR_CODE(err) \
414 case webrtc::WebRtcSession::err: \
415 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 break;
417
deadbeefd59daf82015-10-14 15:02:44 -0700418static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 std::string result;
420 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
423 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800425 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 break;
427 }
428 return result;
429}
430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431static std::string MakeErrorString(const std::string& error,
432 const std::string& desc) {
433 std::ostringstream ret;
434 ret << error << " " << desc;
435 return ret.str();
436}
437
438static std::string MakeTdErrorString(const std::string& desc) {
439 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
deadbeef0ed85b22016-02-23 17:24:52 -0800442// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
443bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
444 const SessionDescriptionInterface* new_desc,
445 const std::string& content_name) {
446 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700447 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 }
deadbeef0ed85b22016-02-23 17:24:52 -0800449 const SessionDescription* new_sd = new_desc->description();
450 const SessionDescription* old_sd = old_desc->description();
451 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
452 if (!cinfo || cinfo->rejected) {
453 return false;
454 }
455 // If the content isn't rejected, check if ufrag and password has changed.
456 const cricket::TransportDescription* new_transport_desc =
457 new_sd->GetTransportDescriptionByName(content_name);
458 const cricket::TransportDescription* old_transport_desc =
459 old_sd->GetTransportDescriptionByName(content_name);
460 if (!new_transport_desc || !old_transport_desc) {
461 // No transport description exists. This is not an ICE restart.
462 return false;
463 }
464 if (cricket::IceCredentialsChanged(
465 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
466 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
467 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
468 << ".";
469 return true;
470 }
471 return false;
472}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473
zhihuang29ff8442016-07-27 11:07:25 -0700474WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700475 Call* call,
476 cricket::ChannelManager* channel_manager,
477 const cricket::MediaConfig& media_config,
478 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700479 rtc::Thread* network_thread,
480 rtc::Thread* worker_thread,
481 rtc::Thread* signaling_thread,
482 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800483 std::unique_ptr<cricket::TransportController> transport_controller,
484 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700485 : network_thread_(network_thread),
486 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700487 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 // RFC 3264: The numeric value of the session id and version in the
489 // o line MUST be representable with a "64 bit signed integer".
490 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700491 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700492 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800493 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700494 media_config_(media_config),
495 event_log_(event_log),
496 call_(call),
497 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 ice_observer_(NULL),
499 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700500 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000502 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000504 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700505 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
506 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700507 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700508 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700509 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700510 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700511 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700512 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700513 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700514 transport_controller_->SignalCandidatesRemoved.connect(
515 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700516 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800517 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518}
519
520WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800521 RTC_DCHECK(signaling_thread()->IsCurrent());
Steve Anton169629a2017-08-30 17:36:36 -0700522 // Destroy video channels first since they may have a pointer to a voice
523 // channel.
524 for (auto* channel : video_channels_) {
525 DestroyVideoChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
Steve Anton169629a2017-08-30 17:36:36 -0700527 for (auto* channel : voice_channels_) {
528 DestroyVoiceChannel(channel);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000529 }
deadbeef953c2ce2017-01-09 14:53:41 -0800530 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800531 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800532 }
533 if (sctp_transport_) {
534 SignalDataChannelDestroyed();
535 network_thread_->Invoke<void>(
536 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
zhihuang9763d562016-08-05 11:14:50 -0700538#ifdef HAVE_QUIC
539 if (quic_data_transport_) {
540 quic_data_transport_.reset();
541 }
542#endif
deadbeefd59daf82015-10-14 15:02:44 -0700543
544 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000548 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200549 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200550 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
551 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700552 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700553 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000554
Henrik Boström87713d02015-08-25 09:53:21 +0200555 // Obtain a certificate from RTCConfiguration if any were provided (optional).
556 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
557 if (!rtc_configuration.certificates.empty()) {
558 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
559 // just picking the first one. The decision should be made based on the DTLS
560 // handshake. The DTLS negotiations need to know about all certificates.
561 certificate = rtc_configuration.certificates[0];
562 }
563
honghaiz1f429e32015-09-28 07:57:34 -0700564 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700565
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000566 if (options.disable_encryption) {
567 dtls_enabled_ = false;
568 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200569 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200570 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800571 // |rtc_configuration| can override the default |dtls_enabled_| value.
572 if (rtc_configuration.enable_dtls_srtp) {
573 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000574 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000575 }
576
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000578 // It takes precendence over the disable_sctp_data_channels
579 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800580 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700582 }
583#ifdef HAVE_QUIC
584 else if (rtc_configuration.enable_quic) {
585 // Use QUIC instead of DTLS when |enable_quic| is true.
586 data_channel_type_ = cricket::DCT_QUIC;
587 transport_controller_->use_quic();
588 if (dtls_enabled_) {
589 LOG(LS_INFO) << "Using QUIC instead of DTLS";
590 }
591 quic_data_transport_.reset(
592 new QuicDataTransport(signaling_thread(), worker_thread(),
593 network_thread(), transport_controller_.get()));
594 }
595#endif // HAVE_QUIC
596 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000597 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000598 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000599 data_channel_type_ = cricket::DCT_SCTP;
600 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
htaa2a49d92016-03-04 02:51:39 -0800603 video_options_.screencast_min_bitrate_kbps =
604 rtc_configuration.screencast_min_bitrate;
605 audio_options_.combined_audio_video_bwe =
606 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000607
kwiberg102c6a62015-10-30 02:47:38 -0700608 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100609 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200610
Karl Wibergbe579832015-11-10 22:34:18 +0100611 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
612 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200613
Henrik Boström87713d02015-08-25 09:53:21 +0200614 if (!dtls_enabled_) {
615 // Construct with DTLS disabled.
616 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200617 signaling_thread(), channel_manager_, this, id(),
618 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200619 } else {
620 // Construct with DTLS enabled.
621 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200622 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200623 signaling_thread(), channel_manager_, this, id(),
624 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200625 } else {
626 // Use the already generated certificate.
627 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200628 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200629 }
630 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000631
Henrik Boströmd8281982015-08-27 10:12:24 +0200632 webrtc_session_desc_factory_->SignalCertificateReady.connect(
633 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000634
wu@webrtc.org97077a32013-10-25 21:18:33 +0000635 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000636 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000637 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700638
jbauch5869f502017-06-29 12:31:36 -0700639 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
640 options.crypto_options.enable_encrypted_rtp_header_extensions);
641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 return true;
643}
644
deadbeefd59daf82015-10-14 15:02:44 -0700645void WebRtcSession::Close() {
646 SetState(STATE_CLOSED);
647 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700648 call_ = nullptr;
Steve Anton169629a2017-08-30 17:36:36 -0700649 RTC_DCHECK(voice_channels_.empty());
650 RTC_DCHECK(video_channels_.empty());
deadbeef953c2ce2017-01-09 14:53:41 -0800651 RTC_DCHECK(!rtp_data_channel_);
652 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653}
654
deadbeef0ed85b22016-02-23 17:24:52 -0800655cricket::BaseChannel* WebRtcSession::GetChannel(
656 const std::string& content_name) {
657 if (voice_channel() && voice_channel()->content_name() == content_name) {
658 return voice_channel();
659 }
660 if (video_channel() && video_channel()->content_name() == content_name) {
661 return video_channel();
662 }
deadbeef953c2ce2017-01-09 14:53:41 -0800663 if (rtp_data_channel() &&
664 rtp_data_channel()->content_name() == content_name) {
665 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800666 }
667 return nullptr;
668}
669
deadbeef953c2ce2017-01-09 14:53:41 -0800670bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
671 if (!local_description() || !remote_description()) {
672 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
673 << "SSL Role of the SCTP transport.";
674 return false;
675 }
676 if (!sctp_transport_) {
677 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
678 << "SSL Role of the SCTP transport.";
679 return false;
680 }
681
682 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
683}
684
685bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800686 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800687 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800688 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000689 << "SSL Role of the session.";
690 return false;
691 }
692
deadbeef953c2ce2017-01-09 14:53:41 -0800693 return transport_controller_->GetSslRole(GetTransportName(content_name),
694 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000695}
696
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000697void WebRtcSession::CreateOffer(
698 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700699 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
700 const cricket::MediaSessionOptions& session_options) {
701 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000702}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703
deadbeefab9b2d12015-10-14 11:33:11 -0700704void WebRtcSession::CreateAnswer(
705 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700706 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800707 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708}
709
Steve Anton8d3444d2017-10-20 15:30:51 -0700710bool WebRtcSession::SetLocalDescription(
711 std::unique_ptr<SessionDescriptionInterface> desc,
712 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800713 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700714
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000715 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700716 if (!ValidateSessionDescription(desc.get(), cricket::CS_LOCAL, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000717 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719
deadbeefd59daf82015-10-14 15:02:44 -0700720 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000721 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700723 initial_offerer_ = true;
724 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726
deadbeeffe4a8a42016-12-20 17:56:17 -0800727 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700728 current_local_description_ = std::move(desc);
729 pending_local_description_ = nullptr;
730 current_remote_description_ = std::move(pending_remote_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800731 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700732 pending_local_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800733 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734
735 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800736 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 // TODO(mallinath) - Handle CreateChannel failure, as new local description
738 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700739 return BadLocalSdp(local_description()->type(), kCreateChannelFailed,
740 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 }
742
deadbeefcbecd352015-09-23 11:50:27 -0700743 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800744 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000746 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return false;
748 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800749 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700750 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800751 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
deadbeef0ed85b22016-02-23 17:24:52 -0800754 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700755 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700756 return BadLocalSdp(local_description()->type(), GetSessionErrorMsg(),
757 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759 return true;
760}
761
Steve Anton8d3444d2017-10-20 15:30:51 -0700762bool WebRtcSession::SetRemoteDescription(
763 std::unique_ptr<SessionDescriptionInterface> desc,
764 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800765 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700766
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000767 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700768 if (!ValidateSessionDescription(desc.get(), cricket::CS_REMOTE, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000769 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
771
Steve Anton8d3444d2017-10-20 15:30:51 -0700772 // Hold this pointer so candidates can be copied to it later in the method.
773 SessionDescriptionInterface* desc_ptr = desc.get();
774
deadbeeffe4a8a42016-12-20 17:56:17 -0800775 const SessionDescriptionInterface* old_remote_description =
776 remote_description();
777 // Grab ownership of the description being replaced for the remainder of this
Steve Anton8d3444d2017-10-20 15:30:51 -0700778 // method, since it's used below as |old_remote_description|.
deadbeeffe4a8a42016-12-20 17:56:17 -0800779 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
780 Action action = GetAction(desc->type());
781 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700782 replaced_remote_description = pending_remote_description_
783 ? std::move(pending_remote_description_)
784 : std::move(current_remote_description_);
785 current_remote_description_ = std::move(desc);
786 pending_remote_description_ = nullptr;
787 current_local_description_ = std::move(pending_local_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800788 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700789 replaced_remote_description = std::move(pending_remote_description_);
790 pending_remote_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800791 }
deadbeefd59daf82015-10-14 15:02:44 -0700792
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 // Transport and Media channels will be created only when offer is set.
Steve Anton8d3444d2017-10-20 15:30:51 -0700794 if (action == kOffer &&
795 !CreateChannels(remote_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 // TODO(mallinath) - Handle CreateChannel failure, as new local description
797 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700798 return BadRemoteSdp(remote_description()->type(), kCreateChannelFailed,
799 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801
deadbeefcbecd352015-09-23 11:50:27 -0700802 // Remove unused channels if MediaContentDescription is rejected.
Steve Anton8d3444d2017-10-20 15:30:51 -0700803 RemoveUnusedChannels(remote_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
805 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
806 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000807 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 return false;
809 }
810
Steve Anton8d3444d2017-10-20 15:30:51 -0700811 if (local_description() &&
812 !UseCandidatesInSessionDescription(remote_description())) {
813 return BadRemoteSdp(remote_description()->type(), kInvalidCandidates,
814 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 }
816
deadbeeffe4a8a42016-12-20 17:56:17 -0800817 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800818 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800819 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800820 // Check if this new SessionDescription contains new ICE ufrag and
821 // password that indicates the remote peer requests an ICE restart.
822 // TODO(deadbeef): When we start storing both the current and pending
823 // remote description, this should reset pending_ice_restarts and compare
824 // against the current description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700825 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
deadbeeffe4a8a42016-12-20 17:56:17 -0800826 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800827 if (action == kOffer) {
828 pending_ice_restarts_.insert(content.name);
829 }
830 } else {
831 // We retain all received candidates only if ICE is not restarted.
832 // When ICE is restarted, all previous candidates belong to an old
833 // generation and should not be kept.
834 // TODO(deadbeef): This goes against the W3C spec which says the remote
835 // description should only contain candidates from the last set remote
836 // description plus any candidates added since then. We should remove
837 // this once we're sure it won't break anything.
838 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700839 old_remote_description, content.name, desc_ptr);
deadbeef0ed85b22016-02-23 17:24:52 -0800840 }
841 }
honghaiz503726c2015-07-31 12:37:38 -0700842 }
843
deadbeefd59daf82015-10-14 15:02:44 -0700844 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700845 return BadRemoteSdp(remote_description()->type(), GetSessionErrorMsg(),
846 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000848
849 // Set the the ICE connection state to connecting since the connection may
850 // become writable with peer reflexive candidates before any remote candidate
851 // is signaled.
852 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
853 // is to have a new signal the indicates a change in checking state from the
854 // transport and expose a new checking() member from transport that can be
855 // read to determine the current checking state. The existing SignalConnecting
856 // actually means "gathering candidates", so cannot be be used here.
Steve Anton8d3444d2017-10-20 15:30:51 -0700857 if (remote_description()->type() != SessionDescriptionInterface::kOffer &&
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000858 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
859 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
860 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 return true;
862}
863
Steve Anton18ee1d52017-09-11 11:32:35 -0700864// TODO(steveanton): Eventually it'd be nice to store the channels as a single
865// vector of BaseChannel pointers instead of separate voice and video channel
866// vectors. At that point, this will become a simple getter.
867std::vector<cricket::BaseChannel*> WebRtcSession::Channels() const {
868 std::vector<cricket::BaseChannel*> channels;
869 channels.insert(channels.end(), voice_channels_.begin(),
870 voice_channels_.end());
871 channels.insert(channels.end(), video_channels_.begin(),
872 video_channels_.end());
873 if (rtp_data_channel_) {
874 channels.push_back(rtp_data_channel_.get());
875 }
876 return channels;
877}
878
deadbeefd59daf82015-10-14 15:02:44 -0700879void WebRtcSession::LogState(State old_state, State new_state) {
880 LOG(LS_INFO) << "Session:" << id()
881 << " Old state:" << GetStateString(old_state)
882 << " New state:" << GetStateString(new_state);
883}
884
885void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800886 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700887 if (state != state_) {
888 LogState(state_, state);
889 state_ = state;
890 SignalState(this, state_);
891 }
892}
893
894void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800895 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700896 if (error != error_) {
897 error_ = error;
898 error_desc_ = error_desc;
899 }
900}
901
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902bool WebRtcSession::UpdateSessionState(
903 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800905 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700906
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 // If there's already a pending error then no state transition should happen.
908 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800909 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000910 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000912 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
913 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
deadbeefd59daf82015-10-14 15:02:44 -0700915 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
916 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000917 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700918 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000919 }
deadbeefd59daf82015-10-14 15:02:44 -0700920 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000921 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
923 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000924 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
925 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 }
927 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700928 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
929 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000930 if (!PushdownMediaDescription(cricket::CA_PRANSWER, 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 BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 }
936 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700937 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800938 local_description()->description()->GetGroupByName(
939 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700940 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800941 remote_description()->description()->GetGroupByName(
942 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700943 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800944 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700945 const cricket::ContentGroup* answer_bundle =
946 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
947 if (!EnableBundle(*answer_bundle)) {
948 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
949 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
950 }
951 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800952 // Only push down the transport description after enabling BUNDLE; we don't
953 // want to push down a description on a transport about to be destroyed.
954 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
955 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
956 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700958 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000959 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700960 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000961 }
deadbeefd59daf82015-10-14 15:02:44 -0700962 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000963 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 }
965 }
966 return true;
967}
968
969WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
970 if (type == SessionDescriptionInterface::kOffer) {
971 return WebRtcSession::kOffer;
972 } else if (type == SessionDescriptionInterface::kPrAnswer) {
973 return WebRtcSession::kPrAnswer;
974 } else if (type == SessionDescriptionInterface::kAnswer) {
975 return WebRtcSession::kAnswer;
976 }
nisseede5da42017-01-12 05:15:36 -0800977 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 return WebRtcSession::kOffer;
979}
980
deadbeefd59daf82015-10-14 15:02:44 -0700981bool WebRtcSession::PushdownMediaDescription(
982 cricket::ContentAction action,
983 cricket::ContentSource source,
984 std::string* err) {
Steve Anton18ee1d52017-09-11 11:32:35 -0700985 const SessionDescription* sdesc =
986 (source == cricket::CS_LOCAL ? local_description() : remote_description())
987 ->description();
988 RTC_DCHECK(sdesc);
989 bool all_success = true;
990 for (auto* channel : Channels()) {
991 // TODO(steveanton): Add support for multiple channels of the same type.
992 const ContentInfo* content_info =
993 cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type());
994 if (!content_info) {
995 continue;
deadbeefd59daf82015-10-14 15:02:44 -0700996 }
Steve Anton18ee1d52017-09-11 11:32:35 -0700997 const MediaContentDescription* content_desc =
998 static_cast<const MediaContentDescription*>(content_info->description);
999 if (content_desc && !content_info->rejected) {
1000 bool success = (source == cricket::CS_LOCAL)
1001 ? channel->SetLocalContent(content_desc, action, err)
1002 : channel->SetRemoteContent(content_desc, action, err);
1003 if (!success) {
1004 all_success = false;
1005 break;
1006 }
1007 }
1008 }
deadbeef953c2ce2017-01-09 14:53:41 -08001009 // Need complete offer/answer with an SCTP m= section before starting SCTP,
1010 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
1011 if (sctp_transport_ && local_description() && remote_description() &&
1012 cricket::GetFirstDataContent(local_description()->description()) &&
1013 cricket::GetFirstDataContent(remote_description()->description())) {
Steve Anton18ee1d52017-09-11 11:32:35 -07001014 all_success &= network_thread_->Invoke<bool>(
deadbeef953c2ce2017-01-09 14:53:41 -08001015 RTC_FROM_HERE,
1016 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
1017 }
Steve Anton18ee1d52017-09-11 11:32:35 -07001018 return all_success;
deadbeef953c2ce2017-01-09 14:53:41 -08001019}
1020
1021bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
1022 RTC_DCHECK(network_thread_->IsCurrent());
1023 RTC_DCHECK(local_description());
1024 RTC_DCHECK(remote_description());
1025 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
1026 // When we support "max-message-size", that would also be pushed down here.
1027 return sctp_transport_->Start(
1028 GetSctpPort(local_description()->description()),
1029 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -07001030}
1031
1032bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1033 cricket::ContentAction action,
1034 std::string* error_desc) {
1035 RTC_DCHECK(signaling_thread()->IsCurrent());
1036
1037 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001038 return PushdownLocalTransportDescription(local_description()->description(),
1039 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001040 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001041 return PushdownRemoteTransportDescription(remote_description()->description(),
1042 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001043}
1044
1045bool WebRtcSession::PushdownLocalTransportDescription(
1046 const SessionDescription* sdesc,
1047 cricket::ContentAction action,
1048 std::string* err) {
1049 RTC_DCHECK(signaling_thread()->IsCurrent());
1050
1051 if (!sdesc) {
1052 return false;
1053 }
1054
1055 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1056 if (!transport_controller_->SetLocalTransportDescription(
1057 tinfo.content_name, tinfo.description, action, err)) {
1058 return false;
1059 }
1060 }
1061
1062 return true;
1063}
1064
1065bool WebRtcSession::PushdownRemoteTransportDescription(
1066 const SessionDescription* sdesc,
1067 cricket::ContentAction action,
1068 std::string* err) {
1069 RTC_DCHECK(signaling_thread()->IsCurrent());
1070
1071 if (!sdesc) {
1072 return false;
1073 }
1074
1075 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1076 if (!transport_controller_->SetRemoteTransportDescription(
1077 tinfo.content_name, tinfo.description, action, err)) {
1078 return false;
1079 }
1080 }
1081
1082 return true;
1083}
1084
1085bool WebRtcSession::GetTransportDescription(
1086 const SessionDescription* description,
1087 const std::string& content_name,
1088 cricket::TransportDescription* tdesc) {
1089 if (!description || !tdesc) {
1090 return false;
1091 }
1092 const TransportInfo* transport_info =
1093 description->GetTransportInfoByName(content_name);
1094 if (!transport_info) {
1095 return false;
1096 }
1097 *tdesc = transport_info->description;
1098 return true;
1099}
1100
deadbeefcbecd352015-09-23 11:50:27 -07001101bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1102 const std::string* first_content_name = bundle.FirstContentName();
1103 if (!first_content_name) {
1104 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1105 return false;
1106 }
1107 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001108
zhihuang9763d562016-08-05 11:14:50 -07001109#ifdef HAVE_QUIC
1110 if (quic_data_transport_ &&
1111 bundle.HasContentName(quic_data_transport_->content_name()) &&
1112 quic_data_transport_->transport_name() != transport_name) {
1113 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1114 << " on " << transport_name << "with QUIC.";
1115 }
1116#endif
deadbeef953c2ce2017-01-09 14:53:41 -08001117 auto maybe_set_transport = [this, bundle,
1118 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001119 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001120 return true;
1121 }
1122
zhihuangf5b251b2017-01-12 19:37:48 -08001123 std::string old_transport_name = ch->transport_name();
1124 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001125 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1126 << " on " << transport_name << ".";
1127 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001128 }
torbjornga81a42f2015-09-23 02:16:58 -07001129
zhihuangb2cdd932017-01-19 16:54:25 -08001130 cricket::DtlsTransportInternal* rtp_dtls_transport =
1131 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001132 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001133 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1134 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001135 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001136 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001137 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1138 }
1139
zhihuangb2cdd932017-01-19 16:54:25 -08001140 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001141 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1142 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001143 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001144 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001145 // If the channel needs rtcp, it means that the channel used to have a
1146 // rtcp transport which needs to be deleted now.
1147 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001148 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001149 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001150 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001151 return true;
1152 };
1153
deadbeefcbecd352015-09-23 11:50:27 -07001154 if (!maybe_set_transport(voice_channel()) ||
1155 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001156 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001157 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001158 }
deadbeef953c2ce2017-01-09 14:53:41 -08001159 // For SCTP, transport creation/deletion happens here instead of in the
1160 // object itself.
1161 if (sctp_transport_) {
1162 RTC_DCHECK(sctp_transport_name_);
1163 RTC_DCHECK(sctp_content_name_);
1164 if (transport_name != *sctp_transport_name_ &&
1165 bundle.HasContentName(*sctp_content_name_)) {
1166 network_thread_->Invoke<void>(
1167 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1168 transport_name));
1169 }
1170 }
deadbeefcbecd352015-09-23 11:50:27 -07001171
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001172 return true;
1173}
1174
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001176 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001177 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1178 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001179 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 }
1181
1182 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001183 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 return false;
1185 }
1186
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001187 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001188 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1189 if (!valid) {
1190 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 }
1192
1193 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001194 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001195 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 return false;
1197 }
1198
deadbeefd59daf82015-10-14 15:02:44 -07001199 if (ready) {
1200 return UseCandidate(candidate);
1201 } else {
1202 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1203 return true;
1204 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205}
1206
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001207bool WebRtcSession::RemoveRemoteIceCandidates(
1208 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001209 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001210 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1211 << "removed without any remote session description.";
1212 return false;
1213 }
1214
1215 if (candidates.empty()) {
1216 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1217 return false;
1218 }
1219
deadbeeffe4a8a42016-12-20 17:56:17 -08001220 size_t number_removed =
1221 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001222 if (number_removed != candidates.size()) {
1223 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1224 << "Requested " << candidates.size() << " but only "
1225 << number_removed << " are removed.";
1226 }
1227
1228 // Remove the candidates from the transport controller.
1229 std::string error;
1230 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1231 if (!res && !error.empty()) {
1232 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1233 }
1234 return true;
1235}
1236
deadbeefd59daf82015-10-14 15:02:44 -07001237cricket::IceConfig WebRtcSession::ParseIceConfig(
1238 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001239 cricket::ContinualGatheringPolicy gathering_policy;
1240 // TODO(honghaiz): Add the third continual gathering policy in
1241 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1242 switch (config.continual_gathering_policy) {
1243 case PeerConnectionInterface::GATHER_ONCE:
1244 gathering_policy = cricket::GATHER_ONCE;
1245 break;
1246 case PeerConnectionInterface::GATHER_CONTINUALLY:
1247 gathering_policy = cricket::GATHER_CONTINUALLY;
1248 break;
1249 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001250 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001251 gathering_policy = cricket::GATHER_ONCE;
1252 }
deadbeefd59daf82015-10-14 15:02:44 -07001253 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001254 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001255 ice_config.prioritize_most_likely_candidate_pairs =
1256 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001257 ice_config.backup_connection_ping_interval =
1258 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001259 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001260 ice_config.presume_writable_when_fully_relayed =
1261 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001262 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001263 ice_config.regather_all_networks_interval_range =
1264 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001265 return ice_config;
1266}
1267
1268void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1269 transport_controller_->SetIceConfig(config);
1270}
1271
1272void WebRtcSession::MaybeStartGathering() {
1273 transport_controller_->MaybeStartGathering();
1274}
1275
Peter Boström0c4e06b2015-10-07 12:23:21 +02001276bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1277 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001278 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001280 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001281 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1282 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283}
1284
Peter Boström0c4e06b2015-10-07 12:23:21 +02001285bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1286 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001287 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001288 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001289 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001290 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1291 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292}
1293
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001294std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001296 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 return desc.str();
1298}
1299
wu@webrtc.org78187522013-10-07 23:32:02 +00001300bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001301 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001302 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001303 if (!rtp_data_channel_ && !sctp_transport_) {
1304 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1305 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001306 return false;
1307 }
deadbeef953c2ce2017-01-09 14:53:41 -08001308 return rtp_data_channel_
1309 ? rtp_data_channel_->SendData(params, payload, result)
1310 : network_thread_->Invoke<bool>(
1311 RTC_FROM_HERE,
1312 Bind(&cricket::SctpTransportInternal::SendData,
1313 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001314}
1315
1316bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001317 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001318 // Don't log an error here, because DataChannels are expected to call
1319 // ConnectDataChannel in this state. It's the only way to initially tell
1320 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001321 return false;
1322 }
deadbeef953c2ce2017-01-09 14:53:41 -08001323 if (rtp_data_channel_) {
1324 rtp_data_channel_->SignalReadyToSendData.connect(
1325 webrtc_data_channel, &DataChannel::OnChannelReady);
1326 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1327 &DataChannel::OnDataReceived);
1328 } else {
1329 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1330 &DataChannel::OnChannelReady);
1331 SignalSctpDataReceived.connect(webrtc_data_channel,
1332 &DataChannel::OnDataReceived);
1333 SignalSctpStreamClosedRemotely.connect(
1334 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1335 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001336 return true;
1337}
1338
1339void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001340 if (!rtp_data_channel_ && !sctp_transport_) {
1341 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1342 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001343 return;
1344 }
deadbeef953c2ce2017-01-09 14:53:41 -08001345 if (rtp_data_channel_) {
1346 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1347 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1348 } else {
1349 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1350 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1351 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1352 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001353}
1354
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001355void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001356 if (!sctp_transport_) {
1357 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001358 return;
1359 }
deadbeef953c2ce2017-01-09 14:53:41 -08001360 network_thread_->Invoke<void>(
1361 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1362 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001363}
1364
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001365void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001366 if (!sctp_transport_) {
1367 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001368 << "NULL.";
1369 return;
1370 }
deadbeef953c2ce2017-01-09 14:53:41 -08001371 network_thread_->Invoke<void>(
1372 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1373 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001374}
1375
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001376bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001377 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1378 sctp_ready_to_send_data_;
1379}
1380
1381std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001382 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001383 ChannelNamePairs channel_name_pairs;
1384 if (voice_channel()) {
1385 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1386 voice_channel()->content_name(), voice_channel()->transport_name()));
1387 }
1388 if (video_channel()) {
1389 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1390 video_channel()->content_name(), video_channel()->transport_name()));
1391 }
1392 if (rtp_data_channel()) {
1393 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1394 ChannelNamePair(rtp_data_channel()->content_name(),
1395 rtp_data_channel()->transport_name()));
1396 }
1397 if (sctp_transport_) {
1398 RTC_DCHECK(sctp_content_name_);
1399 RTC_DCHECK(sctp_transport_name_);
1400 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1401 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1402 }
1403 return GetStats(channel_name_pairs);
1404}
1405
1406std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1407 const ChannelNamePairs& channel_name_pairs) {
1408 if (network_thread()->IsCurrent()) {
1409 return GetStats_n(channel_name_pairs);
1410 }
1411 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1412 RTC_FROM_HERE,
1413 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1414}
1415
1416bool WebRtcSession::GetLocalCertificate(
1417 const std::string& transport_name,
1418 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1419 return transport_controller_->GetLocalCertificate(transport_name,
1420 certificate);
1421}
1422
1423std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1424 const std::string& transport_name) {
1425 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001426}
1427
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428cricket::DataChannelType WebRtcSession::data_channel_type() const {
1429 return data_channel_type_;
1430}
1431
deadbeef0ed85b22016-02-23 17:24:52 -08001432bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1433 return pending_ice_restarts_.find(content_name) !=
1434 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001435}
1436
deadbeefd1a38b52016-12-10 13:15:33 -08001437void WebRtcSession::SetNeedsIceRestartFlag() {
1438 transport_controller_->SetNeedsIceRestartFlag();
1439}
1440
1441bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1442 return transport_controller_->NeedsIceRestart(content_name);
1443}
1444
Henrik Boströmd8281982015-08-27 10:12:24 +02001445void WebRtcSession::OnCertificateReady(
1446 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001447 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001448}
1449
deadbeef953c2ce2017-01-09 14:53:41 -08001450void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1451 SetError(ERROR_TRANSPORT,
1452 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1453}
1454
Henrik Boströmd8281982015-08-27 10:12:24 +02001455bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001456 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001457}
1458
deadbeefcbecd352015-09-23 11:50:27 -07001459const rtc::scoped_refptr<rtc::RTCCertificate>&
1460WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001461 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001462}
1463
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464void WebRtcSession::SetIceConnectionState(
1465 PeerConnectionInterface::IceConnectionState state) {
1466 if (ice_connection_state_ == state) {
1467 return;
1468 }
1469
deadbeefcbecd352015-09-23 11:50:27 -07001470 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1471 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001472 RTC_DCHECK(ice_connection_state_ !=
1473 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 ice_connection_state_ = state;
1475 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001476 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 }
1478}
1479
deadbeefcbecd352015-09-23 11:50:27 -07001480void WebRtcSession::OnTransportControllerConnectionState(
1481 cricket::IceConnectionState state) {
1482 switch (state) {
1483 case cricket::kIceConnectionConnecting:
1484 // If the current state is Connected or Completed, then there were
1485 // writable channels but now there are not, so the next state must
1486 // be Disconnected.
1487 // kIceConnectionConnecting is currently used as the default,
1488 // un-connected state by the TransportController, so its only use is
1489 // detecting disconnections.
1490 if (ice_connection_state_ ==
1491 PeerConnectionInterface::kIceConnectionConnected ||
1492 ice_connection_state_ ==
1493 PeerConnectionInterface::kIceConnectionCompleted) {
1494 SetIceConnectionState(
1495 PeerConnectionInterface::kIceConnectionDisconnected);
1496 }
torbjornga81a42f2015-09-23 02:16:58 -07001497 break;
deadbeefcbecd352015-09-23 11:50:27 -07001498 case cricket::kIceConnectionFailed:
1499 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1500 break;
1501 case cricket::kIceConnectionConnected:
1502 LOG(LS_INFO) << "Changing to ICE connected state because "
1503 << "all transports are writable.";
1504 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1505 break;
1506 case cricket::kIceConnectionCompleted:
1507 LOG(LS_INFO) << "Changing to ICE completed state because "
1508 << "all transports are complete.";
1509 if (ice_connection_state_ !=
1510 PeerConnectionInterface::kIceConnectionConnected) {
1511 // If jumping directly from "checking" to "connected",
1512 // signal "connected" first.
1513 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1514 }
1515 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1516 if (metrics_observer_) {
1517 ReportTransportStats();
1518 }
1519 break;
1520 default:
nissec80e7412017-01-11 05:56:46 -08001521 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001522 }
deadbeefcbecd352015-09-23 11:50:27 -07001523}
1524
1525void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001526 SetIceConnectionReceiving(receiving);
1527}
1528
1529void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1530 if (ice_connection_receiving_ == receiving) {
1531 return;
1532 }
1533 ice_connection_receiving_ = receiving;
1534 if (ice_observer_) {
1535 ice_observer_->OnIceConnectionReceivingChange(receiving);
1536 }
1537}
1538
deadbeefcbecd352015-09-23 11:50:27 -07001539void WebRtcSession::OnTransportControllerCandidatesGathered(
1540 const std::string& transport_name,
1541 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001542 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001543 int sdp_mline_index;
1544 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1545 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1546 << transport_name << " not found";
1547 return;
1548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549
deadbeefcbecd352015-09-23 11:50:27 -07001550 for (cricket::Candidates::const_iterator citer = candidates.begin();
1551 citer != candidates.end(); ++citer) {
1552 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001553 std::unique_ptr<JsepIceCandidate> candidate(
1554 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001555 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001556 mutable_local_description()->AddCandidate(candidate.get());
1557 }
1558 if (ice_observer_) {
1559 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 }
1562}
1563
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001564void WebRtcSession::OnTransportControllerCandidatesRemoved(
1565 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001566 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001567 // Sanity check.
1568 for (const cricket::Candidate& candidate : candidates) {
1569 if (candidate.transport_name().empty()) {
1570 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1571 << "empty content name in candidate "
1572 << candidate.ToString();
1573 return;
1574 }
1575 }
1576
deadbeeffe4a8a42016-12-20 17:56:17 -08001577 if (local_description()) {
1578 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001579 }
1580 if (ice_observer_) {
1581 ice_observer_->OnIceCandidatesRemoved(candidates);
1582 }
1583}
1584
deadbeef953c2ce2017-01-09 14:53:41 -08001585void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1586 rtc::SSLHandshakeError error) {
1587 if (metrics_observer_) {
1588 metrics_observer_->IncrementEnumCounter(
1589 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1590 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1591 }
1592}
1593
Steve Anton169629a2017-08-30 17:36:36 -07001594// Enabling voice and video (and RTP data) channels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595void WebRtcSession::EnableChannels() {
Steve Anton169629a2017-08-30 17:36:36 -07001596 for (cricket::VoiceChannel* voice_channel : voice_channels_) {
1597 if (!voice_channel->enabled()) {
1598 voice_channel->Enable(true);
1599 }
1600 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
Steve Anton169629a2017-08-30 17:36:36 -07001602 for (cricket::VideoChannel* video_channel : video_channels_) {
1603 if (!video_channel->enabled()) {
1604 video_channel->Enable(true);
1605 }
1606 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607
deadbeef953c2ce2017-01-09 14:53:41 -08001608 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1609 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610}
1611
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612// Returns the media index for a local ice candidate given the content name.
1613bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1614 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001615 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001617 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618
1619 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001620 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 for (size_t index = 0; index < contents.size(); ++index) {
1622 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001623 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 content_found = true;
1625 break;
1626 }
1627 }
1628 return content_found;
1629}
1630
1631bool WebRtcSession::UseCandidatesInSessionDescription(
1632 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001633 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001635 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001637
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1639 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001640 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001641 const IceCandidateInterface* candidate = candidates->at(n);
1642 bool valid = false;
1643 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1644 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001645 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1646 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001647 }
1648 continue;
1649 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001650 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001651 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 break;
deadbeefd59daf82015-10-14 15:02:44 -07001653 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 }
1655 }
1656 return ret;
1657}
1658
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001659bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001661 size_t remote_content_size =
1662 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001664 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 return false;
1666 }
1667
1668 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001669 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 std::vector<cricket::Candidate> candidates;
1671 candidates.push_back(candidate->candidate());
1672 // Invoking BaseSession method to handle remote candidates.
1673 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001674 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1675 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 // Candidates successfully submitted for checking.
1677 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1678 ice_connection_state_ ==
1679 PeerConnectionInterface::kIceConnectionDisconnected) {
1680 // If state is New, then the session has just gotten its first remote ICE
1681 // candidates, so go to Checking.
1682 // If state is Disconnected, the session is re-using old candidates or
1683 // receiving additional ones, so go to Checking.
1684 // If state is Connected, stay Connected.
1685 // TODO(bemasc): If state is Connected, and the new candidates are for a
1686 // newly added transport, then the state actually _should_ move to
1687 // checking. Add a way to distinguish that case.
1688 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1689 }
1690 // TODO(bemasc): If state is Completed, go back to Connected.
1691 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001692 if (!error.empty()) {
1693 LOG(LS_WARNING) << error;
1694 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 }
1696 return true;
1697}
1698
deadbeefcbecd352015-09-23 11:50:27 -07001699void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001700 // TODO(steveanton): Add support for multiple audio/video channels.
1701 // Destroy video channel first since it may have a pointer to the
1702 // voice channel.
1703 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
1704 if ((!video_info || video_info->rejected) && video_channel()) {
1705 RemoveAndDestroyVideoChannel(video_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 }
1707
Steve Anton169629a2017-08-30 17:36:36 -07001708 const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc);
1709 if ((!voice_info || voice_info->rejected) && voice_channel()) {
1710 RemoveAndDestroyVoiceChannel(voice_channel());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001711 }
1712
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 const cricket::ContentInfo* data_info =
1714 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001715 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001716 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001717 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001718 }
1719 if (sctp_transport_) {
1720 SignalDataChannelDestroyed();
1721 network_thread_->Invoke<void>(
1722 RTC_FROM_HERE,
1723 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001724 }
1725#ifdef HAVE_QUIC
1726 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1727 if (quic_data_transport_) {
1728 quic_data_transport_.reset();
1729 }
1730#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 }
1732}
1733
skvlad6c87a672016-05-17 17:49:52 -07001734// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1735// if the channel is not part of any bundle.
1736const std::string* WebRtcSession::GetBundleTransportName(
1737 const cricket::ContentInfo* content,
1738 const cricket::ContentGroup* bundle) {
1739 if (!bundle) {
1740 return nullptr;
1741 }
1742 const std::string* first_content_name = bundle->FirstContentName();
1743 if (!first_content_name) {
1744 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1745 return nullptr;
1746 }
1747 if (!bundle->HasContentName(content->name)) {
1748 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1749 return nullptr;
1750 }
1751 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1752 return first_content_name;
1753}
1754
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001756 // TODO(steveanton): Add support for multiple audio/video channels.
skvlad6c87a672016-05-17 17:49:52 -07001757 const cricket::ContentGroup* bundle_group = nullptr;
1758 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1759 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1760 if (!bundle_group) {
1761 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1762 return false;
1763 }
1764 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // Creating the media channels and transport proxies.
1766 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001767 if (voice && !voice->rejected && !voice_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001768 if (!CreateVoiceChannel(voice,
1769 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 LOG(LS_ERROR) << "Failed to create voice channel.";
1771 return false;
1772 }
1773 }
1774
1775 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001776 if (video && !video->rejected && !video_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001777 if (!CreateVideoChannel(video,
1778 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 LOG(LS_ERROR) << "Failed to create video channel.";
1780 return false;
1781 }
1782 }
1783
1784 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001785 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1786 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001787 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 LOG(LS_ERROR) << "Failed to create data channel.";
1789 return false;
1790 }
1791 }
1792
1793 return true;
1794}
1795
skvlad6c87a672016-05-17 17:49:52 -07001796bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1797 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001798 // TODO(steveanton): Check to see if it's safe to create multiple voice
1799 // channels.
1800 RTC_DCHECK(voice_channels_.empty());
1801
skvlad6c87a672016-05-17 17:49:52 -07001802 bool require_rtcp_mux =
1803 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001804
1805 std::string transport_name =
1806 bundle_transport ? *bundle_transport : content->name;
1807
zhihuangb2cdd932017-01-19 16:54:25 -08001808 cricket::DtlsTransportInternal* rtp_dtls_transport =
1809 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001810 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001811 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001812 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001813 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001814 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1815 }
1816
Steve Anton169629a2017-08-30 17:36:36 -07001817 cricket::VoiceChannel* voice_channel = channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001818 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001819 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001820 audio_options_);
1821 if (!voice_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001822 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001823 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001824 if (rtcp_dtls_transport) {
1825 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001826 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001827 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001828 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001829 }
zhihuangf5b251b2017-01-12 19:37:48 -08001830
Steve Anton169629a2017-08-30 17:36:36 -07001831 voice_channels_.push_back(voice_channel);
1832
1833 voice_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001834 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001835 voice_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001836 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001837
Steve Anton169629a2017-08-30 17:36:36 -07001838 // TODO(steveanton): This should signal which voice channel was created since
1839 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001840 SignalVoiceChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001841 voice_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001842 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843}
1844
skvlad6c87a672016-05-17 17:49:52 -07001845bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1846 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001847 // TODO(steveanton): Check to see if it's safe to create multiple video
1848 // channels.
1849 RTC_DCHECK(video_channels_.empty());
1850
skvlad6c87a672016-05-17 17:49:52 -07001851 bool require_rtcp_mux =
1852 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001853
1854 std::string transport_name =
1855 bundle_transport ? *bundle_transport : content->name;
1856
zhihuangb2cdd932017-01-19 16:54:25 -08001857 cricket::DtlsTransportInternal* rtp_dtls_transport =
1858 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001859 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001860 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001861 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001862 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001863 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1864 }
1865
Steve Anton169629a2017-08-30 17:36:36 -07001866 cricket::VideoChannel* video_channel = channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001867 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001868 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001869 video_options_);
zhihuangf5b251b2017-01-12 19:37:48 -08001870
Steve Anton169629a2017-08-30 17:36:36 -07001871 if (!video_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001872 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001873 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001874 if (rtcp_dtls_transport) {
1875 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001876 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001877 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001878 return false;
1879 }
zhihuangf5b251b2017-01-12 19:37:48 -08001880
Steve Anton169629a2017-08-30 17:36:36 -07001881 video_channels_.push_back(video_channel);
1882
1883 video_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001884 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001885 video_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001886 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001887
Steve Anton169629a2017-08-30 17:36:36 -07001888 // TODO(steveanton): This should signal which video channel was created since
1889 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001890 SignalVideoChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001891 video_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001892 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893}
1894
skvlad6c87a672016-05-17 17:49:52 -07001895bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1896 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001897 const std::string transport_name =
1898 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001899#ifdef HAVE_QUIC
1900 if (data_channel_type_ == cricket::DCT_QUIC) {
1901 RTC_DCHECK(transport_controller_->quic());
zhihuangb2cdd932017-01-19 16:54:25 -08001902 quic_data_transport_->SetTransports(transport_name);
zhihuang9763d562016-08-05 11:14:50 -07001903 return true;
1904 }
1905#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001906 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001907 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001908 if (!sctp_factory_) {
1909 LOG(LS_ERROR)
1910 << "Trying to create SCTP transport, but didn't compile with "
1911 "SCTP support (HAVE_SCTP)";
1912 return false;
1913 }
1914 if (!network_thread_->Invoke<bool>(
1915 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1916 this, content->name, transport_name))) {
1917 return false;
1918 };
1919 } else {
1920 bool require_rtcp_mux =
1921 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001922
1923 std::string transport_name =
1924 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001925 cricket::DtlsTransportInternal* rtp_dtls_transport =
1926 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001927 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001928 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001929 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001930 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001931 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1932 }
1933
deadbeef953c2ce2017-01-09 14:53:41 -08001934 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001935 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001936 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001937 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001938
deadbeef953c2ce2017-01-09 14:53:41 -08001939 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001940 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001941 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001942 if (rtcp_dtls_transport) {
1943 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001944 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001945 }
deadbeef953c2ce2017-01-09 14:53:41 -08001946 return false;
1947 }
zhihuangf5b251b2017-01-12 19:37:48 -08001948
deadbeefac22f702017-01-12 21:59:29 -08001949 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1950 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001951 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1952 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1953 rtp_data_channel_->SignalSentPacket.connect(this,
1954 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001955 }
1956
deadbeefab9b2d12015-10-14 11:33:11 -07001957 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001958
wu@webrtc.org91053e72013-08-10 07:18:04 +00001959 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960}
1961
stefanf79ade12017-06-02 06:44:03 -07001962Call::Stats WebRtcSession::GetCallStats() {
1963 if (!worker_thread()->IsCurrent()) {
1964 return worker_thread()->Invoke<Call::Stats>(
1965 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1966 }
zhihuang38ede132017-06-15 12:52:32 -07001967 if (!call_)
1968 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001969 return call_->GetStats();
1970}
1971
hbosdf6075a2016-12-19 04:58:02 -08001972std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1973 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001974 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001975 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1976 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1977 &channel_name_pairs.video,
1978 &channel_name_pairs.data }) {
1979 if (*channel_name_pair) {
1980 cricket::TransportStats transport_stats;
1981 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1982 &transport_stats)) {
1983 return nullptr;
1984 }
1985 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1986 (*channel_name_pair)->transport_name;
1987 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1988 std::move(transport_stats);
1989 }
1990 }
1991 return session_stats;
1992}
1993
deadbeef953c2ce2017-01-09 14:53:41 -08001994bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1995 const std::string& transport_name) {
1996 RTC_DCHECK(network_thread_->IsCurrent());
1997 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001998 cricket::DtlsTransportInternal* tc =
1999 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002000 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2001 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
2002 RTC_DCHECK(sctp_transport_);
2003 sctp_invoker_.reset(new rtc::AsyncInvoker());
2004 sctp_transport_->SignalReadyToSendData.connect(
2005 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
2006 sctp_transport_->SignalDataReceived.connect(
2007 this, &WebRtcSession::OnSctpTransportDataReceived_n);
2008 sctp_transport_->SignalStreamClosedRemotely.connect(
2009 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
2010 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
2011 sctp_content_name_ = rtc::Optional<std::string>(content_name);
2012 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013}
2014
deadbeef953c2ce2017-01-09 14:53:41 -08002015void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
2016 RTC_DCHECK(network_thread_->IsCurrent());
2017 RTC_DCHECK(sctp_transport_);
2018 RTC_DCHECK(sctp_transport_name_);
2019 std::string old_sctp_transport_name = *sctp_transport_name_;
2020 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08002021 cricket::DtlsTransportInternal* tc =
2022 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002023 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2024 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08002025 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08002026 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
2027}
2028
2029void WebRtcSession::DestroySctpTransport_n() {
2030 RTC_DCHECK(network_thread_->IsCurrent());
2031 sctp_transport_.reset(nullptr);
2032 sctp_content_name_.reset();
2033 sctp_transport_name_.reset();
2034 sctp_invoker_.reset(nullptr);
2035 sctp_ready_to_send_data_ = false;
2036}
2037
2038void WebRtcSession::OnSctpTransportReadyToSendData_n() {
2039 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2040 RTC_DCHECK(network_thread_->IsCurrent());
2041 sctp_invoker_->AsyncInvoke<void>(
2042 RTC_FROM_HERE, signaling_thread_,
2043 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
2044}
2045
2046void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
2047 RTC_DCHECK(signaling_thread_->IsCurrent());
2048 sctp_ready_to_send_data_ = ready;
2049 SignalSctpReadyToSendData(ready);
2050}
2051
2052void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002053 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002054 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07002055 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08002056 RTC_DCHECK(network_thread_->IsCurrent());
2057 sctp_invoker_->AsyncInvoke<void>(
2058 RTC_FROM_HERE, signaling_thread_,
2059 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2060 payload));
2061}
2062
2063void WebRtcSession::OnSctpTransportDataReceived_s(
2064 const cricket::ReceiveDataParams& params,
2065 const rtc::CopyOnWriteBuffer& payload) {
2066 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002067 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2068 // Received OPEN message; parse and signal that a new data channel should
2069 // be created.
2070 std::string label;
2071 InternalDataChannelInit config;
2072 config.id = params.ssrc;
2073 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2074 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2075 << params.ssrc;
2076 return;
2077 }
2078 config.open_handshake_role = InternalDataChannelInit::kAcker;
2079 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002080 } else {
2081 // Otherwise just forward the signal.
2082 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 }
deadbeef953c2ce2017-01-09 14:53:41 -08002084}
2085
2086void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
2087 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
2088 RTC_DCHECK(network_thread_->IsCurrent());
2089 sctp_invoker_->AsyncInvoke<void>(
2090 RTC_FROM_HERE, signaling_thread_,
2091 rtc::Bind(&sigslot::signal1<int>::operator(),
2092 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093}
2094
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002095// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002097 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2098 if (!bundle_enabled)
2099 return true;
2100
2101 const cricket::ContentGroup* bundle_group =
2102 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002103 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002104
2105 const cricket::ContentInfos& contents = desc->contents();
2106 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2107 citer != contents.end(); ++citer) {
2108 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002109 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002110 if (bundle_group->HasContentName(content->name) &&
2111 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2112 if (!HasRtcpMuxEnabled(content))
2113 return false;
2114 }
2115 }
2116 // RTCP-MUX is enabled in all the contents.
2117 return true;
2118}
2119
2120bool WebRtcSession::HasRtcpMuxEnabled(
2121 const cricket::ContentInfo* content) {
2122 const cricket::MediaContentDescription* description =
2123 static_cast<cricket::MediaContentDescription*>(content->description);
2124 return description->rtcp_mux();
2125}
2126
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002127bool WebRtcSession::ValidateSessionDescription(
2128 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002129 cricket::ContentSource source, std::string* err_desc) {
2130 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002131 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002132 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002133 }
2134
2135 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002136 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002137 }
2138
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002139 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002140 Action action = GetAction(sdesc->type());
2141 if (source == cricket::CS_LOCAL) {
2142 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002143 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002144 } else {
2145 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002146 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002147 }
2148
2149 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002150 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002151 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2152 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002153 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002154 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002155 }
2156
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002157 // Verify ice-ufrag and ice-pwd.
2158 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002160 }
2161
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002162 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002163 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002164 }
2165
skvlad6c87a672016-05-17 17:49:52 -07002166 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2167 // m-lines that do not rtcp-mux enabled.
2168
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002169 // Verify m-lines in Answer when compared against Offer.
Zhi Huang2a5e4262017-09-14 01:15:03 -07002170 if (action == kAnswer || action == kPrAnswer) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002171 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002172 (source == cricket::CS_LOCAL) ? remote_description()->description()
2173 : local_description()->description();
Tommi589ae452017-10-15 21:20:46 +00002174 if (!MediaSectionsHaveSameCount(sdesc->description(), offer_desc) ||
2175 !MediaSectionsInSameOrder(sdesc->description(), offer_desc)) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002176 return BadAnswerSdp(source, kMlineMismatchInAnswer, err_desc);
2177 }
2178 } else {
Tommi589ae452017-10-15 21:20:46 +00002179 // The re-offers should respect the order of m= sections in current local
Zhi Huang2a5e4262017-09-14 01:15:03 -07002180 // description. See RFC3264 Section 8 paragraph 4 for more details.
Tommi589ae452017-10-15 21:20:46 +00002181 if (local_description() &&
2182 !MediaSectionsInSameOrder(sdesc->description(),
2183 local_description()->description())) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002184 return BadOfferSdp(source, kMlineMismatchInSubsequentOffer, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002185 }
2186 }
2187
2188 return true;
2189}
2190
2191bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2192 return ((action == kOffer && state() == STATE_INIT) ||
2193 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002194 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002195 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002196 (action == kOffer && state() == STATE_INPROGRESS) ||
2197 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002198 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2199 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2200 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2201 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002202}
2203
2204bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2205 return ((action == kOffer && state() == STATE_INIT) ||
2206 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002207 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002208 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002209 (action == kOffer && state() == STATE_INPROGRESS) ||
2210 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002211 (action == kAnswer && state() == STATE_SENTOFFER) ||
2212 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2213 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2214 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002215}
2216
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002217std::string WebRtcSession::GetSessionErrorMsg() {
2218 std::ostringstream desc;
2219 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2220 desc << kSessionErrorDesc << error_desc() << ".";
2221 return desc.str();
2222}
2223
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002224// We need to check the local/remote description for the Transport instead of
2225// the session, because a new Transport added during renegotiation may have
2226// them unset while the session has them set from the previous negotiation.
2227// Not doing so may trigger the auto generation of transport description and
2228// mess up DTLS identity information, ICE credential, etc.
2229bool WebRtcSession::ReadyToUseRemoteCandidate(
2230 const IceCandidateInterface* candidate,
2231 const SessionDescriptionInterface* remote_desc,
2232 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002233 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002234
2235 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002236 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002237
deadbeefd59daf82015-10-14 15:02:44 -07002238 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002239 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002240 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002241
2242 size_t mediacontent_index =
2243 static_cast<size_t>(candidate->sdp_mline_index());
2244 size_t remote_content_size =
2245 current_remote_desc->description()->contents().size();
2246 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002247 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2248 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002249
2250 *valid = false;
2251 return false;
2252 }
2253
2254 cricket::ContentInfo content =
2255 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002256
2257 const std::string transport_name = GetTransportName(content.name);
2258 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002259 return false;
2260 }
zhihuang9763d562016-08-05 11:14:50 -07002261 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002262}
2263
deadbeef7af91dd2016-12-13 11:29:11 -08002264bool WebRtcSession::SrtpRequired() const {
2265 return dtls_enabled_ ||
2266 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2267}
2268
deadbeefcbecd352015-09-23 11:50:27 -07002269void WebRtcSession::OnTransportControllerGatheringState(
2270 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002271 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002272 if (state == cricket::kIceGatheringGathering) {
2273 if (ice_observer_) {
2274 ice_observer_->OnIceGatheringChange(
2275 PeerConnectionInterface::kIceGatheringGathering);
2276 }
2277 } else if (state == cricket::kIceGatheringComplete) {
2278 if (ice_observer_) {
2279 ice_observer_->OnIceGatheringChange(
2280 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002281 }
2282 }
2283}
2284
2285void WebRtcSession::ReportTransportStats() {
2286 // Use a set so we don't report the same stats twice if two channels share
2287 // a transport.
2288 std::set<std::string> transport_names;
2289 if (voice_channel()) {
2290 transport_names.insert(voice_channel()->transport_name());
2291 }
2292 if (video_channel()) {
2293 transport_names.insert(video_channel()->transport_name());
2294 }
deadbeef953c2ce2017-01-09 14:53:41 -08002295 if (rtp_data_channel()) {
2296 transport_names.insert(rtp_data_channel()->transport_name());
2297 }
2298 if (sctp_transport_name_) {
2299 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002300 }
2301 for (const auto& name : transport_names) {
2302 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002303 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002304 ReportBestConnectionState(stats);
2305 ReportNegotiatedCiphers(stats);
2306 }
2307 }
2308}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002309// Walk through the ConnectionInfos to gather best connection usage
2310// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002311void WebRtcSession::ReportBestConnectionState(
2312 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002313 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002314 for (cricket::TransportChannelStatsList::const_iterator it =
2315 stats.channel_stats.begin();
2316 it != stats.channel_stats.end(); ++it) {
2317 for (cricket::ConnectionInfos::const_iterator it_info =
2318 it->connection_infos.begin();
2319 it_info != it->connection_infos.end(); ++it_info) {
2320 if (!it_info->best_connection) {
2321 continue;
2322 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002323
2324 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2325 const cricket::Candidate& local = it_info->local_candidate;
2326 const cricket::Candidate& remote = it_info->remote_candidate;
2327
2328 // Increment the counter for IceCandidatePairType.
2329 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2330 (local.type() == RELAY_PORT_TYPE &&
2331 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2332 type = kEnumCounterIceCandidatePairTypeTcp;
2333 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2334 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002335 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002336 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002337 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002338 metrics_observer_->IncrementEnumCounter(
2339 type, GetIceCandidatePairCounter(local, remote),
2340 kIceCandidatePairMax);
2341
2342 // Increment the counter for IP type.
2343 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002344 metrics_observer_->IncrementEnumCounter(
2345 kEnumCounterAddressFamily, kBestConnections_IPv4,
2346 kPeerConnectionAddressFamilyCounter_Max);
2347
2348 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002349 metrics_observer_->IncrementEnumCounter(
2350 kEnumCounterAddressFamily, kBestConnections_IPv6,
2351 kPeerConnectionAddressFamilyCounter_Max);
2352 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002353 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002354 }
2355
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002356 return;
2357 }
2358 }
2359}
2360
jbauchac8869e2015-07-03 01:36:14 -07002361void WebRtcSession::ReportNegotiatedCiphers(
2362 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002363 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002364 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2365 return;
2366 }
2367
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002368 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2369 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2370 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2371 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002372 return;
2373 }
2374
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002375 PeerConnectionEnumCounterType srtp_counter_type;
2376 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002377 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002378 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2379 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002380 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002381 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2382 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002383 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002384 srtp_counter_type = kEnumCounterDataSrtpCipher;
2385 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002386 } else {
2387 RTC_NOTREACHED();
2388 return;
2389 }
2390
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002391 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2392 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2393 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002394 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002395 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2396 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2397 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002398 }
2399}
2400
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002401void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002402 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002403 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002404 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002405}
2406
zhihuang9763d562016-08-05 11:14:50 -07002407const std::string WebRtcSession::GetTransportName(
2408 const std::string& content_name) {
2409 cricket::BaseChannel* channel = GetChannel(content_name);
2410 if (!channel) {
2411#ifdef HAVE_QUIC
2412 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2413 content_name == quic_data_transport_->transport_name()) {
2414 return quic_data_transport_->transport_name();
2415 }
2416#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002417 if (sctp_transport_) {
2418 RTC_DCHECK(sctp_content_name_);
2419 RTC_DCHECK(sctp_transport_name_);
2420 if (content_name == *sctp_content_name_) {
2421 return *sctp_transport_name_;
2422 }
2423 }
zhihuang9763d562016-08-05 11:14:50 -07002424 // Return an empty string if failed to retrieve the transport name.
2425 return "";
2426 }
2427 return channel->transport_name();
2428}
zhihuangd82eee02016-08-26 11:25:05 -07002429
deadbeefac22f702017-01-12 21:59:29 -08002430void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002431 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002432 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002433 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2434}
2435
Steve Anton169629a2017-08-30 17:36:36 -07002436void WebRtcSession::RemoveAndDestroyVideoChannel(
2437 cricket::VideoChannel* video_channel) {
2438 auto it =
2439 std::find(video_channels_.begin(), video_channels_.end(), video_channel);
2440 RTC_DCHECK(it != video_channels_.end());
2441 if (it == video_channels_.end()) {
2442 return;
2443 }
2444 video_channels_.erase(it);
2445 DestroyVideoChannel(video_channel);
2446}
2447
2448void WebRtcSession::DestroyVideoChannel(cricket::VideoChannel* video_channel) {
2449 // TODO(steveanton): This should take an identifier for the video channel
2450 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002451 SignalVideoChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002452 RTC_DCHECK(video_channel->rtp_dtls_transport());
2453 const std::string transport_name =
2454 video_channel->rtp_dtls_transport()->transport_name();
2455 const bool need_to_delete_rtcp =
2456 (video_channel->rtcp_dtls_transport() != nullptr);
2457 // The above need to be cached before destroying the video channel so that we
2458 // do not access uninitialized memory.
2459 channel_manager_->DestroyVideoChannel(video_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002460 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002461 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002462 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002463 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002464 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002465 }
2466}
2467
Steve Anton169629a2017-08-30 17:36:36 -07002468void WebRtcSession::RemoveAndDestroyVoiceChannel(
2469 cricket::VoiceChannel* voice_channel) {
2470 auto it =
2471 std::find(voice_channels_.begin(), voice_channels_.end(), voice_channel);
2472 RTC_DCHECK(it != voice_channels_.end());
2473 if (it == voice_channels_.end()) {
2474 return;
2475 }
2476 voice_channels_.erase(it);
2477 DestroyVoiceChannel(voice_channel);
2478}
2479
2480void WebRtcSession::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) {
2481 // TODO(steveanton): This should take an identifier for the voice channel
2482 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002483 SignalVoiceChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002484 RTC_DCHECK(voice_channel->rtp_dtls_transport());
2485 const std::string transport_name =
2486 voice_channel->rtp_dtls_transport()->transport_name();
2487 const bool need_to_delete_rtcp =
2488 (voice_channel->rtcp_dtls_transport() != nullptr);
2489 // The above need to be cached before destroying the video channel so that we
2490 // do not access uninitialized memory.
2491 channel_manager_->DestroyVoiceChannel(voice_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002492 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002493 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002494 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002495 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002496 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002497 }
2498}
2499
2500void WebRtcSession::DestroyDataChannel() {
2501 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002502 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002503 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002504 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2505 bool need_to_delete_rtcp =
2506 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002507 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002508 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002509 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002510 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002511 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002512 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002513 }
2514}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002515} // namespace webrtc