blob: 80352cf83246f3ff5f42a4bfa4be64f496a5e153 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/jsepicecandidate.h"
21#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010022#include "webrtc/api/peerconnectioninterface.h"
23#include "webrtc/api/sctputils.h"
24#include "webrtc/api/webrtcsessiondescriptionfactory.h"
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010025#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000026#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070027#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000028#include "webrtc/base/helpers.h"
29#include "webrtc/base/logging.h"
30#include "webrtc/base/stringencode.h"
31#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070032#include "webrtc/call.h"
kjellanderf4752772016-03-02 05:42:30 -080033#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000035#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070036#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010037#include "webrtc/pc/channel.h"
38#include "webrtc/pc/channelmanager.h"
39#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41using cricket::ContentInfo;
42using cricket::ContentInfos;
43using cricket::MediaContentDescription;
44using cricket::SessionDescription;
45using cricket::TransportInfo;
46
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070047using cricket::LOCAL_PORT_TYPE;
48using cricket::STUN_PORT_TYPE;
49using cricket::RELAY_PORT_TYPE;
50using cricket::PRFLX_PORT_TYPE;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052namespace webrtc {
53
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000055const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
56 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000057const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058const char kInvalidCandidates[] = "Description contains invalid candidates.";
59const char kInvalidSdp[] = "Invalid session description.";
60const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
62const char kPushDownTDFailed[] =
63 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000064const char kSdpWithoutDtlsFingerprint[] =
65 "Called with SDP without DTLS fingerprint.";
66const char kSdpWithoutSdesCrypto[] =
67 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000068const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000071const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000072const char kDtlsSetupFailureRtp[] =
73 "Couldn't set up DTLS-SRTP on RTP channel.";
74const char kDtlsSetupFailureRtcp[] =
75 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070076const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070078IceCandidatePairType GetIceCandidatePairCounter(
79 const cricket::Candidate& local,
80 const cricket::Candidate& remote) {
81 const auto& l = local.type();
82 const auto& r = remote.type();
83 const auto& host = LOCAL_PORT_TYPE;
84 const auto& srflx = STUN_PORT_TYPE;
85 const auto& relay = RELAY_PORT_TYPE;
86 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070087 if (l == host && r == host) {
88 bool local_private = IPIsPrivate(local.address().ipaddr());
89 bool remote_private = IPIsPrivate(remote.address().ipaddr());
90 if (local_private) {
91 if (remote_private) {
92 return kIceCandidatePairHostPrivateHostPrivate;
93 } else {
94 return kIceCandidatePairHostPrivateHostPublic;
95 }
96 } else {
97 if (remote_private) {
98 return kIceCandidatePairHostPublicHostPrivate;
99 } else {
100 return kIceCandidatePairHostPublicHostPublic;
101 }
102 }
103 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700104 if (l == host && r == srflx)
105 return kIceCandidatePairHostSrflx;
106 if (l == host && r == relay)
107 return kIceCandidatePairHostRelay;
108 if (l == host && r == prflx)
109 return kIceCandidatePairHostPrflx;
110 if (l == srflx && r == host)
111 return kIceCandidatePairSrflxHost;
112 if (l == srflx && r == srflx)
113 return kIceCandidatePairSrflxSrflx;
114 if (l == srflx && r == relay)
115 return kIceCandidatePairSrflxRelay;
116 if (l == srflx && r == prflx)
117 return kIceCandidatePairSrflxPrflx;
118 if (l == relay && r == host)
119 return kIceCandidatePairRelayHost;
120 if (l == relay && r == srflx)
121 return kIceCandidatePairRelaySrflx;
122 if (l == relay && r == relay)
123 return kIceCandidatePairRelayRelay;
124 if (l == relay && r == prflx)
125 return kIceCandidatePairRelayPrflx;
126 if (l == prflx && r == host)
127 return kIceCandidatePairPrflxHost;
128 if (l == prflx && r == srflx)
129 return kIceCandidatePairPrflxSrflx;
130 if (l == prflx && r == relay)
131 return kIceCandidatePairPrflxRelay;
132 return kIceCandidatePairMax;
133}
134
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135// Compares |answer| against |offer|. Comparision is done
136// for number of m-lines in answer against offer. If matches true will be
137// returned otherwise false.
138static bool VerifyMediaDescriptions(
139 const SessionDescription* answer, const SessionDescription* offer) {
140 if (offer->contents().size() != answer->contents().size())
141 return false;
142
143 for (size_t i = 0; i < offer->contents().size(); ++i) {
144 if ((offer->contents()[i].name) != answer->contents()[i].name) {
145 return false;
146 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000147 const MediaContentDescription* offer_mdesc =
148 static_cast<const MediaContentDescription*>(
149 offer->contents()[i].description);
150 const MediaContentDescription* answer_mdesc =
151 static_cast<const MediaContentDescription*>(
152 answer->contents()[i].description);
153 if (offer_mdesc->type() != answer_mdesc->type()) {
154 return false;
155 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 }
157 return true;
158}
159
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160// Checks that each non-rejected content has SDES crypto keys or a DTLS
161// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
162// keys, will be caught in Transport negotiation, and backstopped by Channel's
163// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000164static bool VerifyCrypto(const SessionDescription* desc,
165 bool dtls_enabled,
166 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 const ContentInfos& contents = desc->contents();
168 for (size_t index = 0; index < contents.size(); ++index) {
169 const ContentInfo* cinfo = &contents[index];
170 if (cinfo->rejected) {
171 continue;
172 }
173
174 // If the content isn't rejected, crypto must be present.
175 const MediaContentDescription* media =
176 static_cast<const MediaContentDescription*>(cinfo->description);
177 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
178 if (!media || !tinfo) {
179 // Something is not right.
180 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000181 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 return false;
183 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000184 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000185 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000186 LOG(LS_WARNING) <<
187 "Session description must have DTLS fingerprint if DTLS enabled.";
188 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000189 return false;
190 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000191 } else {
192 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000193 LOG(LS_WARNING) <<
194 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000195 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000196 return false;
197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 }
199 }
200
201 return true;
202}
203
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000204// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
205static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
206 const ContentInfos& contents = desc->contents();
207 for (size_t index = 0; index < contents.size(); ++index) {
208 const ContentInfo* cinfo = &contents[index];
209 if (cinfo->rejected) {
210 continue;
211 }
212
213 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
214 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
215 if (!tinfo) {
216 // Something is not right.
217 LOG(LS_ERROR) << kInvalidSdp;
218 return false;
219 }
220 if (tinfo->description.ice_ufrag.empty() ||
221 tinfo->description.ice_pwd.empty()) {
222 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
223 return false;
224 }
225 }
226 return true;
227}
228
wu@webrtc.org91053e72013-08-10 07:18:04 +0000229// Forces |sdesc->crypto_required| to the appropriate state based on the
230// current security policy, to ensure a failure occurs if there is an error
231// in crypto negotiation.
232// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000233static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
234 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000235 if (!sdesc) {
236 return;
237 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
wu@webrtc.org91053e72013-08-10 07:18:04 +0000239 // Updating the |crypto_required_| in MediaContentDescription to the
240 // appropriate state based on the current security policy.
241 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
242 iter != sdesc->contents().end(); ++iter) {
243 if (cricket::IsMediaContent(&*iter)) {
244 MediaContentDescription* mdesc =
245 static_cast<MediaContentDescription*> (iter->description);
246 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000247 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248 }
249 }
250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251}
252
Peter Boström0c4e06b2015-10-07 12:23:21 +0200253static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
254 const std::string& track_id,
255 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 const cricket::ContentInfo* audio_info =
257 cricket::GetFirstAudioContent(session_description);
258 if (!audio_info) {
259 LOG(LS_ERROR) << "Audio not used in this call";
260 return false;
261 }
262
263 const cricket::MediaContentDescription* audio_content =
264 static_cast<const cricket::MediaContentDescription*>(
265 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000266 const cricket::StreamParams* stream =
267 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
268 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 return false;
270 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000271
272 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 return true;
274}
275
276static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200277 uint32_t ssrc,
278 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 ASSERT(track_id != NULL);
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 const cricket::ContentInfo* audio_info =
282 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000283 if (audio_info) {
284 const cricket::MediaContentDescription* audio_content =
285 static_cast<const cricket::MediaContentDescription*>(
286 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000288 const auto* found =
289 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
290 if (found) {
291 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000292 return true;
293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 }
295
296 const cricket::ContentInfo* video_info =
297 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000298 if (video_info) {
299 const cricket::MediaContentDescription* video_content =
300 static_cast<const cricket::MediaContentDescription*>(
301 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000303 const auto* found =
304 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
305 if (found) {
306 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000307 return true;
308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
310 return false;
311}
312
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313static bool BadSdp(const std::string& source,
314 const std::string& type,
315 const std::string& reason,
316 std::string* err_desc) {
317 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700318 desc << "Failed to set " << source;
319 if (!type.empty()) {
320 desc << " " << type;
321 }
322 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 return false;
329}
330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000336 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 }
340}
341
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342static bool BadLocalSdp(const std::string& type,
343 const std::string& reason,
344 std::string* err_desc) {
345 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
346}
347
348static bool BadRemoteSdp(const std::string& type,
349 const std::string& reason,
350 std::string* err_desc) {
351 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
352}
353
354static bool BadOfferSdp(cricket::ContentSource source,
355 const std::string& reason,
356 std::string* err_desc) {
357 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
358}
359
360static bool BadPranswerSdp(cricket::ContentSource source,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
364 reason, err_desc);
365}
366
367static bool BadAnswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
deadbeefd59daf82015-10-14 15:02:44 -0700373#define GET_STRING_OF_STATE(state) \
374 case webrtc::WebRtcSession::state: \
375 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 break;
377
deadbeefd59daf82015-10-14 15:02:44 -0700378static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 std::string result;
380 switch (state) {
381 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700382 GET_STRING_OF_STATE(STATE_SENTOFFER)
383 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
384 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
385 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700387 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 default:
389 ASSERT(false);
390 break;
391 }
392 return result;
393}
394
deadbeefd59daf82015-10-14 15:02:44 -0700395#define GET_STRING_OF_ERROR_CODE(err) \
396 case webrtc::WebRtcSession::err: \
397 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 break;
399
deadbeefd59daf82015-10-14 15:02:44 -0700400static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 std::string result;
402 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000403 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
405 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
deadbeefd59daf82015-10-14 15:02:44 -0700407 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 break;
409 }
410 return result;
411}
412
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000413static std::string MakeErrorString(const std::string& error,
414 const std::string& desc) {
415 std::ostringstream ret;
416 ret << error << " " << desc;
417 return ret.str();
418}
419
420static std::string MakeTdErrorString(const std::string& desc) {
421 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422}
423
deadbeef0ed85b22016-02-23 17:24:52 -0800424// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
425bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
426 const SessionDescriptionInterface* new_desc,
427 const std::string& content_name) {
428 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700429 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
deadbeef0ed85b22016-02-23 17:24:52 -0800431 const SessionDescription* new_sd = new_desc->description();
432 const SessionDescription* old_sd = old_desc->description();
433 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
434 if (!cinfo || cinfo->rejected) {
435 return false;
436 }
437 // If the content isn't rejected, check if ufrag and password has changed.
438 const cricket::TransportDescription* new_transport_desc =
439 new_sd->GetTransportDescriptionByName(content_name);
440 const cricket::TransportDescription* old_transport_desc =
441 old_sd->GetTransportDescriptionByName(content_name);
442 if (!new_transport_desc || !old_transport_desc) {
443 // No transport description exists. This is not an ICE restart.
444 return false;
445 }
446 if (cricket::IceCredentialsChanged(
447 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
448 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
449 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
450 << ".";
451 return true;
452 }
453 return false;
454}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455
stefanc1aeaf02015-10-15 07:26:07 -0700456WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
danilchape9021a32016-05-17 01:52:02 -0700457 rtc::Thread* network_thread,
deadbeefab9b2d12015-10-14 11:33:11 -0700458 rtc::Thread* worker_thread,
danilchape9021a32016-05-17 01:52:02 -0700459 rtc::Thread* signaling_thread,
deadbeefab9b2d12015-10-14 11:33:11 -0700460 cricket::PortAllocator* port_allocator)
danilchape9021a32016-05-17 01:52:02 -0700461 : worker_thread_(worker_thread),
462 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 // RFC 3264: The numeric value of the session id and version in the
464 // o line MUST be representable with a "64 bit signed integer".
465 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700466 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
467 transport_controller_(new cricket::TransportController(signaling_thread,
danilchape9021a32016-05-17 01:52:02 -0700468 network_thread,
deadbeefd59daf82015-10-14 15:02:44 -0700469 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700470 media_controller_(media_controller),
471 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 ice_observer_(NULL),
473 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700474 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000476 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000478 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700479 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
480 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700481 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700482 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700483 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700484 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700485 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700486 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700487 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700488 transport_controller_->SignalCandidatesRemoved.connect(
489 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
492WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700493 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000494 // Destroy video_channel_ first since it may have a pointer to the
495 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000496 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 SignalVideoChannelDestroyed();
498 channel_manager_->DestroyVideoChannel(video_channel_.release());
499 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000500 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000501 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200502 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000503 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000504 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 SignalDataChannelDestroyed();
506 channel_manager_->DestroyDataChannel(data_channel_.release());
507 }
deadbeef057ecf02016-01-20 14:30:43 -0800508 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700509
510 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511}
512
wu@webrtc.org91053e72013-08-10 07:18:04 +0000513bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000514 const PeerConnectionFactoryInterface::Options& options,
kwibergd1fe2812016-04-27 06:47:29 -0700515 std::unique_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200516 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
517 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700518 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700519 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000520
Henrik Boström87713d02015-08-25 09:53:21 +0200521 // Obtain a certificate from RTCConfiguration if any were provided (optional).
522 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
523 if (!rtc_configuration.certificates.empty()) {
524 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
525 // just picking the first one. The decision should be made based on the DTLS
526 // handshake. The DTLS negotiations need to know about all certificates.
527 certificate = rtc_configuration.certificates[0];
528 }
529
honghaiz1f429e32015-09-28 07:57:34 -0700530 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700531
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000532 if (options.disable_encryption) {
533 dtls_enabled_ = false;
534 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200535 // Enable DTLS by default if we have an identity store or a certificate.
536 dtls_enabled_ = (dtls_identity_store || certificate);
htaa2a49d92016-03-04 02:51:39 -0800537 // |rtc_configuration| can override the default |dtls_enabled_| value.
538 if (rtc_configuration.enable_dtls_srtp) {
539 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000540 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000541 }
542
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000544 // It takes precendence over the disable_sctp_data_channels
545 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800546 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000548 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000549 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000550 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000551 data_channel_type_ = cricket::DCT_SCTP;
552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
htaa2a49d92016-03-04 02:51:39 -0800555 video_options_.screencast_min_bitrate_kbps =
556 rtc_configuration.screencast_min_bitrate;
557 audio_options_.combined_audio_video_bwe =
558 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000559
kwiberg102c6a62015-10-30 02:47:38 -0700560 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100561 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200562
Karl Wibergbe579832015-11-10 22:34:18 +0100563 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
564 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200565
Henrik Boström87713d02015-08-25 09:53:21 +0200566 if (!dtls_enabled_) {
567 // Construct with DTLS disabled.
568 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700569 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200570 } else {
571 // Construct with DTLS enabled.
572 if (!certificate) {
573 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700574 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200575 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800576 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700577 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200578 } else {
579 // Use the already generated certificate.
580 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700581 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200582 }
583 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000584
Henrik Boströmd8281982015-08-27 10:12:24 +0200585 webrtc_session_desc_factory_->SignalCertificateReady.connect(
586 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000587
wu@webrtc.org97077a32013-10-25 21:18:33 +0000588 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000589 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000590 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700591
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 return true;
593}
594
deadbeefd59daf82015-10-14 15:02:44 -0700595void WebRtcSession::Close() {
596 SetState(STATE_CLOSED);
597 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000598 ASSERT(!voice_channel_);
599 ASSERT(!video_channel_);
600 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800601 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602}
603
deadbeef0ed85b22016-02-23 17:24:52 -0800604cricket::BaseChannel* WebRtcSession::GetChannel(
605 const std::string& content_name) {
606 if (voice_channel() && voice_channel()->content_name() == content_name) {
607 return voice_channel();
608 }
609 if (video_channel() && video_channel()->content_name() == content_name) {
610 return video_channel();
611 }
612 if (data_channel() && data_channel()->content_name() == content_name) {
613 return data_channel();
614 }
615 return nullptr;
616}
617
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000618void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
619 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620}
621
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000622cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
623 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624}
625
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800626bool WebRtcSession::GetSslRole(const std::string& transport_name,
627 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700628 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000629 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
630 << "SSL Role of the session.";
631 return false;
632 }
633
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800634 return transport_controller_->GetSslRole(transport_name, role);
635}
636
637bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
638 rtc::SSLRole* role) {
639 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000640}
641
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000642void WebRtcSession::CreateOffer(
643 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700644 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
645 const cricket::MediaSessionOptions& session_options) {
646 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000647}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648
deadbeefab9b2d12015-10-14 11:33:11 -0700649void WebRtcSession::CreateAnswer(
650 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700651 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800652 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653}
654
655bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
656 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700657 ASSERT(signaling_thread()->IsCurrent());
658
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000659 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700660 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000661
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000662 // Validate SDP.
663 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
664 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 }
666
deadbeefd59daf82015-10-14 15:02:44 -0700667 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000668 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700670 initial_offerer_ = true;
671 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 }
673
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000674 cricket::SecurePolicy sdes_policy =
675 webrtc_session_desc_factory_->SdesPolicy();
676 cricket::CryptoType crypto_required = dtls_enabled_ ?
677 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
678 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000680 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000682 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683
684 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000685 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 // TODO(mallinath) - Handle CreateChannel failure, as new local description
687 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000688 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 }
690
deadbeefcbecd352015-09-23 11:50:27 -0700691 // Remove unused channels if MediaContentDescription is rejected.
692 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000694 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 return false;
696 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000697
deadbeefd59daf82015-10-14 15:02:44 -0700698 if (remote_desc_) {
699 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700700 UseCandidatesInSessionDescription(remote_desc_.get());
701 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702
deadbeef0ed85b22016-02-23 17:24:52 -0800703 pending_ice_restarts_.clear();
704
deadbeefd59daf82015-10-14 15:02:44 -0700705 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000706 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708 return true;
709}
710
711bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
712 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700713 ASSERT(signaling_thread()->IsCurrent());
714
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000715 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700716 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000717
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000718 // Validate SDP.
719 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
720 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 }
722
kwibergd1fe2812016-04-27 06:47:29 -0700723 std::unique_ptr<SessionDescriptionInterface> old_remote_desc(
deadbeefd59daf82015-10-14 15:02:44 -0700724 remote_desc_.release());
725 remote_desc_.reset(desc_temp.release());
726
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000728 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 if (action == kOffer && !CreateChannels(desc->description())) {
730 // TODO(mallinath) - Handle CreateChannel failure, as new local description
731 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000732 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 }
734
deadbeefcbecd352015-09-23 11:50:27 -0700735 // Remove unused channels if MediaContentDescription is rejected.
736 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737
738 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
739 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000740 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 return false;
742 }
743
deadbeefd59daf82015-10-14 15:02:44 -0700744 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000745 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 }
747
deadbeef0ed85b22016-02-23 17:24:52 -0800748 if (old_remote_desc) {
749 for (const cricket::ContentInfo& content :
750 old_remote_desc->description()->contents()) {
751 // Check if this new SessionDescription contains new ICE ufrag and
752 // password that indicates the remote peer requests an ICE restart.
753 // TODO(deadbeef): When we start storing both the current and pending
754 // remote description, this should reset pending_ice_restarts and compare
755 // against the current description.
756 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
757 if (action == kOffer) {
758 pending_ice_restarts_.insert(content.name);
759 }
760 } else {
761 // We retain all received candidates only if ICE is not restarted.
762 // When ICE is restarted, all previous candidates belong to an old
763 // generation and should not be kept.
764 // TODO(deadbeef): This goes against the W3C spec which says the remote
765 // description should only contain candidates from the last set remote
766 // description plus any candidates added since then. We should remove
767 // this once we're sure it won't break anything.
768 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
769 old_remote_desc.get(), content.name, desc);
770 }
771 }
honghaiz503726c2015-07-31 12:37:38 -0700772 }
773
deadbeefd59daf82015-10-14 15:02:44 -0700774 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000775 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000777
778 // Set the the ICE connection state to connecting since the connection may
779 // become writable with peer reflexive candidates before any remote candidate
780 // is signaled.
781 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
782 // is to have a new signal the indicates a change in checking state from the
783 // transport and expose a new checking() member from transport that can be
784 // read to determine the current checking state. The existing SignalConnecting
785 // actually means "gathering candidates", so cannot be be used here.
786 if (desc->type() != SessionDescriptionInterface::kOffer &&
787 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
788 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
789 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 return true;
791}
792
deadbeefd59daf82015-10-14 15:02:44 -0700793void WebRtcSession::LogState(State old_state, State new_state) {
794 LOG(LS_INFO) << "Session:" << id()
795 << " Old state:" << GetStateString(old_state)
796 << " New state:" << GetStateString(new_state);
797}
798
799void WebRtcSession::SetState(State state) {
800 ASSERT(signaling_thread_->IsCurrent());
801 if (state != state_) {
802 LogState(state_, state);
803 state_ = state;
804 SignalState(this, state_);
805 }
806}
807
808void WebRtcSession::SetError(Error error, const std::string& error_desc) {
809 ASSERT(signaling_thread_->IsCurrent());
810 if (error != error_) {
811 error_ = error;
812 error_desc_ = error_desc;
813 }
814}
815
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816bool WebRtcSession::UpdateSessionState(
817 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700819 ASSERT(signaling_thread()->IsCurrent());
820
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 // If there's already a pending error then no state transition should happen.
822 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700823 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000824 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000826 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
827 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
deadbeefd59daf82015-10-14 15:02:44 -0700829 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
830 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000831 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700832 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000833 }
deadbeefd59daf82015-10-14 15:02:44 -0700834 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000835 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
837 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
839 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
841 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700842 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
843 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000844 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700845 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000846 }
deadbeefd59daf82015-10-14 15:02:44 -0700847 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000848 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700851 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700852 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700853 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700854 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700855 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800856 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700857 const cricket::ContentGroup* answer_bundle =
858 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
859 if (!EnableBundle(*answer_bundle)) {
860 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
861 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
862 }
863 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800864 // Only push down the transport description after enabling BUNDLE; we don't
865 // want to push down a description on a transport about to be destroyed.
866 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
867 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
868 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700870 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000871 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700872 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000873 }
deadbeefd59daf82015-10-14 15:02:44 -0700874 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000875 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 }
877 }
878 return true;
879}
880
881WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
882 if (type == SessionDescriptionInterface::kOffer) {
883 return WebRtcSession::kOffer;
884 } else if (type == SessionDescriptionInterface::kPrAnswer) {
885 return WebRtcSession::kPrAnswer;
886 } else if (type == SessionDescriptionInterface::kAnswer) {
887 return WebRtcSession::kAnswer;
888 }
889 ASSERT(false && "unknown action type");
890 return WebRtcSession::kOffer;
891}
892
deadbeefd59daf82015-10-14 15:02:44 -0700893bool WebRtcSession::PushdownMediaDescription(
894 cricket::ContentAction action,
895 cricket::ContentSource source,
896 std::string* err) {
897 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
898 if (!ch) {
899 return true;
900 } else if (source == cricket::CS_LOCAL) {
901 return ch->PushdownLocalDescription(local_desc_->description(), action,
902 err);
903 } else {
904 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
905 err);
906 }
907 };
908
909 return (set_content(voice_channel()) &&
910 set_content(video_channel()) &&
911 set_content(data_channel()));
912}
913
914bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
915 cricket::ContentAction action,
916 std::string* error_desc) {
917 RTC_DCHECK(signaling_thread()->IsCurrent());
918
919 if (source == cricket::CS_LOCAL) {
920 return PushdownLocalTransportDescription(local_desc_->description(), action,
921 error_desc);
922 }
923 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
924 error_desc);
925}
926
927bool WebRtcSession::PushdownLocalTransportDescription(
928 const SessionDescription* sdesc,
929 cricket::ContentAction action,
930 std::string* err) {
931 RTC_DCHECK(signaling_thread()->IsCurrent());
932
933 if (!sdesc) {
934 return false;
935 }
936
937 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
938 if (!transport_controller_->SetLocalTransportDescription(
939 tinfo.content_name, tinfo.description, action, err)) {
940 return false;
941 }
942 }
943
944 return true;
945}
946
947bool WebRtcSession::PushdownRemoteTransportDescription(
948 const SessionDescription* sdesc,
949 cricket::ContentAction action,
950 std::string* err) {
951 RTC_DCHECK(signaling_thread()->IsCurrent());
952
953 if (!sdesc) {
954 return false;
955 }
956
957 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
958 if (!transport_controller_->SetRemoteTransportDescription(
959 tinfo.content_name, tinfo.description, action, err)) {
960 return false;
961 }
962 }
963
964 return true;
965}
966
967bool WebRtcSession::GetTransportDescription(
968 const SessionDescription* description,
969 const std::string& content_name,
970 cricket::TransportDescription* tdesc) {
971 if (!description || !tdesc) {
972 return false;
973 }
974 const TransportInfo* transport_info =
975 description->GetTransportInfoByName(content_name);
976 if (!transport_info) {
977 return false;
978 }
979 *tdesc = transport_info->description;
980 return true;
981}
982
983bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000984 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700985 return (GetChannelTransportStats(voice_channel(), stats) &&
986 GetChannelTransportStats(video_channel(), stats) &&
987 GetChannelTransportStats(data_channel(), stats));
988}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000989
deadbeefcbecd352015-09-23 11:50:27 -0700990bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -0700991 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -0700992 ASSERT(signaling_thread()->IsCurrent());
993 if (!ch) {
994 // Not using this channel.
995 return true;
996 }
997
998 const std::string& content_name = ch->content_name();
999 const std::string& transport_name = ch->transport_name();
1000 stats->proxy_to_transport[content_name] = transport_name;
1001 if (stats->transport_stats.find(transport_name) !=
1002 stats->transport_stats.end()) {
1003 // Transport stats already done for this transport.
1004 return true;
1005 }
1006
1007 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001008 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001009 return false;
1010 }
1011
1012 stats->transport_stats[transport_name] = tstats;
1013 return true;
1014}
1015
1016bool WebRtcSession::GetLocalCertificate(
1017 const std::string& transport_name,
1018 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1019 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001020 return transport_controller_->GetLocalCertificate(transport_name,
1021 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001022}
1023
jbauch555604a2016-04-26 03:13:22 -07001024std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -07001025 const std::string& transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001026 ASSERT(signaling_thread()->IsCurrent());
kwibergb4d01c42016-04-06 05:15:06 -07001027 return transport_controller_->GetRemoteSSLCertificate(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -07001028}
1029
deadbeefcbecd352015-09-23 11:50:27 -07001030bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1031 const std::string* first_content_name = bundle.FirstContentName();
1032 if (!first_content_name) {
1033 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1034 return false;
1035 }
1036 const std::string& transport_name = *first_content_name;
1037 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1038
1039 auto maybe_set_transport = [this, bundle, transport_name,
1040 first_channel](cricket::BaseChannel* ch) {
1041 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001042 return true;
1043 }
1044
deadbeefcbecd352015-09-23 11:50:27 -07001045 if (ch->transport_name() == transport_name) {
1046 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1047 << " on " << transport_name << ".";
1048 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001049 }
torbjornga81a42f2015-09-23 02:16:58 -07001050
deadbeefcbecd352015-09-23 11:50:27 -07001051 if (!ch->SetTransport(transport_name)) {
1052 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1053 return false;
1054 }
1055 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1056 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001057 return true;
1058 };
1059
deadbeefcbecd352015-09-23 11:50:27 -07001060 if (!maybe_set_transport(voice_channel()) ||
1061 !maybe_set_transport(video_channel()) ||
1062 !maybe_set_transport(data_channel())) {
1063 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001064 }
deadbeefcbecd352015-09-23 11:50:27 -07001065
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001066 return true;
1067}
1068
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001070 if (!remote_desc_) {
1071 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1072 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001073 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 }
1075
1076 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001077 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 return false;
1079 }
1080
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001081 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001082 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1083 if (!valid) {
1084 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 }
1086
1087 // Add this candidate to the remote session description.
1088 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001089 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 return false;
1091 }
1092
deadbeefd59daf82015-10-14 15:02:44 -07001093 if (ready) {
1094 return UseCandidate(candidate);
1095 } else {
1096 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1097 return true;
1098 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099}
1100
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001101bool WebRtcSession::RemoveRemoteIceCandidates(
1102 const std::vector<cricket::Candidate>& candidates) {
1103 if (!remote_desc_) {
1104 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1105 << "removed without any remote session description.";
1106 return false;
1107 }
1108
1109 if (candidates.empty()) {
1110 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1111 return false;
1112 }
1113
1114 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1115 if (number_removed != candidates.size()) {
1116 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1117 << "Requested " << candidates.size() << " but only "
1118 << number_removed << " are removed.";
1119 }
1120
1121 // Remove the candidates from the transport controller.
1122 std::string error;
1123 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1124 if (!res && !error.empty()) {
1125 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1126 }
1127 return true;
1128}
1129
deadbeefd59daf82015-10-14 15:02:44 -07001130cricket::IceConfig WebRtcSession::ParseIceConfig(
1131 const PeerConnectionInterface::RTCConfiguration& config) const {
1132 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001133 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001134 ice_config.prioritize_most_likely_candidate_pairs =
1135 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001136 ice_config.backup_connection_ping_interval =
1137 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001138 ice_config.gather_continually = (config.continual_gathering_policy ==
1139 PeerConnectionInterface::GATHER_CONTINUALLY);
1140 return ice_config;
1141}
1142
1143void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1144 transport_controller_->SetIceConfig(config);
1145}
1146
1147void WebRtcSession::MaybeStartGathering() {
1148 transport_controller_->MaybeStartGathering();
1149}
1150
Peter Boström0c4e06b2015-10-07 12:23:21 +02001151bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1152 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001153 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001155 }
1156 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157}
1158
Peter Boström0c4e06b2015-10-07 12:23:21 +02001159bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1160 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001161 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001162 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001163 }
1164 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165}
1166
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001167std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001169 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 return desc.str();
1171}
1172
solenbergd4cec0d2015-10-09 08:55:48 -07001173void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 ASSERT(signaling_thread()->IsCurrent());
1175 if (!voice_channel_) {
1176 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1177 return;
1178 }
solenberg4bac9c52015-10-09 02:32:53 -07001179 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1180 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 // otherwise. This in the normal case when the underlying media channel has
1182 // already been deleted.
1183 ASSERT(enable == false);
1184 }
1185}
1186
Peter Boström0c4e06b2015-10-07 12:23:21 +02001187void WebRtcSession::SetAudioSend(uint32_t ssrc,
1188 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001189 const cricket::AudioOptions& options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001190 cricket::AudioSource* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 ASSERT(signaling_thread()->IsCurrent());
1192 if (!voice_channel_) {
1193 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1194 return;
1195 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001196 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, source)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001197 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199}
1200
Peter Boström0c4e06b2015-10-07 12:23:21 +02001201void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001202 ASSERT(signaling_thread()->IsCurrent());
1203 ASSERT(volume >= 0 && volume <= 10);
1204 if (!voice_channel_) {
1205 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1206 return;
1207 }
1208
solenberg4bac9c52015-10-09 02:32:53 -07001209 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001210 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001211 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001212}
1213
deadbeef2d110be2016-01-13 12:00:26 -08001214void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
kwibergd1fe2812016-04-27 06:47:29 -07001215 std::unique_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001216 ASSERT(signaling_thread()->IsCurrent());
1217 if (!voice_channel_)
1218 return;
1219
kwiberg1c7fdd82016-04-26 08:18:04 -07001220 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001221}
1222
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001223RtpParameters WebRtcSession::GetAudioRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001224 ASSERT(signaling_thread()->IsCurrent());
1225 if (voice_channel_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001226 return voice_channel_->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001227 }
1228 return RtpParameters();
1229}
1230
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001231bool WebRtcSession::SetAudioRtpSendParameters(uint32_t ssrc,
1232 const RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001233 ASSERT(signaling_thread()->IsCurrent());
1234 if (!voice_channel_) {
1235 return false;
1236 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001237 return voice_channel_->SetRtpSendParameters(ssrc, parameters);
1238}
1239
1240RtpParameters WebRtcSession::GetAudioRtpReceiveParameters(uint32_t ssrc) const {
1241 ASSERT(signaling_thread()->IsCurrent());
1242 if (voice_channel_) {
1243 return voice_channel_->GetRtpReceiveParameters(ssrc);
1244 }
1245 return RtpParameters();
1246}
1247
1248bool WebRtcSession::SetAudioRtpReceiveParameters(
1249 uint32_t ssrc,
1250 const RtpParameters& parameters) {
1251 ASSERT(signaling_thread()->IsCurrent());
1252 if (!voice_channel_) {
1253 return false;
1254 }
1255 return voice_channel_->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001256}
1257
nisse2ded9b12016-04-08 02:23:55 -07001258bool WebRtcSession::SetSource(
1259 uint32_t ssrc,
1260 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 ASSERT(signaling_thread()->IsCurrent());
1262
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001263 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1265 // support video.
1266 LOG(LS_WARNING) << "Video not used in this call.";
1267 return false;
1268 }
nisse2ded9b12016-04-08 02:23:55 -07001269 video_channel_->SetSource(ssrc, source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 return true;
1271}
1272
nisse08582ff2016-02-04 01:24:52 -08001273void WebRtcSession::SetVideoPlayout(
1274 uint32_t ssrc,
1275 bool enable,
1276 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 ASSERT(signaling_thread()->IsCurrent());
1278 if (!video_channel_) {
1279 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1280 return;
1281 }
nisse08582ff2016-02-04 01:24:52 -08001282 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1283 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 // This in the normal case when the underlying media channel has already
1285 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001286 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 }
1288}
1289
Peter Boström0c4e06b2015-10-07 12:23:21 +02001290void WebRtcSession::SetVideoSend(uint32_t ssrc,
1291 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 const cricket::VideoOptions* options) {
1293 ASSERT(signaling_thread()->IsCurrent());
1294 if (!video_channel_) {
1295 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1296 return;
1297 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001298 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1300 // This in the normal case when the underlying media channel has already
1301 // been deleted.
1302 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304}
1305
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001306RtpParameters WebRtcSession::GetVideoRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001307 ASSERT(signaling_thread()->IsCurrent());
1308 if (video_channel_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001309 return video_channel_->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001310 }
1311 return RtpParameters();
1312}
1313
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001314bool WebRtcSession::SetVideoRtpSendParameters(uint32_t ssrc,
1315 const RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001316 ASSERT(signaling_thread()->IsCurrent());
1317 if (!video_channel_) {
1318 return false;
1319 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001320 return video_channel_->SetRtpSendParameters(ssrc, parameters);
1321}
1322
1323RtpParameters WebRtcSession::GetVideoRtpReceiveParameters(uint32_t ssrc) const {
1324 ASSERT(signaling_thread()->IsCurrent());
1325 if (video_channel_) {
1326 return video_channel_->GetRtpReceiveParameters(ssrc);
1327 }
1328 return RtpParameters();
1329}
1330
1331bool WebRtcSession::SetVideoRtpReceiveParameters(
1332 uint32_t ssrc,
1333 const RtpParameters& parameters) {
1334 ASSERT(signaling_thread()->IsCurrent());
1335 if (!video_channel_) {
1336 return false;
1337 }
1338 return video_channel_->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001339}
1340
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1342 ASSERT(signaling_thread()->IsCurrent());
1343 if (!voice_channel_) {
1344 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1345 return false;
1346 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001347 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1349 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001350 if (!local_desc_ ||
1351 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 &send_ssrc)) {
1353 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1354 return false;
1355 }
1356 return voice_channel_->CanInsertDtmf();
1357}
1358
1359bool WebRtcSession::InsertDtmf(const std::string& track_id,
1360 int code, int duration) {
1361 ASSERT(signaling_thread()->IsCurrent());
1362 if (!voice_channel_) {
1363 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1364 return false;
1365 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001366 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001367 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1368 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1370 return false;
1371 }
solenberg1d63dd02015-12-02 12:35:09 -08001372 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1374 return false;
1375 }
1376 return true;
1377}
1378
1379sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001380 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381}
1382
wu@webrtc.org78187522013-10-07 23:32:02 +00001383bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001384 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001385 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001386 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001387 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1388 return false;
1389 }
1390 return data_channel_->SendData(params, payload, result);
1391}
1392
1393bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001394 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001395 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1396 return false;
1397 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001398 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1399 &DataChannel::OnChannelReady);
1400 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1401 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001402 data_channel_->SignalStreamClosedRemotely.connect(
1403 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001404 return true;
1405}
1406
1407void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001408 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001409 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1410 return;
1411 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001412 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1413 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001414 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001415}
1416
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001417void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001418 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001419 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1420 return;
1421 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001422 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1423 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001424}
1425
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001426void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001427 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001428 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1429 << "NULL.";
1430 return;
1431 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001432 data_channel_->RemoveRecvStream(sid);
1433 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001434}
1435
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001436bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001437 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001438}
1439
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440cricket::DataChannelType WebRtcSession::data_channel_type() const {
1441 return data_channel_type_;
1442}
1443
deadbeef0ed85b22016-02-23 17:24:52 -08001444bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1445 return pending_ice_restarts_.find(content_name) !=
1446 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001447}
1448
Henrik Boströmd8281982015-08-27 10:12:24 +02001449void WebRtcSession::OnCertificateReady(
1450 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001451 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001452}
1453
Henrik Boströmd8281982015-08-27 10:12:24 +02001454bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001455 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001456}
1457
deadbeefcbecd352015-09-23 11:50:27 -07001458const rtc::scoped_refptr<rtc::RTCCertificate>&
1459WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001460 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001461}
1462
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463void WebRtcSession::SetIceConnectionState(
1464 PeerConnectionInterface::IceConnectionState state) {
1465 if (ice_connection_state_ == state) {
1466 return;
1467 }
1468
1469 // ASSERT that the requested transition is allowed. Note that
1470 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1471 // within PeerConnection). This switch statement should compile away when
1472 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001473 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1474 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 switch (ice_connection_state_) {
1476 case PeerConnectionInterface::kIceConnectionNew:
1477 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1478 break;
1479 case PeerConnectionInterface::kIceConnectionChecking:
1480 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1481 state == PeerConnectionInterface::kIceConnectionConnected);
1482 break;
1483 case PeerConnectionInterface::kIceConnectionConnected:
1484 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1485 state == PeerConnectionInterface::kIceConnectionChecking ||
1486 state == PeerConnectionInterface::kIceConnectionCompleted);
1487 break;
1488 case PeerConnectionInterface::kIceConnectionCompleted:
1489 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1490 state == PeerConnectionInterface::kIceConnectionDisconnected);
1491 break;
1492 case PeerConnectionInterface::kIceConnectionFailed:
1493 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1494 break;
1495 case PeerConnectionInterface::kIceConnectionDisconnected:
1496 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1497 state == PeerConnectionInterface::kIceConnectionConnected ||
1498 state == PeerConnectionInterface::kIceConnectionCompleted ||
1499 state == PeerConnectionInterface::kIceConnectionFailed);
1500 break;
1501 case PeerConnectionInterface::kIceConnectionClosed:
1502 ASSERT(false);
1503 break;
1504 default:
1505 ASSERT(false);
1506 break;
1507 }
1508
1509 ice_connection_state_ = state;
1510 if (ice_observer_) {
1511 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1512 }
1513}
1514
deadbeefcbecd352015-09-23 11:50:27 -07001515void WebRtcSession::OnTransportControllerConnectionState(
1516 cricket::IceConnectionState state) {
1517 switch (state) {
1518 case cricket::kIceConnectionConnecting:
1519 // If the current state is Connected or Completed, then there were
1520 // writable channels but now there are not, so the next state must
1521 // be Disconnected.
1522 // kIceConnectionConnecting is currently used as the default,
1523 // un-connected state by the TransportController, so its only use is
1524 // detecting disconnections.
1525 if (ice_connection_state_ ==
1526 PeerConnectionInterface::kIceConnectionConnected ||
1527 ice_connection_state_ ==
1528 PeerConnectionInterface::kIceConnectionCompleted) {
1529 SetIceConnectionState(
1530 PeerConnectionInterface::kIceConnectionDisconnected);
1531 }
torbjornga81a42f2015-09-23 02:16:58 -07001532 break;
deadbeefcbecd352015-09-23 11:50:27 -07001533 case cricket::kIceConnectionFailed:
1534 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1535 break;
1536 case cricket::kIceConnectionConnected:
1537 LOG(LS_INFO) << "Changing to ICE connected state because "
1538 << "all transports are writable.";
1539 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1540 break;
1541 case cricket::kIceConnectionCompleted:
1542 LOG(LS_INFO) << "Changing to ICE completed state because "
1543 << "all transports are complete.";
1544 if (ice_connection_state_ !=
1545 PeerConnectionInterface::kIceConnectionConnected) {
1546 // If jumping directly from "checking" to "connected",
1547 // signal "connected" first.
1548 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1549 }
1550 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1551 if (metrics_observer_) {
1552 ReportTransportStats();
1553 }
1554 break;
1555 default:
1556 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001557 }
deadbeefcbecd352015-09-23 11:50:27 -07001558}
1559
1560void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001561 SetIceConnectionReceiving(receiving);
1562}
1563
1564void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1565 if (ice_connection_receiving_ == receiving) {
1566 return;
1567 }
1568 ice_connection_receiving_ = receiving;
1569 if (ice_observer_) {
1570 ice_observer_->OnIceConnectionReceivingChange(receiving);
1571 }
1572}
1573
deadbeefcbecd352015-09-23 11:50:27 -07001574void WebRtcSession::OnTransportControllerCandidatesGathered(
1575 const std::string& transport_name,
1576 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001578 int sdp_mline_index;
1579 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1580 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1581 << transport_name << " not found";
1582 return;
1583 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584
deadbeefcbecd352015-09-23 11:50:27 -07001585 for (cricket::Candidates::const_iterator citer = candidates.begin();
1586 citer != candidates.end(); ++citer) {
1587 // Use transport_name as the candidate media id.
1588 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1589 if (ice_observer_) {
1590 ice_observer_->OnIceCandidate(&candidate);
1591 }
1592 if (local_desc_) {
1593 local_desc_->AddCandidate(&candidate);
1594 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 }
1596}
1597
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001598void WebRtcSession::OnTransportControllerCandidatesRemoved(
1599 const std::vector<cricket::Candidate>& candidates) {
1600 ASSERT(signaling_thread()->IsCurrent());
1601 // Sanity check.
1602 for (const cricket::Candidate& candidate : candidates) {
1603 if (candidate.transport_name().empty()) {
1604 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1605 << "empty content name in candidate "
1606 << candidate.ToString();
1607 return;
1608 }
1609 }
1610
1611 if (local_desc_) {
1612 local_desc_->RemoveCandidates(candidates);
1613 }
1614 if (ice_observer_) {
1615 ice_observer_->OnIceCandidatesRemoved(candidates);
1616 }
1617}
1618
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619// Enabling voice and video channel.
1620void WebRtcSession::EnableChannels() {
1621 if (voice_channel_ && !voice_channel_->enabled())
1622 voice_channel_->Enable(true);
1623
1624 if (video_channel_ && !video_channel_->enabled())
1625 video_channel_->Enable(true);
1626
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001627 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 data_channel_->Enable(true);
1629}
1630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631// Returns the media index for a local ice candidate given the content name.
1632bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1633 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001634 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637
1638 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001639 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 for (size_t index = 0; index < contents.size(); ++index) {
1641 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001642 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 content_found = true;
1644 break;
1645 }
1646 }
1647 return content_found;
1648}
1649
1650bool WebRtcSession::UseCandidatesInSessionDescription(
1651 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001652 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001656
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1658 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001659 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001660 const IceCandidateInterface* candidate = candidates->at(n);
1661 bool valid = false;
1662 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1663 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001664 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1665 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001666 }
1667 continue;
1668 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001669 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001670 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 break;
deadbeefd59daf82015-10-14 15:02:44 -07001672 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 }
1674 }
1675 return ret;
1676}
1677
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001678bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001680 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001682 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 return false;
1684 }
1685
1686 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001687 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 std::vector<cricket::Candidate> candidates;
1689 candidates.push_back(candidate->candidate());
1690 // Invoking BaseSession method to handle remote candidates.
1691 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001692 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1693 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 // Candidates successfully submitted for checking.
1695 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1696 ice_connection_state_ ==
1697 PeerConnectionInterface::kIceConnectionDisconnected) {
1698 // If state is New, then the session has just gotten its first remote ICE
1699 // candidates, so go to Checking.
1700 // If state is Disconnected, the session is re-using old candidates or
1701 // receiving additional ones, so go to Checking.
1702 // If state is Connected, stay Connected.
1703 // TODO(bemasc): If state is Connected, and the new candidates are for a
1704 // newly added transport, then the state actually _should_ move to
1705 // checking. Add a way to distinguish that case.
1706 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1707 }
1708 // TODO(bemasc): If state is Completed, go back to Connected.
1709 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001710 if (!error.empty()) {
1711 LOG(LS_WARNING) << error;
1712 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 }
1714 return true;
1715}
1716
deadbeefcbecd352015-09-23 11:50:27 -07001717void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001718 // Destroy video_channel_ first since it may have a pointer to the
1719 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 const cricket::ContentInfo* video_info =
1721 cricket::GetFirstVideoContent(desc);
1722 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 }
1726
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001727 const cricket::ContentInfo* voice_info =
1728 cricket::GetFirstAudioContent(desc);
1729 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001730 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001731 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001732 }
1733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 const cricket::ContentInfo* data_info =
1735 cricket::GetFirstDataContent(desc);
1736 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 }
1740}
1741
skvlad6c87a672016-05-17 17:49:52 -07001742// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1743// if the channel is not part of any bundle.
1744const std::string* WebRtcSession::GetBundleTransportName(
1745 const cricket::ContentInfo* content,
1746 const cricket::ContentGroup* bundle) {
1747 if (!bundle) {
1748 return nullptr;
1749 }
1750 const std::string* first_content_name = bundle->FirstContentName();
1751 if (!first_content_name) {
1752 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1753 return nullptr;
1754 }
1755 if (!bundle->HasContentName(content->name)) {
1756 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1757 return nullptr;
1758 }
1759 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1760 return first_content_name;
1761}
1762
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001764 const cricket::ContentGroup* bundle_group = nullptr;
1765 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1766 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1767 if (!bundle_group) {
1768 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1769 return false;
1770 }
1771 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 // Creating the media channels and transport proxies.
1773 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1774 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001775 if (!CreateVoiceChannel(voice,
1776 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 LOG(LS_ERROR) << "Failed to create voice channel.";
1778 return false;
1779 }
1780 }
1781
1782 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1783 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001784 if (!CreateVideoChannel(video,
1785 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 LOG(LS_ERROR) << "Failed to create video channel.";
1787 return false;
1788 }
1789 }
1790
1791 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1792 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001793 data && !data->rejected && !data_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001794 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 LOG(LS_ERROR) << "Failed to create data channel.";
1796 return false;
1797 }
1798 }
1799
1800 return true;
1801}
1802
skvlad6c87a672016-05-17 17:49:52 -07001803bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1804 const std::string* bundle_transport) {
1805 bool require_rtcp_mux =
1806 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1807 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001809 media_controller_, transport_controller_.get(), content->name,
1810 bundle_transport, create_rtcp_transport_channel, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001811 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001812 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001813 }
skvlad6c87a672016-05-17 17:49:52 -07001814 if (require_rtcp_mux) {
1815 voice_channel_->ActivateRtcpMux();
1816 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001817
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001818 voice_channel_->SignalDtlsSetupFailure.connect(
1819 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001820
1821 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001822 voice_channel_->SignalSentPacket.connect(this,
1823 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001824 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
skvlad6c87a672016-05-17 17:49:52 -07001827bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1828 const std::string* bundle_transport) {
1829 bool require_rtcp_mux =
1830 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1831 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001833 media_controller_, transport_controller_.get(), content->name,
1834 bundle_transport, create_rtcp_transport_channel, video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001835 if (!video_channel_) {
1836 return false;
1837 }
skvlad6c87a672016-05-17 17:49:52 -07001838 if (require_rtcp_mux) {
1839 video_channel_->ActivateRtcpMux();
1840 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001841 video_channel_->SignalDtlsSetupFailure.connect(
1842 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001843
1844 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001845 video_channel_->SignalSentPacket.connect(this,
1846 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001847 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848}
1849
skvlad6c87a672016-05-17 17:49:52 -07001850bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1851 const std::string* bundle_transport) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001852 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
skvlad6c87a672016-05-17 17:49:52 -07001853 bool require_rtcp_mux =
1854 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1855 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 data_channel_.reset(channel_manager_->CreateDataChannel(
skvlad6c87a672016-05-17 17:49:52 -07001857 transport_controller_.get(), content->name, bundle_transport,
1858 create_rtcp_transport_channel, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001859 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001860 return false;
1861 }
skvlad6c87a672016-05-17 17:49:52 -07001862 if (require_rtcp_mux) {
1863 data_channel_->ActivateRtcpMux();
1864 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001865
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001866 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001867 data_channel_->SignalDataReceived.connect(
1868 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001869 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001870
1871 data_channel_->SignalDtlsSetupFailure.connect(
1872 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001873
1874 SignalDataChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001875 data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001876 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877}
1878
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001879void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001880 SetError(ERROR_TRANSPORT,
1881 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001884void WebRtcSession::OnDataChannelMessageReceived(
1885 cricket::DataChannel* channel,
1886 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001887 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001888 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1889 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1890 // Received OPEN message; parse and signal that a new data channel should
1891 // be created.
1892 std::string label;
1893 InternalDataChannelInit config;
1894 config.id = params.ssrc;
1895 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1896 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1897 << params.ssrc;
1898 return;
1899 }
1900 config.open_handshake_role = InternalDataChannelInit::kAcker;
1901 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 }
deadbeefab9b2d12015-10-14 11:33:11 -07001903 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904}
1905
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001906// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001907bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001908 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1909 if (!bundle_enabled)
1910 return true;
1911
1912 const cricket::ContentGroup* bundle_group =
1913 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1914 ASSERT(bundle_group != NULL);
1915
1916 const cricket::ContentInfos& contents = desc->contents();
1917 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1918 citer != contents.end(); ++citer) {
1919 const cricket::ContentInfo* content = (&*citer);
1920 ASSERT(content != NULL);
1921 if (bundle_group->HasContentName(content->name) &&
1922 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1923 if (!HasRtcpMuxEnabled(content))
1924 return false;
1925 }
1926 }
1927 // RTCP-MUX is enabled in all the contents.
1928 return true;
1929}
1930
1931bool WebRtcSession::HasRtcpMuxEnabled(
1932 const cricket::ContentInfo* content) {
1933 const cricket::MediaContentDescription* description =
1934 static_cast<cricket::MediaContentDescription*>(content->description);
1935 return description->rtcp_mux();
1936}
1937
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001938bool WebRtcSession::ValidateSessionDescription(
1939 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001940 cricket::ContentSource source, std::string* err_desc) {
1941 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001942 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001943 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944 }
1945
1946 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001947 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001948 }
1949
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001950 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001951 Action action = GetAction(sdesc->type());
1952 if (source == cricket::CS_LOCAL) {
1953 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001954 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001955 } else {
1956 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001957 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001958 }
1959
1960 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001961 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001962 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1963 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001964 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001965 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001966 }
1967
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001968 // Verify ice-ufrag and ice-pwd.
1969 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001970 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001971 }
1972
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001973 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001974 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001975 }
1976
skvlad6c87a672016-05-17 17:49:52 -07001977 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
1978 // m-lines that do not rtcp-mux enabled.
1979
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 // Verify m-lines in Answer when compared against Offer.
1981 if (action == kAnswer) {
1982 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001983 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1984 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001985 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001986 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001987 }
1988 }
1989
1990 return true;
1991}
1992
1993bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1994 return ((action == kOffer && state() == STATE_INIT) ||
1995 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001996 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001997 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001998 (action == kOffer && state() == STATE_INPROGRESS) ||
1999 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002000 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2001 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2002 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2003 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002004}
2005
2006bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2007 return ((action == kOffer && state() == STATE_INIT) ||
2008 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002009 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002010 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002011 (action == kOffer && state() == STATE_INPROGRESS) ||
2012 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002013 (action == kAnswer && state() == STATE_SENTOFFER) ||
2014 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2015 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2016 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002017}
2018
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002019std::string WebRtcSession::GetSessionErrorMsg() {
2020 std::ostringstream desc;
2021 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2022 desc << kSessionErrorDesc << error_desc() << ".";
2023 return desc.str();
2024}
2025
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002026// We need to check the local/remote description for the Transport instead of
2027// the session, because a new Transport added during renegotiation may have
2028// them unset while the session has them set from the previous negotiation.
2029// Not doing so may trigger the auto generation of transport description and
2030// mess up DTLS identity information, ICE credential, etc.
2031bool WebRtcSession::ReadyToUseRemoteCandidate(
2032 const IceCandidateInterface* candidate,
2033 const SessionDescriptionInterface* remote_desc,
2034 bool* valid) {
2035 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002036
2037 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002038 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002039
deadbeefd59daf82015-10-14 15:02:44 -07002040 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002041 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002042 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002043
2044 size_t mediacontent_index =
2045 static_cast<size_t>(candidate->sdp_mline_index());
2046 size_t remote_content_size =
2047 current_remote_desc->description()->contents().size();
2048 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002049 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2050 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002051
2052 *valid = false;
2053 return false;
2054 }
2055
2056 cricket::ContentInfo content =
2057 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002058 cricket::BaseChannel* channel = GetChannel(content.name);
2059 if (!channel) {
2060 return false;
2061 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002062
deadbeefd59daf82015-10-14 15:02:44 -07002063 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002064 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002065}
2066
deadbeefcbecd352015-09-23 11:50:27 -07002067void WebRtcSession::OnTransportControllerGatheringState(
2068 cricket::IceGatheringState state) {
2069 ASSERT(signaling_thread()->IsCurrent());
2070 if (state == cricket::kIceGatheringGathering) {
2071 if (ice_observer_) {
2072 ice_observer_->OnIceGatheringChange(
2073 PeerConnectionInterface::kIceGatheringGathering);
2074 }
2075 } else if (state == cricket::kIceGatheringComplete) {
2076 if (ice_observer_) {
2077 ice_observer_->OnIceGatheringChange(
2078 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002079 }
2080 }
2081}
2082
2083void WebRtcSession::ReportTransportStats() {
2084 // Use a set so we don't report the same stats twice if two channels share
2085 // a transport.
2086 std::set<std::string> transport_names;
2087 if (voice_channel()) {
2088 transport_names.insert(voice_channel()->transport_name());
2089 }
2090 if (video_channel()) {
2091 transport_names.insert(video_channel()->transport_name());
2092 }
2093 if (data_channel()) {
2094 transport_names.insert(data_channel()->transport_name());
2095 }
2096 for (const auto& name : transport_names) {
2097 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002098 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002099 ReportBestConnectionState(stats);
2100 ReportNegotiatedCiphers(stats);
2101 }
2102 }
2103}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002104// Walk through the ConnectionInfos to gather best connection usage
2105// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002106void WebRtcSession::ReportBestConnectionState(
2107 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002108 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002109 for (cricket::TransportChannelStatsList::const_iterator it =
2110 stats.channel_stats.begin();
2111 it != stats.channel_stats.end(); ++it) {
2112 for (cricket::ConnectionInfos::const_iterator it_info =
2113 it->connection_infos.begin();
2114 it_info != it->connection_infos.end(); ++it_info) {
2115 if (!it_info->best_connection) {
2116 continue;
2117 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002118
2119 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2120 const cricket::Candidate& local = it_info->local_candidate;
2121 const cricket::Candidate& remote = it_info->remote_candidate;
2122
2123 // Increment the counter for IceCandidatePairType.
2124 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2125 (local.type() == RELAY_PORT_TYPE &&
2126 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2127 type = kEnumCounterIceCandidatePairTypeTcp;
2128 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2129 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002130 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002131 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002132 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002133 metrics_observer_->IncrementEnumCounter(
2134 type, GetIceCandidatePairCounter(local, remote),
2135 kIceCandidatePairMax);
2136
2137 // Increment the counter for IP type.
2138 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002139 metrics_observer_->IncrementEnumCounter(
2140 kEnumCounterAddressFamily, kBestConnections_IPv4,
2141 kPeerConnectionAddressFamilyCounter_Max);
2142
2143 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002144 metrics_observer_->IncrementEnumCounter(
2145 kEnumCounterAddressFamily, kBestConnections_IPv6,
2146 kPeerConnectionAddressFamilyCounter_Max);
2147 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002148 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002149 }
2150
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002151 return;
2152 }
2153 }
2154}
2155
jbauchac8869e2015-07-03 01:36:14 -07002156void WebRtcSession::ReportNegotiatedCiphers(
2157 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002158 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002159 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2160 return;
2161 }
2162
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002163 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2164 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2165 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2166 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002167 return;
2168 }
2169
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002170 PeerConnectionEnumCounterType srtp_counter_type;
2171 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002172 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002173 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2174 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002175 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002176 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2177 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002178 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002179 srtp_counter_type = kEnumCounterDataSrtpCipher;
2180 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002181 } else {
2182 RTC_NOTREACHED();
2183 return;
2184 }
2185
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002186 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2187 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2188 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002189 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002190 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2191 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2192 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002193 }
2194}
2195
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002196void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002197 RTC_DCHECK(worker_thread()->IsCurrent());
2198 media_controller_->call_w()->OnSentPacket(sent_packet);
2199}
2200
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201} // namespace webrtc