blob: 1a5751a111ed089af01b63e25ec4c2728091c845 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070034#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/app/webrtc/mediaconstraintsinterface.h"
39#include "talk/app/webrtc/mediastreamsignaling.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070041#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000042#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/base/constants.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/session/media/channel.h"
46#include "talk/session/media/channelmanager.h"
47#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070049#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000054#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56using cricket::ContentInfo;
57using cricket::ContentInfos;
58using cricket::MediaContentDescription;
59using cricket::SessionDescription;
60using cricket::TransportInfo;
61
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070062using cricket::LOCAL_PORT_TYPE;
63using cricket::STUN_PORT_TYPE;
64using cricket::RELAY_PORT_TYPE;
65using cricket::PRFLX_PORT_TYPE;
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067namespace webrtc {
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000070const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
71 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073const char kInvalidCandidates[] = "Description contains invalid candidates.";
74const char kInvalidSdp[] = "Invalid session description.";
75const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
77const char kPushDownTDFailed[] =
78 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000079const char kSdpWithoutDtlsFingerprint[] =
80 "Called with SDP without DTLS fingerprint.";
81const char kSdpWithoutSdesCrypto[] =
82 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000083const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000084 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000086const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000087const char kDtlsSetupFailureRtp[] =
88 "Couldn't set up DTLS-SRTP on RTP channel.";
89const char kDtlsSetupFailureRtcp[] =
90 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070091const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000092const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070094IceCandidatePairType GetIceCandidatePairCounter(
95 const cricket::Candidate& local,
96 const cricket::Candidate& remote) {
97 const auto& l = local.type();
98 const auto& r = remote.type();
99 const auto& host = LOCAL_PORT_TYPE;
100 const auto& srflx = STUN_PORT_TYPE;
101 const auto& relay = RELAY_PORT_TYPE;
102 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700103 if (l == host && r == host) {
104 bool local_private = IPIsPrivate(local.address().ipaddr());
105 bool remote_private = IPIsPrivate(remote.address().ipaddr());
106 if (local_private) {
107 if (remote_private) {
108 return kIceCandidatePairHostPrivateHostPrivate;
109 } else {
110 return kIceCandidatePairHostPrivateHostPublic;
111 }
112 } else {
113 if (remote_private) {
114 return kIceCandidatePairHostPublicHostPrivate;
115 } else {
116 return kIceCandidatePairHostPublicHostPublic;
117 }
118 }
119 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700120 if (l == host && r == srflx)
121 return kIceCandidatePairHostSrflx;
122 if (l == host && r == relay)
123 return kIceCandidatePairHostRelay;
124 if (l == host && r == prflx)
125 return kIceCandidatePairHostPrflx;
126 if (l == srflx && r == host)
127 return kIceCandidatePairSrflxHost;
128 if (l == srflx && r == srflx)
129 return kIceCandidatePairSrflxSrflx;
130 if (l == srflx && r == relay)
131 return kIceCandidatePairSrflxRelay;
132 if (l == srflx && r == prflx)
133 return kIceCandidatePairSrflxPrflx;
134 if (l == relay && r == host)
135 return kIceCandidatePairRelayHost;
136 if (l == relay && r == srflx)
137 return kIceCandidatePairRelaySrflx;
138 if (l == relay && r == relay)
139 return kIceCandidatePairRelayRelay;
140 if (l == relay && r == prflx)
141 return kIceCandidatePairRelayPrflx;
142 if (l == prflx && r == host)
143 return kIceCandidatePairPrflxHost;
144 if (l == prflx && r == srflx)
145 return kIceCandidatePairPrflxSrflx;
146 if (l == prflx && r == relay)
147 return kIceCandidatePairPrflxRelay;
148 return kIceCandidatePairMax;
149}
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151// Compares |answer| against |offer|. Comparision is done
152// for number of m-lines in answer against offer. If matches true will be
153// returned otherwise false.
154static bool VerifyMediaDescriptions(
155 const SessionDescription* answer, const SessionDescription* offer) {
156 if (offer->contents().size() != answer->contents().size())
157 return false;
158
159 for (size_t i = 0; i < offer->contents().size(); ++i) {
160 if ((offer->contents()[i].name) != answer->contents()[i].name) {
161 return false;
162 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000163 const MediaContentDescription* offer_mdesc =
164 static_cast<const MediaContentDescription*>(
165 offer->contents()[i].description);
166 const MediaContentDescription* answer_mdesc =
167 static_cast<const MediaContentDescription*>(
168 answer->contents()[i].description);
169 if (offer_mdesc->type() != answer_mdesc->type()) {
170 return false;
171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 }
173 return true;
174}
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176// Checks that each non-rejected content has SDES crypto keys or a DTLS
177// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
178// keys, will be caught in Transport negotiation, and backstopped by Channel's
179// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000180static bool VerifyCrypto(const SessionDescription* desc,
181 bool dtls_enabled,
182 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 const ContentInfos& contents = desc->contents();
184 for (size_t index = 0; index < contents.size(); ++index) {
185 const ContentInfo* cinfo = &contents[index];
186 if (cinfo->rejected) {
187 continue;
188 }
189
190 // If the content isn't rejected, crypto must be present.
191 const MediaContentDescription* media =
192 static_cast<const MediaContentDescription*>(cinfo->description);
193 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
194 if (!media || !tinfo) {
195 // Something is not right.
196 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return false;
199 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 LOG(LS_WARNING) <<
203 "Session description must have DTLS fingerprint if DTLS enabled.";
204 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000205 return false;
206 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000207 } else {
208 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 LOG(LS_WARNING) <<
210 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000211 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000212 return false;
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215 }
216
217 return true;
218}
219
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000220// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
221static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
222 const ContentInfos& contents = desc->contents();
223 for (size_t index = 0; index < contents.size(); ++index) {
224 const ContentInfo* cinfo = &contents[index];
225 if (cinfo->rejected) {
226 continue;
227 }
228
229 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
230 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
231 if (!tinfo) {
232 // Something is not right.
233 LOG(LS_ERROR) << kInvalidSdp;
234 return false;
235 }
236 if (tinfo->description.ice_ufrag.empty() ||
237 tinfo->description.ice_pwd.empty()) {
238 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
239 return false;
240 }
241 }
242 return true;
243}
244
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245// Forces |sdesc->crypto_required| to the appropriate state based on the
246// current security policy, to ensure a failure occurs if there is an error
247// in crypto negotiation.
248// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000249static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
250 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000251 if (!sdesc) {
252 return;
253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254
wu@webrtc.org91053e72013-08-10 07:18:04 +0000255 // Updating the |crypto_required_| in MediaContentDescription to the
256 // appropriate state based on the current security policy.
257 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
258 iter != sdesc->contents().end(); ++iter) {
259 if (cricket::IsMediaContent(&*iter)) {
260 MediaContentDescription* mdesc =
261 static_cast<MediaContentDescription*> (iter->description);
262 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000263 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000264 }
265 }
266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267}
268
Peter Boström0c4e06b2015-10-07 12:23:21 +0200269static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
270 const std::string& track_id,
271 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 const cricket::ContentInfo* audio_info =
273 cricket::GetFirstAudioContent(session_description);
274 if (!audio_info) {
275 LOG(LS_ERROR) << "Audio not used in this call";
276 return false;
277 }
278
279 const cricket::MediaContentDescription* audio_content =
280 static_cast<const cricket::MediaContentDescription*>(
281 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000282 const cricket::StreamParams* stream =
283 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
284 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 return false;
286 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000287
288 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 return true;
290}
291
292static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200293 uint32_t ssrc,
294 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 ASSERT(track_id != NULL);
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 const cricket::ContentInfo* audio_info =
298 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (audio_info) {
300 const cricket::MediaContentDescription* audio_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311
312 const cricket::ContentInfo* video_info =
313 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000314 if (video_info) {
315 const cricket::MediaContentDescription* video_content =
316 static_cast<const cricket::MediaContentDescription*>(
317 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000319 const auto* found =
320 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
321 if (found) {
322 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000323 return true;
324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 }
326 return false;
327}
328
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000329static bool BadSdp(const std::string& source,
330 const std::string& type,
331 const std::string& reason,
332 std::string* err_desc) {
333 std::ostringstream desc;
334 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
335
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 return false;
341}
342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 const std::string& type,
345 const std::string& reason,
346 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 }
352}
353
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354static bool BadLocalSdp(const std::string& type,
355 const std::string& reason,
356 std::string* err_desc) {
357 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
358}
359
360static bool BadRemoteSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
364}
365
366static bool BadOfferSdp(cricket::ContentSource source,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
370}
371
372static bool BadPranswerSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
376 reason, err_desc);
377}
378
379static bool BadAnswerSdp(cricket::ContentSource source,
380 const std::string& reason,
381 std::string* err_desc) {
382 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
385#define GET_STRING_OF_STATE(state) \
386 case cricket::BaseSession::state: \
387 result = #state; \
388 break;
389
390static std::string GetStateString(cricket::BaseSession::State state) {
391 std::string result;
392 switch (state) {
393 GET_STRING_OF_STATE(STATE_INIT)
394 GET_STRING_OF_STATE(STATE_SENTINITIATE)
395 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
396 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
397 GET_STRING_OF_STATE(STATE_SENTACCEPT)
398 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
399 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
400 GET_STRING_OF_STATE(STATE_SENTMODIFY)
401 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
402 GET_STRING_OF_STATE(STATE_SENTREJECT)
403 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
404 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
405 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
406 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
407 GET_STRING_OF_STATE(STATE_INPROGRESS)
408 GET_STRING_OF_STATE(STATE_DEINIT)
409 default:
410 ASSERT(false);
411 break;
412 }
413 return result;
414}
415
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000416#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 case cricket::BaseSession::err: \
418 result = #err; \
419 break;
420
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 std::string result;
423 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000424 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
425 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
426 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
427 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
428 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
429 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 default:
431 ASSERT(false);
432 break;
433 }
434 return result;
435}
436
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000437static std::string MakeErrorString(const std::string& error,
438 const std::string& desc) {
439 std::ostringstream ret;
440 ret << error << " " << desc;
441 return ret.str();
442}
443
444static std::string MakeTdErrorString(const std::string& desc) {
445 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000448// Set |option| to the highest-priority value of |key| in the optional
449// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000450template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000451static void SetOptionFromOptionalConstraint(
452 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000453 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000454 if (!constraints) {
455 return;
456 }
457 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000458 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000459 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000460 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000461 option->Set(value);
462 }
463 }
464}
465
Peter Boström0c4e06b2015-10-07 12:23:21 +0200466uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000467 PeerConnectionInterface::IceTransportsType type) {
468 switch (type) {
469 case PeerConnectionInterface::kNone:
470 return cricket::CF_NONE;
471 case PeerConnectionInterface::kRelay:
472 return cricket::CF_RELAY;
473 case PeerConnectionInterface::kNoHost:
474 return (cricket::CF_ALL & ~cricket::CF_HOST);
475 case PeerConnectionInterface::kAll:
476 return cricket::CF_ALL;
477 default: ASSERT(false);
478 }
479 return cricket::CF_NONE;
480}
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482// Help class used to remember if a a remote peer has requested ice restart by
483// by sending a description with new ice ufrag and password.
484class IceRestartAnswerLatch {
485 public:
486 IceRestartAnswerLatch() : ice_restart_(false) { }
487
wu@webrtc.org91053e72013-08-10 07:18:04 +0000488 // Returns true if CheckForRemoteIceRestart has been called with a new session
489 // description where ice password and ufrag has changed since last time
490 // Reset() was called.
491 bool Get() const {
492 return ice_restart_;
493 }
494
495 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 if (ice_restart_) {
497 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500
honghaiz503726c2015-07-31 12:37:38 -0700501 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
502 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700504 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 }
506 const SessionDescription* new_sd = new_desc->description();
507 const SessionDescription* old_sd = old_desc->description();
508 const ContentInfos& contents = new_sd->contents();
509 for (size_t index = 0; index < contents.size(); ++index) {
510 const ContentInfo* cinfo = &contents[index];
511 if (cinfo->rejected) {
512 continue;
513 }
514 // If the content isn't rejected, check if ufrag and password has
515 // changed.
516 const cricket::TransportDescription* new_transport_desc =
517 new_sd->GetTransportDescriptionByName(cinfo->name);
518 const cricket::TransportDescription* old_transport_desc =
519 old_sd->GetTransportDescriptionByName(cinfo->name);
520 if (!new_transport_desc || !old_transport_desc) {
521 // No transport description exist. This is not an ice restart.
522 continue;
523 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000524 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
525 old_transport_desc->ice_pwd,
526 new_transport_desc->ice_ufrag,
527 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 LOG(LS_INFO) << "Remote peer request ice restart.";
529 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700530 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 }
532 }
honghaiz503726c2015-07-31 12:37:38 -0700533 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
535
536 private:
537 bool ice_restart_;
538};
539
deadbeefab9b2d12015-10-14 11:33:11 -0700540WebRtcSession::WebRtcSession(cricket::ChannelManager* channel_manager,
541 rtc::Thread* signaling_thread,
542 rtc::Thread* worker_thread,
543 cricket::PortAllocator* port_allocator)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000544 : cricket::BaseSession(signaling_thread,
545 worker_thread,
546 port_allocator,
547 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000548 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 // RFC 3264: The numeric value of the session id and version in the
550 // o line MUST be representable with a "64 bit signed integer".
551 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
552 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 ice_observer_(NULL),
554 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700555 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000557 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000559 ice_restart_latch_(new IceRestartAnswerLatch),
560 metrics_observer_(NULL) {
deadbeefcbecd352015-09-23 11:50:27 -0700561 transport_controller()->SignalConnectionState.connect(
562 this, &WebRtcSession::OnTransportControllerConnectionState);
563 transport_controller()->SignalReceiving.connect(
564 this, &WebRtcSession::OnTransportControllerReceiving);
565 transport_controller()->SignalGatheringState.connect(
566 this, &WebRtcSession::OnTransportControllerGatheringState);
567 transport_controller()->SignalCandidatesGathered.connect(
568 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569}
570
571WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700572 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000573 // Destroy video_channel_ first since it may have a pointer to the
574 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000575 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 SignalVideoChannelDestroyed();
577 channel_manager_->DestroyVideoChannel(video_channel_.release());
578 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000579 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000580 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200581 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000582 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000583 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 SignalDataChannelDestroyed();
585 channel_manager_->DestroyDataChannel(data_channel_.release());
586 }
587 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
588 delete saved_candidates_[i];
589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590}
591
wu@webrtc.org91053e72013-08-10 07:18:04 +0000592bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000593 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700594 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200595 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200596 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
597 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700598 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefcbecd352015-09-23 11:50:27 -0700599 transport_controller()->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000600
Henrik Boström87713d02015-08-25 09:53:21 +0200601 // Obtain a certificate from RTCConfiguration if any were provided (optional).
602 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
603 if (!rtc_configuration.certificates.empty()) {
604 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
605 // just picking the first one. The decision should be made based on the DTLS
606 // handshake. The DTLS negotiations need to know about all certificates.
607 certificate = rtc_configuration.certificates[0];
608 }
609
honghaiz1f429e32015-09-28 07:57:34 -0700610 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700611
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // TODO(perkj): Take |constraints| into consideration. Return false if not all
613 // mandatory constraints can be fulfilled. Note that |constraints|
614 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000616
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000617 if (options.disable_encryption) {
618 dtls_enabled_ = false;
619 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200620 // Enable DTLS by default if we have an identity store or a certificate.
621 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000622 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700623 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
624 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000625 dtls_enabled_ = value;
626 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000627 }
628
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000630 // It takes precendence over the disable_sctp_data_channels
631 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 if (FindConstraint(
633 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
634 &value, NULL) && value) {
635 LOG(LS_INFO) << "Allowing RTP data engine.";
636 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000637 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000638 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000639 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000640 LOG(LS_INFO) << "Allowing SCTP data engine.";
641 data_channel_type_ = cricket::DCT_SCTP;
642 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644
wu@webrtc.orgde305012013-10-31 15:40:38 +0000645 // Find DSCP constraint.
646 if (FindConstraint(
647 constraints,
648 MediaConstraintsInterface::kEnableDscp,
649 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000650 audio_options_.dscp.Set(value);
651 video_options_.dscp.Set(value);
652 }
653
654 // Find Suspend Below Min Bitrate constraint.
655 if (FindConstraint(
656 constraints,
657 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
658 &value,
659 NULL)) {
660 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000661 }
662
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000663 SetOptionFromOptionalConstraint(constraints,
664 MediaConstraintsInterface::kScreencastMinBitrate,
665 &video_options_.screencast_min_bitrate);
666
667 // Find constraints for cpu overuse detection.
668 SetOptionFromOptionalConstraint(constraints,
669 MediaConstraintsInterface::kCpuUnderuseThreshold,
670 &video_options_.cpu_underuse_threshold);
671 SetOptionFromOptionalConstraint(constraints,
672 MediaConstraintsInterface::kCpuOveruseThreshold,
673 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000674 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000675 MediaConstraintsInterface::kCpuOveruseDetection,
676 &video_options_.cpu_overuse_detection);
677 SetOptionFromOptionalConstraint(constraints,
678 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
679 &video_options_.cpu_overuse_encode_usage);
680 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000681 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
682 &video_options_.cpu_underuse_encode_rsd_threshold);
683 SetOptionFromOptionalConstraint(constraints,
684 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
685 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000686
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000687 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000688 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
689 &video_options_.unsignalled_recv_stream_limit);
690 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
691 int stream_limit;
692 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000693 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
694 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000695 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
696 }
697
698 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000699 MediaConstraintsInterface::kHighStartBitrate,
700 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000701
702 if (FindConstraint(
703 constraints,
704 MediaConstraintsInterface::kVeryHighBitrate,
705 &value,
706 NULL)) {
707 video_options_.video_highest_bitrate.Set(
708 cricket::VideoOptions::VERY_HIGH);
709 } else if (FindConstraint(
710 constraints,
711 MediaConstraintsInterface::kHighBitrate,
712 &value,
713 NULL)) {
714 video_options_.video_highest_bitrate.Set(
715 cricket::VideoOptions::HIGH);
716 }
717
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000718 SetOptionFromOptionalConstraint(constraints,
719 MediaConstraintsInterface::kCombinedAudioVideoBwe,
720 &audio_options_.combined_audio_video_bwe);
721
Henrik Lundin64dad832015-05-11 12:44:23 +0200722 audio_options_.audio_jitter_buffer_max_packets.Set(
723 rtc_configuration.audio_jitter_buffer_max_packets);
724
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200725 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
726 rtc_configuration.audio_jitter_buffer_fast_accelerate);
727
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 const cricket::VideoCodec default_codec(
729 JsepSessionDescription::kDefaultVideoCodecId,
730 JsepSessionDescription::kDefaultVideoCodecName,
731 JsepSessionDescription::kMaxVideoCodecWidth,
732 JsepSessionDescription::kMaxVideoCodecHeight,
733 JsepSessionDescription::kDefaultVideoCodecFramerate,
734 JsepSessionDescription::kDefaultVideoCodecPreference);
735 channel_manager_->SetDefaultVideoEncoderConfig(
736 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000737
Henrik Boström87713d02015-08-25 09:53:21 +0200738 if (!dtls_enabled_) {
739 // Construct with DTLS disabled.
740 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700741 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200742 } else {
743 // Construct with DTLS enabled.
744 if (!certificate) {
745 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700746 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200747 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700748 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
749 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200750 } else {
751 // Use the already generated certificate.
752 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700753 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200754 }
755 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000756
Henrik Boströmd8281982015-08-27 10:12:24 +0200757 webrtc_session_desc_factory_->SignalCertificateReady.connect(
758 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000759
wu@webrtc.org97077a32013-10-25 21:18:33 +0000760 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000761 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000762 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000763 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200764 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700765
766 if (rtc_configuration.enable_localhost_ice_candidate) {
767 port_allocator()->set_flags(
768 port_allocator()->flags() |
769 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
770 }
771
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200772 media_controller_.reset(MediaControllerInterface::Create(
773 worker_thread(), channel_manager_->media_engine()->GetVoE()));
774
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 return true;
776}
777
honghaiz1f429e32015-09-28 07:57:34 -0700778cricket::IceConfig WebRtcSession::ParseIceConfig(
779 const PeerConnectionInterface::RTCConfiguration& config) const {
780 cricket::IceConfig ice_config;
781 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
782 ice_config.gather_continually = (config.continual_gathering_policy ==
783 PeerConnectionInterface::GATHER_CONTINUALLY);
784 return ice_config;
785}
786
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787void WebRtcSession::Terminate() {
788 SetState(STATE_RECEIVEDTERMINATE);
deadbeefcbecd352015-09-23 11:50:27 -0700789 RemoveUnusedChannels(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000790 ASSERT(!voice_channel_);
791 ASSERT(!video_channel_);
792 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793}
794
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000795void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
796 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797}
798
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000799cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
800 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801}
802
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000803bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000804 if (local_description() == NULL || remote_description() == NULL) {
805 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
806 << "SSL Role of the session.";
807 return false;
808 }
809
deadbeefcbecd352015-09-23 11:50:27 -0700810 return transport_controller()->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000811}
812
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000813void WebRtcSession::CreateOffer(
814 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700815 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
816 const cricket::MediaSessionOptions& session_options) {
817 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000818}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819
deadbeefab9b2d12015-10-14 11:33:11 -0700820void WebRtcSession::CreateAnswer(
821 CreateSessionDescriptionObserver* observer,
822 const MediaConstraintsInterface* constraints,
823 const cricket::MediaSessionOptions& session_options) {
824 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
825 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826}
827
828bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
829 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700830 ASSERT(signaling_thread()->IsCurrent());
831
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000832 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000834
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000835 // Validate SDP.
836 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
837 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 }
839
840 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000841 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 if (state() == STATE_INIT && action == kOffer) {
843 set_initiator(true);
844 }
845
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000846 cricket::SecurePolicy sdes_policy =
847 webrtc_session_desc_factory_->SdesPolicy();
848 cricket::CryptoType crypto_required = dtls_enabled_ ?
849 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
850 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000852 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853
854 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000855 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856
857 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000858 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 // TODO(mallinath) - Handle CreateChannel failure, as new local description
860 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000861 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863
deadbeefcbecd352015-09-23 11:50:27 -0700864 // Remove unused channels if MediaContentDescription is rejected.
865 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000867 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 return false;
869 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000870
deadbeefcbecd352015-09-23 11:50:27 -0700871 if (remote_description()) {
872 // Now that we have a local description, we can push down remote candidates
873 // that we stored, and those from the remote description.
874 if (!saved_candidates_.empty()) {
875 // If there are saved candidates which arrived before the local
876 // description was set, copy those to the remote description.
877 CopySavedCandidates(remote_desc_.get());
878 }
879 // Push remote candidates in remote description to transport channels.
880 UseCandidatesInSessionDescription(remote_desc_.get());
881 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886 return true;
887}
888
889bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
890 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700891 ASSERT(signaling_thread()->IsCurrent());
892
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000893 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000894 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000895
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000896 // Validate SDP.
897 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
898 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900
901 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000902 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 if (action == kOffer && !CreateChannels(desc->description())) {
904 // TODO(mallinath) - Handle CreateChannel failure, as new local description
905 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000906 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 }
908
deadbeefcbecd352015-09-23 11:50:27 -0700909 // Remove unused channels if MediaContentDescription is rejected.
910 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911
912 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
913 // is called.
914 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000915 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 return false;
917 }
918
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000920 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
922
923 // Copy all saved candidates.
924 CopySavedCandidates(desc);
honghaiz503726c2015-07-31 12:37:38 -0700925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 // Check if this new SessionDescription contains new ice ufrag and password
927 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700928 bool ice_restart =
929 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(), desc);
930 // We retain all received candidates only if ICE is not restarted.
931 // When ICE is restarted, all previous candidates belong to an old generation
932 // and should not be kept.
933 if (!ice_restart) {
934 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
935 remote_desc_.get(), desc);
936 }
937
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000938 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000939
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000941 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000943
944 // Set the the ICE connection state to connecting since the connection may
945 // become writable with peer reflexive candidates before any remote candidate
946 // is signaled.
947 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
948 // is to have a new signal the indicates a change in checking state from the
949 // transport and expose a new checking() member from transport that can be
950 // read to determine the current checking state. The existing SignalConnecting
951 // actually means "gathering candidates", so cannot be be used here.
952 if (desc->type() != SessionDescriptionInterface::kOffer &&
953 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
954 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
955 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 return true;
957}
958
959bool WebRtcSession::UpdateSessionState(
960 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700962 ASSERT(signaling_thread()->IsCurrent());
963
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 // If there's already a pending error then no state transition should happen.
965 // But all call-sites should be verifying this before calling us!
966 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000967 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
970 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
972 SetState(source == cricket::CS_LOCAL ?
973 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000974 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
975 SetError(BaseSession::ERROR_CONTENT, *err_desc);
976 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 }
980 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
982 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 }
984 EnableChannels();
985 SetState(source == cricket::CS_LOCAL ?
986 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000987 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
988 SetError(BaseSession::ERROR_CONTENT, *err_desc);
989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000991 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 }
993 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000994 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
995 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 }
deadbeefcbecd352015-09-23 11:50:27 -0700997 const cricket::ContentGroup* local_bundle =
998 BaseSession::local_description()->GetGroupByName(
999 cricket::GROUP_TYPE_BUNDLE);
1000 const cricket::ContentGroup* remote_bundle =
1001 BaseSession::remote_description()->GetGroupByName(
1002 cricket::GROUP_TYPE_BUNDLE);
1003 if (local_bundle && remote_bundle) {
1004 // The answerer decides the transport to bundle on
1005 const cricket::ContentGroup* answer_bundle =
1006 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
1007 if (!EnableBundle(*answer_bundle)) {
1008 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
1009 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
1010 }
1011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 EnableChannels();
1013 SetState(source == cricket::CS_LOCAL ?
1014 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001015 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
1016 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001019 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 }
1021 }
1022 return true;
1023}
1024
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001025bool WebRtcSession::PushdownMediaDescription(
1026 cricket::ContentAction action,
1027 cricket::ContentSource source,
1028 std::string* err) {
1029 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1030 if (!ch) {
1031 return true;
1032 } else if (source == cricket::CS_LOCAL) {
1033 return ch->PushdownLocalDescription(
1034 base_local_description(), action, err);
1035 } else {
1036 return ch->PushdownRemoteDescription(
1037 base_remote_description(), action, err);
1038 }
1039 };
1040
1041 return (set_content(voice_channel()) &&
1042 set_content(video_channel()) &&
1043 set_content(data_channel()));
1044}
1045
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1047 if (type == SessionDescriptionInterface::kOffer) {
1048 return WebRtcSession::kOffer;
1049 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1050 return WebRtcSession::kPrAnswer;
1051 } else if (type == SessionDescriptionInterface::kAnswer) {
1052 return WebRtcSession::kAnswer;
1053 }
1054 ASSERT(false && "unknown action type");
1055 return WebRtcSession::kOffer;
1056}
1057
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001058bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
1059 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001060 return (GetChannelTransportStats(voice_channel(), stats) &&
1061 GetChannelTransportStats(video_channel(), stats) &&
1062 GetChannelTransportStats(data_channel(), stats));
1063}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001064
deadbeefcbecd352015-09-23 11:50:27 -07001065bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
1066 cricket::SessionStats* stats) {
1067 ASSERT(signaling_thread()->IsCurrent());
1068 if (!ch) {
1069 // Not using this channel.
1070 return true;
1071 }
1072
1073 const std::string& content_name = ch->content_name();
1074 const std::string& transport_name = ch->transport_name();
1075 stats->proxy_to_transport[content_name] = transport_name;
1076 if (stats->transport_stats.find(transport_name) !=
1077 stats->transport_stats.end()) {
1078 // Transport stats already done for this transport.
1079 return true;
1080 }
1081
1082 cricket::TransportStats tstats;
1083 if (!transport_controller()->GetStats(transport_name, &tstats)) {
1084 return false;
1085 }
1086
1087 stats->transport_stats[transport_name] = tstats;
1088 return true;
1089}
1090
1091bool WebRtcSession::GetLocalCertificate(
1092 const std::string& transport_name,
1093 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1094 ASSERT(signaling_thread()->IsCurrent());
1095 return transport_controller()->GetLocalCertificate(transport_name,
1096 certificate);
1097}
1098
1099bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1100 rtc::SSLCertificate** cert) {
1101 ASSERT(signaling_thread()->IsCurrent());
1102 return transport_controller()->GetRemoteSSLCertificate(transport_name, cert);
1103}
1104
1105cricket::BaseChannel* WebRtcSession::GetChannel(
1106 const std::string& content_name) {
1107 if (voice_channel() && voice_channel()->content_name() == content_name) {
1108 return voice_channel();
1109 }
1110 if (video_channel() && video_channel()->content_name() == content_name) {
1111 return video_channel();
1112 }
1113 if (data_channel() && data_channel()->content_name() == content_name) {
1114 return data_channel();
1115 }
1116 return nullptr;
1117}
1118
1119bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1120 const std::string* first_content_name = bundle.FirstContentName();
1121 if (!first_content_name) {
1122 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1123 return false;
1124 }
1125 const std::string& transport_name = *first_content_name;
1126 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1127
1128 auto maybe_set_transport = [this, bundle, transport_name,
1129 first_channel](cricket::BaseChannel* ch) {
1130 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001131 return true;
1132 }
1133
deadbeefcbecd352015-09-23 11:50:27 -07001134 if (ch->transport_name() == transport_name) {
1135 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1136 << " on " << transport_name << ".";
1137 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001138 }
torbjornga81a42f2015-09-23 02:16:58 -07001139
deadbeefcbecd352015-09-23 11:50:27 -07001140 if (!ch->SetTransport(transport_name)) {
1141 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1142 return false;
1143 }
1144 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1145 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001146 return true;
1147 };
1148
deadbeefcbecd352015-09-23 11:50:27 -07001149 if (!maybe_set_transport(voice_channel()) ||
1150 !maybe_set_transport(video_channel()) ||
1151 !maybe_set_transport(data_channel())) {
1152 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001153 }
deadbeefcbecd352015-09-23 11:50:27 -07001154
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001155 return true;
1156}
1157
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
1159 if (state() == STATE_INIT) {
1160 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1161 << "without any offer (local or remote) "
1162 << "session description.";
1163 return false;
1164 }
1165
1166 if (!candidate) {
1167 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
1168 return false;
1169 }
1170
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001171 bool valid = false;
1172 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1173 if (valid) {
1174 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1175 saved_candidates_.push_back(
1176 new JsepIceCandidate(candidate->sdp_mid(),
1177 candidate->sdp_mline_index(),
1178 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001179 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001180 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 }
1182
1183 // Add this candidate to the remote session description.
1184 if (!remote_desc_->AddCandidate(candidate)) {
1185 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1186 return false;
1187 }
1188
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001189 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190}
1191
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001192bool WebRtcSession::SetIceTransports(
1193 PeerConnectionInterface::IceTransportsType type) {
1194 return port_allocator()->set_candidate_filter(
1195 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001196}
1197
Peter Boström0c4e06b2015-10-07 12:23:21 +02001198bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1199 std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001200 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001202 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203}
1204
Peter Boström0c4e06b2015-10-07 12:23:21 +02001205bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1206 std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001207 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001208 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001209 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210}
1211
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001212std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001214 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 return desc.str();
1216}
1217
solenbergd4cec0d2015-10-09 08:55:48 -07001218void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 ASSERT(signaling_thread()->IsCurrent());
1220 if (!voice_channel_) {
1221 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1222 return;
1223 }
solenberg4bac9c52015-10-09 02:32:53 -07001224 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1225 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 // otherwise. This in the normal case when the underlying media channel has
1227 // already been deleted.
1228 ASSERT(enable == false);
1229 }
1230}
1231
Peter Boström0c4e06b2015-10-07 12:23:21 +02001232void WebRtcSession::SetAudioSend(uint32_t ssrc,
1233 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001234 const cricket::AudioOptions& options,
1235 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 ASSERT(signaling_thread()->IsCurrent());
1237 if (!voice_channel_) {
1238 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1239 return;
1240 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001241 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001242 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001243 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244}
1245
Peter Boström0c4e06b2015-10-07 12:23:21 +02001246void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001247 ASSERT(signaling_thread()->IsCurrent());
1248 ASSERT(volume >= 0 && volume <= 10);
1249 if (!voice_channel_) {
1250 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1251 return;
1252 }
1253
solenberg4bac9c52015-10-09 02:32:53 -07001254 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001255 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001256 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001257}
1258
Peter Boström0c4e06b2015-10-07 12:23:21 +02001259bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 cricket::VideoCapturer* camera) {
1261 ASSERT(signaling_thread()->IsCurrent());
1262
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001263 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1265 // support video.
1266 LOG(LS_WARNING) << "Video not used in this call.";
1267 return false;
1268 }
1269 if (!video_channel_->SetCapturer(ssrc, camera)) {
1270 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1271 // This in the normal case when the underlying media channel has already
1272 // been deleted.
1273 ASSERT(camera == NULL);
1274 return false;
1275 }
1276 return true;
1277}
1278
Peter Boström0c4e06b2015-10-07 12:23:21 +02001279void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 bool enable,
1281 cricket::VideoRenderer* renderer) {
1282 ASSERT(signaling_thread()->IsCurrent());
1283 if (!video_channel_) {
1284 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1285 return;
1286 }
1287 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1288 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1289 // This in the normal case when the underlying media channel has already
1290 // been deleted.
1291 ASSERT(renderer == NULL);
1292 }
1293}
1294
Peter Boström0c4e06b2015-10-07 12:23:21 +02001295void WebRtcSession::SetVideoSend(uint32_t ssrc,
1296 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 const cricket::VideoOptions* options) {
1298 ASSERT(signaling_thread()->IsCurrent());
1299 if (!video_channel_) {
1300 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1301 return;
1302 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001303 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1305 // This in the normal case when the underlying media channel has already
1306 // been deleted.
1307 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
1311bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1312 ASSERT(signaling_thread()->IsCurrent());
1313 if (!voice_channel_) {
1314 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1315 return false;
1316 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001317 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1319 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001320 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 &send_ssrc)) {
1322 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1323 return false;
1324 }
1325 return voice_channel_->CanInsertDtmf();
1326}
1327
1328bool WebRtcSession::InsertDtmf(const std::string& track_id,
1329 int code, int duration) {
1330 ASSERT(signaling_thread()->IsCurrent());
1331 if (!voice_channel_) {
1332 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1333 return false;
1334 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001335 uint32_t send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001336 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 track_id, &send_ssrc))) {
1338 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1339 return false;
1340 }
1341 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1342 cricket::DF_SEND)) {
1343 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1344 return false;
1345 }
1346 return true;
1347}
1348
1349sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1350 return &SignalVoiceChannelDestroyed;
1351}
1352
wu@webrtc.org78187522013-10-07 23:32:02 +00001353bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001354 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001355 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001356 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001357 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1358 return false;
1359 }
1360 return data_channel_->SendData(params, payload, result);
1361}
1362
1363bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001364 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001365 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1366 return false;
1367 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001368 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1369 &DataChannel::OnChannelReady);
1370 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1371 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001372 data_channel_->SignalStreamClosedRemotely.connect(
1373 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001374 return true;
1375}
1376
1377void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001378 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001379 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1380 return;
1381 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001382 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1383 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001384 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001385}
1386
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001387void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001388 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001389 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1390 return;
1391 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001392 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1393 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001394}
1395
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001396void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001397 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001398 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1399 << "NULL.";
1400 return;
1401 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001402 data_channel_->RemoveRecvStream(sid);
1403 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001404}
1405
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001406bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001407 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001408}
1409
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410cricket::DataChannelType WebRtcSession::data_channel_type() const {
1411 return data_channel_type_;
1412}
1413
wu@webrtc.org91053e72013-08-10 07:18:04 +00001414bool WebRtcSession::IceRestartPending() const {
1415 return ice_restart_latch_->Get();
1416}
1417
1418void WebRtcSession::ResetIceRestartLatch() {
1419 ice_restart_latch_->Reset();
1420}
1421
Henrik Boströmd8281982015-08-27 10:12:24 +02001422void WebRtcSession::OnCertificateReady(
1423 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefcbecd352015-09-23 11:50:27 -07001424 transport_controller()->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001425}
1426
Henrik Boströmd8281982015-08-27 10:12:24 +02001427bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001428 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001429}
1430
deadbeefcbecd352015-09-23 11:50:27 -07001431const rtc::scoped_refptr<rtc::RTCCertificate>&
1432WebRtcSession::certificate_for_testing() {
1433 return transport_controller()->certificate_for_testing();
1434}
1435
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436void WebRtcSession::SetIceConnectionState(
1437 PeerConnectionInterface::IceConnectionState state) {
1438 if (ice_connection_state_ == state) {
1439 return;
1440 }
1441
1442 // ASSERT that the requested transition is allowed. Note that
1443 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1444 // within PeerConnection). This switch statement should compile away when
1445 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001446 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1447 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 switch (ice_connection_state_) {
1449 case PeerConnectionInterface::kIceConnectionNew:
1450 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1451 break;
1452 case PeerConnectionInterface::kIceConnectionChecking:
1453 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1454 state == PeerConnectionInterface::kIceConnectionConnected);
1455 break;
1456 case PeerConnectionInterface::kIceConnectionConnected:
1457 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1458 state == PeerConnectionInterface::kIceConnectionChecking ||
1459 state == PeerConnectionInterface::kIceConnectionCompleted);
1460 break;
1461 case PeerConnectionInterface::kIceConnectionCompleted:
1462 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1463 state == PeerConnectionInterface::kIceConnectionDisconnected);
1464 break;
1465 case PeerConnectionInterface::kIceConnectionFailed:
1466 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1467 break;
1468 case PeerConnectionInterface::kIceConnectionDisconnected:
1469 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1470 state == PeerConnectionInterface::kIceConnectionConnected ||
1471 state == PeerConnectionInterface::kIceConnectionCompleted ||
1472 state == PeerConnectionInterface::kIceConnectionFailed);
1473 break;
1474 case PeerConnectionInterface::kIceConnectionClosed:
1475 ASSERT(false);
1476 break;
1477 default:
1478 ASSERT(false);
1479 break;
1480 }
1481
1482 ice_connection_state_ = state;
1483 if (ice_observer_) {
1484 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1485 }
1486}
1487
deadbeefcbecd352015-09-23 11:50:27 -07001488void WebRtcSession::OnTransportControllerConnectionState(
1489 cricket::IceConnectionState state) {
1490 switch (state) {
1491 case cricket::kIceConnectionConnecting:
1492 // If the current state is Connected or Completed, then there were
1493 // writable channels but now there are not, so the next state must
1494 // be Disconnected.
1495 // kIceConnectionConnecting is currently used as the default,
1496 // un-connected state by the TransportController, so its only use is
1497 // detecting disconnections.
1498 if (ice_connection_state_ ==
1499 PeerConnectionInterface::kIceConnectionConnected ||
1500 ice_connection_state_ ==
1501 PeerConnectionInterface::kIceConnectionCompleted) {
1502 SetIceConnectionState(
1503 PeerConnectionInterface::kIceConnectionDisconnected);
1504 }
torbjornga81a42f2015-09-23 02:16:58 -07001505 break;
deadbeefcbecd352015-09-23 11:50:27 -07001506 case cricket::kIceConnectionFailed:
1507 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1508 break;
1509 case cricket::kIceConnectionConnected:
1510 LOG(LS_INFO) << "Changing to ICE connected state because "
1511 << "all transports are writable.";
1512 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1513 break;
1514 case cricket::kIceConnectionCompleted:
1515 LOG(LS_INFO) << "Changing to ICE completed state because "
1516 << "all transports are complete.";
1517 if (ice_connection_state_ !=
1518 PeerConnectionInterface::kIceConnectionConnected) {
1519 // If jumping directly from "checking" to "connected",
1520 // signal "connected" first.
1521 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1522 }
1523 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1524 if (metrics_observer_) {
1525 ReportTransportStats();
1526 }
1527 break;
1528 default:
1529 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001530 }
deadbeefcbecd352015-09-23 11:50:27 -07001531}
1532
1533void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001534 SetIceConnectionReceiving(receiving);
1535}
1536
1537void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1538 if (ice_connection_receiving_ == receiving) {
1539 return;
1540 }
1541 ice_connection_receiving_ = receiving;
1542 if (ice_observer_) {
1543 ice_observer_->OnIceConnectionReceivingChange(receiving);
1544 }
1545}
1546
deadbeefcbecd352015-09-23 11:50:27 -07001547void WebRtcSession::OnTransportControllerCandidatesGathered(
1548 const std::string& transport_name,
1549 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001551 int sdp_mline_index;
1552 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1553 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1554 << transport_name << " not found";
1555 return;
1556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557
deadbeefcbecd352015-09-23 11:50:27 -07001558 for (cricket::Candidates::const_iterator citer = candidates.begin();
1559 citer != candidates.end(); ++citer) {
1560 // Use transport_name as the candidate media id.
1561 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1562 if (ice_observer_) {
1563 ice_observer_->OnIceCandidate(&candidate);
1564 }
1565 if (local_desc_) {
1566 local_desc_->AddCandidate(&candidate);
1567 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 }
1569}
1570
1571// Enabling voice and video channel.
1572void WebRtcSession::EnableChannels() {
1573 if (voice_channel_ && !voice_channel_->enabled())
1574 voice_channel_->Enable(true);
1575
1576 if (video_channel_ && !video_channel_->enabled())
1577 video_channel_->Enable(true);
1578
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001579 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 data_channel_->Enable(true);
1581}
1582
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583// Returns the media index for a local ice candidate given the content name.
1584bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1585 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001586 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 return false;
1588
1589 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001590 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 for (size_t index = 0; index < contents.size(); ++index) {
1592 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001593 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 content_found = true;
1595 break;
1596 }
1597 }
1598 return content_found;
1599}
1600
1601bool WebRtcSession::UseCandidatesInSessionDescription(
1602 const SessionDescriptionInterface* remote_desc) {
1603 if (!remote_desc)
1604 return true;
1605 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001606
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1608 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1609 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001610 const IceCandidateInterface* candidate = candidates->at(n);
1611 bool valid = false;
1612 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1613 if (valid) {
1614 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1615 saved_candidates_.push_back(
1616 new JsepIceCandidate(candidate->sdp_mid(),
1617 candidate->sdp_mline_index(),
1618 candidate->candidate()));
1619 }
1620 continue;
1621 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001622 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 if (!ret)
1624 break;
1625 }
1626 }
1627 return ret;
1628}
1629
1630bool WebRtcSession::UseCandidate(
1631 const IceCandidateInterface* candidate) {
1632
1633 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001634 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 if (mediacontent_index >= remote_content_size) {
1636 LOG(LS_ERROR)
1637 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1638 return false;
1639 }
1640
1641 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001642 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 std::vector<cricket::Candidate> candidates;
1644 candidates.push_back(candidate->candidate());
1645 // Invoking BaseSession method to handle remote candidates.
1646 std::string error;
deadbeefcbecd352015-09-23 11:50:27 -07001647 if (transport_controller()->AddRemoteCandidates(content.name, candidates,
1648 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 // Candidates successfully submitted for checking.
1650 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1651 ice_connection_state_ ==
1652 PeerConnectionInterface::kIceConnectionDisconnected) {
1653 // If state is New, then the session has just gotten its first remote ICE
1654 // candidates, so go to Checking.
1655 // If state is Disconnected, the session is re-using old candidates or
1656 // receiving additional ones, so go to Checking.
1657 // If state is Connected, stay Connected.
1658 // TODO(bemasc): If state is Connected, and the new candidates are for a
1659 // newly added transport, then the state actually _should_ move to
1660 // checking. Add a way to distinguish that case.
1661 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1662 }
1663 // TODO(bemasc): If state is Completed, go back to Connected.
1664 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001665 if (!error.empty()) {
1666 LOG(LS_WARNING) << error;
1667 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 }
1669 return true;
1670}
1671
deadbeefcbecd352015-09-23 11:50:27 -07001672void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001673 // Destroy video_channel_ first since it may have a pointer to the
1674 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 const cricket::ContentInfo* video_info =
1676 cricket::GetFirstVideoContent(desc);
1677 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 SignalVideoChannelDestroyed();
1679 const std::string content_name = video_channel_->content_name();
1680 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 }
1682
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001683 const cricket::ContentInfo* voice_info =
1684 cricket::GetFirstAudioContent(desc);
1685 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001686 SignalVoiceChannelDestroyed();
1687 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001688 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001689 }
1690
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 const cricket::ContentInfo* data_info =
1692 cricket::GetFirstDataContent(desc);
1693 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 SignalDataChannelDestroyed();
1695 const std::string content_name = data_channel_->content_name();
1696 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 }
1698}
1699
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001700// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001701// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 // Creating the media channels and transport proxies.
1704 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1705 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001706 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 LOG(LS_ERROR) << "Failed to create voice channel.";
1708 return false;
1709 }
1710 }
1711
1712 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1713 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001714 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 LOG(LS_ERROR) << "Failed to create video channel.";
1716 return false;
1717 }
1718 }
1719
1720 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1721 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001722 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001723 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 LOG(LS_ERROR) << "Failed to create data channel.";
1725 return false;
1726 }
1727 }
1728
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001729 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1730 if (voice_channel()) {
1731 voice_channel()->ActivateRtcpMux();
1732 }
1733 if (video_channel()) {
1734 video_channel()->ActivateRtcpMux();
1735 }
1736 if (data_channel()) {
1737 data_channel()->ActivateRtcpMux();
1738 }
1739 }
1740
deadbeefcbecd352015-09-23 11:50:27 -07001741 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001742 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001743 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1744 cricket::GROUP_TYPE_BUNDLE);
1745 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001746 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1747 return false;
1748 }
deadbeefcbecd352015-09-23 11:50:27 -07001749 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001750 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1751 return false;
1752 }
1753 }
1754
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 return true;
1756}
1757
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001758bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
deadbeefcbecd352015-09-23 11:50:27 -07001760 media_controller_.get(), transport_controller(), content->name, true,
1761 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001762 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001763 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001764 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001765
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001766 voice_channel_->SignalDtlsSetupFailure.connect(
1767 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001768
1769 SignalVoiceChannelCreated();
wu@webrtc.orgde305012013-10-31 15:40:38 +00001770 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771}
1772
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001773bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 video_channel_.reset(channel_manager_->CreateVideoChannel(
deadbeefcbecd352015-09-23 11:50:27 -07001775 media_controller_.get(), transport_controller(), content->name, true,
1776 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001777 if (!video_channel_) {
1778 return false;
1779 }
1780
1781 video_channel_->SignalDtlsSetupFailure.connect(
1782 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001783
1784 SignalVideoChannelCreated();
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001785 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786}
1787
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001788bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001789 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefcbecd352015-09-23 11:50:27 -07001791 transport_controller(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001792 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001793 return false;
1794 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001795
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001796 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001797 data_channel_->SignalDataReceived.connect(
1798 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001799 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001800
1801 data_channel_->SignalDtlsSetupFailure.connect(
1802 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001803
1804 SignalDataChannelCreated();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001805 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806}
1807
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001808void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1809 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1810 kDtlsSetupFailureRtp);
1811}
1812
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813void WebRtcSession::CopySavedCandidates(
1814 SessionDescriptionInterface* dest_desc) {
1815 if (!dest_desc) {
1816 ASSERT(false);
1817 return;
1818 }
1819 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1820 dest_desc->AddCandidate(saved_candidates_[i]);
1821 delete saved_candidates_[i];
1822 }
1823 saved_candidates_.clear();
1824}
1825
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001826void WebRtcSession::OnDataChannelMessageReceived(
1827 cricket::DataChannel* channel,
1828 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001829 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001830 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1831 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1832 // Received OPEN message; parse and signal that a new data channel should
1833 // be created.
1834 std::string label;
1835 InternalDataChannelInit config;
1836 config.id = params.ssrc;
1837 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1838 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1839 << params.ssrc;
1840 return;
1841 }
1842 config.open_handshake_role = InternalDataChannelInit::kAcker;
1843 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 }
deadbeefab9b2d12015-10-14 11:33:11 -07001845 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846}
1847
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001848// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001849bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001850 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1851 if (!bundle_enabled)
1852 return true;
1853
1854 const cricket::ContentGroup* bundle_group =
1855 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1856 ASSERT(bundle_group != NULL);
1857
1858 const cricket::ContentInfos& contents = desc->contents();
1859 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1860 citer != contents.end(); ++citer) {
1861 const cricket::ContentInfo* content = (&*citer);
1862 ASSERT(content != NULL);
1863 if (bundle_group->HasContentName(content->name) &&
1864 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1865 if (!HasRtcpMuxEnabled(content))
1866 return false;
1867 }
1868 }
1869 // RTCP-MUX is enabled in all the contents.
1870 return true;
1871}
1872
1873bool WebRtcSession::HasRtcpMuxEnabled(
1874 const cricket::ContentInfo* content) {
1875 const cricket::MediaContentDescription* description =
1876 static_cast<cricket::MediaContentDescription*>(content->description);
1877 return description->rtcp_mux();
1878}
1879
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001880bool WebRtcSession::ValidateSessionDescription(
1881 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001882 cricket::ContentSource source, std::string* err_desc) {
1883 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001884 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001885 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001886 }
1887
1888 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001889 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001890 }
1891
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001892 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001893 Action action = GetAction(sdesc->type());
1894 if (source == cricket::CS_LOCAL) {
1895 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001896 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001897 } else {
1898 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001899 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001900 }
1901
1902 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001903 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001904 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1905 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001906 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001907 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001908 }
1909
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001910 // Verify ice-ufrag and ice-pwd.
1911 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001912 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001913 }
1914
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001915 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001916 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001917 }
1918
1919 // Verify m-lines in Answer when compared against Offer.
1920 if (action == kAnswer) {
1921 const cricket::SessionDescription* offer_desc =
1922 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1923 local_description()->description();
1924 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001925 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001926 }
1927 }
1928
1929 return true;
1930}
1931
1932bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1933 return ((action == kOffer && state() == STATE_INIT) ||
1934 // update local offer
1935 (action == kOffer && state() == STATE_SENTINITIATE) ||
1936 // update the current ongoing session.
1937 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1938 (action == kOffer && state() == STATE_SENTACCEPT) ||
1939 (action == kOffer && state() == STATE_INPROGRESS) ||
1940 // accept remote offer
1941 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1942 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1943 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1944 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1945}
1946
1947bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1948 return ((action == kOffer && state() == STATE_INIT) ||
1949 // update remote offer
1950 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1951 // update the current ongoing session
1952 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1953 (action == kOffer && state() == STATE_SENTACCEPT) ||
1954 (action == kOffer && state() == STATE_INPROGRESS) ||
1955 // accept local offer
1956 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1957 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1958 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1959 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1960}
1961
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001962std::string WebRtcSession::GetSessionErrorMsg() {
1963 std::ostringstream desc;
1964 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1965 desc << kSessionErrorDesc << error_desc() << ".";
1966 return desc.str();
1967}
1968
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001969// We need to check the local/remote description for the Transport instead of
1970// the session, because a new Transport added during renegotiation may have
1971// them unset while the session has them set from the previous negotiation.
1972// Not doing so may trigger the auto generation of transport description and
1973// mess up DTLS identity information, ICE credential, etc.
1974bool WebRtcSession::ReadyToUseRemoteCandidate(
1975 const IceCandidateInterface* candidate,
1976 const SessionDescriptionInterface* remote_desc,
1977 bool* valid) {
1978 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001979
1980 const SessionDescriptionInterface* current_remote_desc =
1981 remote_desc ? remote_desc : remote_description();
1982
1983 if (!current_remote_desc)
1984 return false;
1985
1986 size_t mediacontent_index =
1987 static_cast<size_t>(candidate->sdp_mline_index());
1988 size_t remote_content_size =
1989 current_remote_desc->description()->contents().size();
1990 if (mediacontent_index >= remote_content_size) {
1991 LOG(LS_ERROR)
1992 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1993
1994 *valid = false;
1995 return false;
1996 }
1997
1998 cricket::ContentInfo content =
1999 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002000 cricket::BaseChannel* channel = GetChannel(content.name);
2001 if (!channel) {
2002 return false;
2003 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002004
deadbeefcbecd352015-09-23 11:50:27 -07002005 return transport_controller()->ReadyForRemoteCandidates(
2006 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002007}
2008
deadbeefcbecd352015-09-23 11:50:27 -07002009void WebRtcSession::OnTransportControllerGatheringState(
2010 cricket::IceGatheringState state) {
2011 ASSERT(signaling_thread()->IsCurrent());
2012 if (state == cricket::kIceGatheringGathering) {
2013 if (ice_observer_) {
2014 ice_observer_->OnIceGatheringChange(
2015 PeerConnectionInterface::kIceGatheringGathering);
2016 }
2017 } else if (state == cricket::kIceGatheringComplete) {
2018 if (ice_observer_) {
2019 ice_observer_->OnIceGatheringChange(
2020 PeerConnectionInterface::kIceGatheringComplete);
2021 ice_observer_->OnIceComplete();
2022 }
2023 }
2024}
2025
2026void WebRtcSession::ReportTransportStats() {
2027 // Use a set so we don't report the same stats twice if two channels share
2028 // a transport.
2029 std::set<std::string> transport_names;
2030 if (voice_channel()) {
2031 transport_names.insert(voice_channel()->transport_name());
2032 }
2033 if (video_channel()) {
2034 transport_names.insert(video_channel()->transport_name());
2035 }
2036 if (data_channel()) {
2037 transport_names.insert(data_channel()->transport_name());
2038 }
2039 for (const auto& name : transport_names) {
2040 cricket::TransportStats stats;
2041 if (transport_controller()->GetStats(name, &stats)) {
2042 ReportBestConnectionState(stats);
2043 ReportNegotiatedCiphers(stats);
2044 }
2045 }
2046}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002047// Walk through the ConnectionInfos to gather best connection usage
2048// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002049void WebRtcSession::ReportBestConnectionState(
2050 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002051 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002052 for (cricket::TransportChannelStatsList::const_iterator it =
2053 stats.channel_stats.begin();
2054 it != stats.channel_stats.end(); ++it) {
2055 for (cricket::ConnectionInfos::const_iterator it_info =
2056 it->connection_infos.begin();
2057 it_info != it->connection_infos.end(); ++it_info) {
2058 if (!it_info->best_connection) {
2059 continue;
2060 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002061
2062 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2063 const cricket::Candidate& local = it_info->local_candidate;
2064 const cricket::Candidate& remote = it_info->remote_candidate;
2065
2066 // Increment the counter for IceCandidatePairType.
2067 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2068 (local.type() == RELAY_PORT_TYPE &&
2069 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2070 type = kEnumCounterIceCandidatePairTypeTcp;
2071 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2072 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002073 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002074 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002075 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002076 metrics_observer_->IncrementEnumCounter(
2077 type, GetIceCandidatePairCounter(local, remote),
2078 kIceCandidatePairMax);
2079
2080 // Increment the counter for IP type.
2081 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002082 metrics_observer_->IncrementEnumCounter(
2083 kEnumCounterAddressFamily, kBestConnections_IPv4,
2084 kPeerConnectionAddressFamilyCounter_Max);
2085
2086 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002087 metrics_observer_->IncrementEnumCounter(
2088 kEnumCounterAddressFamily, kBestConnections_IPv6,
2089 kPeerConnectionAddressFamilyCounter_Max);
2090 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002091 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002092 }
2093
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002094 return;
2095 }
2096 }
2097}
2098
jbauchac8869e2015-07-03 01:36:14 -07002099void WebRtcSession::ReportNegotiatedCiphers(
2100 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002101 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002102 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2103 return;
2104 }
2105
2106 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07002107 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002108 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002109 return;
2110 }
2111
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002112 PeerConnectionEnumCounterType srtp_counter_type;
2113 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002114 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002115 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2116 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002117 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002118 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2119 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002120 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002121 srtp_counter_type = kEnumCounterDataSrtpCipher;
2122 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002123 } else {
2124 RTC_NOTREACHED();
2125 return;
2126 }
2127
2128 if (!srtp_cipher.empty()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002129 metrics_observer_->IncrementSparseEnumCounter(
2130 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002131 }
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002132 if (ssl_cipher) {
2133 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002134 }
2135}
2136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137} // namespace webrtc