blob: dad485c9bd378e135c5c5cd0832fadd2dd5aaf1d [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"
deadbeef67b3bbe2017-01-04 18:38:02 -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: ";
deadbeef67b3bbe2017-01-04 18:38:02 -080078const char kDtlsSrtpSetupFailureRtp[] =
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000079 "Couldn't set up DTLS-SRTP on RTP channel.";
deadbeef67b3bbe2017-01-04 18:38:02 -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
deadbeef67b3bbe2017-01-04 18:38:02 -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:
396 ASSERT(false);
397 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:
deadbeefd59daf82015-10-14 15:02:44 -0700414 RTC_DCHECK(false);
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,
deadbeef67b3bbe2017-01-04 18:38:02 -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)),
deadbeef67b3bbe2017-01-04 18:38:02 -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(
deadbeef67b3bbe2017-01-04 18:38:02 -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 }
deadbeef67b3bbe2017-01-04 18:38:02 -0800516 if (rtp_data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 SignalDataChannelDestroyed();
deadbeef67b3bbe2017-01-04 18:38:02 -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);
deadbeef67b3bbe2017-01-04 18:38:02 -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 }
deadbeef67b3bbe2017-01-04 18:38:02 -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
deadbeef67b3bbe2017-01-04 18:38:02 -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()) {
deadbeef67b3bbe2017-01-04 18:38:02 -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
deadbeef67b3bbe2017-01-04 18:38:02 -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
deadbeef67b3bbe2017-01-04 18:38:02 -0800967 bool ret = (set_content(voice_channel()) && set_content(video_channel()) &&
968 set_content(rtp_data_channel()));
969 // Need complete offer/answer before starting SCTP, according to
970 // https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
971 if (sctp_transport_ && local_description() && remote_description()) {
972 ret &= network_thread_->Invoke<bool>(
973 RTC_FROM_HERE,
974 rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source));
975 }
976 return ret;
977}
978
979bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) {
980 RTC_DCHECK(network_thread_->IsCurrent());
981 RTC_DCHECK(local_description());
982 RTC_DCHECK(remote_description());
983 // Apply the SCTP port (which is hidden inside a DataCodec structure...)
984 // When we support "max-message-size", that would also be pushed down here.
985 return sctp_transport_->Start(
986 GetSctpPort(local_description()->description()),
987 GetSctpPort(remote_description()->description()));
deadbeefd59daf82015-10-14 15:02:44 -0700988}
989
990bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
991 cricket::ContentAction action,
992 std::string* error_desc) {
993 RTC_DCHECK(signaling_thread()->IsCurrent());
994
995 if (source == cricket::CS_LOCAL) {
deadbeeffe4a8a42016-12-20 17:56:17 -0800996 return PushdownLocalTransportDescription(local_description()->description(),
997 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -0700998 }
deadbeeffe4a8a42016-12-20 17:56:17 -0800999 return PushdownRemoteTransportDescription(remote_description()->description(),
1000 action, error_desc);
deadbeefd59daf82015-10-14 15:02:44 -07001001}
1002
1003bool WebRtcSession::PushdownLocalTransportDescription(
1004 const SessionDescription* sdesc,
1005 cricket::ContentAction action,
1006 std::string* err) {
1007 RTC_DCHECK(signaling_thread()->IsCurrent());
1008
1009 if (!sdesc) {
1010 return false;
1011 }
1012
1013 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1014 if (!transport_controller_->SetLocalTransportDescription(
1015 tinfo.content_name, tinfo.description, action, err)) {
1016 return false;
1017 }
1018 }
1019
1020 return true;
1021}
1022
1023bool WebRtcSession::PushdownRemoteTransportDescription(
1024 const SessionDescription* sdesc,
1025 cricket::ContentAction action,
1026 std::string* err) {
1027 RTC_DCHECK(signaling_thread()->IsCurrent());
1028
1029 if (!sdesc) {
1030 return false;
1031 }
1032
1033 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1034 if (!transport_controller_->SetRemoteTransportDescription(
1035 tinfo.content_name, tinfo.description, action, err)) {
1036 return false;
1037 }
1038 }
1039
1040 return true;
1041}
1042
1043bool WebRtcSession::GetTransportDescription(
1044 const SessionDescription* description,
1045 const std::string& content_name,
1046 cricket::TransportDescription* tdesc) {
1047 if (!description || !tdesc) {
1048 return false;
1049 }
1050 const TransportInfo* transport_info =
1051 description->GetTransportInfoByName(content_name);
1052 if (!transport_info) {
1053 return false;
1054 }
1055 *tdesc = transport_info->description;
1056 return true;
1057}
1058
deadbeefcbecd352015-09-23 11:50:27 -07001059bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1060 const std::string* first_content_name = bundle.FirstContentName();
1061 if (!first_content_name) {
1062 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1063 return false;
1064 }
1065 const std::string& transport_name = *first_content_name;
deadbeefcbecd352015-09-23 11:50:27 -07001066
zhihuang9763d562016-08-05 11:14:50 -07001067#ifdef HAVE_QUIC
1068 if (quic_data_transport_ &&
1069 bundle.HasContentName(quic_data_transport_->content_name()) &&
1070 quic_data_transport_->transport_name() != transport_name) {
1071 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1072 << " on " << transport_name << "with QUIC.";
1073 }
1074#endif
1075
deadbeef67b3bbe2017-01-04 18:38:02 -08001076 auto maybe_set_transport = [this, bundle,
1077 transport_name](cricket::BaseChannel* ch) {
deadbeefcbecd352015-09-23 11:50:27 -07001078 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001079 return true;
1080 }
1081
deadbeefcbecd352015-09-23 11:50:27 -07001082 if (ch->transport_name() == transport_name) {
1083 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1084 << " on " << transport_name << ".";
1085 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001086 }
torbjornga81a42f2015-09-23 02:16:58 -07001087
deadbeefcbecd352015-09-23 11:50:27 -07001088 if (!ch->SetTransport(transport_name)) {
1089 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1090 return false;
1091 }
1092 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1093 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001094 return true;
1095 };
1096
deadbeefcbecd352015-09-23 11:50:27 -07001097 if (!maybe_set_transport(voice_channel()) ||
1098 !maybe_set_transport(video_channel()) ||
deadbeef67b3bbe2017-01-04 18:38:02 -08001099 !maybe_set_transport(rtp_data_channel())) {
deadbeefcbecd352015-09-23 11:50:27 -07001100 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001101 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001102 // For SCTP, transport creation/deletion happens here instead of in the
1103 // object itself.
1104 if (sctp_transport_) {
1105 RTC_DCHECK(sctp_transport_name_);
1106 RTC_DCHECK(sctp_content_name_);
1107 if (transport_name != *sctp_transport_name_ &&
1108 bundle.HasContentName(*sctp_content_name_)) {
1109 network_thread_->Invoke<void>(
1110 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this,
1111 transport_name));
1112 }
1113 }
deadbeefcbecd352015-09-23 11:50:27 -07001114
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001115 return true;
1116}
1117
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001119 if (!remote_description()) {
deadbeefd59daf82015-10-14 15:02:44 -07001120 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1121 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001122 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 }
1124
1125 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001126 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 return false;
1128 }
1129
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001130 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001131 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1132 if (!valid) {
1133 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 }
1135
1136 // Add this candidate to the remote session description.
deadbeeffe4a8a42016-12-20 17:56:17 -08001137 if (!mutable_remote_description()->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001138 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 return false;
1140 }
1141
deadbeefd59daf82015-10-14 15:02:44 -07001142 if (ready) {
1143 return UseCandidate(candidate);
1144 } else {
1145 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1146 return true;
1147 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148}
1149
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001150bool WebRtcSession::RemoveRemoteIceCandidates(
1151 const std::vector<cricket::Candidate>& candidates) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001152 if (!remote_description()) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001153 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1154 << "removed without any remote session description.";
1155 return false;
1156 }
1157
1158 if (candidates.empty()) {
1159 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1160 return false;
1161 }
1162
deadbeeffe4a8a42016-12-20 17:56:17 -08001163 size_t number_removed =
1164 mutable_remote_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001165 if (number_removed != candidates.size()) {
1166 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1167 << "Requested " << candidates.size() << " but only "
1168 << number_removed << " are removed.";
1169 }
1170
1171 // Remove the candidates from the transport controller.
1172 std::string error;
1173 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1174 if (!res && !error.empty()) {
1175 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1176 }
1177 return true;
1178}
1179
deadbeefd59daf82015-10-14 15:02:44 -07001180cricket::IceConfig WebRtcSession::ParseIceConfig(
1181 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001182 cricket::ContinualGatheringPolicy gathering_policy;
1183 // TODO(honghaiz): Add the third continual gathering policy in
1184 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1185 switch (config.continual_gathering_policy) {
1186 case PeerConnectionInterface::GATHER_ONCE:
1187 gathering_policy = cricket::GATHER_ONCE;
1188 break;
1189 case PeerConnectionInterface::GATHER_CONTINUALLY:
1190 gathering_policy = cricket::GATHER_CONTINUALLY;
1191 break;
1192 default:
1193 RTC_DCHECK(false);
1194 gathering_policy = cricket::GATHER_ONCE;
1195 }
deadbeefd59daf82015-10-14 15:02:44 -07001196 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001197 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001198 ice_config.prioritize_most_likely_candidate_pairs =
1199 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001200 ice_config.backup_connection_ping_interval =
1201 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001202 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001203 ice_config.presume_writable_when_fully_relayed =
1204 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001205 return ice_config;
1206}
1207
1208void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1209 transport_controller_->SetIceConfig(config);
1210}
1211
1212void WebRtcSession::MaybeStartGathering() {
1213 transport_controller_->MaybeStartGathering();
1214}
1215
Peter Boström0c4e06b2015-10-07 12:23:21 +02001216bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1217 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001218 if (!local_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001220 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001221 return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc,
1222 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223}
1224
Peter Boström0c4e06b2015-10-07 12:23:21 +02001225bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1226 std::string* track_id) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001227 if (!remote_description()) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001228 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001229 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001230 return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc,
1231 track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232}
1233
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001234std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001236 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 return desc.str();
1238}
1239
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1241 ASSERT(signaling_thread()->IsCurrent());
1242 if (!voice_channel_) {
1243 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1244 return false;
1245 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001246 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1248 // exists.
deadbeeffe4a8a42016-12-20 17:56:17 -08001249 if (!local_description() ||
1250 !GetAudioSsrcByTrackId(local_description()->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 &send_ssrc)) {
1252 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1253 return false;
1254 }
1255 return voice_channel_->CanInsertDtmf();
1256}
1257
1258bool WebRtcSession::InsertDtmf(const std::string& track_id,
1259 int code, int duration) {
1260 ASSERT(signaling_thread()->IsCurrent());
1261 if (!voice_channel_) {
1262 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1263 return false;
1264 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001265 uint32_t send_ssrc = 0;
deadbeeffe4a8a42016-12-20 17:56:17 -08001266 if (!VERIFY(local_description() &&
1267 GetAudioSsrcByTrackId(local_description()->description(),
1268 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1270 return false;
1271 }
solenberg1d63dd02015-12-02 12:35:09 -08001272 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1274 return false;
1275 }
1276 return true;
1277}
1278
1279sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001280 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281}
1282
wu@webrtc.org78187522013-10-07 23:32:02 +00001283bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001284 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001285 cricket::SendDataResult* result) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001286 if (!rtp_data_channel_ && !sctp_transport_) {
1287 LOG(LS_ERROR) << "SendData called when rtp_data_channel_ "
1288 << "and sctp_transport_ are NULL.";
wu@webrtc.org78187522013-10-07 23:32:02 +00001289 return false;
1290 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001291 return rtp_data_channel_
1292 ? rtp_data_channel_->SendData(params, payload, result)
1293 : network_thread_->Invoke<bool>(
1294 RTC_FROM_HERE,
1295 Bind(&cricket::SctpTransportInternal::SendData,
1296 sctp_transport_.get(), params, payload, result));
wu@webrtc.org78187522013-10-07 23:32:02 +00001297}
1298
1299bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001300 if (!rtp_data_channel_ && !sctp_transport_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001301 // Don't log an error here, because DataChannels are expected to call
1302 // ConnectDataChannel in this state. It's the only way to initially tell
1303 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001304 return false;
1305 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001306 if (rtp_data_channel_) {
1307 rtp_data_channel_->SignalReadyToSendData.connect(
1308 webrtc_data_channel, &DataChannel::OnChannelReady);
1309 rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1310 &DataChannel::OnDataReceived);
1311 } else {
1312 SignalSctpReadyToSendData.connect(webrtc_data_channel,
1313 &DataChannel::OnChannelReady);
1314 SignalSctpDataReceived.connect(webrtc_data_channel,
1315 &DataChannel::OnDataReceived);
1316 SignalSctpStreamClosedRemotely.connect(
1317 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
1318 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001319 return true;
1320}
1321
1322void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001323 if (!rtp_data_channel_ && !sctp_transport_) {
1324 LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and "
1325 "sctp_transport_ are NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001326 return;
1327 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001328 if (rtp_data_channel_) {
1329 rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1330 rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1331 } else {
1332 SignalSctpReadyToSendData.disconnect(webrtc_data_channel);
1333 SignalSctpDataReceived.disconnect(webrtc_data_channel);
1334 SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel);
1335 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001336}
1337
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001338void WebRtcSession::AddSctpDataStream(int sid) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001339 if (!sctp_transport_) {
1340 LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL.";
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001341 return;
1342 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001343 network_thread_->Invoke<void>(
1344 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream,
1345 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001346}
1347
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001348void WebRtcSession::RemoveSctpDataStream(int sid) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001349 if (!sctp_transport_) {
1350 LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is "
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001351 << "NULL.";
1352 return;
1353 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001354 network_thread_->Invoke<void>(
1355 RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream,
1356 sctp_transport_.get(), sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001357}
1358
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001359bool WebRtcSession::ReadyToSendData() const {
deadbeef67b3bbe2017-01-04 18:38:02 -08001360 return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) ||
1361 sctp_ready_to_send_data_;
1362}
1363
1364std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
1365 ASSERT(signaling_thread()->IsCurrent());
1366 ChannelNamePairs channel_name_pairs;
1367 if (voice_channel()) {
1368 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1369 voice_channel()->content_name(), voice_channel()->transport_name()));
1370 }
1371 if (video_channel()) {
1372 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
1373 video_channel()->content_name(), video_channel()->transport_name()));
1374 }
1375 if (rtp_data_channel()) {
1376 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1377 ChannelNamePair(rtp_data_channel()->content_name(),
1378 rtp_data_channel()->transport_name()));
1379 }
1380 if (sctp_transport_) {
1381 RTC_DCHECK(sctp_content_name_);
1382 RTC_DCHECK(sctp_transport_name_);
1383 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(
1384 ChannelNamePair(*sctp_content_name_, *sctp_transport_name_));
1385 }
1386 return GetStats(channel_name_pairs);
1387}
1388
1389std::unique_ptr<SessionStats> WebRtcSession::GetStats(
1390 const ChannelNamePairs& channel_name_pairs) {
1391 if (network_thread()->IsCurrent()) {
1392 return GetStats_n(channel_name_pairs);
1393 }
1394 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
1395 RTC_FROM_HERE,
1396 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
1397}
1398
1399bool WebRtcSession::GetLocalCertificate(
1400 const std::string& transport_name,
1401 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1402 return transport_controller_->GetLocalCertificate(transport_name,
1403 certificate);
1404}
1405
1406std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
1407 const std::string& transport_name) {
1408 return transport_controller_->GetRemoteSSLCertificate(transport_name);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001409}
1410
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411cricket::DataChannelType WebRtcSession::data_channel_type() const {
1412 return data_channel_type_;
1413}
1414
deadbeef0ed85b22016-02-23 17:24:52 -08001415bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1416 return pending_ice_restarts_.find(content_name) !=
1417 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001418}
1419
deadbeefd1a38b52016-12-10 13:15:33 -08001420void WebRtcSession::SetNeedsIceRestartFlag() {
1421 transport_controller_->SetNeedsIceRestartFlag();
1422}
1423
1424bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1425 return transport_controller_->NeedsIceRestart(content_name);
1426}
1427
Henrik Boströmd8281982015-08-27 10:12:24 +02001428void WebRtcSession::OnCertificateReady(
1429 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001430 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001431}
1432
deadbeef67b3bbe2017-01-04 18:38:02 -08001433void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) {
1434 SetError(ERROR_TRANSPORT,
1435 rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp);
1436}
1437
Henrik Boströmd8281982015-08-27 10:12:24 +02001438bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001439 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001440}
1441
deadbeefcbecd352015-09-23 11:50:27 -07001442const rtc::scoped_refptr<rtc::RTCCertificate>&
1443WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001444 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001445}
1446
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447void WebRtcSession::SetIceConnectionState(
1448 PeerConnectionInterface::IceConnectionState state) {
1449 if (ice_connection_state_ == state) {
1450 return;
1451 }
1452
deadbeefcbecd352015-09-23 11:50:27 -07001453 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1454 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001455 RTC_DCHECK(ice_connection_state_ !=
1456 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 ice_connection_state_ = state;
1458 if (ice_observer_) {
1459 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1460 }
1461}
1462
deadbeefcbecd352015-09-23 11:50:27 -07001463void WebRtcSession::OnTransportControllerConnectionState(
1464 cricket::IceConnectionState state) {
1465 switch (state) {
1466 case cricket::kIceConnectionConnecting:
1467 // If the current state is Connected or Completed, then there were
1468 // writable channels but now there are not, so the next state must
1469 // be Disconnected.
1470 // kIceConnectionConnecting is currently used as the default,
1471 // un-connected state by the TransportController, so its only use is
1472 // detecting disconnections.
1473 if (ice_connection_state_ ==
1474 PeerConnectionInterface::kIceConnectionConnected ||
1475 ice_connection_state_ ==
1476 PeerConnectionInterface::kIceConnectionCompleted) {
1477 SetIceConnectionState(
1478 PeerConnectionInterface::kIceConnectionDisconnected);
1479 }
torbjornga81a42f2015-09-23 02:16:58 -07001480 break;
deadbeefcbecd352015-09-23 11:50:27 -07001481 case cricket::kIceConnectionFailed:
1482 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1483 break;
1484 case cricket::kIceConnectionConnected:
1485 LOG(LS_INFO) << "Changing to ICE connected state because "
1486 << "all transports are writable.";
1487 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1488 break;
1489 case cricket::kIceConnectionCompleted:
1490 LOG(LS_INFO) << "Changing to ICE completed state because "
1491 << "all transports are complete.";
1492 if (ice_connection_state_ !=
1493 PeerConnectionInterface::kIceConnectionConnected) {
1494 // If jumping directly from "checking" to "connected",
1495 // signal "connected" first.
1496 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1497 }
1498 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1499 if (metrics_observer_) {
1500 ReportTransportStats();
1501 }
1502 break;
1503 default:
1504 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001505 }
deadbeefcbecd352015-09-23 11:50:27 -07001506}
1507
1508void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001509 SetIceConnectionReceiving(receiving);
1510}
1511
1512void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1513 if (ice_connection_receiving_ == receiving) {
1514 return;
1515 }
1516 ice_connection_receiving_ = receiving;
1517 if (ice_observer_) {
1518 ice_observer_->OnIceConnectionReceivingChange(receiving);
1519 }
1520}
1521
deadbeefcbecd352015-09-23 11:50:27 -07001522void WebRtcSession::OnTransportControllerCandidatesGathered(
1523 const std::string& transport_name,
1524 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001526 int sdp_mline_index;
1527 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1528 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1529 << transport_name << " not found";
1530 return;
1531 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532
deadbeefcbecd352015-09-23 11:50:27 -07001533 for (cricket::Candidates::const_iterator citer = candidates.begin();
1534 citer != candidates.end(); ++citer) {
1535 // Use transport_name as the candidate media id.
1536 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1537 if (ice_observer_) {
1538 ice_observer_->OnIceCandidate(&candidate);
1539 }
deadbeeffe4a8a42016-12-20 17:56:17 -08001540 if (local_description()) {
1541 mutable_local_description()->AddCandidate(&candidate);
deadbeefcbecd352015-09-23 11:50:27 -07001542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 }
1544}
1545
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001546void WebRtcSession::OnTransportControllerCandidatesRemoved(
1547 const std::vector<cricket::Candidate>& candidates) {
1548 ASSERT(signaling_thread()->IsCurrent());
1549 // Sanity check.
1550 for (const cricket::Candidate& candidate : candidates) {
1551 if (candidate.transport_name().empty()) {
1552 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1553 << "empty content name in candidate "
1554 << candidate.ToString();
1555 return;
1556 }
1557 }
1558
deadbeeffe4a8a42016-12-20 17:56:17 -08001559 if (local_description()) {
1560 mutable_local_description()->RemoveCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001561 }
1562 if (ice_observer_) {
1563 ice_observer_->OnIceCandidatesRemoved(candidates);
1564 }
1565}
1566
deadbeef67b3bbe2017-01-04 18:38:02 -08001567void WebRtcSession::OnTransportControllerDtlsHandshakeError(
1568 rtc::SSLHandshakeError error) {
1569 if (metrics_observer_) {
1570 metrics_observer_->IncrementEnumCounter(
1571 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
1572 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
1573 }
1574}
1575
1576// Enabling voice and video (and RTP data) channel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577void WebRtcSession::EnableChannels() {
1578 if (voice_channel_ && !voice_channel_->enabled())
1579 voice_channel_->Enable(true);
1580
1581 if (video_channel_ && !video_channel_->enabled())
1582 video_channel_->Enable(true);
1583
deadbeef67b3bbe2017-01-04 18:38:02 -08001584 if (rtp_data_channel_ && !rtp_data_channel_->enabled())
1585 rtp_data_channel_->Enable(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586}
1587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588// Returns the media index for a local ice candidate given the content name.
1589bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1590 int* sdp_mline_index) {
deadbeeffe4a8a42016-12-20 17:56:17 -08001591 if (!local_description() || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594
1595 bool content_found = false;
deadbeeffe4a8a42016-12-20 17:56:17 -08001596 const ContentInfos& contents = local_description()->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 for (size_t index = 0; index < contents.size(); ++index) {
1598 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001599 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 content_found = true;
1601 break;
1602 }
1603 }
1604 return content_found;
1605}
1606
1607bool WebRtcSession::UseCandidatesInSessionDescription(
1608 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001609 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001611 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001613
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1615 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001616 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001617 const IceCandidateInterface* candidate = candidates->at(n);
1618 bool valid = false;
1619 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1620 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001621 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1622 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001623 }
1624 continue;
1625 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001626 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001627 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 break;
deadbeefd59daf82015-10-14 15:02:44 -07001629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 }
1631 }
1632 return ret;
1633}
1634
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001635bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeeffe4a8a42016-12-20 17:56:17 -08001637 size_t remote_content_size =
1638 remote_description()->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001640 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 return false;
1642 }
1643
1644 cricket::ContentInfo content =
deadbeeffe4a8a42016-12-20 17:56:17 -08001645 remote_description()->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 std::vector<cricket::Candidate> candidates;
1647 candidates.push_back(candidate->candidate());
1648 // Invoking BaseSession method to handle remote candidates.
1649 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001650 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1651 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 // Candidates successfully submitted for checking.
1653 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1654 ice_connection_state_ ==
1655 PeerConnectionInterface::kIceConnectionDisconnected) {
1656 // If state is New, then the session has just gotten its first remote ICE
1657 // candidates, so go to Checking.
1658 // If state is Disconnected, the session is re-using old candidates or
1659 // receiving additional ones, so go to Checking.
1660 // If state is Connected, stay Connected.
1661 // TODO(bemasc): If state is Connected, and the new candidates are for a
1662 // newly added transport, then the state actually _should_ move to
1663 // checking. Add a way to distinguish that case.
1664 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1665 }
1666 // TODO(bemasc): If state is Completed, go back to Connected.
1667 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001668 if (!error.empty()) {
1669 LOG(LS_WARNING) << error;
1670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 }
1672 return true;
1673}
1674
deadbeefcbecd352015-09-23 11:50:27 -07001675void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001676 // Destroy video_channel_ first since it may have a pointer to the
1677 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 const cricket::ContentInfo* video_info =
1679 cricket::GetFirstVideoContent(desc);
1680 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 }
1684
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001685 const cricket::ContentInfo* voice_info =
1686 cricket::GetFirstAudioContent(desc);
1687 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001688 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001689 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001690 }
1691
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 const cricket::ContentInfo* data_info =
1693 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001694 if (!data_info || data_info->rejected) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001695 if (rtp_data_channel_) {
zhihuang9763d562016-08-05 11:14:50 -07001696 SignalDataChannelDestroyed();
deadbeef67b3bbe2017-01-04 18:38:02 -08001697 channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release());
1698 }
1699 if (sctp_transport_) {
1700 SignalDataChannelDestroyed();
1701 network_thread_->Invoke<void>(
1702 RTC_FROM_HERE,
1703 rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this));
zhihuang9763d562016-08-05 11:14:50 -07001704 }
1705#ifdef HAVE_QUIC
1706 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1707 if (quic_data_transport_) {
1708 quic_data_transport_.reset();
1709 }
1710#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 }
1712}
1713
skvlad6c87a672016-05-17 17:49:52 -07001714// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1715// if the channel is not part of any bundle.
1716const std::string* WebRtcSession::GetBundleTransportName(
1717 const cricket::ContentInfo* content,
1718 const cricket::ContentGroup* bundle) {
1719 if (!bundle) {
1720 return nullptr;
1721 }
1722 const std::string* first_content_name = bundle->FirstContentName();
1723 if (!first_content_name) {
1724 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1725 return nullptr;
1726 }
1727 if (!bundle->HasContentName(content->name)) {
1728 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1729 return nullptr;
1730 }
1731 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1732 return first_content_name;
1733}
1734
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001736 const cricket::ContentGroup* bundle_group = nullptr;
1737 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1738 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1739 if (!bundle_group) {
1740 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1741 return false;
1742 }
1743 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 // Creating the media channels and transport proxies.
1745 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1746 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001747 if (!CreateVoiceChannel(voice,
1748 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 LOG(LS_ERROR) << "Failed to create voice channel.";
1750 return false;
1751 }
1752 }
1753
1754 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1755 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001756 if (!CreateVideoChannel(video,
1757 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 LOG(LS_ERROR) << "Failed to create video channel.";
1759 return false;
1760 }
1761 }
1762
1763 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
deadbeef67b3bbe2017-01-04 18:38:02 -08001764 if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected &&
1765 !rtp_data_channel_ && !sctp_transport_) {
skvlad6c87a672016-05-17 17:49:52 -07001766 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 LOG(LS_ERROR) << "Failed to create data channel.";
1768 return false;
1769 }
1770 }
1771
1772 return true;
1773}
1774
skvlad6c87a672016-05-17 17:49:52 -07001775bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1776 const std::string* bundle_transport) {
1777 bool require_rtcp_mux =
1778 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1779 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001781 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001782 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1783 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001784 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001785 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001786 }
skvlad6c87a672016-05-17 17:49:52 -07001787 if (require_rtcp_mux) {
1788 voice_channel_->ActivateRtcpMux();
1789 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001790
deadbeef67b3bbe2017-01-04 18:38:02 -08001791 voice_channel_->SignalDtlsSrtpSetupFailure.connect(
1792 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001793
1794 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001795 voice_channel_->SignalSentPacket.connect(this,
1796 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001797 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798}
1799
skvlad6c87a672016-05-17 17:49:52 -07001800bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1801 const std::string* bundle_transport) {
1802 bool require_rtcp_mux =
1803 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1804 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001806 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001807 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1808 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001809 if (!video_channel_) {
1810 return false;
1811 }
skvlad6c87a672016-05-17 17:49:52 -07001812 if (require_rtcp_mux) {
1813 video_channel_->ActivateRtcpMux();
1814 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001815 video_channel_->SignalDtlsSrtpSetupFailure.connect(
1816 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001817
1818 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001819 video_channel_->SignalSentPacket.connect(this,
1820 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001821 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822}
1823
skvlad6c87a672016-05-17 17:49:52 -07001824bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1825 const std::string* bundle_transport) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001826 const std::string transport_name =
1827 bundle_transport ? *bundle_transport : content->name;
zhihuang9763d562016-08-05 11:14:50 -07001828#ifdef HAVE_QUIC
1829 if (data_channel_type_ == cricket::DCT_QUIC) {
1830 RTC_DCHECK(transport_controller_->quic());
zhihuang9763d562016-08-05 11:14:50 -07001831 quic_data_transport_->SetTransport(transport_name);
1832 return true;
1833 }
1834#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001835 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001836 if (sctp) {
deadbeef67b3bbe2017-01-04 18:38:02 -08001837 if (!sctp_factory_) {
1838 LOG(LS_ERROR)
1839 << "Trying to create SCTP transport, but didn't compile with "
1840 "SCTP support (HAVE_SCTP)";
1841 return false;
1842 }
1843 if (!network_thread_->Invoke<bool>(
1844 RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n,
1845 this, content->name, transport_name))) {
1846 return false;
1847 };
1848 } else {
1849 bool require_rtcp_mux =
1850 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1851 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
1852 rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel(
1853 media_controller_, transport_controller_.get(), content->name,
1854 bundle_transport, create_rtcp_transport_channel, SrtpRequired()));
1855 if (!rtp_data_channel_) {
1856 return false;
1857 }
1858 if (require_rtcp_mux) {
1859 rtp_data_channel_->ActivateRtcpMux();
1860 }
1861 rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect(
1862 this, &WebRtcSession::OnDtlsSrtpSetupFailure);
1863 rtp_data_channel_->SignalSentPacket.connect(this,
1864 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001865 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001866
deadbeefab9b2d12015-10-14 11:33:11 -07001867 SignalDataChannelCreated();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001868 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869}
1870
hbosdf6075a2016-12-19 04:58:02 -08001871std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1872 const ChannelNamePairs& channel_name_pairs) {
1873 ASSERT(network_thread()->IsCurrent());
1874 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1875 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1876 &channel_name_pairs.video,
1877 &channel_name_pairs.data }) {
1878 if (*channel_name_pair) {
1879 cricket::TransportStats transport_stats;
1880 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1881 &transport_stats)) {
1882 return nullptr;
1883 }
1884 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1885 (*channel_name_pair)->transport_name;
1886 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1887 std::move(transport_stats);
1888 }
1889 }
1890 return session_stats;
1891}
1892
deadbeef67b3bbe2017-01-04 18:38:02 -08001893bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name,
1894 const std::string& transport_name) {
1895 RTC_DCHECK(network_thread_->IsCurrent());
1896 RTC_DCHECK(sctp_factory_);
1897 cricket::TransportChannel* tc =
1898 transport_controller_->CreateTransportChannel_n(
1899 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1900 sctp_transport_ = sctp_factory_->CreateSctpTransport(tc);
1901 RTC_DCHECK(sctp_transport_);
1902 sctp_invoker_.reset(new rtc::AsyncInvoker());
1903 sctp_transport_->SignalReadyToSendData.connect(
1904 this, &WebRtcSession::OnSctpTransportReadyToSendData_n);
1905 sctp_transport_->SignalDataReceived.connect(
1906 this, &WebRtcSession::OnSctpTransportDataReceived_n);
1907 sctp_transport_->SignalStreamClosedRemotely.connect(
1908 this, &WebRtcSession::OnSctpStreamClosedRemotely_n);
1909 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1910 sctp_content_name_ = rtc::Optional<std::string>(content_name);
1911 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912}
1913
deadbeef67b3bbe2017-01-04 18:38:02 -08001914void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) {
1915 RTC_DCHECK(network_thread_->IsCurrent());
1916 RTC_DCHECK(sctp_transport_);
1917 RTC_DCHECK(sctp_transport_name_);
1918 std::string old_sctp_transport_name = *sctp_transport_name_;
1919 sctp_transport_name_ = rtc::Optional<std::string>(transport_name);
1920 cricket::TransportChannel* tc =
1921 transport_controller_->CreateTransportChannel_n(
1922 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1923 sctp_transport_->SetTransportChannel(tc);
1924 transport_controller_->DestroyTransportChannel_n(
1925 old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1926}
1927
1928void WebRtcSession::DestroySctpTransport_n() {
1929 RTC_DCHECK(network_thread_->IsCurrent());
1930 sctp_transport_.reset(nullptr);
1931 sctp_content_name_.reset();
1932 sctp_transport_name_.reset();
1933 sctp_invoker_.reset(nullptr);
1934 sctp_ready_to_send_data_ = false;
1935}
1936
1937void WebRtcSession::OnSctpTransportReadyToSendData_n() {
1938 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1939 RTC_DCHECK(network_thread_->IsCurrent());
1940 sctp_invoker_->AsyncInvoke<void>(
1941 RTC_FROM_HERE, signaling_thread_,
1942 rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true));
1943}
1944
1945void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) {
1946 RTC_DCHECK(signaling_thread_->IsCurrent());
1947 sctp_ready_to_send_data_ = ready;
1948 SignalSctpReadyToSendData(ready);
1949}
1950
1951void WebRtcSession::OnSctpTransportDataReceived_n(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001952 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001953 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001954 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
deadbeef67b3bbe2017-01-04 18:38:02 -08001955 RTC_DCHECK(network_thread_->IsCurrent());
1956 sctp_invoker_->AsyncInvoke<void>(
1957 RTC_FROM_HERE, signaling_thread_,
1958 rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params,
1959 payload));
1960}
1961
1962void WebRtcSession::OnSctpTransportDataReceived_s(
1963 const cricket::ReceiveDataParams& params,
1964 const rtc::CopyOnWriteBuffer& payload) {
1965 RTC_DCHECK(signaling_thread_->IsCurrent());
deadbeefab9b2d12015-10-14 11:33:11 -07001966 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1967 // Received OPEN message; parse and signal that a new data channel should
1968 // be created.
1969 std::string label;
1970 InternalDataChannelInit config;
1971 config.id = params.ssrc;
1972 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1973 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1974 << params.ssrc;
1975 return;
1976 }
1977 config.open_handshake_role = InternalDataChannelInit::kAcker;
1978 SignalDataChannelOpenMessage(label, config);
deadbeef67b3bbe2017-01-04 18:38:02 -08001979 } else {
1980 // Otherwise just forward the signal.
1981 SignalSctpDataReceived(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 }
deadbeef67b3bbe2017-01-04 18:38:02 -08001983}
1984
1985void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) {
1986 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1987 RTC_DCHECK(network_thread_->IsCurrent());
1988 sctp_invoker_->AsyncInvoke<void>(
1989 RTC_FROM_HERE, signaling_thread_,
1990 rtc::Bind(&sigslot::signal1<int>::operator(),
1991 &SignalSctpStreamClosedRemotely, sid));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992}
1993
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001994// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001995bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001996 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1997 if (!bundle_enabled)
1998 return true;
1999
2000 const cricket::ContentGroup* bundle_group =
2001 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
2002 ASSERT(bundle_group != NULL);
2003
2004 const cricket::ContentInfos& contents = desc->contents();
2005 for (cricket::ContentInfos::const_iterator citer = contents.begin();
2006 citer != contents.end(); ++citer) {
2007 const cricket::ContentInfo* content = (&*citer);
2008 ASSERT(content != NULL);
2009 if (bundle_group->HasContentName(content->name) &&
2010 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
2011 if (!HasRtcpMuxEnabled(content))
2012 return false;
2013 }
2014 }
2015 // RTCP-MUX is enabled in all the contents.
2016 return true;
2017}
2018
2019bool WebRtcSession::HasRtcpMuxEnabled(
2020 const cricket::ContentInfo* content) {
2021 const cricket::MediaContentDescription* description =
2022 static_cast<cricket::MediaContentDescription*>(content->description);
2023 return description->rtcp_mux();
2024}
2025
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002026bool WebRtcSession::ValidateSessionDescription(
2027 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002028 cricket::ContentSource source, std::string* err_desc) {
2029 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07002030 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002031 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002032 }
2033
2034 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002035 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002036 }
2037
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002038 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002039 Action action = GetAction(sdesc->type());
2040 if (source == cricket::CS_LOCAL) {
2041 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002042 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002043 } else {
2044 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002045 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002046 }
2047
2048 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002049 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00002050 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
2051 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002052 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002053 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002054 }
2055
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002056 // Verify ice-ufrag and ice-pwd.
2057 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002058 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002059 }
2060
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002061 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002062 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002063 }
2064
skvlad6c87a672016-05-17 17:49:52 -07002065 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
2066 // m-lines that do not rtcp-mux enabled.
2067
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002068 // Verify m-lines in Answer when compared against Offer.
2069 if (action == kAnswer) {
2070 const cricket::SessionDescription* offer_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002071 (source == cricket::CS_LOCAL) ? remote_description()->description()
2072 : local_description()->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002073 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002074 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002075 }
2076 }
2077
2078 return true;
2079}
2080
2081bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2082 return ((action == kOffer && state() == STATE_INIT) ||
2083 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002084 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002085 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002086 (action == kOffer && state() == STATE_INPROGRESS) ||
2087 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002088 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2089 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2090 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2091 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002092}
2093
2094bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2095 return ((action == kOffer && state() == STATE_INIT) ||
2096 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002097 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002098 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002099 (action == kOffer && state() == STATE_INPROGRESS) ||
2100 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002101 (action == kAnswer && state() == STATE_SENTOFFER) ||
2102 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2103 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2104 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002105}
2106
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002107std::string WebRtcSession::GetSessionErrorMsg() {
2108 std::ostringstream desc;
2109 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2110 desc << kSessionErrorDesc << error_desc() << ".";
2111 return desc.str();
2112}
2113
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002114// We need to check the local/remote description for the Transport instead of
2115// the session, because a new Transport added during renegotiation may have
2116// them unset while the session has them set from the previous negotiation.
2117// Not doing so may trigger the auto generation of transport description and
2118// mess up DTLS identity information, ICE credential, etc.
2119bool WebRtcSession::ReadyToUseRemoteCandidate(
2120 const IceCandidateInterface* candidate,
2121 const SessionDescriptionInterface* remote_desc,
2122 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07002123 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002124
2125 const SessionDescriptionInterface* current_remote_desc =
deadbeeffe4a8a42016-12-20 17:56:17 -08002126 remote_desc ? remote_desc : remote_description();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002127
deadbeefd59daf82015-10-14 15:02:44 -07002128 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002129 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002130 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002131
2132 size_t mediacontent_index =
2133 static_cast<size_t>(candidate->sdp_mline_index());
2134 size_t remote_content_size =
2135 current_remote_desc->description()->contents().size();
2136 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002137 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2138 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002139
2140 *valid = false;
2141 return false;
2142 }
2143
2144 cricket::ContentInfo content =
2145 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07002146
2147 const std::string transport_name = GetTransportName(content.name);
2148 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07002149 return false;
2150 }
zhihuang9763d562016-08-05 11:14:50 -07002151 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002152}
2153
deadbeef7af91dd2016-12-13 11:29:11 -08002154bool WebRtcSession::SrtpRequired() const {
2155 return dtls_enabled_ ||
2156 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
2157}
2158
deadbeefcbecd352015-09-23 11:50:27 -07002159void WebRtcSession::OnTransportControllerGatheringState(
2160 cricket::IceGatheringState state) {
2161 ASSERT(signaling_thread()->IsCurrent());
2162 if (state == cricket::kIceGatheringGathering) {
2163 if (ice_observer_) {
2164 ice_observer_->OnIceGatheringChange(
2165 PeerConnectionInterface::kIceGatheringGathering);
2166 }
2167 } else if (state == cricket::kIceGatheringComplete) {
2168 if (ice_observer_) {
2169 ice_observer_->OnIceGatheringChange(
2170 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002171 }
2172 }
2173}
2174
2175void WebRtcSession::ReportTransportStats() {
2176 // Use a set so we don't report the same stats twice if two channels share
2177 // a transport.
2178 std::set<std::string> transport_names;
2179 if (voice_channel()) {
2180 transport_names.insert(voice_channel()->transport_name());
2181 }
2182 if (video_channel()) {
2183 transport_names.insert(video_channel()->transport_name());
2184 }
deadbeef67b3bbe2017-01-04 18:38:02 -08002185 if (rtp_data_channel()) {
2186 transport_names.insert(rtp_data_channel()->transport_name());
2187 }
2188 if (sctp_transport_name_) {
2189 transport_names.insert(*sctp_transport_name_);
deadbeefcbecd352015-09-23 11:50:27 -07002190 }
2191 for (const auto& name : transport_names) {
2192 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002193 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002194 ReportBestConnectionState(stats);
2195 ReportNegotiatedCiphers(stats);
2196 }
2197 }
2198}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002199// Walk through the ConnectionInfos to gather best connection usage
2200// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002201void WebRtcSession::ReportBestConnectionState(
2202 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002203 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002204 for (cricket::TransportChannelStatsList::const_iterator it =
2205 stats.channel_stats.begin();
2206 it != stats.channel_stats.end(); ++it) {
2207 for (cricket::ConnectionInfos::const_iterator it_info =
2208 it->connection_infos.begin();
2209 it_info != it->connection_infos.end(); ++it_info) {
2210 if (!it_info->best_connection) {
2211 continue;
2212 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002213
2214 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2215 const cricket::Candidate& local = it_info->local_candidate;
2216 const cricket::Candidate& remote = it_info->remote_candidate;
2217
2218 // Increment the counter for IceCandidatePairType.
2219 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2220 (local.type() == RELAY_PORT_TYPE &&
2221 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2222 type = kEnumCounterIceCandidatePairTypeTcp;
2223 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2224 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002225 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002226 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002227 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002228 metrics_observer_->IncrementEnumCounter(
2229 type, GetIceCandidatePairCounter(local, remote),
2230 kIceCandidatePairMax);
2231
2232 // Increment the counter for IP type.
2233 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002234 metrics_observer_->IncrementEnumCounter(
2235 kEnumCounterAddressFamily, kBestConnections_IPv4,
2236 kPeerConnectionAddressFamilyCounter_Max);
2237
2238 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002239 metrics_observer_->IncrementEnumCounter(
2240 kEnumCounterAddressFamily, kBestConnections_IPv6,
2241 kPeerConnectionAddressFamilyCounter_Max);
2242 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002243 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002244 }
2245
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002246 return;
2247 }
2248 }
2249}
2250
jbauchac8869e2015-07-03 01:36:14 -07002251void WebRtcSession::ReportNegotiatedCiphers(
2252 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002253 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002254 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2255 return;
2256 }
2257
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002258 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2259 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2260 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2261 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002262 return;
2263 }
2264
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002265 PeerConnectionEnumCounterType srtp_counter_type;
2266 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002267 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002268 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2269 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002270 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002271 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2272 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002273 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002274 srtp_counter_type = kEnumCounterDataSrtpCipher;
2275 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002276 } else {
2277 RTC_NOTREACHED();
2278 return;
2279 }
2280
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002281 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2282 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2283 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002284 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002285 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2286 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2287 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002288 }
2289}
2290
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002291void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002292 RTC_DCHECK(worker_thread()->IsCurrent());
2293 media_controller_->call_w()->OnSentPacket(sent_packet);
2294}
2295
zhihuang9763d562016-08-05 11:14:50 -07002296const std::string WebRtcSession::GetTransportName(
2297 const std::string& content_name) {
2298 cricket::BaseChannel* channel = GetChannel(content_name);
2299 if (!channel) {
2300#ifdef HAVE_QUIC
2301 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2302 content_name == quic_data_transport_->transport_name()) {
2303 return quic_data_transport_->transport_name();
2304 }
2305#endif
deadbeef67b3bbe2017-01-04 18:38:02 -08002306 if (sctp_transport_) {
2307 RTC_DCHECK(sctp_content_name_);
2308 RTC_DCHECK(sctp_transport_name_);
2309 if (content_name == *sctp_content_name_) {
2310 return *sctp_transport_name_;
2311 }
2312 }
zhihuang9763d562016-08-05 11:14:50 -07002313 // Return an empty string if failed to retrieve the transport name.
2314 return "";
2315 }
2316 return channel->transport_name();
2317}
zhihuangd82eee02016-08-26 11:25:05 -07002318
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319} // namespace webrtc