blob: 35a5fab4c6ec08850f13ce8a11fe015ae6e61609 [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"
zhihuang34b54c32016-08-04 11:06:50 -070027#include "webrtc/base/bind.h"
jbauchac8869e2015-07-03 01:36:14 -070028#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000029#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070033#include "webrtc/call.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/videocapturer.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000036#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070037#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010038#include "webrtc/pc/channel.h"
39#include "webrtc/pc/channelmanager.h"
40#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
zhihuang34b54c32016-08-04 11:06:50 -070042#ifdef HAVE_QUIC
43#include "webrtc/p2p/quic/quictransportchannel.h"
44#endif // HAVE_QUIC
45
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046using cricket::ContentInfo;
47using cricket::ContentInfos;
48using cricket::MediaContentDescription;
49using cricket::SessionDescription;
50using cricket::TransportInfo;
51
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070052using cricket::LOCAL_PORT_TYPE;
53using cricket::STUN_PORT_TYPE;
54using cricket::RELAY_PORT_TYPE;
55using cricket::PRFLX_PORT_TYPE;
56
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057namespace webrtc {
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
61 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063const char kInvalidCandidates[] = "Description contains invalid candidates.";
64const char kInvalidSdp[] = "Invalid session description.";
65const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
67const char kPushDownTDFailed[] =
68 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000069const char kSdpWithoutDtlsFingerprint[] =
70 "Called with SDP without DTLS fingerprint.";
71const char kSdpWithoutSdesCrypto[] =
72 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000073const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000074 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000077const char kDtlsSetupFailureRtp[] =
78 "Couldn't set up DTLS-SRTP on RTP channel.";
79const char kDtlsSetupFailureRtcp[] =
80 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070081const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070083IceCandidatePairType GetIceCandidatePairCounter(
84 const cricket::Candidate& local,
85 const cricket::Candidate& remote) {
86 const auto& l = local.type();
87 const auto& r = remote.type();
88 const auto& host = LOCAL_PORT_TYPE;
89 const auto& srflx = STUN_PORT_TYPE;
90 const auto& relay = RELAY_PORT_TYPE;
91 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -070092 if (l == host && r == host) {
93 bool local_private = IPIsPrivate(local.address().ipaddr());
94 bool remote_private = IPIsPrivate(remote.address().ipaddr());
95 if (local_private) {
96 if (remote_private) {
97 return kIceCandidatePairHostPrivateHostPrivate;
98 } else {
99 return kIceCandidatePairHostPrivateHostPublic;
100 }
101 } else {
102 if (remote_private) {
103 return kIceCandidatePairHostPublicHostPrivate;
104 } else {
105 return kIceCandidatePairHostPublicHostPublic;
106 }
107 }
108 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700109 if (l == host && r == srflx)
110 return kIceCandidatePairHostSrflx;
111 if (l == host && r == relay)
112 return kIceCandidatePairHostRelay;
113 if (l == host && r == prflx)
114 return kIceCandidatePairHostPrflx;
115 if (l == srflx && r == host)
116 return kIceCandidatePairSrflxHost;
117 if (l == srflx && r == srflx)
118 return kIceCandidatePairSrflxSrflx;
119 if (l == srflx && r == relay)
120 return kIceCandidatePairSrflxRelay;
121 if (l == srflx && r == prflx)
122 return kIceCandidatePairSrflxPrflx;
123 if (l == relay && r == host)
124 return kIceCandidatePairRelayHost;
125 if (l == relay && r == srflx)
126 return kIceCandidatePairRelaySrflx;
127 if (l == relay && r == relay)
128 return kIceCandidatePairRelayRelay;
129 if (l == relay && r == prflx)
130 return kIceCandidatePairRelayPrflx;
131 if (l == prflx && r == host)
132 return kIceCandidatePairPrflxHost;
133 if (l == prflx && r == srflx)
134 return kIceCandidatePairPrflxSrflx;
135 if (l == prflx && r == relay)
136 return kIceCandidatePairPrflxRelay;
137 return kIceCandidatePairMax;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140// Compares |answer| against |offer|. Comparision is done
141// for number of m-lines in answer against offer. If matches true will be
142// returned otherwise false.
143static bool VerifyMediaDescriptions(
144 const SessionDescription* answer, const SessionDescription* offer) {
145 if (offer->contents().size() != answer->contents().size())
146 return false;
147
148 for (size_t i = 0; i < offer->contents().size(); ++i) {
149 if ((offer->contents()[i].name) != answer->contents()[i].name) {
150 return false;
151 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000152 const MediaContentDescription* offer_mdesc =
153 static_cast<const MediaContentDescription*>(
154 offer->contents()[i].description);
155 const MediaContentDescription* answer_mdesc =
156 static_cast<const MediaContentDescription*>(
157 answer->contents()[i].description);
158 if (offer_mdesc->type() != answer_mdesc->type()) {
159 return false;
160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 }
162 return true;
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Checks that each non-rejected content has SDES crypto keys or a DTLS
166// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
167// keys, will be caught in Transport negotiation, and backstopped by Channel's
168// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000169static bool VerifyCrypto(const SessionDescription* desc,
170 bool dtls_enabled,
171 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 const ContentInfos& contents = desc->contents();
173 for (size_t index = 0; index < contents.size(); ++index) {
174 const ContentInfo* cinfo = &contents[index];
175 if (cinfo->rejected) {
176 continue;
177 }
178
179 // If the content isn't rejected, crypto must be present.
180 const MediaContentDescription* media =
181 static_cast<const MediaContentDescription*>(cinfo->description);
182 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
183 if (!media || !tinfo) {
184 // Something is not right.
185 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000186 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 return false;
188 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000189 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000190 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000191 LOG(LS_WARNING) <<
192 "Session description must have DTLS fingerprint if DTLS enabled.";
193 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 return false;
195 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000196 } else {
197 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 LOG(LS_WARNING) <<
199 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 return false;
202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 }
204 }
205
206 return true;
207}
208
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000209// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
210static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
211 const ContentInfos& contents = desc->contents();
212 for (size_t index = 0; index < contents.size(); ++index) {
213 const ContentInfo* cinfo = &contents[index];
214 if (cinfo->rejected) {
215 continue;
216 }
217
218 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
219 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
220 if (!tinfo) {
221 // Something is not right.
222 LOG(LS_ERROR) << kInvalidSdp;
223 return false;
224 }
225 if (tinfo->description.ice_ufrag.empty() ||
226 tinfo->description.ice_pwd.empty()) {
227 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
228 return false;
229 }
230 }
231 return true;
232}
233
wu@webrtc.org91053e72013-08-10 07:18:04 +0000234// Forces |sdesc->crypto_required| to the appropriate state based on the
235// current security policy, to ensure a failure occurs if there is an error
236// in crypto negotiation.
237// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000238static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
239 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 if (!sdesc) {
241 return;
242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243
wu@webrtc.org91053e72013-08-10 07:18:04 +0000244 // Updating the |crypto_required_| in MediaContentDescription to the
245 // appropriate state based on the current security policy.
246 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
247 iter != sdesc->contents().end(); ++iter) {
248 if (cricket::IsMediaContent(&*iter)) {
249 MediaContentDescription* mdesc =
250 static_cast<MediaContentDescription*> (iter->description);
251 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000252 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000253 }
254 }
255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256}
257
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
259 const std::string& track_id,
260 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 const cricket::ContentInfo* audio_info =
262 cricket::GetFirstAudioContent(session_description);
263 if (!audio_info) {
264 LOG(LS_ERROR) << "Audio not used in this call";
265 return false;
266 }
267
268 const cricket::MediaContentDescription* audio_content =
269 static_cast<const cricket::MediaContentDescription*>(
270 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000271 const cricket::StreamParams* stream =
272 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
273 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return false;
275 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000276
277 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 return true;
279}
280
281static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200282 uint32_t ssrc,
283 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 ASSERT(track_id != NULL);
285
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 const cricket::ContentInfo* audio_info =
287 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000288 if (audio_info) {
289 const cricket::MediaContentDescription* audio_content =
290 static_cast<const cricket::MediaContentDescription*>(
291 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000293 const auto* found =
294 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
295 if (found) {
296 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000297 return true;
298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 }
300
301 const cricket::ContentInfo* video_info =
302 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000303 if (video_info) {
304 const cricket::MediaContentDescription* video_content =
305 static_cast<const cricket::MediaContentDescription*>(
306 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000308 const auto* found =
309 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
310 if (found) {
311 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000312 return true;
313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 }
315 return false;
316}
317
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000318static bool BadSdp(const std::string& source,
319 const std::string& type,
320 const std::string& reason,
321 std::string* err_desc) {
322 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700323 desc << "Failed to set " << source;
324 if (!type.empty()) {
325 desc << " " << type;
326 }
327 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000328
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000330 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 return false;
334}
335
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 const std::string& type,
338 const std::string& reason,
339 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
345}
346
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000347static bool BadLocalSdp(const std::string& type,
348 const std::string& reason,
349 std::string* err_desc) {
350 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
351}
352
353static bool BadRemoteSdp(const std::string& type,
354 const std::string& reason,
355 std::string* err_desc) {
356 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
357}
358
359static bool BadOfferSdp(cricket::ContentSource source,
360 const std::string& reason,
361 std::string* err_desc) {
362 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
363}
364
365static bool BadPranswerSdp(cricket::ContentSource source,
366 const std::string& reason,
367 std::string* err_desc) {
368 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
369 reason, err_desc);
370}
371
372static bool BadAnswerSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376}
377
deadbeefd59daf82015-10-14 15:02:44 -0700378#define GET_STRING_OF_STATE(state) \
379 case webrtc::WebRtcSession::state: \
380 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 break;
382
deadbeefd59daf82015-10-14 15:02:44 -0700383static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 std::string result;
385 switch (state) {
386 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700387 GET_STRING_OF_STATE(STATE_SENTOFFER)
388 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
389 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
390 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700392 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 default:
394 ASSERT(false);
395 break;
396 }
397 return result;
398}
399
deadbeefd59daf82015-10-14 15:02:44 -0700400#define GET_STRING_OF_ERROR_CODE(err) \
401 case webrtc::WebRtcSession::err: \
402 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 break;
404
deadbeefd59daf82015-10-14 15:02:44 -0700405static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 std::string result;
407 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000408 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000409 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
410 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 default:
deadbeefd59daf82015-10-14 15:02:44 -0700412 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 break;
414 }
415 return result;
416}
417
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000418static std::string MakeErrorString(const std::string& error,
419 const std::string& desc) {
420 std::ostringstream ret;
421 ret << error << " " << desc;
422 return ret.str();
423}
424
425static std::string MakeTdErrorString(const std::string& desc) {
426 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427}
428
deadbeef0ed85b22016-02-23 17:24:52 -0800429// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
430bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
431 const SessionDescriptionInterface* new_desc,
432 const std::string& content_name) {
433 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700434 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 }
deadbeef0ed85b22016-02-23 17:24:52 -0800436 const SessionDescription* new_sd = new_desc->description();
437 const SessionDescription* old_sd = old_desc->description();
438 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
439 if (!cinfo || cinfo->rejected) {
440 return false;
441 }
442 // If the content isn't rejected, check if ufrag and password has changed.
443 const cricket::TransportDescription* new_transport_desc =
444 new_sd->GetTransportDescriptionByName(content_name);
445 const cricket::TransportDescription* old_transport_desc =
446 old_sd->GetTransportDescriptionByName(content_name);
447 if (!new_transport_desc || !old_transport_desc) {
448 // No transport description exists. This is not an ICE restart.
449 return false;
450 }
451 if (cricket::IceCredentialsChanged(
452 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
453 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
454 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
455 << ".";
456 return true;
457 }
458 return false;
459}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460
zhihuang29ff8442016-07-27 11:07:25 -0700461WebRtcSession::WebRtcSession(
462 webrtc::MediaControllerInterface* media_controller,
463 rtc::Thread* network_thread,
464 rtc::Thread* worker_thread,
465 rtc::Thread* signaling_thread,
466 cricket::PortAllocator* port_allocator,
467 std::unique_ptr<cricket::TransportController> transport_controller)
zhihuang34b54c32016-08-04 11:06:50 -0700468 : network_thread_(network_thread),
469 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700470 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // RFC 3264: The numeric value of the session id and version in the
472 // o line MUST be representable with a "64 bit signed integer".
473 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700474 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700475 transport_controller_(std::move(transport_controller)),
stefanc1aeaf02015-10-15 07:26:07 -0700476 media_controller_(media_controller),
477 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 ice_observer_(NULL),
479 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700480 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000482 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000484 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700485 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
486 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700487 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700488 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700489 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700490 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700491 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700492 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700493 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700494 transport_controller_->SignalCandidatesRemoved.connect(
495 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496}
497
498WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700499 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000500 // Destroy video_channel_ first since it may have a pointer to the
501 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000502 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 SignalVideoChannelDestroyed();
504 channel_manager_->DestroyVideoChannel(video_channel_.release());
505 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000506 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000507 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200508 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000509 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000510 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 SignalDataChannelDestroyed();
512 channel_manager_->DestroyDataChannel(data_channel_.release());
513 }
zhihuang34b54c32016-08-04 11:06:50 -0700514#ifdef HAVE_QUIC
515 if (quic_data_transport_) {
516 quic_data_transport_.reset();
517 }
518#endif
deadbeef057ecf02016-01-20 14:30:43 -0800519 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700520
521 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522}
523
wu@webrtc.org91053e72013-08-10 07:18:04 +0000524bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000525 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200526 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200527 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
528 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700529 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700530 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000531
Henrik Boström87713d02015-08-25 09:53:21 +0200532 // Obtain a certificate from RTCConfiguration if any were provided (optional).
533 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
534 if (!rtc_configuration.certificates.empty()) {
535 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
536 // just picking the first one. The decision should be made based on the DTLS
537 // handshake. The DTLS negotiations need to know about all certificates.
538 certificate = rtc_configuration.certificates[0];
539 }
540
honghaiz1f429e32015-09-28 07:57:34 -0700541 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700542
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000543 if (options.disable_encryption) {
544 dtls_enabled_ = false;
545 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200546 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200547 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800548 // |rtc_configuration| can override the default |dtls_enabled_| value.
549 if (rtc_configuration.enable_dtls_srtp) {
550 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000551 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000552 }
553
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000555 // It takes precendence over the disable_sctp_data_channels
556 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800557 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 data_channel_type_ = cricket::DCT_RTP;
zhihuang34b54c32016-08-04 11:06:50 -0700559 }
560#ifdef HAVE_QUIC
561 else if (rtc_configuration.enable_quic) {
562 // Use QUIC instead of DTLS when |enable_quic| is true.
563 data_channel_type_ = cricket::DCT_QUIC;
564 transport_controller_->use_quic();
565 if (dtls_enabled_) {
566 LOG(LS_INFO) << "Using QUIC instead of DTLS";
567 }
568 quic_data_transport_.reset(
569 new QuicDataTransport(signaling_thread(), worker_thread(),
570 network_thread(), transport_controller_.get()));
571 }
572#endif // HAVE_QUIC
573 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000574 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000575 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000576 data_channel_type_ = cricket::DCT_SCTP;
577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579
htaa2a49d92016-03-04 02:51:39 -0800580 video_options_.screencast_min_bitrate_kbps =
581 rtc_configuration.screencast_min_bitrate;
582 audio_options_.combined_audio_video_bwe =
583 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000584
kwiberg102c6a62015-10-30 02:47:38 -0700585 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100586 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200587
Karl Wibergbe579832015-11-10 22:34:18 +0100588 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
589 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200590
Henrik Boström87713d02015-08-25 09:53:21 +0200591 if (!dtls_enabled_) {
592 // Construct with DTLS disabled.
593 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200594 signaling_thread(), channel_manager_, this, id(),
595 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200596 } else {
597 // Construct with DTLS enabled.
598 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200599 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200600 signaling_thread(), channel_manager_, this, id(),
601 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200602 } else {
603 // Use the already generated certificate.
604 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200605 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200606 }
607 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000608
Henrik Boströmd8281982015-08-27 10:12:24 +0200609 webrtc_session_desc_factory_->SignalCertificateReady.connect(
610 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000611
wu@webrtc.org97077a32013-10-25 21:18:33 +0000612 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000613 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000614 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700615
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 return true;
617}
618
deadbeefd59daf82015-10-14 15:02:44 -0700619void WebRtcSession::Close() {
620 SetState(STATE_CLOSED);
621 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000622 ASSERT(!voice_channel_);
623 ASSERT(!video_channel_);
624 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800625 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626}
627
deadbeef0ed85b22016-02-23 17:24:52 -0800628cricket::BaseChannel* WebRtcSession::GetChannel(
629 const std::string& content_name) {
630 if (voice_channel() && voice_channel()->content_name() == content_name) {
631 return voice_channel();
632 }
633 if (video_channel() && video_channel()->content_name() == content_name) {
634 return video_channel();
635 }
636 if (data_channel() && data_channel()->content_name() == content_name) {
637 return data_channel();
638 }
639 return nullptr;
640}
641
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000642void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
643 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644}
645
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000646cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
647 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648}
649
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800650bool WebRtcSession::GetSslRole(const std::string& transport_name,
651 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700652 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000653 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
654 << "SSL Role of the session.";
655 return false;
656 }
657
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800658 return transport_controller_->GetSslRole(transport_name, role);
659}
660
661bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
662 rtc::SSLRole* role) {
663 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000664}
665
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000666void WebRtcSession::CreateOffer(
667 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700668 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
669 const cricket::MediaSessionOptions& session_options) {
670 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000671}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672
deadbeefab9b2d12015-10-14 11:33:11 -0700673void WebRtcSession::CreateAnswer(
674 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700675 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800676 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677}
678
679bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
680 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700681 ASSERT(signaling_thread()->IsCurrent());
682
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000683 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700684 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000685
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000686 // Validate SDP.
687 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
688 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 }
690
deadbeefd59daf82015-10-14 15:02:44 -0700691 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000692 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700694 initial_offerer_ = true;
695 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000698 cricket::SecurePolicy sdes_policy =
699 webrtc_session_desc_factory_->SdesPolicy();
700 cricket::CryptoType crypto_required = dtls_enabled_ ?
701 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
702 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000704 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000706 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
708 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000709 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 // TODO(mallinath) - Handle CreateChannel failure, as new local description
711 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000712 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
deadbeefcbecd352015-09-23 11:50:27 -0700715 // Remove unused channels if MediaContentDescription is rejected.
716 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000718 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 return false;
720 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000721
deadbeefd59daf82015-10-14 15:02:44 -0700722 if (remote_desc_) {
723 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700724 UseCandidatesInSessionDescription(remote_desc_.get());
725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
deadbeef0ed85b22016-02-23 17:24:52 -0800727 pending_ice_restarts_.clear();
728
deadbeefd59daf82015-10-14 15:02:44 -0700729 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000730 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732 return true;
733}
734
735bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
736 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700737 ASSERT(signaling_thread()->IsCurrent());
738
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000739 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700740 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000741
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000742 // Validate SDP.
743 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
744 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
746
kwibergd1fe2812016-04-27 06:47:29 -0700747 std::unique_ptr<SessionDescriptionInterface> old_remote_desc(
deadbeefd59daf82015-10-14 15:02:44 -0700748 remote_desc_.release());
749 remote_desc_.reset(desc_temp.release());
750
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000752 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 if (action == kOffer && !CreateChannels(desc->description())) {
754 // TODO(mallinath) - Handle CreateChannel failure, as new local description
755 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000756 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758
deadbeefcbecd352015-09-23 11:50:27 -0700759 // Remove unused channels if MediaContentDescription is rejected.
760 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
762 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
763 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000764 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 return false;
766 }
767
deadbeefd59daf82015-10-14 15:02:44 -0700768 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000769 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
771
deadbeef0ed85b22016-02-23 17:24:52 -0800772 if (old_remote_desc) {
773 for (const cricket::ContentInfo& content :
774 old_remote_desc->description()->contents()) {
775 // Check if this new SessionDescription contains new ICE ufrag and
776 // password that indicates the remote peer requests an ICE restart.
777 // TODO(deadbeef): When we start storing both the current and pending
778 // remote description, this should reset pending_ice_restarts and compare
779 // against the current description.
780 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
781 if (action == kOffer) {
782 pending_ice_restarts_.insert(content.name);
783 }
784 } else {
785 // We retain all received candidates only if ICE is not restarted.
786 // When ICE is restarted, all previous candidates belong to an old
787 // generation and should not be kept.
788 // TODO(deadbeef): This goes against the W3C spec which says the remote
789 // description should only contain candidates from the last set remote
790 // description plus any candidates added since then. We should remove
791 // this once we're sure it won't break anything.
792 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
793 old_remote_desc.get(), content.name, desc);
794 }
795 }
honghaiz503726c2015-07-31 12:37:38 -0700796 }
797
deadbeefd59daf82015-10-14 15:02:44 -0700798 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000799 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000801
802 // Set the the ICE connection state to connecting since the connection may
803 // become writable with peer reflexive candidates before any remote candidate
804 // is signaled.
805 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
806 // is to have a new signal the indicates a change in checking state from the
807 // transport and expose a new checking() member from transport that can be
808 // read to determine the current checking state. The existing SignalConnecting
809 // actually means "gathering candidates", so cannot be be used here.
810 if (desc->type() != SessionDescriptionInterface::kOffer &&
811 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
812 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
813 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 return true;
815}
816
deadbeefd59daf82015-10-14 15:02:44 -0700817void WebRtcSession::LogState(State old_state, State new_state) {
818 LOG(LS_INFO) << "Session:" << id()
819 << " Old state:" << GetStateString(old_state)
820 << " New state:" << GetStateString(new_state);
821}
822
823void WebRtcSession::SetState(State state) {
824 ASSERT(signaling_thread_->IsCurrent());
825 if (state != state_) {
826 LogState(state_, state);
827 state_ = state;
828 SignalState(this, state_);
829 }
830}
831
832void WebRtcSession::SetError(Error error, const std::string& error_desc) {
833 ASSERT(signaling_thread_->IsCurrent());
834 if (error != error_) {
835 error_ = error;
836 error_desc_ = error_desc;
837 }
838}
839
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840bool WebRtcSession::UpdateSessionState(
841 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700843 ASSERT(signaling_thread()->IsCurrent());
844
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 // If there's already a pending error then no state transition should happen.
846 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700847 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000848 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000850 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
851 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
deadbeefd59daf82015-10-14 15:02:44 -0700853 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
854 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000855 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700856 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000857 }
deadbeefd59daf82015-10-14 15:02:44 -0700858 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000859 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 }
861 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000862 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
863 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
865 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700866 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
867 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000868 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700869 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000870 }
deadbeefd59daf82015-10-14 15:02:44 -0700871 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000872 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 }
874 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700875 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700876 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700877 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700878 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700879 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800880 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700881 const cricket::ContentGroup* answer_bundle =
882 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
883 if (!EnableBundle(*answer_bundle)) {
884 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
885 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
886 }
887 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800888 // Only push down the transport description after enabling BUNDLE; we don't
889 // want to push down a description on a transport about to be destroyed.
890 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
891 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
892 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700894 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000895 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700896 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000897 }
deadbeefd59daf82015-10-14 15:02:44 -0700898 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000899 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901 }
902 return true;
903}
904
905WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
906 if (type == SessionDescriptionInterface::kOffer) {
907 return WebRtcSession::kOffer;
908 } else if (type == SessionDescriptionInterface::kPrAnswer) {
909 return WebRtcSession::kPrAnswer;
910 } else if (type == SessionDescriptionInterface::kAnswer) {
911 return WebRtcSession::kAnswer;
912 }
913 ASSERT(false && "unknown action type");
914 return WebRtcSession::kOffer;
915}
916
deadbeefd59daf82015-10-14 15:02:44 -0700917bool WebRtcSession::PushdownMediaDescription(
918 cricket::ContentAction action,
919 cricket::ContentSource source,
920 std::string* err) {
921 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
922 if (!ch) {
923 return true;
924 } else if (source == cricket::CS_LOCAL) {
925 return ch->PushdownLocalDescription(local_desc_->description(), action,
926 err);
927 } else {
928 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
929 err);
930 }
931 };
932
933 return (set_content(voice_channel()) &&
934 set_content(video_channel()) &&
935 set_content(data_channel()));
936}
937
938bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
939 cricket::ContentAction action,
940 std::string* error_desc) {
941 RTC_DCHECK(signaling_thread()->IsCurrent());
942
943 if (source == cricket::CS_LOCAL) {
944 return PushdownLocalTransportDescription(local_desc_->description(), action,
945 error_desc);
946 }
947 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
948 error_desc);
949}
950
951bool WebRtcSession::PushdownLocalTransportDescription(
952 const SessionDescription* sdesc,
953 cricket::ContentAction action,
954 std::string* err) {
955 RTC_DCHECK(signaling_thread()->IsCurrent());
956
957 if (!sdesc) {
958 return false;
959 }
960
961 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
962 if (!transport_controller_->SetLocalTransportDescription(
963 tinfo.content_name, tinfo.description, action, err)) {
964 return false;
965 }
966 }
967
968 return true;
969}
970
971bool WebRtcSession::PushdownRemoteTransportDescription(
972 const SessionDescription* sdesc,
973 cricket::ContentAction action,
974 std::string* err) {
975 RTC_DCHECK(signaling_thread()->IsCurrent());
976
977 if (!sdesc) {
978 return false;
979 }
980
981 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
982 if (!transport_controller_->SetRemoteTransportDescription(
983 tinfo.content_name, tinfo.description, action, err)) {
984 return false;
985 }
986 }
987
988 return true;
989}
990
991bool WebRtcSession::GetTransportDescription(
992 const SessionDescription* description,
993 const std::string& content_name,
994 cricket::TransportDescription* tdesc) {
995 if (!description || !tdesc) {
996 return false;
997 }
998 const TransportInfo* transport_info =
999 description->GetTransportInfoByName(content_name);
1000 if (!transport_info) {
1001 return false;
1002 }
1003 *tdesc = transport_info->description;
1004 return true;
1005}
1006
1007bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001008 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001009 return (GetChannelTransportStats(voice_channel(), stats) &&
1010 GetChannelTransportStats(video_channel(), stats) &&
1011 GetChannelTransportStats(data_channel(), stats));
1012}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001013
deadbeefcbecd352015-09-23 11:50:27 -07001014bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001015 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001016 ASSERT(signaling_thread()->IsCurrent());
1017 if (!ch) {
1018 // Not using this channel.
1019 return true;
1020 }
1021
1022 const std::string& content_name = ch->content_name();
1023 const std::string& transport_name = ch->transport_name();
1024 stats->proxy_to_transport[content_name] = transport_name;
1025 if (stats->transport_stats.find(transport_name) !=
1026 stats->transport_stats.end()) {
1027 // Transport stats already done for this transport.
1028 return true;
1029 }
1030
1031 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001032 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001033 return false;
1034 }
1035
1036 stats->transport_stats[transport_name] = tstats;
1037 return true;
1038}
1039
1040bool WebRtcSession::GetLocalCertificate(
1041 const std::string& transport_name,
1042 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1043 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001044 return transport_controller_->GetLocalCertificate(transport_name,
1045 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001046}
1047
jbauch555604a2016-04-26 03:13:22 -07001048std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -07001049 const std::string& transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001050 ASSERT(signaling_thread()->IsCurrent());
kwibergb4d01c42016-04-06 05:15:06 -07001051 return transport_controller_->GetRemoteSSLCertificate(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -07001052}
1053
deadbeefcbecd352015-09-23 11:50:27 -07001054bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1055 const std::string* first_content_name = bundle.FirstContentName();
1056 if (!first_content_name) {
1057 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1058 return false;
1059 }
1060 const std::string& transport_name = *first_content_name;
1061 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1062
zhihuang34b54c32016-08-04 11:06:50 -07001063#ifdef HAVE_QUIC
1064 if (quic_data_transport_ &&
1065 bundle.HasContentName(quic_data_transport_->content_name()) &&
1066 quic_data_transport_->transport_name() != transport_name) {
1067 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1068 << " on " << transport_name << "with QUIC.";
1069 }
1070#endif
1071
deadbeefcbecd352015-09-23 11:50:27 -07001072 auto maybe_set_transport = [this, bundle, transport_name,
1073 first_channel](cricket::BaseChannel* ch) {
1074 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001075 return true;
1076 }
1077
deadbeefcbecd352015-09-23 11:50:27 -07001078 if (ch->transport_name() == transport_name) {
1079 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1080 << " on " << transport_name << ".";
1081 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001082 }
torbjornga81a42f2015-09-23 02:16:58 -07001083
deadbeefcbecd352015-09-23 11:50:27 -07001084 if (!ch->SetTransport(transport_name)) {
1085 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1086 return false;
1087 }
1088 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1089 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001090 return true;
1091 };
1092
deadbeefcbecd352015-09-23 11:50:27 -07001093 if (!maybe_set_transport(voice_channel()) ||
1094 !maybe_set_transport(video_channel()) ||
1095 !maybe_set_transport(data_channel())) {
1096 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001097 }
deadbeefcbecd352015-09-23 11:50:27 -07001098
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001099 return true;
1100}
1101
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001103 if (!remote_desc_) {
1104 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1105 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001106 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 }
1108
1109 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001110 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 return false;
1112 }
1113
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001114 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001115 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1116 if (!valid) {
1117 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 }
1119
1120 // Add this candidate to the remote session description.
1121 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001122 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 return false;
1124 }
1125
deadbeefd59daf82015-10-14 15:02:44 -07001126 if (ready) {
1127 return UseCandidate(candidate);
1128 } else {
1129 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1130 return true;
1131 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132}
1133
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001134bool WebRtcSession::RemoveRemoteIceCandidates(
1135 const std::vector<cricket::Candidate>& candidates) {
1136 if (!remote_desc_) {
1137 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1138 << "removed without any remote session description.";
1139 return false;
1140 }
1141
1142 if (candidates.empty()) {
1143 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1144 return false;
1145 }
1146
1147 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1148 if (number_removed != candidates.size()) {
1149 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1150 << "Requested " << candidates.size() << " but only "
1151 << number_removed << " are removed.";
1152 }
1153
1154 // Remove the candidates from the transport controller.
1155 std::string error;
1156 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1157 if (!res && !error.empty()) {
1158 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1159 }
1160 return true;
1161}
1162
deadbeefd59daf82015-10-14 15:02:44 -07001163cricket::IceConfig WebRtcSession::ParseIceConfig(
1164 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001165 cricket::ContinualGatheringPolicy gathering_policy;
1166 // TODO(honghaiz): Add the third continual gathering policy in
1167 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1168 switch (config.continual_gathering_policy) {
1169 case PeerConnectionInterface::GATHER_ONCE:
1170 gathering_policy = cricket::GATHER_ONCE;
1171 break;
1172 case PeerConnectionInterface::GATHER_CONTINUALLY:
1173 gathering_policy = cricket::GATHER_CONTINUALLY;
1174 break;
1175 default:
1176 RTC_DCHECK(false);
1177 gathering_policy = cricket::GATHER_ONCE;
1178 }
deadbeefd59daf82015-10-14 15:02:44 -07001179 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001180 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001181 ice_config.prioritize_most_likely_candidate_pairs =
1182 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001183 ice_config.backup_connection_ping_interval =
1184 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001185 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001186 ice_config.presume_writable_when_fully_relayed =
1187 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001188 return ice_config;
1189}
1190
1191void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1192 transport_controller_->SetIceConfig(config);
1193}
1194
1195void WebRtcSession::MaybeStartGathering() {
1196 transport_controller_->MaybeStartGathering();
1197}
1198
Peter Boström0c4e06b2015-10-07 12:23:21 +02001199bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1200 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001201 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001203 }
1204 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205}
1206
Peter Boström0c4e06b2015-10-07 12:23:21 +02001207bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1208 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001209 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001210 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001211 }
1212 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001217 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 return desc.str();
1219}
1220
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1222 ASSERT(signaling_thread()->IsCurrent());
1223 if (!voice_channel_) {
1224 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1225 return false;
1226 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001227 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1229 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001230 if (!local_desc_ ||
1231 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 &send_ssrc)) {
1233 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1234 return false;
1235 }
1236 return voice_channel_->CanInsertDtmf();
1237}
1238
1239bool WebRtcSession::InsertDtmf(const std::string& track_id,
1240 int code, int duration) {
1241 ASSERT(signaling_thread()->IsCurrent());
1242 if (!voice_channel_) {
1243 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1244 return false;
1245 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001246 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001247 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1248 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1250 return false;
1251 }
solenberg1d63dd02015-12-02 12:35:09 -08001252 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1254 return false;
1255 }
1256 return true;
1257}
1258
1259sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001260 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261}
1262
wu@webrtc.org78187522013-10-07 23:32:02 +00001263bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001264 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001265 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001266 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001267 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1268 return false;
1269 }
1270 return data_channel_->SendData(params, payload, result);
1271}
1272
1273bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001274 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001275 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1276 return false;
1277 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001278 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1279 &DataChannel::OnChannelReady);
1280 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1281 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001282 data_channel_->SignalStreamClosedRemotely.connect(
1283 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001284 return true;
1285}
1286
1287void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001288 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001289 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1290 return;
1291 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001292 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1293 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001294 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001295}
1296
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001297void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001298 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001299 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1300 return;
1301 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001302 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1303 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001304}
1305
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001306void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001307 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001308 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1309 << "NULL.";
1310 return;
1311 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001312 data_channel_->RemoveRecvStream(sid);
1313 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001314}
1315
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001316bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001317 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001318}
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320cricket::DataChannelType WebRtcSession::data_channel_type() const {
1321 return data_channel_type_;
1322}
1323
deadbeef0ed85b22016-02-23 17:24:52 -08001324bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1325 return pending_ice_restarts_.find(content_name) !=
1326 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001327}
1328
Henrik Boströmd8281982015-08-27 10:12:24 +02001329void WebRtcSession::OnCertificateReady(
1330 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001331 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001332}
1333
Henrik Boströmd8281982015-08-27 10:12:24 +02001334bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001335 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001336}
1337
deadbeefcbecd352015-09-23 11:50:27 -07001338const rtc::scoped_refptr<rtc::RTCCertificate>&
1339WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001340 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001341}
1342
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343void WebRtcSession::SetIceConnectionState(
1344 PeerConnectionInterface::IceConnectionState state) {
1345 if (ice_connection_state_ == state) {
1346 return;
1347 }
1348
deadbeefcbecd352015-09-23 11:50:27 -07001349 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1350 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001351 RTC_DCHECK(ice_connection_state_ !=
1352 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 ice_connection_state_ = state;
1354 if (ice_observer_) {
1355 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1356 }
1357}
1358
deadbeefcbecd352015-09-23 11:50:27 -07001359void WebRtcSession::OnTransportControllerConnectionState(
1360 cricket::IceConnectionState state) {
1361 switch (state) {
1362 case cricket::kIceConnectionConnecting:
1363 // If the current state is Connected or Completed, then there were
1364 // writable channels but now there are not, so the next state must
1365 // be Disconnected.
1366 // kIceConnectionConnecting is currently used as the default,
1367 // un-connected state by the TransportController, so its only use is
1368 // detecting disconnections.
1369 if (ice_connection_state_ ==
1370 PeerConnectionInterface::kIceConnectionConnected ||
1371 ice_connection_state_ ==
1372 PeerConnectionInterface::kIceConnectionCompleted) {
1373 SetIceConnectionState(
1374 PeerConnectionInterface::kIceConnectionDisconnected);
1375 }
torbjornga81a42f2015-09-23 02:16:58 -07001376 break;
deadbeefcbecd352015-09-23 11:50:27 -07001377 case cricket::kIceConnectionFailed:
1378 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1379 break;
1380 case cricket::kIceConnectionConnected:
1381 LOG(LS_INFO) << "Changing to ICE connected state because "
1382 << "all transports are writable.";
1383 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1384 break;
1385 case cricket::kIceConnectionCompleted:
1386 LOG(LS_INFO) << "Changing to ICE completed state because "
1387 << "all transports are complete.";
1388 if (ice_connection_state_ !=
1389 PeerConnectionInterface::kIceConnectionConnected) {
1390 // If jumping directly from "checking" to "connected",
1391 // signal "connected" first.
1392 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1393 }
1394 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1395 if (metrics_observer_) {
1396 ReportTransportStats();
1397 }
1398 break;
1399 default:
1400 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001401 }
deadbeefcbecd352015-09-23 11:50:27 -07001402}
1403
1404void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001405 SetIceConnectionReceiving(receiving);
1406}
1407
1408void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1409 if (ice_connection_receiving_ == receiving) {
1410 return;
1411 }
1412 ice_connection_receiving_ = receiving;
1413 if (ice_observer_) {
1414 ice_observer_->OnIceConnectionReceivingChange(receiving);
1415 }
1416}
1417
deadbeefcbecd352015-09-23 11:50:27 -07001418void WebRtcSession::OnTransportControllerCandidatesGathered(
1419 const std::string& transport_name,
1420 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001422 int sdp_mline_index;
1423 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1424 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1425 << transport_name << " not found";
1426 return;
1427 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428
deadbeefcbecd352015-09-23 11:50:27 -07001429 for (cricket::Candidates::const_iterator citer = candidates.begin();
1430 citer != candidates.end(); ++citer) {
1431 // Use transport_name as the candidate media id.
1432 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1433 if (ice_observer_) {
1434 ice_observer_->OnIceCandidate(&candidate);
1435 }
1436 if (local_desc_) {
1437 local_desc_->AddCandidate(&candidate);
1438 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 }
1440}
1441
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001442void WebRtcSession::OnTransportControllerCandidatesRemoved(
1443 const std::vector<cricket::Candidate>& candidates) {
1444 ASSERT(signaling_thread()->IsCurrent());
1445 // Sanity check.
1446 for (const cricket::Candidate& candidate : candidates) {
1447 if (candidate.transport_name().empty()) {
1448 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1449 << "empty content name in candidate "
1450 << candidate.ToString();
1451 return;
1452 }
1453 }
1454
1455 if (local_desc_) {
1456 local_desc_->RemoveCandidates(candidates);
1457 }
1458 if (ice_observer_) {
1459 ice_observer_->OnIceCandidatesRemoved(candidates);
1460 }
1461}
1462
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463// Enabling voice and video channel.
1464void WebRtcSession::EnableChannels() {
1465 if (voice_channel_ && !voice_channel_->enabled())
1466 voice_channel_->Enable(true);
1467
1468 if (video_channel_ && !video_channel_->enabled())
1469 video_channel_->Enable(true);
1470
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001471 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 data_channel_->Enable(true);
1473}
1474
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475// Returns the media index for a local ice candidate given the content name.
1476bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1477 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001478 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001480 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481
1482 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001483 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 for (size_t index = 0; index < contents.size(); ++index) {
1485 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001486 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 content_found = true;
1488 break;
1489 }
1490 }
1491 return content_found;
1492}
1493
1494bool WebRtcSession::UseCandidatesInSessionDescription(
1495 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001496 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001500
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1502 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001503 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001504 const IceCandidateInterface* candidate = candidates->at(n);
1505 bool valid = false;
1506 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1507 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001508 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1509 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001510 }
1511 continue;
1512 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001513 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001514 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 break;
deadbeefd59daf82015-10-14 15:02:44 -07001516 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 }
1518 }
1519 return ret;
1520}
1521
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001522bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001524 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001526 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 return false;
1528 }
1529
1530 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001531 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 std::vector<cricket::Candidate> candidates;
1533 candidates.push_back(candidate->candidate());
1534 // Invoking BaseSession method to handle remote candidates.
1535 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001536 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1537 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 // Candidates successfully submitted for checking.
1539 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1540 ice_connection_state_ ==
1541 PeerConnectionInterface::kIceConnectionDisconnected) {
1542 // If state is New, then the session has just gotten its first remote ICE
1543 // candidates, so go to Checking.
1544 // If state is Disconnected, the session is re-using old candidates or
1545 // receiving additional ones, so go to Checking.
1546 // If state is Connected, stay Connected.
1547 // TODO(bemasc): If state is Connected, and the new candidates are for a
1548 // newly added transport, then the state actually _should_ move to
1549 // checking. Add a way to distinguish that case.
1550 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1551 }
1552 // TODO(bemasc): If state is Completed, go back to Connected.
1553 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001554 if (!error.empty()) {
1555 LOG(LS_WARNING) << error;
1556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 }
1558 return true;
1559}
1560
deadbeefcbecd352015-09-23 11:50:27 -07001561void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001562 // Destroy video_channel_ first since it may have a pointer to the
1563 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 const cricket::ContentInfo* video_info =
1565 cricket::GetFirstVideoContent(desc);
1566 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 }
1570
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001571 const cricket::ContentInfo* voice_info =
1572 cricket::GetFirstAudioContent(desc);
1573 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001574 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001575 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001576 }
1577
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 const cricket::ContentInfo* data_info =
1579 cricket::GetFirstDataContent(desc);
zhihuang34b54c32016-08-04 11:06:50 -07001580 if (!data_info || data_info->rejected) {
1581 if (data_channel_) {
1582 SignalDataChannelDestroyed();
1583 channel_manager_->DestroyDataChannel(data_channel_.release());
1584 }
1585#ifdef HAVE_QUIC
1586 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1587 if (quic_data_transport_) {
1588 quic_data_transport_.reset();
1589 }
1590#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 }
1592}
1593
skvlad6c87a672016-05-17 17:49:52 -07001594// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1595// if the channel is not part of any bundle.
1596const std::string* WebRtcSession::GetBundleTransportName(
1597 const cricket::ContentInfo* content,
1598 const cricket::ContentGroup* bundle) {
1599 if (!bundle) {
1600 return nullptr;
1601 }
1602 const std::string* first_content_name = bundle->FirstContentName();
1603 if (!first_content_name) {
1604 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1605 return nullptr;
1606 }
1607 if (!bundle->HasContentName(content->name)) {
1608 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1609 return nullptr;
1610 }
1611 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1612 return first_content_name;
1613}
1614
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001616 const cricket::ContentGroup* bundle_group = nullptr;
1617 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1618 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1619 if (!bundle_group) {
1620 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1621 return false;
1622 }
1623 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 // Creating the media channels and transport proxies.
1625 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1626 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001627 if (!CreateVoiceChannel(voice,
1628 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 LOG(LS_ERROR) << "Failed to create voice channel.";
1630 return false;
1631 }
1632 }
1633
1634 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1635 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001636 if (!CreateVideoChannel(video,
1637 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 LOG(LS_ERROR) << "Failed to create video channel.";
1639 return false;
1640 }
1641 }
1642
1643 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1644 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001645 data && !data->rejected && !data_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001646 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 LOG(LS_ERROR) << "Failed to create data channel.";
1648 return false;
1649 }
1650 }
1651
1652 return true;
1653}
1654
skvlad6c87a672016-05-17 17:49:52 -07001655bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1656 const std::string* bundle_transport) {
1657 bool require_rtcp_mux =
1658 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1659 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001661 media_controller_, transport_controller_.get(), content->name,
1662 bundle_transport, create_rtcp_transport_channel, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001663 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001664 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001665 }
skvlad6c87a672016-05-17 17:49:52 -07001666 if (require_rtcp_mux) {
1667 voice_channel_->ActivateRtcpMux();
1668 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001669
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001670 voice_channel_->SignalDtlsSetupFailure.connect(
1671 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001672
1673 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001674 voice_channel_->SignalSentPacket.connect(this,
1675 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001676 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677}
1678
skvlad6c87a672016-05-17 17:49:52 -07001679bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1680 const std::string* bundle_transport) {
1681 bool require_rtcp_mux =
1682 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1683 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001685 media_controller_, transport_controller_.get(), content->name,
1686 bundle_transport, create_rtcp_transport_channel, video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001687 if (!video_channel_) {
1688 return false;
1689 }
skvlad6c87a672016-05-17 17:49:52 -07001690 if (require_rtcp_mux) {
1691 video_channel_->ActivateRtcpMux();
1692 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001693 video_channel_->SignalDtlsSetupFailure.connect(
1694 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001695
1696 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001697 video_channel_->SignalSentPacket.connect(this,
1698 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001699 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700}
1701
skvlad6c87a672016-05-17 17:49:52 -07001702bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1703 const std::string* bundle_transport) {
zhihuang34b54c32016-08-04 11:06:50 -07001704#ifdef HAVE_QUIC
1705 if (data_channel_type_ == cricket::DCT_QUIC) {
1706 RTC_DCHECK(transport_controller_->quic());
1707 const std::string transport_name =
1708 bundle_transport ? *bundle_transport : content->name;
1709 quic_data_transport_->SetTransport(transport_name);
1710 return true;
1711 }
1712#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001713 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
skvlad6c87a672016-05-17 17:49:52 -07001714 bool require_rtcp_mux =
1715 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1716 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 data_channel_.reset(channel_manager_->CreateDataChannel(
skvlad6c87a672016-05-17 17:49:52 -07001718 transport_controller_.get(), content->name, bundle_transport,
1719 create_rtcp_transport_channel, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001720 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001721 return false;
1722 }
skvlad6c87a672016-05-17 17:49:52 -07001723 if (require_rtcp_mux) {
1724 data_channel_->ActivateRtcpMux();
1725 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001726
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001727 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001728 data_channel_->SignalDataReceived.connect(
1729 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001730 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001731
1732 data_channel_->SignalDtlsSetupFailure.connect(
1733 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001734
1735 SignalDataChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001736 data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001737 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738}
1739
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001740void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001741 SetError(ERROR_TRANSPORT,
1742 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743}
1744
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001745void WebRtcSession::OnDataChannelMessageReceived(
1746 cricket::DataChannel* channel,
1747 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001748 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001749 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1750 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1751 // Received OPEN message; parse and signal that a new data channel should
1752 // be created.
1753 std::string label;
1754 InternalDataChannelInit config;
1755 config.id = params.ssrc;
1756 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1757 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1758 << params.ssrc;
1759 return;
1760 }
1761 config.open_handshake_role = InternalDataChannelInit::kAcker;
1762 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 }
deadbeefab9b2d12015-10-14 11:33:11 -07001764 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765}
1766
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001767// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001768bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001769 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1770 if (!bundle_enabled)
1771 return true;
1772
1773 const cricket::ContentGroup* bundle_group =
1774 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1775 ASSERT(bundle_group != NULL);
1776
1777 const cricket::ContentInfos& contents = desc->contents();
1778 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1779 citer != contents.end(); ++citer) {
1780 const cricket::ContentInfo* content = (&*citer);
1781 ASSERT(content != NULL);
1782 if (bundle_group->HasContentName(content->name) &&
1783 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1784 if (!HasRtcpMuxEnabled(content))
1785 return false;
1786 }
1787 }
1788 // RTCP-MUX is enabled in all the contents.
1789 return true;
1790}
1791
1792bool WebRtcSession::HasRtcpMuxEnabled(
1793 const cricket::ContentInfo* content) {
1794 const cricket::MediaContentDescription* description =
1795 static_cast<cricket::MediaContentDescription*>(content->description);
1796 return description->rtcp_mux();
1797}
1798
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001799bool WebRtcSession::ValidateSessionDescription(
1800 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001801 cricket::ContentSource source, std::string* err_desc) {
1802 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001803 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001804 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001805 }
1806
1807 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001808 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001809 }
1810
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001811 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001812 Action action = GetAction(sdesc->type());
1813 if (source == cricket::CS_LOCAL) {
1814 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001815 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001816 } else {
1817 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001818 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001819 }
1820
1821 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001822 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001823 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1824 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001825 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001826 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001827 }
1828
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001829 // Verify ice-ufrag and ice-pwd.
1830 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001831 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001832 }
1833
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001834 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001835 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001836 }
1837
skvlad6c87a672016-05-17 17:49:52 -07001838 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
1839 // m-lines that do not rtcp-mux enabled.
1840
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001841 // Verify m-lines in Answer when compared against Offer.
1842 if (action == kAnswer) {
1843 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001844 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1845 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001846 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001847 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001848 }
1849 }
1850
1851 return true;
1852}
1853
1854bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1855 return ((action == kOffer && state() == STATE_INIT) ||
1856 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001857 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001858 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001859 (action == kOffer && state() == STATE_INPROGRESS) ||
1860 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001861 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1862 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1863 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1864 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001865}
1866
1867bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1868 return ((action == kOffer && state() == STATE_INIT) ||
1869 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001870 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001871 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001872 (action == kOffer && state() == STATE_INPROGRESS) ||
1873 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001874 (action == kAnswer && state() == STATE_SENTOFFER) ||
1875 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1876 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1877 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001878}
1879
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001880std::string WebRtcSession::GetSessionErrorMsg() {
1881 std::ostringstream desc;
1882 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1883 desc << kSessionErrorDesc << error_desc() << ".";
1884 return desc.str();
1885}
1886
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001887// We need to check the local/remote description for the Transport instead of
1888// the session, because a new Transport added during renegotiation may have
1889// them unset while the session has them set from the previous negotiation.
1890// Not doing so may trigger the auto generation of transport description and
1891// mess up DTLS identity information, ICE credential, etc.
1892bool WebRtcSession::ReadyToUseRemoteCandidate(
1893 const IceCandidateInterface* candidate,
1894 const SessionDescriptionInterface* remote_desc,
1895 bool* valid) {
zhihuang34b54c32016-08-04 11:06:50 -07001896 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001897
1898 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001899 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001900
deadbeefd59daf82015-10-14 15:02:44 -07001901 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001902 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001903 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001904
1905 size_t mediacontent_index =
1906 static_cast<size_t>(candidate->sdp_mline_index());
1907 size_t remote_content_size =
1908 current_remote_desc->description()->contents().size();
1909 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001910 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
1911 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001912
1913 *valid = false;
1914 return false;
1915 }
1916
1917 cricket::ContentInfo content =
1918 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang34b54c32016-08-04 11:06:50 -07001919
1920 const std::string transport_name = GetTransportName(content.name);
1921 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07001922 return false;
1923 }
zhihuang34b54c32016-08-04 11:06:50 -07001924 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001925}
1926
deadbeefcbecd352015-09-23 11:50:27 -07001927void WebRtcSession::OnTransportControllerGatheringState(
1928 cricket::IceGatheringState state) {
1929 ASSERT(signaling_thread()->IsCurrent());
1930 if (state == cricket::kIceGatheringGathering) {
1931 if (ice_observer_) {
1932 ice_observer_->OnIceGatheringChange(
1933 PeerConnectionInterface::kIceGatheringGathering);
1934 }
1935 } else if (state == cricket::kIceGatheringComplete) {
1936 if (ice_observer_) {
1937 ice_observer_->OnIceGatheringChange(
1938 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001939 }
1940 }
1941}
1942
1943void WebRtcSession::ReportTransportStats() {
1944 // Use a set so we don't report the same stats twice if two channels share
1945 // a transport.
1946 std::set<std::string> transport_names;
1947 if (voice_channel()) {
1948 transport_names.insert(voice_channel()->transport_name());
1949 }
1950 if (video_channel()) {
1951 transport_names.insert(video_channel()->transport_name());
1952 }
1953 if (data_channel()) {
1954 transport_names.insert(data_channel()->transport_name());
1955 }
1956 for (const auto& name : transport_names) {
1957 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07001958 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001959 ReportBestConnectionState(stats);
1960 ReportNegotiatedCiphers(stats);
1961 }
1962 }
1963}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001964// Walk through the ConnectionInfos to gather best connection usage
1965// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07001966void WebRtcSession::ReportBestConnectionState(
1967 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07001968 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001969 for (cricket::TransportChannelStatsList::const_iterator it =
1970 stats.channel_stats.begin();
1971 it != stats.channel_stats.end(); ++it) {
1972 for (cricket::ConnectionInfos::const_iterator it_info =
1973 it->connection_infos.begin();
1974 it_info != it->connection_infos.end(); ++it_info) {
1975 if (!it_info->best_connection) {
1976 continue;
1977 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001978
1979 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
1980 const cricket::Candidate& local = it_info->local_candidate;
1981 const cricket::Candidate& remote = it_info->remote_candidate;
1982
1983 // Increment the counter for IceCandidatePairType.
1984 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
1985 (local.type() == RELAY_PORT_TYPE &&
1986 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
1987 type = kEnumCounterIceCandidatePairTypeTcp;
1988 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
1989 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001990 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001991 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001992 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001993 metrics_observer_->IncrementEnumCounter(
1994 type, GetIceCandidatePairCounter(local, remote),
1995 kIceCandidatePairMax);
1996
1997 // Increment the counter for IP type.
1998 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001999 metrics_observer_->IncrementEnumCounter(
2000 kEnumCounterAddressFamily, kBestConnections_IPv4,
2001 kPeerConnectionAddressFamilyCounter_Max);
2002
2003 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002004 metrics_observer_->IncrementEnumCounter(
2005 kEnumCounterAddressFamily, kBestConnections_IPv6,
2006 kPeerConnectionAddressFamilyCounter_Max);
2007 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002008 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002009 }
2010
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002011 return;
2012 }
2013 }
2014}
2015
jbauchac8869e2015-07-03 01:36:14 -07002016void WebRtcSession::ReportNegotiatedCiphers(
2017 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002018 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002019 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2020 return;
2021 }
2022
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002023 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2024 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2025 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2026 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002027 return;
2028 }
2029
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002030 PeerConnectionEnumCounterType srtp_counter_type;
2031 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002032 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002033 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2034 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002035 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002036 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2037 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002038 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002039 srtp_counter_type = kEnumCounterDataSrtpCipher;
2040 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002041 } else {
2042 RTC_NOTREACHED();
2043 return;
2044 }
2045
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002046 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2047 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2048 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002049 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002050 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2051 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2052 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002053 }
2054}
2055
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002056void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002057 RTC_DCHECK(worker_thread()->IsCurrent());
2058 media_controller_->call_w()->OnSentPacket(sent_packet);
2059}
2060
zhihuang34b54c32016-08-04 11:06:50 -07002061const std::string WebRtcSession::GetTransportName(
2062 const std::string& content_name) {
2063 cricket::BaseChannel* channel = GetChannel(content_name);
2064 if (!channel) {
2065#ifdef HAVE_QUIC
2066 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2067 content_name == quic_data_transport_->transport_name()) {
2068 return quic_data_transport_->transport_name();
2069 }
2070#endif
2071 // Return an empty string if failed to retrieve the transport name.
2072 return "";
2073 }
2074 return channel->transport_name();
2075}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076} // namespace webrtc