blob: f17dd34b75bc794455dea0f1e7e3b59a591e675d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070034#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/app/webrtc/mediaconstraintsinterface.h"
39#include "talk/app/webrtc/mediastreamsignaling.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070041#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000042#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/base/constants.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/session/media/channel.h"
46#include "talk/session/media/channelmanager.h"
47#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070049#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000054#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56using cricket::ContentInfo;
57using cricket::ContentInfos;
58using cricket::MediaContentDescription;
59using cricket::SessionDescription;
60using cricket::TransportInfo;
61
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070062using cricket::LOCAL_PORT_TYPE;
63using cricket::STUN_PORT_TYPE;
64using cricket::RELAY_PORT_TYPE;
65using cricket::PRFLX_PORT_TYPE;
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067namespace webrtc {
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000070const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
71 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073const char kInvalidCandidates[] = "Description contains invalid candidates.";
74const char kInvalidSdp[] = "Invalid session description.";
75const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000076 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
77const char kPushDownTDFailed[] =
78 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000079const char kSdpWithoutDtlsFingerprint[] =
80 "Called with SDP without DTLS fingerprint.";
81const char kSdpWithoutSdesCrypto[] =
82 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000083const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000084 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000086const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000087const char kDtlsSetupFailureRtp[] =
88 "Couldn't set up DTLS-SRTP on RTP channel.";
89const char kDtlsSetupFailureRtcp[] =
90 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070091const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000092const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070094IceCandidatePairType GetIceCandidatePairCounter(
95 const cricket::Candidate& local,
96 const cricket::Candidate& remote) {
97 const auto& l = local.type();
98 const auto& r = remote.type();
99 const auto& host = LOCAL_PORT_TYPE;
100 const auto& srflx = STUN_PORT_TYPE;
101 const auto& relay = RELAY_PORT_TYPE;
102 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700103 if (l == host && r == host) {
104 bool local_private = IPIsPrivate(local.address().ipaddr());
105 bool remote_private = IPIsPrivate(remote.address().ipaddr());
106 if (local_private) {
107 if (remote_private) {
108 return kIceCandidatePairHostPrivateHostPrivate;
109 } else {
110 return kIceCandidatePairHostPrivateHostPublic;
111 }
112 } else {
113 if (remote_private) {
114 return kIceCandidatePairHostPublicHostPrivate;
115 } else {
116 return kIceCandidatePairHostPublicHostPublic;
117 }
118 }
119 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700120 if (l == host && r == srflx)
121 return kIceCandidatePairHostSrflx;
122 if (l == host && r == relay)
123 return kIceCandidatePairHostRelay;
124 if (l == host && r == prflx)
125 return kIceCandidatePairHostPrflx;
126 if (l == srflx && r == host)
127 return kIceCandidatePairSrflxHost;
128 if (l == srflx && r == srflx)
129 return kIceCandidatePairSrflxSrflx;
130 if (l == srflx && r == relay)
131 return kIceCandidatePairSrflxRelay;
132 if (l == srflx && r == prflx)
133 return kIceCandidatePairSrflxPrflx;
134 if (l == relay && r == host)
135 return kIceCandidatePairRelayHost;
136 if (l == relay && r == srflx)
137 return kIceCandidatePairRelaySrflx;
138 if (l == relay && r == relay)
139 return kIceCandidatePairRelayRelay;
140 if (l == relay && r == prflx)
141 return kIceCandidatePairRelayPrflx;
142 if (l == prflx && r == host)
143 return kIceCandidatePairPrflxHost;
144 if (l == prflx && r == srflx)
145 return kIceCandidatePairPrflxSrflx;
146 if (l == prflx && r == relay)
147 return kIceCandidatePairPrflxRelay;
148 return kIceCandidatePairMax;
149}
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151// Compares |answer| against |offer|. Comparision is done
152// for number of m-lines in answer against offer. If matches true will be
153// returned otherwise false.
154static bool VerifyMediaDescriptions(
155 const SessionDescription* answer, const SessionDescription* offer) {
156 if (offer->contents().size() != answer->contents().size())
157 return false;
158
159 for (size_t i = 0; i < offer->contents().size(); ++i) {
160 if ((offer->contents()[i].name) != answer->contents()[i].name) {
161 return false;
162 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000163 const MediaContentDescription* offer_mdesc =
164 static_cast<const MediaContentDescription*>(
165 offer->contents()[i].description);
166 const MediaContentDescription* answer_mdesc =
167 static_cast<const MediaContentDescription*>(
168 answer->contents()[i].description);
169 if (offer_mdesc->type() != answer_mdesc->type()) {
170 return false;
171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 }
173 return true;
174}
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176// Checks that each non-rejected content has SDES crypto keys or a DTLS
177// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
178// keys, will be caught in Transport negotiation, and backstopped by Channel's
179// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000180static bool VerifyCrypto(const SessionDescription* desc,
181 bool dtls_enabled,
182 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 const ContentInfos& contents = desc->contents();
184 for (size_t index = 0; index < contents.size(); ++index) {
185 const ContentInfo* cinfo = &contents[index];
186 if (cinfo->rejected) {
187 continue;
188 }
189
190 // If the content isn't rejected, crypto must be present.
191 const MediaContentDescription* media =
192 static_cast<const MediaContentDescription*>(cinfo->description);
193 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
194 if (!media || !tinfo) {
195 // Something is not right.
196 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000197 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return false;
199 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000200 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000201 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 LOG(LS_WARNING) <<
203 "Session description must have DTLS fingerprint if DTLS enabled.";
204 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000205 return false;
206 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000207 } else {
208 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 LOG(LS_WARNING) <<
210 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000211 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000212 return false;
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215 }
216
217 return true;
218}
219
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000220// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
221static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
222 const ContentInfos& contents = desc->contents();
223 for (size_t index = 0; index < contents.size(); ++index) {
224 const ContentInfo* cinfo = &contents[index];
225 if (cinfo->rejected) {
226 continue;
227 }
228
229 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
230 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
231 if (!tinfo) {
232 // Something is not right.
233 LOG(LS_ERROR) << kInvalidSdp;
234 return false;
235 }
236 if (tinfo->description.ice_ufrag.empty() ||
237 tinfo->description.ice_pwd.empty()) {
238 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
239 return false;
240 }
241 }
242 return true;
243}
244
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245// Forces |sdesc->crypto_required| to the appropriate state based on the
246// current security policy, to ensure a failure occurs if there is an error
247// in crypto negotiation.
248// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000249static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
250 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000251 if (!sdesc) {
252 return;
253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254
wu@webrtc.org91053e72013-08-10 07:18:04 +0000255 // Updating the |crypto_required_| in MediaContentDescription to the
256 // appropriate state based on the current security policy.
257 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
258 iter != sdesc->contents().end(); ++iter) {
259 if (cricket::IsMediaContent(&*iter)) {
260 MediaContentDescription* mdesc =
261 static_cast<MediaContentDescription*> (iter->description);
262 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000263 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000264 }
265 }
266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267}
268
Peter Boström0c4e06b2015-10-07 12:23:21 +0200269static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
270 const std::string& track_id,
271 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 const cricket::ContentInfo* audio_info =
273 cricket::GetFirstAudioContent(session_description);
274 if (!audio_info) {
275 LOG(LS_ERROR) << "Audio not used in this call";
276 return false;
277 }
278
279 const cricket::MediaContentDescription* audio_content =
280 static_cast<const cricket::MediaContentDescription*>(
281 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000282 const cricket::StreamParams* stream =
283 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
284 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 return false;
286 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000287
288 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 return true;
290}
291
292static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200293 uint32_t ssrc,
294 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 ASSERT(track_id != NULL);
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 const cricket::ContentInfo* audio_info =
298 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000299 if (audio_info) {
300 const cricket::MediaContentDescription* audio_content =
301 static_cast<const cricket::MediaContentDescription*>(
302 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const auto* found =
305 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
306 if (found) {
307 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000308 return true;
309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311
312 const cricket::ContentInfo* video_info =
313 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000314 if (video_info) {
315 const cricket::MediaContentDescription* video_content =
316 static_cast<const cricket::MediaContentDescription*>(
317 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000319 const auto* found =
320 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
321 if (found) {
322 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000323 return true;
324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 }
326 return false;
327}
328
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000329static bool BadSdp(const std::string& source,
330 const std::string& type,
331 const std::string& reason,
332 std::string* err_desc) {
333 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700334 desc << "Failed to set " << source;
335 if (!type.empty()) {
336 desc << " " << type;
337 }
338 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000339
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 return false;
345}
346
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000348 const std::string& type,
349 const std::string& reason,
350 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000352 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 }
356}
357
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000358static bool BadLocalSdp(const std::string& type,
359 const std::string& reason,
360 std::string* err_desc) {
361 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
362}
363
364static bool BadRemoteSdp(const std::string& type,
365 const std::string& reason,
366 std::string* err_desc) {
367 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
368}
369
370static bool BadOfferSdp(cricket::ContentSource source,
371 const std::string& reason,
372 std::string* err_desc) {
373 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
374}
375
376static bool BadPranswerSdp(cricket::ContentSource source,
377 const std::string& reason,
378 std::string* err_desc) {
379 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
380 reason, err_desc);
381}
382
383static bool BadAnswerSdp(cricket::ContentSource source,
384 const std::string& reason,
385 std::string* err_desc) {
386 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
deadbeefd59daf82015-10-14 15:02:44 -0700389#define GET_STRING_OF_STATE(state) \
390 case webrtc::WebRtcSession::state: \
391 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 break;
393
deadbeefd59daf82015-10-14 15:02:44 -0700394static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 std::string result;
396 switch (state) {
397 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700398 GET_STRING_OF_STATE(STATE_SENTOFFER)
399 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
400 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700403 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 default:
405 ASSERT(false);
406 break;
407 }
408 return result;
409}
410
deadbeefd59daf82015-10-14 15:02:44 -0700411#define GET_STRING_OF_ERROR_CODE(err) \
412 case webrtc::WebRtcSession::err: \
413 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 break;
415
deadbeefd59daf82015-10-14 15:02:44 -0700416static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 std::string result;
418 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
421 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 default:
deadbeefd59daf82015-10-14 15:02:44 -0700423 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 break;
425 }
426 return result;
427}
428
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000429static std::string MakeErrorString(const std::string& error,
430 const std::string& desc) {
431 std::ostringstream ret;
432 ret << error << " " << desc;
433 return ret.str();
434}
435
436static std::string MakeTdErrorString(const std::string& desc) {
437 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438}
439
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000440// Set |option| to the highest-priority value of |key| in the optional
441// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000442template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000443static void SetOptionFromOptionalConstraint(
444 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000445 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000446 if (!constraints) {
447 return;
448 }
449 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000450 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000451 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000453 option->Set(value);
454 }
455 }
456}
457
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000459 PeerConnectionInterface::IceTransportsType type) {
460 switch (type) {
461 case PeerConnectionInterface::kNone:
462 return cricket::CF_NONE;
463 case PeerConnectionInterface::kRelay:
464 return cricket::CF_RELAY;
465 case PeerConnectionInterface::kNoHost:
466 return (cricket::CF_ALL & ~cricket::CF_HOST);
467 case PeerConnectionInterface::kAll:
468 return cricket::CF_ALL;
469 default: ASSERT(false);
470 }
471 return cricket::CF_NONE;
472}
473
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474// Help class used to remember if a a remote peer has requested ice restart by
475// by sending a description with new ice ufrag and password.
476class IceRestartAnswerLatch {
477 public:
478 IceRestartAnswerLatch() : ice_restart_(false) { }
479
wu@webrtc.org91053e72013-08-10 07:18:04 +0000480 // Returns true if CheckForRemoteIceRestart has been called with a new session
481 // description where ice password and ufrag has changed since last time
482 // Reset() was called.
483 bool Get() const {
484 return ice_restart_;
485 }
486
487 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 if (ice_restart_) {
489 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 }
492
honghaiz503726c2015-07-31 12:37:38 -0700493 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
494 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700496 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498 const SessionDescription* new_sd = new_desc->description();
499 const SessionDescription* old_sd = old_desc->description();
500 const ContentInfos& contents = new_sd->contents();
501 for (size_t index = 0; index < contents.size(); ++index) {
502 const ContentInfo* cinfo = &contents[index];
503 if (cinfo->rejected) {
504 continue;
505 }
506 // If the content isn't rejected, check if ufrag and password has
507 // changed.
508 const cricket::TransportDescription* new_transport_desc =
509 new_sd->GetTransportDescriptionByName(cinfo->name);
510 const cricket::TransportDescription* old_transport_desc =
511 old_sd->GetTransportDescriptionByName(cinfo->name);
512 if (!new_transport_desc || !old_transport_desc) {
513 // No transport description exist. This is not an ice restart.
514 continue;
515 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000516 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
517 old_transport_desc->ice_pwd,
518 new_transport_desc->ice_ufrag,
519 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 LOG(LS_INFO) << "Remote peer request ice restart.";
521 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700522 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
524 }
honghaiz503726c2015-07-31 12:37:38 -0700525 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
527
528 private:
529 bool ice_restart_;
530};
531
deadbeefab9b2d12015-10-14 11:33:11 -0700532WebRtcSession::WebRtcSession(cricket::ChannelManager* channel_manager,
533 rtc::Thread* signaling_thread,
534 rtc::Thread* worker_thread,
535 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700536 : signaling_thread_(signaling_thread),
537 worker_thread_(worker_thread),
538 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // RFC 3264: The numeric value of the session id and version in the
540 // o line MUST be representable with a "64 bit signed integer".
541 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700542 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
543 transport_controller_(new cricket::TransportController(signaling_thread,
544 worker_thread,
545 port_allocator)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 ice_observer_(NULL),
548 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700549 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000551 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000553 ice_restart_latch_(new IceRestartAnswerLatch),
554 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700555 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
556 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700557 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700558 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700559 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700560 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700561 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700562 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700563 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564}
565
566WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700567 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000568 // Destroy video_channel_ first since it may have a pointer to the
569 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000570 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 SignalVideoChannelDestroyed();
572 channel_manager_->DestroyVideoChannel(video_channel_.release());
573 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000574 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000575 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200576 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000577 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000578 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 SignalDataChannelDestroyed();
580 channel_manager_->DestroyDataChannel(data_channel_.release());
581 }
deadbeefd59daf82015-10-14 15:02:44 -0700582
583 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584}
585
wu@webrtc.org91053e72013-08-10 07:18:04 +0000586bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000587 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700588 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200589 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200590 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
591 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700592 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700593 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000594
Henrik Boström87713d02015-08-25 09:53:21 +0200595 // Obtain a certificate from RTCConfiguration if any were provided (optional).
596 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
597 if (!rtc_configuration.certificates.empty()) {
598 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
599 // just picking the first one. The decision should be made based on the DTLS
600 // handshake. The DTLS negotiations need to know about all certificates.
601 certificate = rtc_configuration.certificates[0];
602 }
603
honghaiz1f429e32015-09-28 07:57:34 -0700604 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 // TODO(perkj): Take |constraints| into consideration. Return false if not all
607 // mandatory constraints can be fulfilled. Note that |constraints|
608 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000610
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000611 if (options.disable_encryption) {
612 dtls_enabled_ = false;
613 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200614 // Enable DTLS by default if we have an identity store or a certificate.
615 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000616 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700617 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
618 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000619 dtls_enabled_ = value;
620 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000621 }
622
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000624 // It takes precendence over the disable_sctp_data_channels
625 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 if (FindConstraint(
627 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
628 &value, NULL) && value) {
629 LOG(LS_INFO) << "Allowing RTP data engine.";
630 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000631 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000633 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000634 LOG(LS_INFO) << "Allowing SCTP data engine.";
635 data_channel_type_ = cricket::DCT_SCTP;
636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638
wu@webrtc.orgde305012013-10-31 15:40:38 +0000639 // Find DSCP constraint.
640 if (FindConstraint(
641 constraints,
642 MediaConstraintsInterface::kEnableDscp,
643 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000644 audio_options_.dscp.Set(value);
645 video_options_.dscp.Set(value);
646 }
647
648 // Find Suspend Below Min Bitrate constraint.
649 if (FindConstraint(
650 constraints,
651 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
652 &value,
653 NULL)) {
654 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000655 }
656
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000657 SetOptionFromOptionalConstraint(constraints,
658 MediaConstraintsInterface::kScreencastMinBitrate,
659 &video_options_.screencast_min_bitrate);
660
661 // Find constraints for cpu overuse detection.
662 SetOptionFromOptionalConstraint(constraints,
663 MediaConstraintsInterface::kCpuUnderuseThreshold,
664 &video_options_.cpu_underuse_threshold);
665 SetOptionFromOptionalConstraint(constraints,
666 MediaConstraintsInterface::kCpuOveruseThreshold,
667 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000668 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000669 MediaConstraintsInterface::kCpuOveruseDetection,
670 &video_options_.cpu_overuse_detection);
671 SetOptionFromOptionalConstraint(constraints,
672 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
673 &video_options_.cpu_overuse_encode_usage);
674 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000675 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
676 &video_options_.cpu_underuse_encode_rsd_threshold);
677 SetOptionFromOptionalConstraint(constraints,
678 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
679 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000680
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000681 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000682 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
683 &video_options_.unsignalled_recv_stream_limit);
684 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
685 int stream_limit;
686 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000687 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
688 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000689 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
690 }
691
692 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000693 MediaConstraintsInterface::kHighStartBitrate,
694 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000695
696 if (FindConstraint(
697 constraints,
698 MediaConstraintsInterface::kVeryHighBitrate,
699 &value,
700 NULL)) {
701 video_options_.video_highest_bitrate.Set(
702 cricket::VideoOptions::VERY_HIGH);
703 } else if (FindConstraint(
704 constraints,
705 MediaConstraintsInterface::kHighBitrate,
706 &value,
707 NULL)) {
708 video_options_.video_highest_bitrate.Set(
709 cricket::VideoOptions::HIGH);
710 }
711
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000712 SetOptionFromOptionalConstraint(constraints,
713 MediaConstraintsInterface::kCombinedAudioVideoBwe,
714 &audio_options_.combined_audio_video_bwe);
715
Henrik Lundin64dad832015-05-11 12:44:23 +0200716 audio_options_.audio_jitter_buffer_max_packets.Set(
717 rtc_configuration.audio_jitter_buffer_max_packets);
718
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200719 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
720 rtc_configuration.audio_jitter_buffer_fast_accelerate);
721
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 const cricket::VideoCodec default_codec(
723 JsepSessionDescription::kDefaultVideoCodecId,
724 JsepSessionDescription::kDefaultVideoCodecName,
725 JsepSessionDescription::kMaxVideoCodecWidth,
726 JsepSessionDescription::kMaxVideoCodecHeight,
727 JsepSessionDescription::kDefaultVideoCodecFramerate,
728 JsepSessionDescription::kDefaultVideoCodecPreference);
729 channel_manager_->SetDefaultVideoEncoderConfig(
730 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000731
Henrik Boström87713d02015-08-25 09:53:21 +0200732 if (!dtls_enabled_) {
733 // Construct with DTLS disabled.
734 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700735 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200736 } else {
737 // Construct with DTLS enabled.
738 if (!certificate) {
739 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700740 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200741 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700742 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
743 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200744 } else {
745 // Use the already generated certificate.
746 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700747 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200748 }
749 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000750
Henrik Boströmd8281982015-08-27 10:12:24 +0200751 webrtc_session_desc_factory_->SignalCertificateReady.connect(
752 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000753
wu@webrtc.org97077a32013-10-25 21:18:33 +0000754 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000755 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000756 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000757 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200758 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700759
760 if (rtc_configuration.enable_localhost_ice_candidate) {
761 port_allocator()->set_flags(
762 port_allocator()->flags() |
763 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
764 }
765
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200766 media_controller_.reset(MediaControllerInterface::Create(
767 worker_thread(), channel_manager_->media_engine()->GetVoE()));
768
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 return true;
770}
771
deadbeefd59daf82015-10-14 15:02:44 -0700772void WebRtcSession::Close() {
773 SetState(STATE_CLOSED);
774 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000775 ASSERT(!voice_channel_);
776 ASSERT(!video_channel_);
777 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778}
779
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000780void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
781 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782}
783
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000784cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
785 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786}
787
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700789 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000790 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
791 << "SSL Role of the session.";
792 return false;
793 }
794
deadbeefd59daf82015-10-14 15:02:44 -0700795 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000796}
797
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000798void WebRtcSession::CreateOffer(
799 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700800 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
801 const cricket::MediaSessionOptions& session_options) {
802 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000803}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
deadbeefab9b2d12015-10-14 11:33:11 -0700805void WebRtcSession::CreateAnswer(
806 CreateSessionDescriptionObserver* observer,
807 const MediaConstraintsInterface* constraints,
808 const cricket::MediaSessionOptions& session_options) {
809 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
810 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811}
812
813bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
814 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700815 ASSERT(signaling_thread()->IsCurrent());
816
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000817 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000818 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000819
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000820 // Validate SDP.
821 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
822 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
deadbeefd59daf82015-10-14 15:02:44 -0700825 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000826 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700828 initial_offerer_ = true;
829 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000832 cricket::SecurePolicy sdes_policy =
833 webrtc_session_desc_factory_->SdesPolicy();
834 cricket::CryptoType crypto_required = dtls_enabled_ ?
835 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
836 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000838 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000840 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841
842 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000843 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // TODO(mallinath) - Handle CreateChannel failure, as new local description
845 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000846 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
848
deadbeefcbecd352015-09-23 11:50:27 -0700849 // Remove unused channels if MediaContentDescription is rejected.
850 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000852 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 return false;
854 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000855
deadbeefd59daf82015-10-14 15:02:44 -0700856 if (remote_desc_) {
857 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700858 UseCandidatesInSessionDescription(remote_desc_.get());
859 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860
deadbeefd59daf82015-10-14 15:02:44 -0700861 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000862 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
864 return true;
865}
866
867bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
868 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700869 ASSERT(signaling_thread()->IsCurrent());
870
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000871 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000872 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000873
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000874 // Validate SDP.
875 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
876 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 }
878
deadbeefd59daf82015-10-14 15:02:44 -0700879 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
880 remote_desc_.release());
881 remote_desc_.reset(desc_temp.release());
882
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000884 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (action == kOffer && !CreateChannels(desc->description())) {
886 // TODO(mallinath) - Handle CreateChannel failure, as new local description
887 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890
deadbeefcbecd352015-09-23 11:50:27 -0700891 // Remove unused channels if MediaContentDescription is rejected.
892 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893
894 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
895 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 return false;
898 }
899
deadbeefd59daf82015-10-14 15:02:44 -0700900 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000901 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 // Check if this new SessionDescription contains new ice ufrag and password
905 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700906 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700907 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700908 // We retain all received candidates only if ICE is not restarted.
909 // When ICE is restarted, all previous candidates belong to an old generation
910 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700911 // TODO(deadbeef): This goes against the W3C spec which says the remote
912 // description should only contain candidates from the last set remote
913 // description plus any candidates added since then. We should remove this
914 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700915 if (!ice_restart) {
916 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700917 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700918 }
919
deadbeefd59daf82015-10-14 15:02:44 -0700920 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000921 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000923
924 // Set the the ICE connection state to connecting since the connection may
925 // become writable with peer reflexive candidates before any remote candidate
926 // is signaled.
927 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
928 // is to have a new signal the indicates a change in checking state from the
929 // transport and expose a new checking() member from transport that can be
930 // read to determine the current checking state. The existing SignalConnecting
931 // actually means "gathering candidates", so cannot be be used here.
932 if (desc->type() != SessionDescriptionInterface::kOffer &&
933 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
934 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 return true;
937}
938
deadbeefd59daf82015-10-14 15:02:44 -0700939void WebRtcSession::LogState(State old_state, State new_state) {
940 LOG(LS_INFO) << "Session:" << id()
941 << " Old state:" << GetStateString(old_state)
942 << " New state:" << GetStateString(new_state);
943}
944
945void WebRtcSession::SetState(State state) {
946 ASSERT(signaling_thread_->IsCurrent());
947 if (state != state_) {
948 LogState(state_, state);
949 state_ = state;
950 SignalState(this, state_);
951 }
952}
953
954void WebRtcSession::SetError(Error error, const std::string& error_desc) {
955 ASSERT(signaling_thread_->IsCurrent());
956 if (error != error_) {
957 error_ = error;
958 error_desc_ = error_desc;
959 }
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962bool WebRtcSession::UpdateSessionState(
963 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700965 ASSERT(signaling_thread()->IsCurrent());
966
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 // If there's already a pending error then no state transition should happen.
968 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700969 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000970 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000972 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
973 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
deadbeefd59daf82015-10-14 15:02:44 -0700975 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
976 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000977 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700978 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000979 }
deadbeefd59daf82015-10-14 15:02:44 -0700980 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 }
983 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000984 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
985 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 }
987 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700988 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
989 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000990 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700991 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000992 }
deadbeefd59daf82015-10-14 15:02:44 -0700993 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000994 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
996 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000997 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
998 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 }
deadbeefcbecd352015-09-23 11:50:27 -07001000 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -07001001 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -07001002 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -07001003 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -07001004 if (local_bundle && remote_bundle) {
1005 // The answerer decides the transport to bundle on
1006 const cricket::ContentGroup* answer_bundle =
1007 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
1008 if (!EnableBundle(*answer_bundle)) {
1009 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
1010 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
1011 }
1012 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -07001014 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001015 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -07001016 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001017 }
deadbeefd59daf82015-10-14 15:02:44 -07001018 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001019 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 }
1021 }
1022 return true;
1023}
1024
1025WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1026 if (type == SessionDescriptionInterface::kOffer) {
1027 return WebRtcSession::kOffer;
1028 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1029 return WebRtcSession::kPrAnswer;
1030 } else if (type == SessionDescriptionInterface::kAnswer) {
1031 return WebRtcSession::kAnswer;
1032 }
1033 ASSERT(false && "unknown action type");
1034 return WebRtcSession::kOffer;
1035}
1036
deadbeefd59daf82015-10-14 15:02:44 -07001037bool WebRtcSession::PushdownMediaDescription(
1038 cricket::ContentAction action,
1039 cricket::ContentSource source,
1040 std::string* err) {
1041 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1042 if (!ch) {
1043 return true;
1044 } else if (source == cricket::CS_LOCAL) {
1045 return ch->PushdownLocalDescription(local_desc_->description(), action,
1046 err);
1047 } else {
1048 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1049 err);
1050 }
1051 };
1052
1053 return (set_content(voice_channel()) &&
1054 set_content(video_channel()) &&
1055 set_content(data_channel()));
1056}
1057
1058bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1059 cricket::ContentAction action,
1060 std::string* error_desc) {
1061 RTC_DCHECK(signaling_thread()->IsCurrent());
1062
1063 if (source == cricket::CS_LOCAL) {
1064 return PushdownLocalTransportDescription(local_desc_->description(), action,
1065 error_desc);
1066 }
1067 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1068 error_desc);
1069}
1070
1071bool WebRtcSession::PushdownLocalTransportDescription(
1072 const SessionDescription* sdesc,
1073 cricket::ContentAction action,
1074 std::string* err) {
1075 RTC_DCHECK(signaling_thread()->IsCurrent());
1076
1077 if (!sdesc) {
1078 return false;
1079 }
1080
1081 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1082 if (!transport_controller_->SetLocalTransportDescription(
1083 tinfo.content_name, tinfo.description, action, err)) {
1084 return false;
1085 }
1086 }
1087
1088 return true;
1089}
1090
1091bool WebRtcSession::PushdownRemoteTransportDescription(
1092 const SessionDescription* sdesc,
1093 cricket::ContentAction action,
1094 std::string* err) {
1095 RTC_DCHECK(signaling_thread()->IsCurrent());
1096
1097 if (!sdesc) {
1098 return false;
1099 }
1100
1101 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1102 if (!transport_controller_->SetRemoteTransportDescription(
1103 tinfo.content_name, tinfo.description, action, err)) {
1104 return false;
1105 }
1106 }
1107
1108 return true;
1109}
1110
1111bool WebRtcSession::GetTransportDescription(
1112 const SessionDescription* description,
1113 const std::string& content_name,
1114 cricket::TransportDescription* tdesc) {
1115 if (!description || !tdesc) {
1116 return false;
1117 }
1118 const TransportInfo* transport_info =
1119 description->GetTransportInfoByName(content_name);
1120 if (!transport_info) {
1121 return false;
1122 }
1123 *tdesc = transport_info->description;
1124 return true;
1125}
1126
1127bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001128 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001129 return (GetChannelTransportStats(voice_channel(), stats) &&
1130 GetChannelTransportStats(video_channel(), stats) &&
1131 GetChannelTransportStats(data_channel(), stats));
1132}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001133
deadbeefcbecd352015-09-23 11:50:27 -07001134bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001135 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001136 ASSERT(signaling_thread()->IsCurrent());
1137 if (!ch) {
1138 // Not using this channel.
1139 return true;
1140 }
1141
1142 const std::string& content_name = ch->content_name();
1143 const std::string& transport_name = ch->transport_name();
1144 stats->proxy_to_transport[content_name] = transport_name;
1145 if (stats->transport_stats.find(transport_name) !=
1146 stats->transport_stats.end()) {
1147 // Transport stats already done for this transport.
1148 return true;
1149 }
1150
1151 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001152 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001153 return false;
1154 }
1155
1156 stats->transport_stats[transport_name] = tstats;
1157 return true;
1158}
1159
1160bool WebRtcSession::GetLocalCertificate(
1161 const std::string& transport_name,
1162 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1163 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001164 return transport_controller_->GetLocalCertificate(transport_name,
1165 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001166}
1167
1168bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1169 rtc::SSLCertificate** cert) {
1170 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001171 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001172}
1173
1174cricket::BaseChannel* WebRtcSession::GetChannel(
1175 const std::string& content_name) {
1176 if (voice_channel() && voice_channel()->content_name() == content_name) {
1177 return voice_channel();
1178 }
1179 if (video_channel() && video_channel()->content_name() == content_name) {
1180 return video_channel();
1181 }
1182 if (data_channel() && data_channel()->content_name() == content_name) {
1183 return data_channel();
1184 }
1185 return nullptr;
1186}
1187
1188bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1189 const std::string* first_content_name = bundle.FirstContentName();
1190 if (!first_content_name) {
1191 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1192 return false;
1193 }
1194 const std::string& transport_name = *first_content_name;
1195 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1196
1197 auto maybe_set_transport = [this, bundle, transport_name,
1198 first_channel](cricket::BaseChannel* ch) {
1199 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001200 return true;
1201 }
1202
deadbeefcbecd352015-09-23 11:50:27 -07001203 if (ch->transport_name() == transport_name) {
1204 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1205 << " on " << transport_name << ".";
1206 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001207 }
torbjornga81a42f2015-09-23 02:16:58 -07001208
deadbeefcbecd352015-09-23 11:50:27 -07001209 if (!ch->SetTransport(transport_name)) {
1210 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1211 return false;
1212 }
1213 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1214 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001215 return true;
1216 };
1217
deadbeefcbecd352015-09-23 11:50:27 -07001218 if (!maybe_set_transport(voice_channel()) ||
1219 !maybe_set_transport(video_channel()) ||
1220 !maybe_set_transport(data_channel())) {
1221 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001222 }
deadbeefcbecd352015-09-23 11:50:27 -07001223
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001224 return true;
1225}
1226
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001228 if (!remote_desc_) {
1229 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1230 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 return false;
1232 }
1233
1234 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001235 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 return false;
1237 }
1238
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001239 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001240 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1241 if (!valid) {
1242 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 }
1244
1245 // Add this candidate to the remote session description.
1246 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001247 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 return false;
1249 }
1250
deadbeefd59daf82015-10-14 15:02:44 -07001251 if (ready) {
1252 return UseCandidate(candidate);
1253 } else {
1254 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1255 return true;
1256 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257}
1258
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001259bool WebRtcSession::SetIceTransports(
1260 PeerConnectionInterface::IceTransportsType type) {
1261 return port_allocator()->set_candidate_filter(
1262 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001263}
1264
deadbeefd59daf82015-10-14 15:02:44 -07001265cricket::IceConfig WebRtcSession::ParseIceConfig(
1266 const PeerConnectionInterface::RTCConfiguration& config) const {
1267 cricket::IceConfig ice_config;
1268 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1269 ice_config.gather_continually = (config.continual_gathering_policy ==
1270 PeerConnectionInterface::GATHER_CONTINUALLY);
1271 return ice_config;
1272}
1273
1274void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1275 transport_controller_->SetIceConfig(config);
1276}
1277
1278void WebRtcSession::MaybeStartGathering() {
1279 transport_controller_->MaybeStartGathering();
1280}
1281
Peter Boström0c4e06b2015-10-07 12:23:21 +02001282bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1283 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001284 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001286 }
1287 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288}
1289
Peter Boström0c4e06b2015-10-07 12:23:21 +02001290bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1291 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001292 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001293 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001294 }
1295 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296}
1297
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001298std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001300 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 return desc.str();
1302}
1303
solenbergd4cec0d2015-10-09 08:55:48 -07001304void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 ASSERT(signaling_thread()->IsCurrent());
1306 if (!voice_channel_) {
1307 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1308 return;
1309 }
solenberg4bac9c52015-10-09 02:32:53 -07001310 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1311 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 // otherwise. This in the normal case when the underlying media channel has
1313 // already been deleted.
1314 ASSERT(enable == false);
1315 }
1316}
1317
Peter Boström0c4e06b2015-10-07 12:23:21 +02001318void WebRtcSession::SetAudioSend(uint32_t ssrc,
1319 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001320 const cricket::AudioOptions& options,
1321 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ASSERT(signaling_thread()->IsCurrent());
1323 if (!voice_channel_) {
1324 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1325 return;
1326 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001327 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001328 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
Peter Boström0c4e06b2015-10-07 12:23:21 +02001332void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001333 ASSERT(signaling_thread()->IsCurrent());
1334 ASSERT(volume >= 0 && volume <= 10);
1335 if (!voice_channel_) {
1336 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1337 return;
1338 }
1339
solenberg4bac9c52015-10-09 02:32:53 -07001340 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001341 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001342 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001343}
1344
Peter Boström0c4e06b2015-10-07 12:23:21 +02001345bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 cricket::VideoCapturer* camera) {
1347 ASSERT(signaling_thread()->IsCurrent());
1348
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001349 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1351 // support video.
1352 LOG(LS_WARNING) << "Video not used in this call.";
1353 return false;
1354 }
1355 if (!video_channel_->SetCapturer(ssrc, camera)) {
1356 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1357 // This in the normal case when the underlying media channel has already
1358 // been deleted.
1359 ASSERT(camera == NULL);
1360 return false;
1361 }
1362 return true;
1363}
1364
Peter Boström0c4e06b2015-10-07 12:23:21 +02001365void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 bool enable,
1367 cricket::VideoRenderer* renderer) {
1368 ASSERT(signaling_thread()->IsCurrent());
1369 if (!video_channel_) {
1370 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1371 return;
1372 }
1373 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1374 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1375 // This in the normal case when the underlying media channel has already
1376 // been deleted.
1377 ASSERT(renderer == NULL);
1378 }
1379}
1380
Peter Boström0c4e06b2015-10-07 12:23:21 +02001381void WebRtcSession::SetVideoSend(uint32_t ssrc,
1382 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 const cricket::VideoOptions* options) {
1384 ASSERT(signaling_thread()->IsCurrent());
1385 if (!video_channel_) {
1386 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1387 return;
1388 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001389 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1391 // This in the normal case when the underlying media channel has already
1392 // been deleted.
1393 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395}
1396
1397bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1398 ASSERT(signaling_thread()->IsCurrent());
1399 if (!voice_channel_) {
1400 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1401 return false;
1402 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001403 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1405 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001406 if (!local_desc_ ||
1407 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 &send_ssrc)) {
1409 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1410 return false;
1411 }
1412 return voice_channel_->CanInsertDtmf();
1413}
1414
1415bool WebRtcSession::InsertDtmf(const std::string& track_id,
1416 int code, int duration) {
1417 ASSERT(signaling_thread()->IsCurrent());
1418 if (!voice_channel_) {
1419 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1420 return false;
1421 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001422 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001423 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1424 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1426 return false;
1427 }
1428 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1429 cricket::DF_SEND)) {
1430 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1431 return false;
1432 }
1433 return true;
1434}
1435
1436sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1437 return &SignalVoiceChannelDestroyed;
1438}
1439
wu@webrtc.org78187522013-10-07 23:32:02 +00001440bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001441 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001442 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001443 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001444 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1445 return false;
1446 }
1447 return data_channel_->SendData(params, payload, result);
1448}
1449
1450bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001451 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001452 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1453 return false;
1454 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001455 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1456 &DataChannel::OnChannelReady);
1457 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1458 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001459 data_channel_->SignalStreamClosedRemotely.connect(
1460 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001461 return true;
1462}
1463
1464void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001465 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001466 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1467 return;
1468 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001469 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1470 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001471 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001472}
1473
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001474void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001475 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001476 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1477 return;
1478 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001479 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1480 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001481}
1482
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001483void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001484 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001485 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1486 << "NULL.";
1487 return;
1488 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001489 data_channel_->RemoveRecvStream(sid);
1490 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001491}
1492
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001493bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001494 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001495}
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497cricket::DataChannelType WebRtcSession::data_channel_type() const {
1498 return data_channel_type_;
1499}
1500
wu@webrtc.org91053e72013-08-10 07:18:04 +00001501bool WebRtcSession::IceRestartPending() const {
1502 return ice_restart_latch_->Get();
1503}
1504
1505void WebRtcSession::ResetIceRestartLatch() {
1506 ice_restart_latch_->Reset();
1507}
1508
Henrik Boströmd8281982015-08-27 10:12:24 +02001509void WebRtcSession::OnCertificateReady(
1510 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001511 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001512}
1513
Henrik Boströmd8281982015-08-27 10:12:24 +02001514bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001515 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001516}
1517
deadbeefcbecd352015-09-23 11:50:27 -07001518const rtc::scoped_refptr<rtc::RTCCertificate>&
1519WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001520 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523void WebRtcSession::SetIceConnectionState(
1524 PeerConnectionInterface::IceConnectionState state) {
1525 if (ice_connection_state_ == state) {
1526 return;
1527 }
1528
1529 // ASSERT that the requested transition is allowed. Note that
1530 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1531 // within PeerConnection). This switch statement should compile away when
1532 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001533 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1534 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 switch (ice_connection_state_) {
1536 case PeerConnectionInterface::kIceConnectionNew:
1537 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1538 break;
1539 case PeerConnectionInterface::kIceConnectionChecking:
1540 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1541 state == PeerConnectionInterface::kIceConnectionConnected);
1542 break;
1543 case PeerConnectionInterface::kIceConnectionConnected:
1544 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1545 state == PeerConnectionInterface::kIceConnectionChecking ||
1546 state == PeerConnectionInterface::kIceConnectionCompleted);
1547 break;
1548 case PeerConnectionInterface::kIceConnectionCompleted:
1549 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1550 state == PeerConnectionInterface::kIceConnectionDisconnected);
1551 break;
1552 case PeerConnectionInterface::kIceConnectionFailed:
1553 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1554 break;
1555 case PeerConnectionInterface::kIceConnectionDisconnected:
1556 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1557 state == PeerConnectionInterface::kIceConnectionConnected ||
1558 state == PeerConnectionInterface::kIceConnectionCompleted ||
1559 state == PeerConnectionInterface::kIceConnectionFailed);
1560 break;
1561 case PeerConnectionInterface::kIceConnectionClosed:
1562 ASSERT(false);
1563 break;
1564 default:
1565 ASSERT(false);
1566 break;
1567 }
1568
1569 ice_connection_state_ = state;
1570 if (ice_observer_) {
1571 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1572 }
1573}
1574
deadbeefcbecd352015-09-23 11:50:27 -07001575void WebRtcSession::OnTransportControllerConnectionState(
1576 cricket::IceConnectionState state) {
1577 switch (state) {
1578 case cricket::kIceConnectionConnecting:
1579 // If the current state is Connected or Completed, then there were
1580 // writable channels but now there are not, so the next state must
1581 // be Disconnected.
1582 // kIceConnectionConnecting is currently used as the default,
1583 // un-connected state by the TransportController, so its only use is
1584 // detecting disconnections.
1585 if (ice_connection_state_ ==
1586 PeerConnectionInterface::kIceConnectionConnected ||
1587 ice_connection_state_ ==
1588 PeerConnectionInterface::kIceConnectionCompleted) {
1589 SetIceConnectionState(
1590 PeerConnectionInterface::kIceConnectionDisconnected);
1591 }
torbjornga81a42f2015-09-23 02:16:58 -07001592 break;
deadbeefcbecd352015-09-23 11:50:27 -07001593 case cricket::kIceConnectionFailed:
1594 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1595 break;
1596 case cricket::kIceConnectionConnected:
1597 LOG(LS_INFO) << "Changing to ICE connected state because "
1598 << "all transports are writable.";
1599 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1600 break;
1601 case cricket::kIceConnectionCompleted:
1602 LOG(LS_INFO) << "Changing to ICE completed state because "
1603 << "all transports are complete.";
1604 if (ice_connection_state_ !=
1605 PeerConnectionInterface::kIceConnectionConnected) {
1606 // If jumping directly from "checking" to "connected",
1607 // signal "connected" first.
1608 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1609 }
1610 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1611 if (metrics_observer_) {
1612 ReportTransportStats();
1613 }
1614 break;
1615 default:
1616 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001617 }
deadbeefcbecd352015-09-23 11:50:27 -07001618}
1619
1620void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001621 SetIceConnectionReceiving(receiving);
1622}
1623
1624void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1625 if (ice_connection_receiving_ == receiving) {
1626 return;
1627 }
1628 ice_connection_receiving_ = receiving;
1629 if (ice_observer_) {
1630 ice_observer_->OnIceConnectionReceivingChange(receiving);
1631 }
1632}
1633
deadbeefcbecd352015-09-23 11:50:27 -07001634void WebRtcSession::OnTransportControllerCandidatesGathered(
1635 const std::string& transport_name,
1636 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001638 int sdp_mline_index;
1639 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1640 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1641 << transport_name << " not found";
1642 return;
1643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644
deadbeefcbecd352015-09-23 11:50:27 -07001645 for (cricket::Candidates::const_iterator citer = candidates.begin();
1646 citer != candidates.end(); ++citer) {
1647 // Use transport_name as the candidate media id.
1648 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1649 if (ice_observer_) {
1650 ice_observer_->OnIceCandidate(&candidate);
1651 }
1652 if (local_desc_) {
1653 local_desc_->AddCandidate(&candidate);
1654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 }
1656}
1657
1658// Enabling voice and video channel.
1659void WebRtcSession::EnableChannels() {
1660 if (voice_channel_ && !voice_channel_->enabled())
1661 voice_channel_->Enable(true);
1662
1663 if (video_channel_ && !video_channel_->enabled())
1664 video_channel_->Enable(true);
1665
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001666 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 data_channel_->Enable(true);
1668}
1669
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670// Returns the media index for a local ice candidate given the content name.
1671bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1672 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001673 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676
1677 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001678 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 for (size_t index = 0; index < contents.size(); ++index) {
1680 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001681 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 content_found = true;
1683 break;
1684 }
1685 }
1686 return content_found;
1687}
1688
1689bool WebRtcSession::UseCandidatesInSessionDescription(
1690 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001691 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001693 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001695
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1697 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001698 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001699 const IceCandidateInterface* candidate = candidates->at(n);
1700 bool valid = false;
1701 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1702 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001703 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1704 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001705 }
1706 continue;
1707 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001708 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001709 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 break;
deadbeefd59daf82015-10-14 15:02:44 -07001711 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 }
1713 }
1714 return ret;
1715}
1716
1717bool WebRtcSession::UseCandidate(
1718 const IceCandidateInterface* candidate) {
1719
1720 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001721 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 if (mediacontent_index >= remote_content_size) {
1723 LOG(LS_ERROR)
1724 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1725 return false;
1726 }
1727
1728 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001729 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 std::vector<cricket::Candidate> candidates;
1731 candidates.push_back(candidate->candidate());
1732 // Invoking BaseSession method to handle remote candidates.
1733 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001734 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1735 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 // Candidates successfully submitted for checking.
1737 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1738 ice_connection_state_ ==
1739 PeerConnectionInterface::kIceConnectionDisconnected) {
1740 // If state is New, then the session has just gotten its first remote ICE
1741 // candidates, so go to Checking.
1742 // If state is Disconnected, the session is re-using old candidates or
1743 // receiving additional ones, so go to Checking.
1744 // If state is Connected, stay Connected.
1745 // TODO(bemasc): If state is Connected, and the new candidates are for a
1746 // newly added transport, then the state actually _should_ move to
1747 // checking. Add a way to distinguish that case.
1748 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1749 }
1750 // TODO(bemasc): If state is Completed, go back to Connected.
1751 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001752 if (!error.empty()) {
1753 LOG(LS_WARNING) << error;
1754 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756 return true;
1757}
1758
deadbeefcbecd352015-09-23 11:50:27 -07001759void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001760 // Destroy video_channel_ first since it may have a pointer to the
1761 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 const cricket::ContentInfo* video_info =
1763 cricket::GetFirstVideoContent(desc);
1764 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 SignalVideoChannelDestroyed();
1766 const std::string content_name = video_channel_->content_name();
1767 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 }
1769
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001770 const cricket::ContentInfo* voice_info =
1771 cricket::GetFirstAudioContent(desc);
1772 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001773 SignalVoiceChannelDestroyed();
1774 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001775 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001776 }
1777
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 const cricket::ContentInfo* data_info =
1779 cricket::GetFirstDataContent(desc);
1780 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 SignalDataChannelDestroyed();
1782 const std::string content_name = data_channel_->content_name();
1783 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 }
1785}
1786
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001787// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001788// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 // Creating the media channels and transport proxies.
1791 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1792 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_ERROR) << "Failed to create voice channel.";
1795 return false;
1796 }
1797 }
1798
1799 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1800 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001801 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 LOG(LS_ERROR) << "Failed to create video channel.";
1803 return false;
1804 }
1805 }
1806
1807 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1808 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001809 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001810 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 LOG(LS_ERROR) << "Failed to create data channel.";
1812 return false;
1813 }
1814 }
1815
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001816 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1817 if (voice_channel()) {
1818 voice_channel()->ActivateRtcpMux();
1819 }
1820 if (video_channel()) {
1821 video_channel()->ActivateRtcpMux();
1822 }
1823 if (data_channel()) {
1824 data_channel()->ActivateRtcpMux();
1825 }
1826 }
1827
deadbeefcbecd352015-09-23 11:50:27 -07001828 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001829 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001830 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1831 cricket::GROUP_TYPE_BUNDLE);
1832 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001833 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1834 return false;
1835 }
deadbeefcbecd352015-09-23 11:50:27 -07001836 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001837 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1838 return false;
1839 }
1840 }
1841
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 return true;
1843}
1844
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001845bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001847 media_controller_.get(), transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001848 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001849 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001850 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001851 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001852
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001853 voice_channel_->SignalDtlsSetupFailure.connect(
1854 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001855
1856 SignalVoiceChannelCreated();
wu@webrtc.orgde305012013-10-31 15:40:38 +00001857 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858}
1859
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001860bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 video_channel_.reset(channel_manager_->CreateVideoChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001862 media_controller_.get(), transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001863 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001864 if (!video_channel_) {
1865 return false;
1866 }
1867
1868 video_channel_->SignalDtlsSetupFailure.connect(
1869 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001870
1871 SignalVideoChannelCreated();
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001872 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001875bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001876 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001878 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001879 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001880 return false;
1881 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001882
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001883 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001884 data_channel_->SignalDataReceived.connect(
1885 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001886 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001887
1888 data_channel_->SignalDtlsSetupFailure.connect(
1889 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001890
1891 SignalDataChannelCreated();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001892 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893}
1894
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001895void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001896 SetError(ERROR_TRANSPORT,
1897 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898}
1899
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001900void WebRtcSession::OnDataChannelMessageReceived(
1901 cricket::DataChannel* channel,
1902 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001903 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001904 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1905 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1906 // Received OPEN message; parse and signal that a new data channel should
1907 // be created.
1908 std::string label;
1909 InternalDataChannelInit config;
1910 config.id = params.ssrc;
1911 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1912 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1913 << params.ssrc;
1914 return;
1915 }
1916 config.open_handshake_role = InternalDataChannelInit::kAcker;
1917 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 }
deadbeefab9b2d12015-10-14 11:33:11 -07001919 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920}
1921
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001922// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001923bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001924 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1925 if (!bundle_enabled)
1926 return true;
1927
1928 const cricket::ContentGroup* bundle_group =
1929 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1930 ASSERT(bundle_group != NULL);
1931
1932 const cricket::ContentInfos& contents = desc->contents();
1933 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1934 citer != contents.end(); ++citer) {
1935 const cricket::ContentInfo* content = (&*citer);
1936 ASSERT(content != NULL);
1937 if (bundle_group->HasContentName(content->name) &&
1938 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1939 if (!HasRtcpMuxEnabled(content))
1940 return false;
1941 }
1942 }
1943 // RTCP-MUX is enabled in all the contents.
1944 return true;
1945}
1946
1947bool WebRtcSession::HasRtcpMuxEnabled(
1948 const cricket::ContentInfo* content) {
1949 const cricket::MediaContentDescription* description =
1950 static_cast<cricket::MediaContentDescription*>(content->description);
1951 return description->rtcp_mux();
1952}
1953
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954bool WebRtcSession::ValidateSessionDescription(
1955 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001956 cricket::ContentSource source, std::string* err_desc) {
1957 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001958 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001959 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001960 }
1961
1962 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001963 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001964 }
1965
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001966 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001967 Action action = GetAction(sdesc->type());
1968 if (source == cricket::CS_LOCAL) {
1969 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001970 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001971 } else {
1972 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001973 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001974 }
1975
1976 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001977 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001978 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1979 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001980 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001981 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001982 }
1983
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001984 // Verify ice-ufrag and ice-pwd.
1985 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001986 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001987 }
1988
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001989 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001990 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001991 }
1992
1993 // Verify m-lines in Answer when compared against Offer.
1994 if (action == kAnswer) {
1995 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001996 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1997 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001998 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001999 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000 }
2001 }
2002
2003 return true;
2004}
2005
2006bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2007 return ((action == kOffer && state() == STATE_INIT) ||
2008 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002009 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002010 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002011 (action == kOffer && state() == STATE_INPROGRESS) ||
2012 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002013 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2014 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2015 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2016 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002017}
2018
2019bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2020 return ((action == kOffer && state() == STATE_INIT) ||
2021 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002022 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002023 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002024 (action == kOffer && state() == STATE_INPROGRESS) ||
2025 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002026 (action == kAnswer && state() == STATE_SENTOFFER) ||
2027 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2028 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2029 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002030}
2031
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002032std::string WebRtcSession::GetSessionErrorMsg() {
2033 std::ostringstream desc;
2034 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2035 desc << kSessionErrorDesc << error_desc() << ".";
2036 return desc.str();
2037}
2038
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002039// We need to check the local/remote description for the Transport instead of
2040// the session, because a new Transport added during renegotiation may have
2041// them unset while the session has them set from the previous negotiation.
2042// Not doing so may trigger the auto generation of transport description and
2043// mess up DTLS identity information, ICE credential, etc.
2044bool WebRtcSession::ReadyToUseRemoteCandidate(
2045 const IceCandidateInterface* candidate,
2046 const SessionDescriptionInterface* remote_desc,
2047 bool* valid) {
2048 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002049
2050 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002051 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002052
deadbeefd59daf82015-10-14 15:02:44 -07002053 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002054 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002055 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002056
2057 size_t mediacontent_index =
2058 static_cast<size_t>(candidate->sdp_mline_index());
2059 size_t remote_content_size =
2060 current_remote_desc->description()->contents().size();
2061 if (mediacontent_index >= remote_content_size) {
2062 LOG(LS_ERROR)
2063 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2064
2065 *valid = false;
2066 return false;
2067 }
2068
2069 cricket::ContentInfo content =
2070 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002071 cricket::BaseChannel* channel = GetChannel(content.name);
2072 if (!channel) {
2073 return false;
2074 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002075
deadbeefd59daf82015-10-14 15:02:44 -07002076 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002077 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002078}
2079
deadbeefcbecd352015-09-23 11:50:27 -07002080void WebRtcSession::OnTransportControllerGatheringState(
2081 cricket::IceGatheringState state) {
2082 ASSERT(signaling_thread()->IsCurrent());
2083 if (state == cricket::kIceGatheringGathering) {
2084 if (ice_observer_) {
2085 ice_observer_->OnIceGatheringChange(
2086 PeerConnectionInterface::kIceGatheringGathering);
2087 }
2088 } else if (state == cricket::kIceGatheringComplete) {
2089 if (ice_observer_) {
2090 ice_observer_->OnIceGatheringChange(
2091 PeerConnectionInterface::kIceGatheringComplete);
2092 ice_observer_->OnIceComplete();
2093 }
2094 }
2095}
2096
2097void WebRtcSession::ReportTransportStats() {
2098 // Use a set so we don't report the same stats twice if two channels share
2099 // a transport.
2100 std::set<std::string> transport_names;
2101 if (voice_channel()) {
2102 transport_names.insert(voice_channel()->transport_name());
2103 }
2104 if (video_channel()) {
2105 transport_names.insert(video_channel()->transport_name());
2106 }
2107 if (data_channel()) {
2108 transport_names.insert(data_channel()->transport_name());
2109 }
2110 for (const auto& name : transport_names) {
2111 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002112 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002113 ReportBestConnectionState(stats);
2114 ReportNegotiatedCiphers(stats);
2115 }
2116 }
2117}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002118// Walk through the ConnectionInfos to gather best connection usage
2119// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002120void WebRtcSession::ReportBestConnectionState(
2121 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002122 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002123 for (cricket::TransportChannelStatsList::const_iterator it =
2124 stats.channel_stats.begin();
2125 it != stats.channel_stats.end(); ++it) {
2126 for (cricket::ConnectionInfos::const_iterator it_info =
2127 it->connection_infos.begin();
2128 it_info != it->connection_infos.end(); ++it_info) {
2129 if (!it_info->best_connection) {
2130 continue;
2131 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002132
2133 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2134 const cricket::Candidate& local = it_info->local_candidate;
2135 const cricket::Candidate& remote = it_info->remote_candidate;
2136
2137 // Increment the counter for IceCandidatePairType.
2138 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2139 (local.type() == RELAY_PORT_TYPE &&
2140 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2141 type = kEnumCounterIceCandidatePairTypeTcp;
2142 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2143 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002144 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002145 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002146 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002147 metrics_observer_->IncrementEnumCounter(
2148 type, GetIceCandidatePairCounter(local, remote),
2149 kIceCandidatePairMax);
2150
2151 // Increment the counter for IP type.
2152 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002153 metrics_observer_->IncrementEnumCounter(
2154 kEnumCounterAddressFamily, kBestConnections_IPv4,
2155 kPeerConnectionAddressFamilyCounter_Max);
2156
2157 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002158 metrics_observer_->IncrementEnumCounter(
2159 kEnumCounterAddressFamily, kBestConnections_IPv6,
2160 kPeerConnectionAddressFamilyCounter_Max);
2161 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002162 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002163 }
2164
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002165 return;
2166 }
2167 }
2168}
2169
jbauchac8869e2015-07-03 01:36:14 -07002170void WebRtcSession::ReportNegotiatedCiphers(
2171 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002172 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002173 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2174 return;
2175 }
2176
2177 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07002178 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002179 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002180 return;
2181 }
2182
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002183 PeerConnectionEnumCounterType srtp_counter_type;
2184 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002185 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002186 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2187 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002188 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002189 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2190 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002191 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002192 srtp_counter_type = kEnumCounterDataSrtpCipher;
2193 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002194 } else {
2195 RTC_NOTREACHED();
2196 return;
2197 }
2198
2199 if (!srtp_cipher.empty()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002200 metrics_observer_->IncrementSparseEnumCounter(
2201 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002202 }
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002203 if (ssl_cipher) {
2204 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002205 }
2206}
2207
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208} // namespace webrtc