blob: 5b58c243192b43369879119e345906d37ca4a35b [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"
stefanc1aeaf02015-10-15 07:26:07 -070054#include "webrtc/call.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000055#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070056#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58using cricket::ContentInfo;
59using cricket::ContentInfos;
60using cricket::MediaContentDescription;
61using cricket::SessionDescription;
62using cricket::TransportInfo;
63
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070064using cricket::LOCAL_PORT_TYPE;
65using cricket::STUN_PORT_TYPE;
66using cricket::RELAY_PORT_TYPE;
67using cricket::PRFLX_PORT_TYPE;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069namespace webrtc {
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
73 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000074const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kInvalidCandidates[] = "Description contains invalid candidates.";
76const char kInvalidSdp[] = "Invalid session description.";
77const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
79const char kPushDownTDFailed[] =
80 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000081const char kSdpWithoutDtlsFingerprint[] =
82 "Called with SDP without DTLS fingerprint.";
83const char kSdpWithoutSdesCrypto[] =
84 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000085const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000086 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000088const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000089const char kDtlsSetupFailureRtp[] =
90 "Couldn't set up DTLS-SRTP on RTP channel.";
91const char kDtlsSetupFailureRtcp[] =
92 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070093const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000094const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070096IceCandidatePairType GetIceCandidatePairCounter(
97 const cricket::Candidate& local,
98 const cricket::Candidate& remote) {
99 const auto& l = local.type();
100 const auto& r = remote.type();
101 const auto& host = LOCAL_PORT_TYPE;
102 const auto& srflx = STUN_PORT_TYPE;
103 const auto& relay = RELAY_PORT_TYPE;
104 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700105 if (l == host && r == host) {
106 bool local_private = IPIsPrivate(local.address().ipaddr());
107 bool remote_private = IPIsPrivate(remote.address().ipaddr());
108 if (local_private) {
109 if (remote_private) {
110 return kIceCandidatePairHostPrivateHostPrivate;
111 } else {
112 return kIceCandidatePairHostPrivateHostPublic;
113 }
114 } else {
115 if (remote_private) {
116 return kIceCandidatePairHostPublicHostPrivate;
117 } else {
118 return kIceCandidatePairHostPublicHostPublic;
119 }
120 }
121 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700122 if (l == host && r == srflx)
123 return kIceCandidatePairHostSrflx;
124 if (l == host && r == relay)
125 return kIceCandidatePairHostRelay;
126 if (l == host && r == prflx)
127 return kIceCandidatePairHostPrflx;
128 if (l == srflx && r == host)
129 return kIceCandidatePairSrflxHost;
130 if (l == srflx && r == srflx)
131 return kIceCandidatePairSrflxSrflx;
132 if (l == srflx && r == relay)
133 return kIceCandidatePairSrflxRelay;
134 if (l == srflx && r == prflx)
135 return kIceCandidatePairSrflxPrflx;
136 if (l == relay && r == host)
137 return kIceCandidatePairRelayHost;
138 if (l == relay && r == srflx)
139 return kIceCandidatePairRelaySrflx;
140 if (l == relay && r == relay)
141 return kIceCandidatePairRelayRelay;
142 if (l == relay && r == prflx)
143 return kIceCandidatePairRelayPrflx;
144 if (l == prflx && r == host)
145 return kIceCandidatePairPrflxHost;
146 if (l == prflx && r == srflx)
147 return kIceCandidatePairPrflxSrflx;
148 if (l == prflx && r == relay)
149 return kIceCandidatePairPrflxRelay;
150 return kIceCandidatePairMax;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153// Compares |answer| against |offer|. Comparision is done
154// for number of m-lines in answer against offer. If matches true will be
155// returned otherwise false.
156static bool VerifyMediaDescriptions(
157 const SessionDescription* answer, const SessionDescription* offer) {
158 if (offer->contents().size() != answer->contents().size())
159 return false;
160
161 for (size_t i = 0; i < offer->contents().size(); ++i) {
162 if ((offer->contents()[i].name) != answer->contents()[i].name) {
163 return false;
164 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000165 const MediaContentDescription* offer_mdesc =
166 static_cast<const MediaContentDescription*>(
167 offer->contents()[i].description);
168 const MediaContentDescription* answer_mdesc =
169 static_cast<const MediaContentDescription*>(
170 answer->contents()[i].description);
171 if (offer_mdesc->type() != answer_mdesc->type()) {
172 return false;
173 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 }
175 return true;
176}
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178// Checks that each non-rejected content has SDES crypto keys or a DTLS
179// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
180// keys, will be caught in Transport negotiation, and backstopped by Channel's
181// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182static bool VerifyCrypto(const SessionDescription* desc,
183 bool dtls_enabled,
184 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 const ContentInfos& contents = desc->contents();
186 for (size_t index = 0; index < contents.size(); ++index) {
187 const ContentInfo* cinfo = &contents[index];
188 if (cinfo->rejected) {
189 continue;
190 }
191
192 // If the content isn't rejected, crypto must be present.
193 const MediaContentDescription* media =
194 static_cast<const MediaContentDescription*>(cinfo->description);
195 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
196 if (!media || !tinfo) {
197 // Something is not right.
198 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 return false;
201 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000203 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000204 LOG(LS_WARNING) <<
205 "Session description must have DTLS fingerprint if DTLS enabled.";
206 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000207 return false;
208 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000209 } else {
210 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000211 LOG(LS_WARNING) <<
212 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000213 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 return false;
215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217 }
218
219 return true;
220}
221
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000222// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
224 const ContentInfos& contents = desc->contents();
225 for (size_t index = 0; index < contents.size(); ++index) {
226 const ContentInfo* cinfo = &contents[index];
227 if (cinfo->rejected) {
228 continue;
229 }
230
231 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
232 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
233 if (!tinfo) {
234 // Something is not right.
235 LOG(LS_ERROR) << kInvalidSdp;
236 return false;
237 }
238 if (tinfo->description.ice_ufrag.empty() ||
239 tinfo->description.ice_pwd.empty()) {
240 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
241 return false;
242 }
243 }
244 return true;
245}
246
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247// Forces |sdesc->crypto_required| to the appropriate state based on the
248// current security policy, to ensure a failure occurs if there is an error
249// in crypto negotiation.
250// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000251static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
252 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000253 if (!sdesc) {
254 return;
255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 // Updating the |crypto_required_| in MediaContentDescription to the
258 // appropriate state based on the current security policy.
259 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
260 iter != sdesc->contents().end(); ++iter) {
261 if (cricket::IsMediaContent(&*iter)) {
262 MediaContentDescription* mdesc =
263 static_cast<MediaContentDescription*> (iter->description);
264 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000265 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266 }
267 }
268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
272 const std::string& track_id,
273 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 const cricket::ContentInfo* audio_info =
275 cricket::GetFirstAudioContent(session_description);
276 if (!audio_info) {
277 LOG(LS_ERROR) << "Audio not used in this call";
278 return false;
279 }
280
281 const cricket::MediaContentDescription* audio_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const cricket::StreamParams* stream =
285 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
286 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 return false;
288 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289
290 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return true;
292}
293
294static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200295 uint32_t ssrc,
296 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 ASSERT(track_id != NULL);
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const cricket::ContentInfo* audio_info =
300 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000301 if (audio_info) {
302 const cricket::MediaContentDescription* audio_content =
303 static_cast<const cricket::MediaContentDescription*>(
304 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000306 const auto* found =
307 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
308 if (found) {
309 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000310 return true;
311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
314 const cricket::ContentInfo* video_info =
315 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000316 if (video_info) {
317 const cricket::MediaContentDescription* video_content =
318 static_cast<const cricket::MediaContentDescription*>(
319 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000321 const auto* found =
322 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
323 if (found) {
324 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000325 return true;
326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328 return false;
329}
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331static bool BadSdp(const std::string& source,
332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
335 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700336 desc << "Failed to set " << source;
337 if (!type.empty()) {
338 desc << " " << type;
339 }
340 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return false;
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000356 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360static bool BadLocalSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
364}
365
366static bool BadRemoteSdp(const std::string& type,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
370}
371
372static bool BadOfferSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
376}
377
378static bool BadPranswerSdp(cricket::ContentSource source,
379 const std::string& reason,
380 std::string* err_desc) {
381 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
382 reason, err_desc);
383}
384
385static bool BadAnswerSdp(cricket::ContentSource source,
386 const std::string& reason,
387 std::string* err_desc) {
388 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389}
390
deadbeefd59daf82015-10-14 15:02:44 -0700391#define GET_STRING_OF_STATE(state) \
392 case webrtc::WebRtcSession::state: \
393 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 break;
395
deadbeefd59daf82015-10-14 15:02:44 -0700396static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 std::string result;
398 switch (state) {
399 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700400 GET_STRING_OF_STATE(STATE_SENTOFFER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
402 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
403 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700405 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
407 ASSERT(false);
408 break;
409 }
410 return result;
411}
412
deadbeefd59daf82015-10-14 15:02:44 -0700413#define GET_STRING_OF_ERROR_CODE(err) \
414 case webrtc::WebRtcSession::err: \
415 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 break;
417
deadbeefd59daf82015-10-14 15:02:44 -0700418static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 std::string result;
420 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
423 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 default:
deadbeefd59daf82015-10-14 15:02:44 -0700425 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 break;
427 }
428 return result;
429}
430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431static std::string MakeErrorString(const std::string& error,
432 const std::string& desc) {
433 std::ostringstream ret;
434 ret << error << " " << desc;
435 return ret.str();
436}
437
438static std::string MakeTdErrorString(const std::string& desc) {
439 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000442// Set |option| to the highest-priority value of |key| in the optional
443// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700444template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000445static void SetOptionFromOptionalConstraint(
446 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700447 const std::string& key,
448 rtc::Maybe<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000449 if (!constraints) {
450 return;
451 }
452 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000453 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000454 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000455 if (rtc::FromString(string_value, &value)) {
kwiberg102c6a62015-10-30 02:47:38 -0700456 *option = rtc::Maybe<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000457 }
458 }
459}
460
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000462 PeerConnectionInterface::IceTransportsType type) {
463 switch (type) {
464 case PeerConnectionInterface::kNone:
465 return cricket::CF_NONE;
466 case PeerConnectionInterface::kRelay:
467 return cricket::CF_RELAY;
468 case PeerConnectionInterface::kNoHost:
469 return (cricket::CF_ALL & ~cricket::CF_HOST);
470 case PeerConnectionInterface::kAll:
471 return cricket::CF_ALL;
472 default: ASSERT(false);
473 }
474 return cricket::CF_NONE;
475}
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477// Help class used to remember if a a remote peer has requested ice restart by
478// by sending a description with new ice ufrag and password.
479class IceRestartAnswerLatch {
480 public:
481 IceRestartAnswerLatch() : ice_restart_(false) { }
482
wu@webrtc.org91053e72013-08-10 07:18:04 +0000483 // Returns true if CheckForRemoteIceRestart has been called with a new session
484 // description where ice password and ufrag has changed since last time
485 // Reset() was called.
486 bool Get() const {
487 return ice_restart_;
488 }
489
490 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 if (ice_restart_) {
492 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
495
honghaiz503726c2015-07-31 12:37:38 -0700496 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
497 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700499 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 }
501 const SessionDescription* new_sd = new_desc->description();
502 const SessionDescription* old_sd = old_desc->description();
503 const ContentInfos& contents = new_sd->contents();
504 for (size_t index = 0; index < contents.size(); ++index) {
505 const ContentInfo* cinfo = &contents[index];
506 if (cinfo->rejected) {
507 continue;
508 }
509 // If the content isn't rejected, check if ufrag and password has
510 // changed.
511 const cricket::TransportDescription* new_transport_desc =
512 new_sd->GetTransportDescriptionByName(cinfo->name);
513 const cricket::TransportDescription* old_transport_desc =
514 old_sd->GetTransportDescriptionByName(cinfo->name);
515 if (!new_transport_desc || !old_transport_desc) {
516 // No transport description exist. This is not an ice restart.
517 continue;
518 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000519 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
520 old_transport_desc->ice_pwd,
521 new_transport_desc->ice_ufrag,
522 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 LOG(LS_INFO) << "Remote peer request ice restart.";
524 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700525 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
527 }
honghaiz503726c2015-07-31 12:37:38 -0700528 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 }
530
531 private:
532 bool ice_restart_;
533};
534
stefanc1aeaf02015-10-15 07:26:07 -0700535WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700536 rtc::Thread* signaling_thread,
537 rtc::Thread* worker_thread,
538 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700539 : signaling_thread_(signaling_thread),
540 worker_thread_(worker_thread),
541 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // RFC 3264: The numeric value of the session id and version in the
543 // o line MUST be representable with a "64 bit signed integer".
544 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700545 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
546 transport_controller_(new cricket::TransportController(signaling_thread,
547 worker_thread,
548 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700549 media_controller_(media_controller),
550 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 ice_observer_(NULL),
552 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700553 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000555 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000557 ice_restart_latch_(new IceRestartAnswerLatch),
558 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700559 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
560 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700561 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700562 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700563 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700564 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700565 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700566 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700567 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568}
569
570WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700571 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000572 // Destroy video_channel_ first since it may have a pointer to the
573 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000574 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 SignalVideoChannelDestroyed();
576 channel_manager_->DestroyVideoChannel(video_channel_.release());
577 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000578 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000579 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200580 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000581 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000582 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 SignalDataChannelDestroyed();
584 channel_manager_->DestroyDataChannel(data_channel_.release());
585 }
deadbeefd59daf82015-10-14 15:02:44 -0700586
587 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588}
589
wu@webrtc.org91053e72013-08-10 07:18:04 +0000590bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000591 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700592 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200593 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200594 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
595 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700596 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700597 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000598
Henrik Boström87713d02015-08-25 09:53:21 +0200599 // Obtain a certificate from RTCConfiguration if any were provided (optional).
600 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
601 if (!rtc_configuration.certificates.empty()) {
602 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
603 // just picking the first one. The decision should be made based on the DTLS
604 // handshake. The DTLS negotiations need to know about all certificates.
605 certificate = rtc_configuration.certificates[0];
606 }
607
honghaiz1f429e32015-09-28 07:57:34 -0700608 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700609
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 // TODO(perkj): Take |constraints| into consideration. Return false if not all
611 // mandatory constraints can be fulfilled. Note that |constraints|
612 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000614
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000615 if (options.disable_encryption) {
616 dtls_enabled_ = false;
617 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200618 // Enable DTLS by default if we have an identity store or a certificate.
619 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000620 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700621 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
622 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000623 dtls_enabled_ = value;
624 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000625 }
626
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000628 // It takes precendence over the disable_sctp_data_channels
629 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 if (FindConstraint(
631 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
632 &value, NULL) && value) {
633 LOG(LS_INFO) << "Allowing RTP data engine.";
634 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000636 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000637 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000638 LOG(LS_INFO) << "Allowing SCTP data engine.";
639 data_channel_type_ = cricket::DCT_SCTP;
640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642
wu@webrtc.orgde305012013-10-31 15:40:38 +0000643 // Find DSCP constraint.
644 if (FindConstraint(
645 constraints,
646 MediaConstraintsInterface::kEnableDscp,
647 &value, NULL)) {
kwiberg102c6a62015-10-30 02:47:38 -0700648 audio_options_.dscp = rtc::Maybe<bool>(value);
649 video_options_.dscp = rtc::Maybe<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000650 }
651
652 // Find Suspend Below Min Bitrate constraint.
653 if (FindConstraint(
654 constraints,
655 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
656 &value,
657 NULL)) {
kwiberg102c6a62015-10-30 02:47:38 -0700658 video_options_.suspend_below_min_bitrate = rtc::Maybe<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000659 }
660
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000661 SetOptionFromOptionalConstraint(constraints,
662 MediaConstraintsInterface::kScreencastMinBitrate,
663 &video_options_.screencast_min_bitrate);
664
665 // Find constraints for cpu overuse detection.
666 SetOptionFromOptionalConstraint(constraints,
667 MediaConstraintsInterface::kCpuUnderuseThreshold,
668 &video_options_.cpu_underuse_threshold);
669 SetOptionFromOptionalConstraint(constraints,
670 MediaConstraintsInterface::kCpuOveruseThreshold,
671 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000672 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000673 MediaConstraintsInterface::kCpuOveruseDetection,
674 &video_options_.cpu_overuse_detection);
675 SetOptionFromOptionalConstraint(constraints,
676 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
677 &video_options_.cpu_overuse_encode_usage);
678 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000679 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
680 &video_options_.cpu_underuse_encode_rsd_threshold);
681 SetOptionFromOptionalConstraint(constraints,
682 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
683 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000684
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000685 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000686 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
687 &video_options_.unsignalled_recv_stream_limit);
kwiberg102c6a62015-10-30 02:47:38 -0700688 if (video_options_.unsignalled_recv_stream_limit) {
689 video_options_.unsignalled_recv_stream_limit = rtc::Maybe<int>(
690 std::max(0, std::min(kMaxUnsignalledRecvStreams,
691 *video_options_.unsignalled_recv_stream_limit)));
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000692 }
693
694 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000695 MediaConstraintsInterface::kHighStartBitrate,
696 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000697
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000698 SetOptionFromOptionalConstraint(constraints,
699 MediaConstraintsInterface::kCombinedAudioVideoBwe,
700 &audio_options_.combined_audio_video_bwe);
701
kwiberg102c6a62015-10-30 02:47:38 -0700702 audio_options_.audio_jitter_buffer_max_packets =
703 rtc::Maybe<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200704
kwiberg102c6a62015-10-30 02:47:38 -0700705 audio_options_.audio_jitter_buffer_fast_accelerate =
706 rtc::Maybe<bool>(rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 const cricket::VideoCodec default_codec(
709 JsepSessionDescription::kDefaultVideoCodecId,
710 JsepSessionDescription::kDefaultVideoCodecName,
711 JsepSessionDescription::kMaxVideoCodecWidth,
712 JsepSessionDescription::kMaxVideoCodecHeight,
713 JsepSessionDescription::kDefaultVideoCodecFramerate,
714 JsepSessionDescription::kDefaultVideoCodecPreference);
715 channel_manager_->SetDefaultVideoEncoderConfig(
716 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000717
Henrik Boström87713d02015-08-25 09:53:21 +0200718 if (!dtls_enabled_) {
719 // Construct with DTLS disabled.
720 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700721 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200722 } else {
723 // Construct with DTLS enabled.
724 if (!certificate) {
725 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700726 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200727 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700728 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
729 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200730 } else {
731 // Use the already generated certificate.
732 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700733 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200734 }
735 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000736
Henrik Boströmd8281982015-08-27 10:12:24 +0200737 webrtc_session_desc_factory_->SignalCertificateReady.connect(
738 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000739
wu@webrtc.org97077a32013-10-25 21:18:33 +0000740 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000741 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000742 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000743 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200744 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700745
746 if (rtc_configuration.enable_localhost_ice_candidate) {
747 port_allocator()->set_flags(
748 port_allocator()->flags() |
749 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
750 }
751
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 return true;
753}
754
deadbeefd59daf82015-10-14 15:02:44 -0700755void WebRtcSession::Close() {
756 SetState(STATE_CLOSED);
757 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000758 ASSERT(!voice_channel_);
759 ASSERT(!video_channel_);
760 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761}
762
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000763void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
764 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765}
766
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000767cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
768 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769}
770
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000771bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700772 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000773 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
774 << "SSL Role of the session.";
775 return false;
776 }
777
deadbeefd59daf82015-10-14 15:02:44 -0700778 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000779}
780
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000781void WebRtcSession::CreateOffer(
782 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700783 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
784 const cricket::MediaSessionOptions& session_options) {
785 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000786}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787
deadbeefab9b2d12015-10-14 11:33:11 -0700788void WebRtcSession::CreateAnswer(
789 CreateSessionDescriptionObserver* observer,
790 const MediaConstraintsInterface* constraints,
791 const cricket::MediaSessionOptions& session_options) {
792 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
793 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794}
795
796bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
797 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700798 ASSERT(signaling_thread()->IsCurrent());
799
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000800 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000801 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000802
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000803 // Validate SDP.
804 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
805 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
deadbeefd59daf82015-10-14 15:02:44 -0700808 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000809 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700811 initial_offerer_ = true;
812 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000815 cricket::SecurePolicy sdes_policy =
816 webrtc_session_desc_factory_->SdesPolicy();
817 cricket::CryptoType crypto_required = dtls_enabled_ ?
818 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
819 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000821 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000823 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
825 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000826 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 // TODO(mallinath) - Handle CreateChannel failure, as new local description
828 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000829 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
deadbeefcbecd352015-09-23 11:50:27 -0700832 // Remove unused channels if MediaContentDescription is rejected.
833 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000835 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 return false;
837 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000838
deadbeefd59daf82015-10-14 15:02:44 -0700839 if (remote_desc_) {
840 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700841 UseCandidatesInSessionDescription(remote_desc_.get());
842 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843
deadbeefd59daf82015-10-14 15:02:44 -0700844 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000845 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 }
847 return true;
848}
849
850bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
851 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700852 ASSERT(signaling_thread()->IsCurrent());
853
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000854 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000855 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000856
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000857 // Validate SDP.
858 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
859 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 }
861
deadbeefd59daf82015-10-14 15:02:44 -0700862 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
863 remote_desc_.release());
864 remote_desc_.reset(desc_temp.release());
865
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000867 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 if (action == kOffer && !CreateChannels(desc->description())) {
869 // TODO(mallinath) - Handle CreateChannel failure, as new local description
870 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000871 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 }
873
deadbeefcbecd352015-09-23 11:50:27 -0700874 // Remove unused channels if MediaContentDescription is rejected.
875 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876
877 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
878 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000879 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 return false;
881 }
882
deadbeefd59daf82015-10-14 15:02:44 -0700883 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 // Check if this new SessionDescription contains new ice ufrag and password
888 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700889 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700890 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700891 // We retain all received candidates only if ICE is not restarted.
892 // When ICE is restarted, all previous candidates belong to an old generation
893 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700894 // TODO(deadbeef): This goes against the W3C spec which says the remote
895 // description should only contain candidates from the last set remote
896 // description plus any candidates added since then. We should remove this
897 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700898 if (!ice_restart) {
899 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700900 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700901 }
902
deadbeefd59daf82015-10-14 15:02:44 -0700903 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000904 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000906
907 // Set the the ICE connection state to connecting since the connection may
908 // become writable with peer reflexive candidates before any remote candidate
909 // is signaled.
910 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
911 // is to have a new signal the indicates a change in checking state from the
912 // transport and expose a new checking() member from transport that can be
913 // read to determine the current checking state. The existing SignalConnecting
914 // actually means "gathering candidates", so cannot be be used here.
915 if (desc->type() != SessionDescriptionInterface::kOffer &&
916 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
917 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
918 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return true;
920}
921
deadbeefd59daf82015-10-14 15:02:44 -0700922void WebRtcSession::LogState(State old_state, State new_state) {
923 LOG(LS_INFO) << "Session:" << id()
924 << " Old state:" << GetStateString(old_state)
925 << " New state:" << GetStateString(new_state);
926}
927
928void WebRtcSession::SetState(State state) {
929 ASSERT(signaling_thread_->IsCurrent());
930 if (state != state_) {
931 LogState(state_, state);
932 state_ = state;
933 SignalState(this, state_);
934 }
935}
936
937void WebRtcSession::SetError(Error error, const std::string& error_desc) {
938 ASSERT(signaling_thread_->IsCurrent());
939 if (error != error_) {
940 error_ = error;
941 error_desc_ = error_desc;
942 }
943}
944
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945bool WebRtcSession::UpdateSessionState(
946 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700948 ASSERT(signaling_thread()->IsCurrent());
949
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 // If there's already a pending error then no state transition should happen.
951 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700952 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000953 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000955 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
956 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 }
deadbeefd59daf82015-10-14 15:02:44 -0700958 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
959 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000960 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700961 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000962 }
deadbeefd59daf82015-10-14 15:02:44 -0700963 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000964 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 }
966 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000967 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
968 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 }
970 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700971 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
972 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000973 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700974 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000975 }
deadbeefd59daf82015-10-14 15:02:44 -0700976 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000977 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 }
979 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000980 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
981 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 }
deadbeefcbecd352015-09-23 11:50:27 -0700983 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700984 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700985 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700986 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700987 if (local_bundle && remote_bundle) {
988 // The answerer decides the transport to bundle on
989 const cricket::ContentGroup* answer_bundle =
990 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
991 if (!EnableBundle(*answer_bundle)) {
992 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
993 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
994 }
995 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700997 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000998 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700999 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001000 }
deadbeefd59daf82015-10-14 15:02:44 -07001001 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001002 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
1004 }
1005 return true;
1006}
1007
1008WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1009 if (type == SessionDescriptionInterface::kOffer) {
1010 return WebRtcSession::kOffer;
1011 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1012 return WebRtcSession::kPrAnswer;
1013 } else if (type == SessionDescriptionInterface::kAnswer) {
1014 return WebRtcSession::kAnswer;
1015 }
1016 ASSERT(false && "unknown action type");
1017 return WebRtcSession::kOffer;
1018}
1019
deadbeefd59daf82015-10-14 15:02:44 -07001020bool WebRtcSession::PushdownMediaDescription(
1021 cricket::ContentAction action,
1022 cricket::ContentSource source,
1023 std::string* err) {
1024 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1025 if (!ch) {
1026 return true;
1027 } else if (source == cricket::CS_LOCAL) {
1028 return ch->PushdownLocalDescription(local_desc_->description(), action,
1029 err);
1030 } else {
1031 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1032 err);
1033 }
1034 };
1035
1036 return (set_content(voice_channel()) &&
1037 set_content(video_channel()) &&
1038 set_content(data_channel()));
1039}
1040
1041bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1042 cricket::ContentAction action,
1043 std::string* error_desc) {
1044 RTC_DCHECK(signaling_thread()->IsCurrent());
1045
1046 if (source == cricket::CS_LOCAL) {
1047 return PushdownLocalTransportDescription(local_desc_->description(), action,
1048 error_desc);
1049 }
1050 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1051 error_desc);
1052}
1053
1054bool WebRtcSession::PushdownLocalTransportDescription(
1055 const SessionDescription* sdesc,
1056 cricket::ContentAction action,
1057 std::string* err) {
1058 RTC_DCHECK(signaling_thread()->IsCurrent());
1059
1060 if (!sdesc) {
1061 return false;
1062 }
1063
1064 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1065 if (!transport_controller_->SetLocalTransportDescription(
1066 tinfo.content_name, tinfo.description, action, err)) {
1067 return false;
1068 }
1069 }
1070
1071 return true;
1072}
1073
1074bool WebRtcSession::PushdownRemoteTransportDescription(
1075 const SessionDescription* sdesc,
1076 cricket::ContentAction action,
1077 std::string* err) {
1078 RTC_DCHECK(signaling_thread()->IsCurrent());
1079
1080 if (!sdesc) {
1081 return false;
1082 }
1083
1084 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1085 if (!transport_controller_->SetRemoteTransportDescription(
1086 tinfo.content_name, tinfo.description, action, err)) {
1087 return false;
1088 }
1089 }
1090
1091 return true;
1092}
1093
1094bool WebRtcSession::GetTransportDescription(
1095 const SessionDescription* description,
1096 const std::string& content_name,
1097 cricket::TransportDescription* tdesc) {
1098 if (!description || !tdesc) {
1099 return false;
1100 }
1101 const TransportInfo* transport_info =
1102 description->GetTransportInfoByName(content_name);
1103 if (!transport_info) {
1104 return false;
1105 }
1106 *tdesc = transport_info->description;
1107 return true;
1108}
1109
1110bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001111 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001112 return (GetChannelTransportStats(voice_channel(), stats) &&
1113 GetChannelTransportStats(video_channel(), stats) &&
1114 GetChannelTransportStats(data_channel(), stats));
1115}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001116
deadbeefcbecd352015-09-23 11:50:27 -07001117bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001118 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001119 ASSERT(signaling_thread()->IsCurrent());
1120 if (!ch) {
1121 // Not using this channel.
1122 return true;
1123 }
1124
1125 const std::string& content_name = ch->content_name();
1126 const std::string& transport_name = ch->transport_name();
1127 stats->proxy_to_transport[content_name] = transport_name;
1128 if (stats->transport_stats.find(transport_name) !=
1129 stats->transport_stats.end()) {
1130 // Transport stats already done for this transport.
1131 return true;
1132 }
1133
1134 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001135 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001136 return false;
1137 }
1138
1139 stats->transport_stats[transport_name] = tstats;
1140 return true;
1141}
1142
1143bool WebRtcSession::GetLocalCertificate(
1144 const std::string& transport_name,
1145 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1146 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001147 return transport_controller_->GetLocalCertificate(transport_name,
1148 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001149}
1150
1151bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1152 rtc::SSLCertificate** cert) {
1153 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001154 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001155}
1156
1157cricket::BaseChannel* WebRtcSession::GetChannel(
1158 const std::string& content_name) {
1159 if (voice_channel() && voice_channel()->content_name() == content_name) {
1160 return voice_channel();
1161 }
1162 if (video_channel() && video_channel()->content_name() == content_name) {
1163 return video_channel();
1164 }
1165 if (data_channel() && data_channel()->content_name() == content_name) {
1166 return data_channel();
1167 }
1168 return nullptr;
1169}
1170
1171bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1172 const std::string* first_content_name = bundle.FirstContentName();
1173 if (!first_content_name) {
1174 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1175 return false;
1176 }
1177 const std::string& transport_name = *first_content_name;
1178 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1179
1180 auto maybe_set_transport = [this, bundle, transport_name,
1181 first_channel](cricket::BaseChannel* ch) {
1182 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001183 return true;
1184 }
1185
deadbeefcbecd352015-09-23 11:50:27 -07001186 if (ch->transport_name() == transport_name) {
1187 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1188 << " on " << transport_name << ".";
1189 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001190 }
torbjornga81a42f2015-09-23 02:16:58 -07001191
deadbeefcbecd352015-09-23 11:50:27 -07001192 if (!ch->SetTransport(transport_name)) {
1193 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1194 return false;
1195 }
1196 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1197 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001198 return true;
1199 };
1200
deadbeefcbecd352015-09-23 11:50:27 -07001201 if (!maybe_set_transport(voice_channel()) ||
1202 !maybe_set_transport(video_channel()) ||
1203 !maybe_set_transport(data_channel())) {
1204 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001205 }
deadbeefcbecd352015-09-23 11:50:27 -07001206
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001207 return true;
1208}
1209
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001211 if (!remote_desc_) {
1212 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1213 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 return false;
1215 }
1216
1217 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001218 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 return false;
1220 }
1221
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001222 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001223 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1224 if (!valid) {
1225 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 }
1227
1228 // Add this candidate to the remote session description.
1229 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001230 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 return false;
1232 }
1233
deadbeefd59daf82015-10-14 15:02:44 -07001234 if (ready) {
1235 return UseCandidate(candidate);
1236 } else {
1237 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1238 return true;
1239 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240}
1241
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001242bool WebRtcSession::SetIceTransports(
1243 PeerConnectionInterface::IceTransportsType type) {
1244 return port_allocator()->set_candidate_filter(
1245 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001246}
1247
deadbeefd59daf82015-10-14 15:02:44 -07001248cricket::IceConfig WebRtcSession::ParseIceConfig(
1249 const PeerConnectionInterface::RTCConfiguration& config) const {
1250 cricket::IceConfig ice_config;
1251 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1252 ice_config.gather_continually = (config.continual_gathering_policy ==
1253 PeerConnectionInterface::GATHER_CONTINUALLY);
1254 return ice_config;
1255}
1256
1257void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1258 transport_controller_->SetIceConfig(config);
1259}
1260
1261void WebRtcSession::MaybeStartGathering() {
1262 transport_controller_->MaybeStartGathering();
1263}
1264
Peter Boström0c4e06b2015-10-07 12:23:21 +02001265bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1266 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001267 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001269 }
1270 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271}
1272
Peter Boström0c4e06b2015-10-07 12:23:21 +02001273bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1274 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001275 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001276 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001277 }
1278 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279}
1280
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001281std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001283 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 return desc.str();
1285}
1286
solenbergd4cec0d2015-10-09 08:55:48 -07001287void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 ASSERT(signaling_thread()->IsCurrent());
1289 if (!voice_channel_) {
1290 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1291 return;
1292 }
solenberg4bac9c52015-10-09 02:32:53 -07001293 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1294 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 // otherwise. This in the normal case when the underlying media channel has
1296 // already been deleted.
1297 ASSERT(enable == false);
1298 }
1299}
1300
Peter Boström0c4e06b2015-10-07 12:23:21 +02001301void WebRtcSession::SetAudioSend(uint32_t ssrc,
1302 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001303 const cricket::AudioOptions& options,
1304 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 ASSERT(signaling_thread()->IsCurrent());
1306 if (!voice_channel_) {
1307 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1308 return;
1309 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001310 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001311 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313}
1314
Peter Boström0c4e06b2015-10-07 12:23:21 +02001315void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001316 ASSERT(signaling_thread()->IsCurrent());
1317 ASSERT(volume >= 0 && volume <= 10);
1318 if (!voice_channel_) {
1319 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1320 return;
1321 }
1322
solenberg4bac9c52015-10-09 02:32:53 -07001323 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001324 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001325 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001326}
1327
Peter Boström0c4e06b2015-10-07 12:23:21 +02001328bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 cricket::VideoCapturer* camera) {
1330 ASSERT(signaling_thread()->IsCurrent());
1331
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001332 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1334 // support video.
1335 LOG(LS_WARNING) << "Video not used in this call.";
1336 return false;
1337 }
1338 if (!video_channel_->SetCapturer(ssrc, camera)) {
1339 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1340 // This in the normal case when the underlying media channel has already
1341 // been deleted.
1342 ASSERT(camera == NULL);
1343 return false;
1344 }
1345 return true;
1346}
1347
Peter Boström0c4e06b2015-10-07 12:23:21 +02001348void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 bool enable,
1350 cricket::VideoRenderer* renderer) {
1351 ASSERT(signaling_thread()->IsCurrent());
1352 if (!video_channel_) {
1353 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1354 return;
1355 }
1356 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1357 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1358 // This in the normal case when the underlying media channel has already
1359 // been deleted.
1360 ASSERT(renderer == NULL);
1361 }
1362}
1363
Peter Boström0c4e06b2015-10-07 12:23:21 +02001364void WebRtcSession::SetVideoSend(uint32_t ssrc,
1365 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 const cricket::VideoOptions* options) {
1367 ASSERT(signaling_thread()->IsCurrent());
1368 if (!video_channel_) {
1369 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1370 return;
1371 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001372 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1374 // This in the normal case when the underlying media channel has already
1375 // been deleted.
1376 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378}
1379
1380bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1381 ASSERT(signaling_thread()->IsCurrent());
1382 if (!voice_channel_) {
1383 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1384 return false;
1385 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001386 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1388 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001389 if (!local_desc_ ||
1390 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 &send_ssrc)) {
1392 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1393 return false;
1394 }
1395 return voice_channel_->CanInsertDtmf();
1396}
1397
1398bool WebRtcSession::InsertDtmf(const std::string& track_id,
1399 int code, int duration) {
1400 ASSERT(signaling_thread()->IsCurrent());
1401 if (!voice_channel_) {
1402 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1403 return false;
1404 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001405 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001406 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1407 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1409 return false;
1410 }
1411 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1412 cricket::DF_SEND)) {
1413 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1414 return false;
1415 }
1416 return true;
1417}
1418
1419sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1420 return &SignalVoiceChannelDestroyed;
1421}
1422
wu@webrtc.org78187522013-10-07 23:32:02 +00001423bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001424 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001425 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001426 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001427 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1428 return false;
1429 }
1430 return data_channel_->SendData(params, payload, result);
1431}
1432
1433bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001434 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001435 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1436 return false;
1437 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001438 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1439 &DataChannel::OnChannelReady);
1440 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1441 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001442 data_channel_->SignalStreamClosedRemotely.connect(
1443 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001444 return true;
1445}
1446
1447void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001448 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001449 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1450 return;
1451 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001452 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1453 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001454 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001455}
1456
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001457void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001458 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001459 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1460 return;
1461 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001462 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1463 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001464}
1465
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001466void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001467 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001468 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1469 << "NULL.";
1470 return;
1471 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001472 data_channel_->RemoveRecvStream(sid);
1473 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001474}
1475
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001476bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001477 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001478}
1479
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480cricket::DataChannelType WebRtcSession::data_channel_type() const {
1481 return data_channel_type_;
1482}
1483
wu@webrtc.org91053e72013-08-10 07:18:04 +00001484bool WebRtcSession::IceRestartPending() const {
1485 return ice_restart_latch_->Get();
1486}
1487
1488void WebRtcSession::ResetIceRestartLatch() {
1489 ice_restart_latch_->Reset();
1490}
1491
Henrik Boströmd8281982015-08-27 10:12:24 +02001492void WebRtcSession::OnCertificateReady(
1493 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001494 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001495}
1496
Henrik Boströmd8281982015-08-27 10:12:24 +02001497bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001498 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001499}
1500
deadbeefcbecd352015-09-23 11:50:27 -07001501const rtc::scoped_refptr<rtc::RTCCertificate>&
1502WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001503 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001504}
1505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506void WebRtcSession::SetIceConnectionState(
1507 PeerConnectionInterface::IceConnectionState state) {
1508 if (ice_connection_state_ == state) {
1509 return;
1510 }
1511
1512 // ASSERT that the requested transition is allowed. Note that
1513 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1514 // within PeerConnection). This switch statement should compile away when
1515 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001516 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1517 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 switch (ice_connection_state_) {
1519 case PeerConnectionInterface::kIceConnectionNew:
1520 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1521 break;
1522 case PeerConnectionInterface::kIceConnectionChecking:
1523 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1524 state == PeerConnectionInterface::kIceConnectionConnected);
1525 break;
1526 case PeerConnectionInterface::kIceConnectionConnected:
1527 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1528 state == PeerConnectionInterface::kIceConnectionChecking ||
1529 state == PeerConnectionInterface::kIceConnectionCompleted);
1530 break;
1531 case PeerConnectionInterface::kIceConnectionCompleted:
1532 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1533 state == PeerConnectionInterface::kIceConnectionDisconnected);
1534 break;
1535 case PeerConnectionInterface::kIceConnectionFailed:
1536 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1537 break;
1538 case PeerConnectionInterface::kIceConnectionDisconnected:
1539 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1540 state == PeerConnectionInterface::kIceConnectionConnected ||
1541 state == PeerConnectionInterface::kIceConnectionCompleted ||
1542 state == PeerConnectionInterface::kIceConnectionFailed);
1543 break;
1544 case PeerConnectionInterface::kIceConnectionClosed:
1545 ASSERT(false);
1546 break;
1547 default:
1548 ASSERT(false);
1549 break;
1550 }
1551
1552 ice_connection_state_ = state;
1553 if (ice_observer_) {
1554 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1555 }
1556}
1557
deadbeefcbecd352015-09-23 11:50:27 -07001558void WebRtcSession::OnTransportControllerConnectionState(
1559 cricket::IceConnectionState state) {
1560 switch (state) {
1561 case cricket::kIceConnectionConnecting:
1562 // If the current state is Connected or Completed, then there were
1563 // writable channels but now there are not, so the next state must
1564 // be Disconnected.
1565 // kIceConnectionConnecting is currently used as the default,
1566 // un-connected state by the TransportController, so its only use is
1567 // detecting disconnections.
1568 if (ice_connection_state_ ==
1569 PeerConnectionInterface::kIceConnectionConnected ||
1570 ice_connection_state_ ==
1571 PeerConnectionInterface::kIceConnectionCompleted) {
1572 SetIceConnectionState(
1573 PeerConnectionInterface::kIceConnectionDisconnected);
1574 }
torbjornga81a42f2015-09-23 02:16:58 -07001575 break;
deadbeefcbecd352015-09-23 11:50:27 -07001576 case cricket::kIceConnectionFailed:
1577 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1578 break;
1579 case cricket::kIceConnectionConnected:
1580 LOG(LS_INFO) << "Changing to ICE connected state because "
1581 << "all transports are writable.";
1582 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1583 break;
1584 case cricket::kIceConnectionCompleted:
1585 LOG(LS_INFO) << "Changing to ICE completed state because "
1586 << "all transports are complete.";
1587 if (ice_connection_state_ !=
1588 PeerConnectionInterface::kIceConnectionConnected) {
1589 // If jumping directly from "checking" to "connected",
1590 // signal "connected" first.
1591 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1592 }
1593 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1594 if (metrics_observer_) {
1595 ReportTransportStats();
1596 }
1597 break;
1598 default:
1599 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001600 }
deadbeefcbecd352015-09-23 11:50:27 -07001601}
1602
1603void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001604 SetIceConnectionReceiving(receiving);
1605}
1606
1607void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1608 if (ice_connection_receiving_ == receiving) {
1609 return;
1610 }
1611 ice_connection_receiving_ = receiving;
1612 if (ice_observer_) {
1613 ice_observer_->OnIceConnectionReceivingChange(receiving);
1614 }
1615}
1616
deadbeefcbecd352015-09-23 11:50:27 -07001617void WebRtcSession::OnTransportControllerCandidatesGathered(
1618 const std::string& transport_name,
1619 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001621 int sdp_mline_index;
1622 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1623 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1624 << transport_name << " not found";
1625 return;
1626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627
deadbeefcbecd352015-09-23 11:50:27 -07001628 for (cricket::Candidates::const_iterator citer = candidates.begin();
1629 citer != candidates.end(); ++citer) {
1630 // Use transport_name as the candidate media id.
1631 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1632 if (ice_observer_) {
1633 ice_observer_->OnIceCandidate(&candidate);
1634 }
1635 if (local_desc_) {
1636 local_desc_->AddCandidate(&candidate);
1637 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 }
1639}
1640
1641// Enabling voice and video channel.
1642void WebRtcSession::EnableChannels() {
1643 if (voice_channel_ && !voice_channel_->enabled())
1644 voice_channel_->Enable(true);
1645
1646 if (video_channel_ && !video_channel_->enabled())
1647 video_channel_->Enable(true);
1648
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001649 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 data_channel_->Enable(true);
1651}
1652
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653// Returns the media index for a local ice candidate given the content name.
1654bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1655 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001656 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001658 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659
1660 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001661 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 for (size_t index = 0; index < contents.size(); ++index) {
1663 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001664 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 content_found = true;
1666 break;
1667 }
1668 }
1669 return content_found;
1670}
1671
1672bool WebRtcSession::UseCandidatesInSessionDescription(
1673 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001674 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001676 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001678
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1680 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001681 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001682 const IceCandidateInterface* candidate = candidates->at(n);
1683 bool valid = false;
1684 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1685 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001686 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1687 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001688 }
1689 continue;
1690 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001691 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001692 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 break;
deadbeefd59daf82015-10-14 15:02:44 -07001694 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 }
1696 }
1697 return ret;
1698}
1699
1700bool WebRtcSession::UseCandidate(
1701 const IceCandidateInterface* candidate) {
1702
1703 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001704 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 if (mediacontent_index >= remote_content_size) {
1706 LOG(LS_ERROR)
1707 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1708 return false;
1709 }
1710
1711 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001712 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 std::vector<cricket::Candidate> candidates;
1714 candidates.push_back(candidate->candidate());
1715 // Invoking BaseSession method to handle remote candidates.
1716 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001717 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1718 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 // Candidates successfully submitted for checking.
1720 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1721 ice_connection_state_ ==
1722 PeerConnectionInterface::kIceConnectionDisconnected) {
1723 // If state is New, then the session has just gotten its first remote ICE
1724 // candidates, so go to Checking.
1725 // If state is Disconnected, the session is re-using old candidates or
1726 // receiving additional ones, so go to Checking.
1727 // If state is Connected, stay Connected.
1728 // TODO(bemasc): If state is Connected, and the new candidates are for a
1729 // newly added transport, then the state actually _should_ move to
1730 // checking. Add a way to distinguish that case.
1731 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1732 }
1733 // TODO(bemasc): If state is Completed, go back to Connected.
1734 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001735 if (!error.empty()) {
1736 LOG(LS_WARNING) << error;
1737 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 }
1739 return true;
1740}
1741
deadbeefcbecd352015-09-23 11:50:27 -07001742void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001743 // Destroy video_channel_ first since it may have a pointer to the
1744 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 const cricket::ContentInfo* video_info =
1746 cricket::GetFirstVideoContent(desc);
1747 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 SignalVideoChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001749 const std::string content_name = video_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 }
1752
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001753 const cricket::ContentInfo* voice_info =
1754 cricket::GetFirstAudioContent(desc);
1755 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001756 SignalVoiceChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001757 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001758 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001759 }
1760
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 const cricket::ContentInfo* data_info =
1762 cricket::GetFirstDataContent(desc);
1763 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 SignalDataChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001765 const std::string content_name = data_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 }
1768}
1769
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001770// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001771// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 // Creating the media channels and transport proxies.
1774 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1775 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001776 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 LOG(LS_ERROR) << "Failed to create voice channel.";
1778 return false;
1779 }
1780 }
1781
1782 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1783 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001784 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_ERROR) << "Failed to create video channel.";
1786 return false;
1787 }
1788 }
1789
1790 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1791 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001792 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_ERROR) << "Failed to create data channel.";
1795 return false;
1796 }
1797 }
1798
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001799 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1800 if (voice_channel()) {
1801 voice_channel()->ActivateRtcpMux();
1802 }
1803 if (video_channel()) {
1804 video_channel()->ActivateRtcpMux();
1805 }
1806 if (data_channel()) {
1807 data_channel()->ActivateRtcpMux();
1808 }
1809 }
1810
deadbeefcbecd352015-09-23 11:50:27 -07001811 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001812 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001813 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1814 cricket::GROUP_TYPE_BUNDLE);
1815 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001816 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1817 return false;
1818 }
deadbeefcbecd352015-09-23 11:50:27 -07001819 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001820 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1821 return false;
1822 }
1823 }
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 return true;
1826}
1827
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001828bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001830 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001831 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001832 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001833 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001834 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001835
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001836 voice_channel_->SignalDtlsSetupFailure.connect(
1837 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001838
1839 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001840 voice_channel_->transport_channel()->SignalSentPacket.connect(
1841 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001842 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843}
1844
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001845bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001847 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001848 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001849 if (!video_channel_) {
1850 return false;
1851 }
1852
1853 video_channel_->SignalDtlsSetupFailure.connect(
1854 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001855
1856 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001857 video_channel_->transport_channel()->SignalSentPacket.connect(
1858 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001859 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860}
1861
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001862bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001863 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001865 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001866 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001867 return false;
1868 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001869
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001870 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001871 data_channel_->SignalDataReceived.connect(
1872 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001873 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001874
1875 data_channel_->SignalDtlsSetupFailure.connect(
1876 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001877
1878 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001879 data_channel_->transport_channel()->SignalSentPacket.connect(
1880 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001881 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001884void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001885 SetError(ERROR_TRANSPORT,
1886 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887}
1888
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001889void WebRtcSession::OnDataChannelMessageReceived(
1890 cricket::DataChannel* channel,
1891 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001892 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001893 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1894 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1895 // Received OPEN message; parse and signal that a new data channel should
1896 // be created.
1897 std::string label;
1898 InternalDataChannelInit config;
1899 config.id = params.ssrc;
1900 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1901 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1902 << params.ssrc;
1903 return;
1904 }
1905 config.open_handshake_role = InternalDataChannelInit::kAcker;
1906 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 }
deadbeefab9b2d12015-10-14 11:33:11 -07001908 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909}
1910
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001911// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001912bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001913 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1914 if (!bundle_enabled)
1915 return true;
1916
1917 const cricket::ContentGroup* bundle_group =
1918 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1919 ASSERT(bundle_group != NULL);
1920
1921 const cricket::ContentInfos& contents = desc->contents();
1922 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1923 citer != contents.end(); ++citer) {
1924 const cricket::ContentInfo* content = (&*citer);
1925 ASSERT(content != NULL);
1926 if (bundle_group->HasContentName(content->name) &&
1927 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1928 if (!HasRtcpMuxEnabled(content))
1929 return false;
1930 }
1931 }
1932 // RTCP-MUX is enabled in all the contents.
1933 return true;
1934}
1935
1936bool WebRtcSession::HasRtcpMuxEnabled(
1937 const cricket::ContentInfo* content) {
1938 const cricket::MediaContentDescription* description =
1939 static_cast<cricket::MediaContentDescription*>(content->description);
1940 return description->rtcp_mux();
1941}
1942
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001943bool WebRtcSession::ValidateSessionDescription(
1944 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001945 cricket::ContentSource source, std::string* err_desc) {
1946 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001947 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001948 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001949 }
1950
1951 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001952 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001953 }
1954
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001955 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001956 Action action = GetAction(sdesc->type());
1957 if (source == cricket::CS_LOCAL) {
1958 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001959 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001960 } else {
1961 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001962 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001963 }
1964
1965 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001966 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001967 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1968 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001969 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001970 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001971 }
1972
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001973 // Verify ice-ufrag and ice-pwd.
1974 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001975 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001976 }
1977
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001978 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001979 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 }
1981
1982 // Verify m-lines in Answer when compared against Offer.
1983 if (action == kAnswer) {
1984 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001985 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1986 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001987 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001988 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001989 }
1990 }
1991
1992 return true;
1993}
1994
1995bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1996 return ((action == kOffer && state() == STATE_INIT) ||
1997 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001998 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001999 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000 (action == kOffer && state() == STATE_INPROGRESS) ||
2001 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002002 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2003 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2004 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2005 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002006}
2007
2008bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2009 return ((action == kOffer && state() == STATE_INIT) ||
2010 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002011 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002012 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002013 (action == kOffer && state() == STATE_INPROGRESS) ||
2014 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002015 (action == kAnswer && state() == STATE_SENTOFFER) ||
2016 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2017 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2018 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002019}
2020
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002021std::string WebRtcSession::GetSessionErrorMsg() {
2022 std::ostringstream desc;
2023 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2024 desc << kSessionErrorDesc << error_desc() << ".";
2025 return desc.str();
2026}
2027
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002028// We need to check the local/remote description for the Transport instead of
2029// the session, because a new Transport added during renegotiation may have
2030// them unset while the session has them set from the previous negotiation.
2031// Not doing so may trigger the auto generation of transport description and
2032// mess up DTLS identity information, ICE credential, etc.
2033bool WebRtcSession::ReadyToUseRemoteCandidate(
2034 const IceCandidateInterface* candidate,
2035 const SessionDescriptionInterface* remote_desc,
2036 bool* valid) {
2037 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002038
2039 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002040 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002041
deadbeefd59daf82015-10-14 15:02:44 -07002042 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002043 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002044 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002045
2046 size_t mediacontent_index =
2047 static_cast<size_t>(candidate->sdp_mline_index());
2048 size_t remote_content_size =
2049 current_remote_desc->description()->contents().size();
2050 if (mediacontent_index >= remote_content_size) {
2051 LOG(LS_ERROR)
2052 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2053
2054 *valid = false;
2055 return false;
2056 }
2057
2058 cricket::ContentInfo content =
2059 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002060 cricket::BaseChannel* channel = GetChannel(content.name);
2061 if (!channel) {
2062 return false;
2063 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002064
deadbeefd59daf82015-10-14 15:02:44 -07002065 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002066 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002067}
2068
deadbeefcbecd352015-09-23 11:50:27 -07002069void WebRtcSession::OnTransportControllerGatheringState(
2070 cricket::IceGatheringState state) {
2071 ASSERT(signaling_thread()->IsCurrent());
2072 if (state == cricket::kIceGatheringGathering) {
2073 if (ice_observer_) {
2074 ice_observer_->OnIceGatheringChange(
2075 PeerConnectionInterface::kIceGatheringGathering);
2076 }
2077 } else if (state == cricket::kIceGatheringComplete) {
2078 if (ice_observer_) {
2079 ice_observer_->OnIceGatheringChange(
2080 PeerConnectionInterface::kIceGatheringComplete);
2081 ice_observer_->OnIceComplete();
2082 }
2083 }
2084}
2085
2086void WebRtcSession::ReportTransportStats() {
2087 // Use a set so we don't report the same stats twice if two channels share
2088 // a transport.
2089 std::set<std::string> transport_names;
2090 if (voice_channel()) {
2091 transport_names.insert(voice_channel()->transport_name());
2092 }
2093 if (video_channel()) {
2094 transport_names.insert(video_channel()->transport_name());
2095 }
2096 if (data_channel()) {
2097 transport_names.insert(data_channel()->transport_name());
2098 }
2099 for (const auto& name : transport_names) {
2100 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002101 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002102 ReportBestConnectionState(stats);
2103 ReportNegotiatedCiphers(stats);
2104 }
2105 }
2106}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002107// Walk through the ConnectionInfos to gather best connection usage
2108// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002109void WebRtcSession::ReportBestConnectionState(
2110 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002111 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002112 for (cricket::TransportChannelStatsList::const_iterator it =
2113 stats.channel_stats.begin();
2114 it != stats.channel_stats.end(); ++it) {
2115 for (cricket::ConnectionInfos::const_iterator it_info =
2116 it->connection_infos.begin();
2117 it_info != it->connection_infos.end(); ++it_info) {
2118 if (!it_info->best_connection) {
2119 continue;
2120 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002121
2122 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2123 const cricket::Candidate& local = it_info->local_candidate;
2124 const cricket::Candidate& remote = it_info->remote_candidate;
2125
2126 // Increment the counter for IceCandidatePairType.
2127 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2128 (local.type() == RELAY_PORT_TYPE &&
2129 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2130 type = kEnumCounterIceCandidatePairTypeTcp;
2131 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2132 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002133 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002134 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002135 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002136 metrics_observer_->IncrementEnumCounter(
2137 type, GetIceCandidatePairCounter(local, remote),
2138 kIceCandidatePairMax);
2139
2140 // Increment the counter for IP type.
2141 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002142 metrics_observer_->IncrementEnumCounter(
2143 kEnumCounterAddressFamily, kBestConnections_IPv4,
2144 kPeerConnectionAddressFamilyCounter_Max);
2145
2146 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002147 metrics_observer_->IncrementEnumCounter(
2148 kEnumCounterAddressFamily, kBestConnections_IPv6,
2149 kPeerConnectionAddressFamilyCounter_Max);
2150 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002151 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002152 }
2153
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002154 return;
2155 }
2156 }
2157}
2158
jbauchac8869e2015-07-03 01:36:14 -07002159void WebRtcSession::ReportNegotiatedCiphers(
2160 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002161 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002162 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2163 return;
2164 }
2165
2166 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07002167 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002168 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002169 return;
2170 }
2171
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002172 PeerConnectionEnumCounterType srtp_counter_type;
2173 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002174 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002175 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2176 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002177 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002178 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2179 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002180 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002181 srtp_counter_type = kEnumCounterDataSrtpCipher;
2182 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002183 } else {
2184 RTC_NOTREACHED();
2185 return;
2186 }
2187
2188 if (!srtp_cipher.empty()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002189 metrics_observer_->IncrementSparseEnumCounter(
2190 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002191 }
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002192 if (ssl_cipher) {
2193 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002194 }
2195}
2196
stefanc1aeaf02015-10-15 07:26:07 -07002197void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2198 const rtc::SentPacket& sent_packet) {
2199 RTC_DCHECK(worker_thread()->IsCurrent());
2200 media_controller_->call_w()->OnSentPacket(sent_packet);
2201}
2202
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203} // namespace webrtc