blob: 56bea6403d7a295bc7382c2c8c11b2fa25bb7d4e [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"
Tommif888bb52015-12-12 01:37:01 +010026#include "webrtc/audio/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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460// Help class used to remember if a a remote peer has requested ice restart by
461// by sending a description with new ice ufrag and password.
462class IceRestartAnswerLatch {
463 public:
464 IceRestartAnswerLatch() : ice_restart_(false) { }
465
wu@webrtc.org91053e72013-08-10 07:18:04 +0000466 // Returns true if CheckForRemoteIceRestart has been called with a new session
467 // description where ice password and ufrag has changed since last time
468 // Reset() was called.
469 bool Get() const {
470 return ice_restart_;
471 }
472
473 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 if (ice_restart_) {
475 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478
Honghai Zhang04e91462015-12-11 14:26:22 -0800479 // This method has two purposes: 1. Return whether |new_desc| requests
480 // an ICE restart (i.e., new ufrag/pwd). 2. If it requests an ICE restart
481 // and it is an OFFER, remember this in |ice_restart_| so that the next
482 // Local Answer will be created with new ufrag and pwd.
honghaiz503726c2015-07-31 12:37:38 -0700483 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
484 const SessionDescriptionInterface* new_desc) {
Honghai Zhang04e91462015-12-11 14:26:22 -0800485 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700486 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 }
488 const SessionDescription* new_sd = new_desc->description();
489 const SessionDescription* old_sd = old_desc->description();
490 const ContentInfos& contents = new_sd->contents();
491 for (size_t index = 0; index < contents.size(); ++index) {
492 const ContentInfo* cinfo = &contents[index];
493 if (cinfo->rejected) {
494 continue;
495 }
496 // If the content isn't rejected, check if ufrag and password has
497 // changed.
498 const cricket::TransportDescription* new_transport_desc =
499 new_sd->GetTransportDescriptionByName(cinfo->name);
500 const cricket::TransportDescription* old_transport_desc =
501 old_sd->GetTransportDescriptionByName(cinfo->name);
502 if (!new_transport_desc || !old_transport_desc) {
503 // No transport description exist. This is not an ice restart.
504 continue;
505 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000506 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
507 old_transport_desc->ice_pwd,
508 new_transport_desc->ice_ufrag,
509 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 LOG(LS_INFO) << "Remote peer request ice restart.";
Honghai Zhang04e91462015-12-11 14:26:22 -0800511 if (new_desc->type() == SessionDescriptionInterface::kOffer) {
512 ice_restart_ = true;
513 }
honghaiz503726c2015-07-31 12:37:38 -0700514 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
516 }
honghaiz503726c2015-07-31 12:37:38 -0700517 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 }
519
520 private:
521 bool ice_restart_;
522};
523
stefanc1aeaf02015-10-15 07:26:07 -0700524WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700525 rtc::Thread* signaling_thread,
526 rtc::Thread* worker_thread,
527 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700528 : signaling_thread_(signaling_thread),
529 worker_thread_(worker_thread),
530 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // RFC 3264: The numeric value of the session id and version in the
532 // o line MUST be representable with a "64 bit signed integer".
533 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700534 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
535 transport_controller_(new cricket::TransportController(signaling_thread,
536 worker_thread,
537 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700538 media_controller_(media_controller),
539 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 ice_observer_(NULL),
541 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700542 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000544 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000546 ice_restart_latch_(new IceRestartAnswerLatch),
547 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700548 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
549 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700550 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700551 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700552 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700553 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700554 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700555 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700556 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557}
558
559WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700560 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000561 // Destroy video_channel_ first since it may have a pointer to the
562 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000563 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 SignalVideoChannelDestroyed();
565 channel_manager_->DestroyVideoChannel(video_channel_.release());
566 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000567 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000568 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200569 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000570 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000571 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 SignalDataChannelDestroyed();
573 channel_manager_->DestroyDataChannel(data_channel_.release());
574 }
deadbeef057ecf02016-01-20 14:30:43 -0800575 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700576
577 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578}
579
wu@webrtc.org91053e72013-08-10 07:18:04 +0000580bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000581 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700582 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200583 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200584 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
585 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700586 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
qiangchen444682a2015-11-24 18:07:56 -0800587 video_options_.disable_prerenderer_smoothing =
588 rtc::Optional<bool>(rtc_configuration.disable_prerenderer_smoothing);
deadbeefd59daf82015-10-14 15:02:44 -0700589 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000590
Henrik Boström87713d02015-08-25 09:53:21 +0200591 // Obtain a certificate from RTCConfiguration if any were provided (optional).
592 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
593 if (!rtc_configuration.certificates.empty()) {
594 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
595 // just picking the first one. The decision should be made based on the DTLS
596 // handshake. The DTLS negotiations need to know about all certificates.
597 certificate = rtc_configuration.certificates[0];
598 }
599
honghaiz1f429e32015-09-28 07:57:34 -0700600 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 // TODO(perkj): Take |constraints| into consideration. Return false if not all
603 // mandatory constraints can be fulfilled. Note that |constraints|
604 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000606
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000607 if (options.disable_encryption) {
608 dtls_enabled_ = false;
609 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200610 // Enable DTLS by default if we have an identity store or a certificate.
611 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000612 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700613 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
614 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000615 dtls_enabled_ = value;
616 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000617 }
618
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000620 // It takes precendence over the disable_sctp_data_channels
621 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 if (FindConstraint(
623 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
624 &value, NULL) && value) {
625 LOG(LS_INFO) << "Allowing RTP data engine.";
626 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000627 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000628 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000629 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000630 LOG(LS_INFO) << "Allowing SCTP data engine.";
631 data_channel_type_ = cricket::DCT_SCTP;
632 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
wu@webrtc.orgde305012013-10-31 15:40:38 +0000635 // Find DSCP constraint.
636 if (FindConstraint(
637 constraints,
638 MediaConstraintsInterface::kEnableDscp,
639 &value, NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100640 audio_options_.dscp = rtc::Optional<bool>(value);
641 video_options_.dscp = rtc::Optional<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000642 }
643
644 // Find Suspend Below Min Bitrate constraint.
645 if (FindConstraint(
646 constraints,
647 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
648 &value,
649 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100650 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000651 }
652
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000653 SetOptionFromOptionalConstraint(constraints,
654 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800655 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000656
657 // Find constraints for cpu overuse detection.
658 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000659 MediaConstraintsInterface::kCpuOveruseDetection,
660 &video_options_.cpu_overuse_detection);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000661
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000662 SetOptionFromOptionalConstraint(constraints,
663 MediaConstraintsInterface::kCombinedAudioVideoBwe,
664 &audio_options_.combined_audio_video_bwe);
665
kwiberg102c6a62015-10-30 02:47:38 -0700666 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100667 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200668
Karl Wibergbe579832015-11-10 22:34:18 +0100669 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
670 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200671
Henrik Boström87713d02015-08-25 09:53:21 +0200672 if (!dtls_enabled_) {
673 // Construct with DTLS disabled.
674 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700675 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200676 } else {
677 // Construct with DTLS enabled.
678 if (!certificate) {
679 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700680 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200681 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800682 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700683 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200684 } else {
685 // Use the already generated certificate.
686 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700687 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200688 }
689 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000690
Henrik Boströmd8281982015-08-27 10:12:24 +0200691 webrtc_session_desc_factory_->SignalCertificateReady.connect(
692 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000693
wu@webrtc.org97077a32013-10-25 21:18:33 +0000694 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000695 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000696 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000697 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200698 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700699
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 return true;
701}
702
deadbeefd59daf82015-10-14 15:02:44 -0700703void WebRtcSession::Close() {
704 SetState(STATE_CLOSED);
705 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000706 ASSERT(!voice_channel_);
707 ASSERT(!video_channel_);
708 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709}
710
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000711void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
712 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713}
714
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000715cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
716 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717}
718
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800719bool WebRtcSession::GetSslRole(const std::string& transport_name,
720 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700721 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000722 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
723 << "SSL Role of the session.";
724 return false;
725 }
726
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800727 return transport_controller_->GetSslRole(transport_name, role);
728}
729
730bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
731 rtc::SSLRole* role) {
732 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000733}
734
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000735void WebRtcSession::CreateOffer(
736 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700737 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
738 const cricket::MediaSessionOptions& session_options) {
739 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000740}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741
deadbeefab9b2d12015-10-14 11:33:11 -0700742void WebRtcSession::CreateAnswer(
743 CreateSessionDescriptionObserver* observer,
744 const MediaConstraintsInterface* constraints,
745 const cricket::MediaSessionOptions& session_options) {
746 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
747 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748}
749
750bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
751 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700752 ASSERT(signaling_thread()->IsCurrent());
753
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000754 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000755 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000756
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000757 // Validate SDP.
758 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
759 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
761
deadbeefd59daf82015-10-14 15:02:44 -0700762 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000763 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700765 initial_offerer_ = true;
766 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000769 cricket::SecurePolicy sdes_policy =
770 webrtc_session_desc_factory_->SdesPolicy();
771 cricket::CryptoType crypto_required = dtls_enabled_ ?
772 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
773 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000775 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000777 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778
779 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000780 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 // 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 BadLocalSdp(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(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000789 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 return false;
791 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000792
deadbeefd59daf82015-10-14 15:02:44 -0700793 if (remote_desc_) {
794 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700795 UseCandidatesInSessionDescription(remote_desc_.get());
796 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797
deadbeefd59daf82015-10-14 15:02:44 -0700798 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000799 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801 return true;
802}
803
804bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
805 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700806 ASSERT(signaling_thread()->IsCurrent());
807
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000808 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000809 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000810
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000811 // Validate SDP.
812 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
813 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 }
815
deadbeefd59daf82015-10-14 15:02:44 -0700816 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
817 remote_desc_.release());
818 remote_desc_.reset(desc_temp.release());
819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000821 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 if (action == kOffer && !CreateChannels(desc->description())) {
823 // TODO(mallinath) - Handle CreateChannel failure, as new local description
824 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000825 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
deadbeefcbecd352015-09-23 11:50:27 -0700828 // Remove unused channels if MediaContentDescription is rejected.
829 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830
831 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
832 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000833 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 return false;
835 }
836
deadbeefd59daf82015-10-14 15:02:44 -0700837 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 // Check if this new SessionDescription contains new ice ufrag and password
842 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700843 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700844 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700845 // We retain all received candidates only if ICE is not restarted.
846 // When ICE is restarted, all previous candidates belong to an old generation
847 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700848 // TODO(deadbeef): This goes against the W3C spec which says the remote
849 // description should only contain candidates from the last set remote
850 // description plus any candidates added since then. We should remove this
851 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700852 if (!ice_restart) {
853 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700854 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700855 }
856
deadbeefd59daf82015-10-14 15:02:44 -0700857 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000858 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000860
861 // Set the the ICE connection state to connecting since the connection may
862 // become writable with peer reflexive candidates before any remote candidate
863 // is signaled.
864 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
865 // is to have a new signal the indicates a change in checking state from the
866 // transport and expose a new checking() member from transport that can be
867 // read to determine the current checking state. The existing SignalConnecting
868 // actually means "gathering candidates", so cannot be be used here.
869 if (desc->type() != SessionDescriptionInterface::kOffer &&
870 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
871 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
872 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return true;
874}
875
deadbeefd59daf82015-10-14 15:02:44 -0700876void WebRtcSession::LogState(State old_state, State new_state) {
877 LOG(LS_INFO) << "Session:" << id()
878 << " Old state:" << GetStateString(old_state)
879 << " New state:" << GetStateString(new_state);
880}
881
882void WebRtcSession::SetState(State state) {
883 ASSERT(signaling_thread_->IsCurrent());
884 if (state != state_) {
885 LogState(state_, state);
886 state_ = state;
887 SignalState(this, state_);
888 }
889}
890
891void WebRtcSession::SetError(Error error, const std::string& error_desc) {
892 ASSERT(signaling_thread_->IsCurrent());
893 if (error != error_) {
894 error_ = error;
895 error_desc_ = error_desc;
896 }
897}
898
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899bool WebRtcSession::UpdateSessionState(
900 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700902 ASSERT(signaling_thread()->IsCurrent());
903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 // If there's already a pending error then no state transition should happen.
905 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700906 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000907 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000909 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
910 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 }
deadbeefd59daf82015-10-14 15:02:44 -0700912 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
913 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000914 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700915 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000916 }
deadbeefd59daf82015-10-14 15:02:44 -0700917 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000918 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000921 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
922 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
924 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700925 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
926 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000927 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700928 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000929 }
deadbeefd59daf82015-10-14 15:02:44 -0700930 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000931 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 }
933 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700934 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700935 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700936 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700937 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700938 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800939 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700940 const cricket::ContentGroup* answer_bundle =
941 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
942 if (!EnableBundle(*answer_bundle)) {
943 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
944 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
945 }
946 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800947 // Only push down the transport description after enabling BUNDLE; we don't
948 // want to push down a description on a transport about to be destroyed.
949 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
950 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
951 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700953 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000954 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700955 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000956 }
deadbeefd59daf82015-10-14 15:02:44 -0700957 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000958 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 }
960 }
961 return true;
962}
963
964WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
965 if (type == SessionDescriptionInterface::kOffer) {
966 return WebRtcSession::kOffer;
967 } else if (type == SessionDescriptionInterface::kPrAnswer) {
968 return WebRtcSession::kPrAnswer;
969 } else if (type == SessionDescriptionInterface::kAnswer) {
970 return WebRtcSession::kAnswer;
971 }
972 ASSERT(false && "unknown action type");
973 return WebRtcSession::kOffer;
974}
975
deadbeefd59daf82015-10-14 15:02:44 -0700976bool WebRtcSession::PushdownMediaDescription(
977 cricket::ContentAction action,
978 cricket::ContentSource source,
979 std::string* err) {
980 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
981 if (!ch) {
982 return true;
983 } else if (source == cricket::CS_LOCAL) {
984 return ch->PushdownLocalDescription(local_desc_->description(), action,
985 err);
986 } else {
987 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
988 err);
989 }
990 };
991
992 return (set_content(voice_channel()) &&
993 set_content(video_channel()) &&
994 set_content(data_channel()));
995}
996
997bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
998 cricket::ContentAction action,
999 std::string* error_desc) {
1000 RTC_DCHECK(signaling_thread()->IsCurrent());
1001
1002 if (source == cricket::CS_LOCAL) {
1003 return PushdownLocalTransportDescription(local_desc_->description(), action,
1004 error_desc);
1005 }
1006 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1007 error_desc);
1008}
1009
1010bool WebRtcSession::PushdownLocalTransportDescription(
1011 const SessionDescription* sdesc,
1012 cricket::ContentAction action,
1013 std::string* err) {
1014 RTC_DCHECK(signaling_thread()->IsCurrent());
1015
1016 if (!sdesc) {
1017 return false;
1018 }
1019
1020 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1021 if (!transport_controller_->SetLocalTransportDescription(
1022 tinfo.content_name, tinfo.description, action, err)) {
1023 return false;
1024 }
1025 }
1026
1027 return true;
1028}
1029
1030bool WebRtcSession::PushdownRemoteTransportDescription(
1031 const SessionDescription* sdesc,
1032 cricket::ContentAction action,
1033 std::string* err) {
1034 RTC_DCHECK(signaling_thread()->IsCurrent());
1035
1036 if (!sdesc) {
1037 return false;
1038 }
1039
1040 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1041 if (!transport_controller_->SetRemoteTransportDescription(
1042 tinfo.content_name, tinfo.description, action, err)) {
1043 return false;
1044 }
1045 }
1046
1047 return true;
1048}
1049
1050bool WebRtcSession::GetTransportDescription(
1051 const SessionDescription* description,
1052 const std::string& content_name,
1053 cricket::TransportDescription* tdesc) {
1054 if (!description || !tdesc) {
1055 return false;
1056 }
1057 const TransportInfo* transport_info =
1058 description->GetTransportInfoByName(content_name);
1059 if (!transport_info) {
1060 return false;
1061 }
1062 *tdesc = transport_info->description;
1063 return true;
1064}
1065
1066bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001067 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001068 return (GetChannelTransportStats(voice_channel(), stats) &&
1069 GetChannelTransportStats(video_channel(), stats) &&
1070 GetChannelTransportStats(data_channel(), stats));
1071}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001072
deadbeefcbecd352015-09-23 11:50:27 -07001073bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001074 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001075 ASSERT(signaling_thread()->IsCurrent());
1076 if (!ch) {
1077 // Not using this channel.
1078 return true;
1079 }
1080
1081 const std::string& content_name = ch->content_name();
1082 const std::string& transport_name = ch->transport_name();
1083 stats->proxy_to_transport[content_name] = transport_name;
1084 if (stats->transport_stats.find(transport_name) !=
1085 stats->transport_stats.end()) {
1086 // Transport stats already done for this transport.
1087 return true;
1088 }
1089
1090 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001091 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001092 return false;
1093 }
1094
1095 stats->transport_stats[transport_name] = tstats;
1096 return true;
1097}
1098
1099bool WebRtcSession::GetLocalCertificate(
1100 const std::string& transport_name,
1101 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1102 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001103 return transport_controller_->GetLocalCertificate(transport_name,
1104 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001105}
1106
1107bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1108 rtc::SSLCertificate** cert) {
1109 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001110 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001111}
1112
1113cricket::BaseChannel* WebRtcSession::GetChannel(
1114 const std::string& content_name) {
1115 if (voice_channel() && voice_channel()->content_name() == content_name) {
1116 return voice_channel();
1117 }
1118 if (video_channel() && video_channel()->content_name() == content_name) {
1119 return video_channel();
1120 }
1121 if (data_channel() && data_channel()->content_name() == content_name) {
1122 return data_channel();
1123 }
1124 return nullptr;
1125}
1126
1127bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1128 const std::string* first_content_name = bundle.FirstContentName();
1129 if (!first_content_name) {
1130 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1131 return false;
1132 }
1133 const std::string& transport_name = *first_content_name;
1134 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1135
1136 auto maybe_set_transport = [this, bundle, transport_name,
1137 first_channel](cricket::BaseChannel* ch) {
1138 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001139 return true;
1140 }
1141
deadbeefcbecd352015-09-23 11:50:27 -07001142 if (ch->transport_name() == transport_name) {
1143 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1144 << " on " << transport_name << ".";
1145 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001146 }
torbjornga81a42f2015-09-23 02:16:58 -07001147
deadbeefcbecd352015-09-23 11:50:27 -07001148 if (!ch->SetTransport(transport_name)) {
1149 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1150 return false;
1151 }
1152 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1153 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001154 return true;
1155 };
1156
deadbeefcbecd352015-09-23 11:50:27 -07001157 if (!maybe_set_transport(voice_channel()) ||
1158 !maybe_set_transport(video_channel()) ||
1159 !maybe_set_transport(data_channel())) {
1160 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001161 }
deadbeefcbecd352015-09-23 11:50:27 -07001162
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001163 return true;
1164}
1165
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001167 if (!remote_desc_) {
1168 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1169 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 return false;
1171 }
1172
1173 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001174 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 return false;
1176 }
1177
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001178 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001179 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1180 if (!valid) {
1181 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 }
1183
1184 // Add this candidate to the remote session description.
1185 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001186 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 return false;
1188 }
1189
deadbeefd59daf82015-10-14 15:02:44 -07001190 if (ready) {
1191 return UseCandidate(candidate);
1192 } else {
1193 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1194 return true;
1195 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196}
1197
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001198bool WebRtcSession::SetIceTransports(
1199 PeerConnectionInterface::IceTransportsType type) {
1200 return port_allocator()->set_candidate_filter(
1201 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001202}
1203
deadbeefd59daf82015-10-14 15:02:44 -07001204cricket::IceConfig WebRtcSession::ParseIceConfig(
1205 const PeerConnectionInterface::RTCConfiguration& config) const {
1206 cricket::IceConfig ice_config;
1207 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001208 ice_config.backup_connection_ping_interval =
1209 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001210 ice_config.gather_continually = (config.continual_gathering_policy ==
1211 PeerConnectionInterface::GATHER_CONTINUALLY);
1212 return ice_config;
1213}
1214
1215void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1216 transport_controller_->SetIceConfig(config);
1217}
1218
1219void WebRtcSession::MaybeStartGathering() {
1220 transport_controller_->MaybeStartGathering();
1221}
1222
Peter Boström0c4e06b2015-10-07 12:23:21 +02001223bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1224 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001225 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001227 }
1228 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229}
1230
Peter Boström0c4e06b2015-10-07 12:23:21 +02001231bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1232 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001233 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001234 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001235 }
1236 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237}
1238
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001239std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001241 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 return desc.str();
1243}
1244
solenbergd4cec0d2015-10-09 08:55:48 -07001245void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 ASSERT(signaling_thread()->IsCurrent());
1247 if (!voice_channel_) {
1248 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1249 return;
1250 }
solenberg4bac9c52015-10-09 02:32:53 -07001251 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1252 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 // otherwise. This in the normal case when the underlying media channel has
1254 // already been deleted.
1255 ASSERT(enable == false);
1256 }
1257}
1258
Peter Boström0c4e06b2015-10-07 12:23:21 +02001259void WebRtcSession::SetAudioSend(uint32_t ssrc,
1260 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001261 const cricket::AudioOptions& options,
1262 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 ASSERT(signaling_thread()->IsCurrent());
1264 if (!voice_channel_) {
1265 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1266 return;
1267 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001268 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001269 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001270 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271}
1272
Peter Boström0c4e06b2015-10-07 12:23:21 +02001273void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001274 ASSERT(signaling_thread()->IsCurrent());
1275 ASSERT(volume >= 0 && volume <= 10);
1276 if (!voice_channel_) {
1277 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1278 return;
1279 }
1280
solenberg4bac9c52015-10-09 02:32:53 -07001281 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001282 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001283 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001284}
1285
deadbeef2d110be2016-01-13 12:00:26 -08001286void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1287 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001288 ASSERT(signaling_thread()->IsCurrent());
1289 if (!voice_channel_)
1290 return;
1291
deadbeef2d110be2016-01-13 12:00:26 -08001292 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001293}
1294
Peter Boström0c4e06b2015-10-07 12:23:21 +02001295bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 cricket::VideoCapturer* camera) {
1297 ASSERT(signaling_thread()->IsCurrent());
1298
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001299 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1301 // support video.
1302 LOG(LS_WARNING) << "Video not used in this call.";
1303 return false;
1304 }
1305 if (!video_channel_->SetCapturer(ssrc, camera)) {
1306 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1307 // This in the normal case when the underlying media channel has already
1308 // been deleted.
1309 ASSERT(camera == NULL);
1310 return false;
1311 }
1312 return true;
1313}
1314
nisse08582ff2016-02-04 01:24:52 -08001315void WebRtcSession::SetVideoPlayout(
1316 uint32_t ssrc,
1317 bool enable,
1318 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 ASSERT(signaling_thread()->IsCurrent());
1320 if (!video_channel_) {
1321 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1322 return;
1323 }
nisse08582ff2016-02-04 01:24:52 -08001324 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1325 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 // This in the normal case when the underlying media channel has already
1327 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001328 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 }
1330}
1331
Peter Boström0c4e06b2015-10-07 12:23:21 +02001332void WebRtcSession::SetVideoSend(uint32_t ssrc,
1333 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 const cricket::VideoOptions* options) {
1335 ASSERT(signaling_thread()->IsCurrent());
1336 if (!video_channel_) {
1337 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1338 return;
1339 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001340 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1342 // This in the normal case when the underlying media channel has already
1343 // been deleted.
1344 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
1347
1348bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1349 ASSERT(signaling_thread()->IsCurrent());
1350 if (!voice_channel_) {
1351 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1352 return false;
1353 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001354 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1356 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001357 if (!local_desc_ ||
1358 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 &send_ssrc)) {
1360 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1361 return false;
1362 }
1363 return voice_channel_->CanInsertDtmf();
1364}
1365
1366bool WebRtcSession::InsertDtmf(const std::string& track_id,
1367 int code, int duration) {
1368 ASSERT(signaling_thread()->IsCurrent());
1369 if (!voice_channel_) {
1370 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1371 return false;
1372 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001373 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001374 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1375 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1377 return false;
1378 }
solenberg1d63dd02015-12-02 12:35:09 -08001379 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1381 return false;
1382 }
1383 return true;
1384}
1385
1386sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001387 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388}
1389
wu@webrtc.org78187522013-10-07 23:32:02 +00001390bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001391 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001392 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001393 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001394 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1395 return false;
1396 }
1397 return data_channel_->SendData(params, payload, result);
1398}
1399
1400bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001401 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001402 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1403 return false;
1404 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001405 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1406 &DataChannel::OnChannelReady);
1407 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1408 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001409 data_channel_->SignalStreamClosedRemotely.connect(
1410 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001411 return true;
1412}
1413
1414void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001415 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001416 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1417 return;
1418 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001419 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1420 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001421 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001422}
1423
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001424void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001425 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001426 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1427 return;
1428 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001429 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1430 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001431}
1432
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001433void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001434 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001435 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1436 << "NULL.";
1437 return;
1438 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001439 data_channel_->RemoveRecvStream(sid);
1440 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001441}
1442
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001443bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001444 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001445}
1446
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447cricket::DataChannelType WebRtcSession::data_channel_type() const {
1448 return data_channel_type_;
1449}
1450
wu@webrtc.org91053e72013-08-10 07:18:04 +00001451bool WebRtcSession::IceRestartPending() const {
1452 return ice_restart_latch_->Get();
1453}
1454
1455void WebRtcSession::ResetIceRestartLatch() {
1456 ice_restart_latch_->Reset();
1457}
1458
Henrik Boströmd8281982015-08-27 10:12:24 +02001459void WebRtcSession::OnCertificateReady(
1460 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001461 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001462}
1463
Henrik Boströmd8281982015-08-27 10:12:24 +02001464bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001465 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001466}
1467
deadbeefcbecd352015-09-23 11:50:27 -07001468const rtc::scoped_refptr<rtc::RTCCertificate>&
1469WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001470 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001471}
1472
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473void WebRtcSession::SetIceConnectionState(
1474 PeerConnectionInterface::IceConnectionState state) {
1475 if (ice_connection_state_ == state) {
1476 return;
1477 }
1478
1479 // ASSERT that the requested transition is allowed. Note that
1480 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1481 // within PeerConnection). This switch statement should compile away when
1482 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001483 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1484 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 switch (ice_connection_state_) {
1486 case PeerConnectionInterface::kIceConnectionNew:
1487 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1488 break;
1489 case PeerConnectionInterface::kIceConnectionChecking:
1490 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1491 state == PeerConnectionInterface::kIceConnectionConnected);
1492 break;
1493 case PeerConnectionInterface::kIceConnectionConnected:
1494 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1495 state == PeerConnectionInterface::kIceConnectionChecking ||
1496 state == PeerConnectionInterface::kIceConnectionCompleted);
1497 break;
1498 case PeerConnectionInterface::kIceConnectionCompleted:
1499 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1500 state == PeerConnectionInterface::kIceConnectionDisconnected);
1501 break;
1502 case PeerConnectionInterface::kIceConnectionFailed:
1503 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1504 break;
1505 case PeerConnectionInterface::kIceConnectionDisconnected:
1506 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1507 state == PeerConnectionInterface::kIceConnectionConnected ||
1508 state == PeerConnectionInterface::kIceConnectionCompleted ||
1509 state == PeerConnectionInterface::kIceConnectionFailed);
1510 break;
1511 case PeerConnectionInterface::kIceConnectionClosed:
1512 ASSERT(false);
1513 break;
1514 default:
1515 ASSERT(false);
1516 break;
1517 }
1518
1519 ice_connection_state_ = state;
1520 if (ice_observer_) {
1521 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1522 }
1523}
1524
deadbeefcbecd352015-09-23 11:50:27 -07001525void WebRtcSession::OnTransportControllerConnectionState(
1526 cricket::IceConnectionState state) {
1527 switch (state) {
1528 case cricket::kIceConnectionConnecting:
1529 // If the current state is Connected or Completed, then there were
1530 // writable channels but now there are not, so the next state must
1531 // be Disconnected.
1532 // kIceConnectionConnecting is currently used as the default,
1533 // un-connected state by the TransportController, so its only use is
1534 // detecting disconnections.
1535 if (ice_connection_state_ ==
1536 PeerConnectionInterface::kIceConnectionConnected ||
1537 ice_connection_state_ ==
1538 PeerConnectionInterface::kIceConnectionCompleted) {
1539 SetIceConnectionState(
1540 PeerConnectionInterface::kIceConnectionDisconnected);
1541 }
torbjornga81a42f2015-09-23 02:16:58 -07001542 break;
deadbeefcbecd352015-09-23 11:50:27 -07001543 case cricket::kIceConnectionFailed:
1544 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1545 break;
1546 case cricket::kIceConnectionConnected:
1547 LOG(LS_INFO) << "Changing to ICE connected state because "
1548 << "all transports are writable.";
1549 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1550 break;
1551 case cricket::kIceConnectionCompleted:
1552 LOG(LS_INFO) << "Changing to ICE completed state because "
1553 << "all transports are complete.";
1554 if (ice_connection_state_ !=
1555 PeerConnectionInterface::kIceConnectionConnected) {
1556 // If jumping directly from "checking" to "connected",
1557 // signal "connected" first.
1558 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1559 }
1560 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1561 if (metrics_observer_) {
1562 ReportTransportStats();
1563 }
1564 break;
1565 default:
1566 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001567 }
deadbeefcbecd352015-09-23 11:50:27 -07001568}
1569
1570void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001571 SetIceConnectionReceiving(receiving);
1572}
1573
1574void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1575 if (ice_connection_receiving_ == receiving) {
1576 return;
1577 }
1578 ice_connection_receiving_ = receiving;
1579 if (ice_observer_) {
1580 ice_observer_->OnIceConnectionReceivingChange(receiving);
1581 }
1582}
1583
deadbeefcbecd352015-09-23 11:50:27 -07001584void WebRtcSession::OnTransportControllerCandidatesGathered(
1585 const std::string& transport_name,
1586 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001588 int sdp_mline_index;
1589 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1590 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1591 << transport_name << " not found";
1592 return;
1593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594
deadbeefcbecd352015-09-23 11:50:27 -07001595 for (cricket::Candidates::const_iterator citer = candidates.begin();
1596 citer != candidates.end(); ++citer) {
1597 // Use transport_name as the candidate media id.
1598 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1599 if (ice_observer_) {
1600 ice_observer_->OnIceCandidate(&candidate);
1601 }
1602 if (local_desc_) {
1603 local_desc_->AddCandidate(&candidate);
1604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 }
1606}
1607
1608// Enabling voice and video channel.
1609void WebRtcSession::EnableChannels() {
1610 if (voice_channel_ && !voice_channel_->enabled())
1611 voice_channel_->Enable(true);
1612
1613 if (video_channel_ && !video_channel_->enabled())
1614 video_channel_->Enable(true);
1615
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001616 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 data_channel_->Enable(true);
1618}
1619
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620// Returns the media index for a local ice candidate given the content name.
1621bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1622 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001623 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626
1627 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001628 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 for (size_t index = 0; index < contents.size(); ++index) {
1630 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001631 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 content_found = true;
1633 break;
1634 }
1635 }
1636 return content_found;
1637}
1638
1639bool WebRtcSession::UseCandidatesInSessionDescription(
1640 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001641 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001645
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1647 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001648 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001649 const IceCandidateInterface* candidate = candidates->at(n);
1650 bool valid = false;
1651 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1652 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001653 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1654 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001655 }
1656 continue;
1657 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001658 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001659 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 break;
deadbeefd59daf82015-10-14 15:02:44 -07001661 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 }
1663 }
1664 return ret;
1665}
1666
1667bool WebRtcSession::UseCandidate(
1668 const IceCandidateInterface* candidate) {
1669
1670 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001671 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 if (mediacontent_index >= remote_content_size) {
1673 LOG(LS_ERROR)
1674 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1675 return false;
1676 }
1677
1678 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001679 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 std::vector<cricket::Candidate> candidates;
1681 candidates.push_back(candidate->candidate());
1682 // Invoking BaseSession method to handle remote candidates.
1683 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001684 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1685 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 // Candidates successfully submitted for checking.
1687 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1688 ice_connection_state_ ==
1689 PeerConnectionInterface::kIceConnectionDisconnected) {
1690 // If state is New, then the session has just gotten its first remote ICE
1691 // candidates, so go to Checking.
1692 // If state is Disconnected, the session is re-using old candidates or
1693 // receiving additional ones, so go to Checking.
1694 // If state is Connected, stay Connected.
1695 // TODO(bemasc): If state is Connected, and the new candidates are for a
1696 // newly added transport, then the state actually _should_ move to
1697 // checking. Add a way to distinguish that case.
1698 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1699 }
1700 // TODO(bemasc): If state is Completed, go back to Connected.
1701 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001702 if (!error.empty()) {
1703 LOG(LS_WARNING) << error;
1704 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 }
1706 return true;
1707}
1708
deadbeefcbecd352015-09-23 11:50:27 -07001709void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001710 // Destroy video_channel_ first since it may have a pointer to the
1711 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 const cricket::ContentInfo* video_info =
1713 cricket::GetFirstVideoContent(desc);
1714 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 }
1718
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001719 const cricket::ContentInfo* voice_info =
1720 cricket::GetFirstAudioContent(desc);
1721 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001722 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001723 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001724 }
1725
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 const cricket::ContentInfo* data_info =
1727 cricket::GetFirstDataContent(desc);
1728 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 }
1732}
1733
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001734// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001735// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 // Creating the media channels and transport proxies.
1738 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1739 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001740 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 LOG(LS_ERROR) << "Failed to create voice channel.";
1742 return false;
1743 }
1744 }
1745
1746 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1747 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001748 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 LOG(LS_ERROR) << "Failed to create video channel.";
1750 return false;
1751 }
1752 }
1753
1754 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1755 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001756 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001757 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 LOG(LS_ERROR) << "Failed to create data channel.";
1759 return false;
1760 }
1761 }
1762
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001763 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1764 if (voice_channel()) {
1765 voice_channel()->ActivateRtcpMux();
1766 }
1767 if (video_channel()) {
1768 video_channel()->ActivateRtcpMux();
1769 }
1770 if (data_channel()) {
1771 data_channel()->ActivateRtcpMux();
1772 }
1773 }
1774
deadbeefcbecd352015-09-23 11:50:27 -07001775 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001776 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001777 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1778 cricket::GROUP_TYPE_BUNDLE);
1779 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001780 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1781 return false;
1782 }
deadbeefcbecd352015-09-23 11:50:27 -07001783 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001784 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1785 return false;
1786 }
1787 }
1788
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 return true;
1790}
1791
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001792bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001794 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001795 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001796 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001797 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001798 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001799
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001800 voice_channel_->SignalDtlsSetupFailure.connect(
1801 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001802
1803 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001804 voice_channel_->transport_channel()->SignalSentPacket.connect(
1805 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001806 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807}
1808
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001809bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001811 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001812 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001813 if (!video_channel_) {
1814 return false;
1815 }
1816
1817 video_channel_->SignalDtlsSetupFailure.connect(
1818 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001819
1820 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001821 video_channel_->transport_channel()->SignalSentPacket.connect(
1822 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001826bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001827 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001829 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001830 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001831 return false;
1832 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001833
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001834 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001835 data_channel_->SignalDataReceived.connect(
1836 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001837 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001838
1839 data_channel_->SignalDtlsSetupFailure.connect(
1840 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001841
1842 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001843 data_channel_->transport_channel()->SignalSentPacket.connect(
1844 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001845 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846}
1847
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001848void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001849 SetError(ERROR_TRANSPORT,
1850 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851}
1852
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001853void WebRtcSession::OnDataChannelMessageReceived(
1854 cricket::DataChannel* channel,
1855 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001856 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001857 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1858 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1859 // Received OPEN message; parse and signal that a new data channel should
1860 // be created.
1861 std::string label;
1862 InternalDataChannelInit config;
1863 config.id = params.ssrc;
1864 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1865 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1866 << params.ssrc;
1867 return;
1868 }
1869 config.open_handshake_role = InternalDataChannelInit::kAcker;
1870 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 }
deadbeefab9b2d12015-10-14 11:33:11 -07001872 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001875// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001876bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001877 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1878 if (!bundle_enabled)
1879 return true;
1880
1881 const cricket::ContentGroup* bundle_group =
1882 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1883 ASSERT(bundle_group != NULL);
1884
1885 const cricket::ContentInfos& contents = desc->contents();
1886 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1887 citer != contents.end(); ++citer) {
1888 const cricket::ContentInfo* content = (&*citer);
1889 ASSERT(content != NULL);
1890 if (bundle_group->HasContentName(content->name) &&
1891 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1892 if (!HasRtcpMuxEnabled(content))
1893 return false;
1894 }
1895 }
1896 // RTCP-MUX is enabled in all the contents.
1897 return true;
1898}
1899
1900bool WebRtcSession::HasRtcpMuxEnabled(
1901 const cricket::ContentInfo* content) {
1902 const cricket::MediaContentDescription* description =
1903 static_cast<cricket::MediaContentDescription*>(content->description);
1904 return description->rtcp_mux();
1905}
1906
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001907bool WebRtcSession::ValidateSessionDescription(
1908 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001909 cricket::ContentSource source, std::string* err_desc) {
1910 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001911 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001912 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913 }
1914
1915 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001916 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001917 }
1918
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001919 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001920 Action action = GetAction(sdesc->type());
1921 if (source == cricket::CS_LOCAL) {
1922 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001923 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924 } else {
1925 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001927 }
1928
1929 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001930 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001931 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1932 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001933 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001934 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001935 }
1936
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001937 // Verify ice-ufrag and ice-pwd.
1938 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001939 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001940 }
1941
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001942 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001943 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944 }
1945
1946 // Verify m-lines in Answer when compared against Offer.
1947 if (action == kAnswer) {
1948 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001949 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1950 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001951 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001952 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001953 }
1954 }
1955
1956 return true;
1957}
1958
1959bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1960 return ((action == kOffer && state() == STATE_INIT) ||
1961 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001962 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001963 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001964 (action == kOffer && state() == STATE_INPROGRESS) ||
1965 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001966 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1967 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1968 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1969 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001970}
1971
1972bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1973 return ((action == kOffer && state() == STATE_INIT) ||
1974 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001975 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001976 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001977 (action == kOffer && state() == STATE_INPROGRESS) ||
1978 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001979 (action == kAnswer && state() == STATE_SENTOFFER) ||
1980 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1981 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1982 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001983}
1984
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001985std::string WebRtcSession::GetSessionErrorMsg() {
1986 std::ostringstream desc;
1987 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1988 desc << kSessionErrorDesc << error_desc() << ".";
1989 return desc.str();
1990}
1991
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001992// We need to check the local/remote description for the Transport instead of
1993// the session, because a new Transport added during renegotiation may have
1994// them unset while the session has them set from the previous negotiation.
1995// Not doing so may trigger the auto generation of transport description and
1996// mess up DTLS identity information, ICE credential, etc.
1997bool WebRtcSession::ReadyToUseRemoteCandidate(
1998 const IceCandidateInterface* candidate,
1999 const SessionDescriptionInterface* remote_desc,
2000 bool* valid) {
2001 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002002
2003 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002004 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002005
deadbeefd59daf82015-10-14 15:02:44 -07002006 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002007 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002008 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002009
2010 size_t mediacontent_index =
2011 static_cast<size_t>(candidate->sdp_mline_index());
2012 size_t remote_content_size =
2013 current_remote_desc->description()->contents().size();
2014 if (mediacontent_index >= remote_content_size) {
2015 LOG(LS_ERROR)
2016 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2017
2018 *valid = false;
2019 return false;
2020 }
2021
2022 cricket::ContentInfo content =
2023 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002024 cricket::BaseChannel* channel = GetChannel(content.name);
2025 if (!channel) {
2026 return false;
2027 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002028
deadbeefd59daf82015-10-14 15:02:44 -07002029 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002030 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002031}
2032
deadbeefcbecd352015-09-23 11:50:27 -07002033void WebRtcSession::OnTransportControllerGatheringState(
2034 cricket::IceGatheringState state) {
2035 ASSERT(signaling_thread()->IsCurrent());
2036 if (state == cricket::kIceGatheringGathering) {
2037 if (ice_observer_) {
2038 ice_observer_->OnIceGatheringChange(
2039 PeerConnectionInterface::kIceGatheringGathering);
2040 }
2041 } else if (state == cricket::kIceGatheringComplete) {
2042 if (ice_observer_) {
2043 ice_observer_->OnIceGatheringChange(
2044 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002045 }
2046 }
2047}
2048
2049void WebRtcSession::ReportTransportStats() {
2050 // Use a set so we don't report the same stats twice if two channels share
2051 // a transport.
2052 std::set<std::string> transport_names;
2053 if (voice_channel()) {
2054 transport_names.insert(voice_channel()->transport_name());
2055 }
2056 if (video_channel()) {
2057 transport_names.insert(video_channel()->transport_name());
2058 }
2059 if (data_channel()) {
2060 transport_names.insert(data_channel()->transport_name());
2061 }
2062 for (const auto& name : transport_names) {
2063 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002064 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002065 ReportBestConnectionState(stats);
2066 ReportNegotiatedCiphers(stats);
2067 }
2068 }
2069}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002070// Walk through the ConnectionInfos to gather best connection usage
2071// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002072void WebRtcSession::ReportBestConnectionState(
2073 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002074 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002075 for (cricket::TransportChannelStatsList::const_iterator it =
2076 stats.channel_stats.begin();
2077 it != stats.channel_stats.end(); ++it) {
2078 for (cricket::ConnectionInfos::const_iterator it_info =
2079 it->connection_infos.begin();
2080 it_info != it->connection_infos.end(); ++it_info) {
2081 if (!it_info->best_connection) {
2082 continue;
2083 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002084
2085 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2086 const cricket::Candidate& local = it_info->local_candidate;
2087 const cricket::Candidate& remote = it_info->remote_candidate;
2088
2089 // Increment the counter for IceCandidatePairType.
2090 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2091 (local.type() == RELAY_PORT_TYPE &&
2092 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2093 type = kEnumCounterIceCandidatePairTypeTcp;
2094 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2095 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002096 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002097 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002098 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002099 metrics_observer_->IncrementEnumCounter(
2100 type, GetIceCandidatePairCounter(local, remote),
2101 kIceCandidatePairMax);
2102
2103 // Increment the counter for IP type.
2104 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002105 metrics_observer_->IncrementEnumCounter(
2106 kEnumCounterAddressFamily, kBestConnections_IPv4,
2107 kPeerConnectionAddressFamilyCounter_Max);
2108
2109 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002110 metrics_observer_->IncrementEnumCounter(
2111 kEnumCounterAddressFamily, kBestConnections_IPv6,
2112 kPeerConnectionAddressFamilyCounter_Max);
2113 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002114 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002115 }
2116
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002117 return;
2118 }
2119 }
2120}
2121
jbauchac8869e2015-07-03 01:36:14 -07002122void WebRtcSession::ReportNegotiatedCiphers(
2123 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002124 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002125 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2126 return;
2127 }
2128
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002129 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2130 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2131 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2132 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002133 return;
2134 }
2135
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002136 PeerConnectionEnumCounterType srtp_counter_type;
2137 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002138 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002139 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2140 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002141 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002142 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2143 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002144 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002145 srtp_counter_type = kEnumCounterDataSrtpCipher;
2146 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002147 } else {
2148 RTC_NOTREACHED();
2149 return;
2150 }
2151
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002152 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2153 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2154 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002155 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002156 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2157 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2158 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002159 }
2160}
2161
stefanc1aeaf02015-10-15 07:26:07 -07002162void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2163 const rtc::SentPacket& sent_packet) {
2164 RTC_DCHECK(worker_thread()->IsCurrent());
2165 media_controller_->call_w()->OnSentPacket(sent_packet);
2166}
2167
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168} // namespace webrtc