blob: 3d9b6fbc89ede844416fd3d85558a1a19b01eca2 [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
Peter Boström0c4e06b2015-10-07 12:23:21 +0200424uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000425 PeerConnectionInterface::IceTransportsType type) {
426 switch (type) {
427 case PeerConnectionInterface::kNone:
428 return cricket::CF_NONE;
429 case PeerConnectionInterface::kRelay:
430 return cricket::CF_RELAY;
431 case PeerConnectionInterface::kNoHost:
432 return (cricket::CF_ALL & ~cricket::CF_HOST);
433 case PeerConnectionInterface::kAll:
434 return cricket::CF_ALL;
435 default: ASSERT(false);
436 }
437 return cricket::CF_NONE;
438}
439
deadbeef0ed85b22016-02-23 17:24:52 -0800440// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
441bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
442 const SessionDescriptionInterface* new_desc,
443 const std::string& content_name) {
444 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700445 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 }
deadbeef0ed85b22016-02-23 17:24:52 -0800447 const SessionDescription* new_sd = new_desc->description();
448 const SessionDescription* old_sd = old_desc->description();
449 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
450 if (!cinfo || cinfo->rejected) {
451 return false;
452 }
453 // If the content isn't rejected, check if ufrag and password has changed.
454 const cricket::TransportDescription* new_transport_desc =
455 new_sd->GetTransportDescriptionByName(content_name);
456 const cricket::TransportDescription* old_transport_desc =
457 old_sd->GetTransportDescriptionByName(content_name);
458 if (!new_transport_desc || !old_transport_desc) {
459 // No transport description exists. This is not an ICE restart.
460 return false;
461 }
462 if (cricket::IceCredentialsChanged(
463 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
464 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
465 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
466 << ".";
467 return true;
468 }
469 return false;
470}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471
stefanc1aeaf02015-10-15 07:26:07 -0700472WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700473 rtc::Thread* signaling_thread,
474 rtc::Thread* worker_thread,
475 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700476 : signaling_thread_(signaling_thread),
477 worker_thread_(worker_thread),
478 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 // RFC 3264: The numeric value of the session id and version in the
480 // o line MUST be representable with a "64 bit signed integer".
481 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700482 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
483 transport_controller_(new cricket::TransportController(signaling_thread,
484 worker_thread,
485 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700486 media_controller_(media_controller),
487 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 ice_observer_(NULL),
489 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700490 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000492 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000494 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700495 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
496 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700497 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700498 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700499 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700500 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700501 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700502 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700503 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700504 transport_controller_->SignalCandidatesRemoved.connect(
505 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506}
507
508WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700509 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000510 // Destroy video_channel_ first since it may have a pointer to the
511 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000512 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 SignalVideoChannelDestroyed();
514 channel_manager_->DestroyVideoChannel(video_channel_.release());
515 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000516 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000517 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200518 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000519 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000520 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 SignalDataChannelDestroyed();
522 channel_manager_->DestroyDataChannel(data_channel_.release());
523 }
deadbeef057ecf02016-01-20 14:30:43 -0800524 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700525
526 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527}
528
wu@webrtc.org91053e72013-08-10 07:18:04 +0000529bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000530 const PeerConnectionFactoryInterface::Options& options,
Henrik Boström5e56c592015-08-11 10:33:13 +0200531 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200532 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
533 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700534 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700535 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000536
Henrik Boström87713d02015-08-25 09:53:21 +0200537 // Obtain a certificate from RTCConfiguration if any were provided (optional).
538 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
539 if (!rtc_configuration.certificates.empty()) {
540 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
541 // just picking the first one. The decision should be made based on the DTLS
542 // handshake. The DTLS negotiations need to know about all certificates.
543 certificate = rtc_configuration.certificates[0];
544 }
545
honghaiz1f429e32015-09-28 07:57:34 -0700546 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700547
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000548 if (options.disable_encryption) {
549 dtls_enabled_ = false;
550 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200551 // Enable DTLS by default if we have an identity store or a certificate.
552 dtls_enabled_ = (dtls_identity_store || certificate);
htaa2a49d92016-03-04 02:51:39 -0800553 // |rtc_configuration| can override the default |dtls_enabled_| value.
554 if (rtc_configuration.enable_dtls_srtp) {
555 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000556 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000557 }
558
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000560 // It takes precendence over the disable_sctp_data_channels
561 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800562 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000564 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000565 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000566 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000567 data_channel_type_ = cricket::DCT_SCTP;
568 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570
htaa2a49d92016-03-04 02:51:39 -0800571 video_options_.screencast_min_bitrate_kbps =
572 rtc_configuration.screencast_min_bitrate;
573 audio_options_.combined_audio_video_bwe =
574 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000575
kwiberg102c6a62015-10-30 02:47:38 -0700576 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100577 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200578
Karl Wibergbe579832015-11-10 22:34:18 +0100579 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
580 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200581
Henrik Boström87713d02015-08-25 09:53:21 +0200582 if (!dtls_enabled_) {
583 // Construct with DTLS disabled.
584 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700585 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200586 } else {
587 // Construct with DTLS enabled.
588 if (!certificate) {
589 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700590 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200591 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800592 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700593 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200594 } else {
595 // Use the already generated certificate.
596 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700597 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200598 }
599 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000600
Henrik Boströmd8281982015-08-27 10:12:24 +0200601 webrtc_session_desc_factory_->SignalCertificateReady.connect(
602 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000603
wu@webrtc.org97077a32013-10-25 21:18:33 +0000604 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000605 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000606 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000607 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200608 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700609
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 return true;
611}
612
deadbeefd59daf82015-10-14 15:02:44 -0700613void WebRtcSession::Close() {
614 SetState(STATE_CLOSED);
615 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000616 ASSERT(!voice_channel_);
617 ASSERT(!video_channel_);
618 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800619 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620}
621
deadbeef0ed85b22016-02-23 17:24:52 -0800622cricket::BaseChannel* WebRtcSession::GetChannel(
623 const std::string& content_name) {
624 if (voice_channel() && voice_channel()->content_name() == content_name) {
625 return voice_channel();
626 }
627 if (video_channel() && video_channel()->content_name() == content_name) {
628 return video_channel();
629 }
630 if (data_channel() && data_channel()->content_name() == content_name) {
631 return data_channel();
632 }
633 return nullptr;
634}
635
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000636void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
637 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638}
639
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000640cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
641 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642}
643
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800644bool WebRtcSession::GetSslRole(const std::string& transport_name,
645 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700646 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000647 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
648 << "SSL Role of the session.";
649 return false;
650 }
651
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800652 return transport_controller_->GetSslRole(transport_name, role);
653}
654
655bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
656 rtc::SSLRole* role) {
657 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000658}
659
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000660void WebRtcSession::CreateOffer(
661 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700662 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
663 const cricket::MediaSessionOptions& session_options) {
664 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000665}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666
deadbeefab9b2d12015-10-14 11:33:11 -0700667void WebRtcSession::CreateAnswer(
668 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700669 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800670 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671}
672
673bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
674 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700675 ASSERT(signaling_thread()->IsCurrent());
676
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000677 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000678 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000679
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000680 // Validate SDP.
681 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
682 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 }
684
deadbeefd59daf82015-10-14 15:02:44 -0700685 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000686 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700688 initial_offerer_ = true;
689 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 }
691
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000692 cricket::SecurePolicy sdes_policy =
693 webrtc_session_desc_factory_->SdesPolicy();
694 cricket::CryptoType crypto_required = dtls_enabled_ ?
695 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
696 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000698 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000700 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701
702 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000703 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 // TODO(mallinath) - Handle CreateChannel failure, as new local description
705 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000706 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708
deadbeefcbecd352015-09-23 11:50:27 -0700709 // Remove unused channels if MediaContentDescription is rejected.
710 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000712 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 return false;
714 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000715
deadbeefd59daf82015-10-14 15:02:44 -0700716 if (remote_desc_) {
717 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700718 UseCandidatesInSessionDescription(remote_desc_.get());
719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720
deadbeef0ed85b22016-02-23 17:24:52 -0800721 pending_ice_restarts_.clear();
722
deadbeefd59daf82015-10-14 15:02:44 -0700723 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000724 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726 return true;
727}
728
729bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
730 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700731 ASSERT(signaling_thread()->IsCurrent());
732
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000733 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000734 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000735
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000736 // Validate SDP.
737 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
738 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 }
740
deadbeefd59daf82015-10-14 15:02:44 -0700741 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
742 remote_desc_.release());
743 remote_desc_.reset(desc_temp.release());
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000746 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 if (action == kOffer && !CreateChannels(desc->description())) {
748 // TODO(mallinath) - Handle CreateChannel failure, as new local description
749 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000750 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752
deadbeefcbecd352015-09-23 11:50:27 -0700753 // Remove unused channels if MediaContentDescription is rejected.
754 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755
756 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
757 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000758 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 return false;
760 }
761
deadbeefd59daf82015-10-14 15:02:44 -0700762 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000763 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
deadbeef0ed85b22016-02-23 17:24:52 -0800766 if (old_remote_desc) {
767 for (const cricket::ContentInfo& content :
768 old_remote_desc->description()->contents()) {
769 // Check if this new SessionDescription contains new ICE ufrag and
770 // password that indicates the remote peer requests an ICE restart.
771 // TODO(deadbeef): When we start storing both the current and pending
772 // remote description, this should reset pending_ice_restarts and compare
773 // against the current description.
774 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
775 if (action == kOffer) {
776 pending_ice_restarts_.insert(content.name);
777 }
778 } else {
779 // We retain all received candidates only if ICE is not restarted.
780 // When ICE is restarted, all previous candidates belong to an old
781 // generation and should not be kept.
782 // TODO(deadbeef): This goes against the W3C spec which says the remote
783 // description should only contain candidates from the last set remote
784 // description plus any candidates added since then. We should remove
785 // this once we're sure it won't break anything.
786 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
787 old_remote_desc.get(), content.name, desc);
788 }
789 }
honghaiz503726c2015-07-31 12:37:38 -0700790 }
791
deadbeefd59daf82015-10-14 15:02:44 -0700792 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000793 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000795
796 // Set the the ICE connection state to connecting since the connection may
797 // become writable with peer reflexive candidates before any remote candidate
798 // is signaled.
799 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
800 // is to have a new signal the indicates a change in checking state from the
801 // transport and expose a new checking() member from transport that can be
802 // read to determine the current checking state. The existing SignalConnecting
803 // actually means "gathering candidates", so cannot be be used here.
804 if (desc->type() != SessionDescriptionInterface::kOffer &&
805 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
806 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
807 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 return true;
809}
810
deadbeefd59daf82015-10-14 15:02:44 -0700811void WebRtcSession::LogState(State old_state, State new_state) {
812 LOG(LS_INFO) << "Session:" << id()
813 << " Old state:" << GetStateString(old_state)
814 << " New state:" << GetStateString(new_state);
815}
816
817void WebRtcSession::SetState(State state) {
818 ASSERT(signaling_thread_->IsCurrent());
819 if (state != state_) {
820 LogState(state_, state);
821 state_ = state;
822 SignalState(this, state_);
823 }
824}
825
826void WebRtcSession::SetError(Error error, const std::string& error_desc) {
827 ASSERT(signaling_thread_->IsCurrent());
828 if (error != error_) {
829 error_ = error;
830 error_desc_ = error_desc;
831 }
832}
833
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834bool WebRtcSession::UpdateSessionState(
835 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700837 ASSERT(signaling_thread()->IsCurrent());
838
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 // If there's already a pending error then no state transition should happen.
840 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700841 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000842 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000844 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
845 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 }
deadbeefd59daf82015-10-14 15:02:44 -0700847 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
848 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000849 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700850 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000851 }
deadbeefd59daf82015-10-14 15:02:44 -0700852 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000853 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
855 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000856 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
857 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700860 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
861 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000862 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700863 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000864 }
deadbeefd59daf82015-10-14 15:02:44 -0700865 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000866 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700869 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700870 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700871 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700872 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700873 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800874 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700875 const cricket::ContentGroup* answer_bundle =
876 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
877 if (!EnableBundle(*answer_bundle)) {
878 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
879 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
880 }
881 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800882 // Only push down the transport description after enabling BUNDLE; we don't
883 // want to push down a description on a transport about to be destroyed.
884 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
885 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
886 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700888 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000889 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700890 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000891 }
deadbeefd59daf82015-10-14 15:02:44 -0700892 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000893 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895 }
896 return true;
897}
898
899WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
900 if (type == SessionDescriptionInterface::kOffer) {
901 return WebRtcSession::kOffer;
902 } else if (type == SessionDescriptionInterface::kPrAnswer) {
903 return WebRtcSession::kPrAnswer;
904 } else if (type == SessionDescriptionInterface::kAnswer) {
905 return WebRtcSession::kAnswer;
906 }
907 ASSERT(false && "unknown action type");
908 return WebRtcSession::kOffer;
909}
910
deadbeefd59daf82015-10-14 15:02:44 -0700911bool WebRtcSession::PushdownMediaDescription(
912 cricket::ContentAction action,
913 cricket::ContentSource source,
914 std::string* err) {
915 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
916 if (!ch) {
917 return true;
918 } else if (source == cricket::CS_LOCAL) {
919 return ch->PushdownLocalDescription(local_desc_->description(), action,
920 err);
921 } else {
922 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
923 err);
924 }
925 };
926
927 return (set_content(voice_channel()) &&
928 set_content(video_channel()) &&
929 set_content(data_channel()));
930}
931
932bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
933 cricket::ContentAction action,
934 std::string* error_desc) {
935 RTC_DCHECK(signaling_thread()->IsCurrent());
936
937 if (source == cricket::CS_LOCAL) {
938 return PushdownLocalTransportDescription(local_desc_->description(), action,
939 error_desc);
940 }
941 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
942 error_desc);
943}
944
945bool WebRtcSession::PushdownLocalTransportDescription(
946 const SessionDescription* sdesc,
947 cricket::ContentAction action,
948 std::string* err) {
949 RTC_DCHECK(signaling_thread()->IsCurrent());
950
951 if (!sdesc) {
952 return false;
953 }
954
955 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
956 if (!transport_controller_->SetLocalTransportDescription(
957 tinfo.content_name, tinfo.description, action, err)) {
958 return false;
959 }
960 }
961
962 return true;
963}
964
965bool WebRtcSession::PushdownRemoteTransportDescription(
966 const SessionDescription* sdesc,
967 cricket::ContentAction action,
968 std::string* err) {
969 RTC_DCHECK(signaling_thread()->IsCurrent());
970
971 if (!sdesc) {
972 return false;
973 }
974
975 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
976 if (!transport_controller_->SetRemoteTransportDescription(
977 tinfo.content_name, tinfo.description, action, err)) {
978 return false;
979 }
980 }
981
982 return true;
983}
984
985bool WebRtcSession::GetTransportDescription(
986 const SessionDescription* description,
987 const std::string& content_name,
988 cricket::TransportDescription* tdesc) {
989 if (!description || !tdesc) {
990 return false;
991 }
992 const TransportInfo* transport_info =
993 description->GetTransportInfoByName(content_name);
994 if (!transport_info) {
995 return false;
996 }
997 *tdesc = transport_info->description;
998 return true;
999}
1000
1001bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001002 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001003 return (GetChannelTransportStats(voice_channel(), stats) &&
1004 GetChannelTransportStats(video_channel(), stats) &&
1005 GetChannelTransportStats(data_channel(), stats));
1006}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001007
deadbeefcbecd352015-09-23 11:50:27 -07001008bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001009 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001010 ASSERT(signaling_thread()->IsCurrent());
1011 if (!ch) {
1012 // Not using this channel.
1013 return true;
1014 }
1015
1016 const std::string& content_name = ch->content_name();
1017 const std::string& transport_name = ch->transport_name();
1018 stats->proxy_to_transport[content_name] = transport_name;
1019 if (stats->transport_stats.find(transport_name) !=
1020 stats->transport_stats.end()) {
1021 // Transport stats already done for this transport.
1022 return true;
1023 }
1024
1025 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001026 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001027 return false;
1028 }
1029
1030 stats->transport_stats[transport_name] = tstats;
1031 return true;
1032}
1033
1034bool WebRtcSession::GetLocalCertificate(
1035 const std::string& transport_name,
1036 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1037 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001038 return transport_controller_->GetLocalCertificate(transport_name,
1039 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001040}
1041
1042bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1043 rtc::SSLCertificate** cert) {
1044 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001045 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001046}
1047
deadbeefcbecd352015-09-23 11:50:27 -07001048bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1049 const std::string* first_content_name = bundle.FirstContentName();
1050 if (!first_content_name) {
1051 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1052 return false;
1053 }
1054 const std::string& transport_name = *first_content_name;
1055 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1056
1057 auto maybe_set_transport = [this, bundle, transport_name,
1058 first_channel](cricket::BaseChannel* ch) {
1059 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001060 return true;
1061 }
1062
deadbeefcbecd352015-09-23 11:50:27 -07001063 if (ch->transport_name() == transport_name) {
1064 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1065 << " on " << transport_name << ".";
1066 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001067 }
torbjornga81a42f2015-09-23 02:16:58 -07001068
deadbeefcbecd352015-09-23 11:50:27 -07001069 if (!ch->SetTransport(transport_name)) {
1070 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1071 return false;
1072 }
1073 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1074 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001075 return true;
1076 };
1077
deadbeefcbecd352015-09-23 11:50:27 -07001078 if (!maybe_set_transport(voice_channel()) ||
1079 !maybe_set_transport(video_channel()) ||
1080 !maybe_set_transport(data_channel())) {
1081 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001082 }
deadbeefcbecd352015-09-23 11:50:27 -07001083
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001084 return true;
1085}
1086
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001088 if (!remote_desc_) {
1089 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1090 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001091 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 }
1093
1094 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001095 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 return false;
1097 }
1098
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001099 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001100 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1101 if (!valid) {
1102 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 }
1104
1105 // Add this candidate to the remote session description.
1106 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001107 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 return false;
1109 }
1110
deadbeefd59daf82015-10-14 15:02:44 -07001111 if (ready) {
1112 return UseCandidate(candidate);
1113 } else {
1114 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1115 return true;
1116 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117}
1118
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001119bool WebRtcSession::RemoveRemoteIceCandidates(
1120 const std::vector<cricket::Candidate>& candidates) {
1121 if (!remote_desc_) {
1122 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1123 << "removed without any remote session description.";
1124 return false;
1125 }
1126
1127 if (candidates.empty()) {
1128 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1129 return false;
1130 }
1131
1132 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1133 if (number_removed != candidates.size()) {
1134 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1135 << "Requested " << candidates.size() << " but only "
1136 << number_removed << " are removed.";
1137 }
1138
1139 // Remove the candidates from the transport controller.
1140 std::string error;
1141 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1142 if (!res && !error.empty()) {
1143 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1144 }
1145 return true;
1146}
1147
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001148bool WebRtcSession::SetIceTransports(
1149 PeerConnectionInterface::IceTransportsType type) {
1150 return port_allocator()->set_candidate_filter(
1151 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001152}
1153
deadbeefd59daf82015-10-14 15:02:44 -07001154cricket::IceConfig WebRtcSession::ParseIceConfig(
1155 const PeerConnectionInterface::RTCConfiguration& config) const {
1156 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001157 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001158 ice_config.prioritize_most_likely_candidate_pairs =
1159 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001160 ice_config.backup_connection_ping_interval =
1161 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001162 ice_config.gather_continually = (config.continual_gathering_policy ==
1163 PeerConnectionInterface::GATHER_CONTINUALLY);
1164 return ice_config;
1165}
1166
1167void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1168 transport_controller_->SetIceConfig(config);
1169}
1170
1171void WebRtcSession::MaybeStartGathering() {
1172 transport_controller_->MaybeStartGathering();
1173}
1174
Peter Boström0c4e06b2015-10-07 12:23:21 +02001175bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1176 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001177 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001179 }
1180 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181}
1182
Peter Boström0c4e06b2015-10-07 12:23:21 +02001183bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1184 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001185 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001186 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001187 }
1188 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189}
1190
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001191std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 return desc.str();
1195}
1196
solenbergd4cec0d2015-10-09 08:55:48 -07001197void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 ASSERT(signaling_thread()->IsCurrent());
1199 if (!voice_channel_) {
1200 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1201 return;
1202 }
solenberg4bac9c52015-10-09 02:32:53 -07001203 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1204 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 // otherwise. This in the normal case when the underlying media channel has
1206 // already been deleted.
1207 ASSERT(enable == false);
1208 }
1209}
1210
Peter Boström0c4e06b2015-10-07 12:23:21 +02001211void WebRtcSession::SetAudioSend(uint32_t ssrc,
1212 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001213 const cricket::AudioOptions& options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001214 cricket::AudioSource* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 ASSERT(signaling_thread()->IsCurrent());
1216 if (!voice_channel_) {
1217 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1218 return;
1219 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001220 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, source)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001221 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223}
1224
Peter Boström0c4e06b2015-10-07 12:23:21 +02001225void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001226 ASSERT(signaling_thread()->IsCurrent());
1227 ASSERT(volume >= 0 && volume <= 10);
1228 if (!voice_channel_) {
1229 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1230 return;
1231 }
1232
solenberg4bac9c52015-10-09 02:32:53 -07001233 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001234 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001235 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001236}
1237
deadbeef2d110be2016-01-13 12:00:26 -08001238void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1239 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001240 ASSERT(signaling_thread()->IsCurrent());
1241 if (!voice_channel_)
1242 return;
1243
kwiberg31022942016-03-11 14:18:21 -08001244 voice_channel_->SetRawAudioSink(ssrc, rtc::ScopedToUnique(std::move(sink)));
Tommif888bb52015-12-12 01:37:01 +01001245}
1246
Peter Boström0c4e06b2015-10-07 12:23:21 +02001247bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 cricket::VideoCapturer* camera) {
1249 ASSERT(signaling_thread()->IsCurrent());
1250
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001251 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1253 // support video.
1254 LOG(LS_WARNING) << "Video not used in this call.";
1255 return false;
1256 }
1257 if (!video_channel_->SetCapturer(ssrc, camera)) {
1258 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1259 // This in the normal case when the underlying media channel has already
1260 // been deleted.
1261 ASSERT(camera == NULL);
1262 return false;
1263 }
1264 return true;
1265}
1266
nisse08582ff2016-02-04 01:24:52 -08001267void WebRtcSession::SetVideoPlayout(
1268 uint32_t ssrc,
1269 bool enable,
1270 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 ASSERT(signaling_thread()->IsCurrent());
1272 if (!video_channel_) {
1273 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1274 return;
1275 }
nisse08582ff2016-02-04 01:24:52 -08001276 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1277 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 // This in the normal case when the underlying media channel has already
1279 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001280 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
1282}
1283
Peter Boström0c4e06b2015-10-07 12:23:21 +02001284void WebRtcSession::SetVideoSend(uint32_t ssrc,
1285 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 const cricket::VideoOptions* options) {
1287 ASSERT(signaling_thread()->IsCurrent());
1288 if (!video_channel_) {
1289 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1290 return;
1291 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001292 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1294 // This in the normal case when the underlying media channel has already
1295 // been deleted.
1296 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298}
1299
1300bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1301 ASSERT(signaling_thread()->IsCurrent());
1302 if (!voice_channel_) {
1303 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1304 return false;
1305 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001306 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1308 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001309 if (!local_desc_ ||
1310 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 &send_ssrc)) {
1312 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1313 return false;
1314 }
1315 return voice_channel_->CanInsertDtmf();
1316}
1317
1318bool WebRtcSession::InsertDtmf(const std::string& track_id,
1319 int code, int duration) {
1320 ASSERT(signaling_thread()->IsCurrent());
1321 if (!voice_channel_) {
1322 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1323 return false;
1324 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001325 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001326 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1327 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1329 return false;
1330 }
solenberg1d63dd02015-12-02 12:35:09 -08001331 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1333 return false;
1334 }
1335 return true;
1336}
1337
1338sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001339 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
wu@webrtc.org78187522013-10-07 23:32:02 +00001342bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001343 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001344 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001345 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001346 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1347 return false;
1348 }
1349 return data_channel_->SendData(params, payload, result);
1350}
1351
1352bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001353 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001354 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1355 return false;
1356 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001357 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1358 &DataChannel::OnChannelReady);
1359 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1360 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001361 data_channel_->SignalStreamClosedRemotely.connect(
1362 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001363 return true;
1364}
1365
1366void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001367 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001368 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1369 return;
1370 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001371 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1372 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001373 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001374}
1375
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001376void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001377 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001378 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1379 return;
1380 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001381 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1382 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001383}
1384
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001385void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001386 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001387 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1388 << "NULL.";
1389 return;
1390 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001391 data_channel_->RemoveRecvStream(sid);
1392 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001393}
1394
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001395bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001396 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001397}
1398
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399cricket::DataChannelType WebRtcSession::data_channel_type() const {
1400 return data_channel_type_;
1401}
1402
deadbeef0ed85b22016-02-23 17:24:52 -08001403bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1404 return pending_ice_restarts_.find(content_name) !=
1405 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001406}
1407
Henrik Boströmd8281982015-08-27 10:12:24 +02001408void WebRtcSession::OnCertificateReady(
1409 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001410 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001411}
1412
Henrik Boströmd8281982015-08-27 10:12:24 +02001413bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001414 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001415}
1416
deadbeefcbecd352015-09-23 11:50:27 -07001417const rtc::scoped_refptr<rtc::RTCCertificate>&
1418WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001419 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001420}
1421
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422void WebRtcSession::SetIceConnectionState(
1423 PeerConnectionInterface::IceConnectionState state) {
1424 if (ice_connection_state_ == state) {
1425 return;
1426 }
1427
1428 // ASSERT that the requested transition is allowed. Note that
1429 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1430 // within PeerConnection). This switch statement should compile away when
1431 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001432 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1433 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 switch (ice_connection_state_) {
1435 case PeerConnectionInterface::kIceConnectionNew:
1436 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1437 break;
1438 case PeerConnectionInterface::kIceConnectionChecking:
1439 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1440 state == PeerConnectionInterface::kIceConnectionConnected);
1441 break;
1442 case PeerConnectionInterface::kIceConnectionConnected:
1443 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1444 state == PeerConnectionInterface::kIceConnectionChecking ||
1445 state == PeerConnectionInterface::kIceConnectionCompleted);
1446 break;
1447 case PeerConnectionInterface::kIceConnectionCompleted:
1448 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1449 state == PeerConnectionInterface::kIceConnectionDisconnected);
1450 break;
1451 case PeerConnectionInterface::kIceConnectionFailed:
1452 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1453 break;
1454 case PeerConnectionInterface::kIceConnectionDisconnected:
1455 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1456 state == PeerConnectionInterface::kIceConnectionConnected ||
1457 state == PeerConnectionInterface::kIceConnectionCompleted ||
1458 state == PeerConnectionInterface::kIceConnectionFailed);
1459 break;
1460 case PeerConnectionInterface::kIceConnectionClosed:
1461 ASSERT(false);
1462 break;
1463 default:
1464 ASSERT(false);
1465 break;
1466 }
1467
1468 ice_connection_state_ = state;
1469 if (ice_observer_) {
1470 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1471 }
1472}
1473
deadbeefcbecd352015-09-23 11:50:27 -07001474void WebRtcSession::OnTransportControllerConnectionState(
1475 cricket::IceConnectionState state) {
1476 switch (state) {
1477 case cricket::kIceConnectionConnecting:
1478 // If the current state is Connected or Completed, then there were
1479 // writable channels but now there are not, so the next state must
1480 // be Disconnected.
1481 // kIceConnectionConnecting is currently used as the default,
1482 // un-connected state by the TransportController, so its only use is
1483 // detecting disconnections.
1484 if (ice_connection_state_ ==
1485 PeerConnectionInterface::kIceConnectionConnected ||
1486 ice_connection_state_ ==
1487 PeerConnectionInterface::kIceConnectionCompleted) {
1488 SetIceConnectionState(
1489 PeerConnectionInterface::kIceConnectionDisconnected);
1490 }
torbjornga81a42f2015-09-23 02:16:58 -07001491 break;
deadbeefcbecd352015-09-23 11:50:27 -07001492 case cricket::kIceConnectionFailed:
1493 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1494 break;
1495 case cricket::kIceConnectionConnected:
1496 LOG(LS_INFO) << "Changing to ICE connected state because "
1497 << "all transports are writable.";
1498 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1499 break;
1500 case cricket::kIceConnectionCompleted:
1501 LOG(LS_INFO) << "Changing to ICE completed state because "
1502 << "all transports are complete.";
1503 if (ice_connection_state_ !=
1504 PeerConnectionInterface::kIceConnectionConnected) {
1505 // If jumping directly from "checking" to "connected",
1506 // signal "connected" first.
1507 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1508 }
1509 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1510 if (metrics_observer_) {
1511 ReportTransportStats();
1512 }
1513 break;
1514 default:
1515 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001516 }
deadbeefcbecd352015-09-23 11:50:27 -07001517}
1518
1519void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001520 SetIceConnectionReceiving(receiving);
1521}
1522
1523void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1524 if (ice_connection_receiving_ == receiving) {
1525 return;
1526 }
1527 ice_connection_receiving_ = receiving;
1528 if (ice_observer_) {
1529 ice_observer_->OnIceConnectionReceivingChange(receiving);
1530 }
1531}
1532
deadbeefcbecd352015-09-23 11:50:27 -07001533void WebRtcSession::OnTransportControllerCandidatesGathered(
1534 const std::string& transport_name,
1535 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001537 int sdp_mline_index;
1538 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1539 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1540 << transport_name << " not found";
1541 return;
1542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543
deadbeefcbecd352015-09-23 11:50:27 -07001544 for (cricket::Candidates::const_iterator citer = candidates.begin();
1545 citer != candidates.end(); ++citer) {
1546 // Use transport_name as the candidate media id.
1547 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1548 if (ice_observer_) {
1549 ice_observer_->OnIceCandidate(&candidate);
1550 }
1551 if (local_desc_) {
1552 local_desc_->AddCandidate(&candidate);
1553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 }
1555}
1556
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001557void WebRtcSession::OnTransportControllerCandidatesRemoved(
1558 const std::vector<cricket::Candidate>& candidates) {
1559 ASSERT(signaling_thread()->IsCurrent());
1560 // Sanity check.
1561 for (const cricket::Candidate& candidate : candidates) {
1562 if (candidate.transport_name().empty()) {
1563 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1564 << "empty content name in candidate "
1565 << candidate.ToString();
1566 return;
1567 }
1568 }
1569
1570 if (local_desc_) {
1571 local_desc_->RemoveCandidates(candidates);
1572 }
1573 if (ice_observer_) {
1574 ice_observer_->OnIceCandidatesRemoved(candidates);
1575 }
1576}
1577
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578// Enabling voice and video channel.
1579void WebRtcSession::EnableChannels() {
1580 if (voice_channel_ && !voice_channel_->enabled())
1581 voice_channel_->Enable(true);
1582
1583 if (video_channel_ && !video_channel_->enabled())
1584 video_channel_->Enable(true);
1585
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001586 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 data_channel_->Enable(true);
1588}
1589
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590// Returns the media index for a local ice candidate given the content name.
1591bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1592 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001593 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001595 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596
1597 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001598 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 for (size_t index = 0; index < contents.size(); ++index) {
1600 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001601 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 content_found = true;
1603 break;
1604 }
1605 }
1606 return content_found;
1607}
1608
1609bool WebRtcSession::UseCandidatesInSessionDescription(
1610 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001611 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001613 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001615
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1617 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001618 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001619 const IceCandidateInterface* candidate = candidates->at(n);
1620 bool valid = false;
1621 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1622 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001623 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1624 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001625 }
1626 continue;
1627 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001628 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001629 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 break;
deadbeefd59daf82015-10-14 15:02:44 -07001631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633 }
1634 return ret;
1635}
1636
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001637bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001639 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001641 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 return false;
1643 }
1644
1645 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001646 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 std::vector<cricket::Candidate> candidates;
1648 candidates.push_back(candidate->candidate());
1649 // Invoking BaseSession method to handle remote candidates.
1650 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001651 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1652 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 // Candidates successfully submitted for checking.
1654 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1655 ice_connection_state_ ==
1656 PeerConnectionInterface::kIceConnectionDisconnected) {
1657 // If state is New, then the session has just gotten its first remote ICE
1658 // candidates, so go to Checking.
1659 // If state is Disconnected, the session is re-using old candidates or
1660 // receiving additional ones, so go to Checking.
1661 // If state is Connected, stay Connected.
1662 // TODO(bemasc): If state is Connected, and the new candidates are for a
1663 // newly added transport, then the state actually _should_ move to
1664 // checking. Add a way to distinguish that case.
1665 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1666 }
1667 // TODO(bemasc): If state is Completed, go back to Connected.
1668 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001669 if (!error.empty()) {
1670 LOG(LS_WARNING) << error;
1671 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 }
1673 return true;
1674}
1675
deadbeefcbecd352015-09-23 11:50:27 -07001676void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001677 // Destroy video_channel_ first since it may have a pointer to the
1678 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 const cricket::ContentInfo* video_info =
1680 cricket::GetFirstVideoContent(desc);
1681 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 }
1685
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001686 const cricket::ContentInfo* voice_info =
1687 cricket::GetFirstAudioContent(desc);
1688 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001689 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001690 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001691 }
1692
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 const cricket::ContentInfo* data_info =
1694 cricket::GetFirstDataContent(desc);
1695 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 }
1699}
1700
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001701// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001702// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 // Creating the media channels and transport proxies.
1705 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1706 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001707 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 LOG(LS_ERROR) << "Failed to create voice channel.";
1709 return false;
1710 }
1711 }
1712
1713 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1714 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001715 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 LOG(LS_ERROR) << "Failed to create video channel.";
1717 return false;
1718 }
1719 }
1720
1721 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1722 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001723 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001724 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 LOG(LS_ERROR) << "Failed to create data channel.";
1726 return false;
1727 }
1728 }
1729
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001730 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1731 if (voice_channel()) {
1732 voice_channel()->ActivateRtcpMux();
1733 }
1734 if (video_channel()) {
1735 video_channel()->ActivateRtcpMux();
1736 }
1737 if (data_channel()) {
1738 data_channel()->ActivateRtcpMux();
1739 }
1740 }
1741
deadbeefcbecd352015-09-23 11:50:27 -07001742 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001743 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001744 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1745 cricket::GROUP_TYPE_BUNDLE);
1746 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001747 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1748 return false;
1749 }
deadbeefcbecd352015-09-23 11:50:27 -07001750 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001751 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1752 return false;
1753 }
1754 }
1755
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 return true;
1757}
1758
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001759bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001761 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001762 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001763 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001764 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001765 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001766
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001767 voice_channel_->SignalDtlsSetupFailure.connect(
1768 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001769
1770 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001771 voice_channel_->transport_channel()->SignalSentPacket.connect(
1772 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001773 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774}
1775
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001776bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001778 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001779 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001780 if (!video_channel_) {
1781 return false;
1782 }
1783
1784 video_channel_->SignalDtlsSetupFailure.connect(
1785 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001786
1787 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001788 video_channel_->transport_channel()->SignalSentPacket.connect(
1789 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001790 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001794 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001796 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001797 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001798 return false;
1799 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001800
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001801 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001802 data_channel_->SignalDataReceived.connect(
1803 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001804 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001805
1806 data_channel_->SignalDtlsSetupFailure.connect(
1807 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001808
1809 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001810 data_channel_->transport_channel()->SignalSentPacket.connect(
1811 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001812 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813}
1814
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001815void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001816 SetError(ERROR_TRANSPORT,
1817 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818}
1819
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001820void WebRtcSession::OnDataChannelMessageReceived(
1821 cricket::DataChannel* channel,
1822 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001823 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001824 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1825 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1826 // Received OPEN message; parse and signal that a new data channel should
1827 // be created.
1828 std::string label;
1829 InternalDataChannelInit config;
1830 config.id = params.ssrc;
1831 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1832 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1833 << params.ssrc;
1834 return;
1835 }
1836 config.open_handshake_role = InternalDataChannelInit::kAcker;
1837 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 }
deadbeefab9b2d12015-10-14 11:33:11 -07001839 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840}
1841
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001842// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001843bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001844 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1845 if (!bundle_enabled)
1846 return true;
1847
1848 const cricket::ContentGroup* bundle_group =
1849 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1850 ASSERT(bundle_group != NULL);
1851
1852 const cricket::ContentInfos& contents = desc->contents();
1853 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1854 citer != contents.end(); ++citer) {
1855 const cricket::ContentInfo* content = (&*citer);
1856 ASSERT(content != NULL);
1857 if (bundle_group->HasContentName(content->name) &&
1858 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1859 if (!HasRtcpMuxEnabled(content))
1860 return false;
1861 }
1862 }
1863 // RTCP-MUX is enabled in all the contents.
1864 return true;
1865}
1866
1867bool WebRtcSession::HasRtcpMuxEnabled(
1868 const cricket::ContentInfo* content) {
1869 const cricket::MediaContentDescription* description =
1870 static_cast<cricket::MediaContentDescription*>(content->description);
1871 return description->rtcp_mux();
1872}
1873
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001874bool WebRtcSession::ValidateSessionDescription(
1875 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001876 cricket::ContentSource source, std::string* err_desc) {
1877 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001878 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001879 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001880 }
1881
1882 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001883 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001884 }
1885
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001886 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001887 Action action = GetAction(sdesc->type());
1888 if (source == cricket::CS_LOCAL) {
1889 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001890 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001891 } else {
1892 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001893 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001894 }
1895
1896 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001897 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001898 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1899 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001900 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001901 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001902 }
1903
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001904 // Verify ice-ufrag and ice-pwd.
1905 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001906 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001907 }
1908
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001909 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001910 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001911 }
1912
1913 // Verify m-lines in Answer when compared against Offer.
1914 if (action == kAnswer) {
1915 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001916 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1917 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001918 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001919 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001920 }
1921 }
1922
1923 return true;
1924}
1925
1926bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1927 return ((action == kOffer && state() == STATE_INIT) ||
1928 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001929 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001931 (action == kOffer && state() == STATE_INPROGRESS) ||
1932 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001933 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1934 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1935 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1936 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937}
1938
1939bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1940 return ((action == kOffer && state() == STATE_INIT) ||
1941 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001942 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001943 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944 (action == kOffer && state() == STATE_INPROGRESS) ||
1945 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001946 (action == kAnswer && state() == STATE_SENTOFFER) ||
1947 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1948 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1949 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001950}
1951
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001952std::string WebRtcSession::GetSessionErrorMsg() {
1953 std::ostringstream desc;
1954 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1955 desc << kSessionErrorDesc << error_desc() << ".";
1956 return desc.str();
1957}
1958
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001959// We need to check the local/remote description for the Transport instead of
1960// the session, because a new Transport added during renegotiation may have
1961// them unset while the session has them set from the previous negotiation.
1962// Not doing so may trigger the auto generation of transport description and
1963// mess up DTLS identity information, ICE credential, etc.
1964bool WebRtcSession::ReadyToUseRemoteCandidate(
1965 const IceCandidateInterface* candidate,
1966 const SessionDescriptionInterface* remote_desc,
1967 bool* valid) {
1968 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001969
1970 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001971 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001972
deadbeefd59daf82015-10-14 15:02:44 -07001973 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001974 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001975 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001976
1977 size_t mediacontent_index =
1978 static_cast<size_t>(candidate->sdp_mline_index());
1979 size_t remote_content_size =
1980 current_remote_desc->description()->contents().size();
1981 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001982 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
1983 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001984
1985 *valid = false;
1986 return false;
1987 }
1988
1989 cricket::ContentInfo content =
1990 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001991 cricket::BaseChannel* channel = GetChannel(content.name);
1992 if (!channel) {
1993 return false;
1994 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001995
deadbeefd59daf82015-10-14 15:02:44 -07001996 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001997 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001998}
1999
deadbeefcbecd352015-09-23 11:50:27 -07002000void WebRtcSession::OnTransportControllerGatheringState(
2001 cricket::IceGatheringState state) {
2002 ASSERT(signaling_thread()->IsCurrent());
2003 if (state == cricket::kIceGatheringGathering) {
2004 if (ice_observer_) {
2005 ice_observer_->OnIceGatheringChange(
2006 PeerConnectionInterface::kIceGatheringGathering);
2007 }
2008 } else if (state == cricket::kIceGatheringComplete) {
2009 if (ice_observer_) {
2010 ice_observer_->OnIceGatheringChange(
2011 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002012 }
2013 }
2014}
2015
2016void WebRtcSession::ReportTransportStats() {
2017 // Use a set so we don't report the same stats twice if two channels share
2018 // a transport.
2019 std::set<std::string> transport_names;
2020 if (voice_channel()) {
2021 transport_names.insert(voice_channel()->transport_name());
2022 }
2023 if (video_channel()) {
2024 transport_names.insert(video_channel()->transport_name());
2025 }
2026 if (data_channel()) {
2027 transport_names.insert(data_channel()->transport_name());
2028 }
2029 for (const auto& name : transport_names) {
2030 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002031 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002032 ReportBestConnectionState(stats);
2033 ReportNegotiatedCiphers(stats);
2034 }
2035 }
2036}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002037// Walk through the ConnectionInfos to gather best connection usage
2038// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002039void WebRtcSession::ReportBestConnectionState(
2040 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002041 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002042 for (cricket::TransportChannelStatsList::const_iterator it =
2043 stats.channel_stats.begin();
2044 it != stats.channel_stats.end(); ++it) {
2045 for (cricket::ConnectionInfos::const_iterator it_info =
2046 it->connection_infos.begin();
2047 it_info != it->connection_infos.end(); ++it_info) {
2048 if (!it_info->best_connection) {
2049 continue;
2050 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002051
2052 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2053 const cricket::Candidate& local = it_info->local_candidate;
2054 const cricket::Candidate& remote = it_info->remote_candidate;
2055
2056 // Increment the counter for IceCandidatePairType.
2057 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2058 (local.type() == RELAY_PORT_TYPE &&
2059 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2060 type = kEnumCounterIceCandidatePairTypeTcp;
2061 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2062 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002063 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002064 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002065 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002066 metrics_observer_->IncrementEnumCounter(
2067 type, GetIceCandidatePairCounter(local, remote),
2068 kIceCandidatePairMax);
2069
2070 // Increment the counter for IP type.
2071 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002072 metrics_observer_->IncrementEnumCounter(
2073 kEnumCounterAddressFamily, kBestConnections_IPv4,
2074 kPeerConnectionAddressFamilyCounter_Max);
2075
2076 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002077 metrics_observer_->IncrementEnumCounter(
2078 kEnumCounterAddressFamily, kBestConnections_IPv6,
2079 kPeerConnectionAddressFamilyCounter_Max);
2080 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002081 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002082 }
2083
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002084 return;
2085 }
2086 }
2087}
2088
jbauchac8869e2015-07-03 01:36:14 -07002089void WebRtcSession::ReportNegotiatedCiphers(
2090 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002091 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002092 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2093 return;
2094 }
2095
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002096 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2097 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2098 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2099 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002100 return;
2101 }
2102
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002103 PeerConnectionEnumCounterType srtp_counter_type;
2104 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002105 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002106 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2107 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002108 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002109 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2110 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002111 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002112 srtp_counter_type = kEnumCounterDataSrtpCipher;
2113 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002114 } else {
2115 RTC_NOTREACHED();
2116 return;
2117 }
2118
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002119 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2120 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2121 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002122 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002123 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2124 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2125 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002126 }
2127}
2128
stefanc1aeaf02015-10-15 07:26:07 -07002129void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2130 const rtc::SentPacket& sent_packet) {
2131 RTC_DCHECK(worker_thread()->IsCurrent());
2132 media_controller_->call_w()->OnSentPacket(sent_packet);
2133}
2134
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135} // namespace webrtc