blob: ea724a344cae856c45624a10ad5fc4d1f7b1cc1d [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
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_sink.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/jsepicecandidate.h"
22#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/peerconnectioninterface.h"
24#include "webrtc/api/sctputils.h"
25#include "webrtc/api/webrtcsessiondescriptionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000026#include "webrtc/base/basictypes.h"
zhihuang9763d562016-08-05 11:14: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"
ossuf515ab82016-12-07 04:52:58 -080033#include "webrtc/call/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
zhihuang9763d562016-08-05 11:14: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
deadbeef7af91dd2016-12-13 11:29:11 -0800168// |srtp_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
Peter Boström0c4e06b2015-10-07 12:23:21 +0200234static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
235 const std::string& track_id,
236 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 const cricket::ContentInfo* audio_info =
238 cricket::GetFirstAudioContent(session_description);
239 if (!audio_info) {
240 LOG(LS_ERROR) << "Audio not used in this call";
241 return false;
242 }
243
244 const cricket::MediaContentDescription* audio_content =
245 static_cast<const cricket::MediaContentDescription*>(
246 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000247 const cricket::StreamParams* stream =
248 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
249 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 return false;
251 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000252
253 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 return true;
255}
256
257static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 uint32_t ssrc,
259 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 ASSERT(track_id != NULL);
261
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 const cricket::ContentInfo* audio_info =
263 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000264 if (audio_info) {
265 const cricket::MediaContentDescription* audio_content =
266 static_cast<const cricket::MediaContentDescription*>(
267 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000269 const auto* found =
270 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
271 if (found) {
272 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000273 return true;
274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 }
276
277 const cricket::ContentInfo* video_info =
278 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000279 if (video_info) {
280 const cricket::MediaContentDescription* video_content =
281 static_cast<const cricket::MediaContentDescription*>(
282 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const auto* found =
285 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
286 if (found) {
287 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000288 return true;
289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 }
291 return false;
292}
293
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000294static bool BadSdp(const std::string& source,
295 const std::string& type,
296 const std::string& reason,
297 std::string* err_desc) {
298 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700299 desc << "Failed to set " << source;
300 if (!type.empty()) {
301 desc << " " << type;
302 }
303 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000304
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000306 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000308 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 return false;
310}
311
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000313 const std::string& type,
314 const std::string& reason,
315 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000317 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000319 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 }
321}
322
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000323static bool BadLocalSdp(const std::string& type,
324 const std::string& reason,
325 std::string* err_desc) {
326 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
327}
328
329static bool BadRemoteSdp(const std::string& type,
330 const std::string& reason,
331 std::string* err_desc) {
332 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
333}
334
335static bool BadOfferSdp(cricket::ContentSource source,
336 const std::string& reason,
337 std::string* err_desc) {
338 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
339}
340
341static bool BadPranswerSdp(cricket::ContentSource source,
342 const std::string& reason,
343 std::string* err_desc) {
344 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
345 reason, err_desc);
346}
347
348static bool BadAnswerSdp(cricket::ContentSource source,
349 const std::string& reason,
350 std::string* err_desc) {
351 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352}
353
deadbeefd59daf82015-10-14 15:02:44 -0700354#define GET_STRING_OF_STATE(state) \
355 case webrtc::WebRtcSession::state: \
356 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 break;
358
deadbeefd59daf82015-10-14 15:02:44 -0700359static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 std::string result;
361 switch (state) {
362 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700363 GET_STRING_OF_STATE(STATE_SENTOFFER)
364 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
365 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
366 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700368 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 default:
370 ASSERT(false);
371 break;
372 }
373 return result;
374}
375
deadbeefd59daf82015-10-14 15:02:44 -0700376#define GET_STRING_OF_ERROR_CODE(err) \
377 case webrtc::WebRtcSession::err: \
378 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 break;
380
deadbeefd59daf82015-10-14 15:02:44 -0700381static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 std::string result;
383 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000384 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000385 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
386 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 default:
deadbeefd59daf82015-10-14 15:02:44 -0700388 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 break;
390 }
391 return result;
392}
393
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000394static std::string MakeErrorString(const std::string& error,
395 const std::string& desc) {
396 std::ostringstream ret;
397 ret << error << " " << desc;
398 return ret.str();
399}
400
401static std::string MakeTdErrorString(const std::string& desc) {
402 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403}
404
deadbeef0ed85b22016-02-23 17:24:52 -0800405// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
406bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
407 const SessionDescriptionInterface* new_desc,
408 const std::string& content_name) {
409 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700410 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 }
deadbeef0ed85b22016-02-23 17:24:52 -0800412 const SessionDescription* new_sd = new_desc->description();
413 const SessionDescription* old_sd = old_desc->description();
414 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
415 if (!cinfo || cinfo->rejected) {
416 return false;
417 }
418 // If the content isn't rejected, check if ufrag and password has changed.
419 const cricket::TransportDescription* new_transport_desc =
420 new_sd->GetTransportDescriptionByName(content_name);
421 const cricket::TransportDescription* old_transport_desc =
422 old_sd->GetTransportDescriptionByName(content_name);
423 if (!new_transport_desc || !old_transport_desc) {
424 // No transport description exists. This is not an ICE restart.
425 return false;
426 }
427 if (cricket::IceCredentialsChanged(
428 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
429 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
430 LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
431 << ".";
432 return true;
433 }
434 return false;
435}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436
zhihuang29ff8442016-07-27 11:07:25 -0700437WebRtcSession::WebRtcSession(
438 webrtc::MediaControllerInterface* media_controller,
439 rtc::Thread* network_thread,
440 rtc::Thread* worker_thread,
441 rtc::Thread* signaling_thread,
442 cricket::PortAllocator* port_allocator,
443 std::unique_ptr<cricket::TransportController> transport_controller)
zhihuang9763d562016-08-05 11:14:50 -0700444 : network_thread_(network_thread),
445 worker_thread_(worker_thread),
danilchape9021a32016-05-17 01:52:02 -0700446 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 // RFC 3264: The numeric value of the session id and version in the
448 // o line MUST be representable with a "64 bit signed integer".
449 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700450 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
zhihuang29ff8442016-07-27 11:07:25 -0700451 transport_controller_(std::move(transport_controller)),
stefanc1aeaf02015-10-15 07:26:07 -0700452 media_controller_(media_controller),
453 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 ice_observer_(NULL),
455 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700456 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000458 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000460 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700461 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
462 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700463 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700464 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700465 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700466 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700467 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700468 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700469 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700470 transport_controller_->SignalCandidatesRemoved.connect(
471 this, &WebRtcSession::OnTransportControllerCandidatesRemoved);
zhihuangd82eee02016-08-26 11:25:05 -0700472 transport_controller_->SignalDtlsHandshakeError.connect(
473 this, &WebRtcSession::OnDtlsHandshakeError);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474}
475
476WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700477 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000478 // Destroy video_channel_ first since it may have a pointer to the
479 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000480 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 SignalVideoChannelDestroyed();
482 channel_manager_->DestroyVideoChannel(video_channel_.release());
483 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000484 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000485 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200486 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000487 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000488 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 SignalDataChannelDestroyed();
490 channel_manager_->DestroyDataChannel(data_channel_.release());
491 }
zhihuang9763d562016-08-05 11:14:50 -0700492#ifdef HAVE_QUIC
493 if (quic_data_transport_) {
494 quic_data_transport_.reset();
495 }
496#endif
deadbeef057ecf02016-01-20 14:30:43 -0800497 SignalDestroyed();
deadbeefd59daf82015-10-14 15:02:44 -0700498
499 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500}
501
wu@webrtc.org91053e72013-08-10 07:18:04 +0000502bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000503 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200504 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200505 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
506 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700507 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700508 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000509
Henrik Boström87713d02015-08-25 09:53:21 +0200510 // Obtain a certificate from RTCConfiguration if any were provided (optional).
511 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
512 if (!rtc_configuration.certificates.empty()) {
513 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
514 // just picking the first one. The decision should be made based on the DTLS
515 // handshake. The DTLS negotiations need to know about all certificates.
516 certificate = rtc_configuration.certificates[0];
517 }
518
honghaiz1f429e32015-09-28 07:57:34 -0700519 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700520
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000521 if (options.disable_encryption) {
522 dtls_enabled_ = false;
523 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200524 // Enable DTLS by default if we have an identity store or a certificate.
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200525 dtls_enabled_ = (cert_generator || certificate);
htaa2a49d92016-03-04 02:51:39 -0800526 // |rtc_configuration| can override the default |dtls_enabled_| value.
527 if (rtc_configuration.enable_dtls_srtp) {
528 dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000529 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000530 }
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000533 // It takes precendence over the disable_sctp_data_channels
534 // PeerConnectionFactoryInterface::Options.
htaa2a49d92016-03-04 02:51:39 -0800535 if (rtc_configuration.enable_rtp_data_channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 data_channel_type_ = cricket::DCT_RTP;
zhihuang9763d562016-08-05 11:14:50 -0700537 }
538#ifdef HAVE_QUIC
539 else if (rtc_configuration.enable_quic) {
540 // Use QUIC instead of DTLS when |enable_quic| is true.
541 data_channel_type_ = cricket::DCT_QUIC;
542 transport_controller_->use_quic();
543 if (dtls_enabled_) {
544 LOG(LS_INFO) << "Using QUIC instead of DTLS";
545 }
546 quic_data_transport_.reset(
547 new QuicDataTransport(signaling_thread(), worker_thread(),
548 network_thread(), transport_controller_.get()));
549 }
550#endif // HAVE_QUIC
551 else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000552 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000553 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000554 data_channel_type_ = cricket::DCT_SCTP;
555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
htaa2a49d92016-03-04 02:51:39 -0800558 video_options_.screencast_min_bitrate_kbps =
559 rtc_configuration.screencast_min_bitrate;
560 audio_options_.combined_audio_video_bwe =
561 rtc_configuration.combined_audio_video_bwe;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000562
kwiberg102c6a62015-10-30 02:47:38 -0700563 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100564 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200565
Karl Wibergbe579832015-11-10 22:34:18 +0100566 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
567 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200568
Henrik Boström87713d02015-08-25 09:53:21 +0200569 if (!dtls_enabled_) {
570 // Construct with DTLS disabled.
571 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200572 signaling_thread(), channel_manager_, this, id(),
573 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>()));
Henrik Boström87713d02015-08-25 09:53:21 +0200574 } else {
575 // Construct with DTLS enabled.
576 if (!certificate) {
Henrik Boström87713d02015-08-25 09:53:21 +0200577 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200578 signaling_thread(), channel_manager_, this, id(),
579 std::move(cert_generator)));
Henrik Boström87713d02015-08-25 09:53:21 +0200580 } else {
581 // Use the already generated certificate.
582 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200583 signaling_thread(), channel_manager_, this, id(), certificate));
Henrik Boström87713d02015-08-25 09:53:21 +0200584 }
585 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000586
Henrik Boströmd8281982015-08-27 10:12:24 +0200587 webrtc_session_desc_factory_->SignalCertificateReady.connect(
588 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000589
wu@webrtc.org97077a32013-10-25 21:18:33 +0000590 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000591 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000592 }
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 return true;
595}
596
deadbeefd59daf82015-10-14 15:02:44 -0700597void WebRtcSession::Close() {
598 SetState(STATE_CLOSED);
599 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000600 ASSERT(!voice_channel_);
601 ASSERT(!video_channel_);
602 ASSERT(!data_channel_);
solenberg03d6d572016-03-01 12:42:03 -0800603 media_controller_->Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604}
605
deadbeef0ed85b22016-02-23 17:24:52 -0800606cricket::BaseChannel* WebRtcSession::GetChannel(
607 const std::string& content_name) {
608 if (voice_channel() && voice_channel()->content_name() == content_name) {
609 return voice_channel();
610 }
611 if (video_channel() && video_channel()->content_name() == content_name) {
612 return video_channel();
613 }
614 if (data_channel() && data_channel()->content_name() == content_name) {
615 return data_channel();
616 }
617 return nullptr;
618}
619
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000620cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
621 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622}
623
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800624bool WebRtcSession::GetSslRole(const std::string& transport_name,
625 rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700626 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000627 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
628 << "SSL Role of the session.";
629 return false;
630 }
631
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800632 return transport_controller_->GetSslRole(transport_name, role);
633}
634
635bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel,
636 rtc::SSLRole* role) {
637 return channel && GetSslRole(channel->transport_name(), role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000638}
639
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000640void WebRtcSession::CreateOffer(
641 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700642 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
643 const cricket::MediaSessionOptions& session_options) {
644 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000645}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646
deadbeefab9b2d12015-10-14 11:33:11 -0700647void WebRtcSession::CreateAnswer(
648 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700649 const cricket::MediaSessionOptions& session_options) {
htaa2a49d92016-03-04 02:51:39 -0800650 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651}
652
653bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
654 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700655 ASSERT(signaling_thread()->IsCurrent());
656
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000657 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700658 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000659
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000660 // Validate SDP.
661 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
662 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 }
664
deadbeefd59daf82015-10-14 15:02:44 -0700665 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000666 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700668 initial_offerer_ = true;
669 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 }
671
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000672 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673
674 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000675 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 // TODO(mallinath) - Handle CreateChannel failure, as new local description
677 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000678 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 }
680
deadbeefcbecd352015-09-23 11:50:27 -0700681 // Remove unused channels if MediaContentDescription is rejected.
682 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000684 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 return false;
686 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000687
deadbeefd59daf82015-10-14 15:02:44 -0700688 if (remote_desc_) {
689 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700690 UseCandidatesInSessionDescription(remote_desc_.get());
691 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692
deadbeef0ed85b22016-02-23 17:24:52 -0800693 pending_ice_restarts_.clear();
694
deadbeefd59daf82015-10-14 15:02:44 -0700695 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000696 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698 return true;
699}
700
701bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
702 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700703 ASSERT(signaling_thread()->IsCurrent());
704
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000705 // Takes the ownership of |desc| regardless of the result.
kwibergd1fe2812016-04-27 06:47:29 -0700706 std::unique_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000707
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000708 // Validate SDP.
709 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
710 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
kwibergd1fe2812016-04-27 06:47:29 -0700713 std::unique_ptr<SessionDescriptionInterface> old_remote_desc(
deadbeefd59daf82015-10-14 15:02:44 -0700714 remote_desc_.release());
715 remote_desc_.reset(desc_temp.release());
716
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000718 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (action == kOffer && !CreateChannels(desc->description())) {
720 // TODO(mallinath) - Handle CreateChannel failure, as new local description
721 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000722 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 }
724
deadbeefcbecd352015-09-23 11:50:27 -0700725 // Remove unused channels if MediaContentDescription is rejected.
726 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727
728 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
729 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000730 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 return false;
732 }
733
deadbeefd59daf82015-10-14 15:02:44 -0700734 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000735 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 }
737
deadbeef0ed85b22016-02-23 17:24:52 -0800738 if (old_remote_desc) {
739 for (const cricket::ContentInfo& content :
740 old_remote_desc->description()->contents()) {
741 // Check if this new SessionDescription contains new ICE ufrag and
742 // password that indicates the remote peer requests an ICE restart.
743 // TODO(deadbeef): When we start storing both the current and pending
744 // remote description, this should reset pending_ice_restarts and compare
745 // against the current description.
746 if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) {
747 if (action == kOffer) {
748 pending_ice_restarts_.insert(content.name);
749 }
750 } else {
751 // We retain all received candidates only if ICE is not restarted.
752 // When ICE is restarted, all previous candidates belong to an old
753 // generation and should not be kept.
754 // TODO(deadbeef): This goes against the W3C spec which says the remote
755 // description should only contain candidates from the last set remote
756 // description plus any candidates added since then. We should remove
757 // this once we're sure it won't break anything.
758 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
759 old_remote_desc.get(), content.name, desc);
760 }
761 }
honghaiz503726c2015-07-31 12:37:38 -0700762 }
763
deadbeefd59daf82015-10-14 15:02:44 -0700764 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000765 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000767
768 // Set the the ICE connection state to connecting since the connection may
769 // become writable with peer reflexive candidates before any remote candidate
770 // is signaled.
771 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
772 // is to have a new signal the indicates a change in checking state from the
773 // transport and expose a new checking() member from transport that can be
774 // read to determine the current checking state. The existing SignalConnecting
775 // actually means "gathering candidates", so cannot be be used here.
776 if (desc->type() != SessionDescriptionInterface::kOffer &&
777 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
778 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
779 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 return true;
781}
782
deadbeefd59daf82015-10-14 15:02:44 -0700783void WebRtcSession::LogState(State old_state, State new_state) {
784 LOG(LS_INFO) << "Session:" << id()
785 << " Old state:" << GetStateString(old_state)
786 << " New state:" << GetStateString(new_state);
787}
788
789void WebRtcSession::SetState(State state) {
790 ASSERT(signaling_thread_->IsCurrent());
791 if (state != state_) {
792 LogState(state_, state);
793 state_ = state;
794 SignalState(this, state_);
795 }
796}
797
798void WebRtcSession::SetError(Error error, const std::string& error_desc) {
799 ASSERT(signaling_thread_->IsCurrent());
800 if (error != error_) {
801 error_ = error;
802 error_desc_ = error_desc;
803 }
804}
805
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806bool WebRtcSession::UpdateSessionState(
807 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700809 ASSERT(signaling_thread()->IsCurrent());
810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 // If there's already a pending error then no state transition should happen.
812 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700813 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000814 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000816 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
817 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 }
deadbeefd59daf82015-10-14 15:02:44 -0700819 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
820 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000821 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700822 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000823 }
deadbeefd59daf82015-10-14 15:02:44 -0700824 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000825 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000828 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
829 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700832 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
833 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000834 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700835 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000836 }
deadbeefd59daf82015-10-14 15:02:44 -0700837 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840 } else if (action == kAnswer) {
deadbeefcbecd352015-09-23 11:50:27 -0700841 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700842 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700843 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700844 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700845 if (local_bundle && remote_bundle) {
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800846 // The answerer decides the transport to bundle on.
deadbeefcbecd352015-09-23 11:50:27 -0700847 const cricket::ContentGroup* answer_bundle =
848 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
849 if (!EnableBundle(*answer_bundle)) {
850 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
851 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
852 }
853 }
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800854 // Only push down the transport description after enabling BUNDLE; we don't
855 // want to push down a description on a transport about to be destroyed.
856 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
857 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
858 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700860 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000861 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700862 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000863 }
deadbeefd59daf82015-10-14 15:02:44 -0700864 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000865 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 }
867 }
868 return true;
869}
870
871WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
872 if (type == SessionDescriptionInterface::kOffer) {
873 return WebRtcSession::kOffer;
874 } else if (type == SessionDescriptionInterface::kPrAnswer) {
875 return WebRtcSession::kPrAnswer;
876 } else if (type == SessionDescriptionInterface::kAnswer) {
877 return WebRtcSession::kAnswer;
878 }
879 ASSERT(false && "unknown action type");
880 return WebRtcSession::kOffer;
881}
882
deadbeefd59daf82015-10-14 15:02:44 -0700883bool WebRtcSession::PushdownMediaDescription(
884 cricket::ContentAction action,
885 cricket::ContentSource source,
886 std::string* err) {
887 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
888 if (!ch) {
889 return true;
890 } else if (source == cricket::CS_LOCAL) {
891 return ch->PushdownLocalDescription(local_desc_->description(), action,
892 err);
893 } else {
894 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
895 err);
896 }
897 };
898
899 return (set_content(voice_channel()) &&
900 set_content(video_channel()) &&
901 set_content(data_channel()));
902}
903
904bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
905 cricket::ContentAction action,
906 std::string* error_desc) {
907 RTC_DCHECK(signaling_thread()->IsCurrent());
908
909 if (source == cricket::CS_LOCAL) {
910 return PushdownLocalTransportDescription(local_desc_->description(), action,
911 error_desc);
912 }
913 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
914 error_desc);
915}
916
917bool WebRtcSession::PushdownLocalTransportDescription(
918 const SessionDescription* sdesc,
919 cricket::ContentAction action,
920 std::string* err) {
921 RTC_DCHECK(signaling_thread()->IsCurrent());
922
923 if (!sdesc) {
924 return false;
925 }
926
927 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
928 if (!transport_controller_->SetLocalTransportDescription(
929 tinfo.content_name, tinfo.description, action, err)) {
930 return false;
931 }
932 }
933
934 return true;
935}
936
937bool WebRtcSession::PushdownRemoteTransportDescription(
938 const SessionDescription* sdesc,
939 cricket::ContentAction action,
940 std::string* err) {
941 RTC_DCHECK(signaling_thread()->IsCurrent());
942
943 if (!sdesc) {
944 return false;
945 }
946
947 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
948 if (!transport_controller_->SetRemoteTransportDescription(
949 tinfo.content_name, tinfo.description, action, err)) {
950 return false;
951 }
952 }
953
954 return true;
955}
956
957bool WebRtcSession::GetTransportDescription(
958 const SessionDescription* description,
959 const std::string& content_name,
960 cricket::TransportDescription* tdesc) {
961 if (!description || !tdesc) {
962 return false;
963 }
964 const TransportInfo* transport_info =
965 description->GetTransportInfoByName(content_name);
966 if (!transport_info) {
967 return false;
968 }
969 *tdesc = transport_info->description;
970 return true;
971}
972
hbosdf6075a2016-12-19 04:58:02 -0800973std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000974 ASSERT(signaling_thread()->IsCurrent());
hbosdf6075a2016-12-19 04:58:02 -0800975 ChannelNamePairs channel_name_pairs;
976 if (voice_channel()) {
977 channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair(
978 voice_channel()->content_name(), voice_channel()->transport_name()));
979 }
980 if (video_channel()) {
981 channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair(
982 video_channel()->content_name(), video_channel()->transport_name()));
983 }
984 if (data_channel()) {
985 channel_name_pairs.data = rtc::Optional<ChannelNamePair>(ChannelNamePair(
986 data_channel()->content_name(), data_channel()->transport_name()));
987 }
988 return GetStats(channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700989}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000990
hbosdf6075a2016-12-19 04:58:02 -0800991std::unique_ptr<SessionStats> WebRtcSession::GetStats(
992 const ChannelNamePairs& channel_name_pairs) {
993 if (network_thread()->IsCurrent()) {
994 return GetStats_n(channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700995 }
hbosdf6075a2016-12-19 04:58:02 -0800996 return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
997 RTC_FROM_HERE,
998 rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
deadbeefcbecd352015-09-23 11:50:27 -0700999}
1000
1001bool WebRtcSession::GetLocalCertificate(
1002 const std::string& transport_name,
1003 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001004 return transport_controller_->GetLocalCertificate(transport_name,
1005 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001006}
1007
jbauch555604a2016-04-26 03:13:22 -07001008std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -07001009 const std::string& transport_name) {
kwibergb4d01c42016-04-06 05:15:06 -07001010 return transport_controller_->GetRemoteSSLCertificate(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -07001011}
1012
deadbeefcbecd352015-09-23 11:50:27 -07001013bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1014 const std::string* first_content_name = bundle.FirstContentName();
1015 if (!first_content_name) {
1016 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1017 return false;
1018 }
1019 const std::string& transport_name = *first_content_name;
1020 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1021
zhihuang9763d562016-08-05 11:14:50 -07001022#ifdef HAVE_QUIC
1023 if (quic_data_transport_ &&
1024 bundle.HasContentName(quic_data_transport_->content_name()) &&
1025 quic_data_transport_->transport_name() != transport_name) {
1026 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1027 << " on " << transport_name << "with QUIC.";
1028 }
1029#endif
1030
deadbeefcbecd352015-09-23 11:50:27 -07001031 auto maybe_set_transport = [this, bundle, transport_name,
1032 first_channel](cricket::BaseChannel* ch) {
1033 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001034 return true;
1035 }
1036
deadbeefcbecd352015-09-23 11:50:27 -07001037 if (ch->transport_name() == transport_name) {
1038 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1039 << " on " << transport_name << ".";
1040 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001041 }
torbjornga81a42f2015-09-23 02:16:58 -07001042
deadbeefcbecd352015-09-23 11:50:27 -07001043 if (!ch->SetTransport(transport_name)) {
1044 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1045 return false;
1046 }
1047 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1048 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001049 return true;
1050 };
1051
deadbeefcbecd352015-09-23 11:50:27 -07001052 if (!maybe_set_transport(voice_channel()) ||
1053 !maybe_set_transport(video_channel()) ||
1054 !maybe_set_transport(data_channel())) {
1055 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001056 }
deadbeefcbecd352015-09-23 11:50:27 -07001057
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001058 return true;
1059}
1060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001062 if (!remote_desc_) {
1063 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1064 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001065 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 }
1067
1068 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001069 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 return false;
1071 }
1072
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001073 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001074 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1075 if (!valid) {
1076 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 }
1078
1079 // Add this candidate to the remote session description.
1080 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001081 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 return false;
1083 }
1084
deadbeefd59daf82015-10-14 15:02:44 -07001085 if (ready) {
1086 return UseCandidate(candidate);
1087 } else {
1088 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1089 return true;
1090 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091}
1092
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001093bool WebRtcSession::RemoveRemoteIceCandidates(
1094 const std::vector<cricket::Candidate>& candidates) {
1095 if (!remote_desc_) {
1096 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1097 << "removed without any remote session description.";
1098 return false;
1099 }
1100
1101 if (candidates.empty()) {
1102 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1103 return false;
1104 }
1105
1106 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1107 if (number_removed != candidates.size()) {
1108 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1109 << "Requested " << candidates.size() << " but only "
1110 << number_removed << " are removed.";
1111 }
1112
1113 // Remove the candidates from the transport controller.
1114 std::string error;
1115 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1116 if (!res && !error.empty()) {
1117 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1118 }
1119 return true;
1120}
1121
deadbeefd59daf82015-10-14 15:02:44 -07001122cricket::IceConfig WebRtcSession::ParseIceConfig(
1123 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001124 cricket::ContinualGatheringPolicy gathering_policy;
1125 // TODO(honghaiz): Add the third continual gathering policy in
1126 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1127 switch (config.continual_gathering_policy) {
1128 case PeerConnectionInterface::GATHER_ONCE:
1129 gathering_policy = cricket::GATHER_ONCE;
1130 break;
1131 case PeerConnectionInterface::GATHER_CONTINUALLY:
1132 gathering_policy = cricket::GATHER_CONTINUALLY;
1133 break;
1134 default:
1135 RTC_DCHECK(false);
1136 gathering_policy = cricket::GATHER_ONCE;
1137 }
deadbeefd59daf82015-10-14 15:02:44 -07001138 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001139 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001140 ice_config.prioritize_most_likely_candidate_pairs =
1141 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001142 ice_config.backup_connection_ping_interval =
1143 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001144 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001145 ice_config.presume_writable_when_fully_relayed =
1146 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001147 return ice_config;
1148}
1149
1150void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1151 transport_controller_->SetIceConfig(config);
1152}
1153
1154void WebRtcSession::MaybeStartGathering() {
1155 transport_controller_->MaybeStartGathering();
1156}
1157
Peter Boström0c4e06b2015-10-07 12:23:21 +02001158bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1159 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001160 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001162 }
1163 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164}
1165
Peter Boström0c4e06b2015-10-07 12:23:21 +02001166bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1167 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001168 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001169 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001170 }
1171 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172}
1173
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001176 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 return desc.str();
1178}
1179
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1181 ASSERT(signaling_thread()->IsCurrent());
1182 if (!voice_channel_) {
1183 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1184 return false;
1185 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001186 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1188 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001189 if (!local_desc_ ||
1190 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 &send_ssrc)) {
1192 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1193 return false;
1194 }
1195 return voice_channel_->CanInsertDtmf();
1196}
1197
1198bool WebRtcSession::InsertDtmf(const std::string& track_id,
1199 int code, int duration) {
1200 ASSERT(signaling_thread()->IsCurrent());
1201 if (!voice_channel_) {
1202 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1203 return false;
1204 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001205 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001206 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1207 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1209 return false;
1210 }
solenberg1d63dd02015-12-02 12:35:09 -08001211 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1213 return false;
1214 }
1215 return true;
1216}
1217
1218sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001219 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220}
1221
wu@webrtc.org78187522013-10-07 23:32:02 +00001222bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001223 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001224 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001225 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001226 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1227 return false;
1228 }
1229 return data_channel_->SendData(params, payload, result);
1230}
1231
1232bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001233 if (!data_channel_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001234 // Don't log an error here, because DataChannels are expected to call
1235 // ConnectDataChannel in this state. It's the only way to initially tell
1236 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001237 return false;
1238 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001239 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1240 &DataChannel::OnChannelReady);
1241 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1242 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001243 data_channel_->SignalStreamClosedRemotely.connect(
1244 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001245 return true;
1246}
1247
1248void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001249 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001250 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1251 return;
1252 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001253 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1254 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001255 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001256}
1257
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001258void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001259 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001260 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1261 return;
1262 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001263 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1264 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001265}
1266
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001267void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001268 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001269 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1270 << "NULL.";
1271 return;
1272 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001273 data_channel_->RemoveRecvStream(sid);
1274 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001275}
1276
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001277bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001278 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001279}
1280
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281cricket::DataChannelType WebRtcSession::data_channel_type() const {
1282 return data_channel_type_;
1283}
1284
deadbeef0ed85b22016-02-23 17:24:52 -08001285bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1286 return pending_ice_restarts_.find(content_name) !=
1287 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001288}
1289
deadbeefd1a38b52016-12-10 13:15:33 -08001290void WebRtcSession::SetNeedsIceRestartFlag() {
1291 transport_controller_->SetNeedsIceRestartFlag();
1292}
1293
1294bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1295 return transport_controller_->NeedsIceRestart(content_name);
1296}
1297
Henrik Boströmd8281982015-08-27 10:12:24 +02001298void WebRtcSession::OnCertificateReady(
1299 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001300 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001301}
1302
Henrik Boströmd8281982015-08-27 10:12:24 +02001303bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001304 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001305}
1306
deadbeefcbecd352015-09-23 11:50:27 -07001307const rtc::scoped_refptr<rtc::RTCCertificate>&
1308WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001309 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001310}
1311
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312void WebRtcSession::SetIceConnectionState(
1313 PeerConnectionInterface::IceConnectionState state) {
1314 if (ice_connection_state_ == state) {
1315 return;
1316 }
1317
deadbeefcbecd352015-09-23 11:50:27 -07001318 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1319 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001320 RTC_DCHECK(ice_connection_state_ !=
1321 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ice_connection_state_ = state;
1323 if (ice_observer_) {
1324 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1325 }
1326}
1327
deadbeefcbecd352015-09-23 11:50:27 -07001328void WebRtcSession::OnTransportControllerConnectionState(
1329 cricket::IceConnectionState state) {
1330 switch (state) {
1331 case cricket::kIceConnectionConnecting:
1332 // If the current state is Connected or Completed, then there were
1333 // writable channels but now there are not, so the next state must
1334 // be Disconnected.
1335 // kIceConnectionConnecting is currently used as the default,
1336 // un-connected state by the TransportController, so its only use is
1337 // detecting disconnections.
1338 if (ice_connection_state_ ==
1339 PeerConnectionInterface::kIceConnectionConnected ||
1340 ice_connection_state_ ==
1341 PeerConnectionInterface::kIceConnectionCompleted) {
1342 SetIceConnectionState(
1343 PeerConnectionInterface::kIceConnectionDisconnected);
1344 }
torbjornga81a42f2015-09-23 02:16:58 -07001345 break;
deadbeefcbecd352015-09-23 11:50:27 -07001346 case cricket::kIceConnectionFailed:
1347 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1348 break;
1349 case cricket::kIceConnectionConnected:
1350 LOG(LS_INFO) << "Changing to ICE connected state because "
1351 << "all transports are writable.";
1352 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1353 break;
1354 case cricket::kIceConnectionCompleted:
1355 LOG(LS_INFO) << "Changing to ICE completed state because "
1356 << "all transports are complete.";
1357 if (ice_connection_state_ !=
1358 PeerConnectionInterface::kIceConnectionConnected) {
1359 // If jumping directly from "checking" to "connected",
1360 // signal "connected" first.
1361 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1362 }
1363 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1364 if (metrics_observer_) {
1365 ReportTransportStats();
1366 }
1367 break;
1368 default:
1369 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001370 }
deadbeefcbecd352015-09-23 11:50:27 -07001371}
1372
1373void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001374 SetIceConnectionReceiving(receiving);
1375}
1376
1377void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1378 if (ice_connection_receiving_ == receiving) {
1379 return;
1380 }
1381 ice_connection_receiving_ = receiving;
1382 if (ice_observer_) {
1383 ice_observer_->OnIceConnectionReceivingChange(receiving);
1384 }
1385}
1386
deadbeefcbecd352015-09-23 11:50:27 -07001387void WebRtcSession::OnTransportControllerCandidatesGathered(
1388 const std::string& transport_name,
1389 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001391 int sdp_mline_index;
1392 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1393 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1394 << transport_name << " not found";
1395 return;
1396 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397
deadbeefcbecd352015-09-23 11:50:27 -07001398 for (cricket::Candidates::const_iterator citer = candidates.begin();
1399 citer != candidates.end(); ++citer) {
1400 // Use transport_name as the candidate media id.
1401 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1402 if (ice_observer_) {
1403 ice_observer_->OnIceCandidate(&candidate);
1404 }
1405 if (local_desc_) {
1406 local_desc_->AddCandidate(&candidate);
1407 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 }
1409}
1410
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001411void WebRtcSession::OnTransportControllerCandidatesRemoved(
1412 const std::vector<cricket::Candidate>& candidates) {
1413 ASSERT(signaling_thread()->IsCurrent());
1414 // Sanity check.
1415 for (const cricket::Candidate& candidate : candidates) {
1416 if (candidate.transport_name().empty()) {
1417 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1418 << "empty content name in candidate "
1419 << candidate.ToString();
1420 return;
1421 }
1422 }
1423
1424 if (local_desc_) {
1425 local_desc_->RemoveCandidates(candidates);
1426 }
1427 if (ice_observer_) {
1428 ice_observer_->OnIceCandidatesRemoved(candidates);
1429 }
1430}
1431
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432// Enabling voice and video channel.
1433void WebRtcSession::EnableChannels() {
1434 if (voice_channel_ && !voice_channel_->enabled())
1435 voice_channel_->Enable(true);
1436
1437 if (video_channel_ && !video_channel_->enabled())
1438 video_channel_->Enable(true);
1439
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001440 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 data_channel_->Enable(true);
1442}
1443
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444// Returns the media index for a local ice candidate given the content name.
1445bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1446 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001447 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001449 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450
1451 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001452 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 for (size_t index = 0; index < contents.size(); ++index) {
1454 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001455 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 content_found = true;
1457 break;
1458 }
1459 }
1460 return content_found;
1461}
1462
1463bool WebRtcSession::UseCandidatesInSessionDescription(
1464 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001465 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001469
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1471 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001472 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001473 const IceCandidateInterface* candidate = candidates->at(n);
1474 bool valid = false;
1475 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1476 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001477 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1478 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001479 }
1480 continue;
1481 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001482 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001483 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 break;
deadbeefd59daf82015-10-14 15:02:44 -07001485 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 }
1487 }
1488 return ret;
1489}
1490
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001491bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001493 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001495 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 return false;
1497 }
1498
1499 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001500 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 std::vector<cricket::Candidate> candidates;
1502 candidates.push_back(candidate->candidate());
1503 // Invoking BaseSession method to handle remote candidates.
1504 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001505 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1506 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 // Candidates successfully submitted for checking.
1508 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1509 ice_connection_state_ ==
1510 PeerConnectionInterface::kIceConnectionDisconnected) {
1511 // If state is New, then the session has just gotten its first remote ICE
1512 // candidates, so go to Checking.
1513 // If state is Disconnected, the session is re-using old candidates or
1514 // receiving additional ones, so go to Checking.
1515 // If state is Connected, stay Connected.
1516 // TODO(bemasc): If state is Connected, and the new candidates are for a
1517 // newly added transport, then the state actually _should_ move to
1518 // checking. Add a way to distinguish that case.
1519 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1520 }
1521 // TODO(bemasc): If state is Completed, go back to Connected.
1522 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001523 if (!error.empty()) {
1524 LOG(LS_WARNING) << error;
1525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 }
1527 return true;
1528}
1529
deadbeefcbecd352015-09-23 11:50:27 -07001530void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001531 // Destroy video_channel_ first since it may have a pointer to the
1532 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 const cricket::ContentInfo* video_info =
1534 cricket::GetFirstVideoContent(desc);
1535 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 }
1539
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001540 const cricket::ContentInfo* voice_info =
1541 cricket::GetFirstAudioContent(desc);
1542 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001543 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001544 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001545 }
1546
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 const cricket::ContentInfo* data_info =
1548 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001549 if (!data_info || data_info->rejected) {
1550 if (data_channel_) {
1551 SignalDataChannelDestroyed();
1552 channel_manager_->DestroyDataChannel(data_channel_.release());
1553 }
1554#ifdef HAVE_QUIC
1555 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1556 if (quic_data_transport_) {
1557 quic_data_transport_.reset();
1558 }
1559#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 }
1561}
1562
skvlad6c87a672016-05-17 17:49:52 -07001563// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1564// if the channel is not part of any bundle.
1565const std::string* WebRtcSession::GetBundleTransportName(
1566 const cricket::ContentInfo* content,
1567 const cricket::ContentGroup* bundle) {
1568 if (!bundle) {
1569 return nullptr;
1570 }
1571 const std::string* first_content_name = bundle->FirstContentName();
1572 if (!first_content_name) {
1573 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1574 return nullptr;
1575 }
1576 if (!bundle->HasContentName(content->name)) {
1577 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1578 return nullptr;
1579 }
1580 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1581 return first_content_name;
1582}
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001585 const cricket::ContentGroup* bundle_group = nullptr;
1586 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1587 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1588 if (!bundle_group) {
1589 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1590 return false;
1591 }
1592 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 // Creating the media channels and transport proxies.
1594 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1595 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001596 if (!CreateVoiceChannel(voice,
1597 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 LOG(LS_ERROR) << "Failed to create voice channel.";
1599 return false;
1600 }
1601 }
1602
1603 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1604 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001605 if (!CreateVideoChannel(video,
1606 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 LOG(LS_ERROR) << "Failed to create video channel.";
1608 return false;
1609 }
1610 }
1611
1612 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1613 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001614 data && !data->rejected && !data_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001615 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 LOG(LS_ERROR) << "Failed to create data channel.";
1617 return false;
1618 }
1619 }
1620
1621 return true;
1622}
1623
skvlad6c87a672016-05-17 17:49:52 -07001624bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1625 const std::string* bundle_transport) {
1626 bool require_rtcp_mux =
1627 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1628 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001630 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001631 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1632 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001633 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001634 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001635 }
skvlad6c87a672016-05-17 17:49:52 -07001636 if (require_rtcp_mux) {
1637 voice_channel_->ActivateRtcpMux();
1638 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001639
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001640 voice_channel_->SignalDtlsSetupFailure.connect(
1641 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001642
1643 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001644 voice_channel_->SignalSentPacket.connect(this,
1645 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001646 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647}
1648
skvlad6c87a672016-05-17 17:49:52 -07001649bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1650 const std::string* bundle_transport) {
1651 bool require_rtcp_mux =
1652 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1653 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001655 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001656 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1657 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001658 if (!video_channel_) {
1659 return false;
1660 }
skvlad6c87a672016-05-17 17:49:52 -07001661 if (require_rtcp_mux) {
1662 video_channel_->ActivateRtcpMux();
1663 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001664 video_channel_->SignalDtlsSetupFailure.connect(
1665 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001666
1667 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001668 video_channel_->SignalSentPacket.connect(this,
1669 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001670 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671}
1672
skvlad6c87a672016-05-17 17:49:52 -07001673bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1674 const std::string* bundle_transport) {
zhihuang9763d562016-08-05 11:14:50 -07001675#ifdef HAVE_QUIC
1676 if (data_channel_type_ == cricket::DCT_QUIC) {
1677 RTC_DCHECK(transport_controller_->quic());
1678 const std::string transport_name =
1679 bundle_transport ? *bundle_transport : content->name;
1680 quic_data_transport_->SetTransport(transport_name);
1681 return true;
1682 }
1683#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001684 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
skvlad6c87a672016-05-17 17:49:52 -07001685 bool require_rtcp_mux =
1686 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1687 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeef7af91dd2016-12-13 11:29:11 -08001689 media_controller_, transport_controller_.get(), content->name,
1690 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1691 data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001692 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001693 return false;
1694 }
skvlad6c87a672016-05-17 17:49:52 -07001695 if (require_rtcp_mux) {
1696 data_channel_->ActivateRtcpMux();
1697 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001698
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001699 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001700 data_channel_->SignalDataReceived.connect(
1701 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001702 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001703
1704 data_channel_->SignalDtlsSetupFailure.connect(
1705 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001706
1707 SignalDataChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001708 data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001709 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710}
1711
hbosdf6075a2016-12-19 04:58:02 -08001712std::unique_ptr<SessionStats> WebRtcSession::GetStats_n(
1713 const ChannelNamePairs& channel_name_pairs) {
1714 ASSERT(network_thread()->IsCurrent());
1715 std::unique_ptr<SessionStats> session_stats(new SessionStats());
1716 for (const auto channel_name_pair : { &channel_name_pairs.voice,
1717 &channel_name_pairs.video,
1718 &channel_name_pairs.data }) {
1719 if (*channel_name_pair) {
1720 cricket::TransportStats transport_stats;
1721 if (!transport_controller_->GetStats((*channel_name_pair)->transport_name,
1722 &transport_stats)) {
1723 return nullptr;
1724 }
1725 session_stats->proxy_to_transport[(*channel_name_pair)->content_name] =
1726 (*channel_name_pair)->transport_name;
1727 session_stats->transport_stats[(*channel_name_pair)->transport_name] =
1728 std::move(transport_stats);
1729 }
1730 }
1731 return session_stats;
1732}
1733
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001734void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001735 SetError(ERROR_TRANSPORT,
1736 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001739void WebRtcSession::OnDataChannelMessageReceived(
1740 cricket::DataChannel* channel,
1741 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001742 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001743 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1744 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1745 // Received OPEN message; parse and signal that a new data channel should
1746 // be created.
1747 std::string label;
1748 InternalDataChannelInit config;
1749 config.id = params.ssrc;
1750 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1751 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1752 << params.ssrc;
1753 return;
1754 }
1755 config.open_handshake_role = InternalDataChannelInit::kAcker;
1756 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 }
deadbeefab9b2d12015-10-14 11:33:11 -07001758 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759}
1760
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001761// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001762bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001763 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1764 if (!bundle_enabled)
1765 return true;
1766
1767 const cricket::ContentGroup* bundle_group =
1768 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1769 ASSERT(bundle_group != NULL);
1770
1771 const cricket::ContentInfos& contents = desc->contents();
1772 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1773 citer != contents.end(); ++citer) {
1774 const cricket::ContentInfo* content = (&*citer);
1775 ASSERT(content != NULL);
1776 if (bundle_group->HasContentName(content->name) &&
1777 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1778 if (!HasRtcpMuxEnabled(content))
1779 return false;
1780 }
1781 }
1782 // RTCP-MUX is enabled in all the contents.
1783 return true;
1784}
1785
1786bool WebRtcSession::HasRtcpMuxEnabled(
1787 const cricket::ContentInfo* content) {
1788 const cricket::MediaContentDescription* description =
1789 static_cast<cricket::MediaContentDescription*>(content->description);
1790 return description->rtcp_mux();
1791}
1792
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001793bool WebRtcSession::ValidateSessionDescription(
1794 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001795 cricket::ContentSource source, std::string* err_desc) {
1796 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001797 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001798 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001799 }
1800
1801 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001802 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001803 }
1804
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001805 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001806 Action action = GetAction(sdesc->type());
1807 if (source == cricket::CS_LOCAL) {
1808 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001809 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001810 } else {
1811 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001812 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001813 }
1814
1815 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001816 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001817 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1818 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001819 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001820 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001821 }
1822
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001823 // Verify ice-ufrag and ice-pwd.
1824 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001825 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001826 }
1827
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001828 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001829 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001830 }
1831
skvlad6c87a672016-05-17 17:49:52 -07001832 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
1833 // m-lines that do not rtcp-mux enabled.
1834
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001835 // Verify m-lines in Answer when compared against Offer.
1836 if (action == kAnswer) {
1837 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001838 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1839 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001840 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001841 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001842 }
1843 }
1844
1845 return true;
1846}
1847
1848bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1849 return ((action == kOffer && state() == STATE_INIT) ||
1850 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001851 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001852 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001853 (action == kOffer && state() == STATE_INPROGRESS) ||
1854 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001855 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1856 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1857 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1858 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001859}
1860
1861bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1862 return ((action == kOffer && state() == STATE_INIT) ||
1863 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001864 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001865 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001866 (action == kOffer && state() == STATE_INPROGRESS) ||
1867 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001868 (action == kAnswer && state() == STATE_SENTOFFER) ||
1869 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1870 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1871 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001872}
1873
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001874std::string WebRtcSession::GetSessionErrorMsg() {
1875 std::ostringstream desc;
1876 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1877 desc << kSessionErrorDesc << error_desc() << ".";
1878 return desc.str();
1879}
1880
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001881// We need to check the local/remote description for the Transport instead of
1882// the session, because a new Transport added during renegotiation may have
1883// them unset while the session has them set from the previous negotiation.
1884// Not doing so may trigger the auto generation of transport description and
1885// mess up DTLS identity information, ICE credential, etc.
1886bool WebRtcSession::ReadyToUseRemoteCandidate(
1887 const IceCandidateInterface* candidate,
1888 const SessionDescriptionInterface* remote_desc,
1889 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07001890 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001891
1892 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001893 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001894
deadbeefd59daf82015-10-14 15:02:44 -07001895 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001896 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001897 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001898
1899 size_t mediacontent_index =
1900 static_cast<size_t>(candidate->sdp_mline_index());
1901 size_t remote_content_size =
1902 current_remote_desc->description()->contents().size();
1903 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001904 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
1905 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001906
1907 *valid = false;
1908 return false;
1909 }
1910
1911 cricket::ContentInfo content =
1912 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07001913
1914 const std::string transport_name = GetTransportName(content.name);
1915 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07001916 return false;
1917 }
zhihuang9763d562016-08-05 11:14:50 -07001918 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001919}
1920
deadbeef7af91dd2016-12-13 11:29:11 -08001921bool WebRtcSession::SrtpRequired() const {
1922 return dtls_enabled_ ||
1923 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
1924}
1925
deadbeefcbecd352015-09-23 11:50:27 -07001926void WebRtcSession::OnTransportControllerGatheringState(
1927 cricket::IceGatheringState state) {
1928 ASSERT(signaling_thread()->IsCurrent());
1929 if (state == cricket::kIceGatheringGathering) {
1930 if (ice_observer_) {
1931 ice_observer_->OnIceGatheringChange(
1932 PeerConnectionInterface::kIceGatheringGathering);
1933 }
1934 } else if (state == cricket::kIceGatheringComplete) {
1935 if (ice_observer_) {
1936 ice_observer_->OnIceGatheringChange(
1937 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001938 }
1939 }
1940}
1941
1942void WebRtcSession::ReportTransportStats() {
1943 // Use a set so we don't report the same stats twice if two channels share
1944 // a transport.
1945 std::set<std::string> transport_names;
1946 if (voice_channel()) {
1947 transport_names.insert(voice_channel()->transport_name());
1948 }
1949 if (video_channel()) {
1950 transport_names.insert(video_channel()->transport_name());
1951 }
1952 if (data_channel()) {
1953 transport_names.insert(data_channel()->transport_name());
1954 }
1955 for (const auto& name : transport_names) {
1956 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07001957 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001958 ReportBestConnectionState(stats);
1959 ReportNegotiatedCiphers(stats);
1960 }
1961 }
1962}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001963// Walk through the ConnectionInfos to gather best connection usage
1964// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07001965void WebRtcSession::ReportBestConnectionState(
1966 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07001967 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001968 for (cricket::TransportChannelStatsList::const_iterator it =
1969 stats.channel_stats.begin();
1970 it != stats.channel_stats.end(); ++it) {
1971 for (cricket::ConnectionInfos::const_iterator it_info =
1972 it->connection_infos.begin();
1973 it_info != it->connection_infos.end(); ++it_info) {
1974 if (!it_info->best_connection) {
1975 continue;
1976 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001977
1978 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
1979 const cricket::Candidate& local = it_info->local_candidate;
1980 const cricket::Candidate& remote = it_info->remote_candidate;
1981
1982 // Increment the counter for IceCandidatePairType.
1983 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
1984 (local.type() == RELAY_PORT_TYPE &&
1985 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
1986 type = kEnumCounterIceCandidatePairTypeTcp;
1987 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
1988 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001989 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001990 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001991 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001992 metrics_observer_->IncrementEnumCounter(
1993 type, GetIceCandidatePairCounter(local, remote),
1994 kIceCandidatePairMax);
1995
1996 // Increment the counter for IP type.
1997 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001998 metrics_observer_->IncrementEnumCounter(
1999 kEnumCounterAddressFamily, kBestConnections_IPv4,
2000 kPeerConnectionAddressFamilyCounter_Max);
2001
2002 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002003 metrics_observer_->IncrementEnumCounter(
2004 kEnumCounterAddressFamily, kBestConnections_IPv6,
2005 kPeerConnectionAddressFamilyCounter_Max);
2006 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002007 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002008 }
2009
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002010 return;
2011 }
2012 }
2013}
2014
jbauchac8869e2015-07-03 01:36:14 -07002015void WebRtcSession::ReportNegotiatedCiphers(
2016 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002017 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002018 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2019 return;
2020 }
2021
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002022 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2023 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2024 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2025 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002026 return;
2027 }
2028
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002029 PeerConnectionEnumCounterType srtp_counter_type;
2030 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002031 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002032 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2033 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002034 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002035 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2036 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002037 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002038 srtp_counter_type = kEnumCounterDataSrtpCipher;
2039 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002040 } else {
2041 RTC_NOTREACHED();
2042 return;
2043 }
2044
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002045 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2046 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2047 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002048 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002049 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2050 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2051 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002052 }
2053}
2054
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002055void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002056 RTC_DCHECK(worker_thread()->IsCurrent());
2057 media_controller_->call_w()->OnSentPacket(sent_packet);
2058}
2059
zhihuang9763d562016-08-05 11:14:50 -07002060const std::string WebRtcSession::GetTransportName(
2061 const std::string& content_name) {
2062 cricket::BaseChannel* channel = GetChannel(content_name);
2063 if (!channel) {
2064#ifdef HAVE_QUIC
2065 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2066 content_name == quic_data_transport_->transport_name()) {
2067 return quic_data_transport_->transport_name();
2068 }
2069#endif
2070 // Return an empty string if failed to retrieve the transport name.
2071 return "";
2072 }
2073 return channel->transport_name();
2074}
zhihuangd82eee02016-08-26 11:25:05 -07002075
2076void WebRtcSession::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
2077 if (metrics_observer_) {
2078 metrics_observer_->IncrementEnumCounter(
2079 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
2080 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
2081 }
2082}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083} // namespace webrtc