blob: bd1cd1a0ac8589af2b76977748e3ce7e6af25308 [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>
deadbeef9af63f42015-09-18 12:55:56 -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"
wu@webrtc.org91053e72013-08-10 07:18:04 +000041#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/media/base/constants.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/session/media/channel.h"
45#include "talk/session/media/channelmanager.h"
46#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070048#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "webrtc/base/helpers.h"
50#include "webrtc/base/logging.h"
51#include "webrtc/base/stringencode.h"
52#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000053#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55using cricket::ContentInfo;
56using cricket::ContentInfos;
57using cricket::MediaContentDescription;
58using cricket::SessionDescription;
59using cricket::TransportInfo;
60
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070061using cricket::LOCAL_PORT_TYPE;
62using cricket::STUN_PORT_TYPE;
63using cricket::RELAY_PORT_TYPE;
64using cricket::PRFLX_PORT_TYPE;
65
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066namespace webrtc {
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000069const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
70 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000071const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072const char kInvalidCandidates[] = "Description contains invalid candidates.";
73const char kInvalidSdp[] = "Invalid session description.";
74const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
76const char kPushDownTDFailed[] =
77 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000078const char kSdpWithoutDtlsFingerprint[] =
79 "Called with SDP without DTLS fingerprint.";
80const char kSdpWithoutSdesCrypto[] =
81 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000082const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000083 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000085const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000086const char kDtlsSetupFailureRtp[] =
87 "Couldn't set up DTLS-SRTP on RTP channel.";
88const char kDtlsSetupFailureRtcp[] =
89 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeef9af63f42015-09-18 12:55:56 -070090const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000091const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070093IceCandidatePairType GetIceCandidatePairCounter(
94 const cricket::Candidate& local,
95 const cricket::Candidate& remote) {
96 const auto& l = local.type();
97 const auto& r = remote.type();
98 const auto& host = LOCAL_PORT_TYPE;
99 const auto& srflx = STUN_PORT_TYPE;
100 const auto& relay = RELAY_PORT_TYPE;
101 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700102 if (l == host && r == host) {
103 bool local_private = IPIsPrivate(local.address().ipaddr());
104 bool remote_private = IPIsPrivate(remote.address().ipaddr());
105 if (local_private) {
106 if (remote_private) {
107 return kIceCandidatePairHostPrivateHostPrivate;
108 } else {
109 return kIceCandidatePairHostPrivateHostPublic;
110 }
111 } else {
112 if (remote_private) {
113 return kIceCandidatePairHostPublicHostPrivate;
114 } else {
115 return kIceCandidatePairHostPublicHostPublic;
116 }
117 }
118 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700119 if (l == host && r == srflx)
120 return kIceCandidatePairHostSrflx;
121 if (l == host && r == relay)
122 return kIceCandidatePairHostRelay;
123 if (l == host && r == prflx)
124 return kIceCandidatePairHostPrflx;
125 if (l == srflx && r == host)
126 return kIceCandidatePairSrflxHost;
127 if (l == srflx && r == srflx)
128 return kIceCandidatePairSrflxSrflx;
129 if (l == srflx && r == relay)
130 return kIceCandidatePairSrflxRelay;
131 if (l == srflx && r == prflx)
132 return kIceCandidatePairSrflxPrflx;
133 if (l == relay && r == host)
134 return kIceCandidatePairRelayHost;
135 if (l == relay && r == srflx)
136 return kIceCandidatePairRelaySrflx;
137 if (l == relay && r == relay)
138 return kIceCandidatePairRelayRelay;
139 if (l == relay && r == prflx)
140 return kIceCandidatePairRelayPrflx;
141 if (l == prflx && r == host)
142 return kIceCandidatePairPrflxHost;
143 if (l == prflx && r == srflx)
144 return kIceCandidatePairPrflxSrflx;
145 if (l == prflx && r == relay)
146 return kIceCandidatePairPrflxRelay;
147 return kIceCandidatePairMax;
148}
149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150// Compares |answer| against |offer|. Comparision is done
151// for number of m-lines in answer against offer. If matches true will be
152// returned otherwise false.
153static bool VerifyMediaDescriptions(
154 const SessionDescription* answer, const SessionDescription* offer) {
155 if (offer->contents().size() != answer->contents().size())
156 return false;
157
158 for (size_t i = 0; i < offer->contents().size(); ++i) {
159 if ((offer->contents()[i].name) != answer->contents()[i].name) {
160 return false;
161 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000162 const MediaContentDescription* offer_mdesc =
163 static_cast<const MediaContentDescription*>(
164 offer->contents()[i].description);
165 const MediaContentDescription* answer_mdesc =
166 static_cast<const MediaContentDescription*>(
167 answer->contents()[i].description);
168 if (offer_mdesc->type() != answer_mdesc->type()) {
169 return false;
170 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 }
172 return true;
173}
174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175// Checks that each non-rejected content has SDES crypto keys or a DTLS
176// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
177// keys, will be caught in Transport negotiation, and backstopped by Channel's
178// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000179static bool VerifyCrypto(const SessionDescription* desc,
180 bool dtls_enabled,
181 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 const ContentInfos& contents = desc->contents();
183 for (size_t index = 0; index < contents.size(); ++index) {
184 const ContentInfo* cinfo = &contents[index];
185 if (cinfo->rejected) {
186 continue;
187 }
188
189 // If the content isn't rejected, crypto must be present.
190 const MediaContentDescription* media =
191 static_cast<const MediaContentDescription*>(cinfo->description);
192 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
193 if (!media || !tinfo) {
194 // Something is not right.
195 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000196 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 return false;
198 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000199 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000200 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000201 LOG(LS_WARNING) <<
202 "Session description must have DTLS fingerprint if DTLS enabled.";
203 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000204 return false;
205 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000206 } else {
207 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000208 LOG(LS_WARNING) <<
209 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000210 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000211 return false;
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 }
214 }
215
216 return true;
217}
218
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000219// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
220static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
221 const ContentInfos& contents = desc->contents();
222 for (size_t index = 0; index < contents.size(); ++index) {
223 const ContentInfo* cinfo = &contents[index];
224 if (cinfo->rejected) {
225 continue;
226 }
227
228 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
229 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
230 if (!tinfo) {
231 // Something is not right.
232 LOG(LS_ERROR) << kInvalidSdp;
233 return false;
234 }
235 if (tinfo->description.ice_ufrag.empty() ||
236 tinfo->description.ice_pwd.empty()) {
237 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
238 return false;
239 }
240 }
241 return true;
242}
243
wu@webrtc.org91053e72013-08-10 07:18:04 +0000244// Forces |sdesc->crypto_required| to the appropriate state based on the
245// current security policy, to ensure a failure occurs if there is an error
246// in crypto negotiation.
247// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000248static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
249 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000250 if (!sdesc) {
251 return;
252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254 // Updating the |crypto_required_| in MediaContentDescription to the
255 // appropriate state based on the current security policy.
256 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
257 iter != sdesc->contents().end(); ++iter) {
258 if (cricket::IsMediaContent(&*iter)) {
259 MediaContentDescription* mdesc =
260 static_cast<MediaContentDescription*> (iter->description);
261 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000262 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000263 }
264 }
265 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266}
267
268static bool GetAudioSsrcByTrackId(
269 const SessionDescription* session_description,
270 const std::string& track_id, uint32 *ssrc) {
271 const cricket::ContentInfo* audio_info =
272 cricket::GetFirstAudioContent(session_description);
273 if (!audio_info) {
274 LOG(LS_ERROR) << "Audio not used in this call";
275 return false;
276 }
277
278 const cricket::MediaContentDescription* audio_content =
279 static_cast<const cricket::MediaContentDescription*>(
280 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000281 const cricket::StreamParams* stream =
282 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
283 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 return false;
285 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000286
287 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 return true;
289}
290
291static bool GetTrackIdBySsrc(const SessionDescription* session_description,
292 uint32 ssrc, std::string* track_id) {
293 ASSERT(track_id != NULL);
294
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 const cricket::ContentInfo* audio_info =
296 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000297 if (audio_info) {
298 const cricket::MediaContentDescription* audio_content =
299 static_cast<const cricket::MediaContentDescription*>(
300 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000302 const auto* found =
303 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
304 if (found) {
305 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000306 return true;
307 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 }
309
310 const cricket::ContentInfo* video_info =
311 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000312 if (video_info) {
313 const cricket::MediaContentDescription* video_content =
314 static_cast<const cricket::MediaContentDescription*>(
315 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000317 const auto* found =
318 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
319 if (found) {
320 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000321 return true;
322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 }
324 return false;
325}
326
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000327static bool BadSdp(const std::string& source,
328 const std::string& type,
329 const std::string& reason,
330 std::string* err_desc) {
331 std::ostringstream desc;
332 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
333
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000335 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000337 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 return false;
339}
340
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342 const std::string& type,
343 const std::string& reason,
344 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 }
350}
351
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000352static bool BadLocalSdp(const std::string& type,
353 const std::string& reason,
354 std::string* err_desc) {
355 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
356}
357
358static bool BadRemoteSdp(const std::string& type,
359 const std::string& reason,
360 std::string* err_desc) {
361 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
362}
363
364static bool BadOfferSdp(cricket::ContentSource source,
365 const std::string& reason,
366 std::string* err_desc) {
367 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
368}
369
370static bool BadPranswerSdp(cricket::ContentSource source,
371 const std::string& reason,
372 std::string* err_desc) {
373 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
374 reason, err_desc);
375}
376
377static bool BadAnswerSdp(cricket::ContentSource source,
378 const std::string& reason,
379 std::string* err_desc) {
380 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381}
382
383#define GET_STRING_OF_STATE(state) \
384 case cricket::BaseSession::state: \
385 result = #state; \
386 break;
387
388static std::string GetStateString(cricket::BaseSession::State state) {
389 std::string result;
390 switch (state) {
391 GET_STRING_OF_STATE(STATE_INIT)
392 GET_STRING_OF_STATE(STATE_SENTINITIATE)
393 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
394 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
395 GET_STRING_OF_STATE(STATE_SENTACCEPT)
396 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
397 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
398 GET_STRING_OF_STATE(STATE_SENTMODIFY)
399 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
400 GET_STRING_OF_STATE(STATE_SENTREJECT)
401 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
402 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
403 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
404 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
405 GET_STRING_OF_STATE(STATE_INPROGRESS)
406 GET_STRING_OF_STATE(STATE_DEINIT)
407 default:
408 ASSERT(false);
409 break;
410 }
411 return result;
412}
413
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000414#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 case cricket::BaseSession::err: \
416 result = #err; \
417 break;
418
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 std::string result;
421 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
423 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
424 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
425 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
426 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
427 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 default:
429 ASSERT(false);
430 break;
431 }
432 return result;
433}
434
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000435static std::string MakeErrorString(const std::string& error,
436 const std::string& desc) {
437 std::ostringstream ret;
438 ret << error << " " << desc;
439 return ret.str();
440}
441
442static std::string MakeTdErrorString(const std::string& desc) {
443 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000446// Set |option| to the highest-priority value of |key| in the optional
447// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000448template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000449static void SetOptionFromOptionalConstraint(
450 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000451 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000452 if (!constraints) {
453 return;
454 }
455 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000456 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000457 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000459 option->Set(value);
460 }
461 }
462}
463
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000464uint32 ConvertIceTransportTypeToCandidateFilter(
465 PeerConnectionInterface::IceTransportsType type) {
466 switch (type) {
467 case PeerConnectionInterface::kNone:
468 return cricket::CF_NONE;
469 case PeerConnectionInterface::kRelay:
470 return cricket::CF_RELAY;
471 case PeerConnectionInterface::kNoHost:
472 return (cricket::CF_ALL & ~cricket::CF_HOST);
473 case PeerConnectionInterface::kAll:
474 return cricket::CF_ALL;
475 default: ASSERT(false);
476 }
477 return cricket::CF_NONE;
478}
479
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480// Help class used to remember if a a remote peer has requested ice restart by
481// by sending a description with new ice ufrag and password.
482class IceRestartAnswerLatch {
483 public:
484 IceRestartAnswerLatch() : ice_restart_(false) { }
485
wu@webrtc.org91053e72013-08-10 07:18:04 +0000486 // Returns true if CheckForRemoteIceRestart has been called with a new session
487 // description where ice password and ufrag has changed since last time
488 // Reset() was called.
489 bool Get() const {
490 return ice_restart_;
491 }
492
493 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 if (ice_restart_) {
495 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498
honghaiz503726c2015-07-31 12:37:38 -0700499 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
500 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700502 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 }
504 const SessionDescription* new_sd = new_desc->description();
505 const SessionDescription* old_sd = old_desc->description();
506 const ContentInfos& contents = new_sd->contents();
507 for (size_t index = 0; index < contents.size(); ++index) {
508 const ContentInfo* cinfo = &contents[index];
509 if (cinfo->rejected) {
510 continue;
511 }
512 // If the content isn't rejected, check if ufrag and password has
513 // changed.
514 const cricket::TransportDescription* new_transport_desc =
515 new_sd->GetTransportDescriptionByName(cinfo->name);
516 const cricket::TransportDescription* old_transport_desc =
517 old_sd->GetTransportDescriptionByName(cinfo->name);
518 if (!new_transport_desc || !old_transport_desc) {
519 // No transport description exist. This is not an ice restart.
520 continue;
521 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000522 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
523 old_transport_desc->ice_pwd,
524 new_transport_desc->ice_ufrag,
525 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 LOG(LS_INFO) << "Remote peer request ice restart.";
527 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700528 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 }
530 }
honghaiz503726c2015-07-31 12:37:38 -0700531 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 }
533
534 private:
535 bool ice_restart_;
536};
537
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000538WebRtcSession::WebRtcSession(
539 cricket::ChannelManager* channel_manager,
540 rtc::Thread* signaling_thread,
541 rtc::Thread* worker_thread,
542 cricket::PortAllocator* port_allocator,
543 MediaStreamSignaling* mediastream_signaling)
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 mediastream_signaling_(mediastream_signaling),
554 ice_observer_(NULL),
555 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700556 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000558 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000560 ice_restart_latch_(new IceRestartAnswerLatch),
561 metrics_observer_(NULL) {
deadbeef9af63f42015-09-18 12:55:56 -0700562 transport_controller()->SignalConnectionState.connect(
563 this, &WebRtcSession::OnTransportControllerConnectionState);
564 transport_controller()->SignalReceiving.connect(
565 this, &WebRtcSession::OnTransportControllerReceiving);
566 transport_controller()->SignalGatheringState.connect(
567 this, &WebRtcSession::OnTransportControllerGatheringState);
568 transport_controller()->SignalCandidatesGathered.connect(
569 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570}
571
572WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700573 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000574 // Destroy video_channel_ first since it may have a pointer to the
575 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000576 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 SignalVideoChannelDestroyed();
578 channel_manager_->DestroyVideoChannel(video_channel_.release());
579 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000580 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000581 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200582 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000583 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000584 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 SignalDataChannelDestroyed();
586 channel_manager_->DestroyDataChannel(data_channel_.release());
587 }
588 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
589 delete saved_candidates_[i];
590 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591}
592
wu@webrtc.org91053e72013-08-10 07:18:04 +0000593bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000594 const PeerConnectionFactoryInterface::Options& options,
deadbeef9af63f42015-09-18 12:55:56 -0700595 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200596 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200597 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
598 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700599 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeef9af63f42015-09-18 12:55:56 -0700600 transport_controller()->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000601
Henrik Boström87713d02015-08-25 09:53:21 +0200602 // Obtain a certificate from RTCConfiguration if any were provided (optional).
603 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
604 if (!rtc_configuration.certificates.empty()) {
605 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
606 // just picking the first one. The decision should be made based on the DTLS
607 // handshake. The DTLS negotiations need to know about all certificates.
608 certificate = rtc_configuration.certificates[0];
609 }
610
honghaiz4edc39c2015-09-01 09:53:56 -0700611 SetIceConnectionReceivingTimeout(
612 rtc_configuration.ice_connection_receiving_timeout);
613
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // TODO(perkj): Take |constraints| into consideration. Return false if not all
615 // mandatory constraints can be fulfilled. Note that |constraints|
616 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000618
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000619 if (options.disable_encryption) {
620 dtls_enabled_ = false;
621 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200622 // Enable DTLS by default if we have an identity store or a certificate.
623 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 // |constraints| can override the default |dtls_enabled_| value.
deadbeef9af63f42015-09-18 12:55:56 -0700625 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
626 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000627 dtls_enabled_ = value;
628 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000629 }
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000632 // It takes precendence over the disable_sctp_data_channels
633 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 if (FindConstraint(
635 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
636 &value, NULL) && value) {
637 LOG(LS_INFO) << "Allowing RTP data engine.";
638 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000639 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000640 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000641 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000642 LOG(LS_INFO) << "Allowing SCTP data engine.";
643 data_channel_type_ = cricket::DCT_SCTP;
644 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 }
646 if (data_channel_type_ != cricket::DCT_NONE) {
647 mediastream_signaling_->SetDataChannelFactory(this);
648 }
649
wu@webrtc.orgde305012013-10-31 15:40:38 +0000650 // Find DSCP constraint.
651 if (FindConstraint(
652 constraints,
653 MediaConstraintsInterface::kEnableDscp,
654 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000655 audio_options_.dscp.Set(value);
656 video_options_.dscp.Set(value);
657 }
658
659 // Find Suspend Below Min Bitrate constraint.
660 if (FindConstraint(
661 constraints,
662 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
663 &value,
664 NULL)) {
665 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000666 }
667
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000668 SetOptionFromOptionalConstraint(constraints,
669 MediaConstraintsInterface::kScreencastMinBitrate,
670 &video_options_.screencast_min_bitrate);
671
672 // Find constraints for cpu overuse detection.
673 SetOptionFromOptionalConstraint(constraints,
674 MediaConstraintsInterface::kCpuUnderuseThreshold,
675 &video_options_.cpu_underuse_threshold);
676 SetOptionFromOptionalConstraint(constraints,
677 MediaConstraintsInterface::kCpuOveruseThreshold,
678 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000679 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000680 MediaConstraintsInterface::kCpuOveruseDetection,
681 &video_options_.cpu_overuse_detection);
682 SetOptionFromOptionalConstraint(constraints,
683 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
684 &video_options_.cpu_overuse_encode_usage);
685 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000686 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
687 &video_options_.cpu_underuse_encode_rsd_threshold);
688 SetOptionFromOptionalConstraint(constraints,
689 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
690 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000691
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000692 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000693 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
694 &video_options_.unsignalled_recv_stream_limit);
695 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
696 int stream_limit;
697 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000698 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
699 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000700 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
701 }
702
703 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000704 MediaConstraintsInterface::kHighStartBitrate,
705 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000706
707 if (FindConstraint(
708 constraints,
709 MediaConstraintsInterface::kVeryHighBitrate,
710 &value,
711 NULL)) {
712 video_options_.video_highest_bitrate.Set(
713 cricket::VideoOptions::VERY_HIGH);
714 } else if (FindConstraint(
715 constraints,
716 MediaConstraintsInterface::kHighBitrate,
717 &value,
718 NULL)) {
719 video_options_.video_highest_bitrate.Set(
720 cricket::VideoOptions::HIGH);
721 }
722
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000723 SetOptionFromOptionalConstraint(constraints,
724 MediaConstraintsInterface::kCombinedAudioVideoBwe,
725 &audio_options_.combined_audio_video_bwe);
726
Henrik Lundin64dad832015-05-11 12:44:23 +0200727 audio_options_.audio_jitter_buffer_max_packets.Set(
728 rtc_configuration.audio_jitter_buffer_max_packets);
729
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200730 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
731 rtc_configuration.audio_jitter_buffer_fast_accelerate);
732
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 const cricket::VideoCodec default_codec(
734 JsepSessionDescription::kDefaultVideoCodecId,
735 JsepSessionDescription::kDefaultVideoCodecName,
736 JsepSessionDescription::kMaxVideoCodecWidth,
737 JsepSessionDescription::kMaxVideoCodecHeight,
738 JsepSessionDescription::kDefaultVideoCodecFramerate,
739 JsepSessionDescription::kDefaultVideoCodecPreference);
740 channel_manager_->SetDefaultVideoEncoderConfig(
741 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000742
Henrik Boström87713d02015-08-25 09:53:21 +0200743 if (!dtls_enabled_) {
744 // Construct with DTLS disabled.
745 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeef9af63f42015-09-18 12:55:56 -0700746 signaling_thread(), channel_manager_, mediastream_signaling_, this,
747 id(), data_channel_type_));
Henrik Boström87713d02015-08-25 09:53:21 +0200748 } else {
749 // Construct with DTLS enabled.
750 if (!certificate) {
751 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700752 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200753 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeef9af63f42015-09-18 12:55:56 -0700754 signaling_thread(), channel_manager_, mediastream_signaling_,
755 dtls_identity_store.Pass(), this, id(), data_channel_type_));
Henrik Boström87713d02015-08-25 09:53:21 +0200756 } else {
757 // Use the already generated certificate.
758 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeef9af63f42015-09-18 12:55:56 -0700759 signaling_thread(), channel_manager_, mediastream_signaling_,
760 certificate, this, id(), data_channel_type_));
Henrik Boström87713d02015-08-25 09:53:21 +0200761 }
762 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000763
Henrik Boströmd8281982015-08-27 10:12:24 +0200764 webrtc_session_desc_factory_->SignalCertificateReady.connect(
765 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000766
wu@webrtc.org97077a32013-10-25 21:18:33 +0000767 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000768 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000769 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000770 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200771 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700772
773 if (rtc_configuration.enable_localhost_ice_candidate) {
774 port_allocator()->set_flags(
775 port_allocator()->flags() |
776 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
777 }
778
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200779 media_controller_.reset(MediaControllerInterface::Create(
780 worker_thread(), channel_manager_->media_engine()->GetVoE()));
781
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 return true;
783}
784
785void WebRtcSession::Terminate() {
786 SetState(STATE_RECEIVEDTERMINATE);
deadbeef9af63f42015-09-18 12:55:56 -0700787 RemoveUnusedChannels(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000788 ASSERT(!voice_channel_);
789 ASSERT(!video_channel_);
790 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791}
792
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000793void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
794 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795}
796
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000797cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
798 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799}
800
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000801bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000802 if (local_description() == NULL || remote_description() == NULL) {
803 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
804 << "SSL Role of the session.";
805 return false;
806 }
807
deadbeef9af63f42015-09-18 12:55:56 -0700808 return transport_controller()->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000809}
810
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000811void WebRtcSession::CreateOffer(
812 CreateSessionDescriptionObserver* observer,
813 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
814 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000815}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816
wu@webrtc.org91053e72013-08-10 07:18:04 +0000817void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
818 const MediaConstraintsInterface* constraints) {
819 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820}
821
822bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
823 std::string* err_desc) {
deadbeef9af63f42015-09-18 12:55:56 -0700824 ASSERT(signaling_thread()->IsCurrent());
825
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000826 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000827 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000828
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000829 // Validate SDP.
830 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
831 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833
834 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000835 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 if (state() == STATE_INIT && action == kOffer) {
837 set_initiator(true);
838 }
839
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000840 cricket::SecurePolicy sdes_policy =
841 webrtc_session_desc_factory_->SdesPolicy();
842 cricket::CryptoType crypto_required = dtls_enabled_ ?
843 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
844 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000846 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847
848 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000849 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850
851 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000852 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 // TODO(mallinath) - Handle CreateChannel failure, as new local description
854 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000855 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857
deadbeef9af63f42015-09-18 12:55:56 -0700858 // Remove unused channels if MediaContentDescription is rejected.
859 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000861 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 return false;
863 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000864
deadbeef9af63f42015-09-18 12:55:56 -0700865 if (remote_description()) {
866 // Now that we have a local description, we can push down remote candidates
867 // that we stored, and those from the remote description.
868 if (!saved_candidates_.empty()) {
869 // If there are saved candidates which arrived before the local
870 // description was set, copy those to the remote description.
871 CopySavedCandidates(remote_desc_.get());
872 }
873 // Push remote candidates in remote description to transport channels.
874 UseCandidatesInSessionDescription(remote_desc_.get());
875 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876
877 // Update state and SSRC of local MediaStreams and DataChannels based on the
878 // local session description.
879 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
880
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000881 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000882 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
883 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000886 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
888 return true;
889}
890
891bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
892 std::string* err_desc) {
deadbeef9af63f42015-09-18 12:55:56 -0700893 ASSERT(signaling_thread()->IsCurrent());
894
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000895 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000896 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000897
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000898 // Validate SDP.
899 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
900 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902
903 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000904 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 if (action == kOffer && !CreateChannels(desc->description())) {
906 // TODO(mallinath) - Handle CreateChannel failure, as new local description
907 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000908 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910
deadbeef9af63f42015-09-18 12:55:56 -0700911 // Remove unused channels if MediaContentDescription is rejected.
912 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913
914 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
915 // is called.
916 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000917 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 return false;
919 }
920
921 // Update remote MediaStreams.
922 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
923 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000924 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
927 // Copy all saved candidates.
928 CopySavedCandidates(desc);
honghaiz503726c2015-07-31 12:37:38 -0700929
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 // Check if this new SessionDescription contains new ice ufrag and password
931 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700932 bool ice_restart =
933 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(), desc);
934 // We retain all received candidates only if ICE is not restarted.
935 // When ICE is restarted, all previous candidates belong to an old generation
936 // and should not be kept.
937 if (!ice_restart) {
938 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
939 remote_desc_.get(), desc);
940 }
941
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000942 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000943
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000944 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000945 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
946 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
947 }
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000950 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000952
953 // Set the the ICE connection state to connecting since the connection may
954 // become writable with peer reflexive candidates before any remote candidate
955 // is signaled.
956 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
957 // is to have a new signal the indicates a change in checking state from the
958 // transport and expose a new checking() member from transport that can be
959 // read to determine the current checking state. The existing SignalConnecting
960 // actually means "gathering candidates", so cannot be be used here.
961 if (desc->type() != SessionDescriptionInterface::kOffer &&
962 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
963 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
964 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 return true;
966}
967
968bool WebRtcSession::UpdateSessionState(
969 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 std::string* err_desc) {
deadbeef9af63f42015-09-18 12:55:56 -0700971 ASSERT(signaling_thread()->IsCurrent());
972
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 // If there's already a pending error then no state transition should happen.
974 // But all call-sites should be verifying this before calling us!
975 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000976 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
979 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 }
981 SetState(source == cricket::CS_LOCAL ?
982 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000983 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
984 SetError(BaseSession::ERROR_CONTENT, *err_desc);
985 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000987 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 }
989 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000990 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
991 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 }
993 EnableChannels();
994 SetState(source == cricket::CS_LOCAL ?
995 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000996 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
997 SetError(BaseSession::ERROR_CONTENT, *err_desc);
998 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001000 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 }
1002 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001003 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
1004 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 }
deadbeef9af63f42015-09-18 12:55:56 -07001006 const cricket::ContentGroup* local_bundle =
1007 BaseSession::local_description()->GetGroupByName(
1008 cricket::GROUP_TYPE_BUNDLE);
1009 const cricket::ContentGroup* remote_bundle =
1010 BaseSession::remote_description()->GetGroupByName(
1011 cricket::GROUP_TYPE_BUNDLE);
1012 if (local_bundle && remote_bundle) {
1013 // The answerer decides the transport to bundle on
1014 const cricket::ContentGroup* answer_bundle =
1015 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
1016 if (!EnableBundle(*answer_bundle)) {
1017 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
1018 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
1019 }
1020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 EnableChannels();
1022 SetState(source == cricket::CS_LOCAL ?
1023 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001024 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
1025 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001028 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 }
1030 }
1031 return true;
1032}
1033
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001034bool WebRtcSession::PushdownMediaDescription(
1035 cricket::ContentAction action,
1036 cricket::ContentSource source,
1037 std::string* err) {
1038 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1039 if (!ch) {
1040 return true;
1041 } else if (source == cricket::CS_LOCAL) {
1042 return ch->PushdownLocalDescription(
1043 base_local_description(), action, err);
1044 } else {
1045 return ch->PushdownRemoteDescription(
1046 base_remote_description(), action, err);
1047 }
1048 };
1049
1050 return (set_content(voice_channel()) &&
1051 set_content(video_channel()) &&
1052 set_content(data_channel()));
1053}
1054
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1056 if (type == SessionDescriptionInterface::kOffer) {
1057 return WebRtcSession::kOffer;
1058 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1059 return WebRtcSession::kPrAnswer;
1060 } else if (type == SessionDescriptionInterface::kAnswer) {
1061 return WebRtcSession::kAnswer;
1062 }
1063 ASSERT(false && "unknown action type");
1064 return WebRtcSession::kOffer;
1065}
1066
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001067bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
1068 ASSERT(signaling_thread()->IsCurrent());
deadbeef9af63f42015-09-18 12:55:56 -07001069 return (GetChannelTransportStats(voice_channel(), stats) &&
1070 GetChannelTransportStats(video_channel(), stats) &&
1071 GetChannelTransportStats(data_channel(), stats));
1072}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001073
deadbeef9af63f42015-09-18 12:55:56 -07001074bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
1075 cricket::SessionStats* stats) {
1076 ASSERT(signaling_thread()->IsCurrent());
1077 if (!ch) {
1078 // Not using this channel.
1079 return true;
1080 }
1081
1082 const std::string& content_name = ch->content_name();
1083 const std::string& transport_name = ch->transport_name();
1084 stats->proxy_to_transport[content_name] = transport_name;
1085 if (stats->transport_stats.find(transport_name) !=
1086 stats->transport_stats.end()) {
1087 // Transport stats already done for this transport.
1088 return true;
1089 }
1090
1091 cricket::TransportStats tstats;
1092 if (!transport_controller()->GetStats(transport_name, &tstats)) {
1093 return false;
1094 }
1095
1096 stats->transport_stats[transport_name] = tstats;
1097 return true;
1098}
1099
1100bool WebRtcSession::GetLocalCertificate(
1101 const std::string& transport_name,
1102 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1103 ASSERT(signaling_thread()->IsCurrent());
1104 return transport_controller()->GetLocalCertificate(transport_name,
1105 certificate);
1106}
1107
1108bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1109 rtc::SSLCertificate** cert) {
1110 ASSERT(signaling_thread()->IsCurrent());
1111 return transport_controller()->GetRemoteSSLCertificate(transport_name, cert);
1112}
1113
1114cricket::BaseChannel* WebRtcSession::GetChannel(
1115 const std::string& content_name) {
1116 if (voice_channel() && voice_channel()->content_name() == content_name) {
1117 return voice_channel();
1118 }
1119 if (video_channel() && video_channel()->content_name() == content_name) {
1120 return video_channel();
1121 }
1122 if (data_channel() && data_channel()->content_name() == content_name) {
1123 return data_channel();
1124 }
1125 return nullptr;
1126}
1127
1128bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1129 const std::string* first_content_name = bundle.FirstContentName();
1130 if (!first_content_name) {
1131 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1132 return false;
1133 }
1134 const std::string& transport_name = *first_content_name;
1135 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1136
1137 auto maybe_set_transport = [this, bundle, transport_name,
1138 first_channel](cricket::BaseChannel* ch) {
1139 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001140 return true;
1141 }
1142
deadbeef9af63f42015-09-18 12:55:56 -07001143 if (ch->transport_name() == transport_name) {
1144 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1145 << " on " << transport_name << ".";
1146 return true;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001147 }
1148
deadbeef9af63f42015-09-18 12:55:56 -07001149 if (!ch->SetTransport(transport_name)) {
1150 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1151 return false;
1152 }
1153 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1154 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001155 return true;
1156 };
1157
deadbeef9af63f42015-09-18 12:55:56 -07001158 if (!maybe_set_transport(voice_channel()) ||
1159 !maybe_set_transport(video_channel()) ||
1160 !maybe_set_transport(data_channel())) {
1161 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001162 }
deadbeef9af63f42015-09-18 12:55:56 -07001163
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001164 return true;
1165}
1166
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
1168 if (state() == STATE_INIT) {
1169 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1170 << "without any offer (local or remote) "
1171 << "session description.";
1172 return false;
1173 }
1174
1175 if (!candidate) {
1176 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
1177 return false;
1178 }
1179
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001180 bool valid = false;
1181 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1182 if (valid) {
1183 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1184 saved_candidates_.push_back(
1185 new JsepIceCandidate(candidate->sdp_mid(),
1186 candidate->sdp_mline_index(),
1187 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001188 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001189 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 }
1191
1192 // Add this candidate to the remote session description.
1193 if (!remote_desc_->AddCandidate(candidate)) {
1194 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1195 return false;
1196 }
1197
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001198 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199}
1200
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001201bool WebRtcSession::SetIceTransports(
1202 PeerConnectionInterface::IceTransportsType type) {
1203 return port_allocator()->set_candidate_filter(
1204 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001205}
1206
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001207bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001208 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001210 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211}
1212
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001213bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001214 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001215 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001216 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217}
1218
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 return desc.str();
1223}
1224
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001225void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1226 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 ASSERT(signaling_thread()->IsCurrent());
1228 if (!voice_channel_) {
1229 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1230 return;
1231 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001232 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1233 // SetRenderer() can fail if the ssrc does not match any playout channel.
1234 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1235 return;
1236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1238 // Allow that SetOutputScaling fail if |enable| is false but assert
1239 // otherwise. This in the normal case when the underlying media channel has
1240 // already been deleted.
1241 ASSERT(enable == false);
1242 }
1243}
1244
1245void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001246 const cricket::AudioOptions& options,
1247 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 ASSERT(signaling_thread()->IsCurrent());
1249 if (!voice_channel_) {
1250 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1251 return;
1252 }
solenberg1dd98f32015-09-10 01:57:14 -07001253 if (!voice_channel_->SetAudioSend(ssrc, !enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001254 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256}
1257
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001258void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1259 ASSERT(signaling_thread()->IsCurrent());
1260 ASSERT(volume >= 0 && volume <= 10);
1261 if (!voice_channel_) {
1262 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1263 return;
1264 }
1265
solenbergbb741b32015-09-07 03:56:38 -07001266 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001267 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001268 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001269}
1270
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1272 cricket::VideoCapturer* camera) {
1273 ASSERT(signaling_thread()->IsCurrent());
1274
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001275 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1277 // support video.
1278 LOG(LS_WARNING) << "Video not used in this call.";
1279 return false;
1280 }
1281 if (!video_channel_->SetCapturer(ssrc, camera)) {
1282 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1283 // This in the normal case when the underlying media channel has already
1284 // been deleted.
1285 ASSERT(camera == NULL);
1286 return false;
1287 }
1288 return true;
1289}
1290
1291void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1292 bool enable,
1293 cricket::VideoRenderer* renderer) {
1294 ASSERT(signaling_thread()->IsCurrent());
1295 if (!video_channel_) {
1296 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1297 return;
1298 }
1299 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1300 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1301 // This in the normal case when the underlying media channel has already
1302 // been deleted.
1303 ASSERT(renderer == NULL);
1304 }
1305}
1306
1307void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1308 const cricket::VideoOptions* options) {
1309 ASSERT(signaling_thread()->IsCurrent());
1310 if (!video_channel_) {
1311 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1312 return;
1313 }
solenberg1dd98f32015-09-10 01:57:14 -07001314 if (!video_channel_->SetVideoSend(ssrc, !enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1316 // This in the normal case when the underlying media channel has already
1317 // been deleted.
1318 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320}
1321
1322bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1323 ASSERT(signaling_thread()->IsCurrent());
1324 if (!voice_channel_) {
1325 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1326 return false;
1327 }
1328 uint32 send_ssrc = 0;
1329 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1330 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001331 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 &send_ssrc)) {
1333 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1334 return false;
1335 }
1336 return voice_channel_->CanInsertDtmf();
1337}
1338
1339bool WebRtcSession::InsertDtmf(const std::string& track_id,
1340 int code, int duration) {
1341 ASSERT(signaling_thread()->IsCurrent());
1342 if (!voice_channel_) {
1343 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1344 return false;
1345 }
1346 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001347 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 track_id, &send_ssrc))) {
1349 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1350 return false;
1351 }
1352 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1353 cricket::DF_SEND)) {
1354 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1355 return false;
1356 }
1357 return true;
1358}
1359
1360sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1361 return &SignalVoiceChannelDestroyed;
1362}
1363
wu@webrtc.org78187522013-10-07 23:32:02 +00001364bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001365 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001366 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001367 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001368 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1369 return false;
1370 }
1371 return data_channel_->SendData(params, payload, result);
1372}
1373
1374bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001375 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001376 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1377 return false;
1378 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001379 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1380 &DataChannel::OnChannelReady);
1381 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1382 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001383 return true;
1384}
1385
1386void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001387 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001388 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1389 return;
1390 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001391 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1392 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1393}
1394
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001395void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001396 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001397 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1398 return;
1399 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001400 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1401 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001402}
1403
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001404void WebRtcSession::RemoveSctpDataStream(int sid) {
1405 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001406
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001407 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001408 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1409 << "NULL.";
1410 return;
1411 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001412 data_channel_->RemoveRecvStream(sid);
1413 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001414}
1415
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001416bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001417 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001418}
1419
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001420rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001421 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001422 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 if (state() == STATE_RECEIVEDTERMINATE) {
1424 return NULL;
1425 }
1426 if (data_channel_type_ == cricket::DCT_NONE) {
1427 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1428 return NULL;
1429 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001430 InternalDataChannelInit new_config =
1431 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 if (data_channel_type_ == cricket::DCT_SCTP) {
1433 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001434 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001435 if (GetSslRole(&role) &&
1436 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1438 return NULL;
1439 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001440 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1442 << "because the id is already in use or out of range.";
1443 return NULL;
1444 }
1445 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001446
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001447 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001448 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001449 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001451
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 return channel;
1453}
1454
1455cricket::DataChannelType WebRtcSession::data_channel_type() const {
1456 return data_channel_type_;
1457}
1458
wu@webrtc.org91053e72013-08-10 07:18:04 +00001459bool WebRtcSession::IceRestartPending() const {
1460 return ice_restart_latch_->Get();
1461}
1462
1463void WebRtcSession::ResetIceRestartLatch() {
1464 ice_restart_latch_->Reset();
1465}
1466
Henrik Boströmd8281982015-08-27 10:12:24 +02001467void WebRtcSession::OnCertificateReady(
1468 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeef9af63f42015-09-18 12:55:56 -07001469 transport_controller()->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001470}
1471
Henrik Boströmd8281982015-08-27 10:12:24 +02001472bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001473 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001474}
1475
deadbeef9af63f42015-09-18 12:55:56 -07001476const rtc::scoped_refptr<rtc::RTCCertificate>&
1477WebRtcSession::certificate_for_testing() {
1478 return transport_controller()->certificate_for_testing();
1479}
1480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481void WebRtcSession::SetIceConnectionState(
1482 PeerConnectionInterface::IceConnectionState state) {
1483 if (ice_connection_state_ == state) {
1484 return;
1485 }
1486
1487 // ASSERT that the requested transition is allowed. Note that
1488 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1489 // within PeerConnection). This switch statement should compile away when
1490 // ASSERTs are disabled.
deadbeef9af63f42015-09-18 12:55:56 -07001491 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1492 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 switch (ice_connection_state_) {
1494 case PeerConnectionInterface::kIceConnectionNew:
1495 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1496 break;
1497 case PeerConnectionInterface::kIceConnectionChecking:
1498 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1499 state == PeerConnectionInterface::kIceConnectionConnected);
1500 break;
1501 case PeerConnectionInterface::kIceConnectionConnected:
1502 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1503 state == PeerConnectionInterface::kIceConnectionChecking ||
1504 state == PeerConnectionInterface::kIceConnectionCompleted);
1505 break;
1506 case PeerConnectionInterface::kIceConnectionCompleted:
1507 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1508 state == PeerConnectionInterface::kIceConnectionDisconnected);
1509 break;
1510 case PeerConnectionInterface::kIceConnectionFailed:
1511 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1512 break;
1513 case PeerConnectionInterface::kIceConnectionDisconnected:
1514 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1515 state == PeerConnectionInterface::kIceConnectionConnected ||
1516 state == PeerConnectionInterface::kIceConnectionCompleted ||
1517 state == PeerConnectionInterface::kIceConnectionFailed);
1518 break;
1519 case PeerConnectionInterface::kIceConnectionClosed:
1520 ASSERT(false);
1521 break;
1522 default:
1523 ASSERT(false);
1524 break;
1525 }
1526
1527 ice_connection_state_ = state;
1528 if (ice_observer_) {
1529 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1530 }
1531}
1532
deadbeef9af63f42015-09-18 12:55:56 -07001533void WebRtcSession::OnTransportControllerConnectionState(
1534 cricket::IceConnectionState state) {
1535 switch (state) {
1536 case cricket::kIceConnectionConnecting:
1537 // If the current state is Connected or Completed, then there were
1538 // writable channels but now there are not, so the next state must
1539 // be Disconnected.
1540 // kIceConnectionConnecting is currently used as the default,
1541 // un-connected state by the TransportController, so its only use is
1542 // detecting disconnections.
1543 if (ice_connection_state_ ==
1544 PeerConnectionInterface::kIceConnectionConnected ||
1545 ice_connection_state_ ==
1546 PeerConnectionInterface::kIceConnectionCompleted) {
1547 SetIceConnectionState(
1548 PeerConnectionInterface::kIceConnectionDisconnected);
1549 }
Peter Thatcher54360512015-07-08 11:08:35 -07001550 break;
deadbeef9af63f42015-09-18 12:55:56 -07001551 case cricket::kIceConnectionFailed:
1552 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1553 break;
1554 case cricket::kIceConnectionConnected:
1555 LOG(LS_INFO) << "Changing to ICE connected state because "
1556 << "all transports are writable.";
1557 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1558 break;
1559 case cricket::kIceConnectionCompleted:
1560 LOG(LS_INFO) << "Changing to ICE completed state because "
1561 << "all transports are complete.";
1562 if (ice_connection_state_ !=
1563 PeerConnectionInterface::kIceConnectionConnected) {
1564 // If jumping directly from "checking" to "connected",
1565 // signal "connected" first.
1566 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1567 }
1568 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1569 if (metrics_observer_) {
1570 ReportTransportStats();
1571 }
1572 break;
1573 default:
1574 ASSERT(false);
Peter Thatcher54360512015-07-08 11:08:35 -07001575 }
deadbeef9af63f42015-09-18 12:55:56 -07001576}
1577
1578void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001579 SetIceConnectionReceiving(receiving);
1580}
1581
1582void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1583 if (ice_connection_receiving_ == receiving) {
1584 return;
1585 }
1586 ice_connection_receiving_ = receiving;
1587 if (ice_observer_) {
1588 ice_observer_->OnIceConnectionReceivingChange(receiving);
1589 }
1590}
1591
deadbeef9af63f42015-09-18 12:55:56 -07001592void WebRtcSession::OnTransportControllerCandidatesGathered(
1593 const std::string& transport_name,
1594 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 ASSERT(signaling_thread()->IsCurrent());
deadbeef9af63f42015-09-18 12:55:56 -07001596 int sdp_mline_index;
1597 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1598 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1599 << transport_name << " not found";
1600 return;
1601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
deadbeef9af63f42015-09-18 12:55:56 -07001603 for (cricket::Candidates::const_iterator citer = candidates.begin();
1604 citer != candidates.end(); ++citer) {
1605 // Use transport_name as the candidate media id.
1606 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1607 if (ice_observer_) {
1608 ice_observer_->OnIceCandidate(&candidate);
1609 }
1610 if (local_desc_) {
1611 local_desc_->AddCandidate(&candidate);
1612 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 }
1614}
1615
1616// Enabling voice and video channel.
1617void WebRtcSession::EnableChannels() {
1618 if (voice_channel_ && !voice_channel_->enabled())
1619 voice_channel_->Enable(true);
1620
1621 if (video_channel_ && !video_channel_->enabled())
1622 video_channel_->Enable(true);
1623
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001624 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 data_channel_->Enable(true);
1626}
1627
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628// Returns the media index for a local ice candidate given the content name.
1629bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1630 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001631 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 return false;
1633
1634 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001635 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 for (size_t index = 0; index < contents.size(); ++index) {
1637 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001638 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 content_found = true;
1640 break;
1641 }
1642 }
1643 return content_found;
1644}
1645
1646bool WebRtcSession::UseCandidatesInSessionDescription(
1647 const SessionDescriptionInterface* remote_desc) {
1648 if (!remote_desc)
1649 return true;
1650 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001651
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1653 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1654 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001655 const IceCandidateInterface* candidate = candidates->at(n);
1656 bool valid = false;
1657 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1658 if (valid) {
1659 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1660 saved_candidates_.push_back(
1661 new JsepIceCandidate(candidate->sdp_mid(),
1662 candidate->sdp_mline_index(),
1663 candidate->candidate()));
1664 }
1665 continue;
1666 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001667 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 if (!ret)
1669 break;
1670 }
1671 }
1672 return ret;
1673}
1674
1675bool WebRtcSession::UseCandidate(
1676 const IceCandidateInterface* candidate) {
1677
1678 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001679 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 if (mediacontent_index >= remote_content_size) {
1681 LOG(LS_ERROR)
1682 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1683 return false;
1684 }
1685
1686 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001687 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 std::vector<cricket::Candidate> candidates;
1689 candidates.push_back(candidate->candidate());
1690 // Invoking BaseSession method to handle remote candidates.
1691 std::string error;
deadbeef9af63f42015-09-18 12:55:56 -07001692 if (transport_controller()->AddRemoteCandidates(content.name, candidates,
1693 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 // Candidates successfully submitted for checking.
1695 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1696 ice_connection_state_ ==
1697 PeerConnectionInterface::kIceConnectionDisconnected) {
1698 // If state is New, then the session has just gotten its first remote ICE
1699 // candidates, so go to Checking.
1700 // If state is Disconnected, the session is re-using old candidates or
1701 // receiving additional ones, so go to Checking.
1702 // If state is Connected, stay Connected.
1703 // TODO(bemasc): If state is Connected, and the new candidates are for a
1704 // newly added transport, then the state actually _should_ move to
1705 // checking. Add a way to distinguish that case.
1706 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1707 }
1708 // TODO(bemasc): If state is Completed, go back to Connected.
1709 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001710 if (!error.empty()) {
1711 LOG(LS_WARNING) << error;
1712 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 }
1714 return true;
1715}
1716
deadbeef9af63f42015-09-18 12:55:56 -07001717void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001718 // Destroy video_channel_ first since it may have a pointer to the
1719 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 const cricket::ContentInfo* video_info =
1721 cricket::GetFirstVideoContent(desc);
1722 if ((!video_info || video_info->rejected) && video_channel_) {
1723 mediastream_signaling_->OnVideoChannelClose();
1724 SignalVideoChannelDestroyed();
1725 const std::string content_name = video_channel_->content_name();
1726 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 }
1728
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001729 const cricket::ContentInfo* voice_info =
1730 cricket::GetFirstAudioContent(desc);
1731 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1732 mediastream_signaling_->OnAudioChannelClose();
1733 SignalVoiceChannelDestroyed();
1734 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001735 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001736 }
1737
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 const cricket::ContentInfo* data_info =
1739 cricket::GetFirstDataContent(desc);
1740 if ((!data_info || data_info->rejected) && data_channel_) {
1741 mediastream_signaling_->OnDataChannelClose();
1742 SignalDataChannelDestroyed();
1743 const std::string content_name = data_channel_->content_name();
1744 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 }
1746}
1747
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001748// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001749// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 // Creating the media channels and transport proxies.
1752 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1753 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001754 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 LOG(LS_ERROR) << "Failed to create voice channel.";
1756 return false;
1757 }
1758 }
1759
1760 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1761 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001762 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 LOG(LS_ERROR) << "Failed to create video channel.";
1764 return false;
1765 }
1766 }
1767
1768 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1769 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001770 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001771 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 LOG(LS_ERROR) << "Failed to create data channel.";
1773 return false;
1774 }
1775 }
1776
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001777 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1778 if (voice_channel()) {
1779 voice_channel()->ActivateRtcpMux();
1780 }
1781 if (video_channel()) {
1782 video_channel()->ActivateRtcpMux();
1783 }
1784 if (data_channel()) {
1785 data_channel()->ActivateRtcpMux();
1786 }
1787 }
1788
deadbeef9af63f42015-09-18 12:55:56 -07001789 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001790 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001791 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1792 cricket::GROUP_TYPE_BUNDLE);
1793 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001794 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1795 return false;
1796 }
deadbeef9af63f42015-09-18 12:55:56 -07001797 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001798 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1799 return false;
1800 }
1801 }
1802
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 return true;
1804}
1805
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001806bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
deadbeef9af63f42015-09-18 12:55:56 -07001808 media_controller_.get(), transport_controller(), content->name, true,
1809 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001810 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001811 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001812 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001813
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001814 voice_channel_->SignalDtlsSetupFailure.connect(
1815 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001816 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817}
1818
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001819bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 video_channel_.reset(channel_manager_->CreateVideoChannel(
deadbeef9af63f42015-09-18 12:55:56 -07001821 media_controller_.get(), transport_controller(), content->name, true,
1822 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001823 if (!video_channel_) {
1824 return false;
1825 }
1826
1827 video_channel_->SignalDtlsSetupFailure.connect(
1828 this, &WebRtcSession::OnDtlsSetupFailure);
1829 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830}
1831
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001832bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001833 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeef9af63f42015-09-18 12:55:56 -07001835 transport_controller(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001836 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001837 return false;
1838 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001839
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001840 if (sctp) {
1841 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001842 data_channel_->SignalDataReceived.connect(
1843 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001844 data_channel_->SignalStreamClosedRemotely.connect(
1845 mediastream_signaling_,
1846 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001847 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001848
1849 data_channel_->SignalDtlsSetupFailure.connect(
1850 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001851 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852}
1853
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001854void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1855 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1856 kDtlsSetupFailureRtp);
1857}
1858
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859void WebRtcSession::CopySavedCandidates(
1860 SessionDescriptionInterface* dest_desc) {
1861 if (!dest_desc) {
1862 ASSERT(false);
1863 return;
1864 }
1865 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1866 dest_desc->AddCandidate(saved_candidates_[i]);
1867 delete saved_candidates_[i];
1868 }
1869 saved_candidates_.clear();
1870}
1871
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001872void WebRtcSession::OnDataChannelMessageReceived(
1873 cricket::DataChannel* channel,
1874 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001875 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001876 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001877 if (params.type == cricket::DMT_CONTROL &&
1878 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1879 // Received CONTROL on unused sid, process as an OPEN message.
1880 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001882 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883}
1884
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001885// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001886bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001887 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1888 if (!bundle_enabled)
1889 return true;
1890
1891 const cricket::ContentGroup* bundle_group =
1892 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1893 ASSERT(bundle_group != NULL);
1894
1895 const cricket::ContentInfos& contents = desc->contents();
1896 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1897 citer != contents.end(); ++citer) {
1898 const cricket::ContentInfo* content = (&*citer);
1899 ASSERT(content != NULL);
1900 if (bundle_group->HasContentName(content->name) &&
1901 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1902 if (!HasRtcpMuxEnabled(content))
1903 return false;
1904 }
1905 }
1906 // RTCP-MUX is enabled in all the contents.
1907 return true;
1908}
1909
1910bool WebRtcSession::HasRtcpMuxEnabled(
1911 const cricket::ContentInfo* content) {
1912 const cricket::MediaContentDescription* description =
1913 static_cast<cricket::MediaContentDescription*>(content->description);
1914 return description->rtcp_mux();
1915}
1916
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001917bool WebRtcSession::ValidateSessionDescription(
1918 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001919 cricket::ContentSource source, std::string* err_desc) {
1920 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001921 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001922 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001923 }
1924
1925 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001927 }
1928
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001929 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001930 Action action = GetAction(sdesc->type());
1931 if (source == cricket::CS_LOCAL) {
1932 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001933 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001934 } else {
1935 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937 }
1938
1939 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001940 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001941 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1942 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001943 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001944 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001945 }
1946
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001947 // Verify ice-ufrag and ice-pwd.
1948 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001949 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001950 }
1951
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001952 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001953 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954 }
1955
1956 // Verify m-lines in Answer when compared against Offer.
1957 if (action == kAnswer) {
1958 const cricket::SessionDescription* offer_desc =
1959 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1960 local_description()->description();
1961 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001962 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001963 }
1964 }
1965
1966 return true;
1967}
1968
1969bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1970 return ((action == kOffer && state() == STATE_INIT) ||
1971 // update local offer
1972 (action == kOffer && state() == STATE_SENTINITIATE) ||
1973 // update the current ongoing session.
1974 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1975 (action == kOffer && state() == STATE_SENTACCEPT) ||
1976 (action == kOffer && state() == STATE_INPROGRESS) ||
1977 // accept remote offer
1978 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1979 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1980 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1981 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1982}
1983
1984bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1985 return ((action == kOffer && state() == STATE_INIT) ||
1986 // update remote offer
1987 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1988 // update the current ongoing session
1989 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1990 (action == kOffer && state() == STATE_SENTACCEPT) ||
1991 (action == kOffer && state() == STATE_INPROGRESS) ||
1992 // accept local offer
1993 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1994 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1995 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1996 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1997}
1998
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001999std::string WebRtcSession::GetSessionErrorMsg() {
2000 std::ostringstream desc;
2001 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2002 desc << kSessionErrorDesc << error_desc() << ".";
2003 return desc.str();
2004}
2005
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002006// We need to check the local/remote description for the Transport instead of
2007// the session, because a new Transport added during renegotiation may have
2008// them unset while the session has them set from the previous negotiation.
2009// Not doing so may trigger the auto generation of transport description and
2010// mess up DTLS identity information, ICE credential, etc.
2011bool WebRtcSession::ReadyToUseRemoteCandidate(
2012 const IceCandidateInterface* candidate,
2013 const SessionDescriptionInterface* remote_desc,
2014 bool* valid) {
2015 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002016
2017 const SessionDescriptionInterface* current_remote_desc =
2018 remote_desc ? remote_desc : remote_description();
2019
2020 if (!current_remote_desc)
2021 return false;
2022
2023 size_t mediacontent_index =
2024 static_cast<size_t>(candidate->sdp_mline_index());
2025 size_t remote_content_size =
2026 current_remote_desc->description()->contents().size();
2027 if (mediacontent_index >= remote_content_size) {
2028 LOG(LS_ERROR)
2029 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2030
2031 *valid = false;
2032 return false;
2033 }
2034
2035 cricket::ContentInfo content =
2036 current_remote_desc->description()->contents()[mediacontent_index];
deadbeef9af63f42015-09-18 12:55:56 -07002037 cricket::BaseChannel* channel = GetChannel(content.name);
2038 if (!channel) {
2039 return false;
2040 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002041
deadbeef9af63f42015-09-18 12:55:56 -07002042 return transport_controller()->ReadyForRemoteCandidates(
2043 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002044}
2045
deadbeef9af63f42015-09-18 12:55:56 -07002046void WebRtcSession::OnTransportControllerGatheringState(
2047 cricket::IceGatheringState state) {
2048 ASSERT(signaling_thread()->IsCurrent());
2049 if (state == cricket::kIceGatheringGathering) {
2050 if (ice_observer_) {
2051 ice_observer_->OnIceGatheringChange(
2052 PeerConnectionInterface::kIceGatheringGathering);
2053 }
2054 } else if (state == cricket::kIceGatheringComplete) {
2055 if (ice_observer_) {
2056 ice_observer_->OnIceGatheringChange(
2057 PeerConnectionInterface::kIceGatheringComplete);
2058 ice_observer_->OnIceComplete();
2059 }
2060 }
2061}
2062
2063void WebRtcSession::ReportTransportStats() {
2064 // Use a set so we don't report the same stats twice if two channels share
2065 // a transport.
2066 std::set<std::string> transport_names;
2067 if (voice_channel()) {
2068 transport_names.insert(voice_channel()->transport_name());
2069 }
2070 if (video_channel()) {
2071 transport_names.insert(video_channel()->transport_name());
2072 }
2073 if (data_channel()) {
2074 transport_names.insert(data_channel()->transport_name());
2075 }
2076 for (const auto& name : transport_names) {
2077 cricket::TransportStats stats;
2078 if (transport_controller()->GetStats(name, &stats)) {
2079 ReportBestConnectionState(stats);
2080 ReportNegotiatedCiphers(stats);
2081 }
2082 }
2083}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002084// Walk through the ConnectionInfos to gather best connection usage
2085// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002086void WebRtcSession::ReportBestConnectionState(
2087 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002088 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002089 for (cricket::TransportChannelStatsList::const_iterator it =
2090 stats.channel_stats.begin();
2091 it != stats.channel_stats.end(); ++it) {
2092 for (cricket::ConnectionInfos::const_iterator it_info =
2093 it->connection_infos.begin();
2094 it_info != it->connection_infos.end(); ++it_info) {
2095 if (!it_info->best_connection) {
2096 continue;
2097 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002098
2099 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2100 const cricket::Candidate& local = it_info->local_candidate;
2101 const cricket::Candidate& remote = it_info->remote_candidate;
2102
2103 // Increment the counter for IceCandidatePairType.
2104 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2105 (local.type() == RELAY_PORT_TYPE &&
2106 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2107 type = kEnumCounterIceCandidatePairTypeTcp;
2108 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2109 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002110 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002111 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002112 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002113 metrics_observer_->IncrementEnumCounter(
2114 type, GetIceCandidatePairCounter(local, remote),
2115 kIceCandidatePairMax);
2116
2117 // Increment the counter for IP type.
2118 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002119 metrics_observer_->IncrementEnumCounter(
2120 kEnumCounterAddressFamily, kBestConnections_IPv4,
2121 kPeerConnectionAddressFamilyCounter_Max);
2122
2123 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002124 metrics_observer_->IncrementEnumCounter(
2125 kEnumCounterAddressFamily, kBestConnections_IPv6,
2126 kPeerConnectionAddressFamilyCounter_Max);
2127 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002128 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002129 }
2130
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002131 return;
2132 }
2133 }
2134}
2135
jbauchac8869e2015-07-03 01:36:14 -07002136void WebRtcSession::ReportNegotiatedCiphers(
2137 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002138 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002139 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2140 return;
2141 }
2142
2143 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
2144 const std::string& ssl_cipher = stats.channel_stats[0].ssl_cipher;
2145 if (srtp_cipher.empty() && ssl_cipher.empty()) {
2146 return;
2147 }
2148
2149 PeerConnectionMetricsName srtp_name;
2150 PeerConnectionMetricsName ssl_name;
deadbeef9af63f42015-09-18 12:55:56 -07002151 if (stats.transport_name == cricket::CN_AUDIO) {
jbauchac8869e2015-07-03 01:36:14 -07002152 srtp_name = kAudioSrtpCipher;
2153 ssl_name = kAudioSslCipher;
deadbeef9af63f42015-09-18 12:55:56 -07002154 } else if (stats.transport_name == cricket::CN_VIDEO) {
jbauchac8869e2015-07-03 01:36:14 -07002155 srtp_name = kVideoSrtpCipher;
2156 ssl_name = kVideoSslCipher;
deadbeef9af63f42015-09-18 12:55:56 -07002157 } else if (stats.transport_name == cricket::CN_DATA) {
jbauchac8869e2015-07-03 01:36:14 -07002158 srtp_name = kDataSrtpCipher;
2159 ssl_name = kDataSslCipher;
2160 } else {
2161 RTC_NOTREACHED();
2162 return;
2163 }
2164
2165 if (!srtp_cipher.empty()) {
2166 metrics_observer_->AddHistogramSample(srtp_name, srtp_cipher);
2167 }
2168 if (!ssl_cipher.empty()) {
2169 metrics_observer_->AddHistogramSample(ssl_name, ssl_cipher);
2170 }
2171}
2172
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173} // namespace webrtc