blob: c693cb1077ee6e1d072bfd9a1308ad1175b6350e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/jsepicecandidate.h"
21#include "webrtc/api/jsepsessiondescription.h"
22#include "webrtc/api/mediaconstraintsinterface.h"
23#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
Tommif888bb52015-12-12 01:37:01 +010026#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/constants.h"
35#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::ContentInfo;
43using cricket::ContentInfos;
44using cricket::MediaContentDescription;
45using cricket::SessionDescription;
46using cricket::TransportInfo;
47
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070048using cricket::LOCAL_PORT_TYPE;
49using cricket::STUN_PORT_TYPE;
50using cricket::RELAY_PORT_TYPE;
51using cricket::PRFLX_PORT_TYPE;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053namespace webrtc {
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000056const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
61const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
63const char kPushDownTDFailed[] =
64 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000065const char kSdpWithoutDtlsFingerprint[] =
66 "Called with SDP without DTLS fingerprint.";
67const char kSdpWithoutSdesCrypto[] =
68 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000073const char kDtlsSetupFailureRtp[] =
74 "Couldn't set up DTLS-SRTP on RTP channel.";
75const char kDtlsSetupFailureRtcp[] =
76 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070077const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070079IceCandidatePairType GetIceCandidatePairCounter(
80 const cricket::Candidate& local,
81 const cricket::Candidate& remote) {
82 const auto& l = local.type();
83 const auto& r = remote.type();
84 const auto& host = LOCAL_PORT_TYPE;
85 const auto& srflx = STUN_PORT_TYPE;
86 const auto& relay = RELAY_PORT_TYPE;
87 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070088 if (l == host && r == host) {
89 bool local_private = IPIsPrivate(local.address().ipaddr());
90 bool remote_private = IPIsPrivate(remote.address().ipaddr());
91 if (local_private) {
92 if (remote_private) {
93 return kIceCandidatePairHostPrivateHostPrivate;
94 } else {
95 return kIceCandidatePairHostPrivateHostPublic;
96 }
97 } else {
98 if (remote_private) {
99 return kIceCandidatePairHostPublicHostPrivate;
100 } else {
101 return kIceCandidatePairHostPublicHostPublic;
102 }
103 }
104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700105 if (l == host && r == srflx)
106 return kIceCandidatePairHostSrflx;
107 if (l == host && r == relay)
108 return kIceCandidatePairHostRelay;
109 if (l == host && r == prflx)
110 return kIceCandidatePairHostPrflx;
111 if (l == srflx && r == host)
112 return kIceCandidatePairSrflxHost;
113 if (l == srflx && r == srflx)
114 return kIceCandidatePairSrflxSrflx;
115 if (l == srflx && r == relay)
116 return kIceCandidatePairSrflxRelay;
117 if (l == srflx && r == prflx)
118 return kIceCandidatePairSrflxPrflx;
119 if (l == relay && r == host)
120 return kIceCandidatePairRelayHost;
121 if (l == relay && r == srflx)
122 return kIceCandidatePairRelaySrflx;
123 if (l == relay && r == relay)
124 return kIceCandidatePairRelayRelay;
125 if (l == relay && r == prflx)
126 return kIceCandidatePairRelayPrflx;
127 if (l == prflx && r == host)
128 return kIceCandidatePairPrflxHost;
129 if (l == prflx && r == srflx)
130 return kIceCandidatePairPrflxSrflx;
131 if (l == prflx && r == relay)
132 return kIceCandidatePairPrflxRelay;
133 return kIceCandidatePairMax;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Compares |answer| against |offer|. Comparision is done
137// for number of m-lines in answer against offer. If matches true will be
138// returned otherwise false.
139static bool VerifyMediaDescriptions(
140 const SessionDescription* answer, const SessionDescription* offer) {
141 if (offer->contents().size() != answer->contents().size())
142 return false;
143
144 for (size_t i = 0; i < offer->contents().size(); ++i) {
145 if ((offer->contents()[i].name) != answer->contents()[i].name) {
146 return false;
147 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000148 const MediaContentDescription* offer_mdesc =
149 static_cast<const MediaContentDescription*>(
150 offer->contents()[i].description);
151 const MediaContentDescription* answer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 answer->contents()[i].description);
154 if (offer_mdesc->type() != answer_mdesc->type()) {
155 return false;
156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159}
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// Checks that each non-rejected content has SDES crypto keys or a DTLS
162// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
163// keys, will be caught in Transport negotiation, and backstopped by Channel's
164// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000165static bool VerifyCrypto(const SessionDescription* desc,
166 bool dtls_enabled,
167 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 const ContentInfos& contents = desc->contents();
169 for (size_t index = 0; index < contents.size(); ++index) {
170 const ContentInfo* cinfo = &contents[index];
171 if (cinfo->rejected) {
172 continue;
173 }
174
175 // If the content isn't rejected, crypto must be present.
176 const MediaContentDescription* media =
177 static_cast<const MediaContentDescription*>(cinfo->description);
178 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
179 if (!media || !tinfo) {
180 // Something is not right.
181 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return false;
184 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000185 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000187 LOG(LS_WARNING) <<
188 "Session description must have DTLS fingerprint if DTLS enabled.";
189 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 return false;
191 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 } else {
193 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 LOG(LS_WARNING) <<
195 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 return false;
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
200 }
201
202 return true;
203}
204
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000205// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
206static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
207 const ContentInfos& contents = desc->contents();
208 for (size_t index = 0; index < contents.size(); ++index) {
209 const ContentInfo* cinfo = &contents[index];
210 if (cinfo->rejected) {
211 continue;
212 }
213
214 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
215 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
216 if (!tinfo) {
217 // Something is not right.
218 LOG(LS_ERROR) << kInvalidSdp;
219 return false;
220 }
221 if (tinfo->description.ice_ufrag.empty() ||
222 tinfo->description.ice_pwd.empty()) {
223 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
224 return false;
225 }
226 }
227 return true;
228}
229
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230// Forces |sdesc->crypto_required| to the appropriate state based on the
231// current security policy, to ensure a failure occurs if there is an error
232// in crypto negotiation.
233// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000234static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
235 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 if (!sdesc) {
237 return;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 // Updating the |crypto_required_| in MediaContentDescription to the
241 // appropriate state based on the current security policy.
242 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
243 iter != sdesc->contents().end(); ++iter) {
244 if (cricket::IsMediaContent(&*iter)) {
245 MediaContentDescription* mdesc =
246 static_cast<MediaContentDescription*> (iter->description);
247 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 }
250 }
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
255 const std::string& track_id,
256 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 const cricket::ContentInfo* audio_info =
258 cricket::GetFirstAudioContent(session_description);
259 if (!audio_info) {
260 LOG(LS_ERROR) << "Audio not used in this call";
261 return false;
262 }
263
264 const cricket::MediaContentDescription* audio_content =
265 static_cast<const cricket::MediaContentDescription*>(
266 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000267 const cricket::StreamParams* stream =
268 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
269 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return false;
271 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272
273 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return true;
275}
276
277static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint32_t ssrc,
279 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 ASSERT(track_id != NULL);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 const cricket::ContentInfo* audio_info =
283 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000284 if (audio_info) {
285 const cricket::MediaContentDescription* audio_content =
286 static_cast<const cricket::MediaContentDescription*>(
287 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289 const auto* found =
290 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
291 if (found) {
292 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000293 return true;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
297 const cricket::ContentInfo* video_info =
298 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (video_info) {
300 const cricket::MediaContentDescription* video_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311 return false;
312}
313
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314static bool BadSdp(const std::string& source,
315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
318 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700319 desc << "Failed to set " << source;
320 if (!type.empty()) {
321 desc << " " << type;
322 }
323 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000326 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 return false;
330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 }
341}
342
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343static bool BadLocalSdp(const std::string& type,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
347}
348
349static bool BadRemoteSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
353}
354
355static bool BadOfferSdp(cricket::ContentSource source,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
359}
360
361static bool BadPranswerSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
365 reason, err_desc);
366}
367
368static bool BadAnswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
deadbeefd59daf82015-10-14 15:02:44 -0700374#define GET_STRING_OF_STATE(state) \
375 case webrtc::WebRtcSession::state: \
376 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 break;
378
deadbeefd59daf82015-10-14 15:02:44 -0700379static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 std::string result;
381 switch (state) {
382 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700383 GET_STRING_OF_STATE(STATE_SENTOFFER)
384 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
385 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
386 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
390 ASSERT(false);
391 break;
392 }
393 return result;
394}
395
deadbeefd59daf82015-10-14 15:02:44 -0700396#define GET_STRING_OF_ERROR_CODE(err) \
397 case webrtc::WebRtcSession::err: \
398 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 break;
400
deadbeefd59daf82015-10-14 15:02:44 -0700401static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 std::string result;
403 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
406 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
deadbeefd59daf82015-10-14 15:02:44 -0700408 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414static std::string MakeErrorString(const std::string& error,
415 const std::string& desc) {
416 std::ostringstream ret;
417 ret << error << " " << desc;
418 return ret.str();
419}
420
421static std::string MakeTdErrorString(const std::string& desc) {
422 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000425// Set |option| to the highest-priority value of |key| in the optional
426// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700427template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000428static void SetOptionFromOptionalConstraint(
429 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700430 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100431 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000432 if (!constraints) {
433 return;
434 }
435 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000436 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000437 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100439 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440 }
441 }
442}
443
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000445 PeerConnectionInterface::IceTransportsType type) {
446 switch (type) {
447 case PeerConnectionInterface::kNone:
448 return cricket::CF_NONE;
449 case PeerConnectionInterface::kRelay:
450 return cricket::CF_RELAY;
451 case PeerConnectionInterface::kNoHost:
452 return (cricket::CF_ALL & ~cricket::CF_HOST);
453 case PeerConnectionInterface::kAll:
454 return cricket::CF_ALL;
455 default: ASSERT(false);
456 }
457 return cricket::CF_NONE;
458}
459
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460// Help class used to remember if a a remote peer has requested ice restart by
461// by sending a description with new ice ufrag and password.
462class IceRestartAnswerLatch {
463 public:
464 IceRestartAnswerLatch() : ice_restart_(false) { }
465
wu@webrtc.org91053e72013-08-10 07:18:04 +0000466 // Returns true if CheckForRemoteIceRestart has been called with a new session
467 // description where ice password and ufrag has changed since last time
468 // Reset() was called.
469 bool Get() const {
470 return ice_restart_;
471 }
472
473 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 if (ice_restart_) {
475 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478
Honghai Zhang04e91462015-12-11 14:26:22 -0800479 // This method has two purposes: 1. Return whether |new_desc| requests
480 // an ICE restart (i.e., new ufrag/pwd). 2. If it requests an ICE restart
481 // and it is an OFFER, remember this in |ice_restart_| so that the next
482 // Local Answer will be created with new ufrag and pwd.
honghaiz503726c2015-07-31 12:37:38 -0700483 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
484 const SessionDescriptionInterface* new_desc) {
Honghai Zhang04e91462015-12-11 14:26:22 -0800485 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700486 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 }
488 const SessionDescription* new_sd = new_desc->description();
489 const SessionDescription* old_sd = old_desc->description();
490 const ContentInfos& contents = new_sd->contents();
491 for (size_t index = 0; index < contents.size(); ++index) {
492 const ContentInfo* cinfo = &contents[index];
493 if (cinfo->rejected) {
494 continue;
495 }
496 // If the content isn't rejected, check if ufrag and password has
497 // changed.
498 const cricket::TransportDescription* new_transport_desc =
499 new_sd->GetTransportDescriptionByName(cinfo->name);
500 const cricket::TransportDescription* old_transport_desc =
501 old_sd->GetTransportDescriptionByName(cinfo->name);
502 if (!new_transport_desc || !old_transport_desc) {
503 // No transport description exist. This is not an ice restart.
504 continue;
505 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000506 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
507 old_transport_desc->ice_pwd,
508 new_transport_desc->ice_ufrag,
509 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 LOG(LS_INFO) << "Remote peer request ice restart.";
Honghai Zhang04e91462015-12-11 14:26:22 -0800511 if (new_desc->type() == SessionDescriptionInterface::kOffer) {
512 ice_restart_ = true;
513 }
honghaiz503726c2015-07-31 12:37:38 -0700514 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
516 }
honghaiz503726c2015-07-31 12:37:38 -0700517 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 }
519
520 private:
521 bool ice_restart_;
522};
523
stefanc1aeaf02015-10-15 07:26:07 -0700524WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700525 rtc::Thread* signaling_thread,
526 rtc::Thread* worker_thread,
527 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700528 : signaling_thread_(signaling_thread),
529 worker_thread_(worker_thread),
530 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // RFC 3264: The numeric value of the session id and version in the
532 // o line MUST be representable with a "64 bit signed integer".
533 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700534 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
535 transport_controller_(new cricket::TransportController(signaling_thread,
536 worker_thread,
537 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700538 media_controller_(media_controller),
539 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 ice_observer_(NULL),
541 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700542 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000544 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000546 ice_restart_latch_(new IceRestartAnswerLatch),
547 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700548 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
549 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700550 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700551 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700552 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700553 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700554 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700555 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700556 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557}
558
559WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700560 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000561 // Destroy video_channel_ first since it may have a pointer to the
562 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000563 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 SignalVideoChannelDestroyed();
565 channel_manager_->DestroyVideoChannel(video_channel_.release());
566 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000567 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000568 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200569 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000570 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000571 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 SignalDataChannelDestroyed();
573 channel_manager_->DestroyDataChannel(data_channel_.release());
574 }
deadbeef057ecf02016-01-20 14:30:43 -0800575 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700576
577 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578}
579
wu@webrtc.org91053e72013-08-10 07:18:04 +0000580bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000581 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700582 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200583 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200584 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
585 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700586 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700587 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000588
Henrik Boström87713d02015-08-25 09:53:21 +0200589 // Obtain a certificate from RTCConfiguration if any were provided (optional).
590 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
591 if (!rtc_configuration.certificates.empty()) {
592 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
593 // just picking the first one. The decision should be made based on the DTLS
594 // handshake. The DTLS negotiations need to know about all certificates.
595 certificate = rtc_configuration.certificates[0];
596 }
597
honghaiz1f429e32015-09-28 07:57:34 -0700598 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700599
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 // TODO(perkj): Take |constraints| into consideration. Return false if not all
601 // mandatory constraints can be fulfilled. Note that |constraints|
602 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000604
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000605 if (options.disable_encryption) {
606 dtls_enabled_ = false;
607 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200608 // Enable DTLS by default if we have an identity store or a certificate.
609 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000610 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700611 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
612 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000613 dtls_enabled_ = value;
614 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000615 }
616
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000618 // It takes precendence over the disable_sctp_data_channels
619 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 if (FindConstraint(
621 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
622 &value, NULL) && value) {
623 LOG(LS_INFO) << "Allowing RTP data engine.";
624 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000625 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000626 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000627 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000628 LOG(LS_INFO) << "Allowing SCTP data engine.";
629 data_channel_type_ = cricket::DCT_SCTP;
630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000633 // Find Suspend Below Min Bitrate constraint.
634 if (FindConstraint(
635 constraints,
636 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
637 &value,
638 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100639 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000640 }
641
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000642 SetOptionFromOptionalConstraint(constraints,
643 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800644 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000645
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000646 SetOptionFromOptionalConstraint(constraints,
647 MediaConstraintsInterface::kCombinedAudioVideoBwe,
648 &audio_options_.combined_audio_video_bwe);
649
kwiberg102c6a62015-10-30 02:47:38 -0700650 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100651 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200652
Karl Wibergbe579832015-11-10 22:34:18 +0100653 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
654 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200655
Henrik Boström87713d02015-08-25 09:53:21 +0200656 if (!dtls_enabled_) {
657 // Construct with DTLS disabled.
658 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700659 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200660 } else {
661 // Construct with DTLS enabled.
662 if (!certificate) {
663 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700664 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200665 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800666 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700667 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200668 } else {
669 // Use the already generated certificate.
670 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700671 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200672 }
673 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000674
Henrik Boströmd8281982015-08-27 10:12:24 +0200675 webrtc_session_desc_factory_->SignalCertificateReady.connect(
676 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000677
wu@webrtc.org97077a32013-10-25 21:18:33 +0000678 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000679 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000680 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000681 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200682 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700683
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 return true;
685}
686
deadbeefd59daf82015-10-14 15:02:44 -0700687void WebRtcSession::Close() {
688 SetState(STATE_CLOSED);
689 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000690 ASSERT(!voice_channel_);
691 ASSERT(!video_channel_);
692 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693}
694
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000695void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
696 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697}
698
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000699cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
700 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701}
702
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800703bool WebRtcSession::GetSslRole(const std::string& transport_name,
704 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700705 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000706 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
707 << "SSL Role of the session.";
708 return false;
709 }
710
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800711 return transport_controller_->GetSslRole(transport_name, role);
712}
713
714bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
715 rtc::SSLRole* role) {
716 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000717}
718
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000719void WebRtcSession::CreateOffer(
720 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700721 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
722 const cricket::MediaSessionOptions& session_options) {
723 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000724}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725
deadbeefab9b2d12015-10-14 11:33:11 -0700726void WebRtcSession::CreateAnswer(
727 CreateSessionDescriptionObserver* observer,
728 const MediaConstraintsInterface* constraints,
729 const cricket::MediaSessionOptions& session_options) {
730 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
731 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732}
733
734bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
735 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700736 ASSERT(signaling_thread()->IsCurrent());
737
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000738 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000739 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000740
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000741 // Validate SDP.
742 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
743 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745
deadbeefd59daf82015-10-14 15:02:44 -0700746 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000747 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700749 initial_offerer_ = true;
750 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000753 cricket::SecurePolicy sdes_policy =
754 webrtc_session_desc_factory_->SdesPolicy();
755 cricket::CryptoType crypto_required = dtls_enabled_ ?
756 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
757 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000759 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000761 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
763 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000764 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // TODO(mallinath) - Handle CreateChannel failure, as new local description
766 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000767 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 }
769
deadbeefcbecd352015-09-23 11:50:27 -0700770 // Remove unused channels if MediaContentDescription is rejected.
771 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000773 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 return false;
775 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000776
deadbeefd59daf82015-10-14 15:02:44 -0700777 if (remote_desc_) {
778 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700779 UseCandidatesInSessionDescription(remote_desc_.get());
780 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781
deadbeefd59daf82015-10-14 15:02:44 -0700782 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000783 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 }
785 return true;
786}
787
788bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
789 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700790 ASSERT(signaling_thread()->IsCurrent());
791
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000792 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000794
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000795 // Validate SDP.
796 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
deadbeefd59daf82015-10-14 15:02:44 -0700800 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
801 remote_desc_.release());
802 remote_desc_.reset(desc_temp.release());
803
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000805 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 if (action == kOffer && !CreateChannels(desc->description())) {
807 // TODO(mallinath) - Handle CreateChannel failure, as new local description
808 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000809 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811
deadbeefcbecd352015-09-23 11:50:27 -0700812 // Remove unused channels if MediaContentDescription is rejected.
813 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814
815 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
816 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000817 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 return false;
819 }
820
deadbeefd59daf82015-10-14 15:02:44 -0700821 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000822 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 // Check if this new SessionDescription contains new ice ufrag and password
826 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700827 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700828 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700829 // We retain all received candidates only if ICE is not restarted.
830 // When ICE is restarted, all previous candidates belong to an old generation
831 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700832 // TODO(deadbeef): This goes against the W3C spec which says the remote
833 // description should only contain candidates from the last set remote
834 // description plus any candidates added since then. We should remove this
835 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700836 if (!ice_restart) {
837 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700838 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700839 }
840
deadbeefd59daf82015-10-14 15:02:44 -0700841 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000842 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000844
845 // Set the the ICE connection state to connecting since the connection may
846 // become writable with peer reflexive candidates before any remote candidate
847 // is signaled.
848 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
849 // is to have a new signal the indicates a change in checking state from the
850 // transport and expose a new checking() member from transport that can be
851 // read to determine the current checking state. The existing SignalConnecting
852 // actually means "gathering candidates", so cannot be be used here.
853 if (desc->type() != SessionDescriptionInterface::kOffer &&
854 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
855 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
856 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 return true;
858}
859
deadbeefd59daf82015-10-14 15:02:44 -0700860void WebRtcSession::LogState(State old_state, State new_state) {
861 LOG(LS_INFO) << "Session:" << id()
862 << " Old state:" << GetStateString(old_state)
863 << " New state:" << GetStateString(new_state);
864}
865
866void WebRtcSession::SetState(State state) {
867 ASSERT(signaling_thread_->IsCurrent());
868 if (state != state_) {
869 LogState(state_, state);
870 state_ = state;
871 SignalState(this, state_);
872 }
873}
874
875void WebRtcSession::SetError(Error error, const std::string& error_desc) {
876 ASSERT(signaling_thread_->IsCurrent());
877 if (error != error_) {
878 error_ = error;
879 error_desc_ = error_desc;
880 }
881}
882
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883bool WebRtcSession::UpdateSessionState(
884 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700886 ASSERT(signaling_thread()->IsCurrent());
887
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 // If there's already a pending error then no state transition should happen.
889 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700890 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000893 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
894 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
deadbeefd59daf82015-10-14 15:02:44 -0700896 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
897 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000898 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700899 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000900 }
deadbeefd59daf82015-10-14 15:02:44 -0700901 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000902 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 }
904 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000905 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
906 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 }
908 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700909 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
910 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000911 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700912 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000913 }
deadbeefd59daf82015-10-14 15:02:44 -0700914 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000915 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 }
917 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700918 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700919 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700920 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700921 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700922 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800923 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700924 const cricket::ContentGroup* answer_bundle =
925 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
926 if (!EnableBundle(*answer_bundle)) {
927 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
928 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
929 }
930 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800931 // Only push down the transport description after enabling BUNDLE; we don't
932 // want to push down a description on a transport about to be destroyed.
933 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
934 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700937 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000938 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700939 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000940 }
deadbeefd59daf82015-10-14 15:02:44 -0700941 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000942 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 }
944 }
945 return true;
946}
947
948WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
949 if (type == SessionDescriptionInterface::kOffer) {
950 return WebRtcSession::kOffer;
951 } else if (type == SessionDescriptionInterface::kPrAnswer) {
952 return WebRtcSession::kPrAnswer;
953 } else if (type == SessionDescriptionInterface::kAnswer) {
954 return WebRtcSession::kAnswer;
955 }
956 ASSERT(false && "unknown action type");
957 return WebRtcSession::kOffer;
958}
959
deadbeefd59daf82015-10-14 15:02:44 -0700960bool WebRtcSession::PushdownMediaDescription(
961 cricket::ContentAction action,
962 cricket::ContentSource source,
963 std::string* err) {
964 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
965 if (!ch) {
966 return true;
967 } else if (source == cricket::CS_LOCAL) {
968 return ch->PushdownLocalDescription(local_desc_->description(), action,
969 err);
970 } else {
971 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
972 err);
973 }
974 };
975
976 return (set_content(voice_channel()) &&
977 set_content(video_channel()) &&
978 set_content(data_channel()));
979}
980
981bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
982 cricket::ContentAction action,
983 std::string* error_desc) {
984 RTC_DCHECK(signaling_thread()->IsCurrent());
985
986 if (source == cricket::CS_LOCAL) {
987 return PushdownLocalTransportDescription(local_desc_->description(), action,
988 error_desc);
989 }
990 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
991 error_desc);
992}
993
994bool WebRtcSession::PushdownLocalTransportDescription(
995 const SessionDescription* sdesc,
996 cricket::ContentAction action,
997 std::string* err) {
998 RTC_DCHECK(signaling_thread()->IsCurrent());
999
1000 if (!sdesc) {
1001 return false;
1002 }
1003
1004 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1005 if (!transport_controller_->SetLocalTransportDescription(
1006 tinfo.content_name, tinfo.description, action, err)) {
1007 return false;
1008 }
1009 }
1010
1011 return true;
1012}
1013
1014bool WebRtcSession::PushdownRemoteTransportDescription(
1015 const SessionDescription* sdesc,
1016 cricket::ContentAction action,
1017 std::string* err) {
1018 RTC_DCHECK(signaling_thread()->IsCurrent());
1019
1020 if (!sdesc) {
1021 return false;
1022 }
1023
1024 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1025 if (!transport_controller_->SetRemoteTransportDescription(
1026 tinfo.content_name, tinfo.description, action, err)) {
1027 return false;
1028 }
1029 }
1030
1031 return true;
1032}
1033
1034bool WebRtcSession::GetTransportDescription(
1035 const SessionDescription* description,
1036 const std::string& content_name,
1037 cricket::TransportDescription* tdesc) {
1038 if (!description || !tdesc) {
1039 return false;
1040 }
1041 const TransportInfo* transport_info =
1042 description->GetTransportInfoByName(content_name);
1043 if (!transport_info) {
1044 return false;
1045 }
1046 *tdesc = transport_info->description;
1047 return true;
1048}
1049
1050bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001051 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001052 return (GetChannelTransportStats(voice_channel(), stats) &&
1053 GetChannelTransportStats(video_channel(), stats) &&
1054 GetChannelTransportStats(data_channel(), stats));
1055}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001056
deadbeefcbecd352015-09-23 11:50:27 -07001057bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001058 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001059 ASSERT(signaling_thread()->IsCurrent());
1060 if (!ch) {
1061 // Not using this channel.
1062 return true;
1063 }
1064
1065 const std::string& content_name = ch->content_name();
1066 const std::string& transport_name = ch->transport_name();
1067 stats->proxy_to_transport[content_name] = transport_name;
1068 if (stats->transport_stats.find(transport_name) !=
1069 stats->transport_stats.end()) {
1070 // Transport stats already done for this transport.
1071 return true;
1072 }
1073
1074 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001075 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001076 return false;
1077 }
1078
1079 stats->transport_stats[transport_name] = tstats;
1080 return true;
1081}
1082
1083bool WebRtcSession::GetLocalCertificate(
1084 const std::string& transport_name,
1085 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1086 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001087 return transport_controller_->GetLocalCertificate(transport_name,
1088 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001089}
1090
1091bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1092 rtc::SSLCertificate** cert) {
1093 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001094 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001095}
1096
1097cricket::BaseChannel* WebRtcSession::GetChannel(
1098 const std::string& content_name) {
1099 if (voice_channel() && voice_channel()->content_name() == content_name) {
1100 return voice_channel();
1101 }
1102 if (video_channel() && video_channel()->content_name() == content_name) {
1103 return video_channel();
1104 }
1105 if (data_channel() && data_channel()->content_name() == content_name) {
1106 return data_channel();
1107 }
1108 return nullptr;
1109}
1110
1111bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1112 const std::string* first_content_name = bundle.FirstContentName();
1113 if (!first_content_name) {
1114 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1115 return false;
1116 }
1117 const std::string& transport_name = *first_content_name;
1118 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1119
1120 auto maybe_set_transport = [this, bundle, transport_name,
1121 first_channel](cricket::BaseChannel* ch) {
1122 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001123 return true;
1124 }
1125
deadbeefcbecd352015-09-23 11:50:27 -07001126 if (ch->transport_name() == transport_name) {
1127 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1128 << " on " << transport_name << ".";
1129 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001130 }
torbjornga81a42f2015-09-23 02:16:58 -07001131
deadbeefcbecd352015-09-23 11:50:27 -07001132 if (!ch->SetTransport(transport_name)) {
1133 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1134 return false;
1135 }
1136 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1137 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001138 return true;
1139 };
1140
deadbeefcbecd352015-09-23 11:50:27 -07001141 if (!maybe_set_transport(voice_channel()) ||
1142 !maybe_set_transport(video_channel()) ||
1143 !maybe_set_transport(data_channel())) {
1144 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001145 }
deadbeefcbecd352015-09-23 11:50:27 -07001146
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001147 return true;
1148}
1149
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001151 if (!remote_desc_) {
1152 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1153 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 return false;
1155 }
1156
1157 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001158 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 return false;
1160 }
1161
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001162 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001163 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1164 if (!valid) {
1165 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 }
1167
1168 // Add this candidate to the remote session description.
1169 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001170 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 return false;
1172 }
1173
deadbeefd59daf82015-10-14 15:02:44 -07001174 if (ready) {
1175 return UseCandidate(candidate);
1176 } else {
1177 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1178 return true;
1179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180}
1181
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001182bool WebRtcSession::SetIceTransports(
1183 PeerConnectionInterface::IceTransportsType type) {
1184 return port_allocator()->set_candidate_filter(
1185 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001186}
1187
deadbeefd59daf82015-10-14 15:02:44 -07001188cricket::IceConfig WebRtcSession::ParseIceConfig(
1189 const PeerConnectionInterface::RTCConfiguration& config) const {
1190 cricket::IceConfig ice_config;
1191 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001192 ice_config.backup_connection_ping_interval =
1193 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001194 ice_config.gather_continually = (config.continual_gathering_policy ==
1195 PeerConnectionInterface::GATHER_CONTINUALLY);
1196 return ice_config;
1197}
1198
1199void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1200 transport_controller_->SetIceConfig(config);
1201}
1202
1203void WebRtcSession::MaybeStartGathering() {
1204 transport_controller_->MaybeStartGathering();
1205}
1206
Peter Boström0c4e06b2015-10-07 12:23:21 +02001207bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1208 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001209 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001211 }
1212 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
Peter Boström0c4e06b2015-10-07 12:23:21 +02001215bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1216 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001217 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001218 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001219 }
1220 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221}
1222
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001223std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001225 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 return desc.str();
1227}
1228
solenbergd4cec0d2015-10-09 08:55:48 -07001229void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 ASSERT(signaling_thread()->IsCurrent());
1231 if (!voice_channel_) {
1232 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1233 return;
1234 }
solenberg4bac9c52015-10-09 02:32:53 -07001235 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1236 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 // otherwise. This in the normal case when the underlying media channel has
1238 // already been deleted.
1239 ASSERT(enable == false);
1240 }
1241}
1242
Peter Boström0c4e06b2015-10-07 12:23:21 +02001243void WebRtcSession::SetAudioSend(uint32_t ssrc,
1244 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001245 const cricket::AudioOptions& options,
1246 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 ASSERT(signaling_thread()->IsCurrent());
1248 if (!voice_channel_) {
1249 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1250 return;
1251 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001252 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001253 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255}
1256
Peter Boström0c4e06b2015-10-07 12:23:21 +02001257void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001258 ASSERT(signaling_thread()->IsCurrent());
1259 ASSERT(volume >= 0 && volume <= 10);
1260 if (!voice_channel_) {
1261 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1262 return;
1263 }
1264
solenberg4bac9c52015-10-09 02:32:53 -07001265 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001266 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001267 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001268}
1269
deadbeef2d110be2016-01-13 12:00:26 -08001270void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1271 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001272 ASSERT(signaling_thread()->IsCurrent());
1273 if (!voice_channel_)
1274 return;
1275
deadbeef2d110be2016-01-13 12:00:26 -08001276 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001277}
1278
Peter Boström0c4e06b2015-10-07 12:23:21 +02001279bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 cricket::VideoCapturer* camera) {
1281 ASSERT(signaling_thread()->IsCurrent());
1282
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001283 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1285 // support video.
1286 LOG(LS_WARNING) << "Video not used in this call.";
1287 return false;
1288 }
1289 if (!video_channel_->SetCapturer(ssrc, camera)) {
1290 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1291 // This in the normal case when the underlying media channel has already
1292 // been deleted.
1293 ASSERT(camera == NULL);
1294 return false;
1295 }
1296 return true;
1297}
1298
nisse08582ff2016-02-04 01:24:52 -08001299void WebRtcSession::SetVideoPlayout(
1300 uint32_t ssrc,
1301 bool enable,
1302 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 ASSERT(signaling_thread()->IsCurrent());
1304 if (!video_channel_) {
1305 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1306 return;
1307 }
nisse08582ff2016-02-04 01:24:52 -08001308 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1309 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 // This in the normal case when the underlying media channel has already
1311 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001312 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 }
1314}
1315
Peter Boström0c4e06b2015-10-07 12:23:21 +02001316void WebRtcSession::SetVideoSend(uint32_t ssrc,
1317 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 const cricket::VideoOptions* options) {
1319 ASSERT(signaling_thread()->IsCurrent());
1320 if (!video_channel_) {
1321 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1322 return;
1323 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001324 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1326 // This in the normal case when the underlying media channel has already
1327 // been deleted.
1328 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
1332bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1333 ASSERT(signaling_thread()->IsCurrent());
1334 if (!voice_channel_) {
1335 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1336 return false;
1337 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001338 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1340 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001341 if (!local_desc_ ||
1342 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 &send_ssrc)) {
1344 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1345 return false;
1346 }
1347 return voice_channel_->CanInsertDtmf();
1348}
1349
1350bool WebRtcSession::InsertDtmf(const std::string& track_id,
1351 int code, int duration) {
1352 ASSERT(signaling_thread()->IsCurrent());
1353 if (!voice_channel_) {
1354 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1355 return false;
1356 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001357 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001358 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1359 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1361 return false;
1362 }
solenberg1d63dd02015-12-02 12:35:09 -08001363 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1365 return false;
1366 }
1367 return true;
1368}
1369
1370sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001371 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372}
1373
wu@webrtc.org78187522013-10-07 23:32:02 +00001374bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001375 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001376 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001377 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001378 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1379 return false;
1380 }
1381 return data_channel_->SendData(params, payload, result);
1382}
1383
1384bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001385 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001386 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1387 return false;
1388 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001389 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1390 &DataChannel::OnChannelReady);
1391 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1392 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001393 data_channel_->SignalStreamClosedRemotely.connect(
1394 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001395 return true;
1396}
1397
1398void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001399 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001400 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1401 return;
1402 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001403 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1404 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001405 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001406}
1407
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001408void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001409 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001410 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1411 return;
1412 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001413 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1414 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001415}
1416
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001417void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001418 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001419 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1420 << "NULL.";
1421 return;
1422 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001423 data_channel_->RemoveRecvStream(sid);
1424 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001425}
1426
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001427bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001428 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001429}
1430
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001431cricket::DataChannelType WebRtcSession::data_channel_type() const {
1432 return data_channel_type_;
1433}
1434
wu@webrtc.org91053e72013-08-10 07:18:04 +00001435bool WebRtcSession::IceRestartPending() const {
1436 return ice_restart_latch_->Get();
1437}
1438
1439void WebRtcSession::ResetIceRestartLatch() {
1440 ice_restart_latch_->Reset();
1441}
1442
Henrik Boströmd8281982015-08-27 10:12:24 +02001443void WebRtcSession::OnCertificateReady(
1444 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001445 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001446}
1447
Henrik Boströmd8281982015-08-27 10:12:24 +02001448bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001449 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001450}
1451
deadbeefcbecd352015-09-23 11:50:27 -07001452const rtc::scoped_refptr<rtc::RTCCertificate>&
1453WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001454 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001455}
1456
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457void WebRtcSession::SetIceConnectionState(
1458 PeerConnectionInterface::IceConnectionState state) {
1459 if (ice_connection_state_ == state) {
1460 return;
1461 }
1462
1463 // ASSERT that the requested transition is allowed. Note that
1464 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1465 // within PeerConnection). This switch statement should compile away when
1466 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001467 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1468 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 switch (ice_connection_state_) {
1470 case PeerConnectionInterface::kIceConnectionNew:
1471 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1472 break;
1473 case PeerConnectionInterface::kIceConnectionChecking:
1474 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1475 state == PeerConnectionInterface::kIceConnectionConnected);
1476 break;
1477 case PeerConnectionInterface::kIceConnectionConnected:
1478 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1479 state == PeerConnectionInterface::kIceConnectionChecking ||
1480 state == PeerConnectionInterface::kIceConnectionCompleted);
1481 break;
1482 case PeerConnectionInterface::kIceConnectionCompleted:
1483 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1484 state == PeerConnectionInterface::kIceConnectionDisconnected);
1485 break;
1486 case PeerConnectionInterface::kIceConnectionFailed:
1487 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1488 break;
1489 case PeerConnectionInterface::kIceConnectionDisconnected:
1490 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1491 state == PeerConnectionInterface::kIceConnectionConnected ||
1492 state == PeerConnectionInterface::kIceConnectionCompleted ||
1493 state == PeerConnectionInterface::kIceConnectionFailed);
1494 break;
1495 case PeerConnectionInterface::kIceConnectionClosed:
1496 ASSERT(false);
1497 break;
1498 default:
1499 ASSERT(false);
1500 break;
1501 }
1502
1503 ice_connection_state_ = state;
1504 if (ice_observer_) {
1505 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1506 }
1507}
1508
deadbeefcbecd352015-09-23 11:50:27 -07001509void WebRtcSession::OnTransportControllerConnectionState(
1510 cricket::IceConnectionState state) {
1511 switch (state) {
1512 case cricket::kIceConnectionConnecting:
1513 // If the current state is Connected or Completed, then there were
1514 // writable channels but now there are not, so the next state must
1515 // be Disconnected.
1516 // kIceConnectionConnecting is currently used as the default,
1517 // un-connected state by the TransportController, so its only use is
1518 // detecting disconnections.
1519 if (ice_connection_state_ ==
1520 PeerConnectionInterface::kIceConnectionConnected ||
1521 ice_connection_state_ ==
1522 PeerConnectionInterface::kIceConnectionCompleted) {
1523 SetIceConnectionState(
1524 PeerConnectionInterface::kIceConnectionDisconnected);
1525 }
torbjornga81a42f2015-09-23 02:16:58 -07001526 break;
deadbeefcbecd352015-09-23 11:50:27 -07001527 case cricket::kIceConnectionFailed:
1528 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1529 break;
1530 case cricket::kIceConnectionConnected:
1531 LOG(LS_INFO) << "Changing to ICE connected state because "
1532 << "all transports are writable.";
1533 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1534 break;
1535 case cricket::kIceConnectionCompleted:
1536 LOG(LS_INFO) << "Changing to ICE completed state because "
1537 << "all transports are complete.";
1538 if (ice_connection_state_ !=
1539 PeerConnectionInterface::kIceConnectionConnected) {
1540 // If jumping directly from "checking" to "connected",
1541 // signal "connected" first.
1542 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1543 }
1544 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1545 if (metrics_observer_) {
1546 ReportTransportStats();
1547 }
1548 break;
1549 default:
1550 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001551 }
deadbeefcbecd352015-09-23 11:50:27 -07001552}
1553
1554void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001555 SetIceConnectionReceiving(receiving);
1556}
1557
1558void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1559 if (ice_connection_receiving_ == receiving) {
1560 return;
1561 }
1562 ice_connection_receiving_ = receiving;
1563 if (ice_observer_) {
1564 ice_observer_->OnIceConnectionReceivingChange(receiving);
1565 }
1566}
1567
deadbeefcbecd352015-09-23 11:50:27 -07001568void WebRtcSession::OnTransportControllerCandidatesGathered(
1569 const std::string& transport_name,
1570 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001572 int sdp_mline_index;
1573 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1574 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1575 << transport_name << " not found";
1576 return;
1577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578
deadbeefcbecd352015-09-23 11:50:27 -07001579 for (cricket::Candidates::const_iterator citer = candidates.begin();
1580 citer != candidates.end(); ++citer) {
1581 // Use transport_name as the candidate media id.
1582 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1583 if (ice_observer_) {
1584 ice_observer_->OnIceCandidate(&candidate);
1585 }
1586 if (local_desc_) {
1587 local_desc_->AddCandidate(&candidate);
1588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 }
1590}
1591
1592// Enabling voice and video channel.
1593void WebRtcSession::EnableChannels() {
1594 if (voice_channel_ && !voice_channel_->enabled())
1595 voice_channel_->Enable(true);
1596
1597 if (video_channel_ && !video_channel_->enabled())
1598 video_channel_->Enable(true);
1599
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001600 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 data_channel_->Enable(true);
1602}
1603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604// Returns the media index for a local ice candidate given the content name.
1605bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1606 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001607 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610
1611 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001612 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 for (size_t index = 0; index < contents.size(); ++index) {
1614 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001615 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 content_found = true;
1617 break;
1618 }
1619 }
1620 return content_found;
1621}
1622
1623bool WebRtcSession::UseCandidatesInSessionDescription(
1624 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001625 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001627 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1631 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001632 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001633 const IceCandidateInterface* candidate = candidates->at(n);
1634 bool valid = false;
1635 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1636 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001637 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1638 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001639 }
1640 continue;
1641 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001642 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001643 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 break;
deadbeefd59daf82015-10-14 15:02:44 -07001645 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 }
1647 }
1648 return ret;
1649}
1650
1651bool WebRtcSession::UseCandidate(
1652 const IceCandidateInterface* candidate) {
1653
1654 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001655 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 if (mediacontent_index >= remote_content_size) {
1657 LOG(LS_ERROR)
1658 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1659 return false;
1660 }
1661
1662 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001663 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 std::vector<cricket::Candidate> candidates;
1665 candidates.push_back(candidate->candidate());
1666 // Invoking BaseSession method to handle remote candidates.
1667 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001668 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1669 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 // Candidates successfully submitted for checking.
1671 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1672 ice_connection_state_ ==
1673 PeerConnectionInterface::kIceConnectionDisconnected) {
1674 // If state is New, then the session has just gotten its first remote ICE
1675 // candidates, so go to Checking.
1676 // If state is Disconnected, the session is re-using old candidates or
1677 // receiving additional ones, so go to Checking.
1678 // If state is Connected, stay Connected.
1679 // TODO(bemasc): If state is Connected, and the new candidates are for a
1680 // newly added transport, then the state actually _should_ move to
1681 // checking. Add a way to distinguish that case.
1682 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1683 }
1684 // TODO(bemasc): If state is Completed, go back to Connected.
1685 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001686 if (!error.empty()) {
1687 LOG(LS_WARNING) << error;
1688 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 }
1690 return true;
1691}
1692
deadbeefcbecd352015-09-23 11:50:27 -07001693void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001694 // Destroy video_channel_ first since it may have a pointer to the
1695 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 const cricket::ContentInfo* video_info =
1697 cricket::GetFirstVideoContent(desc);
1698 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 }
1702
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001703 const cricket::ContentInfo* voice_info =
1704 cricket::GetFirstAudioContent(desc);
1705 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001706 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001707 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001708 }
1709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 const cricket::ContentInfo* data_info =
1711 cricket::GetFirstDataContent(desc);
1712 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 }
1716}
1717
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001718// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001719// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 // Creating the media channels and transport proxies.
1722 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1723 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001724 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 LOG(LS_ERROR) << "Failed to create voice channel.";
1726 return false;
1727 }
1728 }
1729
1730 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1731 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001732 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 LOG(LS_ERROR) << "Failed to create video channel.";
1734 return false;
1735 }
1736 }
1737
1738 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1739 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001740 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001741 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 LOG(LS_ERROR) << "Failed to create data channel.";
1743 return false;
1744 }
1745 }
1746
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001747 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1748 if (voice_channel()) {
1749 voice_channel()->ActivateRtcpMux();
1750 }
1751 if (video_channel()) {
1752 video_channel()->ActivateRtcpMux();
1753 }
1754 if (data_channel()) {
1755 data_channel()->ActivateRtcpMux();
1756 }
1757 }
1758
deadbeefcbecd352015-09-23 11:50:27 -07001759 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001760 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001761 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1762 cricket::GROUP_TYPE_BUNDLE);
1763 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001764 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1765 return false;
1766 }
deadbeefcbecd352015-09-23 11:50:27 -07001767 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001768 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1769 return false;
1770 }
1771 }
1772
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 return true;
1774}
1775
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001776bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001778 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001779 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001780 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001781 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001782 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001783
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001784 voice_channel_->SignalDtlsSetupFailure.connect(
1785 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001786
1787 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001788 voice_channel_->transport_channel()->SignalSentPacket.connect(
1789 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001790 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001795 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001796 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001797 if (!video_channel_) {
1798 return false;
1799 }
1800
1801 video_channel_->SignalDtlsSetupFailure.connect(
1802 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001803
1804 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001805 video_channel_->transport_channel()->SignalSentPacket.connect(
1806 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001807 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808}
1809
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001810bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001811 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001813 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001814 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001815 return false;
1816 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001817
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001818 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001819 data_channel_->SignalDataReceived.connect(
1820 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001821 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001822
1823 data_channel_->SignalDtlsSetupFailure.connect(
1824 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001825
1826 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001827 data_channel_->transport_channel()->SignalSentPacket.connect(
1828 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001829 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830}
1831
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001832void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001833 SetError(ERROR_TRANSPORT,
1834 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835}
1836
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001837void WebRtcSession::OnDataChannelMessageReceived(
1838 cricket::DataChannel* channel,
1839 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001840 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001841 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1842 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1843 // Received OPEN message; parse and signal that a new data channel should
1844 // be created.
1845 std::string label;
1846 InternalDataChannelInit config;
1847 config.id = params.ssrc;
1848 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1849 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1850 << params.ssrc;
1851 return;
1852 }
1853 config.open_handshake_role = InternalDataChannelInit::kAcker;
1854 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 }
deadbeefab9b2d12015-10-14 11:33:11 -07001856 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857}
1858
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001859// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001860bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001861 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1862 if (!bundle_enabled)
1863 return true;
1864
1865 const cricket::ContentGroup* bundle_group =
1866 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1867 ASSERT(bundle_group != NULL);
1868
1869 const cricket::ContentInfos& contents = desc->contents();
1870 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1871 citer != contents.end(); ++citer) {
1872 const cricket::ContentInfo* content = (&*citer);
1873 ASSERT(content != NULL);
1874 if (bundle_group->HasContentName(content->name) &&
1875 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1876 if (!HasRtcpMuxEnabled(content))
1877 return false;
1878 }
1879 }
1880 // RTCP-MUX is enabled in all the contents.
1881 return true;
1882}
1883
1884bool WebRtcSession::HasRtcpMuxEnabled(
1885 const cricket::ContentInfo* content) {
1886 const cricket::MediaContentDescription* description =
1887 static_cast<cricket::MediaContentDescription*>(content->description);
1888 return description->rtcp_mux();
1889}
1890
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001891bool WebRtcSession::ValidateSessionDescription(
1892 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001893 cricket::ContentSource source, std::string* err_desc) {
1894 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001895 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001896 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001897 }
1898
1899 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001900 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001901 }
1902
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001903 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001904 Action action = GetAction(sdesc->type());
1905 if (source == cricket::CS_LOCAL) {
1906 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001907 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001908 } else {
1909 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001910 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001911 }
1912
1913 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001914 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001915 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1916 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001917 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001918 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001919 }
1920
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001921 // Verify ice-ufrag and ice-pwd.
1922 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001923 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001924 }
1925
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001926 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001927 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001928 }
1929
1930 // Verify m-lines in Answer when compared against Offer.
1931 if (action == kAnswer) {
1932 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001933 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1934 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001935 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937 }
1938 }
1939
1940 return true;
1941}
1942
1943bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1944 return ((action == kOffer && state() == STATE_INIT) ||
1945 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001946 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001947 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001948 (action == kOffer && state() == STATE_INPROGRESS) ||
1949 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001950 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1951 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1952 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1953 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954}
1955
1956bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1957 return ((action == kOffer && state() == STATE_INIT) ||
1958 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001959 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001960 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001961 (action == kOffer && state() == STATE_INPROGRESS) ||
1962 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001963 (action == kAnswer && state() == STATE_SENTOFFER) ||
1964 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1965 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1966 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001967}
1968
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001969std::string WebRtcSession::GetSessionErrorMsg() {
1970 std::ostringstream desc;
1971 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1972 desc << kSessionErrorDesc << error_desc() << ".";
1973 return desc.str();
1974}
1975
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001976// We need to check the local/remote description for the Transport instead of
1977// the session, because a new Transport added during renegotiation may have
1978// them unset while the session has them set from the previous negotiation.
1979// Not doing so may trigger the auto generation of transport description and
1980// mess up DTLS identity information, ICE credential, etc.
1981bool WebRtcSession::ReadyToUseRemoteCandidate(
1982 const IceCandidateInterface* candidate,
1983 const SessionDescriptionInterface* remote_desc,
1984 bool* valid) {
1985 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001986
1987 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001988 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001989
deadbeefd59daf82015-10-14 15:02:44 -07001990 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001991 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001992 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001993
1994 size_t mediacontent_index =
1995 static_cast<size_t>(candidate->sdp_mline_index());
1996 size_t remote_content_size =
1997 current_remote_desc->description()->contents().size();
1998 if (mediacontent_index >= remote_content_size) {
1999 LOG(LS_ERROR)
2000 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2001
2002 *valid = false;
2003 return false;
2004 }
2005
2006 cricket::ContentInfo content =
2007 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002008 cricket::BaseChannel* channel = GetChannel(content.name);
2009 if (!channel) {
2010 return false;
2011 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002012
deadbeefd59daf82015-10-14 15:02:44 -07002013 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002014 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002015}
2016
deadbeefcbecd352015-09-23 11:50:27 -07002017void WebRtcSession::OnTransportControllerGatheringState(
2018 cricket::IceGatheringState state) {
2019 ASSERT(signaling_thread()->IsCurrent());
2020 if (state == cricket::kIceGatheringGathering) {
2021 if (ice_observer_) {
2022 ice_observer_->OnIceGatheringChange(
2023 PeerConnectionInterface::kIceGatheringGathering);
2024 }
2025 } else if (state == cricket::kIceGatheringComplete) {
2026 if (ice_observer_) {
2027 ice_observer_->OnIceGatheringChange(
2028 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002029 }
2030 }
2031}
2032
2033void WebRtcSession::ReportTransportStats() {
2034 // Use a set so we don't report the same stats twice if two channels share
2035 // a transport.
2036 std::set<std::string> transport_names;
2037 if (voice_channel()) {
2038 transport_names.insert(voice_channel()->transport_name());
2039 }
2040 if (video_channel()) {
2041 transport_names.insert(video_channel()->transport_name());
2042 }
2043 if (data_channel()) {
2044 transport_names.insert(data_channel()->transport_name());
2045 }
2046 for (const auto& name : transport_names) {
2047 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002048 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002049 ReportBestConnectionState(stats);
2050 ReportNegotiatedCiphers(stats);
2051 }
2052 }
2053}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002054// Walk through the ConnectionInfos to gather best connection usage
2055// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002056void WebRtcSession::ReportBestConnectionState(
2057 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002058 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002059 for (cricket::TransportChannelStatsList::const_iterator it =
2060 stats.channel_stats.begin();
2061 it != stats.channel_stats.end(); ++it) {
2062 for (cricket::ConnectionInfos::const_iterator it_info =
2063 it->connection_infos.begin();
2064 it_info != it->connection_infos.end(); ++it_info) {
2065 if (!it_info->best_connection) {
2066 continue;
2067 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002068
2069 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2070 const cricket::Candidate& local = it_info->local_candidate;
2071 const cricket::Candidate& remote = it_info->remote_candidate;
2072
2073 // Increment the counter for IceCandidatePairType.
2074 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2075 (local.type() == RELAY_PORT_TYPE &&
2076 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2077 type = kEnumCounterIceCandidatePairTypeTcp;
2078 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2079 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002080 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002081 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002082 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002083 metrics_observer_->IncrementEnumCounter(
2084 type, GetIceCandidatePairCounter(local, remote),
2085 kIceCandidatePairMax);
2086
2087 // Increment the counter for IP type.
2088 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002089 metrics_observer_->IncrementEnumCounter(
2090 kEnumCounterAddressFamily, kBestConnections_IPv4,
2091 kPeerConnectionAddressFamilyCounter_Max);
2092
2093 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002094 metrics_observer_->IncrementEnumCounter(
2095 kEnumCounterAddressFamily, kBestConnections_IPv6,
2096 kPeerConnectionAddressFamilyCounter_Max);
2097 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002098 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002099 }
2100
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002101 return;
2102 }
2103 }
2104}
2105
jbauchac8869e2015-07-03 01:36:14 -07002106void WebRtcSession::ReportNegotiatedCiphers(
2107 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002108 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002109 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2110 return;
2111 }
2112
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002113 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2114 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2115 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2116 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002117 return;
2118 }
2119
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002120 PeerConnectionEnumCounterType srtp_counter_type;
2121 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002122 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002123 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2124 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002125 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002126 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2127 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002128 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002129 srtp_counter_type = kEnumCounterDataSrtpCipher;
2130 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002131 } else {
2132 RTC_NOTREACHED();
2133 return;
2134 }
2135
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002136 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2137 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2138 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002139 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002140 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2141 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2142 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002143 }
2144}
2145
stefanc1aeaf02015-10-15 07:26:07 -07002146void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2147 const rtc::SentPacket& sent_packet) {
2148 RTC_DCHECK(worker_thread()->IsCurrent());
2149 media_controller_->call_w()->OnSentPacket(sent_packet);
2150}
2151
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152} // namespace webrtc