blob: 2c414a9761fca2d9d7e001e71f2a99076763615e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/jsepicecandidate.h"
21#include "webrtc/api/jsepsessiondescription.h"
22#include "webrtc/api/mediaconstraintsinterface.h"
23#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010026#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::ContentInfo;
43using cricket::ContentInfos;
44using cricket::MediaContentDescription;
45using cricket::SessionDescription;
46using cricket::TransportInfo;
47
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070048using cricket::LOCAL_PORT_TYPE;
49using cricket::STUN_PORT_TYPE;
50using cricket::RELAY_PORT_TYPE;
51using cricket::PRFLX_PORT_TYPE;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053namespace webrtc {
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000056const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
61const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
63const char kPushDownTDFailed[] =
64 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000065const char kSdpWithoutDtlsFingerprint[] =
66 "Called with SDP without DTLS fingerprint.";
67const char kSdpWithoutSdesCrypto[] =
68 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000073const char kDtlsSetupFailureRtp[] =
74 "Couldn't set up DTLS-SRTP on RTP channel.";
75const char kDtlsSetupFailureRtcp[] =
76 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070077const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070079IceCandidatePairType GetIceCandidatePairCounter(
80 const cricket::Candidate& local,
81 const cricket::Candidate& remote) {
82 const auto& l = local.type();
83 const auto& r = remote.type();
84 const auto& host = LOCAL_PORT_TYPE;
85 const auto& srflx = STUN_PORT_TYPE;
86 const auto& relay = RELAY_PORT_TYPE;
87 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070088 if (l == host && r == host) {
89 bool local_private = IPIsPrivate(local.address().ipaddr());
90 bool remote_private = IPIsPrivate(remote.address().ipaddr());
91 if (local_private) {
92 if (remote_private) {
93 return kIceCandidatePairHostPrivateHostPrivate;
94 } else {
95 return kIceCandidatePairHostPrivateHostPublic;
96 }
97 } else {
98 if (remote_private) {
99 return kIceCandidatePairHostPublicHostPrivate;
100 } else {
101 return kIceCandidatePairHostPublicHostPublic;
102 }
103 }
104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700105 if (l == host && r == srflx)
106 return kIceCandidatePairHostSrflx;
107 if (l == host && r == relay)
108 return kIceCandidatePairHostRelay;
109 if (l == host && r == prflx)
110 return kIceCandidatePairHostPrflx;
111 if (l == srflx && r == host)
112 return kIceCandidatePairSrflxHost;
113 if (l == srflx && r == srflx)
114 return kIceCandidatePairSrflxSrflx;
115 if (l == srflx && r == relay)
116 return kIceCandidatePairSrflxRelay;
117 if (l == srflx && r == prflx)
118 return kIceCandidatePairSrflxPrflx;
119 if (l == relay && r == host)
120 return kIceCandidatePairRelayHost;
121 if (l == relay && r == srflx)
122 return kIceCandidatePairRelaySrflx;
123 if (l == relay && r == relay)
124 return kIceCandidatePairRelayRelay;
125 if (l == relay && r == prflx)
126 return kIceCandidatePairRelayPrflx;
127 if (l == prflx && r == host)
128 return kIceCandidatePairPrflxHost;
129 if (l == prflx && r == srflx)
130 return kIceCandidatePairPrflxSrflx;
131 if (l == prflx && r == relay)
132 return kIceCandidatePairPrflxRelay;
133 return kIceCandidatePairMax;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Compares |answer| against |offer|. Comparision is done
137// for number of m-lines in answer against offer. If matches true will be
138// returned otherwise false.
139static bool VerifyMediaDescriptions(
140 const SessionDescription* answer, const SessionDescription* offer) {
141 if (offer->contents().size() != answer->contents().size())
142 return false;
143
144 for (size_t i = 0; i < offer->contents().size(); ++i) {
145 if ((offer->contents()[i].name) != answer->contents()[i].name) {
146 return false;
147 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000148 const MediaContentDescription* offer_mdesc =
149 static_cast<const MediaContentDescription*>(
150 offer->contents()[i].description);
151 const MediaContentDescription* answer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 answer->contents()[i].description);
154 if (offer_mdesc->type() != answer_mdesc->type()) {
155 return false;
156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159}
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// Checks that each non-rejected content has SDES crypto keys or a DTLS
162// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
163// keys, will be caught in Transport negotiation, and backstopped by Channel's
164// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000165static bool VerifyCrypto(const SessionDescription* desc,
166 bool dtls_enabled,
167 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 const ContentInfos& contents = desc->contents();
169 for (size_t index = 0; index < contents.size(); ++index) {
170 const ContentInfo* cinfo = &contents[index];
171 if (cinfo->rejected) {
172 continue;
173 }
174
175 // If the content isn't rejected, crypto must be present.
176 const MediaContentDescription* media =
177 static_cast<const MediaContentDescription*>(cinfo->description);
178 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
179 if (!media || !tinfo) {
180 // Something is not right.
181 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return false;
184 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000185 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000187 LOG(LS_WARNING) <<
188 "Session description must have DTLS fingerprint if DTLS enabled.";
189 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 return false;
191 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 } else {
193 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 LOG(LS_WARNING) <<
195 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 return false;
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
200 }
201
202 return true;
203}
204
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000205// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
206static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
207 const ContentInfos& contents = desc->contents();
208 for (size_t index = 0; index < contents.size(); ++index) {
209 const ContentInfo* cinfo = &contents[index];
210 if (cinfo->rejected) {
211 continue;
212 }
213
214 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
215 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
216 if (!tinfo) {
217 // Something is not right.
218 LOG(LS_ERROR) << kInvalidSdp;
219 return false;
220 }
221 if (tinfo->description.ice_ufrag.empty() ||
222 tinfo->description.ice_pwd.empty()) {
223 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
224 return false;
225 }
226 }
227 return true;
228}
229
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230// Forces |sdesc->crypto_required| to the appropriate state based on the
231// current security policy, to ensure a failure occurs if there is an error
232// in crypto negotiation.
233// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000234static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
235 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 if (!sdesc) {
237 return;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 // Updating the |crypto_required_| in MediaContentDescription to the
241 // appropriate state based on the current security policy.
242 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
243 iter != sdesc->contents().end(); ++iter) {
244 if (cricket::IsMediaContent(&*iter)) {
245 MediaContentDescription* mdesc =
246 static_cast<MediaContentDescription*> (iter->description);
247 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 }
250 }
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
255 const std::string& track_id,
256 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 const cricket::ContentInfo* audio_info =
258 cricket::GetFirstAudioContent(session_description);
259 if (!audio_info) {
260 LOG(LS_ERROR) << "Audio not used in this call";
261 return false;
262 }
263
264 const cricket::MediaContentDescription* audio_content =
265 static_cast<const cricket::MediaContentDescription*>(
266 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000267 const cricket::StreamParams* stream =
268 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
269 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return false;
271 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272
273 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return true;
275}
276
277static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint32_t ssrc,
279 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 ASSERT(track_id != NULL);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 const cricket::ContentInfo* audio_info =
283 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000284 if (audio_info) {
285 const cricket::MediaContentDescription* audio_content =
286 static_cast<const cricket::MediaContentDescription*>(
287 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289 const auto* found =
290 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
291 if (found) {
292 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000293 return true;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
297 const cricket::ContentInfo* video_info =
298 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (video_info) {
300 const cricket::MediaContentDescription* video_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311 return false;
312}
313
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314static bool BadSdp(const std::string& source,
315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
318 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700319 desc << "Failed to set " << source;
320 if (!type.empty()) {
321 desc << " " << type;
322 }
323 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000326 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 return false;
330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 }
341}
342
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343static bool BadLocalSdp(const std::string& type,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
347}
348
349static bool BadRemoteSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
353}
354
355static bool BadOfferSdp(cricket::ContentSource source,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
359}
360
361static bool BadPranswerSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
365 reason, err_desc);
366}
367
368static bool BadAnswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
deadbeefd59daf82015-10-14 15:02:44 -0700374#define GET_STRING_OF_STATE(state) \
375 case webrtc::WebRtcSession::state: \
376 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 break;
378
deadbeefd59daf82015-10-14 15:02:44 -0700379static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 std::string result;
381 switch (state) {
382 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700383 GET_STRING_OF_STATE(STATE_SENTOFFER)
384 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
385 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
386 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
390 ASSERT(false);
391 break;
392 }
393 return result;
394}
395
deadbeefd59daf82015-10-14 15:02:44 -0700396#define GET_STRING_OF_ERROR_CODE(err) \
397 case webrtc::WebRtcSession::err: \
398 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 break;
400
deadbeefd59daf82015-10-14 15:02:44 -0700401static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 std::string result;
403 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
406 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
deadbeefd59daf82015-10-14 15:02:44 -0700408 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414static std::string MakeErrorString(const std::string& error,
415 const std::string& desc) {
416 std::ostringstream ret;
417 ret << error << " " << desc;
418 return ret.str();
419}
420
421static std::string MakeTdErrorString(const std::string& desc) {
422 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000425// Set |option| to the highest-priority value of |key| in the optional
426// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700427template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000428static void SetOptionFromOptionalConstraint(
429 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700430 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100431 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000432 if (!constraints) {
433 return;
434 }
435 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000436 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000437 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100439 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440 }
441 }
442}
443
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000445 PeerConnectionInterface::IceTransportsType type) {
446 switch (type) {
447 case PeerConnectionInterface::kNone:
448 return cricket::CF_NONE;
449 case PeerConnectionInterface::kRelay:
450 return cricket::CF_RELAY;
451 case PeerConnectionInterface::kNoHost:
452 return (cricket::CF_ALL & ~cricket::CF_HOST);
453 case PeerConnectionInterface::kAll:
454 return cricket::CF_ALL;
455 default: ASSERT(false);
456 }
457 return cricket::CF_NONE;
458}
459
deadbeef0ed85b22016-02-23 17:24:52 -0800460// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
461bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
462 const SessionDescriptionInterface* new_desc,
463 const std::string& content_name) {
464 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700465 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
deadbeef0ed85b22016-02-23 17:24:52 -0800467 const SessionDescription* new_sd = new_desc->description();
468 const SessionDescription* old_sd = old_desc->description();
469 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
470 if (!cinfo || cinfo->rejected) {
471 return false;
472 }
473 // If the content isn't rejected, check if ufrag and password has changed.
474 const cricket::TransportDescription* new_transport_desc =
475 new_sd->GetTransportDescriptionByName(content_name);
476 const cricket::TransportDescription* old_transport_desc =
477 old_sd->GetTransportDescriptionByName(content_name);
478 if (!new_transport_desc || !old_transport_desc) {
479 // No transport description exists. This is not an ICE restart.
480 return false;
481 }
482 if (cricket::IceCredentialsChanged(
483 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
484 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
485 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
486 << ".";
487 return true;
488 }
489 return false;
490}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
stefanc1aeaf02015-10-15 07:26:07 -0700492WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700493 rtc::Thread* signaling_thread,
494 rtc::Thread* worker_thread,
495 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700496 : signaling_thread_(signaling_thread),
497 worker_thread_(worker_thread),
498 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 // RFC 3264: The numeric value of the session id and version in the
500 // o line MUST be representable with a "64 bit signed integer".
501 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700502 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
503 transport_controller_(new cricket::TransportController(signaling_thread,
504 worker_thread,
505 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700506 media_controller_(media_controller),
507 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 ice_observer_(NULL),
509 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700510 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000512 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000514 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700515 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
516 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700517 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700518 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700519 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700520 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700521 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700522 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700523 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524}
525
526WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700527 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000528 // Destroy video_channel_ first since it may have a pointer to the
529 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000530 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 SignalVideoChannelDestroyed();
532 channel_manager_->DestroyVideoChannel(video_channel_.release());
533 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000534 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000535 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200536 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000537 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000538 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 SignalDataChannelDestroyed();
540 channel_manager_->DestroyDataChannel(data_channel_.release());
541 }
deadbeef057ecf02016-01-20 14:30:43 -0800542 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700543
544 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000548 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700549 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200550 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200551 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
552 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700553 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700554 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000555
Henrik Boström87713d02015-08-25 09:53:21 +0200556 // Obtain a certificate from RTCConfiguration if any were provided (optional).
557 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
558 if (!rtc_configuration.certificates.empty()) {
559 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
560 // just picking the first one. The decision should be made based on the DTLS
561 // handshake. The DTLS negotiations need to know about all certificates.
562 certificate = rtc_configuration.certificates[0];
563 }
564
honghaiz1f429e32015-09-28 07:57:34 -0700565 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700566
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 // TODO(perkj): Take |constraints| into consideration. Return false if not all
568 // mandatory constraints can be fulfilled. Note that |constraints|
569 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000571
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000572 if (options.disable_encryption) {
573 dtls_enabled_ = false;
574 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200575 // Enable DTLS by default if we have an identity store or a certificate.
576 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000577 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700578 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
579 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000580 dtls_enabled_ = value;
581 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000582 }
583
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000585 // It takes precendence over the disable_sctp_data_channels
586 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 if (FindConstraint(
588 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
589 &value, NULL) && value) {
590 LOG(LS_INFO) << "Allowing RTP data engine.";
591 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000592 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000593 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000594 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000595 LOG(LS_INFO) << "Allowing SCTP data engine.";
596 data_channel_type_ = cricket::DCT_SCTP;
597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000600 SetOptionFromOptionalConstraint(constraints,
601 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800602 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000603
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000604 SetOptionFromOptionalConstraint(constraints,
605 MediaConstraintsInterface::kCombinedAudioVideoBwe,
606 &audio_options_.combined_audio_video_bwe);
607
kwiberg102c6a62015-10-30 02:47:38 -0700608 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100609 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200610
Karl Wibergbe579832015-11-10 22:34:18 +0100611 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
612 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200613
Henrik Boström87713d02015-08-25 09:53:21 +0200614 if (!dtls_enabled_) {
615 // Construct with DTLS disabled.
616 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700617 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200618 } else {
619 // Construct with DTLS enabled.
620 if (!certificate) {
621 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700622 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200623 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800624 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700625 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200626 } else {
627 // Use the already generated certificate.
628 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700629 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200630 }
631 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632
Henrik Boströmd8281982015-08-27 10:12:24 +0200633 webrtc_session_desc_factory_->SignalCertificateReady.connect(
634 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000635
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000637 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000638 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000639 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200640 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 return true;
643}
644
deadbeefd59daf82015-10-14 15:02:44 -0700645void WebRtcSession::Close() {
646 SetState(STATE_CLOSED);
647 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000648 ASSERT(!voice_channel_);
649 ASSERT(!video_channel_);
650 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800651 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652}
653
deadbeef0ed85b22016-02-23 17:24:52 -0800654cricket::BaseChannel* WebRtcSession::GetChannel(
655 const std::string& content_name) {
656 if (voice_channel() && voice_channel()->content_name() == content_name) {
657 return voice_channel();
658 }
659 if (video_channel() && video_channel()->content_name() == content_name) {
660 return video_channel();
661 }
662 if (data_channel() && data_channel()->content_name() == content_name) {
663 return data_channel();
664 }
665 return nullptr;
666}
667
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000668void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
669 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670}
671
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000672cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
673 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674}
675
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800676bool WebRtcSession::GetSslRole(const std::string& transport_name,
677 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700678 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000679 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
680 << "SSL Role of the session.";
681 return false;
682 }
683
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800684 return transport_controller_->GetSslRole(transport_name, role);
685}
686
687bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
688 rtc::SSLRole* role) {
689 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000690}
691
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000692void WebRtcSession::CreateOffer(
693 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700694 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
695 const cricket::MediaSessionOptions& session_options) {
696 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000697}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698
deadbeefab9b2d12015-10-14 11:33:11 -0700699void WebRtcSession::CreateAnswer(
700 CreateSessionDescriptionObserver* observer,
701 const MediaConstraintsInterface* constraints,
702 const cricket::MediaSessionOptions& session_options) {
703 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
704 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705}
706
707bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
708 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700709 ASSERT(signaling_thread()->IsCurrent());
710
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000711 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000712 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000713
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000714 // Validate SDP.
715 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
716 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718
deadbeefd59daf82015-10-14 15:02:44 -0700719 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000720 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700722 initial_offerer_ = true;
723 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000726 cricket::SecurePolicy sdes_policy =
727 webrtc_session_desc_factory_->SdesPolicy();
728 cricket::CryptoType crypto_required = dtls_enabled_ ?
729 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
730 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000732 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000734 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
736 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000737 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 // TODO(mallinath) - Handle CreateChannel failure, as new local description
739 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000740 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 }
742
deadbeefcbecd352015-09-23 11:50:27 -0700743 // Remove unused channels if MediaContentDescription is rejected.
744 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000746 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return false;
748 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000749
deadbeefd59daf82015-10-14 15:02:44 -0700750 if (remote_desc_) {
751 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700752 UseCandidatesInSessionDescription(remote_desc_.get());
753 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
deadbeef0ed85b22016-02-23 17:24:52 -0800755 pending_ice_restarts_.clear();
756
deadbeefd59daf82015-10-14 15:02:44 -0700757 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000758 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 return true;
761}
762
763bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
764 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700765 ASSERT(signaling_thread()->IsCurrent());
766
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000767 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000768 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000769
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000770 // Validate SDP.
771 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
772 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774
deadbeefd59daf82015-10-14 15:02:44 -0700775 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
776 remote_desc_.release());
777 remote_desc_.reset(desc_temp.release());
778
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000780 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 if (action == kOffer && !CreateChannels(desc->description())) {
782 // TODO(mallinath) - Handle CreateChannel failure, as new local description
783 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000784 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 }
786
deadbeefcbecd352015-09-23 11:50:27 -0700787 // Remove unused channels if MediaContentDescription is rejected.
788 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789
790 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
791 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000792 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 return false;
794 }
795
deadbeefd59daf82015-10-14 15:02:44 -0700796 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000797 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
deadbeef0ed85b22016-02-23 17:24:52 -0800800 if (old_remote_desc) {
801 for (const cricket::ContentInfo& content :
802 old_remote_desc->description()->contents()) {
803 // Check if this new SessionDescription contains new ICE ufrag and
804 // password that indicates the remote peer requests an ICE restart.
805 // TODO(deadbeef): When we start storing both the current and pending
806 // remote description, this should reset pending_ice_restarts and compare
807 // against the current description.
808 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
809 if (action == kOffer) {
810 pending_ice_restarts_.insert(content.name);
811 }
812 } else {
813 // We retain all received candidates only if ICE is not restarted.
814 // When ICE is restarted, all previous candidates belong to an old
815 // generation and should not be kept.
816 // TODO(deadbeef): This goes against the W3C spec which says the remote
817 // description should only contain candidates from the last set remote
818 // description plus any candidates added since then. We should remove
819 // this once we're sure it won't break anything.
820 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
821 old_remote_desc.get(), content.name, desc);
822 }
823 }
honghaiz503726c2015-07-31 12:37:38 -0700824 }
825
deadbeefd59daf82015-10-14 15:02:44 -0700826 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000827 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000829
830 // Set the the ICE connection state to connecting since the connection may
831 // become writable with peer reflexive candidates before any remote candidate
832 // is signaled.
833 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
834 // is to have a new signal the indicates a change in checking state from the
835 // transport and expose a new checking() member from transport that can be
836 // read to determine the current checking state. The existing SignalConnecting
837 // actually means "gathering candidates", so cannot be be used here.
838 if (desc->type() != SessionDescriptionInterface::kOffer &&
839 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
840 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
841 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 return true;
843}
844
deadbeefd59daf82015-10-14 15:02:44 -0700845void WebRtcSession::LogState(State old_state, State new_state) {
846 LOG(LS_INFO) << "Session:" << id()
847 << " Old state:" << GetStateString(old_state)
848 << " New state:" << GetStateString(new_state);
849}
850
851void WebRtcSession::SetState(State state) {
852 ASSERT(signaling_thread_->IsCurrent());
853 if (state != state_) {
854 LogState(state_, state);
855 state_ = state;
856 SignalState(this, state_);
857 }
858}
859
860void WebRtcSession::SetError(Error error, const std::string& error_desc) {
861 ASSERT(signaling_thread_->IsCurrent());
862 if (error != error_) {
863 error_ = error;
864 error_desc_ = error_desc;
865 }
866}
867
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868bool WebRtcSession::UpdateSessionState(
869 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700871 ASSERT(signaling_thread()->IsCurrent());
872
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 // If there's already a pending error then no state transition should happen.
874 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700875 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000876 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
879 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
deadbeefd59daf82015-10-14 15:02:44 -0700881 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
882 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000883 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700884 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000885 }
deadbeefd59daf82015-10-14 15:02:44 -0700886 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000887 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000890 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
891 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700894 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
895 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000896 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700897 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000898 }
deadbeefd59daf82015-10-14 15:02:44 -0700899 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000900 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700903 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700904 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700905 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700906 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700907 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800908 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700909 const cricket::ContentGroup* answer_bundle =
910 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
911 if (!EnableBundle(*answer_bundle)) {
912 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
913 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
914 }
915 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800916 // Only push down the transport description after enabling BUNDLE; we don't
917 // want to push down a description on a transport about to be destroyed.
918 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
919 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700922 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000923 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700924 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000925 }
deadbeefd59daf82015-10-14 15:02:44 -0700926 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000927 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929 }
930 return true;
931}
932
933WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
934 if (type == SessionDescriptionInterface::kOffer) {
935 return WebRtcSession::kOffer;
936 } else if (type == SessionDescriptionInterface::kPrAnswer) {
937 return WebRtcSession::kPrAnswer;
938 } else if (type == SessionDescriptionInterface::kAnswer) {
939 return WebRtcSession::kAnswer;
940 }
941 ASSERT(false && "unknown action type");
942 return WebRtcSession::kOffer;
943}
944
deadbeefd59daf82015-10-14 15:02:44 -0700945bool WebRtcSession::PushdownMediaDescription(
946 cricket::ContentAction action,
947 cricket::ContentSource source,
948 std::string* err) {
949 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
950 if (!ch) {
951 return true;
952 } else if (source == cricket::CS_LOCAL) {
953 return ch->PushdownLocalDescription(local_desc_->description(), action,
954 err);
955 } else {
956 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
957 err);
958 }
959 };
960
961 return (set_content(voice_channel()) &&
962 set_content(video_channel()) &&
963 set_content(data_channel()));
964}
965
966bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
967 cricket::ContentAction action,
968 std::string* error_desc) {
969 RTC_DCHECK(signaling_thread()->IsCurrent());
970
971 if (source == cricket::CS_LOCAL) {
972 return PushdownLocalTransportDescription(local_desc_->description(), action,
973 error_desc);
974 }
975 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
976 error_desc);
977}
978
979bool WebRtcSession::PushdownLocalTransportDescription(
980 const SessionDescription* sdesc,
981 cricket::ContentAction action,
982 std::string* err) {
983 RTC_DCHECK(signaling_thread()->IsCurrent());
984
985 if (!sdesc) {
986 return false;
987 }
988
989 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
990 if (!transport_controller_->SetLocalTransportDescription(
991 tinfo.content_name, tinfo.description, action, err)) {
992 return false;
993 }
994 }
995
996 return true;
997}
998
999bool WebRtcSession::PushdownRemoteTransportDescription(
1000 const SessionDescription* sdesc,
1001 cricket::ContentAction action,
1002 std::string* err) {
1003 RTC_DCHECK(signaling_thread()->IsCurrent());
1004
1005 if (!sdesc) {
1006 return false;
1007 }
1008
1009 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1010 if (!transport_controller_->SetRemoteTransportDescription(
1011 tinfo.content_name, tinfo.description, action, err)) {
1012 return false;
1013 }
1014 }
1015
1016 return true;
1017}
1018
1019bool WebRtcSession::GetTransportDescription(
1020 const SessionDescription* description,
1021 const std::string& content_name,
1022 cricket::TransportDescription* tdesc) {
1023 if (!description || !tdesc) {
1024 return false;
1025 }
1026 const TransportInfo* transport_info =
1027 description->GetTransportInfoByName(content_name);
1028 if (!transport_info) {
1029 return false;
1030 }
1031 *tdesc = transport_info->description;
1032 return true;
1033}
1034
1035bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001036 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001037 return (GetChannelTransportStats(voice_channel(), stats) &&
1038 GetChannelTransportStats(video_channel(), stats) &&
1039 GetChannelTransportStats(data_channel(), stats));
1040}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001041
deadbeefcbecd352015-09-23 11:50:27 -07001042bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001043 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001044 ASSERT(signaling_thread()->IsCurrent());
1045 if (!ch) {
1046 // Not using this channel.
1047 return true;
1048 }
1049
1050 const std::string& content_name = ch->content_name();
1051 const std::string& transport_name = ch->transport_name();
1052 stats->proxy_to_transport[content_name] = transport_name;
1053 if (stats->transport_stats.find(transport_name) !=
1054 stats->transport_stats.end()) {
1055 // Transport stats already done for this transport.
1056 return true;
1057 }
1058
1059 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001060 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001061 return false;
1062 }
1063
1064 stats->transport_stats[transport_name] = tstats;
1065 return true;
1066}
1067
1068bool WebRtcSession::GetLocalCertificate(
1069 const std::string& transport_name,
1070 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1071 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001072 return transport_controller_->GetLocalCertificate(transport_name,
1073 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001074}
1075
1076bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1077 rtc::SSLCertificate** cert) {
1078 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001079 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001080}
1081
deadbeefcbecd352015-09-23 11:50:27 -07001082bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1083 const std::string* first_content_name = bundle.FirstContentName();
1084 if (!first_content_name) {
1085 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1086 return false;
1087 }
1088 const std::string& transport_name = *first_content_name;
1089 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1090
1091 auto maybe_set_transport = [this, bundle, transport_name,
1092 first_channel](cricket::BaseChannel* ch) {
1093 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001094 return true;
1095 }
1096
deadbeefcbecd352015-09-23 11:50:27 -07001097 if (ch->transport_name() == transport_name) {
1098 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1099 << " on " << transport_name << ".";
1100 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001101 }
torbjornga81a42f2015-09-23 02:16:58 -07001102
deadbeefcbecd352015-09-23 11:50:27 -07001103 if (!ch->SetTransport(transport_name)) {
1104 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1105 return false;
1106 }
1107 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1108 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001109 return true;
1110 };
1111
deadbeefcbecd352015-09-23 11:50:27 -07001112 if (!maybe_set_transport(voice_channel()) ||
1113 !maybe_set_transport(video_channel()) ||
1114 !maybe_set_transport(data_channel())) {
1115 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001116 }
deadbeefcbecd352015-09-23 11:50:27 -07001117
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001118 return true;
1119}
1120
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001122 if (!remote_desc_) {
1123 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1124 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 return false;
1126 }
1127
1128 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001129 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 return false;
1131 }
1132
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001133 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001134 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1135 if (!valid) {
1136 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 }
1138
1139 // Add this candidate to the remote session description.
1140 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001141 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 return false;
1143 }
1144
deadbeefd59daf82015-10-14 15:02:44 -07001145 if (ready) {
1146 return UseCandidate(candidate);
1147 } else {
1148 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1149 return true;
1150 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151}
1152
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001153bool WebRtcSession::SetIceTransports(
1154 PeerConnectionInterface::IceTransportsType type) {
1155 return port_allocator()->set_candidate_filter(
1156 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001157}
1158
deadbeefd59daf82015-10-14 15:02:44 -07001159cricket::IceConfig WebRtcSession::ParseIceConfig(
1160 const PeerConnectionInterface::RTCConfiguration& config) const {
1161 cricket::IceConfig ice_config;
1162 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001163 ice_config.prioritize_most_likely_candidate_pairs =
1164 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001165 ice_config.backup_connection_ping_interval =
1166 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001167 ice_config.gather_continually = (config.continual_gathering_policy ==
1168 PeerConnectionInterface::GATHER_CONTINUALLY);
1169 return ice_config;
1170}
1171
1172void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1173 transport_controller_->SetIceConfig(config);
1174}
1175
1176void WebRtcSession::MaybeStartGathering() {
1177 transport_controller_->MaybeStartGathering();
1178}
1179
Peter Boström0c4e06b2015-10-07 12:23:21 +02001180bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1181 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001182 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001184 }
1185 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186}
1187
Peter Boström0c4e06b2015-10-07 12:23:21 +02001188bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1189 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001190 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001191 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001192 }
1193 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194}
1195
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 return desc.str();
1200}
1201
solenbergd4cec0d2015-10-09 08:55:48 -07001202void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 ASSERT(signaling_thread()->IsCurrent());
1204 if (!voice_channel_) {
1205 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1206 return;
1207 }
solenberg4bac9c52015-10-09 02:32:53 -07001208 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1209 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 // otherwise. This in the normal case when the underlying media channel has
1211 // already been deleted.
1212 ASSERT(enable == false);
1213 }
1214}
1215
Peter Boström0c4e06b2015-10-07 12:23:21 +02001216void WebRtcSession::SetAudioSend(uint32_t ssrc,
1217 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001218 const cricket::AudioOptions& options,
1219 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ASSERT(signaling_thread()->IsCurrent());
1221 if (!voice_channel_) {
1222 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1223 return;
1224 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001225 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001226 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228}
1229
Peter Boström0c4e06b2015-10-07 12:23:21 +02001230void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001231 ASSERT(signaling_thread()->IsCurrent());
1232 ASSERT(volume >= 0 && volume <= 10);
1233 if (!voice_channel_) {
1234 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1235 return;
1236 }
1237
solenberg4bac9c52015-10-09 02:32:53 -07001238 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001239 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001240 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001241}
1242
deadbeef2d110be2016-01-13 12:00:26 -08001243void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1244 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001245 ASSERT(signaling_thread()->IsCurrent());
1246 if (!voice_channel_)
1247 return;
1248
deadbeef2d110be2016-01-13 12:00:26 -08001249 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001250}
1251
Peter Boström0c4e06b2015-10-07 12:23:21 +02001252bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 cricket::VideoCapturer* camera) {
1254 ASSERT(signaling_thread()->IsCurrent());
1255
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001256 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1258 // support video.
1259 LOG(LS_WARNING) << "Video not used in this call.";
1260 return false;
1261 }
1262 if (!video_channel_->SetCapturer(ssrc, camera)) {
1263 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1264 // This in the normal case when the underlying media channel has already
1265 // been deleted.
1266 ASSERT(camera == NULL);
1267 return false;
1268 }
1269 return true;
1270}
1271
nisse08582ff2016-02-04 01:24:52 -08001272void WebRtcSession::SetVideoPlayout(
1273 uint32_t ssrc,
1274 bool enable,
1275 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 ASSERT(signaling_thread()->IsCurrent());
1277 if (!video_channel_) {
1278 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1279 return;
1280 }
nisse08582ff2016-02-04 01:24:52 -08001281 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1282 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 // This in the normal case when the underlying media channel has already
1284 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001285 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 }
1287}
1288
Peter Boström0c4e06b2015-10-07 12:23:21 +02001289void WebRtcSession::SetVideoSend(uint32_t ssrc,
1290 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 const cricket::VideoOptions* options) {
1292 ASSERT(signaling_thread()->IsCurrent());
1293 if (!video_channel_) {
1294 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1295 return;
1296 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001297 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1299 // This in the normal case when the underlying media channel has already
1300 // been deleted.
1301 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303}
1304
1305bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1306 ASSERT(signaling_thread()->IsCurrent());
1307 if (!voice_channel_) {
1308 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1309 return false;
1310 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001311 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1313 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001314 if (!local_desc_ ||
1315 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 &send_ssrc)) {
1317 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1318 return false;
1319 }
1320 return voice_channel_->CanInsertDtmf();
1321}
1322
1323bool WebRtcSession::InsertDtmf(const std::string& track_id,
1324 int code, int duration) {
1325 ASSERT(signaling_thread()->IsCurrent());
1326 if (!voice_channel_) {
1327 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1328 return false;
1329 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001330 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001331 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1332 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1334 return false;
1335 }
solenberg1d63dd02015-12-02 12:35:09 -08001336 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1338 return false;
1339 }
1340 return true;
1341}
1342
1343sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001344 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345}
1346
wu@webrtc.org78187522013-10-07 23:32:02 +00001347bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001348 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001349 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001350 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001351 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1352 return false;
1353 }
1354 return data_channel_->SendData(params, payload, result);
1355}
1356
1357bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001358 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001359 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1360 return false;
1361 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001362 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1363 &DataChannel::OnChannelReady);
1364 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1365 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001366 data_channel_->SignalStreamClosedRemotely.connect(
1367 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001368 return true;
1369}
1370
1371void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001372 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001373 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1374 return;
1375 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001376 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1377 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001378 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001379}
1380
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001381void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001382 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001383 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1384 return;
1385 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001386 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1387 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001388}
1389
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001390void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001391 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001392 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1393 << "NULL.";
1394 return;
1395 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001396 data_channel_->RemoveRecvStream(sid);
1397 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001398}
1399
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001400bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001401 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001402}
1403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404cricket::DataChannelType WebRtcSession::data_channel_type() const {
1405 return data_channel_type_;
1406}
1407
deadbeef0ed85b22016-02-23 17:24:52 -08001408bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1409 return pending_ice_restarts_.find(content_name) !=
1410 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001411}
1412
Henrik Boströmd8281982015-08-27 10:12:24 +02001413void WebRtcSession::OnCertificateReady(
1414 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001415 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001416}
1417
Henrik Boströmd8281982015-08-27 10:12:24 +02001418bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001419 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001420}
1421
deadbeefcbecd352015-09-23 11:50:27 -07001422const rtc::scoped_refptr<rtc::RTCCertificate>&
1423WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001424 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001425}
1426
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427void WebRtcSession::SetIceConnectionState(
1428 PeerConnectionInterface::IceConnectionState state) {
1429 if (ice_connection_state_ == state) {
1430 return;
1431 }
1432
1433 // ASSERT that the requested transition is allowed. Note that
1434 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1435 // within PeerConnection). This switch statement should compile away when
1436 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001437 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1438 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 switch (ice_connection_state_) {
1440 case PeerConnectionInterface::kIceConnectionNew:
1441 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1442 break;
1443 case PeerConnectionInterface::kIceConnectionChecking:
1444 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1445 state == PeerConnectionInterface::kIceConnectionConnected);
1446 break;
1447 case PeerConnectionInterface::kIceConnectionConnected:
1448 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1449 state == PeerConnectionInterface::kIceConnectionChecking ||
1450 state == PeerConnectionInterface::kIceConnectionCompleted);
1451 break;
1452 case PeerConnectionInterface::kIceConnectionCompleted:
1453 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1454 state == PeerConnectionInterface::kIceConnectionDisconnected);
1455 break;
1456 case PeerConnectionInterface::kIceConnectionFailed:
1457 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1458 break;
1459 case PeerConnectionInterface::kIceConnectionDisconnected:
1460 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1461 state == PeerConnectionInterface::kIceConnectionConnected ||
1462 state == PeerConnectionInterface::kIceConnectionCompleted ||
1463 state == PeerConnectionInterface::kIceConnectionFailed);
1464 break;
1465 case PeerConnectionInterface::kIceConnectionClosed:
1466 ASSERT(false);
1467 break;
1468 default:
1469 ASSERT(false);
1470 break;
1471 }
1472
1473 ice_connection_state_ = state;
1474 if (ice_observer_) {
1475 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1476 }
1477}
1478
deadbeefcbecd352015-09-23 11:50:27 -07001479void WebRtcSession::OnTransportControllerConnectionState(
1480 cricket::IceConnectionState state) {
1481 switch (state) {
1482 case cricket::kIceConnectionConnecting:
1483 // If the current state is Connected or Completed, then there were
1484 // writable channels but now there are not, so the next state must
1485 // be Disconnected.
1486 // kIceConnectionConnecting is currently used as the default,
1487 // un-connected state by the TransportController, so its only use is
1488 // detecting disconnections.
1489 if (ice_connection_state_ ==
1490 PeerConnectionInterface::kIceConnectionConnected ||
1491 ice_connection_state_ ==
1492 PeerConnectionInterface::kIceConnectionCompleted) {
1493 SetIceConnectionState(
1494 PeerConnectionInterface::kIceConnectionDisconnected);
1495 }
torbjornga81a42f2015-09-23 02:16:58 -07001496 break;
deadbeefcbecd352015-09-23 11:50:27 -07001497 case cricket::kIceConnectionFailed:
1498 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1499 break;
1500 case cricket::kIceConnectionConnected:
1501 LOG(LS_INFO) << "Changing to ICE connected state because "
1502 << "all transports are writable.";
1503 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1504 break;
1505 case cricket::kIceConnectionCompleted:
1506 LOG(LS_INFO) << "Changing to ICE completed state because "
1507 << "all transports are complete.";
1508 if (ice_connection_state_ !=
1509 PeerConnectionInterface::kIceConnectionConnected) {
1510 // If jumping directly from "checking" to "connected",
1511 // signal "connected" first.
1512 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1513 }
1514 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1515 if (metrics_observer_) {
1516 ReportTransportStats();
1517 }
1518 break;
1519 default:
1520 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001521 }
deadbeefcbecd352015-09-23 11:50:27 -07001522}
1523
1524void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001525 SetIceConnectionReceiving(receiving);
1526}
1527
1528void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1529 if (ice_connection_receiving_ == receiving) {
1530 return;
1531 }
1532 ice_connection_receiving_ = receiving;
1533 if (ice_observer_) {
1534 ice_observer_->OnIceConnectionReceivingChange(receiving);
1535 }
1536}
1537
deadbeefcbecd352015-09-23 11:50:27 -07001538void WebRtcSession::OnTransportControllerCandidatesGathered(
1539 const std::string& transport_name,
1540 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001542 int sdp_mline_index;
1543 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1544 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1545 << transport_name << " not found";
1546 return;
1547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548
deadbeefcbecd352015-09-23 11:50:27 -07001549 for (cricket::Candidates::const_iterator citer = candidates.begin();
1550 citer != candidates.end(); ++citer) {
1551 // Use transport_name as the candidate media id.
1552 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1553 if (ice_observer_) {
1554 ice_observer_->OnIceCandidate(&candidate);
1555 }
1556 if (local_desc_) {
1557 local_desc_->AddCandidate(&candidate);
1558 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 }
1560}
1561
1562// Enabling voice and video channel.
1563void WebRtcSession::EnableChannels() {
1564 if (voice_channel_ && !voice_channel_->enabled())
1565 voice_channel_->Enable(true);
1566
1567 if (video_channel_ && !video_channel_->enabled())
1568 video_channel_->Enable(true);
1569
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001570 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 data_channel_->Enable(true);
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574// Returns the media index for a local ice candidate given the content name.
1575bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1576 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001577 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580
1581 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001582 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 for (size_t index = 0; index < contents.size(); ++index) {
1584 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001585 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 content_found = true;
1587 break;
1588 }
1589 }
1590 return content_found;
1591}
1592
1593bool WebRtcSession::UseCandidatesInSessionDescription(
1594 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001595 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001599
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1601 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001602 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001603 const IceCandidateInterface* candidate = candidates->at(n);
1604 bool valid = false;
1605 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1606 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001607 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1608 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001609 }
1610 continue;
1611 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001612 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001613 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 break;
deadbeefd59daf82015-10-14 15:02:44 -07001615 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 }
1617 }
1618 return ret;
1619}
1620
1621bool WebRtcSession::UseCandidate(
1622 const IceCandidateInterface* candidate) {
1623
1624 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001625 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 if (mediacontent_index >= remote_content_size) {
1627 LOG(LS_ERROR)
1628 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1629 return false;
1630 }
1631
1632 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001633 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 std::vector<cricket::Candidate> candidates;
1635 candidates.push_back(candidate->candidate());
1636 // Invoking BaseSession method to handle remote candidates.
1637 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001638 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1639 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 // Candidates successfully submitted for checking.
1641 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1642 ice_connection_state_ ==
1643 PeerConnectionInterface::kIceConnectionDisconnected) {
1644 // If state is New, then the session has just gotten its first remote ICE
1645 // candidates, so go to Checking.
1646 // If state is Disconnected, the session is re-using old candidates or
1647 // receiving additional ones, so go to Checking.
1648 // If state is Connected, stay Connected.
1649 // TODO(bemasc): If state is Connected, and the new candidates are for a
1650 // newly added transport, then the state actually _should_ move to
1651 // checking. Add a way to distinguish that case.
1652 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1653 }
1654 // TODO(bemasc): If state is Completed, go back to Connected.
1655 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001656 if (!error.empty()) {
1657 LOG(LS_WARNING) << error;
1658 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 }
1660 return true;
1661}
1662
deadbeefcbecd352015-09-23 11:50:27 -07001663void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001664 // Destroy video_channel_ first since it may have a pointer to the
1665 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 const cricket::ContentInfo* video_info =
1667 cricket::GetFirstVideoContent(desc);
1668 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 }
1672
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001673 const cricket::ContentInfo* voice_info =
1674 cricket::GetFirstAudioContent(desc);
1675 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001676 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001677 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001678 }
1679
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 const cricket::ContentInfo* data_info =
1681 cricket::GetFirstDataContent(desc);
1682 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 }
1686}
1687
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001688// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001689// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 // Creating the media channels and transport proxies.
1692 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1693 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001694 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 LOG(LS_ERROR) << "Failed to create voice channel.";
1696 return false;
1697 }
1698 }
1699
1700 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1701 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001702 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 LOG(LS_ERROR) << "Failed to create video channel.";
1704 return false;
1705 }
1706 }
1707
1708 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1709 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001710 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001711 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 LOG(LS_ERROR) << "Failed to create data channel.";
1713 return false;
1714 }
1715 }
1716
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001717 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1718 if (voice_channel()) {
1719 voice_channel()->ActivateRtcpMux();
1720 }
1721 if (video_channel()) {
1722 video_channel()->ActivateRtcpMux();
1723 }
1724 if (data_channel()) {
1725 data_channel()->ActivateRtcpMux();
1726 }
1727 }
1728
deadbeefcbecd352015-09-23 11:50:27 -07001729 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001730 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001731 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1732 cricket::GROUP_TYPE_BUNDLE);
1733 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001734 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1735 return false;
1736 }
deadbeefcbecd352015-09-23 11:50:27 -07001737 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001738 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1739 return false;
1740 }
1741 }
1742
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 return true;
1744}
1745
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001746bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001748 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001749 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001750 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001751 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001752 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001753
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001754 voice_channel_->SignalDtlsSetupFailure.connect(
1755 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001756
1757 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001758 voice_channel_->transport_channel()->SignalSentPacket.connect(
1759 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001760 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761}
1762
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001763bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001765 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001766 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001767 if (!video_channel_) {
1768 return false;
1769 }
1770
1771 video_channel_->SignalDtlsSetupFailure.connect(
1772 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001773
1774 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001775 video_channel_->transport_channel()->SignalSentPacket.connect(
1776 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001777 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778}
1779
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001780bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001781 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001783 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001784 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001785 return false;
1786 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001787
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001788 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001789 data_channel_->SignalDataReceived.connect(
1790 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001791 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001792
1793 data_channel_->SignalDtlsSetupFailure.connect(
1794 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001795
1796 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001797 data_channel_->transport_channel()->SignalSentPacket.connect(
1798 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001799 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800}
1801
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001802void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001803 SetError(ERROR_TRANSPORT,
1804 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805}
1806
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001807void WebRtcSession::OnDataChannelMessageReceived(
1808 cricket::DataChannel* channel,
1809 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001810 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001811 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1812 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1813 // Received OPEN message; parse and signal that a new data channel should
1814 // be created.
1815 std::string label;
1816 InternalDataChannelInit config;
1817 config.id = params.ssrc;
1818 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1819 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1820 << params.ssrc;
1821 return;
1822 }
1823 config.open_handshake_role = InternalDataChannelInit::kAcker;
1824 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 }
deadbeefab9b2d12015-10-14 11:33:11 -07001826 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827}
1828
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001829// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001830bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001831 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1832 if (!bundle_enabled)
1833 return true;
1834
1835 const cricket::ContentGroup* bundle_group =
1836 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1837 ASSERT(bundle_group != NULL);
1838
1839 const cricket::ContentInfos& contents = desc->contents();
1840 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1841 citer != contents.end(); ++citer) {
1842 const cricket::ContentInfo* content = (&*citer);
1843 ASSERT(content != NULL);
1844 if (bundle_group->HasContentName(content->name) &&
1845 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1846 if (!HasRtcpMuxEnabled(content))
1847 return false;
1848 }
1849 }
1850 // RTCP-MUX is enabled in all the contents.
1851 return true;
1852}
1853
1854bool WebRtcSession::HasRtcpMuxEnabled(
1855 const cricket::ContentInfo* content) {
1856 const cricket::MediaContentDescription* description =
1857 static_cast<cricket::MediaContentDescription*>(content->description);
1858 return description->rtcp_mux();
1859}
1860
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001861bool WebRtcSession::ValidateSessionDescription(
1862 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001863 cricket::ContentSource source, std::string* err_desc) {
1864 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001865 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001866 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001867 }
1868
1869 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001870 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001871 }
1872
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001873 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001874 Action action = GetAction(sdesc->type());
1875 if (source == cricket::CS_LOCAL) {
1876 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001877 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001878 } else {
1879 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001880 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001881 }
1882
1883 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001884 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001885 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1886 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001887 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001888 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001889 }
1890
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001891 // Verify ice-ufrag and ice-pwd.
1892 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001893 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001894 }
1895
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001896 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001897 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001898 }
1899
1900 // Verify m-lines in Answer when compared against Offer.
1901 if (action == kAnswer) {
1902 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001903 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1904 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001905 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001906 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001907 }
1908 }
1909
1910 return true;
1911}
1912
1913bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1914 return ((action == kOffer && state() == STATE_INIT) ||
1915 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001916 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001917 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001918 (action == kOffer && state() == STATE_INPROGRESS) ||
1919 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001920 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1921 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1922 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1923 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924}
1925
1926bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1927 return ((action == kOffer && state() == STATE_INIT) ||
1928 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001929 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001931 (action == kOffer && state() == STATE_INPROGRESS) ||
1932 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001933 (action == kAnswer && state() == STATE_SENTOFFER) ||
1934 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1935 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1936 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937}
1938
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001939std::string WebRtcSession::GetSessionErrorMsg() {
1940 std::ostringstream desc;
1941 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1942 desc << kSessionErrorDesc << error_desc() << ".";
1943 return desc.str();
1944}
1945
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001946// We need to check the local/remote description for the Transport instead of
1947// the session, because a new Transport added during renegotiation may have
1948// them unset while the session has them set from the previous negotiation.
1949// Not doing so may trigger the auto generation of transport description and
1950// mess up DTLS identity information, ICE credential, etc.
1951bool WebRtcSession::ReadyToUseRemoteCandidate(
1952 const IceCandidateInterface* candidate,
1953 const SessionDescriptionInterface* remote_desc,
1954 bool* valid) {
1955 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001956
1957 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001958 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001959
deadbeefd59daf82015-10-14 15:02:44 -07001960 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001961 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001962 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001963
1964 size_t mediacontent_index =
1965 static_cast<size_t>(candidate->sdp_mline_index());
1966 size_t remote_content_size =
1967 current_remote_desc->description()->contents().size();
1968 if (mediacontent_index >= remote_content_size) {
1969 LOG(LS_ERROR)
1970 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1971
1972 *valid = false;
1973 return false;
1974 }
1975
1976 cricket::ContentInfo content =
1977 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001978 cricket::BaseChannel* channel = GetChannel(content.name);
1979 if (!channel) {
1980 return false;
1981 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001982
deadbeefd59daf82015-10-14 15:02:44 -07001983 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001984 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001985}
1986
deadbeefcbecd352015-09-23 11:50:27 -07001987void WebRtcSession::OnTransportControllerGatheringState(
1988 cricket::IceGatheringState state) {
1989 ASSERT(signaling_thread()->IsCurrent());
1990 if (state == cricket::kIceGatheringGathering) {
1991 if (ice_observer_) {
1992 ice_observer_->OnIceGatheringChange(
1993 PeerConnectionInterface::kIceGatheringGathering);
1994 }
1995 } else if (state == cricket::kIceGatheringComplete) {
1996 if (ice_observer_) {
1997 ice_observer_->OnIceGatheringChange(
1998 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001999 }
2000 }
2001}
2002
2003void WebRtcSession::ReportTransportStats() {
2004 // Use a set so we don't report the same stats twice if two channels share
2005 // a transport.
2006 std::set<std::string> transport_names;
2007 if (voice_channel()) {
2008 transport_names.insert(voice_channel()->transport_name());
2009 }
2010 if (video_channel()) {
2011 transport_names.insert(video_channel()->transport_name());
2012 }
2013 if (data_channel()) {
2014 transport_names.insert(data_channel()->transport_name());
2015 }
2016 for (const auto& name : transport_names) {
2017 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002018 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002019 ReportBestConnectionState(stats);
2020 ReportNegotiatedCiphers(stats);
2021 }
2022 }
2023}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002024// Walk through the ConnectionInfos to gather best connection usage
2025// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002026void WebRtcSession::ReportBestConnectionState(
2027 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002028 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002029 for (cricket::TransportChannelStatsList::const_iterator it =
2030 stats.channel_stats.begin();
2031 it != stats.channel_stats.end(); ++it) {
2032 for (cricket::ConnectionInfos::const_iterator it_info =
2033 it->connection_infos.begin();
2034 it_info != it->connection_infos.end(); ++it_info) {
2035 if (!it_info->best_connection) {
2036 continue;
2037 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002038
2039 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2040 const cricket::Candidate& local = it_info->local_candidate;
2041 const cricket::Candidate& remote = it_info->remote_candidate;
2042
2043 // Increment the counter for IceCandidatePairType.
2044 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2045 (local.type() == RELAY_PORT_TYPE &&
2046 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2047 type = kEnumCounterIceCandidatePairTypeTcp;
2048 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2049 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002050 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002051 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002052 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002053 metrics_observer_->IncrementEnumCounter(
2054 type, GetIceCandidatePairCounter(local, remote),
2055 kIceCandidatePairMax);
2056
2057 // Increment the counter for IP type.
2058 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002059 metrics_observer_->IncrementEnumCounter(
2060 kEnumCounterAddressFamily, kBestConnections_IPv4,
2061 kPeerConnectionAddressFamilyCounter_Max);
2062
2063 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002064 metrics_observer_->IncrementEnumCounter(
2065 kEnumCounterAddressFamily, kBestConnections_IPv6,
2066 kPeerConnectionAddressFamilyCounter_Max);
2067 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002068 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002069 }
2070
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002071 return;
2072 }
2073 }
2074}
2075
jbauchac8869e2015-07-03 01:36:14 -07002076void WebRtcSession::ReportNegotiatedCiphers(
2077 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002078 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002079 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2080 return;
2081 }
2082
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002083 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2084 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2085 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2086 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002087 return;
2088 }
2089
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002090 PeerConnectionEnumCounterType srtp_counter_type;
2091 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002092 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002093 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2094 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002095 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002096 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2097 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002098 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002099 srtp_counter_type = kEnumCounterDataSrtpCipher;
2100 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002101 } else {
2102 RTC_NOTREACHED();
2103 return;
2104 }
2105
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002106 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2107 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2108 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002109 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002110 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2111 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2112 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002113 }
2114}
2115
stefanc1aeaf02015-10-15 07:26:07 -07002116void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2117 const rtc::SentPacket& sent_packet) {
2118 RTC_DCHECK(worker_thread()->IsCurrent());
2119 media_controller_->call_w()->OnSentPacket(sent_packet);
2120}
2121
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122} // namespace webrtc