blob: 66b515c5ea9ac2b0c62ac7708851287f1eb359fa [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
41using cricket::ContentInfo;
42using cricket::ContentInfos;
43using cricket::MediaContentDescription;
44using cricket::SessionDescription;
45using cricket::TransportInfo;
46
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070047using cricket::LOCAL_PORT_TYPE;
48using cricket::STUN_PORT_TYPE;
49using cricket::RELAY_PORT_TYPE;
50using cricket::PRFLX_PORT_TYPE;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052namespace webrtc {
53
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054// Error messages
Steve Anton6f25b092017-10-23 09:39:20 -070055const char kBundleWithoutRtcpMux[] =
56 "rtcp-mux must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
Zhi Huang2a5e4262017-09-14 01:15:03 -070061const char kMlineMismatchInAnswer[] =
62 "The order of m-lines in answer doesn't match order in offer. Rejecting "
63 "answer.";
64const char kMlineMismatchInSubsequentOffer[] =
65 "The order of m-lines in subsequent offer doesn't match order from "
66 "previous offer/answer.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000067const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080077const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000078 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080079const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000080 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070081const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070083IceCandidatePairType GetIceCandidatePairCounter(
84 const cricket::Candidate& local,
85 const cricket::Candidate& remote) {
86 const auto& l = local.type();
87 const auto& r = remote.type();
88 const auto& host = LOCAL_PORT_TYPE;
89 const auto& srflx = STUN_PORT_TYPE;
90 const auto& relay = RELAY_PORT_TYPE;
91 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070092 if (l == host && r == host) {
93 bool local_private = IPIsPrivate(local.address().ipaddr());
94 bool remote_private = IPIsPrivate(remote.address().ipaddr());
95 if (local_private) {
96 if (remote_private) {
97 return kIceCandidatePairHostPrivateHostPrivate;
98 } else {
99 return kIceCandidatePairHostPrivateHostPublic;
100 }
101 } else {
102 if (remote_private) {
103 return kIceCandidatePairHostPublicHostPrivate;
104 } else {
105 return kIceCandidatePairHostPublicHostPublic;
106 }
107 }
108 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700109 if (l == host && r == srflx)
110 return kIceCandidatePairHostSrflx;
111 if (l == host && r == relay)
112 return kIceCandidatePairHostRelay;
113 if (l == host && r == prflx)
114 return kIceCandidatePairHostPrflx;
115 if (l == srflx && r == host)
116 return kIceCandidatePairSrflxHost;
117 if (l == srflx && r == srflx)
118 return kIceCandidatePairSrflxSrflx;
119 if (l == srflx && r == relay)
120 return kIceCandidatePairSrflxRelay;
121 if (l == srflx && r == prflx)
122 return kIceCandidatePairSrflxPrflx;
123 if (l == relay && r == host)
124 return kIceCandidatePairRelayHost;
125 if (l == relay && r == srflx)
126 return kIceCandidatePairRelaySrflx;
127 if (l == relay && r == relay)
128 return kIceCandidatePairRelayRelay;
129 if (l == relay && r == prflx)
130 return kIceCandidatePairRelayPrflx;
131 if (l == prflx && r == host)
132 return kIceCandidatePairPrflxHost;
133 if (l == prflx && r == srflx)
134 return kIceCandidatePairPrflxSrflx;
135 if (l == prflx && r == relay)
136 return kIceCandidatePairPrflxRelay;
137 return kIceCandidatePairMax;
138}
139
Tommi589ae452017-10-15 21:20:46 +0000140// Verify that the order of media sections in |desc1| matches |desc2|. The
141// number of m= sections could be different.
142static bool MediaSectionsInSameOrder(const SessionDescription* desc1,
143 const SessionDescription* desc2) {
144 if (!desc1 || !desc2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 return false;
Zhi Huang2a5e4262017-09-14 01:15:03 -0700146 }
Tommi589ae452017-10-15 21:20:46 +0000147 for (size_t i = 0;
148 i < desc1->contents().size() && i < desc2->contents().size(); ++i) {
149 if ((desc2->contents()[i].name) != desc1->contents()[i].name) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 return false;
151 }
Tommi589ae452017-10-15 21:20:46 +0000152 const MediaContentDescription* desc2_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000153 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000154 desc2->contents()[i].description);
155 const MediaContentDescription* desc1_mdesc =
wu@webrtc.org4e393072014-04-07 17:04:35 +0000156 static_cast<const MediaContentDescription*>(
Tommi589ae452017-10-15 21:20:46 +0000157 desc1->contents()[i].description);
158 if (desc2_mdesc->type() != desc1_mdesc->type()) {
wu@webrtc.org4e393072014-04-07 17:04:35 +0000159 return false;
160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162 return true;
163}
164
Zhi Huang2a5e4262017-09-14 01:15:03 -0700165static bool MediaSectionsHaveSameCount(const SessionDescription* desc1,
166 const SessionDescription* desc2) {
167 if (!desc1 || !desc2) {
168 return false;
169 }
170 return desc1->contents().size() == desc2->contents().size();
171}
172
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173// Checks that each non-rejected content has SDES crypto keys or a DTLS
deadbeefb7892532017-02-22 19:35:18 -0800174// fingerprint, unless it's in a BUNDLE group, in which case only the
175// BUNDLE-tag section (first media section/description in the BUNDLE group)
176// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
177// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
178// by Channel's |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000179static bool VerifyCrypto(const SessionDescription* desc,
180 bool dtls_enabled,
181 std::string* error) {
deadbeefb7892532017-02-22 19:35:18 -0800182 const cricket::ContentGroup* bundle =
183 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 const ContentInfos& contents = desc->contents();
185 for (size_t index = 0; index < contents.size(); ++index) {
186 const ContentInfo* cinfo = &contents[index];
187 if (cinfo->rejected) {
188 continue;
189 }
deadbeefb7892532017-02-22 19:35:18 -0800190 if (bundle && bundle->HasContentName(cinfo->name) &&
191 cinfo->name != *(bundle->FirstContentName())) {
192 // This isn't the first media section in the BUNDLE group, so it's not
193 // required to have crypto attributes, since only the crypto attributes
194 // from the first section actually get used.
195 continue;
196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
deadbeefb7892532017-02-22 19:35:18 -0800198 // If the content isn't rejected or bundled into another m= section, crypto
199 // must be present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 const MediaContentDescription* media =
201 static_cast<const MediaContentDescription*>(cinfo->description);
202 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
203 if (!media || !tinfo) {
204 // Something is not right.
205 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000206 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 return false;
208 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000209 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000210 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000211 LOG(LS_WARNING) <<
212 "Session description must have DTLS fingerprint if DTLS enabled.";
213 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 return false;
215 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000216 } else {
217 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000218 LOG(LS_WARNING) <<
219 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000220 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000221 return false;
222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 }
224 }
225
226 return true;
227}
228
deadbeefb7892532017-02-22 19:35:18 -0800229// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
230// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
231// media section/description in the BUNDLE group) needs a ufrag and pwd.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000232static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
deadbeefb7892532017-02-22 19:35:18 -0800233 const cricket::ContentGroup* bundle =
234 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000235 const ContentInfos& contents = desc->contents();
236 for (size_t index = 0; index < contents.size(); ++index) {
237 const ContentInfo* cinfo = &contents[index];
238 if (cinfo->rejected) {
239 continue;
240 }
deadbeefb7892532017-02-22 19:35:18 -0800241 if (bundle && bundle->HasContentName(cinfo->name) &&
242 cinfo->name != *(bundle->FirstContentName())) {
243 // This isn't the first media section in the BUNDLE group, so it's not
244 // required to have ufrag/password, since only the ufrag/password from
245 // the first section actually get used.
246 continue;
247 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000248
deadbeefb7892532017-02-22 19:35:18 -0800249 // If the content isn't rejected or bundled into another m= section,
250 // ice-ufrag and ice-pwd must be present.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000251 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
252 if (!tinfo) {
253 // Something is not right.
254 LOG(LS_ERROR) << kInvalidSdp;
255 return false;
256 }
257 if (tinfo->description.ice_ufrag.empty() ||
258 tinfo->description.ice_pwd.empty()) {
259 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
260 return false;
261 }
262 }
263 return true;
264}
265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200267 uint32_t ssrc,
268 std::string* track_id) {
nisseede5da42017-01-12 05:15:36 -0800269 RTC_DCHECK(track_id != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 const cricket::ContentInfo* audio_info =
272 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000273 if (audio_info) {
274 const cricket::MediaContentDescription* audio_content =
275 static_cast<const cricket::MediaContentDescription*>(
276 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000278 const auto* found =
279 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
280 if (found) {
281 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000282 return true;
283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 }
285
286 const cricket::ContentInfo* video_info =
287 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000288 if (video_info) {
289 const cricket::MediaContentDescription* video_content =
290 static_cast<const cricket::MediaContentDescription*>(
291 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000293 const auto* found =
294 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
295 if (found) {
296 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000297 return true;
298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 }
300 return false;
301}
302
deadbeef953c2ce2017-01-09 14:53:41 -0800303// Get the SCTP port out of a SessionDescription.
304// Return -1 if not found.
305static int GetSctpPort(const SessionDescription* session_description) {
306 const ContentInfo* content_info = GetFirstDataContent(session_description);
307 RTC_DCHECK(content_info);
308 if (!content_info) {
309 return -1;
310 }
311 const cricket::DataContentDescription* data =
312 static_cast<const cricket::DataContentDescription*>(
313 (content_info->description));
314 std::string value;
315 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
316 cricket::kGoogleSctpDataCodecName);
317 for (const cricket::DataCodec& codec : data->codecs()) {
318 if (!codec.Matches(match_pattern)) {
319 continue;
320 }
321 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
322 return rtc::FromString<int>(value);
323 }
324 }
325 return -1;
326}
327
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328static bool BadSdp(const std::string& source,
329 const std::string& type,
330 const std::string& reason,
331 std::string* err_desc) {
332 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700333 desc << "Failed to set " << source;
334 if (!type.empty()) {
335 desc << " " << type;
336 }
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000337 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 return false;
344}
345
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000347 const std::string& type,
348 const std::string& reason,
349 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000353 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 }
355}
356
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000357static bool BadLocalSdp(const std::string& type,
358 const std::string& reason,
359 std::string* err_desc) {
360 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
361}
362
363static bool BadRemoteSdp(const std::string& type,
364 const std::string& reason,
365 std::string* err_desc) {
366 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
367}
368
369static bool BadOfferSdp(cricket::ContentSource source,
370 const std::string& reason,
371 std::string* err_desc) {
372 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
373}
374
375static bool BadPranswerSdp(cricket::ContentSource source,
376 const std::string& reason,
377 std::string* err_desc) {
378 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
379 reason, err_desc);
380}
381
382static bool BadAnswerSdp(cricket::ContentSource source,
383 const std::string& reason,
384 std::string* err_desc) {
385 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
deadbeefd59daf82015-10-14 15:02:44 -0700388#define GET_STRING_OF_STATE(state) \
389 case webrtc::WebRtcSession::state: \
390 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 break;
392
deadbeefd59daf82015-10-14 15:02:44 -0700393static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 std::string result;
395 switch (state) {
396 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700397 GET_STRING_OF_STATE(STATE_SENTOFFER)
398 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
399 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
400 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700402 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 default:
nissec80e7412017-01-11 05:56:46 -0800404 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 break;
406 }
407 return result;
408}
409
deadbeefd59daf82015-10-14 15:02:44 -0700410#define GET_STRING_OF_ERROR_CODE(err) \
411 case webrtc::WebRtcSession::err: \
412 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 break;
414
deadbeefd59daf82015-10-14 15:02:44 -0700415static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 std::string result;
417 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000418 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
420 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800422 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 break;
424 }
425 return result;
426}
427
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000428static std::string MakeErrorString(const std::string& error,
429 const std::string& desc) {
430 std::ostringstream ret;
431 ret << error << " " << desc;
432 return ret.str();
433}
434
435static std::string MakeTdErrorString(const std::string& desc) {
436 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437}
438
deadbeef0ed85b22016-02-23 17:24:52 -0800439// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
440bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
441 const SessionDescriptionInterface* new_desc,
442 const std::string& content_name) {
443 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700444 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 }
deadbeef0ed85b22016-02-23 17:24:52 -0800446 const SessionDescription* new_sd = new_desc->description();
447 const SessionDescription* old_sd = old_desc->description();
448 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
449 if (!cinfo || cinfo->rejected) {
450 return false;
451 }
452 // If the content isn't rejected, check if ufrag and password has changed.
453 const cricket::TransportDescription* new_transport_desc =
454 new_sd->GetTransportDescriptionByName(content_name);
455 const cricket::TransportDescription* old_transport_desc =
456 old_sd->GetTransportDescriptionByName(content_name);
457 if (!new_transport_desc || !old_transport_desc) {
458 // No transport description exists. This is not an ICE restart.
459 return false;
460 }
461 if (cricket::IceCredentialsChanged(
462 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
463 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
464 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
465 << ".";
466 return true;
467 }
468 return false;
469}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470
zhihuang29ff8442016-07-27 11:07:25 -0700471WebRtcSession::WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700472 Call* call,
473 cricket::ChannelManager* channel_manager,
474 const cricket::MediaConfig& media_config,
475 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700476 rtc::Thread* network_thread,
477 rtc::Thread* worker_thread,
478 rtc::Thread* signaling_thread,
479 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800480 std::unique_ptr<cricket::TransportController> transport_controller,
481 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700482 : network_thread_(network_thread),
483 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700484 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // RFC 3264: The numeric value of the session id and version in the
486 // o line MUST be representable with a "64 bit signed integer".
487 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700488 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700489 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800490 sctp_factory_(std::move(sctp_factory)),
nisseeaabdf62017-05-05 02:23:02 -0700491 media_config_(media_config),
492 event_log_(event_log),
493 call_(call),
494 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 ice_observer_(NULL),
496 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700497 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000499 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000501 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700502 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
503 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700504 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700505 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700506 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700507 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700508 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700509 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700510 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700511 transport_controller_->SignalCandidatesRemoved.connect(
512 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700513 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800514 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515}
516
517WebRtcSession::~WebRtcSession() {
nisseede5da42017-01-12 05:15:36 -0800518 RTC_DCHECK(signaling_thread()->IsCurrent());
Steve Anton169629a2017-08-30 17:36:36 -0700519 // Destroy video channels first since they may have a pointer to a voice
520 // channel.
521 for (auto* channel : video_channels_) {
522 DestroyVideoChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
Steve Anton169629a2017-08-30 17:36:36 -0700524 for (auto* channel : voice_channels_) {
525 DestroyVoiceChannel(channel);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000526 }
deadbeef953c2ce2017-01-09 14:53:41 -0800527 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -0800528 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -0800529 }
530 if (sctp_transport_) {
531 SignalDataChannelDestroyed();
532 network_thread_->Invoke<void>(
533 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
deadbeefd59daf82015-10-14 15:02:44 -0700535
536 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537}
538
Steve Antond5585ca2017-10-23 14:49:26 -0700539void WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000540 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200541 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Steve Antond5585ca2017-10-23 14:49:26 -0700542 const PeerConnectionInterface::RTCConfiguration& rtc_configuration,
543 PeerConnection* pc) {
Henrik Lundin64dad832015-05-11 12:44:23 +0200544 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700545 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700546 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000547
Henrik Boström87713d02015-08-25 09:53:21 +0200548 // Obtain a certificate from RTCConfiguration if any were provided (optional).
549 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
550 if (!rtc_configuration.certificates.empty()) {
551 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
552 // just picking the first one. The decision should be made based on the DTLS
553 // handshake. The DTLS negotiations need to know about all certificates.
554 certificate = rtc_configuration.certificates[0];
555 }
556
honghaiz1f429e32015-09-28 07:57:34 -0700557 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700558
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000559 if (options.disable_encryption) {
560 dtls_enabled_ = false;
561 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200562 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200563 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800564 // |rtc_configuration| can override the default |dtls_enabled_| value.
565 if (rtc_configuration.enable_dtls_srtp) {
566 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000567 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000568 }
569
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000571 // It takes precendence over the disable_sctp_data_channels
572 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800573 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 data_channel_type_ = cricket::DCT_RTP;
Steve Antonc4faa9c2017-10-23 14:44:03 -0700575 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000576 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000577 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000578 data_channel_type_ = cricket::DCT_SCTP;
579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581
htaa2a49d92016-03-04 02:51:39 -0800582 video_options_.screencast_min_bitrate_kbps =
583 rtc_configuration.screencast_min_bitrate;
584 audio_options_.combined_audio_video_bwe =
585 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000586
kwiberg102c6a62015-10-30 02:47:38 -0700587 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100588 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200589
Karl Wibergbe579832015-11-10 22:34:18 +0100590 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
591 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200592
Steve Antond5585ca2017-10-23 14:49:26 -0700593 // Whether the certificate generator/certificate is null or not determines
594 // what WebRtcSessionDescriptionFactory will do, so make sure that we give it
595 // the right instructions by clearing the variables if needed.
Henrik Boström87713d02015-08-25 09:53:21 +0200596 if (!dtls_enabled_) {
Steve Antond5585ca2017-10-23 14:49:26 -0700597 cert_generator.reset();
598 certificate = nullptr;
599 } else if (certificate) {
600 // Favor generated certificate over the certificate generator.
601 cert_generator.reset();
Henrik Boström87713d02015-08-25 09:53:21 +0200602 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000603
Steve Antond5585ca2017-10-23 14:49:26 -0700604 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
605 signaling_thread(), channel_manager_, pc, id(), std::move(cert_generator),
606 certificate));
Henrik Boströmd8281982015-08-27 10:12:24 +0200607 webrtc_session_desc_factory_->SignalCertificateReady.connect(
608 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000609
wu@webrtc.org97077a32013-10-25 21:18:33 +0000610 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000611 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000612 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700613
jbauch5869f502017-06-29 12:31:36 -0700614 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
615 options.crypto_options.enable_encrypted_rtp_header_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616}
617
deadbeefd59daf82015-10-14 15:02:44 -0700618void WebRtcSession::Close() {
619 SetState(STATE_CLOSED);
620 RemoveUnusedChannels(nullptr);
stefanf79ade12017-06-02 06:44:03 -0700621 call_ = nullptr;
Steve Anton169629a2017-08-30 17:36:36 -0700622 RTC_DCHECK(voice_channels_.empty());
623 RTC_DCHECK(video_channels_.empty());
deadbeef953c2ce2017-01-09 14:53:41 -0800624 RTC_DCHECK(!rtp_data_channel_);
625 RTC_DCHECK(!sctp_transport_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626}
627
deadbeef0ed85b22016-02-23 17:24:52 -0800628cricket::BaseChannel* WebRtcSession::GetChannel(
629 const std::string& content_name) {
630 if (voice_channel() && voice_channel()->content_name() == content_name) {
631 return voice_channel();
632 }
633 if (video_channel() && video_channel()->content_name() == content_name) {
634 return video_channel();
635 }
deadbeef953c2ce2017-01-09 14:53:41 -0800636 if (rtp_data_channel() &&
637 rtp_data_channel()->content_name() == content_name) {
638 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800639 }
640 return nullptr;
641}
642
deadbeef953c2ce2017-01-09 14:53:41 -0800643bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
644 if (!local_description() || !remote_description()) {
645 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
646 << "SSL Role of the SCTP transport.";
647 return false;
648 }
649 if (!sctp_transport_) {
650 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
651 << "SSL Role of the SCTP transport.";
652 return false;
653 }
654
655 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
656}
657
658bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800659 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800660 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800661 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000662 << "SSL Role of the session.";
663 return false;
664 }
665
deadbeef953c2ce2017-01-09 14:53:41 -0800666 return transport_controller_->GetSslRole(GetTransportName(content_name),
667 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000668}
669
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000670void WebRtcSession::CreateOffer(
671 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700672 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
673 const cricket::MediaSessionOptions& session_options) {
674 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000675}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
deadbeefab9b2d12015-10-14 11:33:11 -0700677void WebRtcSession::CreateAnswer(
678 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700679 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800680 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681}
682
Steve Anton8d3444d2017-10-20 15:30:51 -0700683bool WebRtcSession::SetLocalDescription(
684 std::unique_ptr<SessionDescriptionInterface> desc,
685 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800686 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700687
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000688 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700689 if (!ValidateSessionDescription(desc.get(), cricket::CS_LOCAL, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000690 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 }
692
deadbeefd59daf82015-10-14 15:02:44 -0700693 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000694 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700696 initial_offerer_ = true;
697 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 }
699
deadbeeffe4a8a42016-12-20 17:56:17 -0800700 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700701 current_local_description_ = std::move(desc);
702 pending_local_description_ = nullptr;
703 current_remote_description_ = std::move(pending_remote_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800704 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700705 pending_local_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800706 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
708 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800709 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 // TODO(mallinath) - Handle CreateChannel failure, as new local description
711 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700712 return BadLocalSdp(local_description()->type(), kCreateChannelFailed,
713 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 }
715
deadbeefcbecd352015-09-23 11:50:27 -0700716 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800717 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000719 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 return false;
721 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800722 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700723 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800724 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
deadbeef0ed85b22016-02-23 17:24:52 -0800727 pending_ice_restarts_.clear();
deadbeefd59daf82015-10-14 15:02:44 -0700728 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700729 return BadLocalSdp(local_description()->type(), GetSessionErrorMsg(),
730 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732 return true;
733}
734
Steve Anton8d3444d2017-10-20 15:30:51 -0700735bool WebRtcSession::SetRemoteDescription(
736 std::unique_ptr<SessionDescriptionInterface> desc,
737 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800738 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700739
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000740 // Validate SDP.
Steve Anton8d3444d2017-10-20 15:30:51 -0700741 if (!ValidateSessionDescription(desc.get(), cricket::CS_REMOTE, err_desc)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000742 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744
Steve Anton8d3444d2017-10-20 15:30:51 -0700745 // Hold this pointer so candidates can be copied to it later in the method.
746 SessionDescriptionInterface* desc_ptr = desc.get();
747
deadbeeffe4a8a42016-12-20 17:56:17 -0800748 const SessionDescriptionInterface* old_remote_description =
749 remote_description();
750 // Grab ownership of the description being replaced for the remainder of this
Steve Anton8d3444d2017-10-20 15:30:51 -0700751 // method, since it's used below as |old_remote_description|.
deadbeeffe4a8a42016-12-20 17:56:17 -0800752 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
753 Action action = GetAction(desc->type());
754 if (action == kAnswer) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700755 replaced_remote_description = pending_remote_description_
756 ? std::move(pending_remote_description_)
757 : std::move(current_remote_description_);
758 current_remote_description_ = std::move(desc);
759 pending_remote_description_ = nullptr;
760 current_local_description_ = std::move(pending_local_description_);
deadbeeffe4a8a42016-12-20 17:56:17 -0800761 } else {
Steve Anton8d3444d2017-10-20 15:30:51 -0700762 replaced_remote_description = std::move(pending_remote_description_);
763 pending_remote_description_ = std::move(desc);
deadbeeffe4a8a42016-12-20 17:56:17 -0800764 }
deadbeefd59daf82015-10-14 15:02:44 -0700765
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 // Transport and Media channels will be created only when offer is set.
Steve Anton8d3444d2017-10-20 15:30:51 -0700767 if (action == kOffer &&
768 !CreateChannels(remote_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // TODO(mallinath) - Handle CreateChannel failure, as new local description
770 // is applied. Restore back to old description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700771 return BadRemoteSdp(remote_description()->type(), kCreateChannelFailed,
772 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774
deadbeefcbecd352015-09-23 11:50:27 -0700775 // Remove unused channels if MediaContentDescription is rejected.
Steve Anton8d3444d2017-10-20 15:30:51 -0700776 RemoveUnusedChannels(remote_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777
778 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
779 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000780 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 return false;
782 }
783
Steve Anton8d3444d2017-10-20 15:30:51 -0700784 if (local_description() &&
785 !UseCandidatesInSessionDescription(remote_description())) {
786 return BadRemoteSdp(remote_description()->type(), kInvalidCandidates,
787 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
deadbeeffe4a8a42016-12-20 17:56:17 -0800790 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800791 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800792 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800793 // Check if this new SessionDescription contains new ICE ufrag and
794 // password that indicates the remote peer requests an ICE restart.
795 // TODO(deadbeef): When we start storing both the current and pending
796 // remote description, this should reset pending_ice_restarts and compare
797 // against the current description.
Steve Anton8d3444d2017-10-20 15:30:51 -0700798 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
deadbeeffe4a8a42016-12-20 17:56:17 -0800799 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800800 if (action == kOffer) {
801 pending_ice_restarts_.insert(content.name);
802 }
803 } else {
804 // We retain all received candidates only if ICE is not restarted.
805 // When ICE is restarted, all previous candidates belong to an old
806 // generation and should not be kept.
807 // TODO(deadbeef): This goes against the W3C spec which says the remote
808 // description should only contain candidates from the last set remote
809 // description plus any candidates added since then. We should remove
810 // this once we're sure it won't break anything.
811 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700812 old_remote_description, content.name, desc_ptr);
deadbeef0ed85b22016-02-23 17:24:52 -0800813 }
814 }
honghaiz503726c2015-07-31 12:37:38 -0700815 }
816
deadbeefd59daf82015-10-14 15:02:44 -0700817 if (error() != ERROR_NONE) {
Steve Anton8d3444d2017-10-20 15:30:51 -0700818 return BadRemoteSdp(remote_description()->type(), GetSessionErrorMsg(),
819 err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000821
822 // Set the the ICE connection state to connecting since the connection may
823 // become writable with peer reflexive candidates before any remote candidate
824 // is signaled.
825 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
826 // is to have a new signal the indicates a change in checking state from the
827 // transport and expose a new checking() member from transport that can be
828 // read to determine the current checking state. The existing SignalConnecting
829 // actually means "gathering candidates", so cannot be be used here.
Steve Anton8d3444d2017-10-20 15:30:51 -0700830 if (remote_description()->type() != SessionDescriptionInterface::kOffer &&
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000831 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
832 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
833 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 return true;
835}
836
Steve Anton18ee1d52017-09-11 11:32:35 -0700837// TODO(steveanton): Eventually it'd be nice to store the channels as a single
838// vector of BaseChannel pointers instead of separate voice and video channel
839// vectors. At that point, this will become a simple getter.
840std::vector<cricket::BaseChannel*> WebRtcSession::Channels() const {
841 std::vector<cricket::BaseChannel*> channels;
842 channels.insert(channels.end(), voice_channels_.begin(),
843 voice_channels_.end());
844 channels.insert(channels.end(), video_channels_.begin(),
845 video_channels_.end());
846 if (rtp_data_channel_) {
847 channels.push_back(rtp_data_channel_.get());
848 }
849 return channels;
850}
851
deadbeefd59daf82015-10-14 15:02:44 -0700852void WebRtcSession::LogState(State old_state, State new_state) {
853 LOG(LS_INFO) << "Session:" << id()
854 << " Old state:" << GetStateString(old_state)
855 << " New state:" << GetStateString(new_state);
856}
857
858void WebRtcSession::SetState(State state) {
nisseede5da42017-01-12 05:15:36 -0800859 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700860 if (state != state_) {
861 LogState(state_, state);
862 state_ = state;
863 SignalState(this, state_);
864 }
865}
866
867void WebRtcSession::SetError(Error error, const std::string& error_desc) {
nisseede5da42017-01-12 05:15:36 -0800868 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -0700869 if (error != error_) {
870 error_ = error;
871 error_desc_ = error_desc;
872 }
873}
874
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875bool WebRtcSession::UpdateSessionState(
876 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 std::string* err_desc) {
nisseede5da42017-01-12 05:15:36 -0800878 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700879
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 // If there's already a pending error then no state transition should happen.
881 // But all call-sites should be verifying this before calling us!
nisseede5da42017-01-12 05:15:36 -0800882 RTC_DCHECK(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000883 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000885 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
886 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
deadbeefd59daf82015-10-14 15:02:44 -0700888 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
889 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000890 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700891 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000892 }
deadbeefd59daf82015-10-14 15:02:44 -0700893 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000894 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
896 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000897 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
898 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700901 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
902 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000903 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700904 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000905 }
deadbeefd59daf82015-10-14 15:02:44 -0700906 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000907 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700910 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800911 local_description()->description()->GetGroupByName(
912 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700913 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800914 remote_description()->description()->GetGroupByName(
915 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700916 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800917 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700918 const cricket::ContentGroup* answer_bundle =
919 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
920 if (!EnableBundle(*answer_bundle)) {
921 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
922 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
923 }
924 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800925 // Only push down the transport description after enabling BUNDLE; we don't
926 // want to push down a description on a transport about to be destroyed.
927 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
928 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700931 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000932 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700933 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000934 }
deadbeefd59daf82015-10-14 15:02:44 -0700935 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000936 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 }
938 }
939 return true;
940}
941
942WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
943 if (type == SessionDescriptionInterface::kOffer) {
944 return WebRtcSession::kOffer;
945 } else if (type == SessionDescriptionInterface::kPrAnswer) {
946 return WebRtcSession::kPrAnswer;
947 } else if (type == SessionDescriptionInterface::kAnswer) {
948 return WebRtcSession::kAnswer;
949 }
nisseede5da42017-01-12 05:15:36 -0800950 RTC_NOTREACHED() << "unknown action type";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 return WebRtcSession::kOffer;
952}
953
deadbeefd59daf82015-10-14 15:02:44 -0700954bool WebRtcSession::PushdownMediaDescription(
955 cricket::ContentAction action,
956 cricket::ContentSource source,
957 std::string* err) {
Steve Anton18ee1d52017-09-11 11:32:35 -0700958 const SessionDescription* sdesc =
959 (source == cricket::CS_LOCAL ? local_description() : remote_description())
960 ->description();
961 RTC_DCHECK(sdesc);
962 bool all_success = true;
963 for (auto* channel : Channels()) {
964 // TODO(steveanton): Add support for multiple channels of the same type.
965 const ContentInfo* content_info =
966 cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type());
967 if (!content_info) {
968 continue;
deadbeefd59daf82015-10-14 15:02:44 -0700969 }
Steve Anton18ee1d52017-09-11 11:32:35 -0700970 const MediaContentDescription* content_desc =
971 static_cast<const MediaContentDescription*>(content_info->description);
972 if (content_desc && !content_info->rejected) {
973 bool success = (source == cricket::CS_LOCAL)
974 ? channel->SetLocalContent(content_desc, action, err)
975 : channel->SetRemoteContent(content_desc, action, err);
976 if (!success) {
977 all_success = false;
978 break;
979 }
980 }
981 }
deadbeef953c2ce2017-01-09 14:53:41 -0800982 // Need complete offer/answer with an SCTP m= section before starting SCTP,
983 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
984 if (sctp_transport_ && local_description() && remote_description() &&
985 cricket::GetFirstDataContent(local_description()->description()) &&
986 cricket::GetFirstDataContent(remote_description()->description())) {
Steve Anton18ee1d52017-09-11 11:32:35 -0700987 all_success &= network_thread_->Invoke<bool>(
deadbeef953c2ce2017-01-09 14:53:41 -0800988 RTC_FROM_HERE,
989 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
990 }
Steve Anton18ee1d52017-09-11 11:32:35 -0700991 return all_success;
deadbeef953c2ce2017-01-09 14:53:41 -0800992}
993
994bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
995 RTC_DCHECK(network_thread_->IsCurrent());
996 RTC_DCHECK(local_description());
997 RTC_DCHECK(remote_description());
998 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
999 // When we support "max-message-size", that would also be pushed down here.
1000 return sctp_transport_->Start(
1001 GetSctpPort(local_description()->description()),
1002 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -07001003}
1004
1005bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1006 cricket::ContentAction action,
1007 std::string* error_desc) {
1008 RTC_DCHECK(signaling_thread()->IsCurrent());
1009
1010 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001011 return PushdownLocalTransportDescription(local_description()->description(),
1012 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001013 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001014 return PushdownRemoteTransportDescription(remote_description()->description(),
1015 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001016}
1017
1018bool WebRtcSession::PushdownLocalTransportDescription(
1019 const SessionDescription* sdesc,
1020 cricket::ContentAction action,
1021 std::string* err) {
1022 RTC_DCHECK(signaling_thread()->IsCurrent());
1023
1024 if (!sdesc) {
1025 return false;
1026 }
1027
1028 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1029 if (!transport_controller_->SetLocalTransportDescription(
1030 tinfo.content_name, tinfo.description, action, err)) {
1031 return false;
1032 }
1033 }
1034
1035 return true;
1036}
1037
1038bool WebRtcSession::PushdownRemoteTransportDescription(
1039 const SessionDescription* sdesc,
1040 cricket::ContentAction action,
1041 std::string* err) {
1042 RTC_DCHECK(signaling_thread()->IsCurrent());
1043
1044 if (!sdesc) {
1045 return false;
1046 }
1047
1048 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1049 if (!transport_controller_->SetRemoteTransportDescription(
1050 tinfo.content_name, tinfo.description, action, err)) {
1051 return false;
1052 }
1053 }
1054
1055 return true;
1056}
1057
1058bool WebRtcSession::GetTransportDescription(
1059 const SessionDescription* description,
1060 const std::string& content_name,
1061 cricket::TransportDescription* tdesc) {
1062 if (!description || !tdesc) {
1063 return false;
1064 }
1065 const TransportInfo* transport_info =
1066 description->GetTransportInfoByName(content_name);
1067 if (!transport_info) {
1068 return false;
1069 }
1070 *tdesc = transport_info->description;
1071 return true;
1072}
1073
deadbeefcbecd352015-09-23 11:50:27 -07001074bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1075 const std::string* first_content_name = bundle.FirstContentName();
1076 if (!first_content_name) {
1077 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1078 return false;
1079 }
1080 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001081
deadbeef953c2ce2017-01-09 14:53:41 -08001082 auto maybe_set_transport = [this, bundle,
1083 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001084 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001085 return true;
1086 }
1087
zhihuangf5b251b2017-01-12 19:37:48 -08001088 std::string old_transport_name = ch->transport_name();
1089 if (old_transport_name == transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001090 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1091 << " on " << transport_name << ".";
1092 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001093 }
torbjornga81a42f2015-09-23 02:16:58 -07001094
zhihuangb2cdd932017-01-19 16:54:25 -08001095 cricket::DtlsTransportInternal* rtp_dtls_transport =
1096 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001097 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001098 bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr);
1099 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
zhihuangf5b251b2017-01-12 19:37:48 -08001100 if (need_rtcp) {
deadbeefd8cf08f2017-07-10 20:06:59 -07001101 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001102 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1103 }
1104
zhihuangb2cdd932017-01-19 16:54:25 -08001105 ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport);
deadbeefcbecd352015-09-23 11:50:27 -07001106 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1107 << transport_name << ".";
zhihuangb2cdd932017-01-19 16:54:25 -08001108 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001109 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08001110 // If the channel needs rtcp, it means that the channel used to have a
1111 // rtcp transport which needs to be deleted now.
1112 if (need_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08001113 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001114 old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08001115 }
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001116 return true;
1117 };
1118
deadbeefcbecd352015-09-23 11:50:27 -07001119 if (!maybe_set_transport(voice_channel()) ||
1120 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001121 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001122 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001123 }
deadbeef953c2ce2017-01-09 14:53:41 -08001124 // For SCTP, transport creation/deletion happens here instead of in the
1125 // object itself.
1126 if (sctp_transport_) {
1127 RTC_DCHECK(sctp_transport_name_);
1128 RTC_DCHECK(sctp_content_name_);
1129 if (transport_name != *sctp_transport_name_ &&
1130 bundle.HasContentName(*sctp_content_name_)) {
1131 network_thread_->Invoke<void>(
1132 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1133 transport_name));
1134 }
1135 }
deadbeefcbecd352015-09-23 11:50:27 -07001136
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001137 return true;
1138}
1139
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001141 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001142 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1143 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 }
1146
1147 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001148 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 return false;
1150 }
1151
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001152 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001153 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1154 if (!valid) {
1155 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 }
1157
1158 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001159 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001160 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 return false;
1162 }
1163
deadbeefd59daf82015-10-14 15:02:44 -07001164 if (ready) {
1165 return UseCandidate(candidate);
1166 } else {
1167 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1168 return true;
1169 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170}
1171
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001172bool WebRtcSession::RemoveRemoteIceCandidates(
1173 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001174 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001175 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1176 << "removed without any remote session description.";
1177 return false;
1178 }
1179
1180 if (candidates.empty()) {
1181 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1182 return false;
1183 }
1184
deadbeeffe4a8a42016-12-20 17:56:17 -08001185 size_t number_removed =
1186 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001187 if (number_removed != candidates.size()) {
1188 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1189 << "Requested " << candidates.size() << " but only "
1190 << number_removed << " are removed.";
1191 }
1192
1193 // Remove the candidates from the transport controller.
1194 std::string error;
1195 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1196 if (!res && !error.empty()) {
1197 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1198 }
1199 return true;
1200}
1201
deadbeefd59daf82015-10-14 15:02:44 -07001202cricket::IceConfig WebRtcSession::ParseIceConfig(
1203 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001204 cricket::ContinualGatheringPolicy gathering_policy;
1205 // TODO(honghaiz): Add the third continual gathering policy in
1206 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1207 switch (config.continual_gathering_policy) {
1208 case PeerConnectionInterface::GATHER_ONCE:
1209 gathering_policy = cricket::GATHER_ONCE;
1210 break;
1211 case PeerConnectionInterface::GATHER_CONTINUALLY:
1212 gathering_policy = cricket::GATHER_CONTINUALLY;
1213 break;
1214 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001215 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001216 gathering_policy = cricket::GATHER_ONCE;
1217 }
deadbeefd59daf82015-10-14 15:02:44 -07001218 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001219 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001220 ice_config.prioritize_most_likely_candidate_pairs =
1221 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001222 ice_config.backup_connection_ping_interval =
1223 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001224 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001225 ice_config.presume_writable_when_fully_relayed =
1226 config.presume_writable_when_fully_relayed;
skvlad51072462017-02-02 11:50:14 -08001227 ice_config.ice_check_min_interval = config.ice_check_min_interval;
Steve Anton300bf8e2017-07-14 10:13:10 -07001228 ice_config.regather_all_networks_interval_range =
1229 config.ice_regather_interval_range;
deadbeefd59daf82015-10-14 15:02:44 -07001230 return ice_config;
1231}
1232
1233void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1234 transport_controller_->SetIceConfig(config);
1235}
1236
1237void WebRtcSession::MaybeStartGathering() {
1238 transport_controller_->MaybeStartGathering();
1239}
1240
Peter Boström0c4e06b2015-10-07 12:23:21 +02001241bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1242 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001243 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001245 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001246 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1247 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248}
1249
Peter Boström0c4e06b2015-10-07 12:23:21 +02001250bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1251 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001252 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001253 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001254 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001255 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1256 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257}
1258
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001259std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001261 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 return desc.str();
1263}
1264
wu@webrtc.org78187522013-10-07 23:32:02 +00001265bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001266 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001267 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001268 if (!rtp_data_channel_ && !sctp_transport_) {
1269 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1270 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001271 return false;
1272 }
deadbeef953c2ce2017-01-09 14:53:41 -08001273 return rtp_data_channel_
1274 ? rtp_data_channel_->SendData(params, payload, result)
1275 : network_thread_->Invoke<bool>(
1276 RTC_FROM_HERE,
1277 Bind(&cricket::SctpTransportInternal::SendData,
1278 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001279}
1280
1281bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001282 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001283 // Don't log an error here, because DataChannels are expected to call
1284 // ConnectDataChannel in this state. It's the only way to initially tell
1285 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001286 return false;
1287 }
deadbeef953c2ce2017-01-09 14:53:41 -08001288 if (rtp_data_channel_) {
1289 rtp_data_channel_->SignalReadyToSendData.connect(
1290 webrtc_data_channel, &DataChannel::OnChannelReady);
1291 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1292 &DataChannel::OnDataReceived);
1293 } else {
1294 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1295 &DataChannel::OnChannelReady);
1296 SignalSctpDataReceived.connect(webrtc_data_channel,
1297 &DataChannel::OnDataReceived);
1298 SignalSctpStreamClosedRemotely.connect(
1299 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1300 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001301 return true;
1302}
1303
1304void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001305 if (!rtp_data_channel_ && !sctp_transport_) {
1306 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1307 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001308 return;
1309 }
deadbeef953c2ce2017-01-09 14:53:41 -08001310 if (rtp_data_channel_) {
1311 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1312 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1313 } else {
1314 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1315 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1316 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1317 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001318}
1319
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001320void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001321 if (!sctp_transport_) {
1322 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001323 return;
1324 }
deadbeef953c2ce2017-01-09 14:53:41 -08001325 network_thread_->Invoke<void>(
1326 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1327 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001328}
1329
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001330void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001331 if (!sctp_transport_) {
1332 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001333 << "NULL.";
1334 return;
1335 }
deadbeef953c2ce2017-01-09 14:53:41 -08001336 network_thread_->Invoke<void>(
1337 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1338 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001339}
1340
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001341bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001342 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1343 sctp_ready_to_send_data_;
1344}
1345
1346std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
nisseede5da42017-01-12 05:15:36 -08001347 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeef953c2ce2017-01-09 14:53:41 -08001348 ChannelNamePairs channel_name_pairs;
1349 if (voice_channel()) {
1350 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1351 voice_channel()->content_name(), voice_channel()->transport_name()));
1352 }
1353 if (video_channel()) {
1354 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1355 video_channel()->content_name(), video_channel()->transport_name()));
1356 }
1357 if (rtp_data_channel()) {
1358 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1359 ChannelNamePair(rtp_data_channel()->content_name(),
1360 rtp_data_channel()->transport_name()));
1361 }
1362 if (sctp_transport_) {
1363 RTC_DCHECK(sctp_content_name_);
1364 RTC_DCHECK(sctp_transport_name_);
1365 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1366 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1367 }
1368 return GetStats(channel_name_pairs);
1369}
1370
1371std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1372 const ChannelNamePairs& channel_name_pairs) {
1373 if (network_thread()->IsCurrent()) {
1374 return GetStats_n(channel_name_pairs);
1375 }
1376 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1377 RTC_FROM_HERE,
1378 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1379}
1380
1381bool WebRtcSession::GetLocalCertificate(
1382 const std::string& transport_name,
1383 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1384 return transport_controller_->GetLocalCertificate(transport_name,
1385 certificate);
1386}
1387
1388std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1389 const std::string& transport_name) {
1390 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001391}
1392
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393cricket::DataChannelType WebRtcSession::data_channel_type() const {
1394 return data_channel_type_;
1395}
1396
deadbeef0ed85b22016-02-23 17:24:52 -08001397bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1398 return pending_ice_restarts_.find(content_name) !=
1399 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001400}
1401
deadbeefd1a38b52016-12-10 13:15:33 -08001402void WebRtcSession::SetNeedsIceRestartFlag() {
1403 transport_controller_->SetNeedsIceRestartFlag();
1404}
1405
1406bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1407 return transport_controller_->NeedsIceRestart(content_name);
1408}
1409
Henrik Boströmd8281982015-08-27 10:12:24 +02001410void WebRtcSession::OnCertificateReady(
1411 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001412 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001413}
1414
deadbeef953c2ce2017-01-09 14:53:41 -08001415void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1416 SetError(ERROR_TRANSPORT,
1417 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1418}
1419
Henrik Boströmd8281982015-08-27 10:12:24 +02001420bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001421 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001422}
1423
deadbeefcbecd352015-09-23 11:50:27 -07001424const rtc::scoped_refptr<rtc::RTCCertificate>&
1425WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001426 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001427}
1428
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429void WebRtcSession::SetIceConnectionState(
1430 PeerConnectionInterface::IceConnectionState state) {
1431 if (ice_connection_state_ == state) {
1432 return;
1433 }
1434
deadbeefcbecd352015-09-23 11:50:27 -07001435 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1436 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001437 RTC_DCHECK(ice_connection_state_ !=
1438 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 ice_connection_state_ = state;
1440 if (ice_observer_) {
zstein6dfd53a2017-03-06 13:49:03 -08001441 ice_observer_->OnIceConnectionStateChange(ice_connection_state_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442 }
1443}
1444
deadbeefcbecd352015-09-23 11:50:27 -07001445void WebRtcSession::OnTransportControllerConnectionState(
1446 cricket::IceConnectionState state) {
1447 switch (state) {
1448 case cricket::kIceConnectionConnecting:
1449 // If the current state is Connected or Completed, then there were
1450 // writable channels but now there are not, so the next state must
1451 // be Disconnected.
1452 // kIceConnectionConnecting is currently used as the default,
1453 // un-connected state by the TransportController, so its only use is
1454 // detecting disconnections.
1455 if (ice_connection_state_ ==
1456 PeerConnectionInterface::kIceConnectionConnected ||
1457 ice_connection_state_ ==
1458 PeerConnectionInterface::kIceConnectionCompleted) {
1459 SetIceConnectionState(
1460 PeerConnectionInterface::kIceConnectionDisconnected);
1461 }
torbjornga81a42f2015-09-23 02:16:58 -07001462 break;
deadbeefcbecd352015-09-23 11:50:27 -07001463 case cricket::kIceConnectionFailed:
1464 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1465 break;
1466 case cricket::kIceConnectionConnected:
1467 LOG(LS_INFO) << "Changing to ICE connected state because "
1468 << "all transports are writable.";
1469 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1470 break;
1471 case cricket::kIceConnectionCompleted:
1472 LOG(LS_INFO) << "Changing to ICE completed state because "
1473 << "all transports are complete.";
1474 if (ice_connection_state_ !=
1475 PeerConnectionInterface::kIceConnectionConnected) {
1476 // If jumping directly from "checking" to "connected",
1477 // signal "connected" first.
1478 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1479 }
1480 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1481 if (metrics_observer_) {
1482 ReportTransportStats();
1483 }
1484 break;
1485 default:
nissec80e7412017-01-11 05:56:46 -08001486 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001487 }
deadbeefcbecd352015-09-23 11:50:27 -07001488}
1489
1490void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001491 SetIceConnectionReceiving(receiving);
1492}
1493
1494void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1495 if (ice_connection_receiving_ == receiving) {
1496 return;
1497 }
1498 ice_connection_receiving_ = receiving;
1499 if (ice_observer_) {
1500 ice_observer_->OnIceConnectionReceivingChange(receiving);
1501 }
1502}
1503
deadbeefcbecd352015-09-23 11:50:27 -07001504void WebRtcSession::OnTransportControllerCandidatesGathered(
1505 const std::string& transport_name,
1506 const cricket::Candidates& candidates) {
nisseede5da42017-01-12 05:15:36 -08001507 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001508 int sdp_mline_index;
1509 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1510 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1511 << transport_name << " not found";
1512 return;
1513 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514
deadbeefcbecd352015-09-23 11:50:27 -07001515 for (cricket::Candidates::const_iterator citer = candidates.begin();
1516 citer != candidates.end(); ++citer) {
1517 // Use transport_name as the candidate media id.
jbauch81bf7b02017-03-25 08:31:12 -07001518 std::unique_ptr<JsepIceCandidate> candidate(
1519 new JsepIceCandidate(transport_name, sdp_mline_index, *citer));
deadbeeffe4a8a42016-12-20 17:56:17 -08001520 if (local_description()) {
jbauch81bf7b02017-03-25 08:31:12 -07001521 mutable_local_description()->AddCandidate(candidate.get());
1522 }
1523 if (ice_observer_) {
1524 ice_observer_->OnIceCandidate(std::move(candidate));
deadbeefcbecd352015-09-23 11:50:27 -07001525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 }
1527}
1528
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001529void WebRtcSession::OnTransportControllerCandidatesRemoved(
1530 const std::vector<cricket::Candidate>& candidates) {
nisseede5da42017-01-12 05:15:36 -08001531 RTC_DCHECK(signaling_thread()->IsCurrent());
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001532 // Sanity check.
1533 for (const cricket::Candidate& candidate : candidates) {
1534 if (candidate.transport_name().empty()) {
1535 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1536 << "empty content name in candidate "
1537 << candidate.ToString();
1538 return;
1539 }
1540 }
1541
deadbeeffe4a8a42016-12-20 17:56:17 -08001542 if (local_description()) {
1543 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001544 }
1545 if (ice_observer_) {
1546 ice_observer_->OnIceCandidatesRemoved(candidates);
1547 }
1548}
1549
deadbeef953c2ce2017-01-09 14:53:41 -08001550void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1551 rtc::SSLHandshakeError error) {
1552 if (metrics_observer_) {
1553 metrics_observer_->IncrementEnumCounter(
1554 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1555 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1556 }
1557}
1558
Steve Anton169629a2017-08-30 17:36:36 -07001559// Enabling voice and video (and RTP data) channels.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560void WebRtcSession::EnableChannels() {
Steve Anton169629a2017-08-30 17:36:36 -07001561 for (cricket::VoiceChannel* voice_channel : voice_channels_) {
1562 if (!voice_channel->enabled()) {
1563 voice_channel->Enable(true);
1564 }
1565 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
Steve Anton169629a2017-08-30 17:36:36 -07001567 for (cricket::VideoChannel* video_channel : video_channels_) {
1568 if (!video_channel->enabled()) {
1569 video_channel->Enable(true);
1570 }
1571 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572
deadbeef953c2ce2017-01-09 14:53:41 -08001573 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1574 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575}
1576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577// Returns the media index for a local ice candidate given the content name.
1578bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1579 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001580 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001582 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583
1584 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001585 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 for (size_t index = 0; index < contents.size(); ++index) {
1587 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001588 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 content_found = true;
1590 break;
1591 }
1592 }
1593 return content_found;
1594}
1595
1596bool WebRtcSession::UseCandidatesInSessionDescription(
1597 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001598 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001600 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001602
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1604 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001605 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001606 const IceCandidateInterface* candidate = candidates->at(n);
1607 bool valid = false;
1608 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1609 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001610 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1611 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001612 }
1613 continue;
1614 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001615 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001616 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 break;
deadbeefd59daf82015-10-14 15:02:44 -07001618 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 }
1620 }
1621 return ret;
1622}
1623
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001624bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001626 size_t remote_content_size =
1627 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001629 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 return false;
1631 }
1632
1633 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001634 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 std::vector<cricket::Candidate> candidates;
1636 candidates.push_back(candidate->candidate());
1637 // Invoking BaseSession method to handle remote candidates.
1638 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001639 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1640 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 // Candidates successfully submitted for checking.
1642 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1643 ice_connection_state_ ==
1644 PeerConnectionInterface::kIceConnectionDisconnected) {
1645 // If state is New, then the session has just gotten its first remote ICE
1646 // candidates, so go to Checking.
1647 // If state is Disconnected, the session is re-using old candidates or
1648 // receiving additional ones, so go to Checking.
1649 // If state is Connected, stay Connected.
1650 // TODO(bemasc): If state is Connected, and the new candidates are for a
1651 // newly added transport, then the state actually _should_ move to
1652 // checking. Add a way to distinguish that case.
1653 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1654 }
1655 // TODO(bemasc): If state is Completed, go back to Connected.
1656 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001657 if (!error.empty()) {
1658 LOG(LS_WARNING) << error;
1659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 }
1661 return true;
1662}
1663
deadbeefcbecd352015-09-23 11:50:27 -07001664void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001665 // TODO(steveanton): Add support for multiple audio/video channels.
1666 // Destroy video channel first since it may have a pointer to the
1667 // voice channel.
1668 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
1669 if ((!video_info || video_info->rejected) && video_channel()) {
1670 RemoveAndDestroyVideoChannel(video_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 }
1672
Steve Anton169629a2017-08-30 17:36:36 -07001673 const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc);
1674 if ((!voice_info || voice_info->rejected) && voice_channel()) {
1675 RemoveAndDestroyVoiceChannel(voice_channel());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001676 }
1677
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 const cricket::ContentInfo* data_info =
1679 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001680 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001681 if (rtp_data_channel_) {
zhihuangf5b251b2017-01-12 19:37:48 -08001682 DestroyDataChannel();
deadbeef953c2ce2017-01-09 14:53:41 -08001683 }
1684 if (sctp_transport_) {
1685 SignalDataChannelDestroyed();
1686 network_thread_->Invoke<void>(
1687 RTC_FROM_HERE,
1688 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001689 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 }
1691}
1692
skvlad6c87a672016-05-17 17:49:52 -07001693// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1694// if the channel is not part of any bundle.
1695const std::string* WebRtcSession::GetBundleTransportName(
1696 const cricket::ContentInfo* content,
1697 const cricket::ContentGroup* bundle) {
1698 if (!bundle) {
1699 return nullptr;
1700 }
1701 const std::string* first_content_name = bundle->FirstContentName();
1702 if (!first_content_name) {
1703 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1704 return nullptr;
1705 }
1706 if (!bundle->HasContentName(content->name)) {
1707 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1708 return nullptr;
1709 }
1710 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1711 return first_content_name;
1712}
1713
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
Steve Anton169629a2017-08-30 17:36:36 -07001715 // TODO(steveanton): Add support for multiple audio/video channels.
skvlad6c87a672016-05-17 17:49:52 -07001716 const cricket::ContentGroup* bundle_group = nullptr;
1717 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1718 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1719 if (!bundle_group) {
1720 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1721 return false;
1722 }
1723 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 // Creating the media channels and transport proxies.
1725 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001726 if (voice && !voice->rejected && !voice_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001727 if (!CreateVoiceChannel(voice,
1728 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 LOG(LS_ERROR) << "Failed to create voice channel.";
1730 return false;
1731 }
1732 }
1733
1734 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
Steve Anton169629a2017-08-30 17:36:36 -07001735 if (video && !video->rejected && !video_channel()) {
skvlad6c87a672016-05-17 17:49:52 -07001736 if (!CreateVideoChannel(video,
1737 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 LOG(LS_ERROR) << "Failed to create video channel.";
1739 return false;
1740 }
1741 }
1742
1743 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001744 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1745 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001746 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 LOG(LS_ERROR) << "Failed to create data channel.";
1748 return false;
1749 }
1750 }
1751
1752 return true;
1753}
1754
skvlad6c87a672016-05-17 17:49:52 -07001755bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1756 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001757 // TODO(steveanton): Check to see if it's safe to create multiple voice
1758 // channels.
1759 RTC_DCHECK(voice_channels_.empty());
1760
skvlad6c87a672016-05-17 17:49:52 -07001761 bool require_rtcp_mux =
1762 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001763
1764 std::string transport_name =
1765 bundle_transport ? *bundle_transport : content->name;
1766
zhihuangb2cdd932017-01-19 16:54:25 -08001767 cricket::DtlsTransportInternal* rtp_dtls_transport =
1768 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001769 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001770 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001771 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001772 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001773 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1774 }
1775
Steve Anton169629a2017-08-30 17:36:36 -07001776 cricket::VoiceChannel* voice_channel = channel_manager_->CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -07001777 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001778 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001779 audio_options_);
1780 if (!voice_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001781 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001782 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001783 if (rtcp_dtls_transport) {
1784 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001785 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001786 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001787 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001788 }
zhihuangf5b251b2017-01-12 19:37:48 -08001789
Steve Anton169629a2017-08-30 17:36:36 -07001790 voice_channels_.push_back(voice_channel);
1791
1792 voice_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001793 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001794 voice_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001795 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001796
Steve Anton169629a2017-08-30 17:36:36 -07001797 // TODO(steveanton): This should signal which voice channel was created since
1798 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001799 SignalVoiceChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001800 voice_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001801 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802}
1803
skvlad6c87a672016-05-17 17:49:52 -07001804bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1805 const std::string* bundle_transport) {
Steve Anton169629a2017-08-30 17:36:36 -07001806 // TODO(steveanton): Check to see if it's safe to create multiple video
1807 // channels.
1808 RTC_DCHECK(video_channels_.empty());
1809
skvlad6c87a672016-05-17 17:49:52 -07001810 bool require_rtcp_mux =
1811 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001812
1813 std::string transport_name =
1814 bundle_transport ? *bundle_transport : content->name;
1815
zhihuangb2cdd932017-01-19 16:54:25 -08001816 cricket::DtlsTransportInternal* rtp_dtls_transport =
1817 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001818 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001819 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001820 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001821 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001822 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1823 }
1824
Steve Anton169629a2017-08-30 17:36:36 -07001825 cricket::VideoChannel* video_channel = channel_manager_->CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -07001826 call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport,
deadbeef1a2183d2017-02-10 23:44:49 -08001827 transport_controller_->signaling_thread(), content->name, SrtpRequired(),
Steve Anton169629a2017-08-30 17:36:36 -07001828 video_options_);
zhihuangf5b251b2017-01-12 19:37:48 -08001829
Steve Anton169629a2017-08-30 17:36:36 -07001830 if (!video_channel) {
zhihuangb2cdd932017-01-19 16:54:25 -08001831 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001832 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001833 if (rtcp_dtls_transport) {
1834 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001835 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001836 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001837 return false;
1838 }
zhihuangf5b251b2017-01-12 19:37:48 -08001839
Steve Anton169629a2017-08-30 17:36:36 -07001840 video_channels_.push_back(video_channel);
1841
1842 video_channel->SignalRtcpMuxFullyActive.connect(
deadbeefac22f702017-01-12 21:59:29 -08001843 this, &WebRtcSession::DestroyRtcpTransport_n);
Steve Anton169629a2017-08-30 17:36:36 -07001844 video_channel->SignalDtlsSrtpSetupFailure.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08001845 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001846
Steve Anton169629a2017-08-30 17:36:36 -07001847 // TODO(steveanton): This should signal which video channel was created since
1848 // we can have multiple.
deadbeefab9b2d12015-10-14 11:33:11 -07001849 SignalVideoChannelCreated();
Steve Anton169629a2017-08-30 17:36:36 -07001850 video_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001851 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852}
1853
skvlad6c87a672016-05-17 17:49:52 -07001854bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1855 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001856 const std::string transport_name =
1857 bundle_transport ? *bundle_transport : content->name;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001858 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001859 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001860 if (!sctp_factory_) {
1861 LOG(LS_ERROR)
1862 << "Trying to create SCTP transport, but didn't compile with "
1863 "SCTP support (HAVE_SCTP)";
1864 return false;
1865 }
1866 if (!network_thread_->Invoke<bool>(
1867 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1868 this, content->name, transport_name))) {
1869 return false;
1870 };
1871 } else {
1872 bool require_rtcp_mux =
1873 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
zhihuangf5b251b2017-01-12 19:37:48 -08001874
1875 std::string transport_name =
1876 bundle_transport ? *bundle_transport : content->name;
zhihuangb2cdd932017-01-19 16:54:25 -08001877 cricket::DtlsTransportInternal* rtp_dtls_transport =
1878 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001879 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001880 cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr;
deadbeefac22f702017-01-12 21:59:29 -08001881 if (!require_rtcp_mux) {
zhihuangb2cdd932017-01-19 16:54:25 -08001882 rtcp_dtls_transport = transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -08001883 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
1884 }
1885
deadbeef953c2ce2017-01-09 14:53:41 -08001886 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
nisseeaabdf62017-05-05 02:23:02 -07001887 media_config_, rtp_dtls_transport, rtcp_dtls_transport,
zhihuangf5b251b2017-01-12 19:37:48 -08001888 transport_controller_->signaling_thread(), content->name,
deadbeef1a2183d2017-02-10 23:44:49 -08001889 SrtpRequired()));
zhihuangf5b251b2017-01-12 19:37:48 -08001890
deadbeef953c2ce2017-01-09 14:53:41 -08001891 if (!rtp_data_channel_) {
zhihuangb2cdd932017-01-19 16:54:25 -08001892 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08001893 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangb2cdd932017-01-19 16:54:25 -08001894 if (rtcp_dtls_transport) {
1895 transport_controller_->DestroyDtlsTransport(
Steve Anton24efa722017-08-29 18:12:51 -07001896 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
deadbeefbad5dad2017-01-17 18:32:35 -08001897 }
deadbeef953c2ce2017-01-09 14:53:41 -08001898 return false;
1899 }
zhihuangf5b251b2017-01-12 19:37:48 -08001900
deadbeefac22f702017-01-12 21:59:29 -08001901 rtp_data_channel_->SignalRtcpMuxFullyActive.connect(
1902 this, &WebRtcSession::DestroyRtcpTransport_n);
deadbeef953c2ce2017-01-09 14:53:41 -08001903 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1904 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1905 rtp_data_channel_->SignalSentPacket.connect(this,
1906 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001907 }
1908
deadbeefab9b2d12015-10-14 11:33:11 -07001909 SignalDataChannelCreated();
zhihuangf5b251b2017-01-12 19:37:48 -08001910
wu@webrtc.org91053e72013-08-10 07:18:04 +00001911 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912}
1913
stefanf79ade12017-06-02 06:44:03 -07001914Call::Stats WebRtcSession::GetCallStats() {
1915 if (!worker_thread()->IsCurrent()) {
1916 return worker_thread()->Invoke<Call::Stats>(
1917 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this));
1918 }
zhihuang38ede132017-06-15 12:52:32 -07001919 if (!call_)
1920 return Call::Stats();
stefanf79ade12017-06-02 06:44:03 -07001921 return call_->GetStats();
1922}
1923
hbosdf6075a2016-12-19 04:58:02 -08001924std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1925 const ChannelNamePairs& channel_name_pairs) {
nisseede5da42017-01-12 05:15:36 -08001926 RTC_DCHECK(network_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -08001927 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1928 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1929 &channel_name_pairs.video,
1930 &channel_name_pairs.data }) {
1931 if (*channel_name_pair) {
1932 cricket::TransportStats transport_stats;
1933 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1934 &transport_stats)) {
1935 return nullptr;
1936 }
1937 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1938 (*channel_name_pair)->transport_name;
1939 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1940 std::move(transport_stats);
1941 }
1942 }
1943 return session_stats;
1944}
1945
deadbeef953c2ce2017-01-09 14:53:41 -08001946bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1947 const std::string& transport_name) {
1948 RTC_DCHECK(network_thread_->IsCurrent());
1949 RTC_DCHECK(sctp_factory_);
zhihuangb2cdd932017-01-19 16:54:25 -08001950 cricket::DtlsTransportInternal* tc =
1951 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001952 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1953 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1954 RTC_DCHECK(sctp_transport_);
1955 sctp_invoker_.reset(new rtc::AsyncInvoker());
1956 sctp_transport_->SignalReadyToSendData.connect(
1957 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1958 sctp_transport_->SignalDataReceived.connect(
1959 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1960 sctp_transport_->SignalStreamClosedRemotely.connect(
1961 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1962 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1963 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1964 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965}
1966
deadbeef953c2ce2017-01-09 14:53:41 -08001967void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1968 RTC_DCHECK(network_thread_->IsCurrent());
1969 RTC_DCHECK(sctp_transport_);
1970 RTC_DCHECK(sctp_transport_name_);
1971 std::string old_sctp_transport_name = *sctp_transport_name_;
1972 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
zhihuangb2cdd932017-01-19 16:54:25 -08001973 cricket::DtlsTransportInternal* tc =
1974 transport_controller_->CreateDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001975 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1976 sctp_transport_->SetTransportChannel(tc);
zhihuangb2cdd932017-01-19 16:54:25 -08001977 transport_controller_->DestroyDtlsTransport_n(
deadbeef953c2ce2017-01-09 14:53:41 -08001978 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1979}
1980
1981void WebRtcSession::DestroySctpTransport_n() {
1982 RTC_DCHECK(network_thread_->IsCurrent());
1983 sctp_transport_.reset(nullptr);
1984 sctp_content_name_.reset();
1985 sctp_transport_name_.reset();
1986 sctp_invoker_.reset(nullptr);
1987 sctp_ready_to_send_data_ = false;
1988}
1989
1990void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1991 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1992 RTC_DCHECK(network_thread_->IsCurrent());
1993 sctp_invoker_->AsyncInvoke<void>(
1994 RTC_FROM_HERE, signaling_thread_,
1995 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1996}
1997
1998void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1999 RTC_DCHECK(signaling_thread_->IsCurrent());
2000 sctp_ready_to_send_data_ = ready;
2001 SignalSctpReadyToSendData(ready);
2002}
2003
2004void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002005 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002006 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07002007 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08002008 RTC_DCHECK(network_thread_->IsCurrent());
2009 sctp_invoker_->AsyncInvoke<void>(
2010 RTC_FROM_HERE, signaling_thread_,
2011 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
2012 payload));
2013}
2014
2015void WebRtcSession::OnSctpTransportDataReceived_s(
2016 const cricket::ReceiveDataParams& params,
2017 const rtc::CopyOnWriteBuffer& payload) {
2018 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07002019 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
2020 // Received OPEN message; parse and signal that a new data channel should
2021 // be created.
2022 std::string label;
2023 InternalDataChannelInit config;
2024 config.id = params.ssrc;
2025 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
2026 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
2027 << params.ssrc;
2028 return;
2029 }
2030 config.open_handshake_role = InternalDataChannelInit::kAcker;
2031 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08002032 } else {
2033 // Otherwise just forward the signal.
2034 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 }
deadbeef953c2ce2017-01-09 14:53:41 -08002036}
2037
2038void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
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(&sigslot::signal1<int>::operator(),
2044 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045}
2046
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002047// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002048bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002049 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
2050 if (!bundle_enabled)
2051 return true;
2052
2053 const cricket::ContentGroup* bundle_group =
2054 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -08002055 RTC_DCHECK(bundle_group != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002056
2057 const cricket::ContentInfos& contents = desc->contents();
2058 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2059 citer != contents.end(); ++citer) {
2060 const cricket::ContentInfo* content = (&*citer);
nisseede5da42017-01-12 05:15:36 -08002061 RTC_DCHECK(content != NULL);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002062 if (bundle_group->HasContentName(content->name) &&
2063 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2064 if (!HasRtcpMuxEnabled(content))
2065 return false;
2066 }
2067 }
2068 // RTCP-MUX is enabled in all the contents.
2069 return true;
2070}
2071
2072bool WebRtcSession::HasRtcpMuxEnabled(
2073 const cricket::ContentInfo* content) {
2074 const cricket::MediaContentDescription* description =
2075 static_cast<cricket::MediaContentDescription*>(content->description);
2076 return description->rtcp_mux();
2077}
2078
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002079bool WebRtcSession::ValidateSessionDescription(
2080 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002081 cricket::ContentSource source, std::string* err_desc) {
2082 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002083 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002084 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002085 }
2086
2087 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002088 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002089 }
2090
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002091 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002092 Action action = GetAction(sdesc->type());
2093 if (source == cricket::CS_LOCAL) {
2094 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002095 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096 } else {
2097 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002098 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002099 }
2100
2101 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002102 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002103 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2104 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002105 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002106 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002107 }
2108
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002109 // Verify ice-ufrag and ice-pwd.
2110 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002111 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002112 }
2113
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002114 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002115 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002116 }
2117
skvlad6c87a672016-05-17 17:49:52 -07002118 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2119 // m-lines that do not rtcp-mux enabled.
2120
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002121 // Verify m-lines in Answer when compared against Offer.
Zhi Huang2a5e4262017-09-14 01:15:03 -07002122 if (action == kAnswer || action == kPrAnswer) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002123 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002124 (source == cricket::CS_LOCAL) ? remote_description()->description()
2125 : local_description()->description();
Tommi589ae452017-10-15 21:20:46 +00002126 if (!MediaSectionsHaveSameCount(sdesc->description(), offer_desc) ||
2127 !MediaSectionsInSameOrder(sdesc->description(), offer_desc)) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002128 return BadAnswerSdp(source, kMlineMismatchInAnswer, err_desc);
2129 }
2130 } else {
Tommi589ae452017-10-15 21:20:46 +00002131 // The re-offers should respect the order of m= sections in current local
Zhi Huang2a5e4262017-09-14 01:15:03 -07002132 // description. See RFC3264 Section 8 paragraph 4 for more details.
Tommi589ae452017-10-15 21:20:46 +00002133 if (local_description() &&
2134 !MediaSectionsInSameOrder(sdesc->description(),
2135 local_description()->description())) {
Zhi Huang2a5e4262017-09-14 01:15:03 -07002136 return BadOfferSdp(source, kMlineMismatchInSubsequentOffer, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002137 }
2138 }
2139
2140 return true;
2141}
2142
2143bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2144 return ((action == kOffer && state() == STATE_INIT) ||
2145 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002146 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002147 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002148 (action == kOffer && state() == STATE_INPROGRESS) ||
2149 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002150 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2151 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2152 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2153 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002154}
2155
2156bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2157 return ((action == kOffer && state() == STATE_INIT) ||
2158 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002159 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002160 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002161 (action == kOffer && state() == STATE_INPROGRESS) ||
2162 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002163 (action == kAnswer && state() == STATE_SENTOFFER) ||
2164 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2165 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2166 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002167}
2168
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002169std::string WebRtcSession::GetSessionErrorMsg() {
2170 std::ostringstream desc;
2171 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2172 desc << kSessionErrorDesc << error_desc() << ".";
2173 return desc.str();
2174}
2175
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002176// We need to check the local/remote description for the Transport instead of
2177// the session, because a new Transport added during renegotiation may have
2178// them unset while the session has them set from the previous negotiation.
2179// Not doing so may trigger the auto generation of transport description and
2180// mess up DTLS identity information, ICE credential, etc.
2181bool WebRtcSession::ReadyToUseRemoteCandidate(
2182 const IceCandidateInterface* candidate,
2183 const SessionDescriptionInterface* remote_desc,
2184 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002185 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002186
2187 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002188 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002189
deadbeefd59daf82015-10-14 15:02:44 -07002190 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002191 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002192 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002193
2194 size_t mediacontent_index =
2195 static_cast<size_t>(candidate->sdp_mline_index());
2196 size_t remote_content_size =
2197 current_remote_desc->description()->contents().size();
2198 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002199 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2200 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002201
2202 *valid = false;
2203 return false;
2204 }
2205
2206 cricket::ContentInfo content =
2207 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002208
2209 const std::string transport_name = GetTransportName(content.name);
2210 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002211 return false;
2212 }
zhihuang9763d562016-08-05 11:14:50 -07002213 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002214}
2215
deadbeef7af91dd2016-12-13 11:29:11 -08002216bool WebRtcSession::SrtpRequired() const {
2217 return dtls_enabled_ ||
2218 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2219}
2220
deadbeefcbecd352015-09-23 11:50:27 -07002221void WebRtcSession::OnTransportControllerGatheringState(
2222 cricket::IceGatheringState state) {
nisseede5da42017-01-12 05:15:36 -08002223 RTC_DCHECK(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07002224 if (state == cricket::kIceGatheringGathering) {
2225 if (ice_observer_) {
2226 ice_observer_->OnIceGatheringChange(
2227 PeerConnectionInterface::kIceGatheringGathering);
2228 }
2229 } else if (state == cricket::kIceGatheringComplete) {
2230 if (ice_observer_) {
2231 ice_observer_->OnIceGatheringChange(
2232 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002233 }
2234 }
2235}
2236
2237void WebRtcSession::ReportTransportStats() {
2238 // Use a set so we don't report the same stats twice if two channels share
2239 // a transport.
2240 std::set<std::string> transport_names;
2241 if (voice_channel()) {
2242 transport_names.insert(voice_channel()->transport_name());
2243 }
2244 if (video_channel()) {
2245 transport_names.insert(video_channel()->transport_name());
2246 }
deadbeef953c2ce2017-01-09 14:53:41 -08002247 if (rtp_data_channel()) {
2248 transport_names.insert(rtp_data_channel()->transport_name());
2249 }
2250 if (sctp_transport_name_) {
2251 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002252 }
2253 for (const auto& name : transport_names) {
2254 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002255 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002256 ReportBestConnectionState(stats);
2257 ReportNegotiatedCiphers(stats);
2258 }
2259 }
2260}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002261// Walk through the ConnectionInfos to gather best connection usage
2262// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002263void WebRtcSession::ReportBestConnectionState(
2264 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002265 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002266 for (cricket::TransportChannelStatsList::const_iterator it =
2267 stats.channel_stats.begin();
2268 it != stats.channel_stats.end(); ++it) {
2269 for (cricket::ConnectionInfos::const_iterator it_info =
2270 it->connection_infos.begin();
2271 it_info != it->connection_infos.end(); ++it_info) {
2272 if (!it_info->best_connection) {
2273 continue;
2274 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002275
2276 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2277 const cricket::Candidate& local = it_info->local_candidate;
2278 const cricket::Candidate& remote = it_info->remote_candidate;
2279
2280 // Increment the counter for IceCandidatePairType.
2281 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2282 (local.type() == RELAY_PORT_TYPE &&
2283 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2284 type = kEnumCounterIceCandidatePairTypeTcp;
2285 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2286 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002287 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002288 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002289 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002290 metrics_observer_->IncrementEnumCounter(
2291 type, GetIceCandidatePairCounter(local, remote),
2292 kIceCandidatePairMax);
2293
2294 // Increment the counter for IP type.
2295 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002296 metrics_observer_->IncrementEnumCounter(
2297 kEnumCounterAddressFamily, kBestConnections_IPv4,
2298 kPeerConnectionAddressFamilyCounter_Max);
2299
2300 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002301 metrics_observer_->IncrementEnumCounter(
2302 kEnumCounterAddressFamily, kBestConnections_IPv6,
2303 kPeerConnectionAddressFamilyCounter_Max);
2304 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002305 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002306 }
2307
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002308 return;
2309 }
2310 }
2311}
2312
jbauchac8869e2015-07-03 01:36:14 -07002313void WebRtcSession::ReportNegotiatedCiphers(
2314 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002315 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002316 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2317 return;
2318 }
2319
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002320 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2321 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2322 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2323 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002324 return;
2325 }
2326
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002327 PeerConnectionEnumCounterType srtp_counter_type;
2328 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002329 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002330 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2331 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002332 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002333 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2334 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002335 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002336 srtp_counter_type = kEnumCounterDataSrtpCipher;
2337 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002338 } else {
2339 RTC_NOTREACHED();
2340 return;
2341 }
2342
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002343 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2344 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2345 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002346 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002347 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2348 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2349 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002350 }
2351}
2352
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002353void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002354 RTC_DCHECK(worker_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -07002355 RTC_DCHECK(call_);
nisseeaabdf62017-05-05 02:23:02 -07002356 call_->OnSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -07002357}
2358
zhihuang9763d562016-08-05 11:14:50 -07002359const std::string WebRtcSession::GetTransportName(
2360 const std::string& content_name) {
2361 cricket::BaseChannel* channel = GetChannel(content_name);
2362 if (!channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08002363 if (sctp_transport_) {
2364 RTC_DCHECK(sctp_content_name_);
2365 RTC_DCHECK(sctp_transport_name_);
2366 if (content_name == *sctp_content_name_) {
2367 return *sctp_transport_name_;
2368 }
2369 }
zhihuang9763d562016-08-05 11:14:50 -07002370 // Return an empty string if failed to retrieve the transport name.
2371 return "";
2372 }
2373 return channel->transport_name();
2374}
zhihuangd82eee02016-08-26 11:25:05 -07002375
deadbeefac22f702017-01-12 21:59:29 -08002376void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) {
nissea9dd4a12017-01-13 07:08:34 -08002377 RTC_DCHECK(network_thread()->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -08002378 transport_controller_->DestroyDtlsTransport_n(
zhihuangf5b251b2017-01-12 19:37:48 -08002379 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
2380}
2381
Steve Anton169629a2017-08-30 17:36:36 -07002382void WebRtcSession::RemoveAndDestroyVideoChannel(
2383 cricket::VideoChannel* video_channel) {
2384 auto it =
2385 std::find(video_channels_.begin(), video_channels_.end(), video_channel);
2386 RTC_DCHECK(it != video_channels_.end());
2387 if (it == video_channels_.end()) {
2388 return;
2389 }
2390 video_channels_.erase(it);
2391 DestroyVideoChannel(video_channel);
2392}
2393
2394void WebRtcSession::DestroyVideoChannel(cricket::VideoChannel* video_channel) {
2395 // TODO(steveanton): This should take an identifier for the video channel
2396 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002397 SignalVideoChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002398 RTC_DCHECK(video_channel->rtp_dtls_transport());
2399 const std::string transport_name =
2400 video_channel->rtp_dtls_transport()->transport_name();
2401 const bool need_to_delete_rtcp =
2402 (video_channel->rtcp_dtls_transport() != nullptr);
2403 // The above need to be cached before destroying the video channel so that we
2404 // do not access uninitialized memory.
2405 channel_manager_->DestroyVideoChannel(video_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002406 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002407 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002408 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002409 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002410 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002411 }
2412}
2413
Steve Anton169629a2017-08-30 17:36:36 -07002414void WebRtcSession::RemoveAndDestroyVoiceChannel(
2415 cricket::VoiceChannel* voice_channel) {
2416 auto it =
2417 std::find(voice_channels_.begin(), voice_channels_.end(), voice_channel);
2418 RTC_DCHECK(it != voice_channels_.end());
2419 if (it == voice_channels_.end()) {
2420 return;
2421 }
2422 voice_channels_.erase(it);
2423 DestroyVoiceChannel(voice_channel);
2424}
2425
2426void WebRtcSession::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) {
2427 // TODO(steveanton): This should take an identifier for the voice channel
2428 // since we now support more than one.
zhihuangf5b251b2017-01-12 19:37:48 -08002429 SignalVoiceChannelDestroyed();
Steve Anton169629a2017-08-30 17:36:36 -07002430 RTC_DCHECK(voice_channel->rtp_dtls_transport());
2431 const std::string transport_name =
2432 voice_channel->rtp_dtls_transport()->transport_name();
2433 const bool need_to_delete_rtcp =
2434 (voice_channel->rtcp_dtls_transport() != nullptr);
2435 // The above need to be cached before destroying the video channel so that we
2436 // do not access uninitialized memory.
2437 channel_manager_->DestroyVoiceChannel(voice_channel);
zhihuangb2cdd932017-01-19 16:54:25 -08002438 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002439 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002440 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002441 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002442 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002443 }
2444}
2445
2446void WebRtcSession::DestroyDataChannel() {
2447 SignalDataChannelDestroyed();
zhihuangb2cdd932017-01-19 16:54:25 -08002448 RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport());
zhihuangf5b251b2017-01-12 19:37:48 -08002449 std::string transport_name;
zhihuangb2cdd932017-01-19 16:54:25 -08002450 transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name();
2451 bool need_to_delete_rtcp =
2452 (rtp_data_channel_->rtcp_dtls_transport() != nullptr);
zhihuangf5b251b2017-01-12 19:37:48 -08002453 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
zhihuangb2cdd932017-01-19 16:54:25 -08002454 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002455 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
zhihuangf5b251b2017-01-12 19:37:48 -08002456 if (need_to_delete_rtcp) {
zhihuangb2cdd932017-01-19 16:54:25 -08002457 transport_controller_->DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -08002458 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
zhihuangf5b251b2017-01-12 19:37:48 -08002459 }
2460}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461} // namespace webrtc