blob: 6bfa8a5eb6bcc5221050ae9b7bc962d1674cc2d8 [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"
22#include "webrtc/api/mediaconstraintsinterface.h"
23#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010026#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/constants.h"
35#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::ContentInfo;
43using cricket::ContentInfos;
44using cricket::MediaContentDescription;
45using cricket::SessionDescription;
46using cricket::TransportInfo;
47
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070048using cricket::LOCAL_PORT_TYPE;
49using cricket::STUN_PORT_TYPE;
50using cricket::RELAY_PORT_TYPE;
51using cricket::PRFLX_PORT_TYPE;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053namespace webrtc {
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000056const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
61const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
63const char kPushDownTDFailed[] =
64 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000065const char kSdpWithoutDtlsFingerprint[] =
66 "Called with SDP without DTLS fingerprint.";
67const char kSdpWithoutSdesCrypto[] =
68 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000073const char kDtlsSetupFailureRtp[] =
74 "Couldn't set up DTLS-SRTP on RTP channel.";
75const char kDtlsSetupFailureRtcp[] =
76 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070077const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070079IceCandidatePairType GetIceCandidatePairCounter(
80 const cricket::Candidate& local,
81 const cricket::Candidate& remote) {
82 const auto& l = local.type();
83 const auto& r = remote.type();
84 const auto& host = LOCAL_PORT_TYPE;
85 const auto& srflx = STUN_PORT_TYPE;
86 const auto& relay = RELAY_PORT_TYPE;
87 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070088 if (l == host && r == host) {
89 bool local_private = IPIsPrivate(local.address().ipaddr());
90 bool remote_private = IPIsPrivate(remote.address().ipaddr());
91 if (local_private) {
92 if (remote_private) {
93 return kIceCandidatePairHostPrivateHostPrivate;
94 } else {
95 return kIceCandidatePairHostPrivateHostPublic;
96 }
97 } else {
98 if (remote_private) {
99 return kIceCandidatePairHostPublicHostPrivate;
100 } else {
101 return kIceCandidatePairHostPublicHostPublic;
102 }
103 }
104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700105 if (l == host && r == srflx)
106 return kIceCandidatePairHostSrflx;
107 if (l == host && r == relay)
108 return kIceCandidatePairHostRelay;
109 if (l == host && r == prflx)
110 return kIceCandidatePairHostPrflx;
111 if (l == srflx && r == host)
112 return kIceCandidatePairSrflxHost;
113 if (l == srflx && r == srflx)
114 return kIceCandidatePairSrflxSrflx;
115 if (l == srflx && r == relay)
116 return kIceCandidatePairSrflxRelay;
117 if (l == srflx && r == prflx)
118 return kIceCandidatePairSrflxPrflx;
119 if (l == relay && r == host)
120 return kIceCandidatePairRelayHost;
121 if (l == relay && r == srflx)
122 return kIceCandidatePairRelaySrflx;
123 if (l == relay && r == relay)
124 return kIceCandidatePairRelayRelay;
125 if (l == relay && r == prflx)
126 return kIceCandidatePairRelayPrflx;
127 if (l == prflx && r == host)
128 return kIceCandidatePairPrflxHost;
129 if (l == prflx && r == srflx)
130 return kIceCandidatePairPrflxSrflx;
131 if (l == prflx && r == relay)
132 return kIceCandidatePairPrflxRelay;
133 return kIceCandidatePairMax;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Compares |answer| against |offer|. Comparision is done
137// for number of m-lines in answer against offer. If matches true will be
138// returned otherwise false.
139static bool VerifyMediaDescriptions(
140 const SessionDescription* answer, const SessionDescription* offer) {
141 if (offer->contents().size() != answer->contents().size())
142 return false;
143
144 for (size_t i = 0; i < offer->contents().size(); ++i) {
145 if ((offer->contents()[i].name) != answer->contents()[i].name) {
146 return false;
147 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000148 const MediaContentDescription* offer_mdesc =
149 static_cast<const MediaContentDescription*>(
150 offer->contents()[i].description);
151 const MediaContentDescription* answer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 answer->contents()[i].description);
154 if (offer_mdesc->type() != answer_mdesc->type()) {
155 return false;
156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159}
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// Checks that each non-rejected content has SDES crypto keys or a DTLS
162// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
163// keys, will be caught in Transport negotiation, and backstopped by Channel's
164// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000165static bool VerifyCrypto(const SessionDescription* desc,
166 bool dtls_enabled,
167 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 const ContentInfos& contents = desc->contents();
169 for (size_t index = 0; index < contents.size(); ++index) {
170 const ContentInfo* cinfo = &contents[index];
171 if (cinfo->rejected) {
172 continue;
173 }
174
175 // If the content isn't rejected, crypto must be present.
176 const MediaContentDescription* media =
177 static_cast<const MediaContentDescription*>(cinfo->description);
178 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
179 if (!media || !tinfo) {
180 // Something is not right.
181 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return false;
184 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000185 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000187 LOG(LS_WARNING) <<
188 "Session description must have DTLS fingerprint if DTLS enabled.";
189 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 return false;
191 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 } else {
193 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 LOG(LS_WARNING) <<
195 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 return false;
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
200 }
201
202 return true;
203}
204
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000205// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
206static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
207 const ContentInfos& contents = desc->contents();
208 for (size_t index = 0; index < contents.size(); ++index) {
209 const ContentInfo* cinfo = &contents[index];
210 if (cinfo->rejected) {
211 continue;
212 }
213
214 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
215 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
216 if (!tinfo) {
217 // Something is not right.
218 LOG(LS_ERROR) << kInvalidSdp;
219 return false;
220 }
221 if (tinfo->description.ice_ufrag.empty() ||
222 tinfo->description.ice_pwd.empty()) {
223 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
224 return false;
225 }
226 }
227 return true;
228}
229
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230// Forces |sdesc->crypto_required| to the appropriate state based on the
231// current security policy, to ensure a failure occurs if there is an error
232// in crypto negotiation.
233// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000234static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
235 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 if (!sdesc) {
237 return;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 // Updating the |crypto_required_| in MediaContentDescription to the
241 // appropriate state based on the current security policy.
242 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
243 iter != sdesc->contents().end(); ++iter) {
244 if (cricket::IsMediaContent(&*iter)) {
245 MediaContentDescription* mdesc =
246 static_cast<MediaContentDescription*> (iter->description);
247 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 }
250 }
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
255 const std::string& track_id,
256 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 const cricket::ContentInfo* audio_info =
258 cricket::GetFirstAudioContent(session_description);
259 if (!audio_info) {
260 LOG(LS_ERROR) << "Audio not used in this call";
261 return false;
262 }
263
264 const cricket::MediaContentDescription* audio_content =
265 static_cast<const cricket::MediaContentDescription*>(
266 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000267 const cricket::StreamParams* stream =
268 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
269 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return false;
271 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272
273 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return true;
275}
276
277static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint32_t ssrc,
279 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 ASSERT(track_id != NULL);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 const cricket::ContentInfo* audio_info =
283 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000284 if (audio_info) {
285 const cricket::MediaContentDescription* audio_content =
286 static_cast<const cricket::MediaContentDescription*>(
287 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289 const auto* found =
290 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
291 if (found) {
292 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000293 return true;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
297 const cricket::ContentInfo* video_info =
298 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (video_info) {
300 const cricket::MediaContentDescription* video_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311 return false;
312}
313
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314static bool BadSdp(const std::string& source,
315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
318 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700319 desc << "Failed to set " << source;
320 if (!type.empty()) {
321 desc << " " << type;
322 }
323 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000326 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 return false;
330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 }
341}
342
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343static bool BadLocalSdp(const std::string& type,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
347}
348
349static bool BadRemoteSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
353}
354
355static bool BadOfferSdp(cricket::ContentSource source,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
359}
360
361static bool BadPranswerSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
365 reason, err_desc);
366}
367
368static bool BadAnswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
deadbeefd59daf82015-10-14 15:02:44 -0700374#define GET_STRING_OF_STATE(state) \
375 case webrtc::WebRtcSession::state: \
376 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 break;
378
deadbeefd59daf82015-10-14 15:02:44 -0700379static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 std::string result;
381 switch (state) {
382 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700383 GET_STRING_OF_STATE(STATE_SENTOFFER)
384 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
385 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
386 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
390 ASSERT(false);
391 break;
392 }
393 return result;
394}
395
deadbeefd59daf82015-10-14 15:02:44 -0700396#define GET_STRING_OF_ERROR_CODE(err) \
397 case webrtc::WebRtcSession::err: \
398 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 break;
400
deadbeefd59daf82015-10-14 15:02:44 -0700401static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 std::string result;
403 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
406 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
deadbeefd59daf82015-10-14 15:02:44 -0700408 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414static std::string MakeErrorString(const std::string& error,
415 const std::string& desc) {
416 std::ostringstream ret;
417 ret << error << " " << desc;
418 return ret.str();
419}
420
421static std::string MakeTdErrorString(const std::string& desc) {
422 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000425// Set |option| to the highest-priority value of |key| in the optional
426// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700427template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000428static void SetOptionFromOptionalConstraint(
429 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700430 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100431 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000432 if (!constraints) {
433 return;
434 }
435 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000436 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000437 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100439 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440 }
441 }
442}
443
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000445 PeerConnectionInterface::IceTransportsType type) {
446 switch (type) {
447 case PeerConnectionInterface::kNone:
448 return cricket::CF_NONE;
449 case PeerConnectionInterface::kRelay:
450 return cricket::CF_RELAY;
451 case PeerConnectionInterface::kNoHost:
452 return (cricket::CF_ALL & ~cricket::CF_HOST);
453 case PeerConnectionInterface::kAll:
454 return cricket::CF_ALL;
455 default: ASSERT(false);
456 }
457 return cricket::CF_NONE;
458}
459
deadbeef0ed85b22016-02-23 17:24:52 -0800460// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
461bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
462 const SessionDescriptionInterface* new_desc,
463 const std::string& content_name) {
464 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700465 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
deadbeef0ed85b22016-02-23 17:24:52 -0800467 const SessionDescription* new_sd = new_desc->description();
468 const SessionDescription* old_sd = old_desc->description();
469 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
470 if (!cinfo || cinfo->rejected) {
471 return false;
472 }
473 // If the content isn't rejected, check if ufrag and password has changed.
474 const cricket::TransportDescription* new_transport_desc =
475 new_sd->GetTransportDescriptionByName(content_name);
476 const cricket::TransportDescription* old_transport_desc =
477 old_sd->GetTransportDescriptionByName(content_name);
478 if (!new_transport_desc || !old_transport_desc) {
479 // No transport description exists. This is not an ICE restart.
480 return false;
481 }
482 if (cricket::IceCredentialsChanged(
483 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
484 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
485 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
486 << ".";
487 return true;
488 }
489 return false;
490}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
stefanc1aeaf02015-10-15 07:26:07 -0700492WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700493 rtc::Thread* signaling_thread,
494 rtc::Thread* worker_thread,
495 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700496 : signaling_thread_(signaling_thread),
497 worker_thread_(worker_thread),
498 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 // RFC 3264: The numeric value of the session id and version in the
500 // o line MUST be representable with a "64 bit signed integer".
501 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700502 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
503 transport_controller_(new cricket::TransportController(signaling_thread,
504 worker_thread,
505 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700506 media_controller_(media_controller),
507 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 ice_observer_(NULL),
509 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700510 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000512 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000514 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700515 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
516 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700517 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700518 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700519 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700520 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700521 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700522 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700523 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524}
525
526WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700527 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000528 // Destroy video_channel_ first since it may have a pointer to the
529 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000530 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 SignalVideoChannelDestroyed();
532 channel_manager_->DestroyVideoChannel(video_channel_.release());
533 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000534 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000535 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200536 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000537 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000538 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 SignalDataChannelDestroyed();
540 channel_manager_->DestroyDataChannel(data_channel_.release());
541 }
deadbeef057ecf02016-01-20 14:30:43 -0800542 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700543
544 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000548 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700549 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200550 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200551 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
552 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700553 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700554 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000555
Henrik Boström87713d02015-08-25 09:53:21 +0200556 // Obtain a certificate from RTCConfiguration if any were provided (optional).
557 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
558 if (!rtc_configuration.certificates.empty()) {
559 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
560 // just picking the first one. The decision should be made based on the DTLS
561 // handshake. The DTLS negotiations need to know about all certificates.
562 certificate = rtc_configuration.certificates[0];
563 }
564
honghaiz1f429e32015-09-28 07:57:34 -0700565 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700566
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 // TODO(perkj): Take |constraints| into consideration. Return false if not all
568 // mandatory constraints can be fulfilled. Note that |constraints|
569 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000571
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000572 if (options.disable_encryption) {
573 dtls_enabled_ = false;
574 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200575 // Enable DTLS by default if we have an identity store or a certificate.
576 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000577 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700578 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
579 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000580 dtls_enabled_ = value;
581 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000582 }
583
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000585 // It takes precendence over the disable_sctp_data_channels
586 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 if (FindConstraint(
588 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
589 &value, NULL) && value) {
590 LOG(LS_INFO) << "Allowing RTP data engine.";
591 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000592 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000593 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000594 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000595 LOG(LS_INFO) << "Allowing SCTP data engine.";
596 data_channel_type_ = cricket::DCT_SCTP;
597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000600 SetOptionFromOptionalConstraint(constraints,
601 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800602 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000603
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000604 SetOptionFromOptionalConstraint(constraints,
605 MediaConstraintsInterface::kCombinedAudioVideoBwe,
606 &audio_options_.combined_audio_video_bwe);
607
kwiberg102c6a62015-10-30 02:47:38 -0700608 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100609 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200610
Karl Wibergbe579832015-11-10 22:34:18 +0100611 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
612 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200613
Henrik Boström87713d02015-08-25 09:53:21 +0200614 if (!dtls_enabled_) {
615 // Construct with DTLS disabled.
616 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700617 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200618 } else {
619 // Construct with DTLS enabled.
620 if (!certificate) {
621 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700622 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200623 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800624 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700625 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200626 } else {
627 // Use the already generated certificate.
628 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700629 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200630 }
631 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632
Henrik Boströmd8281982015-08-27 10:12:24 +0200633 webrtc_session_desc_factory_->SignalCertificateReady.connect(
634 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000635
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000637 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000638 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000639 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200640 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 return true;
643}
644
deadbeefd59daf82015-10-14 15:02:44 -0700645void WebRtcSession::Close() {
646 SetState(STATE_CLOSED);
647 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000648 ASSERT(!voice_channel_);
649 ASSERT(!video_channel_);
650 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651}
652
deadbeef0ed85b22016-02-23 17:24:52 -0800653cricket::BaseChannel* WebRtcSession::GetChannel(
654 const std::string& content_name) {
655 if (voice_channel() && voice_channel()->content_name() == content_name) {
656 return voice_channel();
657 }
658 if (video_channel() && video_channel()->content_name() == content_name) {
659 return video_channel();
660 }
661 if (data_channel() && data_channel()->content_name() == content_name) {
662 return data_channel();
663 }
664 return nullptr;
665}
666
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000667void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
668 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669}
670
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000671cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
672 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673}
674
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800675bool WebRtcSession::GetSslRole(const std::string& transport_name,
676 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700677 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000678 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
679 << "SSL Role of the session.";
680 return false;
681 }
682
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800683 return transport_controller_->GetSslRole(transport_name, role);
684}
685
686bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
687 rtc::SSLRole* role) {
688 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000689}
690
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000691void WebRtcSession::CreateOffer(
692 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700693 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
694 const cricket::MediaSessionOptions& session_options) {
695 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000696}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697
deadbeefab9b2d12015-10-14 11:33:11 -0700698void WebRtcSession::CreateAnswer(
699 CreateSessionDescriptionObserver* observer,
700 const MediaConstraintsInterface* constraints,
701 const cricket::MediaSessionOptions& session_options) {
702 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
703 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704}
705
706bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
707 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700708 ASSERT(signaling_thread()->IsCurrent());
709
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000710 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000711 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000712
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000713 // Validate SDP.
714 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
715 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 }
717
deadbeefd59daf82015-10-14 15:02:44 -0700718 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000719 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700721 initial_offerer_ = true;
722 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000725 cricket::SecurePolicy sdes_policy =
726 webrtc_session_desc_factory_->SdesPolicy();
727 cricket::CryptoType crypto_required = dtls_enabled_ ?
728 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
729 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000731 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000733 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734
735 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000736 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 // TODO(mallinath) - Handle CreateChannel failure, as new local description
738 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000739 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 }
741
deadbeefcbecd352015-09-23 11:50:27 -0700742 // Remove unused channels if MediaContentDescription is rejected.
743 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000745 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 return false;
747 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000748
deadbeefd59daf82015-10-14 15:02:44 -0700749 if (remote_desc_) {
750 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700751 UseCandidatesInSessionDescription(remote_desc_.get());
752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
deadbeef0ed85b22016-02-23 17:24:52 -0800754 pending_ice_restarts_.clear();
755
deadbeefd59daf82015-10-14 15:02:44 -0700756 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000757 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759 return true;
760}
761
762bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
763 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700764 ASSERT(signaling_thread()->IsCurrent());
765
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000766 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000767 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000768
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000769 // Validate SDP.
770 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
771 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 }
773
deadbeefd59daf82015-10-14 15:02:44 -0700774 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
775 remote_desc_.release());
776 remote_desc_.reset(desc_temp.release());
777
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000779 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 if (action == kOffer && !CreateChannels(desc->description())) {
781 // TODO(mallinath) - Handle CreateChannel failure, as new local description
782 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000783 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 }
785
deadbeefcbecd352015-09-23 11:50:27 -0700786 // Remove unused channels if MediaContentDescription is rejected.
787 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788
789 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
790 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000791 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 return false;
793 }
794
deadbeefd59daf82015-10-14 15:02:44 -0700795 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000796 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 }
798
deadbeef0ed85b22016-02-23 17:24:52 -0800799 if (old_remote_desc) {
800 for (const cricket::ContentInfo& content :
801 old_remote_desc->description()->contents()) {
802 // Check if this new SessionDescription contains new ICE ufrag and
803 // password that indicates the remote peer requests an ICE restart.
804 // TODO(deadbeef): When we start storing both the current and pending
805 // remote description, this should reset pending_ice_restarts and compare
806 // against the current description.
807 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
808 if (action == kOffer) {
809 pending_ice_restarts_.insert(content.name);
810 }
811 } else {
812 // We retain all received candidates only if ICE is not restarted.
813 // When ICE is restarted, all previous candidates belong to an old
814 // generation and should not be kept.
815 // TODO(deadbeef): This goes against the W3C spec which says the remote
816 // description should only contain candidates from the last set remote
817 // description plus any candidates added since then. We should remove
818 // this once we're sure it won't break anything.
819 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
820 old_remote_desc.get(), content.name, desc);
821 }
822 }
honghaiz503726c2015-07-31 12:37:38 -0700823 }
824
deadbeefd59daf82015-10-14 15:02:44 -0700825 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000826 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000828
829 // Set the the ICE connection state to connecting since the connection may
830 // become writable with peer reflexive candidates before any remote candidate
831 // is signaled.
832 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
833 // is to have a new signal the indicates a change in checking state from the
834 // transport and expose a new checking() member from transport that can be
835 // read to determine the current checking state. The existing SignalConnecting
836 // actually means "gathering candidates", so cannot be be used here.
837 if (desc->type() != SessionDescriptionInterface::kOffer &&
838 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
839 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
840 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return true;
842}
843
deadbeefd59daf82015-10-14 15:02:44 -0700844void WebRtcSession::LogState(State old_state, State new_state) {
845 LOG(LS_INFO) << "Session:" << id()
846 << " Old state:" << GetStateString(old_state)
847 << " New state:" << GetStateString(new_state);
848}
849
850void WebRtcSession::SetState(State state) {
851 ASSERT(signaling_thread_->IsCurrent());
852 if (state != state_) {
853 LogState(state_, state);
854 state_ = state;
855 SignalState(this, state_);
856 }
857}
858
859void WebRtcSession::SetError(Error error, const std::string& error_desc) {
860 ASSERT(signaling_thread_->IsCurrent());
861 if (error != error_) {
862 error_ = error;
863 error_desc_ = error_desc;
864 }
865}
866
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867bool WebRtcSession::UpdateSessionState(
868 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700870 ASSERT(signaling_thread()->IsCurrent());
871
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 // If there's already a pending error then no state transition should happen.
873 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700874 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000875 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000877 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
878 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
deadbeefd59daf82015-10-14 15:02:44 -0700880 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
881 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000882 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700883 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000884 }
deadbeefd59daf82015-10-14 15:02:44 -0700885 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000886 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
888 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000889 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
890 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700893 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
894 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000895 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700896 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000897 }
deadbeefd59daf82015-10-14 15:02:44 -0700898 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000899 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700902 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700903 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700904 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700905 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700906 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800907 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700908 const cricket::ContentGroup* answer_bundle =
909 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
910 if (!EnableBundle(*answer_bundle)) {
911 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
912 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
913 }
914 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800915 // Only push down the transport description after enabling BUNDLE; we don't
916 // want to push down a description on a transport about to be destroyed.
917 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
918 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
919 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700921 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000922 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700923 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000924 }
deadbeefd59daf82015-10-14 15:02:44 -0700925 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000926 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928 }
929 return true;
930}
931
932WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
933 if (type == SessionDescriptionInterface::kOffer) {
934 return WebRtcSession::kOffer;
935 } else if (type == SessionDescriptionInterface::kPrAnswer) {
936 return WebRtcSession::kPrAnswer;
937 } else if (type == SessionDescriptionInterface::kAnswer) {
938 return WebRtcSession::kAnswer;
939 }
940 ASSERT(false && "unknown action type");
941 return WebRtcSession::kOffer;
942}
943
deadbeefd59daf82015-10-14 15:02:44 -0700944bool WebRtcSession::PushdownMediaDescription(
945 cricket::ContentAction action,
946 cricket::ContentSource source,
947 std::string* err) {
948 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
949 if (!ch) {
950 return true;
951 } else if (source == cricket::CS_LOCAL) {
952 return ch->PushdownLocalDescription(local_desc_->description(), action,
953 err);
954 } else {
955 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
956 err);
957 }
958 };
959
960 return (set_content(voice_channel()) &&
961 set_content(video_channel()) &&
962 set_content(data_channel()));
963}
964
965bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
966 cricket::ContentAction action,
967 std::string* error_desc) {
968 RTC_DCHECK(signaling_thread()->IsCurrent());
969
970 if (source == cricket::CS_LOCAL) {
971 return PushdownLocalTransportDescription(local_desc_->description(), action,
972 error_desc);
973 }
974 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
975 error_desc);
976}
977
978bool WebRtcSession::PushdownLocalTransportDescription(
979 const SessionDescription* sdesc,
980 cricket::ContentAction action,
981 std::string* err) {
982 RTC_DCHECK(signaling_thread()->IsCurrent());
983
984 if (!sdesc) {
985 return false;
986 }
987
988 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
989 if (!transport_controller_->SetLocalTransportDescription(
990 tinfo.content_name, tinfo.description, action, err)) {
991 return false;
992 }
993 }
994
995 return true;
996}
997
998bool WebRtcSession::PushdownRemoteTransportDescription(
999 const SessionDescription* sdesc,
1000 cricket::ContentAction action,
1001 std::string* err) {
1002 RTC_DCHECK(signaling_thread()->IsCurrent());
1003
1004 if (!sdesc) {
1005 return false;
1006 }
1007
1008 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1009 if (!transport_controller_->SetRemoteTransportDescription(
1010 tinfo.content_name, tinfo.description, action, err)) {
1011 return false;
1012 }
1013 }
1014
1015 return true;
1016}
1017
1018bool WebRtcSession::GetTransportDescription(
1019 const SessionDescription* description,
1020 const std::string& content_name,
1021 cricket::TransportDescription* tdesc) {
1022 if (!description || !tdesc) {
1023 return false;
1024 }
1025 const TransportInfo* transport_info =
1026 description->GetTransportInfoByName(content_name);
1027 if (!transport_info) {
1028 return false;
1029 }
1030 *tdesc = transport_info->description;
1031 return true;
1032}
1033
1034bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001035 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001036 return (GetChannelTransportStats(voice_channel(), stats) &&
1037 GetChannelTransportStats(video_channel(), stats) &&
1038 GetChannelTransportStats(data_channel(), stats));
1039}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001040
deadbeefcbecd352015-09-23 11:50:27 -07001041bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001042 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001043 ASSERT(signaling_thread()->IsCurrent());
1044 if (!ch) {
1045 // Not using this channel.
1046 return true;
1047 }
1048
1049 const std::string& content_name = ch->content_name();
1050 const std::string& transport_name = ch->transport_name();
1051 stats->proxy_to_transport[content_name] = transport_name;
1052 if (stats->transport_stats.find(transport_name) !=
1053 stats->transport_stats.end()) {
1054 // Transport stats already done for this transport.
1055 return true;
1056 }
1057
1058 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001059 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001060 return false;
1061 }
1062
1063 stats->transport_stats[transport_name] = tstats;
1064 return true;
1065}
1066
1067bool WebRtcSession::GetLocalCertificate(
1068 const std::string& transport_name,
1069 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1070 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001071 return transport_controller_->GetLocalCertificate(transport_name,
1072 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001073}
1074
1075bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1076 rtc::SSLCertificate** cert) {
1077 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001078 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001079}
1080
deadbeefcbecd352015-09-23 11:50:27 -07001081bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1082 const std::string* first_content_name = bundle.FirstContentName();
1083 if (!first_content_name) {
1084 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1085 return false;
1086 }
1087 const std::string& transport_name = *first_content_name;
1088 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1089
1090 auto maybe_set_transport = [this, bundle, transport_name,
1091 first_channel](cricket::BaseChannel* ch) {
1092 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001093 return true;
1094 }
1095
deadbeefcbecd352015-09-23 11:50:27 -07001096 if (ch->transport_name() == transport_name) {
1097 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1098 << " on " << transport_name << ".";
1099 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001100 }
torbjornga81a42f2015-09-23 02:16:58 -07001101
deadbeefcbecd352015-09-23 11:50:27 -07001102 if (!ch->SetTransport(transport_name)) {
1103 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1104 return false;
1105 }
1106 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1107 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001108 return true;
1109 };
1110
deadbeefcbecd352015-09-23 11:50:27 -07001111 if (!maybe_set_transport(voice_channel()) ||
1112 !maybe_set_transport(video_channel()) ||
1113 !maybe_set_transport(data_channel())) {
1114 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001115 }
deadbeefcbecd352015-09-23 11:50:27 -07001116
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001117 return true;
1118}
1119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001121 if (!remote_desc_) {
1122 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1123 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 return false;
1125 }
1126
1127 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001128 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 return false;
1130 }
1131
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001132 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001133 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1134 if (!valid) {
1135 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 }
1137
1138 // Add this candidate to the remote session description.
1139 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001140 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 return false;
1142 }
1143
deadbeefd59daf82015-10-14 15:02:44 -07001144 if (ready) {
1145 return UseCandidate(candidate);
1146 } else {
1147 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1148 return true;
1149 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150}
1151
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001152bool WebRtcSession::SetIceTransports(
1153 PeerConnectionInterface::IceTransportsType type) {
1154 return port_allocator()->set_candidate_filter(
1155 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001156}
1157
deadbeefd59daf82015-10-14 15:02:44 -07001158cricket::IceConfig WebRtcSession::ParseIceConfig(
1159 const PeerConnectionInterface::RTCConfiguration& config) const {
1160 cricket::IceConfig ice_config;
1161 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001162 ice_config.backup_connection_ping_interval =
1163 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001164 ice_config.gather_continually = (config.continual_gathering_policy ==
1165 PeerConnectionInterface::GATHER_CONTINUALLY);
1166 return ice_config;
1167}
1168
1169void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1170 transport_controller_->SetIceConfig(config);
1171}
1172
1173void WebRtcSession::MaybeStartGathering() {
1174 transport_controller_->MaybeStartGathering();
1175}
1176
Peter Boström0c4e06b2015-10-07 12:23:21 +02001177bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1178 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001179 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001181 }
1182 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183}
1184
Peter Boström0c4e06b2015-10-07 12:23:21 +02001185bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1186 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001187 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001188 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001189 }
1190 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191}
1192
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 return desc.str();
1197}
1198
solenbergd4cec0d2015-10-09 08:55:48 -07001199void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 ASSERT(signaling_thread()->IsCurrent());
1201 if (!voice_channel_) {
1202 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1203 return;
1204 }
solenberg4bac9c52015-10-09 02:32:53 -07001205 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1206 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 // otherwise. This in the normal case when the underlying media channel has
1208 // already been deleted.
1209 ASSERT(enable == false);
1210 }
1211}
1212
Peter Boström0c4e06b2015-10-07 12:23:21 +02001213void WebRtcSession::SetAudioSend(uint32_t ssrc,
1214 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001215 const cricket::AudioOptions& options,
1216 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 ASSERT(signaling_thread()->IsCurrent());
1218 if (!voice_channel_) {
1219 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1220 return;
1221 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001222 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001223 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225}
1226
Peter Boström0c4e06b2015-10-07 12:23:21 +02001227void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001228 ASSERT(signaling_thread()->IsCurrent());
1229 ASSERT(volume >= 0 && volume <= 10);
1230 if (!voice_channel_) {
1231 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1232 return;
1233 }
1234
solenberg4bac9c52015-10-09 02:32:53 -07001235 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001236 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001237 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001238}
1239
deadbeef2d110be2016-01-13 12:00:26 -08001240void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1241 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001242 ASSERT(signaling_thread()->IsCurrent());
1243 if (!voice_channel_)
1244 return;
1245
deadbeef2d110be2016-01-13 12:00:26 -08001246 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001247}
1248
Peter Boström0c4e06b2015-10-07 12:23:21 +02001249bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 cricket::VideoCapturer* camera) {
1251 ASSERT(signaling_thread()->IsCurrent());
1252
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001253 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1255 // support video.
1256 LOG(LS_WARNING) << "Video not used in this call.";
1257 return false;
1258 }
1259 if (!video_channel_->SetCapturer(ssrc, camera)) {
1260 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1261 // This in the normal case when the underlying media channel has already
1262 // been deleted.
1263 ASSERT(camera == NULL);
1264 return false;
1265 }
1266 return true;
1267}
1268
nisse08582ff2016-02-04 01:24:52 -08001269void WebRtcSession::SetVideoPlayout(
1270 uint32_t ssrc,
1271 bool enable,
1272 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 ASSERT(signaling_thread()->IsCurrent());
1274 if (!video_channel_) {
1275 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1276 return;
1277 }
nisse08582ff2016-02-04 01:24:52 -08001278 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1279 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 // This in the normal case when the underlying media channel has already
1281 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001282 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 }
1284}
1285
Peter Boström0c4e06b2015-10-07 12:23:21 +02001286void WebRtcSession::SetVideoSend(uint32_t ssrc,
1287 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 const cricket::VideoOptions* options) {
1289 ASSERT(signaling_thread()->IsCurrent());
1290 if (!video_channel_) {
1291 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1292 return;
1293 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001294 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1296 // This in the normal case when the underlying media channel has already
1297 // been deleted.
1298 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
1302bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1303 ASSERT(signaling_thread()->IsCurrent());
1304 if (!voice_channel_) {
1305 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1306 return false;
1307 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001308 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1310 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001311 if (!local_desc_ ||
1312 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 &send_ssrc)) {
1314 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1315 return false;
1316 }
1317 return voice_channel_->CanInsertDtmf();
1318}
1319
1320bool WebRtcSession::InsertDtmf(const std::string& track_id,
1321 int code, int duration) {
1322 ASSERT(signaling_thread()->IsCurrent());
1323 if (!voice_channel_) {
1324 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1325 return false;
1326 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001327 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001328 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1329 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1331 return false;
1332 }
solenberg1d63dd02015-12-02 12:35:09 -08001333 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1335 return false;
1336 }
1337 return true;
1338}
1339
1340sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001341 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342}
1343
wu@webrtc.org78187522013-10-07 23:32:02 +00001344bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001345 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001346 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001347 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001348 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1349 return false;
1350 }
1351 return data_channel_->SendData(params, payload, result);
1352}
1353
1354bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001355 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001356 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1357 return false;
1358 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001359 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1360 &DataChannel::OnChannelReady);
1361 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1362 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001363 data_channel_->SignalStreamClosedRemotely.connect(
1364 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001365 return true;
1366}
1367
1368void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001369 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001370 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1371 return;
1372 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001373 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1374 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001375 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001376}
1377
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001378void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001379 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001380 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1381 return;
1382 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001383 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1384 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001385}
1386
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001387void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001388 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001389 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1390 << "NULL.";
1391 return;
1392 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001393 data_channel_->RemoveRecvStream(sid);
1394 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001395}
1396
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001397bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001398 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001399}
1400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401cricket::DataChannelType WebRtcSession::data_channel_type() const {
1402 return data_channel_type_;
1403}
1404
deadbeef0ed85b22016-02-23 17:24:52 -08001405bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1406 return pending_ice_restarts_.find(content_name) !=
1407 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001408}
1409
Henrik Boströmd8281982015-08-27 10:12:24 +02001410void WebRtcSession::OnCertificateReady(
1411 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001412 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001413}
1414
Henrik Boströmd8281982015-08-27 10:12:24 +02001415bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001416 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001417}
1418
deadbeefcbecd352015-09-23 11:50:27 -07001419const rtc::scoped_refptr<rtc::RTCCertificate>&
1420WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001421 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001422}
1423
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424void WebRtcSession::SetIceConnectionState(
1425 PeerConnectionInterface::IceConnectionState state) {
1426 if (ice_connection_state_ == state) {
1427 return;
1428 }
1429
1430 // ASSERT that the requested transition is allowed. Note that
1431 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1432 // within PeerConnection). This switch statement should compile away when
1433 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001434 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1435 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 switch (ice_connection_state_) {
1437 case PeerConnectionInterface::kIceConnectionNew:
1438 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1439 break;
1440 case PeerConnectionInterface::kIceConnectionChecking:
1441 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1442 state == PeerConnectionInterface::kIceConnectionConnected);
1443 break;
1444 case PeerConnectionInterface::kIceConnectionConnected:
1445 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1446 state == PeerConnectionInterface::kIceConnectionChecking ||
1447 state == PeerConnectionInterface::kIceConnectionCompleted);
1448 break;
1449 case PeerConnectionInterface::kIceConnectionCompleted:
1450 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1451 state == PeerConnectionInterface::kIceConnectionDisconnected);
1452 break;
1453 case PeerConnectionInterface::kIceConnectionFailed:
1454 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1455 break;
1456 case PeerConnectionInterface::kIceConnectionDisconnected:
1457 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1458 state == PeerConnectionInterface::kIceConnectionConnected ||
1459 state == PeerConnectionInterface::kIceConnectionCompleted ||
1460 state == PeerConnectionInterface::kIceConnectionFailed);
1461 break;
1462 case PeerConnectionInterface::kIceConnectionClosed:
1463 ASSERT(false);
1464 break;
1465 default:
1466 ASSERT(false);
1467 break;
1468 }
1469
1470 ice_connection_state_ = state;
1471 if (ice_observer_) {
1472 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1473 }
1474}
1475
deadbeefcbecd352015-09-23 11:50:27 -07001476void WebRtcSession::OnTransportControllerConnectionState(
1477 cricket::IceConnectionState state) {
1478 switch (state) {
1479 case cricket::kIceConnectionConnecting:
1480 // If the current state is Connected or Completed, then there were
1481 // writable channels but now there are not, so the next state must
1482 // be Disconnected.
1483 // kIceConnectionConnecting is currently used as the default,
1484 // un-connected state by the TransportController, so its only use is
1485 // detecting disconnections.
1486 if (ice_connection_state_ ==
1487 PeerConnectionInterface::kIceConnectionConnected ||
1488 ice_connection_state_ ==
1489 PeerConnectionInterface::kIceConnectionCompleted) {
1490 SetIceConnectionState(
1491 PeerConnectionInterface::kIceConnectionDisconnected);
1492 }
torbjornga81a42f2015-09-23 02:16:58 -07001493 break;
deadbeefcbecd352015-09-23 11:50:27 -07001494 case cricket::kIceConnectionFailed:
1495 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1496 break;
1497 case cricket::kIceConnectionConnected:
1498 LOG(LS_INFO) << "Changing to ICE connected state because "
1499 << "all transports are writable.";
1500 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1501 break;
1502 case cricket::kIceConnectionCompleted:
1503 LOG(LS_INFO) << "Changing to ICE completed state because "
1504 << "all transports are complete.";
1505 if (ice_connection_state_ !=
1506 PeerConnectionInterface::kIceConnectionConnected) {
1507 // If jumping directly from "checking" to "connected",
1508 // signal "connected" first.
1509 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1510 }
1511 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1512 if (metrics_observer_) {
1513 ReportTransportStats();
1514 }
1515 break;
1516 default:
1517 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001518 }
deadbeefcbecd352015-09-23 11:50:27 -07001519}
1520
1521void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001522 SetIceConnectionReceiving(receiving);
1523}
1524
1525void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1526 if (ice_connection_receiving_ == receiving) {
1527 return;
1528 }
1529 ice_connection_receiving_ = receiving;
1530 if (ice_observer_) {
1531 ice_observer_->OnIceConnectionReceivingChange(receiving);
1532 }
1533}
1534
deadbeefcbecd352015-09-23 11:50:27 -07001535void WebRtcSession::OnTransportControllerCandidatesGathered(
1536 const std::string& transport_name,
1537 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001539 int sdp_mline_index;
1540 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1541 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1542 << transport_name << " not found";
1543 return;
1544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545
deadbeefcbecd352015-09-23 11:50:27 -07001546 for (cricket::Candidates::const_iterator citer = candidates.begin();
1547 citer != candidates.end(); ++citer) {
1548 // Use transport_name as the candidate media id.
1549 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1550 if (ice_observer_) {
1551 ice_observer_->OnIceCandidate(&candidate);
1552 }
1553 if (local_desc_) {
1554 local_desc_->AddCandidate(&candidate);
1555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 }
1557}
1558
1559// Enabling voice and video channel.
1560void WebRtcSession::EnableChannels() {
1561 if (voice_channel_ && !voice_channel_->enabled())
1562 voice_channel_->Enable(true);
1563
1564 if (video_channel_ && !video_channel_->enabled())
1565 video_channel_->Enable(true);
1566
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001567 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 data_channel_->Enable(true);
1569}
1570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571// Returns the media index for a local ice candidate given the content name.
1572bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1573 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001574 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001576 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001579 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 for (size_t index = 0; index < contents.size(); ++index) {
1581 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001582 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 content_found = true;
1584 break;
1585 }
1586 }
1587 return content_found;
1588}
1589
1590bool WebRtcSession::UseCandidatesInSessionDescription(
1591 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001592 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001594 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001596
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1598 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001599 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001600 const IceCandidateInterface* candidate = candidates->at(n);
1601 bool valid = false;
1602 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1603 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001604 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1605 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001606 }
1607 continue;
1608 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001609 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001610 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 break;
deadbeefd59daf82015-10-14 15:02:44 -07001612 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 }
1614 }
1615 return ret;
1616}
1617
1618bool WebRtcSession::UseCandidate(
1619 const IceCandidateInterface* candidate) {
1620
1621 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001622 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 if (mediacontent_index >= remote_content_size) {
1624 LOG(LS_ERROR)
1625 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1626 return false;
1627 }
1628
1629 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001630 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 std::vector<cricket::Candidate> candidates;
1632 candidates.push_back(candidate->candidate());
1633 // Invoking BaseSession method to handle remote candidates.
1634 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001635 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1636 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 // Candidates successfully submitted for checking.
1638 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1639 ice_connection_state_ ==
1640 PeerConnectionInterface::kIceConnectionDisconnected) {
1641 // If state is New, then the session has just gotten its first remote ICE
1642 // candidates, so go to Checking.
1643 // If state is Disconnected, the session is re-using old candidates or
1644 // receiving additional ones, so go to Checking.
1645 // If state is Connected, stay Connected.
1646 // TODO(bemasc): If state is Connected, and the new candidates are for a
1647 // newly added transport, then the state actually _should_ move to
1648 // checking. Add a way to distinguish that case.
1649 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1650 }
1651 // TODO(bemasc): If state is Completed, go back to Connected.
1652 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001653 if (!error.empty()) {
1654 LOG(LS_WARNING) << error;
1655 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 }
1657 return true;
1658}
1659
deadbeefcbecd352015-09-23 11:50:27 -07001660void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001661 // Destroy video_channel_ first since it may have a pointer to the
1662 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 const cricket::ContentInfo* video_info =
1664 cricket::GetFirstVideoContent(desc);
1665 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 }
1669
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001670 const cricket::ContentInfo* voice_info =
1671 cricket::GetFirstAudioContent(desc);
1672 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001673 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001674 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001675 }
1676
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 const cricket::ContentInfo* data_info =
1678 cricket::GetFirstDataContent(desc);
1679 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 }
1683}
1684
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001685// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001686// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 // Creating the media channels and transport proxies.
1689 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1690 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001691 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 LOG(LS_ERROR) << "Failed to create voice channel.";
1693 return false;
1694 }
1695 }
1696
1697 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1698 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001699 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 LOG(LS_ERROR) << "Failed to create video channel.";
1701 return false;
1702 }
1703 }
1704
1705 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1706 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001707 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001708 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 LOG(LS_ERROR) << "Failed to create data channel.";
1710 return false;
1711 }
1712 }
1713
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001714 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1715 if (voice_channel()) {
1716 voice_channel()->ActivateRtcpMux();
1717 }
1718 if (video_channel()) {
1719 video_channel()->ActivateRtcpMux();
1720 }
1721 if (data_channel()) {
1722 data_channel()->ActivateRtcpMux();
1723 }
1724 }
1725
deadbeefcbecd352015-09-23 11:50:27 -07001726 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001727 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001728 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1729 cricket::GROUP_TYPE_BUNDLE);
1730 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001731 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1732 return false;
1733 }
deadbeefcbecd352015-09-23 11:50:27 -07001734 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001735 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1736 return false;
1737 }
1738 }
1739
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 return true;
1741}
1742
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001743bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001745 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001746 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001747 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001748 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001749 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001750
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001751 voice_channel_->SignalDtlsSetupFailure.connect(
1752 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001753
1754 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001755 voice_channel_->transport_channel()->SignalSentPacket.connect(
1756 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001757 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758}
1759
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001760bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001762 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001763 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001764 if (!video_channel_) {
1765 return false;
1766 }
1767
1768 video_channel_->SignalDtlsSetupFailure.connect(
1769 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001770
1771 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001772 video_channel_->transport_channel()->SignalSentPacket.connect(
1773 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001774 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775}
1776
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001777bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001778 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001780 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001781 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001782 return false;
1783 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001784
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001785 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001786 data_channel_->SignalDataReceived.connect(
1787 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001788 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001789
1790 data_channel_->SignalDtlsSetupFailure.connect(
1791 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001792
1793 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001794 data_channel_->transport_channel()->SignalSentPacket.connect(
1795 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001796 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797}
1798
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001799void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001800 SetError(ERROR_TRANSPORT,
1801 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802}
1803
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001804void WebRtcSession::OnDataChannelMessageReceived(
1805 cricket::DataChannel* channel,
1806 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001807 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001808 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1809 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1810 // Received OPEN message; parse and signal that a new data channel should
1811 // be created.
1812 std::string label;
1813 InternalDataChannelInit config;
1814 config.id = params.ssrc;
1815 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1816 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1817 << params.ssrc;
1818 return;
1819 }
1820 config.open_handshake_role = InternalDataChannelInit::kAcker;
1821 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 }
deadbeefab9b2d12015-10-14 11:33:11 -07001823 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001826// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001827bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001828 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1829 if (!bundle_enabled)
1830 return true;
1831
1832 const cricket::ContentGroup* bundle_group =
1833 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1834 ASSERT(bundle_group != NULL);
1835
1836 const cricket::ContentInfos& contents = desc->contents();
1837 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1838 citer != contents.end(); ++citer) {
1839 const cricket::ContentInfo* content = (&*citer);
1840 ASSERT(content != NULL);
1841 if (bundle_group->HasContentName(content->name) &&
1842 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1843 if (!HasRtcpMuxEnabled(content))
1844 return false;
1845 }
1846 }
1847 // RTCP-MUX is enabled in all the contents.
1848 return true;
1849}
1850
1851bool WebRtcSession::HasRtcpMuxEnabled(
1852 const cricket::ContentInfo* content) {
1853 const cricket::MediaContentDescription* description =
1854 static_cast<cricket::MediaContentDescription*>(content->description);
1855 return description->rtcp_mux();
1856}
1857
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001858bool WebRtcSession::ValidateSessionDescription(
1859 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001860 cricket::ContentSource source, std::string* err_desc) {
1861 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001862 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001863 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001864 }
1865
1866 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001867 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001868 }
1869
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001870 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001871 Action action = GetAction(sdesc->type());
1872 if (source == cricket::CS_LOCAL) {
1873 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001874 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001875 } else {
1876 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001877 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001878 }
1879
1880 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001881 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001882 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1883 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001884 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001885 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001886 }
1887
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001888 // Verify ice-ufrag and ice-pwd.
1889 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001890 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001891 }
1892
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001893 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001894 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001895 }
1896
1897 // Verify m-lines in Answer when compared against Offer.
1898 if (action == kAnswer) {
1899 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001900 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1901 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001902 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001903 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001904 }
1905 }
1906
1907 return true;
1908}
1909
1910bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1911 return ((action == kOffer && state() == STATE_INIT) ||
1912 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001913 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001914 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001915 (action == kOffer && state() == STATE_INPROGRESS) ||
1916 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001917 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1918 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1919 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1920 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001921}
1922
1923bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1924 return ((action == kOffer && state() == STATE_INIT) ||
1925 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001926 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001927 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001928 (action == kOffer && state() == STATE_INPROGRESS) ||
1929 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001930 (action == kAnswer && state() == STATE_SENTOFFER) ||
1931 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1932 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1933 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001934}
1935
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936std::string WebRtcSession::GetSessionErrorMsg() {
1937 std::ostringstream desc;
1938 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1939 desc << kSessionErrorDesc << error_desc() << ".";
1940 return desc.str();
1941}
1942
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001943// We need to check the local/remote description for the Transport instead of
1944// the session, because a new Transport added during renegotiation may have
1945// them unset while the session has them set from the previous negotiation.
1946// Not doing so may trigger the auto generation of transport description and
1947// mess up DTLS identity information, ICE credential, etc.
1948bool WebRtcSession::ReadyToUseRemoteCandidate(
1949 const IceCandidateInterface* candidate,
1950 const SessionDescriptionInterface* remote_desc,
1951 bool* valid) {
1952 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001953
1954 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001955 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001956
deadbeefd59daf82015-10-14 15:02:44 -07001957 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001958 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001959 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001960
1961 size_t mediacontent_index =
1962 static_cast<size_t>(candidate->sdp_mline_index());
1963 size_t remote_content_size =
1964 current_remote_desc->description()->contents().size();
1965 if (mediacontent_index >= remote_content_size) {
1966 LOG(LS_ERROR)
1967 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1968
1969 *valid = false;
1970 return false;
1971 }
1972
1973 cricket::ContentInfo content =
1974 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001975 cricket::BaseChannel* channel = GetChannel(content.name);
1976 if (!channel) {
1977 return false;
1978 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001979
deadbeefd59daf82015-10-14 15:02:44 -07001980 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001981 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001982}
1983
deadbeefcbecd352015-09-23 11:50:27 -07001984void WebRtcSession::OnTransportControllerGatheringState(
1985 cricket::IceGatheringState state) {
1986 ASSERT(signaling_thread()->IsCurrent());
1987 if (state == cricket::kIceGatheringGathering) {
1988 if (ice_observer_) {
1989 ice_observer_->OnIceGatheringChange(
1990 PeerConnectionInterface::kIceGatheringGathering);
1991 }
1992 } else if (state == cricket::kIceGatheringComplete) {
1993 if (ice_observer_) {
1994 ice_observer_->OnIceGatheringChange(
1995 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001996 }
1997 }
1998}
1999
2000void WebRtcSession::ReportTransportStats() {
2001 // Use a set so we don't report the same stats twice if two channels share
2002 // a transport.
2003 std::set<std::string> transport_names;
2004 if (voice_channel()) {
2005 transport_names.insert(voice_channel()->transport_name());
2006 }
2007 if (video_channel()) {
2008 transport_names.insert(video_channel()->transport_name());
2009 }
2010 if (data_channel()) {
2011 transport_names.insert(data_channel()->transport_name());
2012 }
2013 for (const auto& name : transport_names) {
2014 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002015 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002016 ReportBestConnectionState(stats);
2017 ReportNegotiatedCiphers(stats);
2018 }
2019 }
2020}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002021// Walk through the ConnectionInfos to gather best connection usage
2022// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002023void WebRtcSession::ReportBestConnectionState(
2024 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002025 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002026 for (cricket::TransportChannelStatsList::const_iterator it =
2027 stats.channel_stats.begin();
2028 it != stats.channel_stats.end(); ++it) {
2029 for (cricket::ConnectionInfos::const_iterator it_info =
2030 it->connection_infos.begin();
2031 it_info != it->connection_infos.end(); ++it_info) {
2032 if (!it_info->best_connection) {
2033 continue;
2034 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002035
2036 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2037 const cricket::Candidate& local = it_info->local_candidate;
2038 const cricket::Candidate& remote = it_info->remote_candidate;
2039
2040 // Increment the counter for IceCandidatePairType.
2041 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2042 (local.type() == RELAY_PORT_TYPE &&
2043 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2044 type = kEnumCounterIceCandidatePairTypeTcp;
2045 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2046 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002047 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002048 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002049 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002050 metrics_observer_->IncrementEnumCounter(
2051 type, GetIceCandidatePairCounter(local, remote),
2052 kIceCandidatePairMax);
2053
2054 // Increment the counter for IP type.
2055 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002056 metrics_observer_->IncrementEnumCounter(
2057 kEnumCounterAddressFamily, kBestConnections_IPv4,
2058 kPeerConnectionAddressFamilyCounter_Max);
2059
2060 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002061 metrics_observer_->IncrementEnumCounter(
2062 kEnumCounterAddressFamily, kBestConnections_IPv6,
2063 kPeerConnectionAddressFamilyCounter_Max);
2064 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002065 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002066 }
2067
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002068 return;
2069 }
2070 }
2071}
2072
jbauchac8869e2015-07-03 01:36:14 -07002073void WebRtcSession::ReportNegotiatedCiphers(
2074 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002075 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002076 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2077 return;
2078 }
2079
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002080 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2081 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2082 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2083 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002084 return;
2085 }
2086
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002087 PeerConnectionEnumCounterType srtp_counter_type;
2088 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002089 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002090 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2091 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002092 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002093 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2094 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002095 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002096 srtp_counter_type = kEnumCounterDataSrtpCipher;
2097 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002098 } else {
2099 RTC_NOTREACHED();
2100 return;
2101 }
2102
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002103 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2104 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2105 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002106 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002107 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2108 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2109 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002110 }
2111}
2112
stefanc1aeaf02015-10-15 07:26:07 -07002113void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2114 const rtc::SentPacket& sent_packet) {
2115 RTC_DCHECK(worker_thread()->IsCurrent());
2116 media_controller_->call_w()->OnSentPacket(sent_packet);
2117}
2118
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119} // namespace webrtc