blob: e662896fadf763a807fe9887f3e489a57dcf37ce [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_sink.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000026#include "webrtc/base/basictypes.h"
zhihuang9763d562016-08-05 11:14:50 -070027#include "webrtc/base/bind.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
ossuf515ab82016-12-07 04:52:58 -080033#include "webrtc/call/call.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/videocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080036#include "webrtc/media/sctp/sctptransportinternal.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000037#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070038#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010039#include "webrtc/pc/channel.h"
40#include "webrtc/pc/channelmanager.h"
41#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
zhihuang9763d562016-08-05 11:14:50 -070043#ifdef HAVE_QUIC
44#include "webrtc/p2p/quic/quictransportchannel.h"
45#endif // HAVE_QUIC
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047using cricket::ContentInfo;
48using cricket::ContentInfos;
49using cricket::MediaContentDescription;
50using cricket::SessionDescription;
51using cricket::TransportInfo;
52
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070053using cricket::LOCAL_PORT_TYPE;
54using cricket::STUN_PORT_TYPE;
55using cricket::RELAY_PORT_TYPE;
56using cricket::PRFLX_PORT_TYPE;
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058namespace webrtc {
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000061const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
62 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064const char kInvalidCandidates[] = "Description contains invalid candidates.";
65const char kInvalidSdp[] = "Invalid session description.";
66const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000067 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
68const char kPushDownTDFailed[] =
69 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070const char kSdpWithoutDtlsFingerprint[] =
71 "Called with SDP without DTLS fingerprint.";
72const char kSdpWithoutSdesCrypto[] =
73 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000074const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000075 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077const char kSessionErrorDesc[] = "Session error description: ";
deadbeef953c2ce2017-01-09 14:53:41 -080078const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef953c2ce2017-01-09 14:53:41 -080080const char kDtlsSrtpSetupFailureRtcp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000081 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070082const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070084IceCandidatePairType GetIceCandidatePairCounter(
85 const cricket::Candidate& local,
86 const cricket::Candidate& remote) {
87 const auto& l = local.type();
88 const auto& r = remote.type();
89 const auto& host = LOCAL_PORT_TYPE;
90 const auto& srflx = STUN_PORT_TYPE;
91 const auto& relay = RELAY_PORT_TYPE;
92 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070093 if (l == host && r == host) {
94 bool local_private = IPIsPrivate(local.address().ipaddr());
95 bool remote_private = IPIsPrivate(remote.address().ipaddr());
96 if (local_private) {
97 if (remote_private) {
98 return kIceCandidatePairHostPrivateHostPrivate;
99 } else {
100 return kIceCandidatePairHostPrivateHostPublic;
101 }
102 } else {
103 if (remote_private) {
104 return kIceCandidatePairHostPublicHostPrivate;
105 } else {
106 return kIceCandidatePairHostPublicHostPublic;
107 }
108 }
109 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700110 if (l == host && r == srflx)
111 return kIceCandidatePairHostSrflx;
112 if (l == host && r == relay)
113 return kIceCandidatePairHostRelay;
114 if (l == host && r == prflx)
115 return kIceCandidatePairHostPrflx;
116 if (l == srflx && r == host)
117 return kIceCandidatePairSrflxHost;
118 if (l == srflx && r == srflx)
119 return kIceCandidatePairSrflxSrflx;
120 if (l == srflx && r == relay)
121 return kIceCandidatePairSrflxRelay;
122 if (l == srflx && r == prflx)
123 return kIceCandidatePairSrflxPrflx;
124 if (l == relay && r == host)
125 return kIceCandidatePairRelayHost;
126 if (l == relay && r == srflx)
127 return kIceCandidatePairRelaySrflx;
128 if (l == relay && r == relay)
129 return kIceCandidatePairRelayRelay;
130 if (l == relay && r == prflx)
131 return kIceCandidatePairRelayPrflx;
132 if (l == prflx && r == host)
133 return kIceCandidatePairPrflxHost;
134 if (l == prflx && r == srflx)
135 return kIceCandidatePairPrflxSrflx;
136 if (l == prflx && r == relay)
137 return kIceCandidatePairPrflxRelay;
138 return kIceCandidatePairMax;
139}
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141// Compares |answer| against |offer|. Comparision is done
142// for number of m-lines in answer against offer. If matches true will be
143// returned otherwise false.
144static bool VerifyMediaDescriptions(
145 const SessionDescription* answer, const SessionDescription* offer) {
146 if (offer->contents().size() != answer->contents().size())
147 return false;
148
149 for (size_t i = 0; i < offer->contents().size(); ++i) {
150 if ((offer->contents()[i].name) != answer->contents()[i].name) {
151 return false;
152 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000153 const MediaContentDescription* offer_mdesc =
154 static_cast<const MediaContentDescription*>(
155 offer->contents()[i].description);
156 const MediaContentDescription* answer_mdesc =
157 static_cast<const MediaContentDescription*>(
158 answer->contents()[i].description);
159 if (offer_mdesc->type() != answer_mdesc->type()) {
160 return false;
161 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 }
163 return true;
164}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166// Checks that each non-rejected content has SDES crypto keys or a DTLS
167// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
168// keys, will be caught in Transport negotiation, and backstopped by Channel's
deadbeef7af91dd2016-12-13 11:29:11 -0800169// |srtp_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000170static bool VerifyCrypto(const SessionDescription* desc,
171 bool dtls_enabled,
172 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 const ContentInfos& contents = desc->contents();
174 for (size_t index = 0; index < contents.size(); ++index) {
175 const ContentInfo* cinfo = &contents[index];
176 if (cinfo->rejected) {
177 continue;
178 }
179
180 // If the content isn't rejected, crypto must be present.
181 const MediaContentDescription* media =
182 static_cast<const MediaContentDescription*>(cinfo->description);
183 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
184 if (!media || !tinfo) {
185 // Something is not right.
186 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000187 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 return false;
189 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000190 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000191 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 LOG(LS_WARNING) <<
193 "Session description must have DTLS fingerprint if DTLS enabled.";
194 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000195 return false;
196 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000197 } else {
198 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 LOG(LS_WARNING) <<
200 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 return false;
203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 }
205 }
206
207 return true;
208}
209
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000210// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
211static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
212 const ContentInfos& contents = desc->contents();
213 for (size_t index = 0; index < contents.size(); ++index) {
214 const ContentInfo* cinfo = &contents[index];
215 if (cinfo->rejected) {
216 continue;
217 }
218
219 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
220 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
221 if (!tinfo) {
222 // Something is not right.
223 LOG(LS_ERROR) << kInvalidSdp;
224 return false;
225 }
226 if (tinfo->description.ice_ufrag.empty() ||
227 tinfo->description.ice_pwd.empty()) {
228 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
229 return false;
230 }
231 }
232 return true;
233}
234
Peter Boström0c4e06b2015-10-07 12:23:21 +0200235static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
236 const std::string& track_id,
237 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 const cricket::ContentInfo* audio_info =
239 cricket::GetFirstAudioContent(session_description);
240 if (!audio_info) {
241 LOG(LS_ERROR) << "Audio not used in this call";
242 return false;
243 }
244
245 const cricket::MediaContentDescription* audio_content =
246 static_cast<const cricket::MediaContentDescription*>(
247 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000248 const cricket::StreamParams* stream =
249 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
250 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 return false;
252 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000253
254 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 return true;
256}
257
258static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 uint32_t ssrc,
260 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 ASSERT(track_id != NULL);
262
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 const cricket::ContentInfo* audio_info =
264 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000265 if (audio_info) {
266 const cricket::MediaContentDescription* audio_content =
267 static_cast<const cricket::MediaContentDescription*>(
268 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000270 const auto* found =
271 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
272 if (found) {
273 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000274 return true;
275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 }
277
278 const cricket::ContentInfo* video_info =
279 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000280 if (video_info) {
281 const cricket::MediaContentDescription* video_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000285 const auto* found =
286 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
287 if (found) {
288 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000289 return true;
290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292 return false;
293}
294
deadbeef953c2ce2017-01-09 14:53:41 -0800295// Get the SCTP port out of a SessionDescription.
296// Return -1 if not found.
297static int GetSctpPort(const SessionDescription* session_description) {
298 const ContentInfo* content_info = GetFirstDataContent(session_description);
299 RTC_DCHECK(content_info);
300 if (!content_info) {
301 return -1;
302 }
303 const cricket::DataContentDescription* data =
304 static_cast<const cricket::DataContentDescription*>(
305 (content_info->description));
306 std::string value;
307 cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType,
308 cricket::kGoogleSctpDataCodecName);
309 for (const cricket::DataCodec& codec : data->codecs()) {
310 if (!codec.Matches(match_pattern)) {
311 continue;
312 }
313 if (codec.GetParam(cricket::kCodecParamPort, &value)) {
314 return rtc::FromString<int>(value);
315 }
316 }
317 return -1;
318}
319
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000320static bool BadSdp(const std::string& source,
321 const std::string& type,
322 const std::string& reason,
323 std::string* err_desc) {
324 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700325 desc << "Failed to set " << source;
326 if (!type.empty()) {
327 desc << " " << type;
328 }
329 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000334 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 return false;
336}
337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 const std::string& type,
340 const std::string& reason,
341 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 }
347}
348
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000349static bool BadLocalSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
353}
354
355static bool BadRemoteSdp(const std::string& type,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
359}
360
361static bool BadOfferSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
365}
366
367static bool BadPranswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
371 reason, err_desc);
372}
373
374static bool BadAnswerSdp(cricket::ContentSource source,
375 const std::string& reason,
376 std::string* err_desc) {
377 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378}
379
deadbeefd59daf82015-10-14 15:02:44 -0700380#define GET_STRING_OF_STATE(state) \
381 case webrtc::WebRtcSession::state: \
382 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 break;
384
deadbeefd59daf82015-10-14 15:02:44 -0700385static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 std::string result;
387 switch (state) {
388 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700389 GET_STRING_OF_STATE(STATE_SENTOFFER)
390 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
391 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
392 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700394 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 default:
nissec80e7412017-01-11 05:56:46 -0800396 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 break;
398 }
399 return result;
400}
401
deadbeefd59daf82015-10-14 15:02:44 -0700402#define GET_STRING_OF_ERROR_CODE(err) \
403 case webrtc::WebRtcSession::err: \
404 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 break;
406
deadbeefd59daf82015-10-14 15:02:44 -0700407static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 std::string result;
409 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000410 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000411 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
412 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800414 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 break;
416 }
417 return result;
418}
419
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420static std::string MakeErrorString(const std::string& error,
421 const std::string& desc) {
422 std::ostringstream ret;
423 ret << error << " " << desc;
424 return ret.str();
425}
426
427static std::string MakeTdErrorString(const std::string& desc) {
428 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429}
430
deadbeef0ed85b22016-02-23 17:24:52 -0800431// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
432bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
433 const SessionDescriptionInterface* new_desc,
434 const std::string& content_name) {
435 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700436 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
deadbeef0ed85b22016-02-23 17:24:52 -0800438 const SessionDescription* new_sd = new_desc->description();
439 const SessionDescription* old_sd = old_desc->description();
440 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
441 if (!cinfo || cinfo->rejected) {
442 return false;
443 }
444 // If the content isn't rejected, check if ufrag and password has changed.
445 const cricket::TransportDescription* new_transport_desc =
446 new_sd->GetTransportDescriptionByName(content_name);
447 const cricket::TransportDescription* old_transport_desc =
448 old_sd->GetTransportDescriptionByName(content_name);
449 if (!new_transport_desc || !old_transport_desc) {
450 // No transport description exists. This is not an ICE restart.
451 return false;
452 }
453 if (cricket::IceCredentialsChanged(
454 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
455 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
456 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
457 << ".";
458 return true;
459 }
460 return false;
461}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
zhihuang29ff8442016-07-27 11:07:25 -0700463WebRtcSession::WebRtcSession(
464 webrtc::MediaControllerInterface* media_controller,
465 rtc::Thread* network_thread,
466 rtc::Thread* worker_thread,
467 rtc::Thread* signaling_thread,
468 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800469 std::unique_ptr<cricket::TransportController> transport_controller,
470 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory)
zhihuang9763d562016-08-05 11:14:50 -0700471 : network_thread_(network_thread),
472 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700473 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 // RFC 3264: The numeric value of the session id and version in the
475 // o line MUST be representable with a "64 bit signed integer".
476 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700477 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700478 transport_controller_(std::move(transport_controller)),
deadbeef953c2ce2017-01-09 14:53:41 -0800479 sctp_factory_(std::move(sctp_factory)),
stefanc1aeaf02015-10-15 07:26:07 -0700480 media_controller_(media_controller),
481 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 ice_observer_(NULL),
483 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700484 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000486 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000488 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700489 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
490 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700491 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700492 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700493 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700494 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700495 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700496 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700497 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700498 transport_controller_->SignalCandidatesRemoved.connect(
499 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700500 transport_controller_->SignalDtlsHandshakeError.connect(
deadbeef953c2ce2017-01-09 14:53:41 -0800501 this, &WebRtcSession::OnTransportControllerDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502}
503
504WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700505 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000506 // Destroy video_channel_ first since it may have a pointer to the
507 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000508 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 SignalVideoChannelDestroyed();
510 channel_manager_->DestroyVideoChannel(video_channel_.release());
511 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000512 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000513 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200514 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000515 }
deadbeef953c2ce2017-01-09 14:53:41 -0800516 if (rtp_data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 SignalDataChannelDestroyed();
deadbeef953c2ce2017-01-09 14:53:41 -0800518 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
519 }
520 if (sctp_transport_) {
521 SignalDataChannelDestroyed();
522 network_thread_->Invoke<void>(
523 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
zhihuang9763d562016-08-05 11:14:50 -0700525#ifdef HAVE_QUIC
526 if (quic_data_transport_) {
527 quic_data_transport_.reset();
528 }
529#endif
deadbeef057ecf02016-01-20 14:30:43 -0800530 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700531
532 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533}
534
wu@webrtc.org91053e72013-08-10 07:18:04 +0000535bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200537 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200538 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
539 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700540 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700541 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000542
Henrik Boström87713d02015-08-25 09:53:21 +0200543 // Obtain a certificate from RTCConfiguration if any were provided (optional).
544 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
545 if (!rtc_configuration.certificates.empty()) {
546 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
547 // just picking the first one. The decision should be made based on the DTLS
548 // handshake. The DTLS negotiations need to know about all certificates.
549 certificate = rtc_configuration.certificates[0];
550 }
551
honghaiz1f429e32015-09-28 07:57:34 -0700552 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700553
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000554 if (options.disable_encryption) {
555 dtls_enabled_ = false;
556 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200557 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200558 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800559 // |rtc_configuration| can override the default |dtls_enabled_| value.
560 if (rtc_configuration.enable_dtls_srtp) {
561 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000562 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000563 }
564
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000566 // It takes precendence over the disable_sctp_data_channels
567 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800568 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700570 }
571#ifdef HAVE_QUIC
572 else if (rtc_configuration.enable_quic) {
573 // Use QUIC instead of DTLS when |enable_quic| is true.
574 data_channel_type_ = cricket::DCT_QUIC;
575 transport_controller_->use_quic();
576 if (dtls_enabled_) {
577 LOG(LS_INFO) << "Using QUIC instead of DTLS";
578 }
579 quic_data_transport_.reset(
580 new QuicDataTransport(signaling_thread(), worker_thread(),
581 network_thread(), transport_controller_.get()));
582 }
583#endif // HAVE_QUIC
584 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000585 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000586 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000587 data_channel_type_ = cricket::DCT_SCTP;
588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
htaa2a49d92016-03-04 02:51:39 -0800591 video_options_.screencast_min_bitrate_kbps =
592 rtc_configuration.screencast_min_bitrate;
593 audio_options_.combined_audio_video_bwe =
594 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000595
kwiberg102c6a62015-10-30 02:47:38 -0700596 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100597 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200598
Karl Wibergbe579832015-11-10 22:34:18 +0100599 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
600 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200601
Henrik Boström87713d02015-08-25 09:53:21 +0200602 if (!dtls_enabled_) {
603 // Construct with DTLS disabled.
604 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200605 signaling_thread(), channel_manager_, this, id(),
606 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200607 } else {
608 // Construct with DTLS enabled.
609 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200610 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200611 signaling_thread(), channel_manager_, this, id(),
612 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200613 } else {
614 // Use the already generated certificate.
615 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200616 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200617 }
618 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000619
Henrik Boströmd8281982015-08-27 10:12:24 +0200620 webrtc_session_desc_factory_->SignalCertificateReady.connect(
621 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000622
wu@webrtc.org97077a32013-10-25 21:18:33 +0000623 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000625 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700626
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 return true;
628}
629
deadbeefd59daf82015-10-14 15:02:44 -0700630void WebRtcSession::Close() {
631 SetState(STATE_CLOSED);
632 RemoveUnusedChannels(nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800633 RTC_DCHECK(!voice_channel_);
634 RTC_DCHECK(!video_channel_);
635 RTC_DCHECK(!rtp_data_channel_);
636 RTC_DCHECK(!sctp_transport_);
solenberg03d6d572016-03-01 12:42:03 -0800637 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638}
639
deadbeef0ed85b22016-02-23 17:24:52 -0800640cricket::BaseChannel* WebRtcSession::GetChannel(
641 const std::string& content_name) {
642 if (voice_channel() && voice_channel()->content_name() == content_name) {
643 return voice_channel();
644 }
645 if (video_channel() && video_channel()->content_name() == content_name) {
646 return video_channel();
647 }
deadbeef953c2ce2017-01-09 14:53:41 -0800648 if (rtp_data_channel() &&
649 rtp_data_channel()->content_name() == content_name) {
650 return rtp_data_channel();
deadbeef0ed85b22016-02-23 17:24:52 -0800651 }
652 return nullptr;
653}
654
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000655cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
656 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657}
658
deadbeef953c2ce2017-01-09 14:53:41 -0800659bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) {
660 if (!local_description() || !remote_description()) {
661 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
662 << "SSL Role of the SCTP transport.";
663 return false;
664 }
665 if (!sctp_transport_) {
666 LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the "
667 << "SSL Role of the SCTP transport.";
668 return false;
669 }
670
671 return transport_controller_->GetSslRole(*sctp_transport_name_, role);
672}
673
674bool WebRtcSession::GetSslRole(const std::string& content_name,
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800675 rtc::SSLRole* role) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800676 if (!local_description() || !remote_description()) {
deadbeef953c2ce2017-01-09 14:53:41 -0800677 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the "
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000678 << "SSL Role of the session.";
679 return false;
680 }
681
deadbeef953c2ce2017-01-09 14:53:41 -0800682 return transport_controller_->GetSslRole(GetTransportName(content_name),
683 role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000684}
685
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000686void WebRtcSession::CreateOffer(
687 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700688 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
689 const cricket::MediaSessionOptions& session_options) {
690 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000691}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692
deadbeefab9b2d12015-10-14 11:33:11 -0700693void WebRtcSession::CreateAnswer(
694 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700695 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800696 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697}
698
699bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
700 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700701 ASSERT(signaling_thread()->IsCurrent());
702
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000703 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700704 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000705
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000706 // Validate SDP.
707 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
708 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
710
deadbeefd59daf82015-10-14 15:02:44 -0700711 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000712 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700714 initial_offerer_ = true;
715 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 }
717
deadbeeffe4a8a42016-12-20 17:56:17 -0800718 if (action == kAnswer) {
719 current_local_description_.reset(desc_temp.release());
720 pending_local_description_.reset(nullptr);
721 current_remote_description_.reset(pending_remote_description_.release());
722 } else {
723 pending_local_description_.reset(desc_temp.release());
724 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
726 // Transport and Media channels will be created only when offer is set.
deadbeeffe4a8a42016-12-20 17:56:17 -0800727 if (action == kOffer && !CreateChannels(local_description()->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 // TODO(mallinath) - Handle CreateChannel failure, as new local description
729 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000730 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732
deadbeefcbecd352015-09-23 11:50:27 -0700733 // Remove unused channels if MediaContentDescription is rejected.
deadbeeffe4a8a42016-12-20 17:56:17 -0800734 RemoveUnusedChannels(local_description()->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000736 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 return false;
738 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000739
deadbeeffe4a8a42016-12-20 17:56:17 -0800740 if (remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -0700741 // Now that we have a local description, we can push down remote candidates.
deadbeeffe4a8a42016-12-20 17:56:17 -0800742 UseCandidatesInSessionDescription(remote_description());
deadbeefcbecd352015-09-23 11:50:27 -0700743 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744
deadbeef0ed85b22016-02-23 17:24:52 -0800745 pending_ice_restarts_.clear();
746
deadbeefd59daf82015-10-14 15:02:44 -0700747 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000748 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750 return true;
751}
752
753bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
754 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700755 ASSERT(signaling_thread()->IsCurrent());
756
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000757 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700758 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000760 // Validate SDP.
761 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
762 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 }
764
deadbeeffe4a8a42016-12-20 17:56:17 -0800765 const SessionDescriptionInterface* old_remote_description =
766 remote_description();
767 // Grab ownership of the description being replaced for the remainder of this
768 // method, since it's used below.
769 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
770 Action action = GetAction(desc->type());
771 if (action == kAnswer) {
772 replaced_remote_description.reset(
773 pending_remote_description_ ? pending_remote_description_.release()
774 : current_remote_description_.release());
775 current_remote_description_.reset(desc_temp.release());
776 pending_remote_description_.reset(nullptr);
777 current_local_description_.reset(pending_local_description_.release());
778 } else {
779 replaced_remote_description.reset(pending_remote_description_.release());
780 pending_remote_description_.reset(desc_temp.release());
781 }
deadbeefd59daf82015-10-14 15:02:44 -0700782
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 // Transport and Media channels will be created only when offer is set.
784 if (action == kOffer && !CreateChannels(desc->description())) {
785 // TODO(mallinath) - Handle CreateChannel failure, as new local description
786 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000787 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
deadbeefcbecd352015-09-23 11:50:27 -0700790 // Remove unused channels if MediaContentDescription is rejected.
791 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792
793 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
794 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000795 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return false;
797 }
798
deadbeeffe4a8a42016-12-20 17:56:17 -0800799 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000800 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 }
802
deadbeeffe4a8a42016-12-20 17:56:17 -0800803 if (old_remote_description) {
deadbeef0ed85b22016-02-23 17:24:52 -0800804 for (const cricket::ContentInfo& content :
deadbeeffe4a8a42016-12-20 17:56:17 -0800805 old_remote_description->description()->contents()) {
deadbeef0ed85b22016-02-23 17:24:52 -0800806 // Check if this new SessionDescription contains new ICE ufrag and
807 // password that indicates the remote peer requests an ICE restart.
808 // TODO(deadbeef): When we start storing both the current and pending
809 // remote description, this should reset pending_ice_restarts and compare
810 // against the current description.
deadbeeffe4a8a42016-12-20 17:56:17 -0800811 if (CheckForRemoteIceRestart(old_remote_description, desc,
812 content.name)) {
deadbeef0ed85b22016-02-23 17:24:52 -0800813 if (action == kOffer) {
814 pending_ice_restarts_.insert(content.name);
815 }
816 } else {
817 // We retain all received candidates only if ICE is not restarted.
818 // When ICE is restarted, all previous candidates belong to an old
819 // generation and should not be kept.
820 // TODO(deadbeef): This goes against the W3C spec which says the remote
821 // description should only contain candidates from the last set remote
822 // description plus any candidates added since then. We should remove
823 // this once we're sure it won't break anything.
824 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeeffe4a8a42016-12-20 17:56:17 -0800825 old_remote_description, content.name, desc);
deadbeef0ed85b22016-02-23 17:24:52 -0800826 }
827 }
honghaiz503726c2015-07-31 12:37:38 -0700828 }
829
deadbeefd59daf82015-10-14 15:02:44 -0700830 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000831 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000833
834 // Set the the ICE connection state to connecting since the connection may
835 // become writable with peer reflexive candidates before any remote candidate
836 // is signaled.
837 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
838 // is to have a new signal the indicates a change in checking state from the
839 // transport and expose a new checking() member from transport that can be
840 // read to determine the current checking state. The existing SignalConnecting
841 // actually means "gathering candidates", so cannot be be used here.
842 if (desc->type() != SessionDescriptionInterface::kOffer &&
843 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
844 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
845 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 return true;
847}
848
deadbeefd59daf82015-10-14 15:02:44 -0700849void WebRtcSession::LogState(State old_state, State new_state) {
850 LOG(LS_INFO) << "Session:" << id()
851 << " Old state:" << GetStateString(old_state)
852 << " New state:" << GetStateString(new_state);
853}
854
855void WebRtcSession::SetState(State state) {
856 ASSERT(signaling_thread_->IsCurrent());
857 if (state != state_) {
858 LogState(state_, state);
859 state_ = state;
860 SignalState(this, state_);
861 }
862}
863
864void WebRtcSession::SetError(Error error, const std::string& error_desc) {
865 ASSERT(signaling_thread_->IsCurrent());
866 if (error != error_) {
867 error_ = error;
868 error_desc_ = error_desc;
869 }
870}
871
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872bool WebRtcSession::UpdateSessionState(
873 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700875 ASSERT(signaling_thread()->IsCurrent());
876
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 // If there's already a pending error then no state transition should happen.
878 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700879 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000880 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000882 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
883 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 }
deadbeefd59daf82015-10-14 15:02:44 -0700885 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
886 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000887 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700888 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000889 }
deadbeefd59daf82015-10-14 15:02:44 -0700890 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000894 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
895 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700898 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
899 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000900 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700901 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000902 }
deadbeefd59daf82015-10-14 15:02:44 -0700903 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000904 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700907 const cricket::ContentGroup* local_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800908 local_description()->description()->GetGroupByName(
909 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700910 const cricket::ContentGroup* remote_bundle =
deadbeeffe4a8a42016-12-20 17:56:17 -0800911 remote_description()->description()->GetGroupByName(
912 cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700913 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800914 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700915 const cricket::ContentGroup* answer_bundle =
916 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
917 if (!EnableBundle(*answer_bundle)) {
918 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
919 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
920 }
921 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800922 // Only push down the transport description after enabling BUNDLE; we don't
923 // want to push down a description on a transport about to be destroyed.
924 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
925 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
926 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700928 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000929 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700930 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 }
deadbeefd59daf82015-10-14 15:02:44 -0700932 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000933 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 }
935 }
936 return true;
937}
938
939WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
940 if (type == SessionDescriptionInterface::kOffer) {
941 return WebRtcSession::kOffer;
942 } else if (type == SessionDescriptionInterface::kPrAnswer) {
943 return WebRtcSession::kPrAnswer;
944 } else if (type == SessionDescriptionInterface::kAnswer) {
945 return WebRtcSession::kAnswer;
946 }
947 ASSERT(false && "unknown action type");
948 return WebRtcSession::kOffer;
949}
950
deadbeefd59daf82015-10-14 15:02:44 -0700951bool WebRtcSession::PushdownMediaDescription(
952 cricket::ContentAction action,
953 cricket::ContentSource source,
954 std::string* err) {
955 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
956 if (!ch) {
957 return true;
958 } else if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800959 return ch->PushdownLocalDescription(local_description()->description(),
960 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700961 } else {
deadbeeffe4a8a42016-12-20 17:56:17 -0800962 return ch->PushdownRemoteDescription(remote_description()->description(),
963 action, err);
deadbeefd59daf82015-10-14 15:02:44 -0700964 }
965 };
966
deadbeef953c2ce2017-01-09 14:53:41 -0800967 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
968 set_content(rtp_data_channel()));
969 // Need complete offer/answer with an SCTP m= section before starting SCTP,
970 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
971 if (sctp_transport_ && local_description() && remote_description() &&
972 cricket::GetFirstDataContent(local_description()->description()) &&
973 cricket::GetFirstDataContent(remote_description()->description())) {
974 ret &= network_thread_->Invoke<bool>(
975 RTC_FROM_HERE,
976 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
977 }
978 return ret;
979}
980
981bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
982 RTC_DCHECK(network_thread_->IsCurrent());
983 RTC_DCHECK(local_description());
984 RTC_DCHECK(remote_description());
985 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
986 // When we support "max-message-size", that would also be pushed down here.
987 return sctp_transport_->Start(
988 GetSctpPort(local_description()->description()),
989 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700990}
991
992bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
993 cricket::ContentAction action,
994 std::string* error_desc) {
995 RTC_DCHECK(signaling_thread()->IsCurrent());
996
997 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800998 return PushdownLocalTransportDescription(local_description()->description(),
999 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001000 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001001 return PushdownRemoteTransportDescription(remote_description()->description(),
1002 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001003}
1004
1005bool WebRtcSession::PushdownLocalTransportDescription(
1006 const SessionDescription* sdesc,
1007 cricket::ContentAction action,
1008 std::string* err) {
1009 RTC_DCHECK(signaling_thread()->IsCurrent());
1010
1011 if (!sdesc) {
1012 return false;
1013 }
1014
1015 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1016 if (!transport_controller_->SetLocalTransportDescription(
1017 tinfo.content_name, tinfo.description, action, err)) {
1018 return false;
1019 }
1020 }
1021
1022 return true;
1023}
1024
1025bool WebRtcSession::PushdownRemoteTransportDescription(
1026 const SessionDescription* sdesc,
1027 cricket::ContentAction action,
1028 std::string* err) {
1029 RTC_DCHECK(signaling_thread()->IsCurrent());
1030
1031 if (!sdesc) {
1032 return false;
1033 }
1034
1035 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1036 if (!transport_controller_->SetRemoteTransportDescription(
1037 tinfo.content_name, tinfo.description, action, err)) {
1038 return false;
1039 }
1040 }
1041
1042 return true;
1043}
1044
1045bool WebRtcSession::GetTransportDescription(
1046 const SessionDescription* description,
1047 const std::string& content_name,
1048 cricket::TransportDescription* tdesc) {
1049 if (!description || !tdesc) {
1050 return false;
1051 }
1052 const TransportInfo* transport_info =
1053 description->GetTransportInfoByName(content_name);
1054 if (!transport_info) {
1055 return false;
1056 }
1057 *tdesc = transport_info->description;
1058 return true;
1059}
1060
deadbeefcbecd352015-09-23 11:50:27 -07001061bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1062 const std::string* first_content_name = bundle.FirstContentName();
1063 if (!first_content_name) {
1064 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1065 return false;
1066 }
1067 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001068
zhihuang9763d562016-08-05 11:14:50 -07001069#ifdef HAVE_QUIC
1070 if (quic_data_transport_ &&
1071 bundle.HasContentName(quic_data_transport_->content_name()) &&
1072 quic_data_transport_->transport_name() != transport_name) {
1073 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1074 << " on " << transport_name << "with QUIC.";
1075 }
1076#endif
1077
deadbeef953c2ce2017-01-09 14:53:41 -08001078 auto maybe_set_transport = [this, bundle,
1079 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001080 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001081 return true;
1082 }
1083
deadbeefcbecd352015-09-23 11:50:27 -07001084 if (ch->transport_name() == transport_name) {
1085 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1086 << " on " << transport_name << ".";
1087 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001088 }
torbjornga81a42f2015-09-23 02:16:58 -07001089
deadbeefcbecd352015-09-23 11:50:27 -07001090 if (!ch->SetTransport(transport_name)) {
1091 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1092 return false;
1093 }
1094 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1095 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001096 return true;
1097 };
1098
deadbeefcbecd352015-09-23 11:50:27 -07001099 if (!maybe_set_transport(voice_channel()) ||
1100 !maybe_set_transport(video_channel()) ||
deadbeef953c2ce2017-01-09 14:53:41 -08001101 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001102 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001103 }
deadbeef953c2ce2017-01-09 14:53:41 -08001104 // For SCTP, transport creation/deletion happens here instead of in the
1105 // object itself.
1106 if (sctp_transport_) {
1107 RTC_DCHECK(sctp_transport_name_);
1108 RTC_DCHECK(sctp_content_name_);
1109 if (transport_name != *sctp_transport_name_ &&
1110 bundle.HasContentName(*sctp_content_name_)) {
1111 network_thread_->Invoke<void>(
1112 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1113 transport_name));
1114 }
1115 }
deadbeefcbecd352015-09-23 11:50:27 -07001116
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001117 return true;
1118}
1119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001121 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001122 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1123 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001124 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
1126
1127 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001128 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 return false;
1130 }
1131
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001132 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001133 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1134 if (!valid) {
1135 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 }
1137
1138 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001139 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001140 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 return false;
1142 }
1143
deadbeefd59daf82015-10-14 15:02:44 -07001144 if (ready) {
1145 return UseCandidate(candidate);
1146 } else {
1147 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1148 return true;
1149 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150}
1151
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001152bool WebRtcSession::RemoveRemoteIceCandidates(
1153 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001154 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001155 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1156 << "removed without any remote session description.";
1157 return false;
1158 }
1159
1160 if (candidates.empty()) {
1161 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1162 return false;
1163 }
1164
deadbeeffe4a8a42016-12-20 17:56:17 -08001165 size_t number_removed =
1166 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001167 if (number_removed != candidates.size()) {
1168 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1169 << "Requested " << candidates.size() << " but only "
1170 << number_removed << " are removed.";
1171 }
1172
1173 // Remove the candidates from the transport controller.
1174 std::string error;
1175 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1176 if (!res && !error.empty()) {
1177 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1178 }
1179 return true;
1180}
1181
deadbeefd59daf82015-10-14 15:02:44 -07001182cricket::IceConfig WebRtcSession::ParseIceConfig(
1183 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001184 cricket::ContinualGatheringPolicy gathering_policy;
1185 // TODO(honghaiz): Add the third continual gathering policy in
1186 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1187 switch (config.continual_gathering_policy) {
1188 case PeerConnectionInterface::GATHER_ONCE:
1189 gathering_policy = cricket::GATHER_ONCE;
1190 break;
1191 case PeerConnectionInterface::GATHER_CONTINUALLY:
1192 gathering_policy = cricket::GATHER_CONTINUALLY;
1193 break;
1194 default:
nisseeb4ca4e2017-01-12 02:24:27 -08001195 RTC_NOTREACHED();
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001196 gathering_policy = cricket::GATHER_ONCE;
1197 }
deadbeefd59daf82015-10-14 15:02:44 -07001198 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001199 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001200 ice_config.prioritize_most_likely_candidate_pairs =
1201 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001202 ice_config.backup_connection_ping_interval =
1203 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001204 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001205 ice_config.presume_writable_when_fully_relayed =
1206 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001207 return ice_config;
1208}
1209
1210void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1211 transport_controller_->SetIceConfig(config);
1212}
1213
1214void WebRtcSession::MaybeStartGathering() {
1215 transport_controller_->MaybeStartGathering();
1216}
1217
Peter Boström0c4e06b2015-10-07 12:23:21 +02001218bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1219 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001220 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001222 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001223 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1224 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225}
1226
Peter Boström0c4e06b2015-10-07 12:23:21 +02001227bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1228 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001229 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001230 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001231 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001232 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1233 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234}
1235
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001236std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 return desc.str();
1240}
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1243 ASSERT(signaling_thread()->IsCurrent());
1244 if (!voice_channel_) {
1245 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1246 return false;
1247 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001248 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1250 // exists.
deadbeeffe4a8a42016-12-20 17:56:17 -08001251 if (!local_description() ||
1252 !GetAudioSsrcByTrackId(local_description()->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 &send_ssrc)) {
1254 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1255 return false;
1256 }
1257 return voice_channel_->CanInsertDtmf();
1258}
1259
1260bool WebRtcSession::InsertDtmf(const std::string& track_id,
1261 int code, int duration) {
1262 ASSERT(signaling_thread()->IsCurrent());
1263 if (!voice_channel_) {
1264 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1265 return false;
1266 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001267 uint32_t send_ssrc = 0;
deadbeeffe4a8a42016-12-20 17:56:17 -08001268 if (!VERIFY(local_description() &&
1269 GetAudioSsrcByTrackId(local_description()->description(),
1270 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1272 return false;
1273 }
solenberg1d63dd02015-12-02 12:35:09 -08001274 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1276 return false;
1277 }
1278 return true;
1279}
1280
1281sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001282 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283}
1284
wu@webrtc.org78187522013-10-07 23:32:02 +00001285bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001286 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001287 cricket::SendDataResult* result) {
deadbeef953c2ce2017-01-09 14:53:41 -08001288 if (!rtp_data_channel_ && !sctp_transport_) {
1289 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1290 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001291 return false;
1292 }
deadbeef953c2ce2017-01-09 14:53:41 -08001293 return rtp_data_channel_
1294 ? rtp_data_channel_->SendData(params, payload, result)
1295 : network_thread_->Invoke<bool>(
1296 RTC_FROM_HERE,
1297 Bind(&cricket::SctpTransportInternal::SendData,
1298 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001299}
1300
1301bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001302 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001303 // Don't log an error here, because DataChannels are expected to call
1304 // ConnectDataChannel in this state. It's the only way to initially tell
1305 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001306 return false;
1307 }
deadbeef953c2ce2017-01-09 14:53:41 -08001308 if (rtp_data_channel_) {
1309 rtp_data_channel_->SignalReadyToSendData.connect(
1310 webrtc_data_channel, &DataChannel::OnChannelReady);
1311 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1312 &DataChannel::OnDataReceived);
1313 } else {
1314 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1315 &DataChannel::OnChannelReady);
1316 SignalSctpDataReceived.connect(webrtc_data_channel,
1317 &DataChannel::OnDataReceived);
1318 SignalSctpStreamClosedRemotely.connect(
1319 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1320 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001321 return true;
1322}
1323
1324void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef953c2ce2017-01-09 14:53:41 -08001325 if (!rtp_data_channel_ && !sctp_transport_) {
1326 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1327 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001328 return;
1329 }
deadbeef953c2ce2017-01-09 14:53:41 -08001330 if (rtp_data_channel_) {
1331 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1332 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1333 } else {
1334 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1335 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1336 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1337 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001338}
1339
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001340void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001341 if (!sctp_transport_) {
1342 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001343 return;
1344 }
deadbeef953c2ce2017-01-09 14:53:41 -08001345 network_thread_->Invoke<void>(
1346 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1347 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001348}
1349
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001350void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef953c2ce2017-01-09 14:53:41 -08001351 if (!sctp_transport_) {
1352 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001353 << "NULL.";
1354 return;
1355 }
deadbeef953c2ce2017-01-09 14:53:41 -08001356 network_thread_->Invoke<void>(
1357 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1358 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001359}
1360
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001361bool WebRtcSession::ReadyToSendData() const {
deadbeef953c2ce2017-01-09 14:53:41 -08001362 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1363 sctp_ready_to_send_data_;
1364}
1365
1366std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
1367 ASSERT(signaling_thread()->IsCurrent());
1368 ChannelNamePairs channel_name_pairs;
1369 if (voice_channel()) {
1370 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1371 voice_channel()->content_name(), voice_channel()->transport_name()));
1372 }
1373 if (video_channel()) {
1374 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1375 video_channel()->content_name(), video_channel()->transport_name()));
1376 }
1377 if (rtp_data_channel()) {
1378 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1379 ChannelNamePair(rtp_data_channel()->content_name(),
1380 rtp_data_channel()->transport_name()));
1381 }
1382 if (sctp_transport_) {
1383 RTC_DCHECK(sctp_content_name_);
1384 RTC_DCHECK(sctp_transport_name_);
1385 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1386 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1387 }
1388 return GetStats(channel_name_pairs);
1389}
1390
1391std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1392 const ChannelNamePairs& channel_name_pairs) {
1393 if (network_thread()->IsCurrent()) {
1394 return GetStats_n(channel_name_pairs);
1395 }
1396 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1397 RTC_FROM_HERE,
1398 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1399}
1400
1401bool WebRtcSession::GetLocalCertificate(
1402 const std::string& transport_name,
1403 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1404 return transport_controller_->GetLocalCertificate(transport_name,
1405 certificate);
1406}
1407
1408std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1409 const std::string& transport_name) {
1410 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001411}
1412
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413cricket::DataChannelType WebRtcSession::data_channel_type() const {
1414 return data_channel_type_;
1415}
1416
deadbeef0ed85b22016-02-23 17:24:52 -08001417bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1418 return pending_ice_restarts_.find(content_name) !=
1419 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001420}
1421
deadbeefd1a38b52016-12-10 13:15:33 -08001422void WebRtcSession::SetNeedsIceRestartFlag() {
1423 transport_controller_->SetNeedsIceRestartFlag();
1424}
1425
1426bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1427 return transport_controller_->NeedsIceRestart(content_name);
1428}
1429
Henrik Boströmd8281982015-08-27 10:12:24 +02001430void WebRtcSession::OnCertificateReady(
1431 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001432 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001433}
1434
deadbeef953c2ce2017-01-09 14:53:41 -08001435void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1436 SetError(ERROR_TRANSPORT,
1437 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1438}
1439
Henrik Boströmd8281982015-08-27 10:12:24 +02001440bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001441 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001442}
1443
deadbeefcbecd352015-09-23 11:50:27 -07001444const rtc::scoped_refptr<rtc::RTCCertificate>&
1445WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001446 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001447}
1448
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449void WebRtcSession::SetIceConnectionState(
1450 PeerConnectionInterface::IceConnectionState state) {
1451 if (ice_connection_state_ == state) {
1452 return;
1453 }
1454
deadbeefcbecd352015-09-23 11:50:27 -07001455 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1456 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001457 RTC_DCHECK(ice_connection_state_ !=
1458 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 ice_connection_state_ = state;
1460 if (ice_observer_) {
1461 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1462 }
1463}
1464
deadbeefcbecd352015-09-23 11:50:27 -07001465void WebRtcSession::OnTransportControllerConnectionState(
1466 cricket::IceConnectionState state) {
1467 switch (state) {
1468 case cricket::kIceConnectionConnecting:
1469 // If the current state is Connected or Completed, then there were
1470 // writable channels but now there are not, so the next state must
1471 // be Disconnected.
1472 // kIceConnectionConnecting is currently used as the default,
1473 // un-connected state by the TransportController, so its only use is
1474 // detecting disconnections.
1475 if (ice_connection_state_ ==
1476 PeerConnectionInterface::kIceConnectionConnected ||
1477 ice_connection_state_ ==
1478 PeerConnectionInterface::kIceConnectionCompleted) {
1479 SetIceConnectionState(
1480 PeerConnectionInterface::kIceConnectionDisconnected);
1481 }
torbjornga81a42f2015-09-23 02:16:58 -07001482 break;
deadbeefcbecd352015-09-23 11:50:27 -07001483 case cricket::kIceConnectionFailed:
1484 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1485 break;
1486 case cricket::kIceConnectionConnected:
1487 LOG(LS_INFO) << "Changing to ICE connected state because "
1488 << "all transports are writable.";
1489 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1490 break;
1491 case cricket::kIceConnectionCompleted:
1492 LOG(LS_INFO) << "Changing to ICE completed state because "
1493 << "all transports are complete.";
1494 if (ice_connection_state_ !=
1495 PeerConnectionInterface::kIceConnectionConnected) {
1496 // If jumping directly from "checking" to "connected",
1497 // signal "connected" first.
1498 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1499 }
1500 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1501 if (metrics_observer_) {
1502 ReportTransportStats();
1503 }
1504 break;
1505 default:
nissec80e7412017-01-11 05:56:46 -08001506 RTC_NOTREACHED();
torbjornga81a42f2015-09-23 02:16:58 -07001507 }
deadbeefcbecd352015-09-23 11:50:27 -07001508}
1509
1510void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001511 SetIceConnectionReceiving(receiving);
1512}
1513
1514void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1515 if (ice_connection_receiving_ == receiving) {
1516 return;
1517 }
1518 ice_connection_receiving_ = receiving;
1519 if (ice_observer_) {
1520 ice_observer_->OnIceConnectionReceivingChange(receiving);
1521 }
1522}
1523
deadbeefcbecd352015-09-23 11:50:27 -07001524void WebRtcSession::OnTransportControllerCandidatesGathered(
1525 const std::string& transport_name,
1526 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001528 int sdp_mline_index;
1529 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1530 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1531 << transport_name << " not found";
1532 return;
1533 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534
deadbeefcbecd352015-09-23 11:50:27 -07001535 for (cricket::Candidates::const_iterator citer = candidates.begin();
1536 citer != candidates.end(); ++citer) {
1537 // Use transport_name as the candidate media id.
1538 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1539 if (ice_observer_) {
1540 ice_observer_->OnIceCandidate(&candidate);
1541 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001542 if (local_description()) {
1543 mutable_local_description()->AddCandidate(&candidate);
deadbeefcbecd352015-09-23 11:50:27 -07001544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 }
1546}
1547
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001548void WebRtcSession::OnTransportControllerCandidatesRemoved(
1549 const std::vector<cricket::Candidate>& candidates) {
1550 ASSERT(signaling_thread()->IsCurrent());
1551 // Sanity check.
1552 for (const cricket::Candidate& candidate : candidates) {
1553 if (candidate.transport_name().empty()) {
1554 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1555 << "empty content name in candidate "
1556 << candidate.ToString();
1557 return;
1558 }
1559 }
1560
deadbeeffe4a8a42016-12-20 17:56:17 -08001561 if (local_description()) {
1562 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001563 }
1564 if (ice_observer_) {
1565 ice_observer_->OnIceCandidatesRemoved(candidates);
1566 }
1567}
1568
deadbeef953c2ce2017-01-09 14:53:41 -08001569void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1570 rtc::SSLHandshakeError error) {
1571 if (metrics_observer_) {
1572 metrics_observer_->IncrementEnumCounter(
1573 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1574 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1575 }
1576}
1577
1578// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579void WebRtcSession::EnableChannels() {
1580 if (voice_channel_ && !voice_channel_->enabled())
1581 voice_channel_->Enable(true);
1582
1583 if (video_channel_ && !video_channel_->enabled())
1584 video_channel_->Enable(true);
1585
deadbeef953c2ce2017-01-09 14:53:41 -08001586 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1587 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588}
1589
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590// Returns the media index for a local ice candidate given the content name.
1591bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1592 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001593 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001595 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596
1597 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001598 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 for (size_t index = 0; index < contents.size(); ++index) {
1600 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001601 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 content_found = true;
1603 break;
1604 }
1605 }
1606 return content_found;
1607}
1608
1609bool WebRtcSession::UseCandidatesInSessionDescription(
1610 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001611 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001613 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001615
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1617 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001618 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001619 const IceCandidateInterface* candidate = candidates->at(n);
1620 bool valid = false;
1621 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1622 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001623 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1624 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001625 }
1626 continue;
1627 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001628 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001629 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 break;
deadbeefd59daf82015-10-14 15:02:44 -07001631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633 }
1634 return ret;
1635}
1636
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001637bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001639 size_t remote_content_size =
1640 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001642 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 return false;
1644 }
1645
1646 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001647 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 std::vector<cricket::Candidate> candidates;
1649 candidates.push_back(candidate->candidate());
1650 // Invoking BaseSession method to handle remote candidates.
1651 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001652 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1653 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 // Candidates successfully submitted for checking.
1655 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1656 ice_connection_state_ ==
1657 PeerConnectionInterface::kIceConnectionDisconnected) {
1658 // If state is New, then the session has just gotten its first remote ICE
1659 // candidates, so go to Checking.
1660 // If state is Disconnected, the session is re-using old candidates or
1661 // receiving additional ones, so go to Checking.
1662 // If state is Connected, stay Connected.
1663 // TODO(bemasc): If state is Connected, and the new candidates are for a
1664 // newly added transport, then the state actually _should_ move to
1665 // checking. Add a way to distinguish that case.
1666 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1667 }
1668 // TODO(bemasc): If state is Completed, go back to Connected.
1669 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001670 if (!error.empty()) {
1671 LOG(LS_WARNING) << error;
1672 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 }
1674 return true;
1675}
1676
deadbeefcbecd352015-09-23 11:50:27 -07001677void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001678 // Destroy video_channel_ first since it may have a pointer to the
1679 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 const cricket::ContentInfo* video_info =
1681 cricket::GetFirstVideoContent(desc);
1682 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 }
1686
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001687 const cricket::ContentInfo* voice_info =
1688 cricket::GetFirstAudioContent(desc);
1689 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001690 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001691 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001692 }
1693
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 const cricket::ContentInfo* data_info =
1695 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001696 if (!data_info || data_info->rejected) {
deadbeef953c2ce2017-01-09 14:53:41 -08001697 if (rtp_data_channel_) {
zhihuang9763d562016-08-05 11:14:50 -07001698 SignalDataChannelDestroyed();
deadbeef953c2ce2017-01-09 14:53:41 -08001699 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
1700 }
1701 if (sctp_transport_) {
1702 SignalDataChannelDestroyed();
1703 network_thread_->Invoke<void>(
1704 RTC_FROM_HERE,
1705 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001706 }
1707#ifdef HAVE_QUIC
1708 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1709 if (quic_data_transport_) {
1710 quic_data_transport_.reset();
1711 }
1712#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 }
1714}
1715
skvlad6c87a672016-05-17 17:49:52 -07001716// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1717// if the channel is not part of any bundle.
1718const std::string* WebRtcSession::GetBundleTransportName(
1719 const cricket::ContentInfo* content,
1720 const cricket::ContentGroup* bundle) {
1721 if (!bundle) {
1722 return nullptr;
1723 }
1724 const std::string* first_content_name = bundle->FirstContentName();
1725 if (!first_content_name) {
1726 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1727 return nullptr;
1728 }
1729 if (!bundle->HasContentName(content->name)) {
1730 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1731 return nullptr;
1732 }
1733 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1734 return first_content_name;
1735}
1736
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001738 const cricket::ContentGroup* bundle_group = nullptr;
1739 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1740 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1741 if (!bundle_group) {
1742 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1743 return false;
1744 }
1745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 // Creating the media channels and transport proxies.
1747 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1748 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001749 if (!CreateVoiceChannel(voice,
1750 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 LOG(LS_ERROR) << "Failed to create voice channel.";
1752 return false;
1753 }
1754 }
1755
1756 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1757 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001758 if (!CreateVideoChannel(video,
1759 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 LOG(LS_ERROR) << "Failed to create video channel.";
1761 return false;
1762 }
1763 }
1764
1765 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef953c2ce2017-01-09 14:53:41 -08001766 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1767 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001768 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 LOG(LS_ERROR) << "Failed to create data channel.";
1770 return false;
1771 }
1772 }
1773
1774 return true;
1775}
1776
skvlad6c87a672016-05-17 17:49:52 -07001777bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1778 const std::string* bundle_transport) {
1779 bool require_rtcp_mux =
1780 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1781 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001783 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001784 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1785 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001786 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001787 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001788 }
skvlad6c87a672016-05-17 17:49:52 -07001789 if (require_rtcp_mux) {
1790 voice_channel_->ActivateRtcpMux();
1791 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001792
deadbeef953c2ce2017-01-09 14:53:41 -08001793 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1794 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001795
1796 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001797 voice_channel_->SignalSentPacket.connect(this,
1798 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001799 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800}
1801
skvlad6c87a672016-05-17 17:49:52 -07001802bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1803 const std::string* bundle_transport) {
1804 bool require_rtcp_mux =
1805 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1806 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001808 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001809 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1810 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001811 if (!video_channel_) {
1812 return false;
1813 }
skvlad6c87a672016-05-17 17:49:52 -07001814 if (require_rtcp_mux) {
1815 video_channel_->ActivateRtcpMux();
1816 }
deadbeef953c2ce2017-01-09 14:53:41 -08001817 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1818 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001819
1820 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001821 video_channel_->SignalSentPacket.connect(this,
1822 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
skvlad6c87a672016-05-17 17:49:52 -07001826bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1827 const std::string* bundle_transport) {
deadbeef953c2ce2017-01-09 14:53:41 -08001828 const std::string transport_name =
1829 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001830#ifdef HAVE_QUIC
1831 if (data_channel_type_ == cricket::DCT_QUIC) {
1832 RTC_DCHECK(transport_controller_->quic());
zhihuang9763d562016-08-05 11:14:50 -07001833 quic_data_transport_->SetTransport(transport_name);
1834 return true;
1835 }
1836#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001837 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
deadbeefc0dad892017-01-04 20:28:21 -08001838 if (sctp) {
deadbeef953c2ce2017-01-09 14:53:41 -08001839 if (!sctp_factory_) {
1840 LOG(LS_ERROR)
1841 << "Trying to create SCTP transport, but didn't compile with "
1842 "SCTP support (HAVE_SCTP)";
1843 return false;
1844 }
1845 if (!network_thread_->Invoke<bool>(
1846 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1847 this, content->name, transport_name))) {
1848 return false;
1849 };
1850 } else {
1851 bool require_rtcp_mux =
1852 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1853 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
1854 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
1855 media_controller_, transport_controller_.get(), content->name,
1856 bundle_transport, create_rtcp_transport_channel, SrtpRequired()));
1857 if (!rtp_data_channel_) {
1858 return false;
1859 }
1860 if (require_rtcp_mux) {
1861 rtp_data_channel_->ActivateRtcpMux();
1862 }
1863 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1864 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1865 rtp_data_channel_->SignalSentPacket.connect(this,
1866 &WebRtcSession::OnSentPacket_w);
deadbeefc0dad892017-01-04 20:28:21 -08001867 }
1868
deadbeefab9b2d12015-10-14 11:33:11 -07001869 SignalDataChannelCreated();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001870 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871}
1872
hbosdf6075a2016-12-19 04:58:02 -08001873std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1874 const ChannelNamePairs& channel_name_pairs) {
1875 ASSERT(network_thread()->IsCurrent());
1876 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1877 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1878 &channel_name_pairs.video,
1879 &channel_name_pairs.data }) {
1880 if (*channel_name_pair) {
1881 cricket::TransportStats transport_stats;
1882 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1883 &transport_stats)) {
1884 return nullptr;
1885 }
1886 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1887 (*channel_name_pair)->transport_name;
1888 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1889 std::move(transport_stats);
1890 }
1891 }
1892 return session_stats;
1893}
1894
deadbeef953c2ce2017-01-09 14:53:41 -08001895bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1896 const std::string& transport_name) {
1897 RTC_DCHECK(network_thread_->IsCurrent());
1898 RTC_DCHECK(sctp_factory_);
1899 cricket::TransportChannel* tc =
1900 transport_controller_->CreateTransportChannel_n(
1901 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1902 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1903 RTC_DCHECK(sctp_transport_);
1904 sctp_invoker_.reset(new rtc::AsyncInvoker());
1905 sctp_transport_->SignalReadyToSendData.connect(
1906 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1907 sctp_transport_->SignalDataReceived.connect(
1908 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1909 sctp_transport_->SignalStreamClosedRemotely.connect(
1910 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1911 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1912 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1913 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914}
1915
deadbeef953c2ce2017-01-09 14:53:41 -08001916void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1917 RTC_DCHECK(network_thread_->IsCurrent());
1918 RTC_DCHECK(sctp_transport_);
1919 RTC_DCHECK(sctp_transport_name_);
1920 std::string old_sctp_transport_name = *sctp_transport_name_;
1921 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1922 cricket::TransportChannel* tc =
1923 transport_controller_->CreateTransportChannel_n(
1924 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1925 sctp_transport_->SetTransportChannel(tc);
1926 transport_controller_->DestroyTransportChannel_n(
1927 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1928}
1929
1930void WebRtcSession::DestroySctpTransport_n() {
1931 RTC_DCHECK(network_thread_->IsCurrent());
1932 sctp_transport_.reset(nullptr);
1933 sctp_content_name_.reset();
1934 sctp_transport_name_.reset();
1935 sctp_invoker_.reset(nullptr);
1936 sctp_ready_to_send_data_ = false;
1937}
1938
1939void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1940 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1941 RTC_DCHECK(network_thread_->IsCurrent());
1942 sctp_invoker_->AsyncInvoke<void>(
1943 RTC_FROM_HERE, signaling_thread_,
1944 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1945}
1946
1947void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1948 RTC_DCHECK(signaling_thread_->IsCurrent());
1949 sctp_ready_to_send_data_ = ready;
1950 SignalSctpReadyToSendData(ready);
1951}
1952
1953void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001954 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001955 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001956 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef953c2ce2017-01-09 14:53:41 -08001957 RTC_DCHECK(network_thread_->IsCurrent());
1958 sctp_invoker_->AsyncInvoke<void>(
1959 RTC_FROM_HERE, signaling_thread_,
1960 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
1961 payload));
1962}
1963
1964void WebRtcSession::OnSctpTransportDataReceived_s(
1965 const cricket::ReceiveDataParams& params,
1966 const rtc::CopyOnWriteBuffer& payload) {
1967 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07001968 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1969 // Received OPEN message; parse and signal that a new data channel should
1970 // be created.
1971 std::string label;
1972 InternalDataChannelInit config;
1973 config.id = params.ssrc;
1974 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1975 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1976 << params.ssrc;
1977 return;
1978 }
1979 config.open_handshake_role = InternalDataChannelInit::kAcker;
1980 SignalDataChannelOpenMessage(label, config);
deadbeef953c2ce2017-01-09 14:53:41 -08001981 } else {
1982 // Otherwise just forward the signal.
1983 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 }
deadbeef953c2ce2017-01-09 14:53:41 -08001985}
1986
1987void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
1988 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1989 RTC_DCHECK(network_thread_->IsCurrent());
1990 sctp_invoker_->AsyncInvoke<void>(
1991 RTC_FROM_HERE, signaling_thread_,
1992 rtc::Bind(&sigslot::signal1<int>::operator(),
1993 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994}
1995
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001996// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001997bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001998 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1999 if (!bundle_enabled)
2000 return true;
2001
2002 const cricket::ContentGroup* bundle_group =
2003 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
2004 ASSERT(bundle_group != NULL);
2005
2006 const cricket::ContentInfos& contents = desc->contents();
2007 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2008 citer != contents.end(); ++citer) {
2009 const cricket::ContentInfo* content = (&*citer);
2010 ASSERT(content != NULL);
2011 if (bundle_group->HasContentName(content->name) &&
2012 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2013 if (!HasRtcpMuxEnabled(content))
2014 return false;
2015 }
2016 }
2017 // RTCP-MUX is enabled in all the contents.
2018 return true;
2019}
2020
2021bool WebRtcSession::HasRtcpMuxEnabled(
2022 const cricket::ContentInfo* content) {
2023 const cricket::MediaContentDescription* description =
2024 static_cast<cricket::MediaContentDescription*>(content->description);
2025 return description->rtcp_mux();
2026}
2027
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002028bool WebRtcSession::ValidateSessionDescription(
2029 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002030 cricket::ContentSource source, std::string* err_desc) {
2031 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002032 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002033 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002034 }
2035
2036 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002037 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002038 }
2039
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002040 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002041 Action action = GetAction(sdesc->type());
2042 if (source == cricket::CS_LOCAL) {
2043 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002044 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002045 } else {
2046 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002047 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002048 }
2049
2050 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002051 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002052 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2053 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002054 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002055 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002056 }
2057
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002058 // Verify ice-ufrag and ice-pwd.
2059 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002060 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002061 }
2062
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002063 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002064 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002065 }
2066
skvlad6c87a672016-05-17 17:49:52 -07002067 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2068 // m-lines that do not rtcp-mux enabled.
2069
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002070 // Verify m-lines in Answer when compared against Offer.
2071 if (action == kAnswer) {
2072 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002073 (source == cricket::CS_LOCAL) ? remote_description()->description()
2074 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002075 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002076 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002077 }
2078 }
2079
2080 return true;
2081}
2082
2083bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2084 return ((action == kOffer && state() == STATE_INIT) ||
2085 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002086 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002087 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002088 (action == kOffer && state() == STATE_INPROGRESS) ||
2089 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002090 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2091 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2092 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2093 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002094}
2095
2096bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2097 return ((action == kOffer && state() == STATE_INIT) ||
2098 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002099 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002100 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002101 (action == kOffer && state() == STATE_INPROGRESS) ||
2102 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002103 (action == kAnswer && state() == STATE_SENTOFFER) ||
2104 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2105 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2106 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002107}
2108
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002109std::string WebRtcSession::GetSessionErrorMsg() {
2110 std::ostringstream desc;
2111 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2112 desc << kSessionErrorDesc << error_desc() << ".";
2113 return desc.str();
2114}
2115
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002116// We need to check the local/remote description for the Transport instead of
2117// the session, because a new Transport added during renegotiation may have
2118// them unset while the session has them set from the previous negotiation.
2119// Not doing so may trigger the auto generation of transport description and
2120// mess up DTLS identity information, ICE credential, etc.
2121bool WebRtcSession::ReadyToUseRemoteCandidate(
2122 const IceCandidateInterface* candidate,
2123 const SessionDescriptionInterface* remote_desc,
2124 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002125 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002126
2127 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002128 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002129
deadbeefd59daf82015-10-14 15:02:44 -07002130 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002131 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002132 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002133
2134 size_t mediacontent_index =
2135 static_cast<size_t>(candidate->sdp_mline_index());
2136 size_t remote_content_size =
2137 current_remote_desc->description()->contents().size();
2138 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002139 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2140 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002141
2142 *valid = false;
2143 return false;
2144 }
2145
2146 cricket::ContentInfo content =
2147 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002148
2149 const std::string transport_name = GetTransportName(content.name);
2150 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002151 return false;
2152 }
zhihuang9763d562016-08-05 11:14:50 -07002153 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002154}
2155
deadbeef7af91dd2016-12-13 11:29:11 -08002156bool WebRtcSession::SrtpRequired() const {
2157 return dtls_enabled_ ||
2158 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2159}
2160
deadbeefcbecd352015-09-23 11:50:27 -07002161void WebRtcSession::OnTransportControllerGatheringState(
2162 cricket::IceGatheringState state) {
2163 ASSERT(signaling_thread()->IsCurrent());
2164 if (state == cricket::kIceGatheringGathering) {
2165 if (ice_observer_) {
2166 ice_observer_->OnIceGatheringChange(
2167 PeerConnectionInterface::kIceGatheringGathering);
2168 }
2169 } else if (state == cricket::kIceGatheringComplete) {
2170 if (ice_observer_) {
2171 ice_observer_->OnIceGatheringChange(
2172 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002173 }
2174 }
2175}
2176
2177void WebRtcSession::ReportTransportStats() {
2178 // Use a set so we don't report the same stats twice if two channels share
2179 // a transport.
2180 std::set<std::string> transport_names;
2181 if (voice_channel()) {
2182 transport_names.insert(voice_channel()->transport_name());
2183 }
2184 if (video_channel()) {
2185 transport_names.insert(video_channel()->transport_name());
2186 }
deadbeef953c2ce2017-01-09 14:53:41 -08002187 if (rtp_data_channel()) {
2188 transport_names.insert(rtp_data_channel()->transport_name());
2189 }
2190 if (sctp_transport_name_) {
2191 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002192 }
2193 for (const auto& name : transport_names) {
2194 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002195 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002196 ReportBestConnectionState(stats);
2197 ReportNegotiatedCiphers(stats);
2198 }
2199 }
2200}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002201// Walk through the ConnectionInfos to gather best connection usage
2202// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002203void WebRtcSession::ReportBestConnectionState(
2204 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002205 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002206 for (cricket::TransportChannelStatsList::const_iterator it =
2207 stats.channel_stats.begin();
2208 it != stats.channel_stats.end(); ++it) {
2209 for (cricket::ConnectionInfos::const_iterator it_info =
2210 it->connection_infos.begin();
2211 it_info != it->connection_infos.end(); ++it_info) {
2212 if (!it_info->best_connection) {
2213 continue;
2214 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002215
2216 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2217 const cricket::Candidate& local = it_info->local_candidate;
2218 const cricket::Candidate& remote = it_info->remote_candidate;
2219
2220 // Increment the counter for IceCandidatePairType.
2221 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2222 (local.type() == RELAY_PORT_TYPE &&
2223 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2224 type = kEnumCounterIceCandidatePairTypeTcp;
2225 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2226 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002227 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002228 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002229 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002230 metrics_observer_->IncrementEnumCounter(
2231 type, GetIceCandidatePairCounter(local, remote),
2232 kIceCandidatePairMax);
2233
2234 // Increment the counter for IP type.
2235 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002236 metrics_observer_->IncrementEnumCounter(
2237 kEnumCounterAddressFamily, kBestConnections_IPv4,
2238 kPeerConnectionAddressFamilyCounter_Max);
2239
2240 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002241 metrics_observer_->IncrementEnumCounter(
2242 kEnumCounterAddressFamily, kBestConnections_IPv6,
2243 kPeerConnectionAddressFamilyCounter_Max);
2244 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002245 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002246 }
2247
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002248 return;
2249 }
2250 }
2251}
2252
jbauchac8869e2015-07-03 01:36:14 -07002253void WebRtcSession::ReportNegotiatedCiphers(
2254 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002255 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002256 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2257 return;
2258 }
2259
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002260 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2261 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2262 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2263 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002264 return;
2265 }
2266
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002267 PeerConnectionEnumCounterType srtp_counter_type;
2268 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002269 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002270 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2271 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002272 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002273 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2274 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002275 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002276 srtp_counter_type = kEnumCounterDataSrtpCipher;
2277 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002278 } else {
2279 RTC_NOTREACHED();
2280 return;
2281 }
2282
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002283 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2284 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2285 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002286 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002287 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2288 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2289 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002290 }
2291}
2292
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002293void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002294 RTC_DCHECK(worker_thread()->IsCurrent());
2295 media_controller_->call_w()->OnSentPacket(sent_packet);
2296}
2297
zhihuang9763d562016-08-05 11:14:50 -07002298const std::string WebRtcSession::GetTransportName(
2299 const std::string& content_name) {
2300 cricket::BaseChannel* channel = GetChannel(content_name);
2301 if (!channel) {
2302#ifdef HAVE_QUIC
2303 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2304 content_name == quic_data_transport_->transport_name()) {
2305 return quic_data_transport_->transport_name();
2306 }
2307#endif
deadbeef953c2ce2017-01-09 14:53:41 -08002308 if (sctp_transport_) {
2309 RTC_DCHECK(sctp_content_name_);
2310 RTC_DCHECK(sctp_transport_name_);
2311 if (content_name == *sctp_content_name_) {
2312 return *sctp_transport_name_;
2313 }
2314 }
zhihuang9763d562016-08-05 11:14:50 -07002315 // Return an empty string if failed to retrieve the transport name.
2316 return "";
2317 }
2318 return channel->transport_name();
2319}
zhihuangd82eee02016-08-26 11:25:05 -07002320
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321} // namespace webrtc