blob: b05862d37c6a5c03dac76333d6edae8a071186a7 [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
973bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000974 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700975 return (GetChannelTransportStats(voice_channel(), stats) &&
976 GetChannelTransportStats(video_channel(), stats) &&
977 GetChannelTransportStats(data_channel(), stats));
978}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000979
deadbeefcbecd352015-09-23 11:50:27 -0700980bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -0700981 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -0700982 ASSERT(signaling_thread()->IsCurrent());
983 if (!ch) {
984 // Not using this channel.
985 return true;
986 }
987
988 const std::string& content_name = ch->content_name();
989 const std::string& transport_name = ch->transport_name();
990 stats->proxy_to_transport[content_name] = transport_name;
991 if (stats->transport_stats.find(transport_name) !=
992 stats->transport_stats.end()) {
993 // Transport stats already done for this transport.
994 return true;
995 }
996
997 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -0700998 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -0700999 return false;
1000 }
1001
1002 stats->transport_stats[transport_name] = tstats;
1003 return true;
1004}
1005
1006bool WebRtcSession::GetLocalCertificate(
1007 const std::string& transport_name,
1008 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1009 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001010 return transport_controller_->GetLocalCertificate(transport_name,
1011 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001012}
1013
jbauch555604a2016-04-26 03:13:22 -07001014std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -07001015 const std::string& transport_name) {
deadbeefcbecd352015-09-23 11:50:27 -07001016 ASSERT(signaling_thread()->IsCurrent());
kwibergb4d01c42016-04-06 05:15:06 -07001017 return transport_controller_->GetRemoteSSLCertificate(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -07001018}
1019
deadbeefcbecd352015-09-23 11:50:27 -07001020bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1021 const std::string* first_content_name = bundle.FirstContentName();
1022 if (!first_content_name) {
1023 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1024 return false;
1025 }
1026 const std::string& transport_name = *first_content_name;
1027 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1028
zhihuang9763d562016-08-05 11:14:50 -07001029#ifdef HAVE_QUIC
1030 if (quic_data_transport_ &&
1031 bundle.HasContentName(quic_data_transport_->content_name()) &&
1032 quic_data_transport_->transport_name() != transport_name) {
1033 LOG(LS_ERROR) << "Unable to BUNDLE " << quic_data_transport_->content_name()
1034 << " on " << transport_name << "with QUIC.";
1035 }
1036#endif
1037
deadbeefcbecd352015-09-23 11:50:27 -07001038 auto maybe_set_transport = [this, bundle, transport_name,
1039 first_channel](cricket::BaseChannel* ch) {
1040 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001041 return true;
1042 }
1043
deadbeefcbecd352015-09-23 11:50:27 -07001044 if (ch->transport_name() == transport_name) {
1045 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1046 << " on " << transport_name << ".";
1047 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001048 }
torbjornga81a42f2015-09-23 02:16:58 -07001049
deadbeefcbecd352015-09-23 11:50:27 -07001050 if (!ch->SetTransport(transport_name)) {
1051 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1052 return false;
1053 }
1054 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1055 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001056 return true;
1057 };
1058
deadbeefcbecd352015-09-23 11:50:27 -07001059 if (!maybe_set_transport(voice_channel()) ||
1060 !maybe_set_transport(video_channel()) ||
1061 !maybe_set_transport(data_channel())) {
1062 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001063 }
deadbeefcbecd352015-09-23 11:50:27 -07001064
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001065 return true;
1066}
1067
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001069 if (!remote_desc_) {
1070 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1071 << "without any remote session description.";
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001072 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 }
1074
1075 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001076 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 return false;
1078 }
1079
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001080 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001081 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1082 if (!valid) {
1083 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 }
1085
1086 // Add this candidate to the remote session description.
1087 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001088 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 return false;
1090 }
1091
deadbeefd59daf82015-10-14 15:02:44 -07001092 if (ready) {
1093 return UseCandidate(candidate);
1094 } else {
1095 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1096 return true;
1097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098}
1099
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001100bool WebRtcSession::RemoveRemoteIceCandidates(
1101 const std::vector<cricket::Candidate>& candidates) {
1102 if (!remote_desc_) {
1103 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be "
1104 << "removed without any remote session description.";
1105 return false;
1106 }
1107
1108 if (candidates.empty()) {
1109 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty.";
1110 return false;
1111 }
1112
1113 size_t number_removed = remote_desc_->RemoveCandidates(candidates);
1114 if (number_removed != candidates.size()) {
1115 LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. "
1116 << "Requested " << candidates.size() << " but only "
1117 << number_removed << " are removed.";
1118 }
1119
1120 // Remove the candidates from the transport controller.
1121 std::string error;
1122 bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error);
1123 if (!res && !error.empty()) {
1124 LOG(LS_ERROR) << "Error when removing remote candidates: " << error;
1125 }
1126 return true;
1127}
1128
deadbeefd59daf82015-10-14 15:02:44 -07001129cricket::IceConfig WebRtcSession::ParseIceConfig(
1130 const PeerConnectionInterface::RTCConfiguration& config) const {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001131 cricket::ContinualGatheringPolicy gathering_policy;
1132 // TODO(honghaiz): Add the third continual gathering policy in
1133 // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER.
1134 switch (config.continual_gathering_policy) {
1135 case PeerConnectionInterface::GATHER_ONCE:
1136 gathering_policy = cricket::GATHER_ONCE;
1137 break;
1138 case PeerConnectionInterface::GATHER_CONTINUALLY:
1139 gathering_policy = cricket::GATHER_CONTINUALLY;
1140 break;
1141 default:
1142 RTC_DCHECK(false);
1143 gathering_policy = cricket::GATHER_ONCE;
1144 }
deadbeefd59daf82015-10-14 15:02:44 -07001145 cricket::IceConfig ice_config;
Honghai Zhang049fbb12016-03-07 11:13:07 -08001146 ice_config.receiving_timeout = config.ice_connection_receiving_timeout;
guoweis36f01372016-03-02 18:02:40 -08001147 ice_config.prioritize_most_likely_candidate_pairs =
1148 config.prioritize_most_likely_ice_candidate_pairs;
Honghai Zhang381b4212015-12-04 12:24:03 -08001149 ice_config.backup_connection_ping_interval =
1150 config.ice_backup_candidate_pair_ping_interval;
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001151 ice_config.continual_gathering_policy = gathering_policy;
Taylor Brandstettere9851112016-07-01 11:11:13 -07001152 ice_config.presume_writable_when_fully_relayed =
1153 config.presume_writable_when_fully_relayed;
deadbeefd59daf82015-10-14 15:02:44 -07001154 return ice_config;
1155}
1156
1157void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1158 transport_controller_->SetIceConfig(config);
1159}
1160
1161void WebRtcSession::MaybeStartGathering() {
1162 transport_controller_->MaybeStartGathering();
1163}
1164
Peter Boström0c4e06b2015-10-07 12:23:21 +02001165bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1166 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001167 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001169 }
1170 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171}
1172
Peter Boström0c4e06b2015-10-07 12:23:21 +02001173bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1174 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001175 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001176 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001177 }
1178 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179}
1180
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001181std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001183 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 return desc.str();
1185}
1186
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1188 ASSERT(signaling_thread()->IsCurrent());
1189 if (!voice_channel_) {
1190 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1191 return false;
1192 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001193 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1195 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001196 if (!local_desc_ ||
1197 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 &send_ssrc)) {
1199 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1200 return false;
1201 }
1202 return voice_channel_->CanInsertDtmf();
1203}
1204
1205bool WebRtcSession::InsertDtmf(const std::string& track_id,
1206 int code, int duration) {
1207 ASSERT(signaling_thread()->IsCurrent());
1208 if (!voice_channel_) {
1209 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1210 return false;
1211 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001212 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001213 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1214 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1216 return false;
1217 }
solenberg1d63dd02015-12-02 12:35:09 -08001218 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1220 return false;
1221 }
1222 return true;
1223}
1224
1225sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
deadbeef057ecf02016-01-20 14:30:43 -08001226 return &SignalDestroyed;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227}
1228
wu@webrtc.org78187522013-10-07 23:32:02 +00001229bool WebRtcSession::SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001230 const rtc::CopyOnWriteBuffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001231 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001232 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001233 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1234 return false;
1235 }
1236 return data_channel_->SendData(params, payload, result);
1237}
1238
1239bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001240 if (!data_channel_) {
deadbeefdaf88b12016-10-05 22:29:30 -07001241 // Don't log an error here, because DataChannels are expected to call
1242 // ConnectDataChannel in this state. It's the only way to initially tell
1243 // whether or not the underlying transport is ready.
wu@webrtc.org78187522013-10-07 23:32:02 +00001244 return false;
1245 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001246 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1247 &DataChannel::OnChannelReady);
1248 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1249 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001250 data_channel_->SignalStreamClosedRemotely.connect(
1251 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001252 return true;
1253}
1254
1255void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001256 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001257 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1258 return;
1259 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001260 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1261 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001262 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001263}
1264
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001265void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001266 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001267 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1268 return;
1269 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001270 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1271 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001272}
1273
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001274void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001275 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001276 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1277 << "NULL.";
1278 return;
1279 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001280 data_channel_->RemoveRecvStream(sid);
1281 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001282}
1283
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001284bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001285 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001286}
1287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288cricket::DataChannelType WebRtcSession::data_channel_type() const {
1289 return data_channel_type_;
1290}
1291
deadbeef0ed85b22016-02-23 17:24:52 -08001292bool WebRtcSession::IceRestartPending(const std::string& content_name) const {
1293 return pending_ice_restarts_.find(content_name) !=
1294 pending_ice_restarts_.end();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001295}
1296
deadbeefd1a38b52016-12-10 13:15:33 -08001297void WebRtcSession::SetNeedsIceRestartFlag() {
1298 transport_controller_->SetNeedsIceRestartFlag();
1299}
1300
1301bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const {
1302 return transport_controller_->NeedsIceRestart(content_name);
1303}
1304
Henrik Boströmd8281982015-08-27 10:12:24 +02001305void WebRtcSession::OnCertificateReady(
1306 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001307 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001308}
1309
Henrik Boströmd8281982015-08-27 10:12:24 +02001310bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001311 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001312}
1313
deadbeefcbecd352015-09-23 11:50:27 -07001314const rtc::scoped_refptr<rtc::RTCCertificate>&
1315WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001316 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001317}
1318
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319void WebRtcSession::SetIceConnectionState(
1320 PeerConnectionInterface::IceConnectionState state) {
1321 if (ice_connection_state_ == state) {
1322 return;
1323 }
1324
deadbeefcbecd352015-09-23 11:50:27 -07001325 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1326 << " => " << state;
Taylor Brandstetter6aefc632016-05-26 16:08:23 -07001327 RTC_DCHECK(ice_connection_state_ !=
1328 PeerConnectionInterface::kIceConnectionClosed);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 ice_connection_state_ = state;
1330 if (ice_observer_) {
1331 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1332 }
1333}
1334
deadbeefcbecd352015-09-23 11:50:27 -07001335void WebRtcSession::OnTransportControllerConnectionState(
1336 cricket::IceConnectionState state) {
1337 switch (state) {
1338 case cricket::kIceConnectionConnecting:
1339 // If the current state is Connected or Completed, then there were
1340 // writable channels but now there are not, so the next state must
1341 // be Disconnected.
1342 // kIceConnectionConnecting is currently used as the default,
1343 // un-connected state by the TransportController, so its only use is
1344 // detecting disconnections.
1345 if (ice_connection_state_ ==
1346 PeerConnectionInterface::kIceConnectionConnected ||
1347 ice_connection_state_ ==
1348 PeerConnectionInterface::kIceConnectionCompleted) {
1349 SetIceConnectionState(
1350 PeerConnectionInterface::kIceConnectionDisconnected);
1351 }
torbjornga81a42f2015-09-23 02:16:58 -07001352 break;
deadbeefcbecd352015-09-23 11:50:27 -07001353 case cricket::kIceConnectionFailed:
1354 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1355 break;
1356 case cricket::kIceConnectionConnected:
1357 LOG(LS_INFO) << "Changing to ICE connected state because "
1358 << "all transports are writable.";
1359 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1360 break;
1361 case cricket::kIceConnectionCompleted:
1362 LOG(LS_INFO) << "Changing to ICE completed state because "
1363 << "all transports are complete.";
1364 if (ice_connection_state_ !=
1365 PeerConnectionInterface::kIceConnectionConnected) {
1366 // If jumping directly from "checking" to "connected",
1367 // signal "connected" first.
1368 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1369 }
1370 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1371 if (metrics_observer_) {
1372 ReportTransportStats();
1373 }
1374 break;
1375 default:
1376 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001377 }
deadbeefcbecd352015-09-23 11:50:27 -07001378}
1379
1380void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001381 SetIceConnectionReceiving(receiving);
1382}
1383
1384void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1385 if (ice_connection_receiving_ == receiving) {
1386 return;
1387 }
1388 ice_connection_receiving_ = receiving;
1389 if (ice_observer_) {
1390 ice_observer_->OnIceConnectionReceivingChange(receiving);
1391 }
1392}
1393
deadbeefcbecd352015-09-23 11:50:27 -07001394void WebRtcSession::OnTransportControllerCandidatesGathered(
1395 const std::string& transport_name,
1396 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001398 int sdp_mline_index;
1399 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1400 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1401 << transport_name << " not found";
1402 return;
1403 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404
deadbeefcbecd352015-09-23 11:50:27 -07001405 for (cricket::Candidates::const_iterator citer = candidates.begin();
1406 citer != candidates.end(); ++citer) {
1407 // Use transport_name as the candidate media id.
1408 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1409 if (ice_observer_) {
1410 ice_observer_->OnIceCandidate(&candidate);
1411 }
1412 if (local_desc_) {
1413 local_desc_->AddCandidate(&candidate);
1414 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 }
1416}
1417
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001418void WebRtcSession::OnTransportControllerCandidatesRemoved(
1419 const std::vector<cricket::Candidate>& candidates) {
1420 ASSERT(signaling_thread()->IsCurrent());
1421 // Sanity check.
1422 for (const cricket::Candidate& candidate : candidates) {
1423 if (candidate.transport_name().empty()) {
1424 LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: "
1425 << "empty content name in candidate "
1426 << candidate.ToString();
1427 return;
1428 }
1429 }
1430
1431 if (local_desc_) {
1432 local_desc_->RemoveCandidates(candidates);
1433 }
1434 if (ice_observer_) {
1435 ice_observer_->OnIceCandidatesRemoved(candidates);
1436 }
1437}
1438
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439// Enabling voice and video channel.
1440void WebRtcSession::EnableChannels() {
1441 if (voice_channel_ && !voice_channel_->enabled())
1442 voice_channel_->Enable(true);
1443
1444 if (video_channel_ && !video_channel_->enabled())
1445 video_channel_->Enable(true);
1446
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001447 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 data_channel_->Enable(true);
1449}
1450
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451// Returns the media index for a local ice candidate given the content name.
1452bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1453 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001454 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001456 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
1458 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001459 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 for (size_t index = 0; index < contents.size(); ++index) {
1461 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001462 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 content_found = true;
1464 break;
1465 }
1466 }
1467 return content_found;
1468}
1469
1470bool WebRtcSession::UseCandidatesInSessionDescription(
1471 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001472 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001476
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1478 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001479 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001480 const IceCandidateInterface* candidate = candidates->at(n);
1481 bool valid = false;
1482 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1483 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001484 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1485 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001486 }
1487 continue;
1488 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001489 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001490 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 break;
deadbeefd59daf82015-10-14 15:02:44 -07001492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 }
1494 }
1495 return ret;
1496}
1497
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001498bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001500 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001502 LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 return false;
1504 }
1505
1506 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001507 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 std::vector<cricket::Candidate> candidates;
1509 candidates.push_back(candidate->candidate());
1510 // Invoking BaseSession method to handle remote candidates.
1511 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001512 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1513 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 // Candidates successfully submitted for checking.
1515 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1516 ice_connection_state_ ==
1517 PeerConnectionInterface::kIceConnectionDisconnected) {
1518 // If state is New, then the session has just gotten its first remote ICE
1519 // candidates, so go to Checking.
1520 // If state is Disconnected, the session is re-using old candidates or
1521 // receiving additional ones, so go to Checking.
1522 // If state is Connected, stay Connected.
1523 // TODO(bemasc): If state is Connected, and the new candidates are for a
1524 // newly added transport, then the state actually _should_ move to
1525 // checking. Add a way to distinguish that case.
1526 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1527 }
1528 // TODO(bemasc): If state is Completed, go back to Connected.
1529 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001530 if (!error.empty()) {
1531 LOG(LS_WARNING) << error;
1532 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 }
1534 return true;
1535}
1536
deadbeefcbecd352015-09-23 11:50:27 -07001537void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001538 // Destroy video_channel_ first since it may have a pointer to the
1539 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 const cricket::ContentInfo* video_info =
1541 cricket::GetFirstVideoContent(desc);
1542 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 }
1546
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001547 const cricket::ContentInfo* voice_info =
1548 cricket::GetFirstAudioContent(desc);
1549 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001550 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001551 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001552 }
1553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 const cricket::ContentInfo* data_info =
1555 cricket::GetFirstDataContent(desc);
zhihuang9763d562016-08-05 11:14:50 -07001556 if (!data_info || data_info->rejected) {
1557 if (data_channel_) {
1558 SignalDataChannelDestroyed();
1559 channel_manager_->DestroyDataChannel(data_channel_.release());
1560 }
1561#ifdef HAVE_QUIC
1562 // Clean up the existing QuicDataTransport and its QuicTransportChannels.
1563 if (quic_data_transport_) {
1564 quic_data_transport_.reset();
1565 }
1566#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 }
1568}
1569
skvlad6c87a672016-05-17 17:49:52 -07001570// Returns the name of the transport channel when BUNDLE is enabled, or nullptr
1571// if the channel is not part of any bundle.
1572const std::string* WebRtcSession::GetBundleTransportName(
1573 const cricket::ContentInfo* content,
1574 const cricket::ContentGroup* bundle) {
1575 if (!bundle) {
1576 return nullptr;
1577 }
1578 const std::string* first_content_name = bundle->FirstContentName();
1579 if (!first_content_name) {
1580 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1581 return nullptr;
1582 }
1583 if (!bundle->HasContentName(content->name)) {
1584 LOG(LS_WARNING) << content->name << " is not part of any bundle group";
1585 return nullptr;
1586 }
1587 LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name;
1588 return first_content_name;
1589}
1590
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
skvlad6c87a672016-05-17 17:49:52 -07001592 const cricket::ContentGroup* bundle_group = nullptr;
1593 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
1594 bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1595 if (!bundle_group) {
1596 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1597 return false;
1598 }
1599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 // Creating the media channels and transport proxies.
1601 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1602 if (voice && !voice->rejected && !voice_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001603 if (!CreateVoiceChannel(voice,
1604 GetBundleTransportName(voice, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 LOG(LS_ERROR) << "Failed to create voice channel.";
1606 return false;
1607 }
1608 }
1609
1610 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1611 if (video && !video->rejected && !video_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001612 if (!CreateVideoChannel(video,
1613 GetBundleTransportName(video, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 LOG(LS_ERROR) << "Failed to create video channel.";
1615 return false;
1616 }
1617 }
1618
1619 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1620 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001621 data && !data->rejected && !data_channel_) {
skvlad6c87a672016-05-17 17:49:52 -07001622 if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 LOG(LS_ERROR) << "Failed to create data channel.";
1624 return false;
1625 }
1626 }
1627
1628 return true;
1629}
1630
skvlad6c87a672016-05-17 17:49:52 -07001631bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content,
1632 const std::string* bundle_transport) {
1633 bool require_rtcp_mux =
1634 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1635 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
skvlad6c87a672016-05-17 17:49:52 -07001637 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001638 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1639 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001640 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001641 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001642 }
skvlad6c87a672016-05-17 17:49:52 -07001643 if (require_rtcp_mux) {
1644 voice_channel_->ActivateRtcpMux();
1645 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001646
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001647 voice_channel_->SignalDtlsSetupFailure.connect(
1648 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001649
1650 SignalVoiceChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001651 voice_channel_->SignalSentPacket.connect(this,
1652 &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001653 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654}
1655
skvlad6c87a672016-05-17 17:49:52 -07001656bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content,
1657 const std::string* bundle_transport) {
1658 bool require_rtcp_mux =
1659 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1660 bool create_rtcp_transport_channel = !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 video_channel_.reset(channel_manager_->CreateVideoChannel(
skvlad6c87a672016-05-17 17:49:52 -07001662 media_controller_, transport_controller_.get(), content->name,
deadbeef7af91dd2016-12-13 11:29:11 -08001663 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1664 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001665 if (!video_channel_) {
1666 return false;
1667 }
skvlad6c87a672016-05-17 17:49:52 -07001668 if (require_rtcp_mux) {
1669 video_channel_->ActivateRtcpMux();
1670 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001671 video_channel_->SignalDtlsSetupFailure.connect(
1672 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001673
1674 SignalVideoChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001675 video_channel_->SignalSentPacket.connect(this,
1676 &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001677 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
skvlad6c87a672016-05-17 17:49:52 -07001680bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content,
1681 const std::string* bundle_transport) {
zhihuang9763d562016-08-05 11:14:50 -07001682#ifdef HAVE_QUIC
1683 if (data_channel_type_ == cricket::DCT_QUIC) {
1684 RTC_DCHECK(transport_controller_->quic());
1685 const std::string transport_name =
1686 bundle_transport ? *bundle_transport : content->name;
1687 quic_data_transport_->SetTransport(transport_name);
1688 return true;
1689 }
1690#endif // HAVE_QUIC
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001691 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
skvlad6c87a672016-05-17 17:49:52 -07001692 bool require_rtcp_mux =
1693 rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire;
1694 bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeef7af91dd2016-12-13 11:29:11 -08001696 media_controller_, transport_controller_.get(), content->name,
1697 bundle_transport, create_rtcp_transport_channel, SrtpRequired(),
1698 data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001699 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001700 return false;
1701 }
skvlad6c87a672016-05-17 17:49:52 -07001702 if (require_rtcp_mux) {
1703 data_channel_->ActivateRtcpMux();
1704 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001705
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001706 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001707 data_channel_->SignalDataReceived.connect(
1708 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001709 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001710
1711 data_channel_->SignalDtlsSetupFailure.connect(
1712 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001713
1714 SignalDataChannelCreated();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001715 data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001716 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717}
1718
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001719void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001720 SetError(ERROR_TRANSPORT,
1721 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722}
1723
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001724void WebRtcSession::OnDataChannelMessageReceived(
1725 cricket::DataChannel* channel,
1726 const cricket::ReceiveDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07001727 const rtc::CopyOnWriteBuffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001728 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1729 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1730 // Received OPEN message; parse and signal that a new data channel should
1731 // be created.
1732 std::string label;
1733 InternalDataChannelInit config;
1734 config.id = params.ssrc;
1735 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1736 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1737 << params.ssrc;
1738 return;
1739 }
1740 config.open_handshake_role = InternalDataChannelInit::kAcker;
1741 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 }
deadbeefab9b2d12015-10-14 11:33:11 -07001743 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001746// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001747bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001748 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1749 if (!bundle_enabled)
1750 return true;
1751
1752 const cricket::ContentGroup* bundle_group =
1753 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1754 ASSERT(bundle_group != NULL);
1755
1756 const cricket::ContentInfos& contents = desc->contents();
1757 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1758 citer != contents.end(); ++citer) {
1759 const cricket::ContentInfo* content = (&*citer);
1760 ASSERT(content != NULL);
1761 if (bundle_group->HasContentName(content->name) &&
1762 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1763 if (!HasRtcpMuxEnabled(content))
1764 return false;
1765 }
1766 }
1767 // RTCP-MUX is enabled in all the contents.
1768 return true;
1769}
1770
1771bool WebRtcSession::HasRtcpMuxEnabled(
1772 const cricket::ContentInfo* content) {
1773 const cricket::MediaContentDescription* description =
1774 static_cast<cricket::MediaContentDescription*>(content->description);
1775 return description->rtcp_mux();
1776}
1777
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001778bool WebRtcSession::ValidateSessionDescription(
1779 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001780 cricket::ContentSource source, std::string* err_desc) {
1781 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001782 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001783 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001784 }
1785
1786 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001787 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001788 }
1789
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001791 Action action = GetAction(sdesc->type());
1792 if (source == cricket::CS_LOCAL) {
1793 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001794 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001795 } else {
1796 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001797 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001798 }
1799
1800 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001801 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001802 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1803 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001804 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001805 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001806 }
1807
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001808 // Verify ice-ufrag and ice-pwd.
1809 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001810 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001811 }
1812
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001813 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001814 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001815 }
1816
skvlad6c87a672016-05-17 17:49:52 -07001817 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
1818 // m-lines that do not rtcp-mux enabled.
1819
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001820 // Verify m-lines in Answer when compared against Offer.
1821 if (action == kAnswer) {
1822 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001823 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1824 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001825 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001826 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001827 }
1828 }
1829
1830 return true;
1831}
1832
1833bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1834 return ((action == kOffer && state() == STATE_INIT) ||
1835 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001836 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001837 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001838 (action == kOffer && state() == STATE_INPROGRESS) ||
1839 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001840 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1841 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1842 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1843 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001844}
1845
1846bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1847 return ((action == kOffer && state() == STATE_INIT) ||
1848 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001849 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001850 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001851 (action == kOffer && state() == STATE_INPROGRESS) ||
1852 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07001853 (action == kAnswer && state() == STATE_SENTOFFER) ||
1854 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
1855 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
1856 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001857}
1858
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001859std::string WebRtcSession::GetSessionErrorMsg() {
1860 std::ostringstream desc;
1861 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1862 desc << kSessionErrorDesc << error_desc() << ".";
1863 return desc.str();
1864}
1865
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001866// We need to check the local/remote description for the Transport instead of
1867// the session, because a new Transport added during renegotiation may have
1868// them unset while the session has them set from the previous negotiation.
1869// Not doing so may trigger the auto generation of transport description and
1870// mess up DTLS identity information, ICE credential, etc.
1871bool WebRtcSession::ReadyToUseRemoteCandidate(
1872 const IceCandidateInterface* candidate,
1873 const SessionDescriptionInterface* remote_desc,
1874 bool* valid) {
zhihuang9763d562016-08-05 11:14:50 -07001875 *valid = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001876
1877 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001878 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001879
deadbeefd59daf82015-10-14 15:02:44 -07001880 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001881 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001882 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001883
1884 size_t mediacontent_index =
1885 static_cast<size_t>(candidate->sdp_mline_index());
1886 size_t remote_content_size =
1887 current_remote_desc->description()->contents().size();
1888 if (mediacontent_index >= remote_content_size) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001889 LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index "
1890 << mediacontent_index;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001891
1892 *valid = false;
1893 return false;
1894 }
1895
1896 cricket::ContentInfo content =
1897 current_remote_desc->description()->contents()[mediacontent_index];
zhihuang9763d562016-08-05 11:14:50 -07001898
1899 const std::string transport_name = GetTransportName(content.name);
1900 if (transport_name.empty()) {
deadbeefcbecd352015-09-23 11:50:27 -07001901 return false;
1902 }
zhihuang9763d562016-08-05 11:14:50 -07001903 return transport_controller_->ReadyForRemoteCandidates(transport_name);
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001904}
1905
deadbeef7af91dd2016-12-13 11:29:11 -08001906bool WebRtcSession::SrtpRequired() const {
1907 return dtls_enabled_ ||
1908 webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED;
1909}
1910
deadbeefcbecd352015-09-23 11:50:27 -07001911void WebRtcSession::OnTransportControllerGatheringState(
1912 cricket::IceGatheringState state) {
1913 ASSERT(signaling_thread()->IsCurrent());
1914 if (state == cricket::kIceGatheringGathering) {
1915 if (ice_observer_) {
1916 ice_observer_->OnIceGatheringChange(
1917 PeerConnectionInterface::kIceGatheringGathering);
1918 }
1919 } else if (state == cricket::kIceGatheringComplete) {
1920 if (ice_observer_) {
1921 ice_observer_->OnIceGatheringChange(
1922 PeerConnectionInterface::kIceGatheringComplete);
deadbeefcbecd352015-09-23 11:50:27 -07001923 }
1924 }
1925}
1926
1927void WebRtcSession::ReportTransportStats() {
1928 // Use a set so we don't report the same stats twice if two channels share
1929 // a transport.
1930 std::set<std::string> transport_names;
1931 if (voice_channel()) {
1932 transport_names.insert(voice_channel()->transport_name());
1933 }
1934 if (video_channel()) {
1935 transport_names.insert(video_channel()->transport_name());
1936 }
1937 if (data_channel()) {
1938 transport_names.insert(data_channel()->transport_name());
1939 }
1940 for (const auto& name : transport_names) {
1941 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07001942 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001943 ReportBestConnectionState(stats);
1944 ReportNegotiatedCiphers(stats);
1945 }
1946 }
1947}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001948// Walk through the ConnectionInfos to gather best connection usage
1949// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07001950void WebRtcSession::ReportBestConnectionState(
1951 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07001952 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001953 for (cricket::TransportChannelStatsList::const_iterator it =
1954 stats.channel_stats.begin();
1955 it != stats.channel_stats.end(); ++it) {
1956 for (cricket::ConnectionInfos::const_iterator it_info =
1957 it->connection_infos.begin();
1958 it_info != it->connection_infos.end(); ++it_info) {
1959 if (!it_info->best_connection) {
1960 continue;
1961 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001962
1963 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
1964 const cricket::Candidate& local = it_info->local_candidate;
1965 const cricket::Candidate& remote = it_info->remote_candidate;
1966
1967 // Increment the counter for IceCandidatePairType.
1968 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
1969 (local.type() == RELAY_PORT_TYPE &&
1970 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
1971 type = kEnumCounterIceCandidatePairTypeTcp;
1972 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
1973 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001974 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001975 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001976 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001977 metrics_observer_->IncrementEnumCounter(
1978 type, GetIceCandidatePairCounter(local, remote),
1979 kIceCandidatePairMax);
1980
1981 // Increment the counter for IP type.
1982 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001983 metrics_observer_->IncrementEnumCounter(
1984 kEnumCounterAddressFamily, kBestConnections_IPv4,
1985 kPeerConnectionAddressFamilyCounter_Max);
1986
1987 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001988 metrics_observer_->IncrementEnumCounter(
1989 kEnumCounterAddressFamily, kBestConnections_IPv6,
1990 kPeerConnectionAddressFamilyCounter_Max);
1991 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001992 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07001993 }
1994
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001995 return;
1996 }
1997 }
1998}
1999
jbauchac8869e2015-07-03 01:36:14 -07002000void WebRtcSession::ReportNegotiatedCiphers(
2001 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002002 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002003 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2004 return;
2005 }
2006
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002007 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2008 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2009 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2010 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
jbauchac8869e2015-07-03 01:36:14 -07002011 return;
2012 }
2013
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002014 PeerConnectionEnumCounterType srtp_counter_type;
2015 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002016 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002017 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2018 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002019 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002020 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2021 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002022 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002023 srtp_counter_type = kEnumCounterDataSrtpCipher;
2024 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002025 } else {
2026 RTC_NOTREACHED();
2027 return;
2028 }
2029
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002030 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2031 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2032 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002033 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002034 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2035 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2036 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002037 }
2038}
2039
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002040void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) {
stefanc1aeaf02015-10-15 07:26:07 -07002041 RTC_DCHECK(worker_thread()->IsCurrent());
2042 media_controller_->call_w()->OnSentPacket(sent_packet);
2043}
2044
zhihuang9763d562016-08-05 11:14:50 -07002045const std::string WebRtcSession::GetTransportName(
2046 const std::string& content_name) {
2047 cricket::BaseChannel* channel = GetChannel(content_name);
2048 if (!channel) {
2049#ifdef HAVE_QUIC
2050 if (data_channel_type_ == cricket::DCT_QUIC && quic_data_transport_ &&
2051 content_name == quic_data_transport_->transport_name()) {
2052 return quic_data_transport_->transport_name();
2053 }
2054#endif
2055 // Return an empty string if failed to retrieve the transport name.
2056 return "";
2057 }
2058 return channel->transport_name();
2059}
zhihuangd82eee02016-08-26 11:25:05 -07002060
2061void WebRtcSession::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
2062 if (metrics_observer_) {
2063 metrics_observer_->IncrementEnumCounter(
2064 webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error),
2065 static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE));
2066 }
2067}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068} // namespace webrtc