blob: 455be0f80890afc059bffae40b8c0b47749e3916 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/webrtcsession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
pbos@webrtc.org371243d2014-03-07 15:22:04 +000013#include <limits.h>
14
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <algorithm>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <set>
Tommif888bb52015-12-12 01:37:01 +010017#include <utility>
18#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/jsepicecandidate.h"
21#include "webrtc/api/jsepsessiondescription.h"
22#include "webrtc/api/mediaconstraintsinterface.h"
23#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010026#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/constants.h"
35#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::ContentInfo;
43using cricket::ContentInfos;
44using cricket::MediaContentDescription;
45using cricket::SessionDescription;
46using cricket::TransportInfo;
47
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070048using cricket::LOCAL_PORT_TYPE;
49using cricket::STUN_PORT_TYPE;
50using cricket::RELAY_PORT_TYPE;
51using cricket::PRFLX_PORT_TYPE;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053namespace webrtc {
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000056const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
57 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000058const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059const char kInvalidCandidates[] = "Description contains invalid candidates.";
60const char kInvalidSdp[] = "Invalid session description.";
61const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
63const char kPushDownTDFailed[] =
64 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000065const char kSdpWithoutDtlsFingerprint[] =
66 "Called with SDP without DTLS fingerprint.";
67const char kSdpWithoutSdesCrypto[] =
68 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000070 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000073const char kDtlsSetupFailureRtp[] =
74 "Couldn't set up DTLS-SRTP on RTP channel.";
75const char kDtlsSetupFailureRtcp[] =
76 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070077const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070079IceCandidatePairType GetIceCandidatePairCounter(
80 const cricket::Candidate& local,
81 const cricket::Candidate& remote) {
82 const auto& l = local.type();
83 const auto& r = remote.type();
84 const auto& host = LOCAL_PORT_TYPE;
85 const auto& srflx = STUN_PORT_TYPE;
86 const auto& relay = RELAY_PORT_TYPE;
87 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070088 if (l == host && r == host) {
89 bool local_private = IPIsPrivate(local.address().ipaddr());
90 bool remote_private = IPIsPrivate(remote.address().ipaddr());
91 if (local_private) {
92 if (remote_private) {
93 return kIceCandidatePairHostPrivateHostPrivate;
94 } else {
95 return kIceCandidatePairHostPrivateHostPublic;
96 }
97 } else {
98 if (remote_private) {
99 return kIceCandidatePairHostPublicHostPrivate;
100 } else {
101 return kIceCandidatePairHostPublicHostPublic;
102 }
103 }
104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700105 if (l == host && r == srflx)
106 return kIceCandidatePairHostSrflx;
107 if (l == host && r == relay)
108 return kIceCandidatePairHostRelay;
109 if (l == host && r == prflx)
110 return kIceCandidatePairHostPrflx;
111 if (l == srflx && r == host)
112 return kIceCandidatePairSrflxHost;
113 if (l == srflx && r == srflx)
114 return kIceCandidatePairSrflxSrflx;
115 if (l == srflx && r == relay)
116 return kIceCandidatePairSrflxRelay;
117 if (l == srflx && r == prflx)
118 return kIceCandidatePairSrflxPrflx;
119 if (l == relay && r == host)
120 return kIceCandidatePairRelayHost;
121 if (l == relay && r == srflx)
122 return kIceCandidatePairRelaySrflx;
123 if (l == relay && r == relay)
124 return kIceCandidatePairRelayRelay;
125 if (l == relay && r == prflx)
126 return kIceCandidatePairRelayPrflx;
127 if (l == prflx && r == host)
128 return kIceCandidatePairPrflxHost;
129 if (l == prflx && r == srflx)
130 return kIceCandidatePairPrflxSrflx;
131 if (l == prflx && r == relay)
132 return kIceCandidatePairPrflxRelay;
133 return kIceCandidatePairMax;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136// Compares |answer| against |offer|. Comparision is done
137// for number of m-lines in answer against offer. If matches true will be
138// returned otherwise false.
139static bool VerifyMediaDescriptions(
140 const SessionDescription* answer, const SessionDescription* offer) {
141 if (offer->contents().size() != answer->contents().size())
142 return false;
143
144 for (size_t i = 0; i < offer->contents().size(); ++i) {
145 if ((offer->contents()[i].name) != answer->contents()[i].name) {
146 return false;
147 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000148 const MediaContentDescription* offer_mdesc =
149 static_cast<const MediaContentDescription*>(
150 offer->contents()[i].description);
151 const MediaContentDescription* answer_mdesc =
152 static_cast<const MediaContentDescription*>(
153 answer->contents()[i].description);
154 if (offer_mdesc->type() != answer_mdesc->type()) {
155 return false;
156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159}
160
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161// Checks that each non-rejected content has SDES crypto keys or a DTLS
162// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
163// keys, will be caught in Transport negotiation, and backstopped by Channel's
164// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000165static bool VerifyCrypto(const SessionDescription* desc,
166 bool dtls_enabled,
167 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 const ContentInfos& contents = desc->contents();
169 for (size_t index = 0; index < contents.size(); ++index) {
170 const ContentInfo* cinfo = &contents[index];
171 if (cinfo->rejected) {
172 continue;
173 }
174
175 // If the content isn't rejected, crypto must be present.
176 const MediaContentDescription* media =
177 static_cast<const MediaContentDescription*>(cinfo->description);
178 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
179 if (!media || !tinfo) {
180 // Something is not right.
181 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return false;
184 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000185 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000187 LOG(LS_WARNING) <<
188 "Session description must have DTLS fingerprint if DTLS enabled.";
189 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 return false;
191 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000192 } else {
193 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 LOG(LS_WARNING) <<
195 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 return false;
198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 }
200 }
201
202 return true;
203}
204
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000205// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
206static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
207 const ContentInfos& contents = desc->contents();
208 for (size_t index = 0; index < contents.size(); ++index) {
209 const ContentInfo* cinfo = &contents[index];
210 if (cinfo->rejected) {
211 continue;
212 }
213
214 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
215 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
216 if (!tinfo) {
217 // Something is not right.
218 LOG(LS_ERROR) << kInvalidSdp;
219 return false;
220 }
221 if (tinfo->description.ice_ufrag.empty() ||
222 tinfo->description.ice_pwd.empty()) {
223 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
224 return false;
225 }
226 }
227 return true;
228}
229
wu@webrtc.org91053e72013-08-10 07:18:04 +0000230// Forces |sdesc->crypto_required| to the appropriate state based on the
231// current security policy, to ensure a failure occurs if there is an error
232// in crypto negotiation.
233// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000234static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
235 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 if (!sdesc) {
237 return;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 // Updating the |crypto_required_| in MediaContentDescription to the
241 // appropriate state based on the current security policy.
242 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
243 iter != sdesc->contents().end(); ++iter) {
244 if (cricket::IsMediaContent(&*iter)) {
245 MediaContentDescription* mdesc =
246 static_cast<MediaContentDescription*> (iter->description);
247 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000249 }
250 }
251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
255 const std::string& track_id,
256 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 const cricket::ContentInfo* audio_info =
258 cricket::GetFirstAudioContent(session_description);
259 if (!audio_info) {
260 LOG(LS_ERROR) << "Audio not used in this call";
261 return false;
262 }
263
264 const cricket::MediaContentDescription* audio_content =
265 static_cast<const cricket::MediaContentDescription*>(
266 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000267 const cricket::StreamParams* stream =
268 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
269 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 return false;
271 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272
273 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return true;
275}
276
277static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200278 uint32_t ssrc,
279 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 ASSERT(track_id != NULL);
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 const cricket::ContentInfo* audio_info =
283 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000284 if (audio_info) {
285 const cricket::MediaContentDescription* audio_content =
286 static_cast<const cricket::MediaContentDescription*>(
287 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289 const auto* found =
290 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
291 if (found) {
292 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000293 return true;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
297 const cricket::ContentInfo* video_info =
298 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (video_info) {
300 const cricket::MediaContentDescription* video_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311 return false;
312}
313
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000314static bool BadSdp(const std::string& source,
315 const std::string& type,
316 const std::string& reason,
317 std::string* err_desc) {
318 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700319 desc << "Failed to set " << source;
320 if (!type.empty()) {
321 desc << " " << type;
322 }
323 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000326 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 return false;
330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 }
341}
342
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343static bool BadLocalSdp(const std::string& type,
344 const std::string& reason,
345 std::string* err_desc) {
346 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
347}
348
349static bool BadRemoteSdp(const std::string& type,
350 const std::string& reason,
351 std::string* err_desc) {
352 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
353}
354
355static bool BadOfferSdp(cricket::ContentSource source,
356 const std::string& reason,
357 std::string* err_desc) {
358 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
359}
360
361static bool BadPranswerSdp(cricket::ContentSource source,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
365 reason, err_desc);
366}
367
368static bool BadAnswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
deadbeefd59daf82015-10-14 15:02:44 -0700374#define GET_STRING_OF_STATE(state) \
375 case webrtc::WebRtcSession::state: \
376 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 break;
378
deadbeefd59daf82015-10-14 15:02:44 -0700379static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 std::string result;
381 switch (state) {
382 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700383 GET_STRING_OF_STATE(STATE_SENTOFFER)
384 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
385 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
386 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700388 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 default:
390 ASSERT(false);
391 break;
392 }
393 return result;
394}
395
deadbeefd59daf82015-10-14 15:02:44 -0700396#define GET_STRING_OF_ERROR_CODE(err) \
397 case webrtc::WebRtcSession::err: \
398 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 break;
400
deadbeefd59daf82015-10-14 15:02:44 -0700401static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 std::string result;
403 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000405 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
406 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
deadbeefd59daf82015-10-14 15:02:44 -0700408 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414static std::string MakeErrorString(const std::string& error,
415 const std::string& desc) {
416 std::ostringstream ret;
417 ret << error << " " << desc;
418 return ret.str();
419}
420
421static std::string MakeTdErrorString(const std::string& desc) {
422 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000425// Set |option| to the highest-priority value of |key| in the optional
426// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700427template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000428static void SetOptionFromOptionalConstraint(
429 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700430 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100431 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000432 if (!constraints) {
433 return;
434 }
435 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000436 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000437 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100439 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440 }
441 }
442}
443
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000445 PeerConnectionInterface::IceTransportsType type) {
446 switch (type) {
447 case PeerConnectionInterface::kNone:
448 return cricket::CF_NONE;
449 case PeerConnectionInterface::kRelay:
450 return cricket::CF_RELAY;
451 case PeerConnectionInterface::kNoHost:
452 return (cricket::CF_ALL & ~cricket::CF_HOST);
453 case PeerConnectionInterface::kAll:
454 return cricket::CF_ALL;
455 default: ASSERT(false);
456 }
457 return cricket::CF_NONE;
458}
459
deadbeef0ed85b22016-02-23 17:24:52 -0800460// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
461bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
462 const SessionDescriptionInterface* new_desc,
463 const std::string& content_name) {
464 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700465 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
deadbeef0ed85b22016-02-23 17:24:52 -0800467 const SessionDescription* new_sd = new_desc->description();
468 const SessionDescription* old_sd = old_desc->description();
469 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
470 if (!cinfo || cinfo->rejected) {
471 return false;
472 }
473 // If the content isn't rejected, check if ufrag and password has changed.
474 const cricket::TransportDescription* new_transport_desc =
475 new_sd->GetTransportDescriptionByName(content_name);
476 const cricket::TransportDescription* old_transport_desc =
477 old_sd->GetTransportDescriptionByName(content_name);
478 if (!new_transport_desc || !old_transport_desc) {
479 // No transport description exists. This is not an ICE restart.
480 return false;
481 }
482 if (cricket::IceCredentialsChanged(
483 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
484 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
485 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
486 << ".";
487 return true;
488 }
489 return false;
490}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
stefanc1aeaf02015-10-15 07:26:07 -0700492WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700493 rtc::Thread* signaling_thread,
494 rtc::Thread* worker_thread,
495 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700496 : signaling_thread_(signaling_thread),
497 worker_thread_(worker_thread),
498 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 // RFC 3264: The numeric value of the session id and version in the
500 // o line MUST be representable with a "64 bit signed integer".
501 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700502 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
503 transport_controller_(new cricket::TransportController(signaling_thread,
504 worker_thread,
505 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700506 media_controller_(media_controller),
507 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 ice_observer_(NULL),
509 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700510 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000512 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000514 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700515 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
516 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700517 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700518 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700519 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700520 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700521 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700522 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700523 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524}
525
526WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700527 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000528 // Destroy video_channel_ first since it may have a pointer to the
529 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000530 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 SignalVideoChannelDestroyed();
532 channel_manager_->DestroyVideoChannel(video_channel_.release());
533 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000534 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000535 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200536 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000537 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000538 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 SignalDataChannelDestroyed();
540 channel_manager_->DestroyDataChannel(data_channel_.release());
541 }
deadbeef057ecf02016-01-20 14:30:43 -0800542 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700543
544 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000548 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700549 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200550 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200551 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
552 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700553 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700554 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000555
Henrik Boström87713d02015-08-25 09:53:21 +0200556 // Obtain a certificate from RTCConfiguration if any were provided (optional).
557 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
558 if (!rtc_configuration.certificates.empty()) {
559 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
560 // just picking the first one. The decision should be made based on the DTLS
561 // handshake. The DTLS negotiations need to know about all certificates.
562 certificate = rtc_configuration.certificates[0];
563 }
564
honghaiz1f429e32015-09-28 07:57:34 -0700565 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700566
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 // TODO(perkj): Take |constraints| into consideration. Return false if not all
568 // mandatory constraints can be fulfilled. Note that |constraints|
569 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000571
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000572 if (options.disable_encryption) {
573 dtls_enabled_ = false;
574 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200575 // Enable DTLS by default if we have an identity store or a certificate.
576 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000577 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700578 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
579 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000580 dtls_enabled_ = value;
581 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000582 }
583
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000585 // It takes precendence over the disable_sctp_data_channels
586 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 if (FindConstraint(
588 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
589 &value, NULL) && value) {
590 LOG(LS_INFO) << "Allowing RTP data engine.";
591 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000592 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000593 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000594 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000595 LOG(LS_INFO) << "Allowing SCTP data engine.";
596 data_channel_type_ = cricket::DCT_SCTP;
597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000600 SetOptionFromOptionalConstraint(constraints,
601 MediaConstraintsInterface::kScreencastMinBitrate,
nisseb163c3f2016-01-29 01:14:38 -0800602 &video_options_.screencast_min_bitrate_kbps);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000603
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000604 SetOptionFromOptionalConstraint(constraints,
605 MediaConstraintsInterface::kCombinedAudioVideoBwe,
606 &audio_options_.combined_audio_video_bwe);
607
kwiberg102c6a62015-10-30 02:47:38 -0700608 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100609 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200610
Karl Wibergbe579832015-11-10 22:34:18 +0100611 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
612 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200613
Henrik Boström87713d02015-08-25 09:53:21 +0200614 if (!dtls_enabled_) {
615 // Construct with DTLS disabled.
616 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700617 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200618 } else {
619 // Construct with DTLS enabled.
620 if (!certificate) {
621 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700622 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200623 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800624 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700625 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200626 } else {
627 // Use the already generated certificate.
628 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700629 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200630 }
631 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632
Henrik Boströmd8281982015-08-27 10:12:24 +0200633 webrtc_session_desc_factory_->SignalCertificateReady.connect(
634 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000635
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000637 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000638 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000639 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200640 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 return true;
643}
644
deadbeefd59daf82015-10-14 15:02:44 -0700645void WebRtcSession::Close() {
646 SetState(STATE_CLOSED);
647 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000648 ASSERT(!voice_channel_);
649 ASSERT(!video_channel_);
650 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800651 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652}
653
deadbeef0ed85b22016-02-23 17:24:52 -0800654cricket::BaseChannel* WebRtcSession::GetChannel(
655 const std::string& content_name) {
656 if (voice_channel() && voice_channel()->content_name() == content_name) {
657 return voice_channel();
658 }
659 if (video_channel() && video_channel()->content_name() == content_name) {
660 return video_channel();
661 }
662 if (data_channel() && data_channel()->content_name() == content_name) {
663 return data_channel();
664 }
665 return nullptr;
666}
667
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000668void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
669 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670}
671
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000672cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
673 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674}
675
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800676bool WebRtcSession::GetSslRole(const std::string& transport_name,
677 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700678 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000679 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
680 << "SSL Role of the session.";
681 return false;
682 }
683
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800684 return transport_controller_->GetSslRole(transport_name, role);
685}
686
687bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
688 rtc::SSLRole* role) {
689 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000690}
691
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000692void WebRtcSession::CreateOffer(
693 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700694 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
695 const cricket::MediaSessionOptions& session_options) {
696 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000697}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698
deadbeefab9b2d12015-10-14 11:33:11 -0700699void WebRtcSession::CreateAnswer(
700 CreateSessionDescriptionObserver* observer,
701 const MediaConstraintsInterface* constraints,
702 const cricket::MediaSessionOptions& session_options) {
703 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
704 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705}
706
707bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
708 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700709 ASSERT(signaling_thread()->IsCurrent());
710
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000711 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000712 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000713
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000714 // Validate SDP.
715 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
716 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718
deadbeefd59daf82015-10-14 15:02:44 -0700719 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000720 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700722 initial_offerer_ = true;
723 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000726 cricket::SecurePolicy sdes_policy =
727 webrtc_session_desc_factory_->SdesPolicy();
728 cricket::CryptoType crypto_required = dtls_enabled_ ?
729 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
730 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000732 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000734 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
736 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000737 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 // TODO(mallinath) - Handle CreateChannel failure, as new local description
739 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000740 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 }
742
deadbeefcbecd352015-09-23 11:50:27 -0700743 // Remove unused channels if MediaContentDescription is rejected.
744 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000746 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return false;
748 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000749
deadbeefd59daf82015-10-14 15:02:44 -0700750 if (remote_desc_) {
751 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700752 UseCandidatesInSessionDescription(remote_desc_.get());
753 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
deadbeef0ed85b22016-02-23 17:24:52 -0800755 pending_ice_restarts_.clear();
756
deadbeefd59daf82015-10-14 15:02:44 -0700757 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000758 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 return true;
761}
762
763bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
764 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700765 ASSERT(signaling_thread()->IsCurrent());
766
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000767 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000768 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000769
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000770 // Validate SDP.
771 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
772 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774
deadbeefd59daf82015-10-14 15:02:44 -0700775 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
776 remote_desc_.release());
777 remote_desc_.reset(desc_temp.release());
778
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000780 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 if (action == kOffer && !CreateChannels(desc->description())) {
782 // TODO(mallinath) - Handle CreateChannel failure, as new local description
783 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000784 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 }
786
deadbeefcbecd352015-09-23 11:50:27 -0700787 // Remove unused channels if MediaContentDescription is rejected.
788 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789
790 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
791 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000792 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 return false;
794 }
795
deadbeefd59daf82015-10-14 15:02:44 -0700796 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000797 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
deadbeef0ed85b22016-02-23 17:24:52 -0800800 if (old_remote_desc) {
801 for (const cricket::ContentInfo& content :
802 old_remote_desc->description()->contents()) {
803 // Check if this new SessionDescription contains new ICE ufrag and
804 // password that indicates the remote peer requests an ICE restart.
805 // TODO(deadbeef): When we start storing both the current and pending
806 // remote description, this should reset pending_ice_restarts and compare
807 // against the current description.
808 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
809 if (action == kOffer) {
810 pending_ice_restarts_.insert(content.name);
811 }
812 } else {
813 // We retain all received candidates only if ICE is not restarted.
814 // When ICE is restarted, all previous candidates belong to an old
815 // generation and should not be kept.
816 // TODO(deadbeef): This goes against the W3C spec which says the remote
817 // description should only contain candidates from the last set remote
818 // description plus any candidates added since then. We should remove
819 // this once we're sure it won't break anything.
820 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
821 old_remote_desc.get(), content.name, desc);
822 }
823 }
honghaiz503726c2015-07-31 12:37:38 -0700824 }
825
deadbeefd59daf82015-10-14 15:02:44 -0700826 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000827 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000829
830 // Set the the ICE connection state to connecting since the connection may
831 // become writable with peer reflexive candidates before any remote candidate
832 // is signaled.
833 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
834 // is to have a new signal the indicates a change in checking state from the
835 // transport and expose a new checking() member from transport that can be
836 // read to determine the current checking state. The existing SignalConnecting
837 // actually means "gathering candidates", so cannot be be used here.
838 if (desc->type() != SessionDescriptionInterface::kOffer &&
839 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
840 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
841 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 return true;
843}
844
deadbeefd59daf82015-10-14 15:02:44 -0700845void WebRtcSession::LogState(State old_state, State new_state) {
846 LOG(LS_INFO) << "Session:" << id()
847 << " Old state:" << GetStateString(old_state)
848 << " New state:" << GetStateString(new_state);
849}
850
851void WebRtcSession::SetState(State state) {
852 ASSERT(signaling_thread_->IsCurrent());
853 if (state != state_) {
854 LogState(state_, state);
855 state_ = state;
856 SignalState(this, state_);
857 }
858}
859
860void WebRtcSession::SetError(Error error, const std::string& error_desc) {
861 ASSERT(signaling_thread_->IsCurrent());
862 if (error != error_) {
863 error_ = error;
864 error_desc_ = error_desc;
865 }
866}
867
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868bool WebRtcSession::UpdateSessionState(
869 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700871 ASSERT(signaling_thread()->IsCurrent());
872
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 // If there's already a pending error then no state transition should happen.
874 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700875 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000876 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
879 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
deadbeefd59daf82015-10-14 15:02:44 -0700881 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
882 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000883 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700884 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000885 }
deadbeefd59daf82015-10-14 15:02:44 -0700886 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000887 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000890 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
891 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700894 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
895 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000896 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700897 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000898 }
deadbeefd59daf82015-10-14 15:02:44 -0700899 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000900 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700903 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700904 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700905 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700906 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700907 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800908 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700909 const cricket::ContentGroup* answer_bundle =
910 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
911 if (!EnableBundle(*answer_bundle)) {
912 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
913 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
914 }
915 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800916 // Only push down the transport description after enabling BUNDLE; we don't
917 // want to push down a description on a transport about to be destroyed.
918 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
919 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700922 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000923 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700924 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000925 }
deadbeefd59daf82015-10-14 15:02:44 -0700926 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000927 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929 }
930 return true;
931}
932
933WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
934 if (type == SessionDescriptionInterface::kOffer) {
935 return WebRtcSession::kOffer;
936 } else if (type == SessionDescriptionInterface::kPrAnswer) {
937 return WebRtcSession::kPrAnswer;
938 } else if (type == SessionDescriptionInterface::kAnswer) {
939 return WebRtcSession::kAnswer;
940 }
941 ASSERT(false && "unknown action type");
942 return WebRtcSession::kOffer;
943}
944
deadbeefd59daf82015-10-14 15:02:44 -0700945bool WebRtcSession::PushdownMediaDescription(
946 cricket::ContentAction action,
947 cricket::ContentSource source,
948 std::string* err) {
949 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
950 if (!ch) {
951 return true;
952 } else if (source == cricket::CS_LOCAL) {
953 return ch->PushdownLocalDescription(local_desc_->description(), action,
954 err);
955 } else {
956 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
957 err);
958 }
959 };
960
961 return (set_content(voice_channel()) &&
962 set_content(video_channel()) &&
963 set_content(data_channel()));
964}
965
966bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
967 cricket::ContentAction action,
968 std::string* error_desc) {
969 RTC_DCHECK(signaling_thread()->IsCurrent());
970
971 if (source == cricket::CS_LOCAL) {
972 return PushdownLocalTransportDescription(local_desc_->description(), action,
973 error_desc);
974 }
975 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
976 error_desc);
977}
978
979bool WebRtcSession::PushdownLocalTransportDescription(
980 const SessionDescription* sdesc,
981 cricket::ContentAction action,
982 std::string* err) {
983 RTC_DCHECK(signaling_thread()->IsCurrent());
984
985 if (!sdesc) {
986 return false;
987 }
988
989 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
990 if (!transport_controller_->SetLocalTransportDescription(
991 tinfo.content_name, tinfo.description, action, err)) {
992 return false;
993 }
994 }
995
996 return true;
997}
998
999bool WebRtcSession::PushdownRemoteTransportDescription(
1000 const SessionDescription* sdesc,
1001 cricket::ContentAction action,
1002 std::string* err) {
1003 RTC_DCHECK(signaling_thread()->IsCurrent());
1004
1005 if (!sdesc) {
1006 return false;
1007 }
1008
1009 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1010 if (!transport_controller_->SetRemoteTransportDescription(
1011 tinfo.content_name, tinfo.description, action, err)) {
1012 return false;
1013 }
1014 }
1015
1016 return true;
1017}
1018
1019bool WebRtcSession::GetTransportDescription(
1020 const SessionDescription* description,
1021 const std::string& content_name,
1022 cricket::TransportDescription* tdesc) {
1023 if (!description || !tdesc) {
1024 return false;
1025 }
1026 const TransportInfo* transport_info =
1027 description->GetTransportInfoByName(content_name);
1028 if (!transport_info) {
1029 return false;
1030 }
1031 *tdesc = transport_info->description;
1032 return true;
1033}
1034
1035bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001036 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001037 return (GetChannelTransportStats(voice_channel(), stats) &&
1038 GetChannelTransportStats(video_channel(), stats) &&
1039 GetChannelTransportStats(data_channel(), stats));
1040}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001041
deadbeefcbecd352015-09-23 11:50:27 -07001042bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001043 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001044 ASSERT(signaling_thread()->IsCurrent());
1045 if (!ch) {
1046 // Not using this channel.
1047 return true;
1048 }
1049
1050 const std::string& content_name = ch->content_name();
1051 const std::string& transport_name = ch->transport_name();
1052 stats->proxy_to_transport[content_name] = transport_name;
1053 if (stats->transport_stats.find(transport_name) !=
1054 stats->transport_stats.end()) {
1055 // Transport stats already done for this transport.
1056 return true;
1057 }
1058
1059 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001060 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001061 return false;
1062 }
1063
1064 stats->transport_stats[transport_name] = tstats;
1065 return true;
1066}
1067
1068bool WebRtcSession::GetLocalCertificate(
1069 const std::string& transport_name,
1070 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1071 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001072 return transport_controller_->GetLocalCertificate(transport_name,
1073 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001074}
1075
1076bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1077 rtc::SSLCertificate** cert) {
1078 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001079 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001080}
1081
deadbeefcbecd352015-09-23 11:50:27 -07001082bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1083 const std::string* first_content_name = bundle.FirstContentName();
1084 if (!first_content_name) {
1085 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1086 return false;
1087 }
1088 const std::string& transport_name = *first_content_name;
1089 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1090
1091 auto maybe_set_transport = [this, bundle, transport_name,
1092 first_channel](cricket::BaseChannel* ch) {
1093 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001094 return true;
1095 }
1096
deadbeefcbecd352015-09-23 11:50:27 -07001097 if (ch->transport_name() == transport_name) {
1098 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1099 << " on " << transport_name << ".";
1100 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001101 }
torbjornga81a42f2015-09-23 02:16:58 -07001102
deadbeefcbecd352015-09-23 11:50:27 -07001103 if (!ch->SetTransport(transport_name)) {
1104 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1105 return false;
1106 }
1107 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1108 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001109 return true;
1110 };
1111
deadbeefcbecd352015-09-23 11:50:27 -07001112 if (!maybe_set_transport(voice_channel()) ||
1113 !maybe_set_transport(video_channel()) ||
1114 !maybe_set_transport(data_channel())) {
1115 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001116 }
deadbeefcbecd352015-09-23 11:50:27 -07001117
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001118 return true;
1119}
1120
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001122 if (!remote_desc_) {
1123 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1124 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 return false;
1126 }
1127
1128 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001129 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 return false;
1131 }
1132
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001133 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001134 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1135 if (!valid) {
1136 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 }
1138
1139 // Add this candidate to the remote session description.
1140 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001141 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 return false;
1143 }
1144
deadbeefd59daf82015-10-14 15:02:44 -07001145 if (ready) {
1146 return UseCandidate(candidate);
1147 } else {
1148 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1149 return true;
1150 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151}
1152
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001153bool WebRtcSession::SetIceTransports(
1154 PeerConnectionInterface::IceTransportsType type) {
1155 return port_allocator()->set_candidate_filter(
1156 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001157}
1158
deadbeefd59daf82015-10-14 15:02:44 -07001159cricket::IceConfig WebRtcSession::ParseIceConfig(
1160 const PeerConnectionInterface::RTCConfiguration& config) const {
1161 cricket::IceConfig ice_config;
1162 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001163 ice_config.backup_connection_ping_interval =
1164 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001165 ice_config.gather_continually = (config.continual_gathering_policy ==
1166 PeerConnectionInterface::GATHER_CONTINUALLY);
1167 return ice_config;
1168}
1169
1170void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1171 transport_controller_->SetIceConfig(config);
1172}
1173
1174void WebRtcSession::MaybeStartGathering() {
1175 transport_controller_->MaybeStartGathering();
1176}
1177
Peter Boström0c4e06b2015-10-07 12:23:21 +02001178bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1179 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001180 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001182 }
1183 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184}
1185
Peter Boström0c4e06b2015-10-07 12:23:21 +02001186bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1187 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001188 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001189 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001190 }
1191 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192}
1193
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001194std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return desc.str();
1198}
1199
solenbergd4cec0d2015-10-09 08:55:48 -07001200void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 ASSERT(signaling_thread()->IsCurrent());
1202 if (!voice_channel_) {
1203 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1204 return;
1205 }
solenberg4bac9c52015-10-09 02:32:53 -07001206 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1207 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 // otherwise. This in the normal case when the underlying media channel has
1209 // already been deleted.
1210 ASSERT(enable == false);
1211 }
1212}
1213
Peter Boström0c4e06b2015-10-07 12:23:21 +02001214void WebRtcSession::SetAudioSend(uint32_t ssrc,
1215 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001216 const cricket::AudioOptions& options,
1217 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 ASSERT(signaling_thread()->IsCurrent());
1219 if (!voice_channel_) {
1220 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1221 return;
1222 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001223 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001224 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226}
1227
Peter Boström0c4e06b2015-10-07 12:23:21 +02001228void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001229 ASSERT(signaling_thread()->IsCurrent());
1230 ASSERT(volume >= 0 && volume <= 10);
1231 if (!voice_channel_) {
1232 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1233 return;
1234 }
1235
solenberg4bac9c52015-10-09 02:32:53 -07001236 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001237 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001238 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001239}
1240
deadbeef2d110be2016-01-13 12:00:26 -08001241void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1242 rtc::scoped_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001243 ASSERT(signaling_thread()->IsCurrent());
1244 if (!voice_channel_)
1245 return;
1246
deadbeef2d110be2016-01-13 12:00:26 -08001247 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001248}
1249
Peter Boström0c4e06b2015-10-07 12:23:21 +02001250bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 cricket::VideoCapturer* camera) {
1252 ASSERT(signaling_thread()->IsCurrent());
1253
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001254 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1256 // support video.
1257 LOG(LS_WARNING) << "Video not used in this call.";
1258 return false;
1259 }
1260 if (!video_channel_->SetCapturer(ssrc, camera)) {
1261 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1262 // This in the normal case when the underlying media channel has already
1263 // been deleted.
1264 ASSERT(camera == NULL);
1265 return false;
1266 }
1267 return true;
1268}
1269
nisse08582ff2016-02-04 01:24:52 -08001270void WebRtcSession::SetVideoPlayout(
1271 uint32_t ssrc,
1272 bool enable,
1273 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 ASSERT(signaling_thread()->IsCurrent());
1275 if (!video_channel_) {
1276 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1277 return;
1278 }
nisse08582ff2016-02-04 01:24:52 -08001279 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1280 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 // This in the normal case when the underlying media channel has already
1282 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001283 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 }
1285}
1286
Peter Boström0c4e06b2015-10-07 12:23:21 +02001287void WebRtcSession::SetVideoSend(uint32_t ssrc,
1288 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 const cricket::VideoOptions* options) {
1290 ASSERT(signaling_thread()->IsCurrent());
1291 if (!video_channel_) {
1292 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1293 return;
1294 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001295 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1297 // This in the normal case when the underlying media channel has already
1298 // been deleted.
1299 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301}
1302
1303bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1304 ASSERT(signaling_thread()->IsCurrent());
1305 if (!voice_channel_) {
1306 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1307 return false;
1308 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001309 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1311 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001312 if (!local_desc_ ||
1313 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 &send_ssrc)) {
1315 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1316 return false;
1317 }
1318 return voice_channel_->CanInsertDtmf();
1319}
1320
1321bool WebRtcSession::InsertDtmf(const std::string& track_id,
1322 int code, int duration) {
1323 ASSERT(signaling_thread()->IsCurrent());
1324 if (!voice_channel_) {
1325 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1326 return false;
1327 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001328 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001329 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1330 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1332 return false;
1333 }
solenberg1d63dd02015-12-02 12:35:09 -08001334 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1336 return false;
1337 }
1338 return true;
1339}
1340
1341sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001342 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
1344
wu@webrtc.org78187522013-10-07 23:32:02 +00001345bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001346 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001347 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001348 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001349 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1350 return false;
1351 }
1352 return data_channel_->SendData(params, payload, result);
1353}
1354
1355bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001356 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001357 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1358 return false;
1359 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001360 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1361 &DataChannel::OnChannelReady);
1362 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1363 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001364 data_channel_->SignalStreamClosedRemotely.connect(
1365 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001366 return true;
1367}
1368
1369void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001370 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001371 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1372 return;
1373 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001374 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1375 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001376 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001377}
1378
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001379void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001380 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001381 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1382 return;
1383 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001384 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1385 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001386}
1387
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001388void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001389 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001390 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1391 << "NULL.";
1392 return;
1393 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001394 data_channel_->RemoveRecvStream(sid);
1395 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001396}
1397
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001398bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001399 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001400}
1401
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402cricket::DataChannelType WebRtcSession::data_channel_type() const {
1403 return data_channel_type_;
1404}
1405
deadbeef0ed85b22016-02-23 17:24:52 -08001406bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1407 return pending_ice_restarts_.find(content_name) !=
1408 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001409}
1410
Henrik Boströmd8281982015-08-27 10:12:24 +02001411void WebRtcSession::OnCertificateReady(
1412 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001413 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001414}
1415
Henrik Boströmd8281982015-08-27 10:12:24 +02001416bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001417 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001418}
1419
deadbeefcbecd352015-09-23 11:50:27 -07001420const rtc::scoped_refptr<rtc::RTCCertificate>&
1421WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001422 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001423}
1424
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425void WebRtcSession::SetIceConnectionState(
1426 PeerConnectionInterface::IceConnectionState state) {
1427 if (ice_connection_state_ == state) {
1428 return;
1429 }
1430
1431 // ASSERT that the requested transition is allowed. Note that
1432 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1433 // within PeerConnection). This switch statement should compile away when
1434 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001435 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1436 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 switch (ice_connection_state_) {
1438 case PeerConnectionInterface::kIceConnectionNew:
1439 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1440 break;
1441 case PeerConnectionInterface::kIceConnectionChecking:
1442 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1443 state == PeerConnectionInterface::kIceConnectionConnected);
1444 break;
1445 case PeerConnectionInterface::kIceConnectionConnected:
1446 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1447 state == PeerConnectionInterface::kIceConnectionChecking ||
1448 state == PeerConnectionInterface::kIceConnectionCompleted);
1449 break;
1450 case PeerConnectionInterface::kIceConnectionCompleted:
1451 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1452 state == PeerConnectionInterface::kIceConnectionDisconnected);
1453 break;
1454 case PeerConnectionInterface::kIceConnectionFailed:
1455 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1456 break;
1457 case PeerConnectionInterface::kIceConnectionDisconnected:
1458 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1459 state == PeerConnectionInterface::kIceConnectionConnected ||
1460 state == PeerConnectionInterface::kIceConnectionCompleted ||
1461 state == PeerConnectionInterface::kIceConnectionFailed);
1462 break;
1463 case PeerConnectionInterface::kIceConnectionClosed:
1464 ASSERT(false);
1465 break;
1466 default:
1467 ASSERT(false);
1468 break;
1469 }
1470
1471 ice_connection_state_ = state;
1472 if (ice_observer_) {
1473 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1474 }
1475}
1476
deadbeefcbecd352015-09-23 11:50:27 -07001477void WebRtcSession::OnTransportControllerConnectionState(
1478 cricket::IceConnectionState state) {
1479 switch (state) {
1480 case cricket::kIceConnectionConnecting:
1481 // If the current state is Connected or Completed, then there were
1482 // writable channels but now there are not, so the next state must
1483 // be Disconnected.
1484 // kIceConnectionConnecting is currently used as the default,
1485 // un-connected state by the TransportController, so its only use is
1486 // detecting disconnections.
1487 if (ice_connection_state_ ==
1488 PeerConnectionInterface::kIceConnectionConnected ||
1489 ice_connection_state_ ==
1490 PeerConnectionInterface::kIceConnectionCompleted) {
1491 SetIceConnectionState(
1492 PeerConnectionInterface::kIceConnectionDisconnected);
1493 }
torbjornga81a42f2015-09-23 02:16:58 -07001494 break;
deadbeefcbecd352015-09-23 11:50:27 -07001495 case cricket::kIceConnectionFailed:
1496 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1497 break;
1498 case cricket::kIceConnectionConnected:
1499 LOG(LS_INFO) << "Changing to ICE connected state because "
1500 << "all transports are writable.";
1501 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1502 break;
1503 case cricket::kIceConnectionCompleted:
1504 LOG(LS_INFO) << "Changing to ICE completed state because "
1505 << "all transports are complete.";
1506 if (ice_connection_state_ !=
1507 PeerConnectionInterface::kIceConnectionConnected) {
1508 // If jumping directly from "checking" to "connected",
1509 // signal "connected" first.
1510 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1511 }
1512 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1513 if (metrics_observer_) {
1514 ReportTransportStats();
1515 }
1516 break;
1517 default:
1518 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001519 }
deadbeefcbecd352015-09-23 11:50:27 -07001520}
1521
1522void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001523 SetIceConnectionReceiving(receiving);
1524}
1525
1526void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1527 if (ice_connection_receiving_ == receiving) {
1528 return;
1529 }
1530 ice_connection_receiving_ = receiving;
1531 if (ice_observer_) {
1532 ice_observer_->OnIceConnectionReceivingChange(receiving);
1533 }
1534}
1535
deadbeefcbecd352015-09-23 11:50:27 -07001536void WebRtcSession::OnTransportControllerCandidatesGathered(
1537 const std::string& transport_name,
1538 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001540 int sdp_mline_index;
1541 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1542 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1543 << transport_name << " not found";
1544 return;
1545 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546
deadbeefcbecd352015-09-23 11:50:27 -07001547 for (cricket::Candidates::const_iterator citer = candidates.begin();
1548 citer != candidates.end(); ++citer) {
1549 // Use transport_name as the candidate media id.
1550 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1551 if (ice_observer_) {
1552 ice_observer_->OnIceCandidate(&candidate);
1553 }
1554 if (local_desc_) {
1555 local_desc_->AddCandidate(&candidate);
1556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 }
1558}
1559
1560// Enabling voice and video channel.
1561void WebRtcSession::EnableChannels() {
1562 if (voice_channel_ && !voice_channel_->enabled())
1563 voice_channel_->Enable(true);
1564
1565 if (video_channel_ && !video_channel_->enabled())
1566 video_channel_->Enable(true);
1567
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001568 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 data_channel_->Enable(true);
1570}
1571
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572// Returns the media index for a local ice candidate given the content name.
1573bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1574 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001575 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578
1579 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001580 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 for (size_t index = 0; index < contents.size(); ++index) {
1582 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001583 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 content_found = true;
1585 break;
1586 }
1587 }
1588 return content_found;
1589}
1590
1591bool WebRtcSession::UseCandidatesInSessionDescription(
1592 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001593 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001595 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1599 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001600 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001601 const IceCandidateInterface* candidate = candidates->at(n);
1602 bool valid = false;
1603 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1604 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001605 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1606 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001607 }
1608 continue;
1609 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001610 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001611 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 break;
deadbeefd59daf82015-10-14 15:02:44 -07001613 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 }
1615 }
1616 return ret;
1617}
1618
1619bool WebRtcSession::UseCandidate(
1620 const IceCandidateInterface* candidate) {
1621
1622 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001623 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 if (mediacontent_index >= remote_content_size) {
1625 LOG(LS_ERROR)
1626 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1627 return false;
1628 }
1629
1630 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001631 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 std::vector<cricket::Candidate> candidates;
1633 candidates.push_back(candidate->candidate());
1634 // Invoking BaseSession method to handle remote candidates.
1635 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001636 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1637 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 // Candidates successfully submitted for checking.
1639 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1640 ice_connection_state_ ==
1641 PeerConnectionInterface::kIceConnectionDisconnected) {
1642 // If state is New, then the session has just gotten its first remote ICE
1643 // candidates, so go to Checking.
1644 // If state is Disconnected, the session is re-using old candidates or
1645 // receiving additional ones, so go to Checking.
1646 // If state is Connected, stay Connected.
1647 // TODO(bemasc): If state is Connected, and the new candidates are for a
1648 // newly added transport, then the state actually _should_ move to
1649 // checking. Add a way to distinguish that case.
1650 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1651 }
1652 // TODO(bemasc): If state is Completed, go back to Connected.
1653 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001654 if (!error.empty()) {
1655 LOG(LS_WARNING) << error;
1656 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 }
1658 return true;
1659}
1660
deadbeefcbecd352015-09-23 11:50:27 -07001661void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001662 // Destroy video_channel_ first since it may have a pointer to the
1663 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 const cricket::ContentInfo* video_info =
1665 cricket::GetFirstVideoContent(desc);
1666 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 }
1670
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001671 const cricket::ContentInfo* voice_info =
1672 cricket::GetFirstAudioContent(desc);
1673 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001674 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001675 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001676 }
1677
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 const cricket::ContentInfo* data_info =
1679 cricket::GetFirstDataContent(desc);
1680 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 }
1684}
1685
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001686// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001687// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 // Creating the media channels and transport proxies.
1690 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1691 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001692 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 LOG(LS_ERROR) << "Failed to create voice channel.";
1694 return false;
1695 }
1696 }
1697
1698 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1699 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001700 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 LOG(LS_ERROR) << "Failed to create video channel.";
1702 return false;
1703 }
1704 }
1705
1706 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1707 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001708 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001709 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 LOG(LS_ERROR) << "Failed to create data channel.";
1711 return false;
1712 }
1713 }
1714
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001715 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1716 if (voice_channel()) {
1717 voice_channel()->ActivateRtcpMux();
1718 }
1719 if (video_channel()) {
1720 video_channel()->ActivateRtcpMux();
1721 }
1722 if (data_channel()) {
1723 data_channel()->ActivateRtcpMux();
1724 }
1725 }
1726
deadbeefcbecd352015-09-23 11:50:27 -07001727 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001728 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001729 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1730 cricket::GROUP_TYPE_BUNDLE);
1731 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001732 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1733 return false;
1734 }
deadbeefcbecd352015-09-23 11:50:27 -07001735 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001736 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1737 return false;
1738 }
1739 }
1740
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 return true;
1742}
1743
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001744bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001746 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001747 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001748 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001749 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001750 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001751
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001752 voice_channel_->SignalDtlsSetupFailure.connect(
1753 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001754
1755 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001756 voice_channel_->transport_channel()->SignalSentPacket.connect(
1757 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001758 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759}
1760
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001761bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001763 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001764 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001765 if (!video_channel_) {
1766 return false;
1767 }
1768
1769 video_channel_->SignalDtlsSetupFailure.connect(
1770 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001771
1772 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001773 video_channel_->transport_channel()->SignalSentPacket.connect(
1774 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001775 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776}
1777
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001778bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001779 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001781 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001782 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001783 return false;
1784 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001785
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001786 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001787 data_channel_->SignalDataReceived.connect(
1788 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001789 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001790
1791 data_channel_->SignalDtlsSetupFailure.connect(
1792 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001793
1794 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001795 data_channel_->transport_channel()->SignalSentPacket.connect(
1796 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001797 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798}
1799
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001800void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001801 SetError(ERROR_TRANSPORT,
1802 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803}
1804
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001805void WebRtcSession::OnDataChannelMessageReceived(
1806 cricket::DataChannel* channel,
1807 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001808 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001809 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1810 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1811 // Received OPEN message; parse and signal that a new data channel should
1812 // be created.
1813 std::string label;
1814 InternalDataChannelInit config;
1815 config.id = params.ssrc;
1816 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1817 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1818 << params.ssrc;
1819 return;
1820 }
1821 config.open_handshake_role = InternalDataChannelInit::kAcker;
1822 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 }
deadbeefab9b2d12015-10-14 11:33:11 -07001824 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001827// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001828bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001829 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1830 if (!bundle_enabled)
1831 return true;
1832
1833 const cricket::ContentGroup* bundle_group =
1834 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1835 ASSERT(bundle_group != NULL);
1836
1837 const cricket::ContentInfos& contents = desc->contents();
1838 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1839 citer != contents.end(); ++citer) {
1840 const cricket::ContentInfo* content = (&*citer);
1841 ASSERT(content != NULL);
1842 if (bundle_group->HasContentName(content->name) &&
1843 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1844 if (!HasRtcpMuxEnabled(content))
1845 return false;
1846 }
1847 }
1848 // RTCP-MUX is enabled in all the contents.
1849 return true;
1850}
1851
1852bool WebRtcSession::HasRtcpMuxEnabled(
1853 const cricket::ContentInfo* content) {
1854 const cricket::MediaContentDescription* description =
1855 static_cast<cricket::MediaContentDescription*>(content->description);
1856 return description->rtcp_mux();
1857}
1858
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001859bool WebRtcSession::ValidateSessionDescription(
1860 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001861 cricket::ContentSource source, std::string* err_desc) {
1862 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001863 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001864 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001865 }
1866
1867 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001868 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001869 }
1870
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001871 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001872 Action action = GetAction(sdesc->type());
1873 if (source == cricket::CS_LOCAL) {
1874 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001875 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001876 } else {
1877 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001878 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001879 }
1880
1881 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001882 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001883 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1884 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001885 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001886 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001887 }
1888
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001889 // Verify ice-ufrag and ice-pwd.
1890 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001891 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001892 }
1893
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001894 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001895 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001896 }
1897
1898 // Verify m-lines in Answer when compared against Offer.
1899 if (action == kAnswer) {
1900 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001901 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1902 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001903 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001904 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001905 }
1906 }
1907
1908 return true;
1909}
1910
1911bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1912 return ((action == kOffer && state() == STATE_INIT) ||
1913 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001914 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001915 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001916 (action == kOffer && state() == STATE_INPROGRESS) ||
1917 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001918 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1919 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1920 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1921 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001922}
1923
1924bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1925 return ((action == kOffer && state() == STATE_INIT) ||
1926 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001927 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001928 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001929 (action == kOffer && state() == STATE_INPROGRESS) ||
1930 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001931 (action == kAnswer && state() == STATE_SENTOFFER) ||
1932 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1933 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1934 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001935}
1936
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001937std::string WebRtcSession::GetSessionErrorMsg() {
1938 std::ostringstream desc;
1939 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1940 desc << kSessionErrorDesc << error_desc() << ".";
1941 return desc.str();
1942}
1943
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001944// We need to check the local/remote description for the Transport instead of
1945// the session, because a new Transport added during renegotiation may have
1946// them unset while the session has them set from the previous negotiation.
1947// Not doing so may trigger the auto generation of transport description and
1948// mess up DTLS identity information, ICE credential, etc.
1949bool WebRtcSession::ReadyToUseRemoteCandidate(
1950 const IceCandidateInterface* candidate,
1951 const SessionDescriptionInterface* remote_desc,
1952 bool* valid) {
1953 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001954
1955 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001956 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001957
deadbeefd59daf82015-10-14 15:02:44 -07001958 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001959 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001960 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001961
1962 size_t mediacontent_index =
1963 static_cast<size_t>(candidate->sdp_mline_index());
1964 size_t remote_content_size =
1965 current_remote_desc->description()->contents().size();
1966 if (mediacontent_index >= remote_content_size) {
1967 LOG(LS_ERROR)
1968 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1969
1970 *valid = false;
1971 return false;
1972 }
1973
1974 cricket::ContentInfo content =
1975 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07001976 cricket::BaseChannel* channel = GetChannel(content.name);
1977 if (!channel) {
1978 return false;
1979 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001980
deadbeefd59daf82015-10-14 15:02:44 -07001981 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07001982 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001983}
1984
deadbeefcbecd352015-09-23 11:50:27 -07001985void WebRtcSession::OnTransportControllerGatheringState(
1986 cricket::IceGatheringState state) {
1987 ASSERT(signaling_thread()->IsCurrent());
1988 if (state == cricket::kIceGatheringGathering) {
1989 if (ice_observer_) {
1990 ice_observer_->OnIceGatheringChange(
1991 PeerConnectionInterface::kIceGatheringGathering);
1992 }
1993 } else if (state == cricket::kIceGatheringComplete) {
1994 if (ice_observer_) {
1995 ice_observer_->OnIceGatheringChange(
1996 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001997 }
1998 }
1999}
2000
2001void WebRtcSession::ReportTransportStats() {
2002 // Use a set so we don't report the same stats twice if two channels share
2003 // a transport.
2004 std::set<std::string> transport_names;
2005 if (voice_channel()) {
2006 transport_names.insert(voice_channel()->transport_name());
2007 }
2008 if (video_channel()) {
2009 transport_names.insert(video_channel()->transport_name());
2010 }
2011 if (data_channel()) {
2012 transport_names.insert(data_channel()->transport_name());
2013 }
2014 for (const auto& name : transport_names) {
2015 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002016 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002017 ReportBestConnectionState(stats);
2018 ReportNegotiatedCiphers(stats);
2019 }
2020 }
2021}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002022// Walk through the ConnectionInfos to gather best connection usage
2023// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002024void WebRtcSession::ReportBestConnectionState(
2025 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002026 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002027 for (cricket::TransportChannelStatsList::const_iterator it =
2028 stats.channel_stats.begin();
2029 it != stats.channel_stats.end(); ++it) {
2030 for (cricket::ConnectionInfos::const_iterator it_info =
2031 it->connection_infos.begin();
2032 it_info != it->connection_infos.end(); ++it_info) {
2033 if (!it_info->best_connection) {
2034 continue;
2035 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002036
2037 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2038 const cricket::Candidate& local = it_info->local_candidate;
2039 const cricket::Candidate& remote = it_info->remote_candidate;
2040
2041 // Increment the counter for IceCandidatePairType.
2042 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2043 (local.type() == RELAY_PORT_TYPE &&
2044 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2045 type = kEnumCounterIceCandidatePairTypeTcp;
2046 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2047 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002048 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002049 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002050 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002051 metrics_observer_->IncrementEnumCounter(
2052 type, GetIceCandidatePairCounter(local, remote),
2053 kIceCandidatePairMax);
2054
2055 // Increment the counter for IP type.
2056 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002057 metrics_observer_->IncrementEnumCounter(
2058 kEnumCounterAddressFamily, kBestConnections_IPv4,
2059 kPeerConnectionAddressFamilyCounter_Max);
2060
2061 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002062 metrics_observer_->IncrementEnumCounter(
2063 kEnumCounterAddressFamily, kBestConnections_IPv6,
2064 kPeerConnectionAddressFamilyCounter_Max);
2065 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002066 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002067 }
2068
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002069 return;
2070 }
2071 }
2072}
2073
jbauchac8869e2015-07-03 01:36:14 -07002074void WebRtcSession::ReportNegotiatedCiphers(
2075 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002076 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002077 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2078 return;
2079 }
2080
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002081 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2082 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2083 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2084 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002085 return;
2086 }
2087
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002088 PeerConnectionEnumCounterType srtp_counter_type;
2089 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002090 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002091 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2092 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002093 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002094 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2095 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002096 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002097 srtp_counter_type = kEnumCounterDataSrtpCipher;
2098 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002099 } else {
2100 RTC_NOTREACHED();
2101 return;
2102 }
2103
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002104 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2105 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2106 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002107 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002108 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2109 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2110 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002111 }
2112}
2113
stefanc1aeaf02015-10-15 07:26:07 -07002114void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2115 const rtc::SentPacket& sent_packet) {
2116 RTC_DCHECK(worker_thread()->IsCurrent());
2117 media_controller_->call_w()->OnSentPacket(sent_packet);
2118}
2119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120} // namespace webrtc