blob: b2494140b4f863c6d4beeb77b185626531c9df53 [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"
kjellander7324eb92016-02-25 08:36:42 -080026#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/constants.h"
35#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::ContentInfo;
43using cricket::ContentInfos;
44using cricket::MediaContentDescription;
45using cricket::SessionDescription;
46using cricket::TransportInfo;
47
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070048using cricket::LOCAL_PORT_TYPE;
49using cricket::STUN_PORT_TYPE;
50using cricket::RELAY_PORT_TYPE;
51using cricket::PRFLX_PORT_TYPE;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053namespace webrtc {
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000056const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
61const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
63const char kPushDownTDFailed[] =
64 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000065const char kSdpWithoutDtlsFingerprint[] =
66 "Called with SDP without DTLS fingerprint.";
67const char kSdpWithoutSdesCrypto[] =
68 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000073const char kDtlsSetupFailureRtp[] =
74 "Couldn't set up DTLS-SRTP on RTP channel.";
75const char kDtlsSetupFailureRtcp[] =
76 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070077const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070079IceCandidatePairType GetIceCandidatePairCounter(
80 const cricket::Candidate& local,
81 const cricket::Candidate& remote) {
82 const auto& l = local.type();
83 const auto& r = remote.type();
84 const auto& host = LOCAL_PORT_TYPE;
85 const auto& srflx = STUN_PORT_TYPE;
86 const auto& relay = RELAY_PORT_TYPE;
87 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070088 if (l == host && r == host) {
89 bool local_private = IPIsPrivate(local.address().ipaddr());
90 bool remote_private = IPIsPrivate(remote.address().ipaddr());
91 if (local_private) {
92 if (remote_private) {
93 return kIceCandidatePairHostPrivateHostPrivate;
94 } else {
95 return kIceCandidatePairHostPrivateHostPublic;
96 }
97 } else {
98 if (remote_private) {
99 return kIceCandidatePairHostPublicHostPrivate;
100 } else {
101 return kIceCandidatePairHostPublicHostPublic;
102 }
103 }
104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700105 if (l == host && r == srflx)
106 return kIceCandidatePairHostSrflx;
107 if (l == host && r == relay)
108 return kIceCandidatePairHostRelay;
109 if (l == host && r == prflx)
110 return kIceCandidatePairHostPrflx;
111 if (l == srflx && r == host)
112 return kIceCandidatePairSrflxHost;
113 if (l == srflx && r == srflx)
114 return kIceCandidatePairSrflxSrflx;
115 if (l == srflx && r == relay)
116 return kIceCandidatePairSrflxRelay;
117 if (l == srflx && r == prflx)
118 return kIceCandidatePairSrflxPrflx;
119 if (l == relay && r == host)
120 return kIceCandidatePairRelayHost;
121 if (l == relay && r == srflx)
122 return kIceCandidatePairRelaySrflx;
123 if (l == relay && r == relay)
124 return kIceCandidatePairRelayRelay;
125 if (l == relay && r == prflx)
126 return kIceCandidatePairRelayPrflx;
127 if (l == prflx && r == host)
128 return kIceCandidatePairPrflxHost;
129 if (l == prflx && r == srflx)
130 return kIceCandidatePairPrflxSrflx;
131 if (l == prflx && r == relay)
132 return kIceCandidatePairPrflxRelay;
133 return kIceCandidatePairMax;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Compares |answer| against |offer|. Comparision is done
137// for number of m-lines in answer against offer. If matches true will be
138// returned otherwise false.
139static bool VerifyMediaDescriptions(
140 const SessionDescription* answer, const SessionDescription* offer) {
141 if (offer->contents().size() != answer->contents().size())
142 return false;
143
144 for (size_t i = 0; i < offer->contents().size(); ++i) {
145 if ((offer->contents()[i].name) != answer->contents()[i].name) {
146 return false;
147 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000148 const MediaContentDescription* offer_mdesc =
149 static_cast<const MediaContentDescription*>(
150 offer->contents()[i].description);
151 const MediaContentDescription* answer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 answer->contents()[i].description);
154 if (offer_mdesc->type() != answer_mdesc->type()) {
155 return false;
156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159}
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// Checks that each non-rejected content has SDES crypto keys or a DTLS
162// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
163// keys, will be caught in Transport negotiation, and backstopped by Channel's
164// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000165static bool VerifyCrypto(const SessionDescription* desc,
166 bool dtls_enabled,
167 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 const ContentInfos& contents = desc->contents();
169 for (size_t index = 0; index < contents.size(); ++index) {
170 const ContentInfo* cinfo = &contents[index];
171 if (cinfo->rejected) {
172 continue;
173 }
174
175 // If the content isn't rejected, crypto must be present.
176 const MediaContentDescription* media =
177 static_cast<const MediaContentDescription*>(cinfo->description);
178 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
179 if (!media || !tinfo) {
180 // Something is not right.
181 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return false;
184 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000185 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000187 LOG(LS_WARNING) <<
188 "Session description must have DTLS fingerprint if DTLS enabled.";
189 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 return false;
191 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 } else {
193 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 LOG(LS_WARNING) <<
195 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 return false;
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
200 }
201
202 return true;
203}
204
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000205// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
206static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
207 const ContentInfos& contents = desc->contents();
208 for (size_t index = 0; index < contents.size(); ++index) {
209 const ContentInfo* cinfo = &contents[index];
210 if (cinfo->rejected) {
211 continue;
212 }
213
214 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
215 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
216 if (!tinfo) {
217 // Something is not right.
218 LOG(LS_ERROR) << kInvalidSdp;
219 return false;
220 }
221 if (tinfo->description.ice_ufrag.empty() ||
222 tinfo->description.ice_pwd.empty()) {
223 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
224 return false;
225 }
226 }
227 return true;
228}
229
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230// Forces |sdesc->crypto_required| to the appropriate state based on the
231// current security policy, to ensure a failure occurs if there is an error
232// in crypto negotiation.
233// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000234static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
235 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 if (!sdesc) {
237 return;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 // Updating the |crypto_required_| in MediaContentDescription to the
241 // appropriate state based on the current security policy.
242 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
243 iter != sdesc->contents().end(); ++iter) {
244 if (cricket::IsMediaContent(&*iter)) {
245 MediaContentDescription* mdesc =
246 static_cast<MediaContentDescription*> (iter->description);
247 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 }
250 }
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
255 const std::string& track_id,
256 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 const cricket::ContentInfo* audio_info =
258 cricket::GetFirstAudioContent(session_description);
259 if (!audio_info) {
260 LOG(LS_ERROR) << "Audio not used in this call";
261 return false;
262 }
263
264 const cricket::MediaContentDescription* audio_content =
265 static_cast<const cricket::MediaContentDescription*>(
266 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000267 const cricket::StreamParams* stream =
268 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
269 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return false;
271 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272
273 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return true;
275}
276
277static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint32_t ssrc,
279 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 ASSERT(track_id != NULL);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 const cricket::ContentInfo* audio_info =
283 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000284 if (audio_info) {
285 const cricket::MediaContentDescription* audio_content =
286 static_cast<const cricket::MediaContentDescription*>(
287 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289 const auto* found =
290 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
291 if (found) {
292 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000293 return true;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
297 const cricket::ContentInfo* video_info =
298 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (video_info) {
300 const cricket::MediaContentDescription* video_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311 return false;
312}
313
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314static bool BadSdp(const std::string& source,
315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
318 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700319 desc << "Failed to set " << source;
320 if (!type.empty()) {
321 desc << " " << type;
322 }
323 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000326 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 return false;
330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 }
341}
342
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343static bool BadLocalSdp(const std::string& type,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
347}
348
349static bool BadRemoteSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
353}
354
355static bool BadOfferSdp(cricket::ContentSource source,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
359}
360
361static bool BadPranswerSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
365 reason, err_desc);
366}
367
368static bool BadAnswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
deadbeefd59daf82015-10-14 15:02:44 -0700374#define GET_STRING_OF_STATE(state) \
375 case webrtc::WebRtcSession::state: \
376 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 break;
378
deadbeefd59daf82015-10-14 15:02:44 -0700379static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 std::string result;
381 switch (state) {
382 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700383 GET_STRING_OF_STATE(STATE_SENTOFFER)
384 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
385 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
386 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
390 ASSERT(false);
391 break;
392 }
393 return result;
394}
395
deadbeefd59daf82015-10-14 15:02:44 -0700396#define GET_STRING_OF_ERROR_CODE(err) \
397 case webrtc::WebRtcSession::err: \
398 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 break;
400
deadbeefd59daf82015-10-14 15:02:44 -0700401static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 std::string result;
403 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
406 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
deadbeefd59daf82015-10-14 15:02:44 -0700408 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414static std::string MakeErrorString(const std::string& error,
415 const std::string& desc) {
416 std::ostringstream ret;
417 ret << error << " " << desc;
418 return ret.str();
419}
420
421static std::string MakeTdErrorString(const std::string& desc) {
422 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000425// Set |option| to the highest-priority value of |key| in the optional
426// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700427template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000428static void SetOptionFromOptionalConstraint(
429 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700430 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100431 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000432 if (!constraints) {
433 return;
434 }
435 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000436 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000437 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100439 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440 }
441 }
442}
443
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000445 PeerConnectionInterface::IceTransportsType type) {
446 switch (type) {
447 case PeerConnectionInterface::kNone:
448 return cricket::CF_NONE;
449 case PeerConnectionInterface::kRelay:
450 return cricket::CF_RELAY;
451 case PeerConnectionInterface::kNoHost:
452 return (cricket::CF_ALL & ~cricket::CF_HOST);
453 case PeerConnectionInterface::kAll:
454 return cricket::CF_ALL;
455 default: ASSERT(false);
456 }
457 return cricket::CF_NONE;
458}
459
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.org6e3dbc22014-03-25 17:09:47 +0000600 // Find Suspend Below Min Bitrate constraint.
601 if (FindConstraint(
602 constraints,
603 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
604 &value,
605 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100606 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000607 }
608
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000609 SetOptionFromOptionalConstraint(constraints,
610 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800611 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000612
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000613 SetOptionFromOptionalConstraint(constraints,
614 MediaConstraintsInterface::kCombinedAudioVideoBwe,
615 &audio_options_.combined_audio_video_bwe);
616
kwiberg102c6a62015-10-30 02:47:38 -0700617 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100618 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200619
Karl Wibergbe579832015-11-10 22:34:18 +0100620 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
621 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200622
Henrik Boström87713d02015-08-25 09:53:21 +0200623 if (!dtls_enabled_) {
624 // Construct with DTLS disabled.
625 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700626 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200627 } else {
628 // Construct with DTLS enabled.
629 if (!certificate) {
630 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700631 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200632 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800633 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700634 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200635 } else {
636 // Use the already generated certificate.
637 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700638 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200639 }
640 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000641
Henrik Boströmd8281982015-08-27 10:12:24 +0200642 webrtc_session_desc_factory_->SignalCertificateReady.connect(
643 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000644
wu@webrtc.org97077a32013-10-25 21:18:33 +0000645 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000646 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000647 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000648 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200649 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700650
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 return true;
652}
653
deadbeefd59daf82015-10-14 15:02:44 -0700654void WebRtcSession::Close() {
655 SetState(STATE_CLOSED);
656 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000657 ASSERT(!voice_channel_);
658 ASSERT(!video_channel_);
659 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660}
661
deadbeef0ed85b22016-02-23 17:24:52 -0800662cricket::BaseChannel* WebRtcSession::GetChannel(
663 const std::string& content_name) {
664 if (voice_channel() && voice_channel()->content_name() == content_name) {
665 return voice_channel();
666 }
667 if (video_channel() && video_channel()->content_name() == content_name) {
668 return video_channel();
669 }
670 if (data_channel() && data_channel()->content_name() == content_name) {
671 return data_channel();
672 }
673 return nullptr;
674}
675
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000676void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
677 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678}
679
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000680cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
681 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682}
683
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800684bool WebRtcSession::GetSslRole(const std::string& transport_name,
685 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700686 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000687 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
688 << "SSL Role of the session.";
689 return false;
690 }
691
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800692 return transport_controller_->GetSslRole(transport_name, role);
693}
694
695bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
696 rtc::SSLRole* role) {
697 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000698}
699
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000700void WebRtcSession::CreateOffer(
701 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700702 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
703 const cricket::MediaSessionOptions& session_options) {
704 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000705}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706
deadbeefab9b2d12015-10-14 11:33:11 -0700707void WebRtcSession::CreateAnswer(
708 CreateSessionDescriptionObserver* observer,
709 const MediaConstraintsInterface* constraints,
710 const cricket::MediaSessionOptions& session_options) {
711 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
712 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713}
714
715bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
716 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700717 ASSERT(signaling_thread()->IsCurrent());
718
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000719 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000720 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000721
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000722 // Validate SDP.
723 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
724 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726
deadbeefd59daf82015-10-14 15:02:44 -0700727 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000728 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700730 initial_offerer_ = true;
731 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 }
733
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000734 cricket::SecurePolicy sdes_policy =
735 webrtc_session_desc_factory_->SdesPolicy();
736 cricket::CryptoType crypto_required = dtls_enabled_ ?
737 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
738 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000740 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000742 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
744 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000745 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 // TODO(mallinath) - Handle CreateChannel failure, as new local description
747 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000748 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750
deadbeefcbecd352015-09-23 11:50:27 -0700751 // Remove unused channels if MediaContentDescription is rejected.
752 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000754 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return false;
756 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000757
deadbeefd59daf82015-10-14 15:02:44 -0700758 if (remote_desc_) {
759 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700760 UseCandidatesInSessionDescription(remote_desc_.get());
761 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
deadbeef0ed85b22016-02-23 17:24:52 -0800763 pending_ice_restarts_.clear();
764
deadbeefd59daf82015-10-14 15:02:44 -0700765 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000766 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768 return true;
769}
770
771bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
772 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700773 ASSERT(signaling_thread()->IsCurrent());
774
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000775 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000776 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000777
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000778 // Validate SDP.
779 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
780 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 }
782
deadbeefd59daf82015-10-14 15:02:44 -0700783 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
784 remote_desc_.release());
785 remote_desc_.reset(desc_temp.release());
786
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000788 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 if (action == kOffer && !CreateChannels(desc->description())) {
790 // TODO(mallinath) - Handle CreateChannel failure, as new local description
791 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000792 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794
deadbeefcbecd352015-09-23 11:50:27 -0700795 // Remove unused channels if MediaContentDescription is rejected.
796 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797
798 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
799 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000800 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 return false;
802 }
803
deadbeefd59daf82015-10-14 15:02:44 -0700804 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000805 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
deadbeef0ed85b22016-02-23 17:24:52 -0800808 if (old_remote_desc) {
809 for (const cricket::ContentInfo& content :
810 old_remote_desc->description()->contents()) {
811 // Check if this new SessionDescription contains new ICE ufrag and
812 // password that indicates the remote peer requests an ICE restart.
813 // TODO(deadbeef): When we start storing both the current and pending
814 // remote description, this should reset pending_ice_restarts and compare
815 // against the current description.
816 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
817 if (action == kOffer) {
818 pending_ice_restarts_.insert(content.name);
819 }
820 } else {
821 // We retain all received candidates only if ICE is not restarted.
822 // When ICE is restarted, all previous candidates belong to an old
823 // generation and should not be kept.
824 // TODO(deadbeef): This goes against the W3C spec which says the remote
825 // description should only contain candidates from the last set remote
826 // description plus any candidates added since then. We should remove
827 // this once we're sure it won't break anything.
828 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
829 old_remote_desc.get(), content.name, desc);
830 }
831 }
honghaiz503726c2015-07-31 12:37:38 -0700832 }
833
deadbeefd59daf82015-10-14 15:02:44 -0700834 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000835 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000837
838 // Set the the ICE connection state to connecting since the connection may
839 // become writable with peer reflexive candidates before any remote candidate
840 // is signaled.
841 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
842 // is to have a new signal the indicates a change in checking state from the
843 // transport and expose a new checking() member from transport that can be
844 // read to determine the current checking state. The existing SignalConnecting
845 // actually means "gathering candidates", so cannot be be used here.
846 if (desc->type() != SessionDescriptionInterface::kOffer &&
847 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
848 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
849 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 return true;
851}
852
deadbeefd59daf82015-10-14 15:02:44 -0700853void WebRtcSession::LogState(State old_state, State new_state) {
854 LOG(LS_INFO) << "Session:" << id()
855 << " Old state:" << GetStateString(old_state)
856 << " New state:" << GetStateString(new_state);
857}
858
859void WebRtcSession::SetState(State state) {
860 ASSERT(signaling_thread_->IsCurrent());
861 if (state != state_) {
862 LogState(state_, state);
863 state_ = state;
864 SignalState(this, state_);
865 }
866}
867
868void WebRtcSession::SetError(Error error, const std::string& error_desc) {
869 ASSERT(signaling_thread_->IsCurrent());
870 if (error != error_) {
871 error_ = error;
872 error_desc_ = error_desc;
873 }
874}
875
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876bool WebRtcSession::UpdateSessionState(
877 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700879 ASSERT(signaling_thread()->IsCurrent());
880
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 // If there's already a pending error then no state transition should happen.
882 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700883 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000886 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
887 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
deadbeefd59daf82015-10-14 15:02:44 -0700889 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
890 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000891 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700892 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000893 }
deadbeefd59daf82015-10-14 15:02:44 -0700894 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000895 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000898 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
899 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700902 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
903 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000904 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700905 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000906 }
deadbeefd59daf82015-10-14 15:02:44 -0700907 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000908 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700911 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700912 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700913 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700914 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700915 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800916 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700917 const cricket::ContentGroup* answer_bundle =
918 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
919 if (!EnableBundle(*answer_bundle)) {
920 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
921 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
922 }
923 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800924 // Only push down the transport description after enabling BUNDLE; we don't
925 // want to push down a description on a transport about to be destroyed.
926 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
927 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700930 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700932 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000933 }
deadbeefd59daf82015-10-14 15:02:44 -0700934 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000935 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937 }
938 return true;
939}
940
941WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
942 if (type == SessionDescriptionInterface::kOffer) {
943 return WebRtcSession::kOffer;
944 } else if (type == SessionDescriptionInterface::kPrAnswer) {
945 return WebRtcSession::kPrAnswer;
946 } else if (type == SessionDescriptionInterface::kAnswer) {
947 return WebRtcSession::kAnswer;
948 }
949 ASSERT(false && "unknown action type");
950 return WebRtcSession::kOffer;
951}
952
deadbeefd59daf82015-10-14 15:02:44 -0700953bool WebRtcSession::PushdownMediaDescription(
954 cricket::ContentAction action,
955 cricket::ContentSource source,
956 std::string* err) {
957 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
958 if (!ch) {
959 return true;
960 } else if (source == cricket::CS_LOCAL) {
961 return ch->PushdownLocalDescription(local_desc_->description(), action,
962 err);
963 } else {
964 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
965 err);
966 }
967 };
968
969 return (set_content(voice_channel()) &&
970 set_content(video_channel()) &&
971 set_content(data_channel()));
972}
973
974bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
975 cricket::ContentAction action,
976 std::string* error_desc) {
977 RTC_DCHECK(signaling_thread()->IsCurrent());
978
979 if (source == cricket::CS_LOCAL) {
980 return PushdownLocalTransportDescription(local_desc_->description(), action,
981 error_desc);
982 }
983 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
984 error_desc);
985}
986
987bool WebRtcSession::PushdownLocalTransportDescription(
988 const SessionDescription* sdesc,
989 cricket::ContentAction action,
990 std::string* err) {
991 RTC_DCHECK(signaling_thread()->IsCurrent());
992
993 if (!sdesc) {
994 return false;
995 }
996
997 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
998 if (!transport_controller_->SetLocalTransportDescription(
999 tinfo.content_name, tinfo.description, action, err)) {
1000 return false;
1001 }
1002 }
1003
1004 return true;
1005}
1006
1007bool WebRtcSession::PushdownRemoteTransportDescription(
1008 const SessionDescription* sdesc,
1009 cricket::ContentAction action,
1010 std::string* err) {
1011 RTC_DCHECK(signaling_thread()->IsCurrent());
1012
1013 if (!sdesc) {
1014 return false;
1015 }
1016
1017 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1018 if (!transport_controller_->SetRemoteTransportDescription(
1019 tinfo.content_name, tinfo.description, action, err)) {
1020 return false;
1021 }
1022 }
1023
1024 return true;
1025}
1026
1027bool WebRtcSession::GetTransportDescription(
1028 const SessionDescription* description,
1029 const std::string& content_name,
1030 cricket::TransportDescription* tdesc) {
1031 if (!description || !tdesc) {
1032 return false;
1033 }
1034 const TransportInfo* transport_info =
1035 description->GetTransportInfoByName(content_name);
1036 if (!transport_info) {
1037 return false;
1038 }
1039 *tdesc = transport_info->description;
1040 return true;
1041}
1042
1043bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001044 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001045 return (GetChannelTransportStats(voice_channel(), stats) &&
1046 GetChannelTransportStats(video_channel(), stats) &&
1047 GetChannelTransportStats(data_channel(), stats));
1048}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001049
deadbeefcbecd352015-09-23 11:50:27 -07001050bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001051 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001052 ASSERT(signaling_thread()->IsCurrent());
1053 if (!ch) {
1054 // Not using this channel.
1055 return true;
1056 }
1057
1058 const std::string& content_name = ch->content_name();
1059 const std::string& transport_name = ch->transport_name();
1060 stats->proxy_to_transport[content_name] = transport_name;
1061 if (stats->transport_stats.find(transport_name) !=
1062 stats->transport_stats.end()) {
1063 // Transport stats already done for this transport.
1064 return true;
1065 }
1066
1067 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001068 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001069 return false;
1070 }
1071
1072 stats->transport_stats[transport_name] = tstats;
1073 return true;
1074}
1075
1076bool WebRtcSession::GetLocalCertificate(
1077 const std::string& transport_name,
1078 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1079 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001080 return transport_controller_->GetLocalCertificate(transport_name,
1081 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001082}
1083
1084bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1085 rtc::SSLCertificate** cert) {
1086 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001087 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001088}
1089
deadbeefcbecd352015-09-23 11:50:27 -07001090bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1091 const std::string* first_content_name = bundle.FirstContentName();
1092 if (!first_content_name) {
1093 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1094 return false;
1095 }
1096 const std::string& transport_name = *first_content_name;
1097 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1098
1099 auto maybe_set_transport = [this, bundle, transport_name,
1100 first_channel](cricket::BaseChannel* ch) {
1101 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001102 return true;
1103 }
1104
deadbeefcbecd352015-09-23 11:50:27 -07001105 if (ch->transport_name() == transport_name) {
1106 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1107 << " on " << transport_name << ".";
1108 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001109 }
torbjornga81a42f2015-09-23 02:16:58 -07001110
deadbeefcbecd352015-09-23 11:50:27 -07001111 if (!ch->SetTransport(transport_name)) {
1112 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1113 return false;
1114 }
1115 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1116 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001117 return true;
1118 };
1119
deadbeefcbecd352015-09-23 11:50:27 -07001120 if (!maybe_set_transport(voice_channel()) ||
1121 !maybe_set_transport(video_channel()) ||
1122 !maybe_set_transport(data_channel())) {
1123 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001124 }
deadbeefcbecd352015-09-23 11:50:27 -07001125
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001126 return true;
1127}
1128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001130 if (!remote_desc_) {
1131 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1132 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 return false;
1134 }
1135
1136 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001137 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 return false;
1139 }
1140
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001141 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001142 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1143 if (!valid) {
1144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 }
1146
1147 // Add this candidate to the remote session description.
1148 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001149 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 return false;
1151 }
1152
deadbeefd59daf82015-10-14 15:02:44 -07001153 if (ready) {
1154 return UseCandidate(candidate);
1155 } else {
1156 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1157 return true;
1158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159}
1160
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001161bool WebRtcSession::SetIceTransports(
1162 PeerConnectionInterface::IceTransportsType type) {
1163 return port_allocator()->set_candidate_filter(
1164 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001165}
1166
deadbeefd59daf82015-10-14 15:02:44 -07001167cricket::IceConfig WebRtcSession::ParseIceConfig(
1168 const PeerConnectionInterface::RTCConfiguration& config) const {
1169 cricket::IceConfig ice_config;
1170 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001171 ice_config.backup_connection_ping_interval =
1172 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001173 ice_config.gather_continually = (config.continual_gathering_policy ==
1174 PeerConnectionInterface::GATHER_CONTINUALLY);
1175 return ice_config;
1176}
1177
1178void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1179 transport_controller_->SetIceConfig(config);
1180}
1181
1182void WebRtcSession::MaybeStartGathering() {
1183 transport_controller_->MaybeStartGathering();
1184}
1185
Peter Boström0c4e06b2015-10-07 12:23:21 +02001186bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1187 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001188 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001190 }
1191 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192}
1193
Peter Boström0c4e06b2015-10-07 12:23:21 +02001194bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1195 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001196 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001197 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001198 }
1199 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200}
1201
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001202std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 return desc.str();
1206}
1207
solenbergd4cec0d2015-10-09 08:55:48 -07001208void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 ASSERT(signaling_thread()->IsCurrent());
1210 if (!voice_channel_) {
1211 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1212 return;
1213 }
solenberg4bac9c52015-10-09 02:32:53 -07001214 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1215 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 // otherwise. This in the normal case when the underlying media channel has
1217 // already been deleted.
1218 ASSERT(enable == false);
1219 }
1220}
1221
Peter Boström0c4e06b2015-10-07 12:23:21 +02001222void WebRtcSession::SetAudioSend(uint32_t ssrc,
1223 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001224 const cricket::AudioOptions& options,
1225 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 ASSERT(signaling_thread()->IsCurrent());
1227 if (!voice_channel_) {
1228 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1229 return;
1230 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001231 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001232 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001233 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234}
1235
Peter Boström0c4e06b2015-10-07 12:23:21 +02001236void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001237 ASSERT(signaling_thread()->IsCurrent());
1238 ASSERT(volume >= 0 && volume <= 10);
1239 if (!voice_channel_) {
1240 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1241 return;
1242 }
1243
solenberg4bac9c52015-10-09 02:32:53 -07001244 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001245 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001246 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001247}
1248
deadbeef2d110be2016-01-13 12:00:26 -08001249void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1250 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001251 ASSERT(signaling_thread()->IsCurrent());
1252 if (!voice_channel_)
1253 return;
1254
deadbeef2d110be2016-01-13 12:00:26 -08001255 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001256}
1257
Peter Boström0c4e06b2015-10-07 12:23:21 +02001258bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 cricket::VideoCapturer* camera) {
1260 ASSERT(signaling_thread()->IsCurrent());
1261
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001262 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1264 // support video.
1265 LOG(LS_WARNING) << "Video not used in this call.";
1266 return false;
1267 }
1268 if (!video_channel_->SetCapturer(ssrc, camera)) {
1269 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1270 // This in the normal case when the underlying media channel has already
1271 // been deleted.
1272 ASSERT(camera == NULL);
1273 return false;
1274 }
1275 return true;
1276}
1277
nisse08582ff2016-02-04 01:24:52 -08001278void WebRtcSession::SetVideoPlayout(
1279 uint32_t ssrc,
1280 bool enable,
1281 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 ASSERT(signaling_thread()->IsCurrent());
1283 if (!video_channel_) {
1284 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1285 return;
1286 }
nisse08582ff2016-02-04 01:24:52 -08001287 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1288 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 // This in the normal case when the underlying media channel has already
1290 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001291 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 }
1293}
1294
Peter Boström0c4e06b2015-10-07 12:23:21 +02001295void WebRtcSession::SetVideoSend(uint32_t ssrc,
1296 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 const cricket::VideoOptions* options) {
1298 ASSERT(signaling_thread()->IsCurrent());
1299 if (!video_channel_) {
1300 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1301 return;
1302 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001303 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1305 // This in the normal case when the underlying media channel has already
1306 // been deleted.
1307 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
1311bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1312 ASSERT(signaling_thread()->IsCurrent());
1313 if (!voice_channel_) {
1314 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1315 return false;
1316 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001317 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1319 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001320 if (!local_desc_ ||
1321 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 &send_ssrc)) {
1323 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1324 return false;
1325 }
1326 return voice_channel_->CanInsertDtmf();
1327}
1328
1329bool WebRtcSession::InsertDtmf(const std::string& track_id,
1330 int code, int duration) {
1331 ASSERT(signaling_thread()->IsCurrent());
1332 if (!voice_channel_) {
1333 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1334 return false;
1335 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001336 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001337 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1338 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1340 return false;
1341 }
solenberg1d63dd02015-12-02 12:35:09 -08001342 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1344 return false;
1345 }
1346 return true;
1347}
1348
1349sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001350 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351}
1352
wu@webrtc.org78187522013-10-07 23:32:02 +00001353bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001354 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001355 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001356 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001357 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1358 return false;
1359 }
1360 return data_channel_->SendData(params, payload, result);
1361}
1362
1363bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001364 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001365 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1366 return false;
1367 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001368 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1369 &DataChannel::OnChannelReady);
1370 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1371 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001372 data_channel_->SignalStreamClosedRemotely.connect(
1373 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001374 return true;
1375}
1376
1377void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001378 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001379 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1380 return;
1381 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001382 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1383 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001384 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001385}
1386
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001387void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001388 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001389 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1390 return;
1391 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001392 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1393 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001394}
1395
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001396void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001397 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001398 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1399 << "NULL.";
1400 return;
1401 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001402 data_channel_->RemoveRecvStream(sid);
1403 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001404}
1405
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001406bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001407 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001408}
1409
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410cricket::DataChannelType WebRtcSession::data_channel_type() const {
1411 return data_channel_type_;
1412}
1413
deadbeef0ed85b22016-02-23 17:24:52 -08001414bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1415 return pending_ice_restarts_.find(content_name) !=
1416 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001417}
1418
Henrik Boströmd8281982015-08-27 10:12:24 +02001419void WebRtcSession::OnCertificateReady(
1420 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001421 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001422}
1423
Henrik Boströmd8281982015-08-27 10:12:24 +02001424bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001425 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001426}
1427
deadbeefcbecd352015-09-23 11:50:27 -07001428const rtc::scoped_refptr<rtc::RTCCertificate>&
1429WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001430 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001431}
1432
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433void WebRtcSession::SetIceConnectionState(
1434 PeerConnectionInterface::IceConnectionState state) {
1435 if (ice_connection_state_ == state) {
1436 return;
1437 }
1438
1439 // ASSERT that the requested transition is allowed. Note that
1440 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1441 // within PeerConnection). This switch statement should compile away when
1442 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001443 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1444 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 switch (ice_connection_state_) {
1446 case PeerConnectionInterface::kIceConnectionNew:
1447 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1448 break;
1449 case PeerConnectionInterface::kIceConnectionChecking:
1450 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1451 state == PeerConnectionInterface::kIceConnectionConnected);
1452 break;
1453 case PeerConnectionInterface::kIceConnectionConnected:
1454 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1455 state == PeerConnectionInterface::kIceConnectionChecking ||
1456 state == PeerConnectionInterface::kIceConnectionCompleted);
1457 break;
1458 case PeerConnectionInterface::kIceConnectionCompleted:
1459 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1460 state == PeerConnectionInterface::kIceConnectionDisconnected);
1461 break;
1462 case PeerConnectionInterface::kIceConnectionFailed:
1463 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1464 break;
1465 case PeerConnectionInterface::kIceConnectionDisconnected:
1466 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1467 state == PeerConnectionInterface::kIceConnectionConnected ||
1468 state == PeerConnectionInterface::kIceConnectionCompleted ||
1469 state == PeerConnectionInterface::kIceConnectionFailed);
1470 break;
1471 case PeerConnectionInterface::kIceConnectionClosed:
1472 ASSERT(false);
1473 break;
1474 default:
1475 ASSERT(false);
1476 break;
1477 }
1478
1479 ice_connection_state_ = state;
1480 if (ice_observer_) {
1481 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1482 }
1483}
1484
deadbeefcbecd352015-09-23 11:50:27 -07001485void WebRtcSession::OnTransportControllerConnectionState(
1486 cricket::IceConnectionState state) {
1487 switch (state) {
1488 case cricket::kIceConnectionConnecting:
1489 // If the current state is Connected or Completed, then there were
1490 // writable channels but now there are not, so the next state must
1491 // be Disconnected.
1492 // kIceConnectionConnecting is currently used as the default,
1493 // un-connected state by the TransportController, so its only use is
1494 // detecting disconnections.
1495 if (ice_connection_state_ ==
1496 PeerConnectionInterface::kIceConnectionConnected ||
1497 ice_connection_state_ ==
1498 PeerConnectionInterface::kIceConnectionCompleted) {
1499 SetIceConnectionState(
1500 PeerConnectionInterface::kIceConnectionDisconnected);
1501 }
torbjornga81a42f2015-09-23 02:16:58 -07001502 break;
deadbeefcbecd352015-09-23 11:50:27 -07001503 case cricket::kIceConnectionFailed:
1504 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1505 break;
1506 case cricket::kIceConnectionConnected:
1507 LOG(LS_INFO) << "Changing to ICE connected state because "
1508 << "all transports are writable.";
1509 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1510 break;
1511 case cricket::kIceConnectionCompleted:
1512 LOG(LS_INFO) << "Changing to ICE completed state because "
1513 << "all transports are complete.";
1514 if (ice_connection_state_ !=
1515 PeerConnectionInterface::kIceConnectionConnected) {
1516 // If jumping directly from "checking" to "connected",
1517 // signal "connected" first.
1518 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1519 }
1520 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1521 if (metrics_observer_) {
1522 ReportTransportStats();
1523 }
1524 break;
1525 default:
1526 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001527 }
deadbeefcbecd352015-09-23 11:50:27 -07001528}
1529
1530void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001531 SetIceConnectionReceiving(receiving);
1532}
1533
1534void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1535 if (ice_connection_receiving_ == receiving) {
1536 return;
1537 }
1538 ice_connection_receiving_ = receiving;
1539 if (ice_observer_) {
1540 ice_observer_->OnIceConnectionReceivingChange(receiving);
1541 }
1542}
1543
deadbeefcbecd352015-09-23 11:50:27 -07001544void WebRtcSession::OnTransportControllerCandidatesGathered(
1545 const std::string& transport_name,
1546 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001548 int sdp_mline_index;
1549 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1550 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1551 << transport_name << " not found";
1552 return;
1553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554
deadbeefcbecd352015-09-23 11:50:27 -07001555 for (cricket::Candidates::const_iterator citer = candidates.begin();
1556 citer != candidates.end(); ++citer) {
1557 // Use transport_name as the candidate media id.
1558 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1559 if (ice_observer_) {
1560 ice_observer_->OnIceCandidate(&candidate);
1561 }
1562 if (local_desc_) {
1563 local_desc_->AddCandidate(&candidate);
1564 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 }
1566}
1567
1568// Enabling voice and video channel.
1569void WebRtcSession::EnableChannels() {
1570 if (voice_channel_ && !voice_channel_->enabled())
1571 voice_channel_->Enable(true);
1572
1573 if (video_channel_ && !video_channel_->enabled())
1574 video_channel_->Enable(true);
1575
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001576 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 data_channel_->Enable(true);
1578}
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580// Returns the media index for a local ice candidate given the content name.
1581bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1582 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001583 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001585 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
1587 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001588 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 for (size_t index = 0; index < contents.size(); ++index) {
1590 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001591 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 content_found = true;
1593 break;
1594 }
1595 }
1596 return content_found;
1597}
1598
1599bool WebRtcSession::UseCandidatesInSessionDescription(
1600 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001601 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001603 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001605
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1607 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001608 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001609 const IceCandidateInterface* candidate = candidates->at(n);
1610 bool valid = false;
1611 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1612 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001613 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1614 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001615 }
1616 continue;
1617 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001618 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001619 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 break;
deadbeefd59daf82015-10-14 15:02:44 -07001621 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 }
1623 }
1624 return ret;
1625}
1626
1627bool WebRtcSession::UseCandidate(
1628 const IceCandidateInterface* candidate) {
1629
1630 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001631 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 if (mediacontent_index >= remote_content_size) {
1633 LOG(LS_ERROR)
1634 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1635 return false;
1636 }
1637
1638 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001639 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 std::vector<cricket::Candidate> candidates;
1641 candidates.push_back(candidate->candidate());
1642 // Invoking BaseSession method to handle remote candidates.
1643 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001644 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1645 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 // Candidates successfully submitted for checking.
1647 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1648 ice_connection_state_ ==
1649 PeerConnectionInterface::kIceConnectionDisconnected) {
1650 // If state is New, then the session has just gotten its first remote ICE
1651 // candidates, so go to Checking.
1652 // If state is Disconnected, the session is re-using old candidates or
1653 // receiving additional ones, so go to Checking.
1654 // If state is Connected, stay Connected.
1655 // TODO(bemasc): If state is Connected, and the new candidates are for a
1656 // newly added transport, then the state actually _should_ move to
1657 // checking. Add a way to distinguish that case.
1658 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1659 }
1660 // TODO(bemasc): If state is Completed, go back to Connected.
1661 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001662 if (!error.empty()) {
1663 LOG(LS_WARNING) << error;
1664 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 }
1666 return true;
1667}
1668
deadbeefcbecd352015-09-23 11:50:27 -07001669void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001670 // Destroy video_channel_ first since it may have a pointer to the
1671 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 const cricket::ContentInfo* video_info =
1673 cricket::GetFirstVideoContent(desc);
1674 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 }
1678
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001679 const cricket::ContentInfo* voice_info =
1680 cricket::GetFirstAudioContent(desc);
1681 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001682 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001683 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001684 }
1685
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 const cricket::ContentInfo* data_info =
1687 cricket::GetFirstDataContent(desc);
1688 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 }
1692}
1693
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001694// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001695// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 // Creating the media channels and transport proxies.
1698 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1699 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001700 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 LOG(LS_ERROR) << "Failed to create voice channel.";
1702 return false;
1703 }
1704 }
1705
1706 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1707 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001708 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 LOG(LS_ERROR) << "Failed to create video channel.";
1710 return false;
1711 }
1712 }
1713
1714 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1715 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001716 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001717 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 LOG(LS_ERROR) << "Failed to create data channel.";
1719 return false;
1720 }
1721 }
1722
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001723 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1724 if (voice_channel()) {
1725 voice_channel()->ActivateRtcpMux();
1726 }
1727 if (video_channel()) {
1728 video_channel()->ActivateRtcpMux();
1729 }
1730 if (data_channel()) {
1731 data_channel()->ActivateRtcpMux();
1732 }
1733 }
1734
deadbeefcbecd352015-09-23 11:50:27 -07001735 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001736 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001737 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1738 cricket::GROUP_TYPE_BUNDLE);
1739 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001740 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1741 return false;
1742 }
deadbeefcbecd352015-09-23 11:50:27 -07001743 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001744 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1745 return false;
1746 }
1747 }
1748
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 return true;
1750}
1751
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001752bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001754 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001755 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001756 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001757 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001758 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001759
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001760 voice_channel_->SignalDtlsSetupFailure.connect(
1761 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001762
1763 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001764 voice_channel_->transport_channel()->SignalSentPacket.connect(
1765 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001766 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767}
1768
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001769bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001771 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001772 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001773 if (!video_channel_) {
1774 return false;
1775 }
1776
1777 video_channel_->SignalDtlsSetupFailure.connect(
1778 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001779
1780 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001781 video_channel_->transport_channel()->SignalSentPacket.connect(
1782 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001783 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784}
1785
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001786bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001787 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001789 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001790 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001791 return false;
1792 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001793
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001794 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001795 data_channel_->SignalDataReceived.connect(
1796 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001797 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001798
1799 data_channel_->SignalDtlsSetupFailure.connect(
1800 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001801
1802 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001803 data_channel_->transport_channel()->SignalSentPacket.connect(
1804 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001805 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806}
1807
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001808void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001809 SetError(ERROR_TRANSPORT,
1810 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811}
1812
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001813void WebRtcSession::OnDataChannelMessageReceived(
1814 cricket::DataChannel* channel,
1815 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001816 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001817 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1818 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1819 // Received OPEN message; parse and signal that a new data channel should
1820 // be created.
1821 std::string label;
1822 InternalDataChannelInit config;
1823 config.id = params.ssrc;
1824 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1825 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1826 << params.ssrc;
1827 return;
1828 }
1829 config.open_handshake_role = InternalDataChannelInit::kAcker;
1830 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 }
deadbeefab9b2d12015-10-14 11:33:11 -07001832 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833}
1834
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001835// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001836bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001837 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1838 if (!bundle_enabled)
1839 return true;
1840
1841 const cricket::ContentGroup* bundle_group =
1842 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1843 ASSERT(bundle_group != NULL);
1844
1845 const cricket::ContentInfos& contents = desc->contents();
1846 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1847 citer != contents.end(); ++citer) {
1848 const cricket::ContentInfo* content = (&*citer);
1849 ASSERT(content != NULL);
1850 if (bundle_group->HasContentName(content->name) &&
1851 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1852 if (!HasRtcpMuxEnabled(content))
1853 return false;
1854 }
1855 }
1856 // RTCP-MUX is enabled in all the contents.
1857 return true;
1858}
1859
1860bool WebRtcSession::HasRtcpMuxEnabled(
1861 const cricket::ContentInfo* content) {
1862 const cricket::MediaContentDescription* description =
1863 static_cast<cricket::MediaContentDescription*>(content->description);
1864 return description->rtcp_mux();
1865}
1866
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001867bool WebRtcSession::ValidateSessionDescription(
1868 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001869 cricket::ContentSource source, std::string* err_desc) {
1870 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001871 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001872 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001873 }
1874
1875 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001876 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001877 }
1878
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001879 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001880 Action action = GetAction(sdesc->type());
1881 if (source == cricket::CS_LOCAL) {
1882 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001883 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001884 } else {
1885 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001886 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001887 }
1888
1889 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001890 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001891 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1892 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001893 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001894 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001895 }
1896
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001897 // Verify ice-ufrag and ice-pwd.
1898 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001899 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001900 }
1901
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001902 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001903 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001904 }
1905
1906 // Verify m-lines in Answer when compared against Offer.
1907 if (action == kAnswer) {
1908 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001909 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1910 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001911 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001912 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913 }
1914 }
1915
1916 return true;
1917}
1918
1919bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1920 return ((action == kOffer && state() == STATE_INIT) ||
1921 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001922 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001923 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924 (action == kOffer && state() == STATE_INPROGRESS) ||
1925 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001926 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1927 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1928 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1929 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930}
1931
1932bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1933 return ((action == kOffer && state() == STATE_INIT) ||
1934 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001935 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001936 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937 (action == kOffer && state() == STATE_INPROGRESS) ||
1938 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001939 (action == kAnswer && state() == STATE_SENTOFFER) ||
1940 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1941 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1942 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001943}
1944
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001945std::string WebRtcSession::GetSessionErrorMsg() {
1946 std::ostringstream desc;
1947 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1948 desc << kSessionErrorDesc << error_desc() << ".";
1949 return desc.str();
1950}
1951
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001952// We need to check the local/remote description for the Transport instead of
1953// the session, because a new Transport added during renegotiation may have
1954// them unset while the session has them set from the previous negotiation.
1955// Not doing so may trigger the auto generation of transport description and
1956// mess up DTLS identity information, ICE credential, etc.
1957bool WebRtcSession::ReadyToUseRemoteCandidate(
1958 const IceCandidateInterface* candidate,
1959 const SessionDescriptionInterface* remote_desc,
1960 bool* valid) {
1961 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001962
1963 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001964 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001965
deadbeefd59daf82015-10-14 15:02:44 -07001966 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001967 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001968 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001969
1970 size_t mediacontent_index =
1971 static_cast<size_t>(candidate->sdp_mline_index());
1972 size_t remote_content_size =
1973 current_remote_desc->description()->contents().size();
1974 if (mediacontent_index >= remote_content_size) {
1975 LOG(LS_ERROR)
1976 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1977
1978 *valid = false;
1979 return false;
1980 }
1981
1982 cricket::ContentInfo content =
1983 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001984 cricket::BaseChannel* channel = GetChannel(content.name);
1985 if (!channel) {
1986 return false;
1987 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001988
deadbeefd59daf82015-10-14 15:02:44 -07001989 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001990 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001991}
1992
deadbeefcbecd352015-09-23 11:50:27 -07001993void WebRtcSession::OnTransportControllerGatheringState(
1994 cricket::IceGatheringState state) {
1995 ASSERT(signaling_thread()->IsCurrent());
1996 if (state == cricket::kIceGatheringGathering) {
1997 if (ice_observer_) {
1998 ice_observer_->OnIceGatheringChange(
1999 PeerConnectionInterface::kIceGatheringGathering);
2000 }
2001 } else if (state == cricket::kIceGatheringComplete) {
2002 if (ice_observer_) {
2003 ice_observer_->OnIceGatheringChange(
2004 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002005 }
2006 }
2007}
2008
2009void WebRtcSession::ReportTransportStats() {
2010 // Use a set so we don't report the same stats twice if two channels share
2011 // a transport.
2012 std::set<std::string> transport_names;
2013 if (voice_channel()) {
2014 transport_names.insert(voice_channel()->transport_name());
2015 }
2016 if (video_channel()) {
2017 transport_names.insert(video_channel()->transport_name());
2018 }
2019 if (data_channel()) {
2020 transport_names.insert(data_channel()->transport_name());
2021 }
2022 for (const auto& name : transport_names) {
2023 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002024 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002025 ReportBestConnectionState(stats);
2026 ReportNegotiatedCiphers(stats);
2027 }
2028 }
2029}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002030// Walk through the ConnectionInfos to gather best connection usage
2031// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002032void WebRtcSession::ReportBestConnectionState(
2033 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002034 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002035 for (cricket::TransportChannelStatsList::const_iterator it =
2036 stats.channel_stats.begin();
2037 it != stats.channel_stats.end(); ++it) {
2038 for (cricket::ConnectionInfos::const_iterator it_info =
2039 it->connection_infos.begin();
2040 it_info != it->connection_infos.end(); ++it_info) {
2041 if (!it_info->best_connection) {
2042 continue;
2043 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002044
2045 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2046 const cricket::Candidate& local = it_info->local_candidate;
2047 const cricket::Candidate& remote = it_info->remote_candidate;
2048
2049 // Increment the counter for IceCandidatePairType.
2050 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2051 (local.type() == RELAY_PORT_TYPE &&
2052 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2053 type = kEnumCounterIceCandidatePairTypeTcp;
2054 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2055 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002056 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002057 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002058 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002059 metrics_observer_->IncrementEnumCounter(
2060 type, GetIceCandidatePairCounter(local, remote),
2061 kIceCandidatePairMax);
2062
2063 // Increment the counter for IP type.
2064 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002065 metrics_observer_->IncrementEnumCounter(
2066 kEnumCounterAddressFamily, kBestConnections_IPv4,
2067 kPeerConnectionAddressFamilyCounter_Max);
2068
2069 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002070 metrics_observer_->IncrementEnumCounter(
2071 kEnumCounterAddressFamily, kBestConnections_IPv6,
2072 kPeerConnectionAddressFamilyCounter_Max);
2073 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002074 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002075 }
2076
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002077 return;
2078 }
2079 }
2080}
2081
jbauchac8869e2015-07-03 01:36:14 -07002082void WebRtcSession::ReportNegotiatedCiphers(
2083 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002084 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002085 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2086 return;
2087 }
2088
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002089 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2090 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2091 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2092 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002093 return;
2094 }
2095
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002096 PeerConnectionEnumCounterType srtp_counter_type;
2097 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002098 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002099 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2100 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002101 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002102 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2103 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002104 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002105 srtp_counter_type = kEnumCounterDataSrtpCipher;
2106 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002107 } else {
2108 RTC_NOTREACHED();
2109 return;
2110 }
2111
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002112 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2113 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2114 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002115 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002116 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2117 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2118 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002119 }
2120}
2121
stefanc1aeaf02015-10-15 07:26:07 -07002122void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2123 const rtc::SentPacket& sent_packet) {
2124 RTC_DCHECK(worker_thread()->IsCurrent());
2125 media_controller_->call_w()->OnSentPacket(sent_packet);
2126}
2127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128} // namespace webrtc