blob: 96442c8dafbc066c32721c82c839329114cebdbf [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"
Henrik Kjellander15583c12016-02-10 10:53:12 +010022#include "webrtc/api/peerconnectioninterface.h"
23#include "webrtc/api/sctputils.h"
24#include "webrtc/api/webrtcsessiondescriptionfactory.h"
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010025#include "webrtc/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000026#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070027#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000028#include "webrtc/base/helpers.h"
29#include "webrtc/base/logging.h"
30#include "webrtc/base/stringencode.h"
31#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070032#include "webrtc/call.h"
kjellanderf4752772016-03-02 05:42:30 -080033#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080034#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000035#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070036#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010037#include "webrtc/pc/channel.h"
38#include "webrtc/pc/channelmanager.h"
39#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41using cricket::ContentInfo;
42using cricket::ContentInfos;
43using cricket::MediaContentDescription;
44using cricket::SessionDescription;
45using cricket::TransportInfo;
46
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070047using cricket::LOCAL_PORT_TYPE;
48using cricket::STUN_PORT_TYPE;
49using cricket::RELAY_PORT_TYPE;
50using cricket::PRFLX_PORT_TYPE;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052namespace webrtc {
53
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000055const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
56 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000057const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058const char kInvalidCandidates[] = "Description contains invalid candidates.";
59const char kInvalidSdp[] = "Invalid session description.";
60const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
62const char kPushDownTDFailed[] =
63 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000064const char kSdpWithoutDtlsFingerprint[] =
65 "Called with SDP without DTLS fingerprint.";
66const char kSdpWithoutSdesCrypto[] =
67 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000068const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000071const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000072const char kDtlsSetupFailureRtp[] =
73 "Couldn't set up DTLS-SRTP on RTP channel.";
74const char kDtlsSetupFailureRtcp[] =
75 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070076const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070078IceCandidatePairType GetIceCandidatePairCounter(
79 const cricket::Candidate& local,
80 const cricket::Candidate& remote) {
81 const auto& l = local.type();
82 const auto& r = remote.type();
83 const auto& host = LOCAL_PORT_TYPE;
84 const auto& srflx = STUN_PORT_TYPE;
85 const auto& relay = RELAY_PORT_TYPE;
86 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070087 if (l == host && r == host) {
88 bool local_private = IPIsPrivate(local.address().ipaddr());
89 bool remote_private = IPIsPrivate(remote.address().ipaddr());
90 if (local_private) {
91 if (remote_private) {
92 return kIceCandidatePairHostPrivateHostPrivate;
93 } else {
94 return kIceCandidatePairHostPrivateHostPublic;
95 }
96 } else {
97 if (remote_private) {
98 return kIceCandidatePairHostPublicHostPrivate;
99 } else {
100 return kIceCandidatePairHostPublicHostPublic;
101 }
102 }
103 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700104 if (l == host && r == srflx)
105 return kIceCandidatePairHostSrflx;
106 if (l == host && r == relay)
107 return kIceCandidatePairHostRelay;
108 if (l == host && r == prflx)
109 return kIceCandidatePairHostPrflx;
110 if (l == srflx && r == host)
111 return kIceCandidatePairSrflxHost;
112 if (l == srflx && r == srflx)
113 return kIceCandidatePairSrflxSrflx;
114 if (l == srflx && r == relay)
115 return kIceCandidatePairSrflxRelay;
116 if (l == srflx && r == prflx)
117 return kIceCandidatePairSrflxPrflx;
118 if (l == relay && r == host)
119 return kIceCandidatePairRelayHost;
120 if (l == relay && r == srflx)
121 return kIceCandidatePairRelaySrflx;
122 if (l == relay && r == relay)
123 return kIceCandidatePairRelayRelay;
124 if (l == relay && r == prflx)
125 return kIceCandidatePairRelayPrflx;
126 if (l == prflx && r == host)
127 return kIceCandidatePairPrflxHost;
128 if (l == prflx && r == srflx)
129 return kIceCandidatePairPrflxSrflx;
130 if (l == prflx && r == relay)
131 return kIceCandidatePairPrflxRelay;
132 return kIceCandidatePairMax;
133}
134
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135// Compares |answer| against |offer|. Comparision is done
136// for number of m-lines in answer against offer. If matches true will be
137// returned otherwise false.
138static bool VerifyMediaDescriptions(
139 const SessionDescription* answer, const SessionDescription* offer) {
140 if (offer->contents().size() != answer->contents().size())
141 return false;
142
143 for (size_t i = 0; i < offer->contents().size(); ++i) {
144 if ((offer->contents()[i].name) != answer->contents()[i].name) {
145 return false;
146 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000147 const MediaContentDescription* offer_mdesc =
148 static_cast<const MediaContentDescription*>(
149 offer->contents()[i].description);
150 const MediaContentDescription* answer_mdesc =
151 static_cast<const MediaContentDescription*>(
152 answer->contents()[i].description);
153 if (offer_mdesc->type() != answer_mdesc->type()) {
154 return false;
155 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 }
157 return true;
158}
159
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160// Checks that each non-rejected content has SDES crypto keys or a DTLS
161// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
162// keys, will be caught in Transport negotiation, and backstopped by Channel's
163// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000164static bool VerifyCrypto(const SessionDescription* desc,
165 bool dtls_enabled,
166 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 const ContentInfos& contents = desc->contents();
168 for (size_t index = 0; index < contents.size(); ++index) {
169 const ContentInfo* cinfo = &contents[index];
170 if (cinfo->rejected) {
171 continue;
172 }
173
174 // If the content isn't rejected, crypto must be present.
175 const MediaContentDescription* media =
176 static_cast<const MediaContentDescription*>(cinfo->description);
177 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
178 if (!media || !tinfo) {
179 // Something is not right.
180 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000181 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 return false;
183 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000184 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000185 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000186 LOG(LS_WARNING) <<
187 "Session description must have DTLS fingerprint if DTLS enabled.";
188 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000189 return false;
190 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000191 } else {
192 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000193 LOG(LS_WARNING) <<
194 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000195 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000196 return false;
197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 }
199 }
200
201 return true;
202}
203
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000204// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
205static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
206 const ContentInfos& contents = desc->contents();
207 for (size_t index = 0; index < contents.size(); ++index) {
208 const ContentInfo* cinfo = &contents[index];
209 if (cinfo->rejected) {
210 continue;
211 }
212
213 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
214 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
215 if (!tinfo) {
216 // Something is not right.
217 LOG(LS_ERROR) << kInvalidSdp;
218 return false;
219 }
220 if (tinfo->description.ice_ufrag.empty() ||
221 tinfo->description.ice_pwd.empty()) {
222 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
223 return false;
224 }
225 }
226 return true;
227}
228
wu@webrtc.org91053e72013-08-10 07:18:04 +0000229// Forces |sdesc->crypto_required| to the appropriate state based on the
230// current security policy, to ensure a failure occurs if there is an error
231// in crypto negotiation.
232// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000233static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
234 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000235 if (!sdesc) {
236 return;
237 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
wu@webrtc.org91053e72013-08-10 07:18:04 +0000239 // Updating the |crypto_required_| in MediaContentDescription to the
240 // appropriate state based on the current security policy.
241 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
242 iter != sdesc->contents().end(); ++iter) {
243 if (cricket::IsMediaContent(&*iter)) {
244 MediaContentDescription* mdesc =
245 static_cast<MediaContentDescription*> (iter->description);
246 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000247 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248 }
249 }
250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251}
252
Peter Boström0c4e06b2015-10-07 12:23:21 +0200253static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
254 const std::string& track_id,
255 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 const cricket::ContentInfo* audio_info =
257 cricket::GetFirstAudioContent(session_description);
258 if (!audio_info) {
259 LOG(LS_ERROR) << "Audio not used in this call";
260 return false;
261 }
262
263 const cricket::MediaContentDescription* audio_content =
264 static_cast<const cricket::MediaContentDescription*>(
265 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000266 const cricket::StreamParams* stream =
267 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
268 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 return false;
270 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000271
272 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 return true;
274}
275
276static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200277 uint32_t ssrc,
278 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 ASSERT(track_id != NULL);
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 const cricket::ContentInfo* audio_info =
282 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000283 if (audio_info) {
284 const cricket::MediaContentDescription* audio_content =
285 static_cast<const cricket::MediaContentDescription*>(
286 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000288 const auto* found =
289 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
290 if (found) {
291 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000292 return true;
293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 }
295
296 const cricket::ContentInfo* video_info =
297 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000298 if (video_info) {
299 const cricket::MediaContentDescription* video_content =
300 static_cast<const cricket::MediaContentDescription*>(
301 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000303 const auto* found =
304 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
305 if (found) {
306 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000307 return true;
308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
310 return false;
311}
312
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313static bool BadSdp(const std::string& source,
314 const std::string& type,
315 const std::string& reason,
316 std::string* err_desc) {
317 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700318 desc << "Failed to set " << source;
319 if (!type.empty()) {
320 desc << " " << type;
321 }
322 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 return false;
329}
330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000336 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000338 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 }
340}
341
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342static bool BadLocalSdp(const std::string& type,
343 const std::string& reason,
344 std::string* err_desc) {
345 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
346}
347
348static bool BadRemoteSdp(const std::string& type,
349 const std::string& reason,
350 std::string* err_desc) {
351 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
352}
353
354static bool BadOfferSdp(cricket::ContentSource source,
355 const std::string& reason,
356 std::string* err_desc) {
357 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
358}
359
360static bool BadPranswerSdp(cricket::ContentSource source,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
364 reason, err_desc);
365}
366
367static bool BadAnswerSdp(cricket::ContentSource source,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
deadbeefd59daf82015-10-14 15:02:44 -0700373#define GET_STRING_OF_STATE(state) \
374 case webrtc::WebRtcSession::state: \
375 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 break;
377
deadbeefd59daf82015-10-14 15:02:44 -0700378static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 std::string result;
380 switch (state) {
381 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700382 GET_STRING_OF_STATE(STATE_SENTOFFER)
383 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
384 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
385 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700387 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 default:
389 ASSERT(false);
390 break;
391 }
392 return result;
393}
394
deadbeefd59daf82015-10-14 15:02:44 -0700395#define GET_STRING_OF_ERROR_CODE(err) \
396 case webrtc::WebRtcSession::err: \
397 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 break;
399
deadbeefd59daf82015-10-14 15:02:44 -0700400static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 std::string result;
402 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000403 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000404 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
405 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
deadbeefd59daf82015-10-14 15:02:44 -0700407 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 break;
409 }
410 return result;
411}
412
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000413static std::string MakeErrorString(const std::string& error,
414 const std::string& desc) {
415 std::ostringstream ret;
416 ret << error << " " << desc;
417 return ret.str();
418}
419
420static std::string MakeTdErrorString(const std::string& desc) {
421 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422}
423
deadbeef0ed85b22016-02-23 17:24:52 -0800424// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
425bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
426 const SessionDescriptionInterface* new_desc,
427 const std::string& content_name) {
428 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700429 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
deadbeef0ed85b22016-02-23 17:24:52 -0800431 const SessionDescription* new_sd = new_desc->description();
432 const SessionDescription* old_sd = old_desc->description();
433 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
434 if (!cinfo || cinfo->rejected) {
435 return false;
436 }
437 // If the content isn't rejected, check if ufrag and password has changed.
438 const cricket::TransportDescription* new_transport_desc =
439 new_sd->GetTransportDescriptionByName(content_name);
440 const cricket::TransportDescription* old_transport_desc =
441 old_sd->GetTransportDescriptionByName(content_name);
442 if (!new_transport_desc || !old_transport_desc) {
443 // No transport description exists. This is not an ICE restart.
444 return false;
445 }
446 if (cricket::IceCredentialsChanged(
447 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
448 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
449 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
450 << ".";
451 return true;
452 }
453 return false;
454}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455
stefanc1aeaf02015-10-15 07:26:07 -0700456WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700457 rtc::Thread* signaling_thread,
458 rtc::Thread* worker_thread,
459 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700460 : signaling_thread_(signaling_thread),
461 worker_thread_(worker_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 // RFC 3264: The numeric value of the session id and version in the
463 // o line MUST be representable with a "64 bit signed integer".
464 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700465 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
466 transport_controller_(new cricket::TransportController(signaling_thread,
467 worker_thread,
468 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700469 media_controller_(media_controller),
470 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 ice_observer_(NULL),
472 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700473 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000475 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000477 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700478 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
479 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700480 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700481 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700482 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700483 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700484 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700485 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700486 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700487 transport_controller_->SignalCandidatesRemoved.connect(
488 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489}
490
491WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700492 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000493 // Destroy video_channel_ first since it may have a pointer to the
494 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000495 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 SignalVideoChannelDestroyed();
497 channel_manager_->DestroyVideoChannel(video_channel_.release());
498 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000499 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000500 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200501 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000502 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000503 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 SignalDataChannelDestroyed();
505 channel_manager_->DestroyDataChannel(data_channel_.release());
506 }
deadbeef057ecf02016-01-20 14:30:43 -0800507 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700508
509 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510}
511
wu@webrtc.org91053e72013-08-10 07:18:04 +0000512bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000513 const PeerConnectionFactoryInterface::Options& options,
kwibergd1fe2812016-04-27 06:47:29 -0700514 std::unique_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200515 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
516 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700517 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700518 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000519
Henrik Boström87713d02015-08-25 09:53:21 +0200520 // Obtain a certificate from RTCConfiguration if any were provided (optional).
521 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
522 if (!rtc_configuration.certificates.empty()) {
523 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
524 // just picking the first one. The decision should be made based on the DTLS
525 // handshake. The DTLS negotiations need to know about all certificates.
526 certificate = rtc_configuration.certificates[0];
527 }
528
honghaiz1f429e32015-09-28 07:57:34 -0700529 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700530
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000531 if (options.disable_encryption) {
532 dtls_enabled_ = false;
533 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200534 // Enable DTLS by default if we have an identity store or a certificate.
535 dtls_enabled_ = (dtls_identity_store || certificate);
htaa2a49d92016-03-04 02:51:39 -0800536 // |rtc_configuration| can override the default |dtls_enabled_| value.
537 if (rtc_configuration.enable_dtls_srtp) {
538 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000539 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000540 }
541
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000543 // It takes precendence over the disable_sctp_data_channels
544 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800545 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000547 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000548 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000549 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000550 data_channel_type_ = cricket::DCT_SCTP;
551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
htaa2a49d92016-03-04 02:51:39 -0800554 video_options_.screencast_min_bitrate_kbps =
555 rtc_configuration.screencast_min_bitrate;
556 audio_options_.combined_audio_video_bwe =
557 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000558
kwiberg102c6a62015-10-30 02:47:38 -0700559 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100560 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200561
Karl Wibergbe579832015-11-10 22:34:18 +0100562 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
563 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200564
Henrik Boström87713d02015-08-25 09:53:21 +0200565 if (!dtls_enabled_) {
566 // Construct with DTLS disabled.
567 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700568 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200569 } else {
570 // Construct with DTLS enabled.
571 if (!certificate) {
572 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700573 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200574 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
kwiberg0eb15ed2015-12-17 03:04:15 -0800575 signaling_thread(), channel_manager_, std::move(dtls_identity_store),
deadbeefab9b2d12015-10-14 11:33:11 -0700576 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200577 } else {
578 // Use the already generated certificate.
579 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700580 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200581 }
582 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000583
Henrik Boströmd8281982015-08-27 10:12:24 +0200584 webrtc_session_desc_factory_->SignalCertificateReady.connect(
585 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000586
wu@webrtc.org97077a32013-10-25 21:18:33 +0000587 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000588 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000589 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700590
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 return true;
592}
593
deadbeefd59daf82015-10-14 15:02:44 -0700594void WebRtcSession::Close() {
595 SetState(STATE_CLOSED);
596 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000597 ASSERT(!voice_channel_);
598 ASSERT(!video_channel_);
599 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800600 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601}
602
deadbeef0ed85b22016-02-23 17:24:52 -0800603cricket::BaseChannel* WebRtcSession::GetChannel(
604 const std::string& content_name) {
605 if (voice_channel() && voice_channel()->content_name() == content_name) {
606 return voice_channel();
607 }
608 if (video_channel() && video_channel()->content_name() == content_name) {
609 return video_channel();
610 }
611 if (data_channel() && data_channel()->content_name() == content_name) {
612 return data_channel();
613 }
614 return nullptr;
615}
616
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000617void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
618 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619}
620
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000621cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
622 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623}
624
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800625bool WebRtcSession::GetSslRole(const std::string& transport_name,
626 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700627 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000628 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
629 << "SSL Role of the session.";
630 return false;
631 }
632
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800633 return transport_controller_->GetSslRole(transport_name, role);
634}
635
636bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
637 rtc::SSLRole* role) {
638 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000639}
640
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000641void WebRtcSession::CreateOffer(
642 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700643 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
644 const cricket::MediaSessionOptions& session_options) {
645 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000646}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647
deadbeefab9b2d12015-10-14 11:33:11 -0700648void WebRtcSession::CreateAnswer(
649 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700650 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800651 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652}
653
654bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
655 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700656 ASSERT(signaling_thread()->IsCurrent());
657
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000658 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700659 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000660
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000661 // Validate SDP.
662 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
663 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 }
665
deadbeefd59daf82015-10-14 15:02:44 -0700666 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000667 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700669 initial_offerer_ = true;
670 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 }
672
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000673 cricket::SecurePolicy sdes_policy =
674 webrtc_session_desc_factory_->SdesPolicy();
675 cricket::CryptoType crypto_required = dtls_enabled_ ?
676 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
677 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000679 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000681 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682
683 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000684 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 // TODO(mallinath) - Handle CreateChannel failure, as new local description
686 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000687 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 }
689
deadbeefcbecd352015-09-23 11:50:27 -0700690 // Remove unused channels if MediaContentDescription is rejected.
691 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000693 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 return false;
695 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000696
deadbeefd59daf82015-10-14 15:02:44 -0700697 if (remote_desc_) {
698 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700699 UseCandidatesInSessionDescription(remote_desc_.get());
700 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701
deadbeef0ed85b22016-02-23 17:24:52 -0800702 pending_ice_restarts_.clear();
703
deadbeefd59daf82015-10-14 15:02:44 -0700704 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000705 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
707 return true;
708}
709
710bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
711 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700712 ASSERT(signaling_thread()->IsCurrent());
713
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000714 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700715 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000716
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000717 // Validate SDP.
718 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
719 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 }
721
kwibergd1fe2812016-04-27 06:47:29 -0700722 std::unique_ptr<SessionDescriptionInterface> old_remote_desc(
deadbeefd59daf82015-10-14 15:02:44 -0700723 remote_desc_.release());
724 remote_desc_.reset(desc_temp.release());
725
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000727 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 if (action == kOffer && !CreateChannels(desc->description())) {
729 // TODO(mallinath) - Handle CreateChannel failure, as new local description
730 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000731 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 }
733
deadbeefcbecd352015-09-23 11:50:27 -0700734 // Remove unused channels if MediaContentDescription is rejected.
735 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736
737 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
738 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000739 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 return false;
741 }
742
deadbeefd59daf82015-10-14 15:02:44 -0700743 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000744 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
746
deadbeef0ed85b22016-02-23 17:24:52 -0800747 if (old_remote_desc) {
748 for (const cricket::ContentInfo& content :
749 old_remote_desc->description()->contents()) {
750 // Check if this new SessionDescription contains new ICE ufrag and
751 // password that indicates the remote peer requests an ICE restart.
752 // TODO(deadbeef): When we start storing both the current and pending
753 // remote description, this should reset pending_ice_restarts and compare
754 // against the current description.
755 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
756 if (action == kOffer) {
757 pending_ice_restarts_.insert(content.name);
758 }
759 } else {
760 // We retain all received candidates only if ICE is not restarted.
761 // When ICE is restarted, all previous candidates belong to an old
762 // generation and should not be kept.
763 // TODO(deadbeef): This goes against the W3C spec which says the remote
764 // description should only contain candidates from the last set remote
765 // description plus any candidates added since then. We should remove
766 // this once we're sure it won't break anything.
767 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
768 old_remote_desc.get(), content.name, desc);
769 }
770 }
honghaiz503726c2015-07-31 12:37:38 -0700771 }
772
deadbeefd59daf82015-10-14 15:02:44 -0700773 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000774 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000776
777 // Set the the ICE connection state to connecting since the connection may
778 // become writable with peer reflexive candidates before any remote candidate
779 // is signaled.
780 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
781 // is to have a new signal the indicates a change in checking state from the
782 // transport and expose a new checking() member from transport that can be
783 // read to determine the current checking state. The existing SignalConnecting
784 // actually means "gathering candidates", so cannot be be used here.
785 if (desc->type() != SessionDescriptionInterface::kOffer &&
786 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
787 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
788 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 return true;
790}
791
deadbeefd59daf82015-10-14 15:02:44 -0700792void WebRtcSession::LogState(State old_state, State new_state) {
793 LOG(LS_INFO) << "Session:" << id()
794 << " Old state:" << GetStateString(old_state)
795 << " New state:" << GetStateString(new_state);
796}
797
798void WebRtcSession::SetState(State state) {
799 ASSERT(signaling_thread_->IsCurrent());
800 if (state != state_) {
801 LogState(state_, state);
802 state_ = state;
803 SignalState(this, state_);
804 }
805}
806
807void WebRtcSession::SetError(Error error, const std::string& error_desc) {
808 ASSERT(signaling_thread_->IsCurrent());
809 if (error != error_) {
810 error_ = error;
811 error_desc_ = error_desc;
812 }
813}
814
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815bool WebRtcSession::UpdateSessionState(
816 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700818 ASSERT(signaling_thread()->IsCurrent());
819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 // If there's already a pending error then no state transition should happen.
821 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700822 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000823 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000825 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
826 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 }
deadbeefd59daf82015-10-14 15:02:44 -0700828 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
829 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000830 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700831 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000832 }
deadbeefd59daf82015-10-14 15:02:44 -0700833 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000834 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 }
836 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000837 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
838 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700841 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
842 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000843 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700844 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000845 }
deadbeefd59daf82015-10-14 15:02:44 -0700846 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000847 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
849 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700850 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700851 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700852 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700853 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700854 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800855 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700856 const cricket::ContentGroup* answer_bundle =
857 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
858 if (!EnableBundle(*answer_bundle)) {
859 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
860 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
861 }
862 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800863 // Only push down the transport description after enabling BUNDLE; we don't
864 // want to push down a description on a transport about to be destroyed.
865 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
866 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700869 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000870 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700871 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000872 }
deadbeefd59daf82015-10-14 15:02:44 -0700873 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 }
876 }
877 return true;
878}
879
880WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
881 if (type == SessionDescriptionInterface::kOffer) {
882 return WebRtcSession::kOffer;
883 } else if (type == SessionDescriptionInterface::kPrAnswer) {
884 return WebRtcSession::kPrAnswer;
885 } else if (type == SessionDescriptionInterface::kAnswer) {
886 return WebRtcSession::kAnswer;
887 }
888 ASSERT(false && "unknown action type");
889 return WebRtcSession::kOffer;
890}
891
deadbeefd59daf82015-10-14 15:02:44 -0700892bool WebRtcSession::PushdownMediaDescription(
893 cricket::ContentAction action,
894 cricket::ContentSource source,
895 std::string* err) {
896 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
897 if (!ch) {
898 return true;
899 } else if (source == cricket::CS_LOCAL) {
900 return ch->PushdownLocalDescription(local_desc_->description(), action,
901 err);
902 } else {
903 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
904 err);
905 }
906 };
907
908 return (set_content(voice_channel()) &&
909 set_content(video_channel()) &&
910 set_content(data_channel()));
911}
912
913bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
914 cricket::ContentAction action,
915 std::string* error_desc) {
916 RTC_DCHECK(signaling_thread()->IsCurrent());
917
918 if (source == cricket::CS_LOCAL) {
919 return PushdownLocalTransportDescription(local_desc_->description(), action,
920 error_desc);
921 }
922 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
923 error_desc);
924}
925
926bool WebRtcSession::PushdownLocalTransportDescription(
927 const SessionDescription* sdesc,
928 cricket::ContentAction action,
929 std::string* err) {
930 RTC_DCHECK(signaling_thread()->IsCurrent());
931
932 if (!sdesc) {
933 return false;
934 }
935
936 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
937 if (!transport_controller_->SetLocalTransportDescription(
938 tinfo.content_name, tinfo.description, action, err)) {
939 return false;
940 }
941 }
942
943 return true;
944}
945
946bool WebRtcSession::PushdownRemoteTransportDescription(
947 const SessionDescription* sdesc,
948 cricket::ContentAction action,
949 std::string* err) {
950 RTC_DCHECK(signaling_thread()->IsCurrent());
951
952 if (!sdesc) {
953 return false;
954 }
955
956 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
957 if (!transport_controller_->SetRemoteTransportDescription(
958 tinfo.content_name, tinfo.description, action, err)) {
959 return false;
960 }
961 }
962
963 return true;
964}
965
966bool WebRtcSession::GetTransportDescription(
967 const SessionDescription* description,
968 const std::string& content_name,
969 cricket::TransportDescription* tdesc) {
970 if (!description || !tdesc) {
971 return false;
972 }
973 const TransportInfo* transport_info =
974 description->GetTransportInfoByName(content_name);
975 if (!transport_info) {
976 return false;
977 }
978 *tdesc = transport_info->description;
979 return true;
980}
981
982bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000983 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700984 return (GetChannelTransportStats(voice_channel(), stats) &&
985 GetChannelTransportStats(video_channel(), stats) &&
986 GetChannelTransportStats(data_channel(), stats));
987}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000988
deadbeefcbecd352015-09-23 11:50:27 -0700989bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -0700990 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -0700991 ASSERT(signaling_thread()->IsCurrent());
992 if (!ch) {
993 // Not using this channel.
994 return true;
995 }
996
997 const std::string& content_name = ch->content_name();
998 const std::string& transport_name = ch->transport_name();
999 stats->proxy_to_transport[content_name] = transport_name;
1000 if (stats->transport_stats.find(transport_name) !=
1001 stats->transport_stats.end()) {
1002 // Transport stats already done for this transport.
1003 return true;
1004 }
1005
1006 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001007 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001008 return false;
1009 }
1010
1011 stats->transport_stats[transport_name] = tstats;
1012 return true;
1013}
1014
1015bool WebRtcSession::GetLocalCertificate(
1016 const std::string& transport_name,
1017 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1018 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001019 return transport_controller_->GetLocalCertificate(transport_name,
1020 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001021}
1022
jbauch555604a2016-04-26 03:13:22 -07001023std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -07001024 const std::string& transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001025 ASSERT(signaling_thread()->IsCurrent());
kwibergb4d01c42016-04-06 05:15:06 -07001026 return transport_controller_->GetRemoteSSLCertificate(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -07001027}
1028
deadbeefcbecd352015-09-23 11:50:27 -07001029bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1030 const std::string* first_content_name = bundle.FirstContentName();
1031 if (!first_content_name) {
1032 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1033 return false;
1034 }
1035 const std::string& transport_name = *first_content_name;
1036 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1037
1038 auto maybe_set_transport = [this, bundle, transport_name,
1039 first_channel](cricket::BaseChannel* ch) {
1040 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001041 return true;
1042 }
1043
deadbeefcbecd352015-09-23 11:50:27 -07001044 if (ch->transport_name() == transport_name) {
1045 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1046 << " on " << transport_name << ".";
1047 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001048 }
torbjornga81a42f2015-09-23 02:16:58 -07001049
deadbeefcbecd352015-09-23 11:50:27 -07001050 if (!ch->SetTransport(transport_name)) {
1051 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1052 return false;
1053 }
1054 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1055 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001056 return true;
1057 };
1058
deadbeefcbecd352015-09-23 11:50:27 -07001059 if (!maybe_set_transport(voice_channel()) ||
1060 !maybe_set_transport(video_channel()) ||
1061 !maybe_set_transport(data_channel())) {
1062 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001063 }
deadbeefcbecd352015-09-23 11:50:27 -07001064
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001065 return true;
1066}
1067
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001069 if (!remote_desc_) {
1070 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1071 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001072 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 }
1074
1075 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001076 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 return false;
1078 }
1079
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001080 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001081 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1082 if (!valid) {
1083 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 }
1085
1086 // Add this candidate to the remote session description.
1087 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001088 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 return false;
1090 }
1091
deadbeefd59daf82015-10-14 15:02:44 -07001092 if (ready) {
1093 return UseCandidate(candidate);
1094 } else {
1095 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1096 return true;
1097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098}
1099
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001100bool WebRtcSession::RemoveRemoteIceCandidates(
1101 const std::vector<cricket::Candidate>& candidates) {
1102 if (!remote_desc_) {
1103 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1104 << "removed without any remote session description.";
1105 return false;
1106 }
1107
1108 if (candidates.empty()) {
1109 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1110 return false;
1111 }
1112
1113 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1114 if (number_removed != candidates.size()) {
1115 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1116 << "Requested " << candidates.size() << " but only "
1117 << number_removed << " are removed.";
1118 }
1119
1120 // Remove the candidates from the transport controller.
1121 std::string error;
1122 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1123 if (!res && !error.empty()) {
1124 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1125 }
1126 return true;
1127}
1128
deadbeefd59daf82015-10-14 15:02:44 -07001129cricket::IceConfig WebRtcSession::ParseIceConfig(
1130 const PeerConnectionInterface::RTCConfiguration& config) const {
1131 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001132 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001133 ice_config.prioritize_most_likely_candidate_pairs =
1134 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001135 ice_config.backup_connection_ping_interval =
1136 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001137 ice_config.gather_continually = (config.continual_gathering_policy ==
1138 PeerConnectionInterface::GATHER_CONTINUALLY);
1139 return ice_config;
1140}
1141
1142void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1143 transport_controller_->SetIceConfig(config);
1144}
1145
1146void WebRtcSession::MaybeStartGathering() {
1147 transport_controller_->MaybeStartGathering();
1148}
1149
Peter Boström0c4e06b2015-10-07 12:23:21 +02001150bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1151 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001152 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001154 }
1155 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156}
1157
Peter Boström0c4e06b2015-10-07 12:23:21 +02001158bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1159 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001160 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001161 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001162 }
1163 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164}
1165
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001166std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001168 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 return desc.str();
1170}
1171
solenbergd4cec0d2015-10-09 08:55:48 -07001172void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 ASSERT(signaling_thread()->IsCurrent());
1174 if (!voice_channel_) {
1175 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1176 return;
1177 }
solenberg4bac9c52015-10-09 02:32:53 -07001178 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1179 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 // otherwise. This in the normal case when the underlying media channel has
1181 // already been deleted.
1182 ASSERT(enable == false);
1183 }
1184}
1185
Peter Boström0c4e06b2015-10-07 12:23:21 +02001186void WebRtcSession::SetAudioSend(uint32_t ssrc,
1187 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001188 const cricket::AudioOptions& options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001189 cricket::AudioSource* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 ASSERT(signaling_thread()->IsCurrent());
1191 if (!voice_channel_) {
1192 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1193 return;
1194 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001195 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, source)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001196 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198}
1199
Peter Boström0c4e06b2015-10-07 12:23:21 +02001200void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001201 ASSERT(signaling_thread()->IsCurrent());
1202 ASSERT(volume >= 0 && volume <= 10);
1203 if (!voice_channel_) {
1204 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1205 return;
1206 }
1207
solenberg4bac9c52015-10-09 02:32:53 -07001208 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001209 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001210 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001211}
1212
deadbeef2d110be2016-01-13 12:00:26 -08001213void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
kwibergd1fe2812016-04-27 06:47:29 -07001214 std::unique_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001215 ASSERT(signaling_thread()->IsCurrent());
1216 if (!voice_channel_)
1217 return;
1218
kwiberg1c7fdd82016-04-26 08:18:04 -07001219 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001220}
1221
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001222RtpParameters WebRtcSession::GetAudioRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001223 ASSERT(signaling_thread()->IsCurrent());
1224 if (voice_channel_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001225 return voice_channel_->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001226 }
1227 return RtpParameters();
1228}
1229
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001230bool WebRtcSession::SetAudioRtpSendParameters(uint32_t ssrc,
1231 const RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001232 ASSERT(signaling_thread()->IsCurrent());
1233 if (!voice_channel_) {
1234 return false;
1235 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001236 return voice_channel_->SetRtpSendParameters(ssrc, parameters);
1237}
1238
1239RtpParameters WebRtcSession::GetAudioRtpReceiveParameters(uint32_t ssrc) const {
1240 ASSERT(signaling_thread()->IsCurrent());
1241 if (voice_channel_) {
1242 return voice_channel_->GetRtpReceiveParameters(ssrc);
1243 }
1244 return RtpParameters();
1245}
1246
1247bool WebRtcSession::SetAudioRtpReceiveParameters(
1248 uint32_t ssrc,
1249 const RtpParameters& parameters) {
1250 ASSERT(signaling_thread()->IsCurrent());
1251 if (!voice_channel_) {
1252 return false;
1253 }
1254 return voice_channel_->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001255}
1256
nisse2ded9b12016-04-08 02:23:55 -07001257bool WebRtcSession::SetSource(
1258 uint32_t ssrc,
1259 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 ASSERT(signaling_thread()->IsCurrent());
1261
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001262 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1264 // support video.
1265 LOG(LS_WARNING) << "Video not used in this call.";
1266 return false;
1267 }
nisse2ded9b12016-04-08 02:23:55 -07001268 video_channel_->SetSource(ssrc, source);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 return true;
1270}
1271
nisse08582ff2016-02-04 01:24:52 -08001272void WebRtcSession::SetVideoPlayout(
1273 uint32_t ssrc,
1274 bool enable,
1275 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 ASSERT(signaling_thread()->IsCurrent());
1277 if (!video_channel_) {
1278 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1279 return;
1280 }
nisse08582ff2016-02-04 01:24:52 -08001281 if (!video_channel_->SetSink(ssrc, enable ? sink : NULL)) {
1282 // Allow that SetSink fail if |sink| is NULL but assert otherwise.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 // This in the normal case when the underlying media channel has already
1284 // been deleted.
nisse08582ff2016-02-04 01:24:52 -08001285 ASSERT(sink == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 }
1287}
1288
Peter Boström0c4e06b2015-10-07 12:23:21 +02001289void WebRtcSession::SetVideoSend(uint32_t ssrc,
1290 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 const cricket::VideoOptions* options) {
1292 ASSERT(signaling_thread()->IsCurrent());
1293 if (!video_channel_) {
1294 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1295 return;
1296 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001297 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1299 // This in the normal case when the underlying media channel has already
1300 // been deleted.
1301 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303}
1304
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001305RtpParameters WebRtcSession::GetVideoRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001306 ASSERT(signaling_thread()->IsCurrent());
1307 if (video_channel_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001308 return video_channel_->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001309 }
1310 return RtpParameters();
1311}
1312
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001313bool WebRtcSession::SetVideoRtpSendParameters(uint32_t ssrc,
1314 const RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001315 ASSERT(signaling_thread()->IsCurrent());
1316 if (!video_channel_) {
1317 return false;
1318 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001319 return video_channel_->SetRtpSendParameters(ssrc, parameters);
1320}
1321
1322RtpParameters WebRtcSession::GetVideoRtpReceiveParameters(uint32_t ssrc) const {
1323 ASSERT(signaling_thread()->IsCurrent());
1324 if (video_channel_) {
1325 return video_channel_->GetRtpReceiveParameters(ssrc);
1326 }
1327 return RtpParameters();
1328}
1329
1330bool WebRtcSession::SetVideoRtpReceiveParameters(
1331 uint32_t ssrc,
1332 const RtpParameters& parameters) {
1333 ASSERT(signaling_thread()->IsCurrent());
1334 if (!video_channel_) {
1335 return false;
1336 }
1337 return video_channel_->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001338}
1339
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1341 ASSERT(signaling_thread()->IsCurrent());
1342 if (!voice_channel_) {
1343 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1344 return false;
1345 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001346 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1348 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001349 if (!local_desc_ ||
1350 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 &send_ssrc)) {
1352 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1353 return false;
1354 }
1355 return voice_channel_->CanInsertDtmf();
1356}
1357
1358bool WebRtcSession::InsertDtmf(const std::string& track_id,
1359 int code, int duration) {
1360 ASSERT(signaling_thread()->IsCurrent());
1361 if (!voice_channel_) {
1362 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1363 return false;
1364 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001365 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001366 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1367 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1369 return false;
1370 }
solenberg1d63dd02015-12-02 12:35:09 -08001371 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1373 return false;
1374 }
1375 return true;
1376}
1377
1378sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001379 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380}
1381
wu@webrtc.org78187522013-10-07 23:32:02 +00001382bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001383 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001384 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001385 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001386 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1387 return false;
1388 }
1389 return data_channel_->SendData(params, payload, result);
1390}
1391
1392bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001393 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001394 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1395 return false;
1396 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001397 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1398 &DataChannel::OnChannelReady);
1399 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1400 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001401 data_channel_->SignalStreamClosedRemotely.connect(
1402 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001403 return true;
1404}
1405
1406void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001407 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001408 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1409 return;
1410 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001411 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1412 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001413 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001414}
1415
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001416void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001417 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001418 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1419 return;
1420 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001421 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1422 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001423}
1424
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001425void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001426 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001427 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1428 << "NULL.";
1429 return;
1430 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001431 data_channel_->RemoveRecvStream(sid);
1432 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001433}
1434
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001435bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001436 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001437}
1438
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439cricket::DataChannelType WebRtcSession::data_channel_type() const {
1440 return data_channel_type_;
1441}
1442
deadbeef0ed85b22016-02-23 17:24:52 -08001443bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1444 return pending_ice_restarts_.find(content_name) !=
1445 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001446}
1447
Henrik Boströmd8281982015-08-27 10:12:24 +02001448void WebRtcSession::OnCertificateReady(
1449 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001450 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001451}
1452
Henrik Boströmd8281982015-08-27 10:12:24 +02001453bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001454 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001455}
1456
deadbeefcbecd352015-09-23 11:50:27 -07001457const rtc::scoped_refptr<rtc::RTCCertificate>&
1458WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001459 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001460}
1461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462void WebRtcSession::SetIceConnectionState(
1463 PeerConnectionInterface::IceConnectionState state) {
1464 if (ice_connection_state_ == state) {
1465 return;
1466 }
1467
1468 // ASSERT that the requested transition is allowed. Note that
1469 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1470 // within PeerConnection). This switch statement should compile away when
1471 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001472 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1473 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 switch (ice_connection_state_) {
1475 case PeerConnectionInterface::kIceConnectionNew:
1476 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1477 break;
1478 case PeerConnectionInterface::kIceConnectionChecking:
1479 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1480 state == PeerConnectionInterface::kIceConnectionConnected);
1481 break;
1482 case PeerConnectionInterface::kIceConnectionConnected:
1483 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1484 state == PeerConnectionInterface::kIceConnectionChecking ||
1485 state == PeerConnectionInterface::kIceConnectionCompleted);
1486 break;
1487 case PeerConnectionInterface::kIceConnectionCompleted:
1488 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1489 state == PeerConnectionInterface::kIceConnectionDisconnected);
1490 break;
1491 case PeerConnectionInterface::kIceConnectionFailed:
1492 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1493 break;
1494 case PeerConnectionInterface::kIceConnectionDisconnected:
1495 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1496 state == PeerConnectionInterface::kIceConnectionConnected ||
1497 state == PeerConnectionInterface::kIceConnectionCompleted ||
1498 state == PeerConnectionInterface::kIceConnectionFailed);
1499 break;
1500 case PeerConnectionInterface::kIceConnectionClosed:
1501 ASSERT(false);
1502 break;
1503 default:
1504 ASSERT(false);
1505 break;
1506 }
1507
1508 ice_connection_state_ = state;
1509 if (ice_observer_) {
1510 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1511 }
1512}
1513
deadbeefcbecd352015-09-23 11:50:27 -07001514void WebRtcSession::OnTransportControllerConnectionState(
1515 cricket::IceConnectionState state) {
1516 switch (state) {
1517 case cricket::kIceConnectionConnecting:
1518 // If the current state is Connected or Completed, then there were
1519 // writable channels but now there are not, so the next state must
1520 // be Disconnected.
1521 // kIceConnectionConnecting is currently used as the default,
1522 // un-connected state by the TransportController, so its only use is
1523 // detecting disconnections.
1524 if (ice_connection_state_ ==
1525 PeerConnectionInterface::kIceConnectionConnected ||
1526 ice_connection_state_ ==
1527 PeerConnectionInterface::kIceConnectionCompleted) {
1528 SetIceConnectionState(
1529 PeerConnectionInterface::kIceConnectionDisconnected);
1530 }
torbjornga81a42f2015-09-23 02:16:58 -07001531 break;
deadbeefcbecd352015-09-23 11:50:27 -07001532 case cricket::kIceConnectionFailed:
1533 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1534 break;
1535 case cricket::kIceConnectionConnected:
1536 LOG(LS_INFO) << "Changing to ICE connected state because "
1537 << "all transports are writable.";
1538 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1539 break;
1540 case cricket::kIceConnectionCompleted:
1541 LOG(LS_INFO) << "Changing to ICE completed state because "
1542 << "all transports are complete.";
1543 if (ice_connection_state_ !=
1544 PeerConnectionInterface::kIceConnectionConnected) {
1545 // If jumping directly from "checking" to "connected",
1546 // signal "connected" first.
1547 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1548 }
1549 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1550 if (metrics_observer_) {
1551 ReportTransportStats();
1552 }
1553 break;
1554 default:
1555 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001556 }
deadbeefcbecd352015-09-23 11:50:27 -07001557}
1558
1559void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001560 SetIceConnectionReceiving(receiving);
1561}
1562
1563void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1564 if (ice_connection_receiving_ == receiving) {
1565 return;
1566 }
1567 ice_connection_receiving_ = receiving;
1568 if (ice_observer_) {
1569 ice_observer_->OnIceConnectionReceivingChange(receiving);
1570 }
1571}
1572
deadbeefcbecd352015-09-23 11:50:27 -07001573void WebRtcSession::OnTransportControllerCandidatesGathered(
1574 const std::string& transport_name,
1575 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001577 int sdp_mline_index;
1578 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1579 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1580 << transport_name << " not found";
1581 return;
1582 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583
deadbeefcbecd352015-09-23 11:50:27 -07001584 for (cricket::Candidates::const_iterator citer = candidates.begin();
1585 citer != candidates.end(); ++citer) {
1586 // Use transport_name as the candidate media id.
1587 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1588 if (ice_observer_) {
1589 ice_observer_->OnIceCandidate(&candidate);
1590 }
1591 if (local_desc_) {
1592 local_desc_->AddCandidate(&candidate);
1593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 }
1595}
1596
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001597void WebRtcSession::OnTransportControllerCandidatesRemoved(
1598 const std::vector<cricket::Candidate>& candidates) {
1599 ASSERT(signaling_thread()->IsCurrent());
1600 // Sanity check.
1601 for (const cricket::Candidate& candidate : candidates) {
1602 if (candidate.transport_name().empty()) {
1603 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1604 << "empty content name in candidate "
1605 << candidate.ToString();
1606 return;
1607 }
1608 }
1609
1610 if (local_desc_) {
1611 local_desc_->RemoveCandidates(candidates);
1612 }
1613 if (ice_observer_) {
1614 ice_observer_->OnIceCandidatesRemoved(candidates);
1615 }
1616}
1617
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618// Enabling voice and video channel.
1619void WebRtcSession::EnableChannels() {
1620 if (voice_channel_ && !voice_channel_->enabled())
1621 voice_channel_->Enable(true);
1622
1623 if (video_channel_ && !video_channel_->enabled())
1624 video_channel_->Enable(true);
1625
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001626 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 data_channel_->Enable(true);
1628}
1629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630// Returns the media index for a local ice candidate given the content name.
1631bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1632 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001633 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001635 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636
1637 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001638 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 for (size_t index = 0; index < contents.size(); ++index) {
1640 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001641 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 content_found = true;
1643 break;
1644 }
1645 }
1646 return content_found;
1647}
1648
1649bool WebRtcSession::UseCandidatesInSessionDescription(
1650 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001651 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001653 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001655
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1657 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001658 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001659 const IceCandidateInterface* candidate = candidates->at(n);
1660 bool valid = false;
1661 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1662 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001663 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1664 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001665 }
1666 continue;
1667 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001668 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001669 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 break;
deadbeefd59daf82015-10-14 15:02:44 -07001671 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 }
1673 }
1674 return ret;
1675}
1676
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001677bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001679 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001681 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 return false;
1683 }
1684
1685 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001686 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 std::vector<cricket::Candidate> candidates;
1688 candidates.push_back(candidate->candidate());
1689 // Invoking BaseSession method to handle remote candidates.
1690 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001691 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1692 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 // Candidates successfully submitted for checking.
1694 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1695 ice_connection_state_ ==
1696 PeerConnectionInterface::kIceConnectionDisconnected) {
1697 // If state is New, then the session has just gotten its first remote ICE
1698 // candidates, so go to Checking.
1699 // If state is Disconnected, the session is re-using old candidates or
1700 // receiving additional ones, so go to Checking.
1701 // If state is Connected, stay Connected.
1702 // TODO(bemasc): If state is Connected, and the new candidates are for a
1703 // newly added transport, then the state actually _should_ move to
1704 // checking. Add a way to distinguish that case.
1705 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1706 }
1707 // TODO(bemasc): If state is Completed, go back to Connected.
1708 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001709 if (!error.empty()) {
1710 LOG(LS_WARNING) << error;
1711 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 }
1713 return true;
1714}
1715
deadbeefcbecd352015-09-23 11:50:27 -07001716void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001717 // Destroy video_channel_ first since it may have a pointer to the
1718 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 const cricket::ContentInfo* video_info =
1720 cricket::GetFirstVideoContent(desc);
1721 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 }
1725
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001726 const cricket::ContentInfo* voice_info =
1727 cricket::GetFirstAudioContent(desc);
1728 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001729 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001730 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001731 }
1732
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 const cricket::ContentInfo* data_info =
1734 cricket::GetFirstDataContent(desc);
1735 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 SignalDataChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 }
1739}
1740
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001741// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001742// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 // Creating the media channels and transport proxies.
1745 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1746 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001747 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 LOG(LS_ERROR) << "Failed to create voice channel.";
1749 return false;
1750 }
1751 }
1752
1753 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1754 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001755 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 LOG(LS_ERROR) << "Failed to create video channel.";
1757 return false;
1758 }
1759 }
1760
1761 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1762 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001763 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001764 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 LOG(LS_ERROR) << "Failed to create data channel.";
1766 return false;
1767 }
1768 }
1769
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001770 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1771 if (voice_channel()) {
1772 voice_channel()->ActivateRtcpMux();
1773 }
1774 if (video_channel()) {
1775 video_channel()->ActivateRtcpMux();
1776 }
1777 if (data_channel()) {
1778 data_channel()->ActivateRtcpMux();
1779 }
1780 }
1781
deadbeefcbecd352015-09-23 11:50:27 -07001782 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001783 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001784 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1785 cricket::GROUP_TYPE_BUNDLE);
1786 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001787 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1788 return false;
1789 }
deadbeefcbecd352015-09-23 11:50:27 -07001790 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001791 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1792 return false;
1793 }
1794 }
1795
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 return true;
1797}
1798
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001799bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001801 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001802 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001803 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001804 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001805 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001806
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001807 voice_channel_->SignalDtlsSetupFailure.connect(
1808 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001809
1810 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001811 voice_channel_->SignalSentPacket.connect(this,
1812 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001813 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814}
1815
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001816bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001818 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001819 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001820 if (!video_channel_) {
1821 return false;
1822 }
1823
1824 video_channel_->SignalDtlsSetupFailure.connect(
1825 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001826
1827 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001828 video_channel_->SignalSentPacket.connect(this,
1829 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001830 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831}
1832
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001833bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001834 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001836 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001837 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001838 return false;
1839 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001840
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001841 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001842 data_channel_->SignalDataReceived.connect(
1843 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001844 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001845
1846 data_channel_->SignalDtlsSetupFailure.connect(
1847 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001848
1849 SignalDataChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001850 data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001851 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852}
1853
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001854void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001855 SetError(ERROR_TRANSPORT,
1856 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857}
1858
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001859void WebRtcSession::OnDataChannelMessageReceived(
1860 cricket::DataChannel* channel,
1861 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001862 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001863 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1864 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1865 // Received OPEN message; parse and signal that a new data channel should
1866 // be created.
1867 std::string label;
1868 InternalDataChannelInit config;
1869 config.id = params.ssrc;
1870 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1871 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1872 << params.ssrc;
1873 return;
1874 }
1875 config.open_handshake_role = InternalDataChannelInit::kAcker;
1876 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 }
deadbeefab9b2d12015-10-14 11:33:11 -07001878 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879}
1880
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001881// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001882bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001883 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1884 if (!bundle_enabled)
1885 return true;
1886
1887 const cricket::ContentGroup* bundle_group =
1888 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1889 ASSERT(bundle_group != NULL);
1890
1891 const cricket::ContentInfos& contents = desc->contents();
1892 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1893 citer != contents.end(); ++citer) {
1894 const cricket::ContentInfo* content = (&*citer);
1895 ASSERT(content != NULL);
1896 if (bundle_group->HasContentName(content->name) &&
1897 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1898 if (!HasRtcpMuxEnabled(content))
1899 return false;
1900 }
1901 }
1902 // RTCP-MUX is enabled in all the contents.
1903 return true;
1904}
1905
1906bool WebRtcSession::HasRtcpMuxEnabled(
1907 const cricket::ContentInfo* content) {
1908 const cricket::MediaContentDescription* description =
1909 static_cast<cricket::MediaContentDescription*>(content->description);
1910 return description->rtcp_mux();
1911}
1912
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913bool WebRtcSession::ValidateSessionDescription(
1914 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001915 cricket::ContentSource source, std::string* err_desc) {
1916 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001917 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001918 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001919 }
1920
1921 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001922 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001923 }
1924
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001925 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001926 Action action = GetAction(sdesc->type());
1927 if (source == cricket::CS_LOCAL) {
1928 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001929 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930 } else {
1931 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001932 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001933 }
1934
1935 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001936 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001937 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1938 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001939 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001940 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001941 }
1942
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001943 // Verify ice-ufrag and ice-pwd.
1944 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001945 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001946 }
1947
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001948 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001949 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001950 }
1951
1952 // Verify m-lines in Answer when compared against Offer.
1953 if (action == kAnswer) {
1954 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001955 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1956 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001957 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001958 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001959 }
1960 }
1961
1962 return true;
1963}
1964
1965bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1966 return ((action == kOffer && state() == STATE_INIT) ||
1967 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001968 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001969 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001970 (action == kOffer && state() == STATE_INPROGRESS) ||
1971 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001972 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1973 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1974 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1975 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001976}
1977
1978bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1979 return ((action == kOffer && state() == STATE_INIT) ||
1980 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001981 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001982 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001983 (action == kOffer && state() == STATE_INPROGRESS) ||
1984 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001985 (action == kAnswer && state() == STATE_SENTOFFER) ||
1986 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1987 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1988 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001989}
1990
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001991std::string WebRtcSession::GetSessionErrorMsg() {
1992 std::ostringstream desc;
1993 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1994 desc << kSessionErrorDesc << error_desc() << ".";
1995 return desc.str();
1996}
1997
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001998// We need to check the local/remote description for the Transport instead of
1999// the session, because a new Transport added during renegotiation may have
2000// them unset while the session has them set from the previous negotiation.
2001// Not doing so may trigger the auto generation of transport description and
2002// mess up DTLS identity information, ICE credential, etc.
2003bool WebRtcSession::ReadyToUseRemoteCandidate(
2004 const IceCandidateInterface* candidate,
2005 const SessionDescriptionInterface* remote_desc,
2006 bool* valid) {
2007 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002008
2009 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002010 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002011
deadbeefd59daf82015-10-14 15:02:44 -07002012 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002013 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002014 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002015
2016 size_t mediacontent_index =
2017 static_cast<size_t>(candidate->sdp_mline_index());
2018 size_t remote_content_size =
2019 current_remote_desc->description()->contents().size();
2020 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07002021 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
2022 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002023
2024 *valid = false;
2025 return false;
2026 }
2027
2028 cricket::ContentInfo content =
2029 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002030 cricket::BaseChannel* channel = GetChannel(content.name);
2031 if (!channel) {
2032 return false;
2033 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002034
deadbeefd59daf82015-10-14 15:02:44 -07002035 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002036 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002037}
2038
deadbeefcbecd352015-09-23 11:50:27 -07002039void WebRtcSession::OnTransportControllerGatheringState(
2040 cricket::IceGatheringState state) {
2041 ASSERT(signaling_thread()->IsCurrent());
2042 if (state == cricket::kIceGatheringGathering) {
2043 if (ice_observer_) {
2044 ice_observer_->OnIceGatheringChange(
2045 PeerConnectionInterface::kIceGatheringGathering);
2046 }
2047 } else if (state == cricket::kIceGatheringComplete) {
2048 if (ice_observer_) {
2049 ice_observer_->OnIceGatheringChange(
2050 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07002051 }
2052 }
2053}
2054
2055void WebRtcSession::ReportTransportStats() {
2056 // Use a set so we don't report the same stats twice if two channels share
2057 // a transport.
2058 std::set<std::string> transport_names;
2059 if (voice_channel()) {
2060 transport_names.insert(voice_channel()->transport_name());
2061 }
2062 if (video_channel()) {
2063 transport_names.insert(video_channel()->transport_name());
2064 }
2065 if (data_channel()) {
2066 transport_names.insert(data_channel()->transport_name());
2067 }
2068 for (const auto& name : transport_names) {
2069 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002070 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002071 ReportBestConnectionState(stats);
2072 ReportNegotiatedCiphers(stats);
2073 }
2074 }
2075}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002076// Walk through the ConnectionInfos to gather best connection usage
2077// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002078void WebRtcSession::ReportBestConnectionState(
2079 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002080 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002081 for (cricket::TransportChannelStatsList::const_iterator it =
2082 stats.channel_stats.begin();
2083 it != stats.channel_stats.end(); ++it) {
2084 for (cricket::ConnectionInfos::const_iterator it_info =
2085 it->connection_infos.begin();
2086 it_info != it->connection_infos.end(); ++it_info) {
2087 if (!it_info->best_connection) {
2088 continue;
2089 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002090
2091 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2092 const cricket::Candidate& local = it_info->local_candidate;
2093 const cricket::Candidate& remote = it_info->remote_candidate;
2094
2095 // Increment the counter for IceCandidatePairType.
2096 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2097 (local.type() == RELAY_PORT_TYPE &&
2098 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2099 type = kEnumCounterIceCandidatePairTypeTcp;
2100 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2101 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002102 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002103 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002104 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002105 metrics_observer_->IncrementEnumCounter(
2106 type, GetIceCandidatePairCounter(local, remote),
2107 kIceCandidatePairMax);
2108
2109 // Increment the counter for IP type.
2110 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002111 metrics_observer_->IncrementEnumCounter(
2112 kEnumCounterAddressFamily, kBestConnections_IPv4,
2113 kPeerConnectionAddressFamilyCounter_Max);
2114
2115 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002116 metrics_observer_->IncrementEnumCounter(
2117 kEnumCounterAddressFamily, kBestConnections_IPv6,
2118 kPeerConnectionAddressFamilyCounter_Max);
2119 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002120 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002121 }
2122
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002123 return;
2124 }
2125 }
2126}
2127
jbauchac8869e2015-07-03 01:36:14 -07002128void WebRtcSession::ReportNegotiatedCiphers(
2129 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002130 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002131 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2132 return;
2133 }
2134
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002135 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2136 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2137 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2138 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002139 return;
2140 }
2141
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002142 PeerConnectionEnumCounterType srtp_counter_type;
2143 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002144 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002145 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2146 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002147 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002148 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2149 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002150 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002151 srtp_counter_type = kEnumCounterDataSrtpCipher;
2152 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002153 } else {
2154 RTC_NOTREACHED();
2155 return;
2156 }
2157
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002158 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2159 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2160 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002161 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002162 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2163 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2164 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002165 }
2166}
2167
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002168void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002169 RTC_DCHECK(worker_thread()->IsCurrent());
2170 media_controller_->call_w()->OnSentPacket(sent_packet);
2171}
2172
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173} // namespace webrtc