blob: 9ed5640c509fb1d6d0808719c9db43455c52f7bf [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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
506WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700507 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000508 // Destroy video_channel_ first since it may have a pointer to the
509 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000510 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 SignalVideoChannelDestroyed();
512 channel_manager_->DestroyVideoChannel(video_channel_.release());
513 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000514 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000515 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200516 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000517 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000518 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 SignalDataChannelDestroyed();
520 channel_manager_->DestroyDataChannel(data_channel_.release());
521 }
deadbeef057ecf02016-01-20 14:30:43 -0800522 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700523
524 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525}
526
wu@webrtc.org91053e72013-08-10 07:18:04 +0000527bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000528 const PeerConnectionFactoryInterface::Options& options,
Henrik Boström5e56c592015-08-11 10:33:13 +0200529 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200530 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
531 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700532 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700533 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000534
Henrik Boström87713d02015-08-25 09:53:21 +0200535 // Obtain a certificate from RTCConfiguration if any were provided (optional).
536 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
537 if (!rtc_configuration.certificates.empty()) {
538 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
539 // just picking the first one. The decision should be made based on the DTLS
540 // handshake. The DTLS negotiations need to know about all certificates.
541 certificate = rtc_configuration.certificates[0];
542 }
543
honghaiz1f429e32015-09-28 07:57:34 -0700544 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700545
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000546 if (options.disable_encryption) {
547 dtls_enabled_ = false;
548 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200549 // Enable DTLS by default if we have an identity store or a certificate.
550 dtls_enabled_ = (dtls_identity_store || certificate);
htaa2a49d92016-03-04 02:51:39 -0800551 // |rtc_configuration| can override the default |dtls_enabled_| value.
552 if (rtc_configuration.enable_dtls_srtp) {
553 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000554 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000555 }
556
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000558 // It takes precendence over the disable_sctp_data_channels
559 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800560 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000562 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000563 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000564 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000565 data_channel_type_ = cricket::DCT_SCTP;
566 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
htaa2a49d92016-03-04 02:51:39 -0800569 video_options_.screencast_min_bitrate_kbps =
570 rtc_configuration.screencast_min_bitrate;
571 audio_options_.combined_audio_video_bwe =
572 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000573
kwiberg102c6a62015-10-30 02:47:38 -0700574 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100575 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200576
Karl Wibergbe579832015-11-10 22:34:18 +0100577 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
578 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200579
Henrik Boström87713d02015-08-25 09:53:21 +0200580 if (!dtls_enabled_) {
581 // Construct with DTLS disabled.
582 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700583 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200584 } else {
585 // Construct with DTLS enabled.
586 if (!certificate) {
587 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700588 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200589 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800590 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700591 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200592 } else {
593 // Use the already generated certificate.
594 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700595 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200596 }
597 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000598
Henrik Boströmd8281982015-08-27 10:12:24 +0200599 webrtc_session_desc_factory_->SignalCertificateReady.connect(
600 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000601
wu@webrtc.org97077a32013-10-25 21:18:33 +0000602 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000603 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000604 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000605 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200606 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700607
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 return true;
609}
610
deadbeefd59daf82015-10-14 15:02:44 -0700611void WebRtcSession::Close() {
612 SetState(STATE_CLOSED);
613 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000614 ASSERT(!voice_channel_);
615 ASSERT(!video_channel_);
616 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800617 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618}
619
deadbeef0ed85b22016-02-23 17:24:52 -0800620cricket::BaseChannel* WebRtcSession::GetChannel(
621 const std::string& content_name) {
622 if (voice_channel() && voice_channel()->content_name() == content_name) {
623 return voice_channel();
624 }
625 if (video_channel() && video_channel()->content_name() == content_name) {
626 return video_channel();
627 }
628 if (data_channel() && data_channel()->content_name() == content_name) {
629 return data_channel();
630 }
631 return nullptr;
632}
633
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000634void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
635 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636}
637
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000638cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
639 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640}
641
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800642bool WebRtcSession::GetSslRole(const std::string& transport_name,
643 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700644 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000645 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
646 << "SSL Role of the session.";
647 return false;
648 }
649
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800650 return transport_controller_->GetSslRole(transport_name, role);
651}
652
653bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
654 rtc::SSLRole* role) {
655 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000656}
657
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000658void WebRtcSession::CreateOffer(
659 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700660 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
661 const cricket::MediaSessionOptions& session_options) {
662 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000663}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664
deadbeefab9b2d12015-10-14 11:33:11 -0700665void WebRtcSession::CreateAnswer(
666 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700667 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800668 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669}
670
671bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
672 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700673 ASSERT(signaling_thread()->IsCurrent());
674
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000675 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000676 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000677
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000678 // Validate SDP.
679 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
680 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 }
682
deadbeefd59daf82015-10-14 15:02:44 -0700683 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000684 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700686 initial_offerer_ = true;
687 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 }
689
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000690 cricket::SecurePolicy sdes_policy =
691 webrtc_session_desc_factory_->SdesPolicy();
692 cricket::CryptoType crypto_required = dtls_enabled_ ?
693 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
694 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000696 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000698 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699
700 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000701 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 // TODO(mallinath) - Handle CreateChannel failure, as new local description
703 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000704 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 }
706
deadbeefcbecd352015-09-23 11:50:27 -0700707 // Remove unused channels if MediaContentDescription is rejected.
708 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000710 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 return false;
712 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000713
deadbeefd59daf82015-10-14 15:02:44 -0700714 if (remote_desc_) {
715 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700716 UseCandidatesInSessionDescription(remote_desc_.get());
717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718
deadbeef0ed85b22016-02-23 17:24:52 -0800719 pending_ice_restarts_.clear();
720
deadbeefd59daf82015-10-14 15:02:44 -0700721 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000722 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724 return true;
725}
726
727bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
728 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700729 ASSERT(signaling_thread()->IsCurrent());
730
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000731 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000732 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000733
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000734 // Validate SDP.
735 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
736 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 }
738
deadbeefd59daf82015-10-14 15:02:44 -0700739 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
740 remote_desc_.release());
741 remote_desc_.reset(desc_temp.release());
742
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000744 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 if (action == kOffer && !CreateChannels(desc->description())) {
746 // TODO(mallinath) - Handle CreateChannel failure, as new local description
747 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000748 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750
deadbeefcbecd352015-09-23 11:50:27 -0700751 // Remove unused channels if MediaContentDescription is rejected.
752 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
754 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
755 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000756 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 return false;
758 }
759
deadbeefd59daf82015-10-14 15:02:44 -0700760 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000761 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 }
763
deadbeef0ed85b22016-02-23 17:24:52 -0800764 if (old_remote_desc) {
765 for (const cricket::ContentInfo& content :
766 old_remote_desc->description()->contents()) {
767 // Check if this new SessionDescription contains new ICE ufrag and
768 // password that indicates the remote peer requests an ICE restart.
769 // TODO(deadbeef): When we start storing both the current and pending
770 // remote description, this should reset pending_ice_restarts and compare
771 // against the current description.
772 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
773 if (action == kOffer) {
774 pending_ice_restarts_.insert(content.name);
775 }
776 } else {
777 // We retain all received candidates only if ICE is not restarted.
778 // When ICE is restarted, all previous candidates belong to an old
779 // generation and should not be kept.
780 // TODO(deadbeef): This goes against the W3C spec which says the remote
781 // description should only contain candidates from the last set remote
782 // description plus any candidates added since then. We should remove
783 // this once we're sure it won't break anything.
784 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
785 old_remote_desc.get(), content.name, desc);
786 }
787 }
honghaiz503726c2015-07-31 12:37:38 -0700788 }
789
deadbeefd59daf82015-10-14 15:02:44 -0700790 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000791 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000793
794 // Set the the ICE connection state to connecting since the connection may
795 // become writable with peer reflexive candidates before any remote candidate
796 // is signaled.
797 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
798 // is to have a new signal the indicates a change in checking state from the
799 // transport and expose a new checking() member from transport that can be
800 // read to determine the current checking state. The existing SignalConnecting
801 // actually means "gathering candidates", so cannot be be used here.
802 if (desc->type() != SessionDescriptionInterface::kOffer &&
803 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
804 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
805 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 return true;
807}
808
deadbeefd59daf82015-10-14 15:02:44 -0700809void WebRtcSession::LogState(State old_state, State new_state) {
810 LOG(LS_INFO) << "Session:" << id()
811 << " Old state:" << GetStateString(old_state)
812 << " New state:" << GetStateString(new_state);
813}
814
815void WebRtcSession::SetState(State state) {
816 ASSERT(signaling_thread_->IsCurrent());
817 if (state != state_) {
818 LogState(state_, state);
819 state_ = state;
820 SignalState(this, state_);
821 }
822}
823
824void WebRtcSession::SetError(Error error, const std::string& error_desc) {
825 ASSERT(signaling_thread_->IsCurrent());
826 if (error != error_) {
827 error_ = error;
828 error_desc_ = error_desc;
829 }
830}
831
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832bool WebRtcSession::UpdateSessionState(
833 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700835 ASSERT(signaling_thread()->IsCurrent());
836
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 // If there's already a pending error then no state transition should happen.
838 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700839 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000840 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000842 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
843 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 }
deadbeefd59daf82015-10-14 15:02:44 -0700845 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
846 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000847 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700848 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000849 }
deadbeefd59daf82015-10-14 15:02:44 -0700850 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000851 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
853 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000854 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
855 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700858 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
859 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000860 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700861 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000862 }
deadbeefd59daf82015-10-14 15:02:44 -0700863 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000864 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 }
866 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700867 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700868 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700869 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700870 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700871 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800872 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700873 const cricket::ContentGroup* answer_bundle =
874 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
875 if (!EnableBundle(*answer_bundle)) {
876 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
877 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
878 }
879 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800880 // Only push down the transport description after enabling BUNDLE; we don't
881 // want to push down a description on a transport about to be destroyed.
882 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
883 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700886 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000887 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700888 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000889 }
deadbeefd59daf82015-10-14 15:02:44 -0700890 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 }
894 return true;
895}
896
897WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
898 if (type == SessionDescriptionInterface::kOffer) {
899 return WebRtcSession::kOffer;
900 } else if (type == SessionDescriptionInterface::kPrAnswer) {
901 return WebRtcSession::kPrAnswer;
902 } else if (type == SessionDescriptionInterface::kAnswer) {
903 return WebRtcSession::kAnswer;
904 }
905 ASSERT(false && "unknown action type");
906 return WebRtcSession::kOffer;
907}
908
deadbeefd59daf82015-10-14 15:02:44 -0700909bool WebRtcSession::PushdownMediaDescription(
910 cricket::ContentAction action,
911 cricket::ContentSource source,
912 std::string* err) {
913 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
914 if (!ch) {
915 return true;
916 } else if (source == cricket::CS_LOCAL) {
917 return ch->PushdownLocalDescription(local_desc_->description(), action,
918 err);
919 } else {
920 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
921 err);
922 }
923 };
924
925 return (set_content(voice_channel()) &&
926 set_content(video_channel()) &&
927 set_content(data_channel()));
928}
929
930bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
931 cricket::ContentAction action,
932 std::string* error_desc) {
933 RTC_DCHECK(signaling_thread()->IsCurrent());
934
935 if (source == cricket::CS_LOCAL) {
936 return PushdownLocalTransportDescription(local_desc_->description(), action,
937 error_desc);
938 }
939 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
940 error_desc);
941}
942
943bool WebRtcSession::PushdownLocalTransportDescription(
944 const SessionDescription* sdesc,
945 cricket::ContentAction action,
946 std::string* err) {
947 RTC_DCHECK(signaling_thread()->IsCurrent());
948
949 if (!sdesc) {
950 return false;
951 }
952
953 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
954 if (!transport_controller_->SetLocalTransportDescription(
955 tinfo.content_name, tinfo.description, action, err)) {
956 return false;
957 }
958 }
959
960 return true;
961}
962
963bool WebRtcSession::PushdownRemoteTransportDescription(
964 const SessionDescription* sdesc,
965 cricket::ContentAction action,
966 std::string* err) {
967 RTC_DCHECK(signaling_thread()->IsCurrent());
968
969 if (!sdesc) {
970 return false;
971 }
972
973 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
974 if (!transport_controller_->SetRemoteTransportDescription(
975 tinfo.content_name, tinfo.description, action, err)) {
976 return false;
977 }
978 }
979
980 return true;
981}
982
983bool WebRtcSession::GetTransportDescription(
984 const SessionDescription* description,
985 const std::string& content_name,
986 cricket::TransportDescription* tdesc) {
987 if (!description || !tdesc) {
988 return false;
989 }
990 const TransportInfo* transport_info =
991 description->GetTransportInfoByName(content_name);
992 if (!transport_info) {
993 return false;
994 }
995 *tdesc = transport_info->description;
996 return true;
997}
998
999bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001000 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001001 return (GetChannelTransportStats(voice_channel(), stats) &&
1002 GetChannelTransportStats(video_channel(), stats) &&
1003 GetChannelTransportStats(data_channel(), stats));
1004}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001005
deadbeefcbecd352015-09-23 11:50:27 -07001006bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001007 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001008 ASSERT(signaling_thread()->IsCurrent());
1009 if (!ch) {
1010 // Not using this channel.
1011 return true;
1012 }
1013
1014 const std::string& content_name = ch->content_name();
1015 const std::string& transport_name = ch->transport_name();
1016 stats->proxy_to_transport[content_name] = transport_name;
1017 if (stats->transport_stats.find(transport_name) !=
1018 stats->transport_stats.end()) {
1019 // Transport stats already done for this transport.
1020 return true;
1021 }
1022
1023 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001024 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001025 return false;
1026 }
1027
1028 stats->transport_stats[transport_name] = tstats;
1029 return true;
1030}
1031
1032bool WebRtcSession::GetLocalCertificate(
1033 const std::string& transport_name,
1034 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1035 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001036 return transport_controller_->GetLocalCertificate(transport_name,
1037 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001038}
1039
1040bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1041 rtc::SSLCertificate** cert) {
1042 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001043 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001044}
1045
deadbeefcbecd352015-09-23 11:50:27 -07001046bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1047 const std::string* first_content_name = bundle.FirstContentName();
1048 if (!first_content_name) {
1049 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1050 return false;
1051 }
1052 const std::string& transport_name = *first_content_name;
1053 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1054
1055 auto maybe_set_transport = [this, bundle, transport_name,
1056 first_channel](cricket::BaseChannel* ch) {
1057 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001058 return true;
1059 }
1060
deadbeefcbecd352015-09-23 11:50:27 -07001061 if (ch->transport_name() == transport_name) {
1062 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1063 << " on " << transport_name << ".";
1064 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001065 }
torbjornga81a42f2015-09-23 02:16:58 -07001066
deadbeefcbecd352015-09-23 11:50:27 -07001067 if (!ch->SetTransport(transport_name)) {
1068 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1069 return false;
1070 }
1071 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1072 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001073 return true;
1074 };
1075
deadbeefcbecd352015-09-23 11:50:27 -07001076 if (!maybe_set_transport(voice_channel()) ||
1077 !maybe_set_transport(video_channel()) ||
1078 !maybe_set_transport(data_channel())) {
1079 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001080 }
deadbeefcbecd352015-09-23 11:50:27 -07001081
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001082 return true;
1083}
1084
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001086 if (!remote_desc_) {
1087 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1088 << "without any remote session description.";
tommi6f59a4f2016-03-11 14:05:09 -08001089 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 }
1091
1092 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001093 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 return false;
1095 }
1096
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001097 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001098 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1099 if (!valid) {
1100 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 }
1102
1103 // Add this candidate to the remote session description.
1104 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001105 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 return false;
1107 }
1108
deadbeefd59daf82015-10-14 15:02:44 -07001109 if (ready) {
1110 return UseCandidate(candidate);
1111 } else {
1112 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1113 return true;
1114 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115}
1116
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001117bool WebRtcSession::SetIceTransports(
1118 PeerConnectionInterface::IceTransportsType type) {
1119 return port_allocator()->set_candidate_filter(
1120 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001121}
1122
deadbeefd59daf82015-10-14 15:02:44 -07001123cricket::IceConfig WebRtcSession::ParseIceConfig(
1124 const PeerConnectionInterface::RTCConfiguration& config) const {
1125 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001126 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001127 ice_config.prioritize_most_likely_candidate_pairs =
1128 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001129 ice_config.backup_connection_ping_interval =
1130 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001131 ice_config.gather_continually = (config.continual_gathering_policy ==
1132 PeerConnectionInterface::GATHER_CONTINUALLY);
1133 return ice_config;
1134}
1135
1136void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1137 transport_controller_->SetIceConfig(config);
1138}
1139
1140void WebRtcSession::MaybeStartGathering() {
1141 transport_controller_->MaybeStartGathering();
1142}
1143
Peter Boström0c4e06b2015-10-07 12:23:21 +02001144bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1145 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001146 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001148 }
1149 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150}
1151
Peter Boström0c4e06b2015-10-07 12:23:21 +02001152bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1153 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001154 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001155 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001156 }
1157 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158}
1159
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001160std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 return desc.str();
1164}
1165
solenbergd4cec0d2015-10-09 08:55:48 -07001166void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 ASSERT(signaling_thread()->IsCurrent());
1168 if (!voice_channel_) {
1169 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1170 return;
1171 }
solenberg4bac9c52015-10-09 02:32:53 -07001172 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1173 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 // otherwise. This in the normal case when the underlying media channel has
1175 // already been deleted.
1176 ASSERT(enable == false);
1177 }
1178}
1179
Peter Boström0c4e06b2015-10-07 12:23:21 +02001180void WebRtcSession::SetAudioSend(uint32_t ssrc,
1181 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001182 const cricket::AudioOptions& options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001183 cricket::AudioSource* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 ASSERT(signaling_thread()->IsCurrent());
1185 if (!voice_channel_) {
1186 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1187 return;
1188 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001189 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, source)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001190 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001191 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192}
1193
Peter Boström0c4e06b2015-10-07 12:23:21 +02001194void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001195 ASSERT(signaling_thread()->IsCurrent());
1196 ASSERT(volume >= 0 && volume <= 10);
1197 if (!voice_channel_) {
1198 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1199 return;
1200 }
1201
solenberg4bac9c52015-10-09 02:32:53 -07001202 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001203 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001204 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001205}
1206
deadbeef2d110be2016-01-13 12:00:26 -08001207void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1208 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001209 ASSERT(signaling_thread()->IsCurrent());
1210 if (!voice_channel_)
1211 return;
1212
kwiberg31022942016-03-11 14:18:21 -08001213 voice_channel_->SetRawAudioSink(ssrc, rtc::ScopedToUnique(std::move(sink)));
Tommif888bb52015-12-12 01:37:01 +01001214}
1215
Peter Boström0c4e06b2015-10-07 12:23:21 +02001216bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 cricket::VideoCapturer* camera) {
1218 ASSERT(signaling_thread()->IsCurrent());
1219
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001220 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1222 // support video.
1223 LOG(LS_WARNING) << "Video not used in this call.";
1224 return false;
1225 }
1226 if (!video_channel_->SetCapturer(ssrc, camera)) {
1227 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1228 // This in the normal case when the underlying media channel has already
1229 // been deleted.
1230 ASSERT(camera == NULL);
1231 return false;
1232 }
1233 return true;
1234}
1235
nisse08582ff2016-02-04 01:24:52 -08001236void WebRtcSession::SetVideoPlayout(
1237 uint32_t ssrc,
1238 bool enable,
1239 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 ASSERT(signaling_thread()->IsCurrent());
1241 if (!video_channel_) {
1242 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1243 return;
1244 }
nisse08582ff2016-02-04 01:24:52 -08001245 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1246 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 // This in the normal case when the underlying media channel has already
1248 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001249 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
1251}
1252
Peter Boström0c4e06b2015-10-07 12:23:21 +02001253void WebRtcSession::SetVideoSend(uint32_t ssrc,
1254 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 const cricket::VideoOptions* options) {
1256 ASSERT(signaling_thread()->IsCurrent());
1257 if (!video_channel_) {
1258 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1259 return;
1260 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001261 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1263 // This in the normal case when the underlying media channel has already
1264 // been deleted.
1265 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267}
1268
1269bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1270 ASSERT(signaling_thread()->IsCurrent());
1271 if (!voice_channel_) {
1272 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1273 return false;
1274 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001275 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1277 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001278 if (!local_desc_ ||
1279 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 &send_ssrc)) {
1281 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1282 return false;
1283 }
1284 return voice_channel_->CanInsertDtmf();
1285}
1286
1287bool WebRtcSession::InsertDtmf(const std::string& track_id,
1288 int code, int duration) {
1289 ASSERT(signaling_thread()->IsCurrent());
1290 if (!voice_channel_) {
1291 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1292 return false;
1293 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001294 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001295 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1296 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1298 return false;
1299 }
solenberg1d63dd02015-12-02 12:35:09 -08001300 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1302 return false;
1303 }
1304 return true;
1305}
1306
1307sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001308 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
wu@webrtc.org78187522013-10-07 23:32:02 +00001311bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001312 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001313 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001314 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001315 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1316 return false;
1317 }
1318 return data_channel_->SendData(params, payload, result);
1319}
1320
1321bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001322 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001323 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1324 return false;
1325 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001326 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1327 &DataChannel::OnChannelReady);
1328 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1329 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001330 data_channel_->SignalStreamClosedRemotely.connect(
1331 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001332 return true;
1333}
1334
1335void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001336 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001337 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1338 return;
1339 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001340 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1341 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001342 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001343}
1344
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001345void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001346 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001347 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1348 return;
1349 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001350 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1351 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001352}
1353
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001354void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001355 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001356 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1357 << "NULL.";
1358 return;
1359 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001360 data_channel_->RemoveRecvStream(sid);
1361 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001362}
1363
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001364bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001365 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001366}
1367
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368cricket::DataChannelType WebRtcSession::data_channel_type() const {
1369 return data_channel_type_;
1370}
1371
deadbeef0ed85b22016-02-23 17:24:52 -08001372bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1373 return pending_ice_restarts_.find(content_name) !=
1374 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001375}
1376
Henrik Boströmd8281982015-08-27 10:12:24 +02001377void WebRtcSession::OnCertificateReady(
1378 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001379 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001380}
1381
Henrik Boströmd8281982015-08-27 10:12:24 +02001382bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001383 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001384}
1385
deadbeefcbecd352015-09-23 11:50:27 -07001386const rtc::scoped_refptr<rtc::RTCCertificate>&
1387WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001388 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001389}
1390
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391void WebRtcSession::SetIceConnectionState(
1392 PeerConnectionInterface::IceConnectionState state) {
1393 if (ice_connection_state_ == state) {
1394 return;
1395 }
1396
1397 // ASSERT that the requested transition is allowed. Note that
1398 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1399 // within PeerConnection). This switch statement should compile away when
1400 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001401 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1402 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 switch (ice_connection_state_) {
1404 case PeerConnectionInterface::kIceConnectionNew:
1405 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1406 break;
1407 case PeerConnectionInterface::kIceConnectionChecking:
1408 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1409 state == PeerConnectionInterface::kIceConnectionConnected);
1410 break;
1411 case PeerConnectionInterface::kIceConnectionConnected:
1412 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1413 state == PeerConnectionInterface::kIceConnectionChecking ||
1414 state == PeerConnectionInterface::kIceConnectionCompleted);
1415 break;
1416 case PeerConnectionInterface::kIceConnectionCompleted:
1417 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1418 state == PeerConnectionInterface::kIceConnectionDisconnected);
1419 break;
1420 case PeerConnectionInterface::kIceConnectionFailed:
1421 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1422 break;
1423 case PeerConnectionInterface::kIceConnectionDisconnected:
1424 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1425 state == PeerConnectionInterface::kIceConnectionConnected ||
1426 state == PeerConnectionInterface::kIceConnectionCompleted ||
1427 state == PeerConnectionInterface::kIceConnectionFailed);
1428 break;
1429 case PeerConnectionInterface::kIceConnectionClosed:
1430 ASSERT(false);
1431 break;
1432 default:
1433 ASSERT(false);
1434 break;
1435 }
1436
1437 ice_connection_state_ = state;
1438 if (ice_observer_) {
1439 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1440 }
1441}
1442
deadbeefcbecd352015-09-23 11:50:27 -07001443void WebRtcSession::OnTransportControllerConnectionState(
1444 cricket::IceConnectionState state) {
1445 switch (state) {
1446 case cricket::kIceConnectionConnecting:
1447 // If the current state is Connected or Completed, then there were
1448 // writable channels but now there are not, so the next state must
1449 // be Disconnected.
1450 // kIceConnectionConnecting is currently used as the default,
1451 // un-connected state by the TransportController, so its only use is
1452 // detecting disconnections.
1453 if (ice_connection_state_ ==
1454 PeerConnectionInterface::kIceConnectionConnected ||
1455 ice_connection_state_ ==
1456 PeerConnectionInterface::kIceConnectionCompleted) {
1457 SetIceConnectionState(
1458 PeerConnectionInterface::kIceConnectionDisconnected);
1459 }
torbjornga81a42f2015-09-23 02:16:58 -07001460 break;
deadbeefcbecd352015-09-23 11:50:27 -07001461 case cricket::kIceConnectionFailed:
1462 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1463 break;
1464 case cricket::kIceConnectionConnected:
1465 LOG(LS_INFO) << "Changing to ICE connected state because "
1466 << "all transports are writable.";
1467 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1468 break;
1469 case cricket::kIceConnectionCompleted:
1470 LOG(LS_INFO) << "Changing to ICE completed state because "
1471 << "all transports are complete.";
1472 if (ice_connection_state_ !=
1473 PeerConnectionInterface::kIceConnectionConnected) {
1474 // If jumping directly from "checking" to "connected",
1475 // signal "connected" first.
1476 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1477 }
1478 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1479 if (metrics_observer_) {
1480 ReportTransportStats();
1481 }
1482 break;
1483 default:
1484 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001485 }
deadbeefcbecd352015-09-23 11:50:27 -07001486}
1487
1488void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001489 SetIceConnectionReceiving(receiving);
1490}
1491
1492void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1493 if (ice_connection_receiving_ == receiving) {
1494 return;
1495 }
1496 ice_connection_receiving_ = receiving;
1497 if (ice_observer_) {
1498 ice_observer_->OnIceConnectionReceivingChange(receiving);
1499 }
1500}
1501
deadbeefcbecd352015-09-23 11:50:27 -07001502void WebRtcSession::OnTransportControllerCandidatesGathered(
1503 const std::string& transport_name,
1504 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001506 int sdp_mline_index;
1507 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1508 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1509 << transport_name << " not found";
1510 return;
1511 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512
deadbeefcbecd352015-09-23 11:50:27 -07001513 for (cricket::Candidates::const_iterator citer = candidates.begin();
1514 citer != candidates.end(); ++citer) {
1515 // Use transport_name as the candidate media id.
1516 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1517 if (ice_observer_) {
1518 ice_observer_->OnIceCandidate(&candidate);
1519 }
1520 if (local_desc_) {
1521 local_desc_->AddCandidate(&candidate);
1522 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 }
1524}
1525
1526// Enabling voice and video channel.
1527void WebRtcSession::EnableChannels() {
1528 if (voice_channel_ && !voice_channel_->enabled())
1529 voice_channel_->Enable(true);
1530
1531 if (video_channel_ && !video_channel_->enabled())
1532 video_channel_->Enable(true);
1533
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001534 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 data_channel_->Enable(true);
1536}
1537
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538// Returns the media index for a local ice candidate given the content name.
1539bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1540 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001541 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001543 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
1545 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001546 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 for (size_t index = 0; index < contents.size(); ++index) {
1548 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001549 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 content_found = true;
1551 break;
1552 }
1553 }
1554 return content_found;
1555}
1556
1557bool WebRtcSession::UseCandidatesInSessionDescription(
1558 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001559 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001563
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1565 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001566 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001567 const IceCandidateInterface* candidate = candidates->at(n);
1568 bool valid = false;
1569 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1570 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001571 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1572 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001573 }
1574 continue;
1575 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001576 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001577 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 break;
deadbeefd59daf82015-10-14 15:02:44 -07001579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 }
1581 }
1582 return ret;
1583}
1584
tommi6f59a4f2016-03-11 14:05:09 -08001585bool WebRtcSession::UseCandidate(
1586 const IceCandidateInterface* candidate) {
1587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001589 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 if (mediacontent_index >= remote_content_size) {
tommi6f59a4f2016-03-11 14:05:09 -08001591 LOG(LS_ERROR)
1592 << "UseRemoteCandidateInSession: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 return false;
1594 }
1595
1596 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001597 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 std::vector<cricket::Candidate> candidates;
1599 candidates.push_back(candidate->candidate());
1600 // Invoking BaseSession method to handle remote candidates.
1601 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001602 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1603 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 // Candidates successfully submitted for checking.
1605 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1606 ice_connection_state_ ==
1607 PeerConnectionInterface::kIceConnectionDisconnected) {
1608 // If state is New, then the session has just gotten its first remote ICE
1609 // candidates, so go to Checking.
1610 // If state is Disconnected, the session is re-using old candidates or
1611 // receiving additional ones, so go to Checking.
1612 // If state is Connected, stay Connected.
1613 // TODO(bemasc): If state is Connected, and the new candidates are for a
1614 // newly added transport, then the state actually _should_ move to
1615 // checking. Add a way to distinguish that case.
1616 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1617 }
1618 // TODO(bemasc): If state is Completed, go back to Connected.
1619 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001620 if (!error.empty()) {
1621 LOG(LS_WARNING) << error;
1622 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 }
1624 return true;
1625}
1626
deadbeefcbecd352015-09-23 11:50:27 -07001627void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001628 // Destroy video_channel_ first since it may have a pointer to the
1629 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 const cricket::ContentInfo* video_info =
1631 cricket::GetFirstVideoContent(desc);
1632 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 }
1636
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001637 const cricket::ContentInfo* voice_info =
1638 cricket::GetFirstAudioContent(desc);
1639 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001640 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001641 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001642 }
1643
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 const cricket::ContentInfo* data_info =
1645 cricket::GetFirstDataContent(desc);
1646 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 }
1650}
1651
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001652// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001653// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 // Creating the media channels and transport proxies.
1656 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1657 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001658 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 LOG(LS_ERROR) << "Failed to create voice channel.";
1660 return false;
1661 }
1662 }
1663
1664 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1665 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001666 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 LOG(LS_ERROR) << "Failed to create video channel.";
1668 return false;
1669 }
1670 }
1671
1672 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1673 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001674 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001675 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 LOG(LS_ERROR) << "Failed to create data channel.";
1677 return false;
1678 }
1679 }
1680
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001681 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1682 if (voice_channel()) {
1683 voice_channel()->ActivateRtcpMux();
1684 }
1685 if (video_channel()) {
1686 video_channel()->ActivateRtcpMux();
1687 }
1688 if (data_channel()) {
1689 data_channel()->ActivateRtcpMux();
1690 }
1691 }
1692
deadbeefcbecd352015-09-23 11:50:27 -07001693 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001694 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001695 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1696 cricket::GROUP_TYPE_BUNDLE);
1697 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001698 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1699 return false;
1700 }
deadbeefcbecd352015-09-23 11:50:27 -07001701 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001702 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1703 return false;
1704 }
1705 }
1706
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 return true;
1708}
1709
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001710bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001712 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001713 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001714 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001715 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001716 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001717
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001718 voice_channel_->SignalDtlsSetupFailure.connect(
1719 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001720
1721 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001722 voice_channel_->transport_channel()->SignalSentPacket.connect(
1723 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001724 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725}
1726
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001727bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001729 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001730 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001731 if (!video_channel_) {
1732 return false;
1733 }
1734
1735 video_channel_->SignalDtlsSetupFailure.connect(
1736 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001737
1738 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001739 video_channel_->transport_channel()->SignalSentPacket.connect(
1740 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001741 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742}
1743
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001744bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001745 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001747 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001748 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001749 return false;
1750 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001751
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001752 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001753 data_channel_->SignalDataReceived.connect(
1754 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001755 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001756
1757 data_channel_->SignalDtlsSetupFailure.connect(
1758 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001759
1760 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001761 data_channel_->transport_channel()->SignalSentPacket.connect(
1762 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001763 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764}
1765
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001766void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001767 SetError(ERROR_TRANSPORT,
1768 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769}
1770
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001771void WebRtcSession::OnDataChannelMessageReceived(
1772 cricket::DataChannel* channel,
1773 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001774 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001775 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1776 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1777 // Received OPEN message; parse and signal that a new data channel should
1778 // be created.
1779 std::string label;
1780 InternalDataChannelInit config;
1781 config.id = params.ssrc;
1782 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1783 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1784 << params.ssrc;
1785 return;
1786 }
1787 config.open_handshake_role = InternalDataChannelInit::kAcker;
1788 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 }
deadbeefab9b2d12015-10-14 11:33:11 -07001790 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001794bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001795 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1796 if (!bundle_enabled)
1797 return true;
1798
1799 const cricket::ContentGroup* bundle_group =
1800 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1801 ASSERT(bundle_group != NULL);
1802
1803 const cricket::ContentInfos& contents = desc->contents();
1804 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1805 citer != contents.end(); ++citer) {
1806 const cricket::ContentInfo* content = (&*citer);
1807 ASSERT(content != NULL);
1808 if (bundle_group->HasContentName(content->name) &&
1809 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1810 if (!HasRtcpMuxEnabled(content))
1811 return false;
1812 }
1813 }
1814 // RTCP-MUX is enabled in all the contents.
1815 return true;
1816}
1817
1818bool WebRtcSession::HasRtcpMuxEnabled(
1819 const cricket::ContentInfo* content) {
1820 const cricket::MediaContentDescription* description =
1821 static_cast<cricket::MediaContentDescription*>(content->description);
1822 return description->rtcp_mux();
1823}
1824
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001825bool WebRtcSession::ValidateSessionDescription(
1826 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001827 cricket::ContentSource source, std::string* err_desc) {
1828 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001829 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001830 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001831 }
1832
1833 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001834 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001835 }
1836
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001837 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001838 Action action = GetAction(sdesc->type());
1839 if (source == cricket::CS_LOCAL) {
1840 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001841 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001842 } else {
1843 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001844 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001845 }
1846
1847 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001848 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001849 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1850 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001851 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001852 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001853 }
1854
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001855 // Verify ice-ufrag and ice-pwd.
1856 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001857 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001858 }
1859
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001860 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001861 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001862 }
1863
1864 // Verify m-lines in Answer when compared against Offer.
1865 if (action == kAnswer) {
1866 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001867 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1868 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001869 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001870 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001871 }
1872 }
1873
1874 return true;
1875}
1876
1877bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1878 return ((action == kOffer && state() == STATE_INIT) ||
1879 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001880 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001881 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001882 (action == kOffer && state() == STATE_INPROGRESS) ||
1883 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001884 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1885 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1886 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1887 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001888}
1889
1890bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1891 return ((action == kOffer && state() == STATE_INIT) ||
1892 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001893 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001894 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001895 (action == kOffer && state() == STATE_INPROGRESS) ||
1896 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001897 (action == kAnswer && state() == STATE_SENTOFFER) ||
1898 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1899 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1900 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001901}
1902
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001903std::string WebRtcSession::GetSessionErrorMsg() {
1904 std::ostringstream desc;
1905 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1906 desc << kSessionErrorDesc << error_desc() << ".";
1907 return desc.str();
1908}
1909
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001910// We need to check the local/remote description for the Transport instead of
1911// the session, because a new Transport added during renegotiation may have
1912// them unset while the session has them set from the previous negotiation.
1913// Not doing so may trigger the auto generation of transport description and
1914// mess up DTLS identity information, ICE credential, etc.
1915bool WebRtcSession::ReadyToUseRemoteCandidate(
1916 const IceCandidateInterface* candidate,
1917 const SessionDescriptionInterface* remote_desc,
1918 bool* valid) {
1919 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001920
1921 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001922 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001923
deadbeefd59daf82015-10-14 15:02:44 -07001924 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001925 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001926 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001927
1928 size_t mediacontent_index =
1929 static_cast<size_t>(candidate->sdp_mline_index());
1930 size_t remote_content_size =
1931 current_remote_desc->description()->contents().size();
1932 if (mediacontent_index >= remote_content_size) {
tommi6f59a4f2016-03-11 14:05:09 -08001933 LOG(LS_ERROR)
1934 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001935
1936 *valid = false;
1937 return false;
1938 }
1939
1940 cricket::ContentInfo content =
1941 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001942 cricket::BaseChannel* channel = GetChannel(content.name);
1943 if (!channel) {
1944 return false;
1945 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001946
deadbeefd59daf82015-10-14 15:02:44 -07001947 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001948 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001949}
1950
deadbeefcbecd352015-09-23 11:50:27 -07001951void WebRtcSession::OnTransportControllerGatheringState(
1952 cricket::IceGatheringState state) {
1953 ASSERT(signaling_thread()->IsCurrent());
1954 if (state == cricket::kIceGatheringGathering) {
1955 if (ice_observer_) {
1956 ice_observer_->OnIceGatheringChange(
1957 PeerConnectionInterface::kIceGatheringGathering);
1958 }
1959 } else if (state == cricket::kIceGatheringComplete) {
1960 if (ice_observer_) {
1961 ice_observer_->OnIceGatheringChange(
1962 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001963 }
1964 }
1965}
1966
1967void WebRtcSession::ReportTransportStats() {
1968 // Use a set so we don't report the same stats twice if two channels share
1969 // a transport.
1970 std::set<std::string> transport_names;
1971 if (voice_channel()) {
1972 transport_names.insert(voice_channel()->transport_name());
1973 }
1974 if (video_channel()) {
1975 transport_names.insert(video_channel()->transport_name());
1976 }
1977 if (data_channel()) {
1978 transport_names.insert(data_channel()->transport_name());
1979 }
1980 for (const auto& name : transport_names) {
1981 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07001982 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001983 ReportBestConnectionState(stats);
1984 ReportNegotiatedCiphers(stats);
1985 }
1986 }
1987}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001988// Walk through the ConnectionInfos to gather best connection usage
1989// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07001990void WebRtcSession::ReportBestConnectionState(
1991 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07001992 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001993 for (cricket::TransportChannelStatsList::const_iterator it =
1994 stats.channel_stats.begin();
1995 it != stats.channel_stats.end(); ++it) {
1996 for (cricket::ConnectionInfos::const_iterator it_info =
1997 it->connection_infos.begin();
1998 it_info != it->connection_infos.end(); ++it_info) {
1999 if (!it_info->best_connection) {
2000 continue;
2001 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002002
2003 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2004 const cricket::Candidate& local = it_info->local_candidate;
2005 const cricket::Candidate& remote = it_info->remote_candidate;
2006
2007 // Increment the counter for IceCandidatePairType.
2008 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2009 (local.type() == RELAY_PORT_TYPE &&
2010 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2011 type = kEnumCounterIceCandidatePairTypeTcp;
2012 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2013 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002014 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002015 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002016 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002017 metrics_observer_->IncrementEnumCounter(
2018 type, GetIceCandidatePairCounter(local, remote),
2019 kIceCandidatePairMax);
2020
2021 // Increment the counter for IP type.
2022 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002023 metrics_observer_->IncrementEnumCounter(
2024 kEnumCounterAddressFamily, kBestConnections_IPv4,
2025 kPeerConnectionAddressFamilyCounter_Max);
2026
2027 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002028 metrics_observer_->IncrementEnumCounter(
2029 kEnumCounterAddressFamily, kBestConnections_IPv6,
2030 kPeerConnectionAddressFamilyCounter_Max);
2031 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002032 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002033 }
2034
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002035 return;
2036 }
2037 }
2038}
2039
jbauchac8869e2015-07-03 01:36:14 -07002040void WebRtcSession::ReportNegotiatedCiphers(
2041 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002042 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002043 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2044 return;
2045 }
2046
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002047 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2048 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2049 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2050 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002051 return;
2052 }
2053
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002054 PeerConnectionEnumCounterType srtp_counter_type;
2055 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002056 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002057 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2058 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002059 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002060 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2061 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002062 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002063 srtp_counter_type = kEnumCounterDataSrtpCipher;
2064 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002065 } else {
2066 RTC_NOTREACHED();
2067 return;
2068 }
2069
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002070 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2071 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2072 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002073 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002074 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2075 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2076 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002077 }
2078}
2079
stefanc1aeaf02015-10-15 07:26:07 -07002080void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2081 const rtc::SentPacket& sent_packet) {
2082 RTC_DCHECK(worker_thread()->IsCurrent());
2083 media_controller_->call_w()->OnSentPacket(sent_packet);
2084}
2085
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086} // namespace webrtc