blob: af1dc61a93b184f8d9f26a8615aafb6fc2e077a3 [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.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000444template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000445static void SetOptionFromOptionalConstraint(
446 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000447 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000448 if (!constraints) {
449 return;
450 }
451 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000452 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000453 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000455 option->Set(value);
456 }
457 }
458}
459
Peter Boström0c4e06b2015-10-07 12:23:21 +0200460uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000461 PeerConnectionInterface::IceTransportsType type) {
462 switch (type) {
463 case PeerConnectionInterface::kNone:
464 return cricket::CF_NONE;
465 case PeerConnectionInterface::kRelay:
466 return cricket::CF_RELAY;
467 case PeerConnectionInterface::kNoHost:
468 return (cricket::CF_ALL & ~cricket::CF_HOST);
469 case PeerConnectionInterface::kAll:
470 return cricket::CF_ALL;
471 default: ASSERT(false);
472 }
473 return cricket::CF_NONE;
474}
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476// Help class used to remember if a a remote peer has requested ice restart by
477// by sending a description with new ice ufrag and password.
478class IceRestartAnswerLatch {
479 public:
480 IceRestartAnswerLatch() : ice_restart_(false) { }
481
wu@webrtc.org91053e72013-08-10 07:18:04 +0000482 // Returns true if CheckForRemoteIceRestart has been called with a new session
483 // description where ice password and ufrag has changed since last time
484 // Reset() was called.
485 bool Get() const {
486 return ice_restart_;
487 }
488
489 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 if (ice_restart_) {
491 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
494
honghaiz503726c2015-07-31 12:37:38 -0700495 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
496 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700498 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500 const SessionDescription* new_sd = new_desc->description();
501 const SessionDescription* old_sd = old_desc->description();
502 const ContentInfos& contents = new_sd->contents();
503 for (size_t index = 0; index < contents.size(); ++index) {
504 const ContentInfo* cinfo = &contents[index];
505 if (cinfo->rejected) {
506 continue;
507 }
508 // If the content isn't rejected, check if ufrag and password has
509 // changed.
510 const cricket::TransportDescription* new_transport_desc =
511 new_sd->GetTransportDescriptionByName(cinfo->name);
512 const cricket::TransportDescription* old_transport_desc =
513 old_sd->GetTransportDescriptionByName(cinfo->name);
514 if (!new_transport_desc || !old_transport_desc) {
515 // No transport description exist. This is not an ice restart.
516 continue;
517 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000518 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
519 old_transport_desc->ice_pwd,
520 new_transport_desc->ice_ufrag,
521 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 LOG(LS_INFO) << "Remote peer request ice restart.";
523 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700524 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
526 }
honghaiz503726c2015-07-31 12:37:38 -0700527 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 }
529
530 private:
531 bool ice_restart_;
532};
533
stefanc1aeaf02015-10-15 07:26:07 -0700534WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700535 rtc::Thread* signaling_thread,
536 rtc::Thread* worker_thread,
537 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700538 : signaling_thread_(signaling_thread),
539 worker_thread_(worker_thread),
540 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // RFC 3264: The numeric value of the session id and version in the
542 // o line MUST be representable with a "64 bit signed integer".
543 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700544 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
545 transport_controller_(new cricket::TransportController(signaling_thread,
546 worker_thread,
547 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700548 media_controller_(media_controller),
549 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 ice_observer_(NULL),
551 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700552 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000554 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000556 ice_restart_latch_(new IceRestartAnswerLatch),
557 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700558 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
559 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700560 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700561 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700562 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700563 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700564 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700565 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700566 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567}
568
569WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700570 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000571 // Destroy video_channel_ first since it may have a pointer to the
572 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000573 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SignalVideoChannelDestroyed();
575 channel_manager_->DestroyVideoChannel(video_channel_.release());
576 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000577 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000578 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200579 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000580 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000581 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 SignalDataChannelDestroyed();
583 channel_manager_->DestroyDataChannel(data_channel_.release());
584 }
deadbeefd59daf82015-10-14 15:02:44 -0700585
586 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587}
588
wu@webrtc.org91053e72013-08-10 07:18:04 +0000589bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000590 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700591 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200592 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200593 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
594 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700595 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700596 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000597
Henrik Boström87713d02015-08-25 09:53:21 +0200598 // Obtain a certificate from RTCConfiguration if any were provided (optional).
599 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
600 if (!rtc_configuration.certificates.empty()) {
601 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
602 // just picking the first one. The decision should be made based on the DTLS
603 // handshake. The DTLS negotiations need to know about all certificates.
604 certificate = rtc_configuration.certificates[0];
605 }
606
honghaiz1f429e32015-09-28 07:57:34 -0700607 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // TODO(perkj): Take |constraints| into consideration. Return false if not all
610 // mandatory constraints can be fulfilled. Note that |constraints|
611 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000613
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000614 if (options.disable_encryption) {
615 dtls_enabled_ = false;
616 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200617 // Enable DTLS by default if we have an identity store or a certificate.
618 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000619 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700620 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
621 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000622 dtls_enabled_ = value;
623 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000627 // It takes precendence over the disable_sctp_data_channels
628 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 if (FindConstraint(
630 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
631 &value, NULL) && value) {
632 LOG(LS_INFO) << "Allowing RTP data engine.";
633 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000634 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000637 LOG(LS_INFO) << "Allowing SCTP data engine.";
638 data_channel_type_ = cricket::DCT_SCTP;
639 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641
wu@webrtc.orgde305012013-10-31 15:40:38 +0000642 // Find DSCP constraint.
643 if (FindConstraint(
644 constraints,
645 MediaConstraintsInterface::kEnableDscp,
646 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000647 audio_options_.dscp.Set(value);
648 video_options_.dscp.Set(value);
649 }
650
651 // Find Suspend Below Min Bitrate constraint.
652 if (FindConstraint(
653 constraints,
654 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
655 &value,
656 NULL)) {
657 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000658 }
659
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000660 SetOptionFromOptionalConstraint(constraints,
661 MediaConstraintsInterface::kScreencastMinBitrate,
662 &video_options_.screencast_min_bitrate);
663
664 // Find constraints for cpu overuse detection.
665 SetOptionFromOptionalConstraint(constraints,
666 MediaConstraintsInterface::kCpuUnderuseThreshold,
667 &video_options_.cpu_underuse_threshold);
668 SetOptionFromOptionalConstraint(constraints,
669 MediaConstraintsInterface::kCpuOveruseThreshold,
670 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000671 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000672 MediaConstraintsInterface::kCpuOveruseDetection,
673 &video_options_.cpu_overuse_detection);
674 SetOptionFromOptionalConstraint(constraints,
675 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
676 &video_options_.cpu_overuse_encode_usage);
677 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000678 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
679 &video_options_.cpu_underuse_encode_rsd_threshold);
680 SetOptionFromOptionalConstraint(constraints,
681 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
682 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000683
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000684 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000685 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
686 &video_options_.unsignalled_recv_stream_limit);
687 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
688 int stream_limit;
689 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000690 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
691 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000692 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
693 }
694
695 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000696 MediaConstraintsInterface::kHighStartBitrate,
697 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000698
699 if (FindConstraint(
700 constraints,
701 MediaConstraintsInterface::kVeryHighBitrate,
702 &value,
703 NULL)) {
704 video_options_.video_highest_bitrate.Set(
705 cricket::VideoOptions::VERY_HIGH);
706 } else if (FindConstraint(
707 constraints,
708 MediaConstraintsInterface::kHighBitrate,
709 &value,
710 NULL)) {
711 video_options_.video_highest_bitrate.Set(
712 cricket::VideoOptions::HIGH);
713 }
714
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000715 SetOptionFromOptionalConstraint(constraints,
716 MediaConstraintsInterface::kCombinedAudioVideoBwe,
717 &audio_options_.combined_audio_video_bwe);
718
Henrik Lundin64dad832015-05-11 12:44:23 +0200719 audio_options_.audio_jitter_buffer_max_packets.Set(
720 rtc_configuration.audio_jitter_buffer_max_packets);
721
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200722 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
723 rtc_configuration.audio_jitter_buffer_fast_accelerate);
724
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 const cricket::VideoCodec default_codec(
726 JsepSessionDescription::kDefaultVideoCodecId,
727 JsepSessionDescription::kDefaultVideoCodecName,
728 JsepSessionDescription::kMaxVideoCodecWidth,
729 JsepSessionDescription::kMaxVideoCodecHeight,
730 JsepSessionDescription::kDefaultVideoCodecFramerate,
731 JsepSessionDescription::kDefaultVideoCodecPreference);
732 channel_manager_->SetDefaultVideoEncoderConfig(
733 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000734
Henrik Boström87713d02015-08-25 09:53:21 +0200735 if (!dtls_enabled_) {
736 // Construct with DTLS disabled.
737 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700738 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200739 } else {
740 // Construct with DTLS enabled.
741 if (!certificate) {
742 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700743 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200744 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700745 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
746 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200747 } else {
748 // Use the already generated certificate.
749 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700750 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200751 }
752 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000753
Henrik Boströmd8281982015-08-27 10:12:24 +0200754 webrtc_session_desc_factory_->SignalCertificateReady.connect(
755 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000756
wu@webrtc.org97077a32013-10-25 21:18:33 +0000757 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000758 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000759 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000760 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200761 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700762
763 if (rtc_configuration.enable_localhost_ice_candidate) {
764 port_allocator()->set_flags(
765 port_allocator()->flags() |
766 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
767 }
768
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 return true;
770}
771
deadbeefd59daf82015-10-14 15:02:44 -0700772void WebRtcSession::Close() {
773 SetState(STATE_CLOSED);
774 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000775 ASSERT(!voice_channel_);
776 ASSERT(!video_channel_);
777 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778}
779
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000780void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
781 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782}
783
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000784cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
785 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786}
787
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700789 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000790 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
791 << "SSL Role of the session.";
792 return false;
793 }
794
deadbeefd59daf82015-10-14 15:02:44 -0700795 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000796}
797
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000798void WebRtcSession::CreateOffer(
799 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700800 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
801 const cricket::MediaSessionOptions& session_options) {
802 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000803}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
deadbeefab9b2d12015-10-14 11:33:11 -0700805void WebRtcSession::CreateAnswer(
806 CreateSessionDescriptionObserver* observer,
807 const MediaConstraintsInterface* constraints,
808 const cricket::MediaSessionOptions& session_options) {
809 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
810 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811}
812
813bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
814 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700815 ASSERT(signaling_thread()->IsCurrent());
816
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000817 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000818 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000819
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000820 // Validate SDP.
821 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
822 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
deadbeefd59daf82015-10-14 15:02:44 -0700825 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000826 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700828 initial_offerer_ = true;
829 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000832 cricket::SecurePolicy sdes_policy =
833 webrtc_session_desc_factory_->SdesPolicy();
834 cricket::CryptoType crypto_required = dtls_enabled_ ?
835 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
836 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000838 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000840 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841
842 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000843 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // TODO(mallinath) - Handle CreateChannel failure, as new local description
845 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000846 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
848
deadbeefcbecd352015-09-23 11:50:27 -0700849 // Remove unused channels if MediaContentDescription is rejected.
850 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000852 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 return false;
854 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000855
deadbeefd59daf82015-10-14 15:02:44 -0700856 if (remote_desc_) {
857 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700858 UseCandidatesInSessionDescription(remote_desc_.get());
859 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860
deadbeefd59daf82015-10-14 15:02:44 -0700861 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000862 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
864 return true;
865}
866
867bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
868 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700869 ASSERT(signaling_thread()->IsCurrent());
870
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000871 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000872 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000873
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000874 // Validate SDP.
875 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
876 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 }
878
deadbeefd59daf82015-10-14 15:02:44 -0700879 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
880 remote_desc_.release());
881 remote_desc_.reset(desc_temp.release());
882
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000884 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (action == kOffer && !CreateChannels(desc->description())) {
886 // TODO(mallinath) - Handle CreateChannel failure, as new local description
887 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 }
890
deadbeefcbecd352015-09-23 11:50:27 -0700891 // Remove unused channels if MediaContentDescription is rejected.
892 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893
894 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
895 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 return false;
898 }
899
deadbeefd59daf82015-10-14 15:02:44 -0700900 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000901 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 // Check if this new SessionDescription contains new ice ufrag and password
905 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700906 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700907 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700908 // We retain all received candidates only if ICE is not restarted.
909 // When ICE is restarted, all previous candidates belong to an old generation
910 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700911 // TODO(deadbeef): This goes against the W3C spec which says the remote
912 // description should only contain candidates from the last set remote
913 // description plus any candidates added since then. We should remove this
914 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700915 if (!ice_restart) {
916 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700917 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700918 }
919
deadbeefd59daf82015-10-14 15:02:44 -0700920 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000921 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000923
924 // Set the the ICE connection state to connecting since the connection may
925 // become writable with peer reflexive candidates before any remote candidate
926 // is signaled.
927 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
928 // is to have a new signal the indicates a change in checking state from the
929 // transport and expose a new checking() member from transport that can be
930 // read to determine the current checking state. The existing SignalConnecting
931 // actually means "gathering candidates", so cannot be be used here.
932 if (desc->type() != SessionDescriptionInterface::kOffer &&
933 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
934 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 return true;
937}
938
deadbeefd59daf82015-10-14 15:02:44 -0700939void WebRtcSession::LogState(State old_state, State new_state) {
940 LOG(LS_INFO) << "Session:" << id()
941 << " Old state:" << GetStateString(old_state)
942 << " New state:" << GetStateString(new_state);
943}
944
945void WebRtcSession::SetState(State state) {
946 ASSERT(signaling_thread_->IsCurrent());
947 if (state != state_) {
948 LogState(state_, state);
949 state_ = state;
950 SignalState(this, state_);
951 }
952}
953
954void WebRtcSession::SetError(Error error, const std::string& error_desc) {
955 ASSERT(signaling_thread_->IsCurrent());
956 if (error != error_) {
957 error_ = error;
958 error_desc_ = error_desc;
959 }
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962bool WebRtcSession::UpdateSessionState(
963 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700965 ASSERT(signaling_thread()->IsCurrent());
966
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 // If there's already a pending error then no state transition should happen.
968 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700969 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000970 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000972 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
973 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
deadbeefd59daf82015-10-14 15:02:44 -0700975 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
976 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000977 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700978 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000979 }
deadbeefd59daf82015-10-14 15:02:44 -0700980 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 }
983 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000984 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
985 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 }
987 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700988 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
989 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000990 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700991 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000992 }
deadbeefd59daf82015-10-14 15:02:44 -0700993 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000994 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
996 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000997 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
998 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 }
deadbeefcbecd352015-09-23 11:50:27 -07001000 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -07001001 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -07001002 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -07001003 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -07001004 if (local_bundle && remote_bundle) {
1005 // The answerer decides the transport to bundle on
1006 const cricket::ContentGroup* answer_bundle =
1007 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
1008 if (!EnableBundle(*answer_bundle)) {
1009 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
1010 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
1011 }
1012 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -07001014 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001015 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -07001016 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001017 }
deadbeefd59daf82015-10-14 15:02:44 -07001018 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001019 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 }
1021 }
1022 return true;
1023}
1024
1025WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1026 if (type == SessionDescriptionInterface::kOffer) {
1027 return WebRtcSession::kOffer;
1028 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1029 return WebRtcSession::kPrAnswer;
1030 } else if (type == SessionDescriptionInterface::kAnswer) {
1031 return WebRtcSession::kAnswer;
1032 }
1033 ASSERT(false && "unknown action type");
1034 return WebRtcSession::kOffer;
1035}
1036
deadbeefd59daf82015-10-14 15:02:44 -07001037bool WebRtcSession::PushdownMediaDescription(
1038 cricket::ContentAction action,
1039 cricket::ContentSource source,
1040 std::string* err) {
1041 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1042 if (!ch) {
1043 return true;
1044 } else if (source == cricket::CS_LOCAL) {
1045 return ch->PushdownLocalDescription(local_desc_->description(), action,
1046 err);
1047 } else {
1048 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1049 err);
1050 }
1051 };
1052
1053 return (set_content(voice_channel()) &&
1054 set_content(video_channel()) &&
1055 set_content(data_channel()));
1056}
1057
1058bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1059 cricket::ContentAction action,
1060 std::string* error_desc) {
1061 RTC_DCHECK(signaling_thread()->IsCurrent());
1062
1063 if (source == cricket::CS_LOCAL) {
1064 return PushdownLocalTransportDescription(local_desc_->description(), action,
1065 error_desc);
1066 }
1067 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1068 error_desc);
1069}
1070
1071bool WebRtcSession::PushdownLocalTransportDescription(
1072 const SessionDescription* sdesc,
1073 cricket::ContentAction action,
1074 std::string* err) {
1075 RTC_DCHECK(signaling_thread()->IsCurrent());
1076
1077 if (!sdesc) {
1078 return false;
1079 }
1080
1081 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1082 if (!transport_controller_->SetLocalTransportDescription(
1083 tinfo.content_name, tinfo.description, action, err)) {
1084 return false;
1085 }
1086 }
1087
1088 return true;
1089}
1090
1091bool WebRtcSession::PushdownRemoteTransportDescription(
1092 const SessionDescription* sdesc,
1093 cricket::ContentAction action,
1094 std::string* err) {
1095 RTC_DCHECK(signaling_thread()->IsCurrent());
1096
1097 if (!sdesc) {
1098 return false;
1099 }
1100
1101 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1102 if (!transport_controller_->SetRemoteTransportDescription(
1103 tinfo.content_name, tinfo.description, action, err)) {
1104 return false;
1105 }
1106 }
1107
1108 return true;
1109}
1110
1111bool WebRtcSession::GetTransportDescription(
1112 const SessionDescription* description,
1113 const std::string& content_name,
1114 cricket::TransportDescription* tdesc) {
1115 if (!description || !tdesc) {
1116 return false;
1117 }
1118 const TransportInfo* transport_info =
1119 description->GetTransportInfoByName(content_name);
1120 if (!transport_info) {
1121 return false;
1122 }
1123 *tdesc = transport_info->description;
1124 return true;
1125}
1126
1127bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001128 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001129 return (GetChannelTransportStats(voice_channel(), stats) &&
1130 GetChannelTransportStats(video_channel(), stats) &&
1131 GetChannelTransportStats(data_channel(), stats));
1132}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001133
deadbeefcbecd352015-09-23 11:50:27 -07001134bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001135 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001136 ASSERT(signaling_thread()->IsCurrent());
1137 if (!ch) {
1138 // Not using this channel.
1139 return true;
1140 }
1141
1142 const std::string& content_name = ch->content_name();
1143 const std::string& transport_name = ch->transport_name();
1144 stats->proxy_to_transport[content_name] = transport_name;
1145 if (stats->transport_stats.find(transport_name) !=
1146 stats->transport_stats.end()) {
1147 // Transport stats already done for this transport.
1148 return true;
1149 }
1150
1151 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001152 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001153 return false;
1154 }
1155
1156 stats->transport_stats[transport_name] = tstats;
1157 return true;
1158}
1159
1160bool WebRtcSession::GetLocalCertificate(
1161 const std::string& transport_name,
1162 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1163 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001164 return transport_controller_->GetLocalCertificate(transport_name,
1165 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001166}
1167
1168bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1169 rtc::SSLCertificate** cert) {
1170 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001171 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001172}
1173
1174cricket::BaseChannel* WebRtcSession::GetChannel(
1175 const std::string& content_name) {
1176 if (voice_channel() && voice_channel()->content_name() == content_name) {
1177 return voice_channel();
1178 }
1179 if (video_channel() && video_channel()->content_name() == content_name) {
1180 return video_channel();
1181 }
1182 if (data_channel() && data_channel()->content_name() == content_name) {
1183 return data_channel();
1184 }
1185 return nullptr;
1186}
1187
1188bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1189 const std::string* first_content_name = bundle.FirstContentName();
1190 if (!first_content_name) {
1191 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1192 return false;
1193 }
1194 const std::string& transport_name = *first_content_name;
1195 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1196
1197 auto maybe_set_transport = [this, bundle, transport_name,
1198 first_channel](cricket::BaseChannel* ch) {
1199 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001200 return true;
1201 }
1202
deadbeefcbecd352015-09-23 11:50:27 -07001203 if (ch->transport_name() == transport_name) {
1204 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1205 << " on " << transport_name << ".";
1206 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001207 }
torbjornga81a42f2015-09-23 02:16:58 -07001208
deadbeefcbecd352015-09-23 11:50:27 -07001209 if (!ch->SetTransport(transport_name)) {
1210 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1211 return false;
1212 }
1213 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1214 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001215 return true;
1216 };
1217
deadbeefcbecd352015-09-23 11:50:27 -07001218 if (!maybe_set_transport(voice_channel()) ||
1219 !maybe_set_transport(video_channel()) ||
1220 !maybe_set_transport(data_channel())) {
1221 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001222 }
deadbeefcbecd352015-09-23 11:50:27 -07001223
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001224 return true;
1225}
1226
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001228 if (!remote_desc_) {
1229 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1230 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 return false;
1232 }
1233
1234 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001235 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 return false;
1237 }
1238
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001239 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001240 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1241 if (!valid) {
1242 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 }
1244
1245 // Add this candidate to the remote session description.
1246 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001247 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 return false;
1249 }
1250
deadbeefd59daf82015-10-14 15:02:44 -07001251 if (ready) {
1252 return UseCandidate(candidate);
1253 } else {
1254 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1255 return true;
1256 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257}
1258
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001259bool WebRtcSession::SetIceTransports(
1260 PeerConnectionInterface::IceTransportsType type) {
1261 return port_allocator()->set_candidate_filter(
1262 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001263}
1264
deadbeefd59daf82015-10-14 15:02:44 -07001265cricket::IceConfig WebRtcSession::ParseIceConfig(
1266 const PeerConnectionInterface::RTCConfiguration& config) const {
1267 cricket::IceConfig ice_config;
1268 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1269 ice_config.gather_continually = (config.continual_gathering_policy ==
1270 PeerConnectionInterface::GATHER_CONTINUALLY);
1271 return ice_config;
1272}
1273
1274void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1275 transport_controller_->SetIceConfig(config);
1276}
1277
1278void WebRtcSession::MaybeStartGathering() {
1279 transport_controller_->MaybeStartGathering();
1280}
1281
Peter Boström0c4e06b2015-10-07 12:23:21 +02001282bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1283 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001284 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001286 }
1287 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288}
1289
Peter Boström0c4e06b2015-10-07 12:23:21 +02001290bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1291 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001292 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001293 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001294 }
1295 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296}
1297
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001298std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001300 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 return desc.str();
1302}
1303
solenbergd4cec0d2015-10-09 08:55:48 -07001304void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 ASSERT(signaling_thread()->IsCurrent());
1306 if (!voice_channel_) {
1307 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1308 return;
1309 }
solenberg4bac9c52015-10-09 02:32:53 -07001310 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1311 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 // otherwise. This in the normal case when the underlying media channel has
1313 // already been deleted.
1314 ASSERT(enable == false);
1315 }
1316}
1317
Peter Boström0c4e06b2015-10-07 12:23:21 +02001318void WebRtcSession::SetAudioSend(uint32_t ssrc,
1319 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001320 const cricket::AudioOptions& options,
1321 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ASSERT(signaling_thread()->IsCurrent());
1323 if (!voice_channel_) {
1324 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1325 return;
1326 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001327 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001328 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
Peter Boström0c4e06b2015-10-07 12:23:21 +02001332void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001333 ASSERT(signaling_thread()->IsCurrent());
1334 ASSERT(volume >= 0 && volume <= 10);
1335 if (!voice_channel_) {
1336 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1337 return;
1338 }
1339
solenberg4bac9c52015-10-09 02:32:53 -07001340 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001341 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001342 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001343}
1344
Peter Boström0c4e06b2015-10-07 12:23:21 +02001345bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 cricket::VideoCapturer* camera) {
1347 ASSERT(signaling_thread()->IsCurrent());
1348
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001349 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1351 // support video.
1352 LOG(LS_WARNING) << "Video not used in this call.";
1353 return false;
1354 }
1355 if (!video_channel_->SetCapturer(ssrc, camera)) {
1356 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1357 // This in the normal case when the underlying media channel has already
1358 // been deleted.
1359 ASSERT(camera == NULL);
1360 return false;
1361 }
1362 return true;
1363}
1364
Peter Boström0c4e06b2015-10-07 12:23:21 +02001365void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 bool enable,
1367 cricket::VideoRenderer* renderer) {
1368 ASSERT(signaling_thread()->IsCurrent());
1369 if (!video_channel_) {
1370 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1371 return;
1372 }
1373 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1374 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1375 // This in the normal case when the underlying media channel has already
1376 // been deleted.
1377 ASSERT(renderer == NULL);
1378 }
1379}
1380
Peter Boström0c4e06b2015-10-07 12:23:21 +02001381void WebRtcSession::SetVideoSend(uint32_t ssrc,
1382 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 const cricket::VideoOptions* options) {
1384 ASSERT(signaling_thread()->IsCurrent());
1385 if (!video_channel_) {
1386 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1387 return;
1388 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001389 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1391 // This in the normal case when the underlying media channel has already
1392 // been deleted.
1393 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395}
1396
1397bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1398 ASSERT(signaling_thread()->IsCurrent());
1399 if (!voice_channel_) {
1400 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1401 return false;
1402 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001403 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1405 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001406 if (!local_desc_ ||
1407 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 &send_ssrc)) {
1409 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1410 return false;
1411 }
1412 return voice_channel_->CanInsertDtmf();
1413}
1414
1415bool WebRtcSession::InsertDtmf(const std::string& track_id,
1416 int code, int duration) {
1417 ASSERT(signaling_thread()->IsCurrent());
1418 if (!voice_channel_) {
1419 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1420 return false;
1421 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001422 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001423 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1424 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1426 return false;
1427 }
1428 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1429 cricket::DF_SEND)) {
1430 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1431 return false;
1432 }
1433 return true;
1434}
1435
1436sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1437 return &SignalVoiceChannelDestroyed;
1438}
1439
wu@webrtc.org78187522013-10-07 23:32:02 +00001440bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001441 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001442 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001443 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001444 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1445 return false;
1446 }
1447 return data_channel_->SendData(params, payload, result);
1448}
1449
1450bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001451 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001452 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1453 return false;
1454 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001455 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1456 &DataChannel::OnChannelReady);
1457 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1458 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001459 data_channel_->SignalStreamClosedRemotely.connect(
1460 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001461 return true;
1462}
1463
1464void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001465 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001466 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1467 return;
1468 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001469 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1470 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001471 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001472}
1473
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001474void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001475 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001476 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1477 return;
1478 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001479 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1480 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001481}
1482
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001483void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001484 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001485 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1486 << "NULL.";
1487 return;
1488 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001489 data_channel_->RemoveRecvStream(sid);
1490 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001491}
1492
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001493bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001494 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001495}
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497cricket::DataChannelType WebRtcSession::data_channel_type() const {
1498 return data_channel_type_;
1499}
1500
wu@webrtc.org91053e72013-08-10 07:18:04 +00001501bool WebRtcSession::IceRestartPending() const {
1502 return ice_restart_latch_->Get();
1503}
1504
1505void WebRtcSession::ResetIceRestartLatch() {
1506 ice_restart_latch_->Reset();
1507}
1508
Henrik Boströmd8281982015-08-27 10:12:24 +02001509void WebRtcSession::OnCertificateReady(
1510 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001511 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001512}
1513
Henrik Boströmd8281982015-08-27 10:12:24 +02001514bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001515 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001516}
1517
deadbeefcbecd352015-09-23 11:50:27 -07001518const rtc::scoped_refptr<rtc::RTCCertificate>&
1519WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001520 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523void WebRtcSession::SetIceConnectionState(
1524 PeerConnectionInterface::IceConnectionState state) {
1525 if (ice_connection_state_ == state) {
1526 return;
1527 }
1528
1529 // ASSERT that the requested transition is allowed. Note that
1530 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1531 // within PeerConnection). This switch statement should compile away when
1532 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001533 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1534 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 switch (ice_connection_state_) {
1536 case PeerConnectionInterface::kIceConnectionNew:
1537 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1538 break;
1539 case PeerConnectionInterface::kIceConnectionChecking:
1540 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1541 state == PeerConnectionInterface::kIceConnectionConnected);
1542 break;
1543 case PeerConnectionInterface::kIceConnectionConnected:
1544 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1545 state == PeerConnectionInterface::kIceConnectionChecking ||
1546 state == PeerConnectionInterface::kIceConnectionCompleted);
1547 break;
1548 case PeerConnectionInterface::kIceConnectionCompleted:
1549 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1550 state == PeerConnectionInterface::kIceConnectionDisconnected);
1551 break;
1552 case PeerConnectionInterface::kIceConnectionFailed:
1553 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1554 break;
1555 case PeerConnectionInterface::kIceConnectionDisconnected:
1556 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1557 state == PeerConnectionInterface::kIceConnectionConnected ||
1558 state == PeerConnectionInterface::kIceConnectionCompleted ||
1559 state == PeerConnectionInterface::kIceConnectionFailed);
1560 break;
1561 case PeerConnectionInterface::kIceConnectionClosed:
1562 ASSERT(false);
1563 break;
1564 default:
1565 ASSERT(false);
1566 break;
1567 }
1568
1569 ice_connection_state_ = state;
1570 if (ice_observer_) {
1571 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1572 }
1573}
1574
deadbeefcbecd352015-09-23 11:50:27 -07001575void WebRtcSession::OnTransportControllerConnectionState(
1576 cricket::IceConnectionState state) {
1577 switch (state) {
1578 case cricket::kIceConnectionConnecting:
1579 // If the current state is Connected or Completed, then there were
1580 // writable channels but now there are not, so the next state must
1581 // be Disconnected.
1582 // kIceConnectionConnecting is currently used as the default,
1583 // un-connected state by the TransportController, so its only use is
1584 // detecting disconnections.
1585 if (ice_connection_state_ ==
1586 PeerConnectionInterface::kIceConnectionConnected ||
1587 ice_connection_state_ ==
1588 PeerConnectionInterface::kIceConnectionCompleted) {
1589 SetIceConnectionState(
1590 PeerConnectionInterface::kIceConnectionDisconnected);
1591 }
torbjornga81a42f2015-09-23 02:16:58 -07001592 break;
deadbeefcbecd352015-09-23 11:50:27 -07001593 case cricket::kIceConnectionFailed:
1594 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1595 break;
1596 case cricket::kIceConnectionConnected:
1597 LOG(LS_INFO) << "Changing to ICE connected state because "
1598 << "all transports are writable.";
1599 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1600 break;
1601 case cricket::kIceConnectionCompleted:
1602 LOG(LS_INFO) << "Changing to ICE completed state because "
1603 << "all transports are complete.";
1604 if (ice_connection_state_ !=
1605 PeerConnectionInterface::kIceConnectionConnected) {
1606 // If jumping directly from "checking" to "connected",
1607 // signal "connected" first.
1608 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1609 }
1610 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1611 if (metrics_observer_) {
1612 ReportTransportStats();
1613 }
1614 break;
1615 default:
1616 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001617 }
deadbeefcbecd352015-09-23 11:50:27 -07001618}
1619
1620void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001621 SetIceConnectionReceiving(receiving);
1622}
1623
1624void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1625 if (ice_connection_receiving_ == receiving) {
1626 return;
1627 }
1628 ice_connection_receiving_ = receiving;
1629 if (ice_observer_) {
1630 ice_observer_->OnIceConnectionReceivingChange(receiving);
1631 }
1632}
1633
deadbeefcbecd352015-09-23 11:50:27 -07001634void WebRtcSession::OnTransportControllerCandidatesGathered(
1635 const std::string& transport_name,
1636 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001638 int sdp_mline_index;
1639 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1640 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1641 << transport_name << " not found";
1642 return;
1643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644
deadbeefcbecd352015-09-23 11:50:27 -07001645 for (cricket::Candidates::const_iterator citer = candidates.begin();
1646 citer != candidates.end(); ++citer) {
1647 // Use transport_name as the candidate media id.
1648 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1649 if (ice_observer_) {
1650 ice_observer_->OnIceCandidate(&candidate);
1651 }
1652 if (local_desc_) {
1653 local_desc_->AddCandidate(&candidate);
1654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 }
1656}
1657
1658// Enabling voice and video channel.
1659void WebRtcSession::EnableChannels() {
1660 if (voice_channel_ && !voice_channel_->enabled())
1661 voice_channel_->Enable(true);
1662
1663 if (video_channel_ && !video_channel_->enabled())
1664 video_channel_->Enable(true);
1665
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001666 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 data_channel_->Enable(true);
1668}
1669
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670// Returns the media index for a local ice candidate given the content name.
1671bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1672 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001673 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676
1677 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001678 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 for (size_t index = 0; index < contents.size(); ++index) {
1680 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001681 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 content_found = true;
1683 break;
1684 }
1685 }
1686 return content_found;
1687}
1688
1689bool WebRtcSession::UseCandidatesInSessionDescription(
1690 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001691 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001693 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001695
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1697 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001698 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001699 const IceCandidateInterface* candidate = candidates->at(n);
1700 bool valid = false;
1701 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1702 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001703 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1704 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001705 }
1706 continue;
1707 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001708 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001709 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 break;
deadbeefd59daf82015-10-14 15:02:44 -07001711 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 }
1713 }
1714 return ret;
1715}
1716
1717bool WebRtcSession::UseCandidate(
1718 const IceCandidateInterface* candidate) {
1719
1720 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001721 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 if (mediacontent_index >= remote_content_size) {
1723 LOG(LS_ERROR)
1724 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1725 return false;
1726 }
1727
1728 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001729 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 std::vector<cricket::Candidate> candidates;
1731 candidates.push_back(candidate->candidate());
1732 // Invoking BaseSession method to handle remote candidates.
1733 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001734 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1735 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 // Candidates successfully submitted for checking.
1737 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1738 ice_connection_state_ ==
1739 PeerConnectionInterface::kIceConnectionDisconnected) {
1740 // If state is New, then the session has just gotten its first remote ICE
1741 // candidates, so go to Checking.
1742 // If state is Disconnected, the session is re-using old candidates or
1743 // receiving additional ones, so go to Checking.
1744 // If state is Connected, stay Connected.
1745 // TODO(bemasc): If state is Connected, and the new candidates are for a
1746 // newly added transport, then the state actually _should_ move to
1747 // checking. Add a way to distinguish that case.
1748 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1749 }
1750 // TODO(bemasc): If state is Completed, go back to Connected.
1751 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001752 if (!error.empty()) {
1753 LOG(LS_WARNING) << error;
1754 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756 return true;
1757}
1758
deadbeefcbecd352015-09-23 11:50:27 -07001759void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001760 // Destroy video_channel_ first since it may have a pointer to the
1761 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 const cricket::ContentInfo* video_info =
1763 cricket::GetFirstVideoContent(desc);
1764 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 SignalVideoChannelDestroyed();
1766 const std::string content_name = video_channel_->content_name();
1767 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 }
1769
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001770 const cricket::ContentInfo* voice_info =
1771 cricket::GetFirstAudioContent(desc);
1772 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001773 SignalVoiceChannelDestroyed();
1774 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001775 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001776 }
1777
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 const cricket::ContentInfo* data_info =
1779 cricket::GetFirstDataContent(desc);
1780 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 SignalDataChannelDestroyed();
1782 const std::string content_name = data_channel_->content_name();
1783 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 }
1785}
1786
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001787// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001788// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 // Creating the media channels and transport proxies.
1791 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1792 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_ERROR) << "Failed to create voice channel.";
1795 return false;
1796 }
1797 }
1798
1799 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1800 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001801 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 LOG(LS_ERROR) << "Failed to create video channel.";
1803 return false;
1804 }
1805 }
1806
1807 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1808 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001809 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001810 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 LOG(LS_ERROR) << "Failed to create data channel.";
1812 return false;
1813 }
1814 }
1815
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001816 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1817 if (voice_channel()) {
1818 voice_channel()->ActivateRtcpMux();
1819 }
1820 if (video_channel()) {
1821 video_channel()->ActivateRtcpMux();
1822 }
1823 if (data_channel()) {
1824 data_channel()->ActivateRtcpMux();
1825 }
1826 }
1827
deadbeefcbecd352015-09-23 11:50:27 -07001828 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001829 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001830 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1831 cricket::GROUP_TYPE_BUNDLE);
1832 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001833 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1834 return false;
1835 }
deadbeefcbecd352015-09-23 11:50:27 -07001836 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001837 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1838 return false;
1839 }
1840 }
1841
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 return true;
1843}
1844
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001845bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001847 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001848 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001849 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001850 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001851 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001852
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001853 voice_channel_->SignalDtlsSetupFailure.connect(
1854 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001855
1856 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001857 voice_channel_->transport_channel()->SignalSentPacket.connect(
1858 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001859 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860}
1861
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001862bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001864 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001865 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001866 if (!video_channel_) {
1867 return false;
1868 }
1869
1870 video_channel_->SignalDtlsSetupFailure.connect(
1871 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001872
1873 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001874 video_channel_->transport_channel()->SignalSentPacket.connect(
1875 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001876 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877}
1878
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001879bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001880 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001882 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001883 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001884 return false;
1885 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001886
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001887 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001888 data_channel_->SignalDataReceived.connect(
1889 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001890 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001891
1892 data_channel_->SignalDtlsSetupFailure.connect(
1893 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001894
1895 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001896 data_channel_->transport_channel()->SignalSentPacket.connect(
1897 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001898 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899}
1900
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001901void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001902 SetError(ERROR_TRANSPORT,
1903 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904}
1905
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001906void WebRtcSession::OnDataChannelMessageReceived(
1907 cricket::DataChannel* channel,
1908 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001909 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001910 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1911 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1912 // Received OPEN message; parse and signal that a new data channel should
1913 // be created.
1914 std::string label;
1915 InternalDataChannelInit config;
1916 config.id = params.ssrc;
1917 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1918 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1919 << params.ssrc;
1920 return;
1921 }
1922 config.open_handshake_role = InternalDataChannelInit::kAcker;
1923 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 }
deadbeefab9b2d12015-10-14 11:33:11 -07001925 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926}
1927
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001928// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001929bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001930 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1931 if (!bundle_enabled)
1932 return true;
1933
1934 const cricket::ContentGroup* bundle_group =
1935 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1936 ASSERT(bundle_group != NULL);
1937
1938 const cricket::ContentInfos& contents = desc->contents();
1939 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1940 citer != contents.end(); ++citer) {
1941 const cricket::ContentInfo* content = (&*citer);
1942 ASSERT(content != NULL);
1943 if (bundle_group->HasContentName(content->name) &&
1944 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1945 if (!HasRtcpMuxEnabled(content))
1946 return false;
1947 }
1948 }
1949 // RTCP-MUX is enabled in all the contents.
1950 return true;
1951}
1952
1953bool WebRtcSession::HasRtcpMuxEnabled(
1954 const cricket::ContentInfo* content) {
1955 const cricket::MediaContentDescription* description =
1956 static_cast<cricket::MediaContentDescription*>(content->description);
1957 return description->rtcp_mux();
1958}
1959
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001960bool WebRtcSession::ValidateSessionDescription(
1961 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001962 cricket::ContentSource source, std::string* err_desc) {
1963 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001964 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001965 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001966 }
1967
1968 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001969 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001970 }
1971
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001972 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001973 Action action = GetAction(sdesc->type());
1974 if (source == cricket::CS_LOCAL) {
1975 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001976 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001977 } else {
1978 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001979 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 }
1981
1982 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001983 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001984 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1985 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001986 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001987 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001988 }
1989
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001990 // Verify ice-ufrag and ice-pwd.
1991 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001992 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001993 }
1994
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001995 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001996 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001997 }
1998
1999 // Verify m-lines in Answer when compared against Offer.
2000 if (action == kAnswer) {
2001 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002002 (source == cricket::CS_LOCAL) ? remote_desc_->description()
2003 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002004 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002005 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002006 }
2007 }
2008
2009 return true;
2010}
2011
2012bool WebRtcSession::ExpectSetLocalDescription(Action action) {
2013 return ((action == kOffer && state() == STATE_INIT) ||
2014 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07002015 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002016 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002017 (action == kOffer && state() == STATE_INPROGRESS) ||
2018 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002019 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2020 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2021 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2022 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002023}
2024
2025bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2026 return ((action == kOffer && state() == STATE_INIT) ||
2027 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002028 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002029 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002030 (action == kOffer && state() == STATE_INPROGRESS) ||
2031 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002032 (action == kAnswer && state() == STATE_SENTOFFER) ||
2033 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2034 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2035 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002036}
2037
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002038std::string WebRtcSession::GetSessionErrorMsg() {
2039 std::ostringstream desc;
2040 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2041 desc << kSessionErrorDesc << error_desc() << ".";
2042 return desc.str();
2043}
2044
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002045// We need to check the local/remote description for the Transport instead of
2046// the session, because a new Transport added during renegotiation may have
2047// them unset while the session has them set from the previous negotiation.
2048// Not doing so may trigger the auto generation of transport description and
2049// mess up DTLS identity information, ICE credential, etc.
2050bool WebRtcSession::ReadyToUseRemoteCandidate(
2051 const IceCandidateInterface* candidate,
2052 const SessionDescriptionInterface* remote_desc,
2053 bool* valid) {
2054 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002055
2056 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002057 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002058
deadbeefd59daf82015-10-14 15:02:44 -07002059 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002060 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002061 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002062
2063 size_t mediacontent_index =
2064 static_cast<size_t>(candidate->sdp_mline_index());
2065 size_t remote_content_size =
2066 current_remote_desc->description()->contents().size();
2067 if (mediacontent_index >= remote_content_size) {
2068 LOG(LS_ERROR)
2069 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2070
2071 *valid = false;
2072 return false;
2073 }
2074
2075 cricket::ContentInfo content =
2076 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002077 cricket::BaseChannel* channel = GetChannel(content.name);
2078 if (!channel) {
2079 return false;
2080 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002081
deadbeefd59daf82015-10-14 15:02:44 -07002082 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002083 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002084}
2085
deadbeefcbecd352015-09-23 11:50:27 -07002086void WebRtcSession::OnTransportControllerGatheringState(
2087 cricket::IceGatheringState state) {
2088 ASSERT(signaling_thread()->IsCurrent());
2089 if (state == cricket::kIceGatheringGathering) {
2090 if (ice_observer_) {
2091 ice_observer_->OnIceGatheringChange(
2092 PeerConnectionInterface::kIceGatheringGathering);
2093 }
2094 } else if (state == cricket::kIceGatheringComplete) {
2095 if (ice_observer_) {
2096 ice_observer_->OnIceGatheringChange(
2097 PeerConnectionInterface::kIceGatheringComplete);
2098 ice_observer_->OnIceComplete();
2099 }
2100 }
2101}
2102
2103void WebRtcSession::ReportTransportStats() {
2104 // Use a set so we don't report the same stats twice if two channels share
2105 // a transport.
2106 std::set<std::string> transport_names;
2107 if (voice_channel()) {
2108 transport_names.insert(voice_channel()->transport_name());
2109 }
2110 if (video_channel()) {
2111 transport_names.insert(video_channel()->transport_name());
2112 }
2113 if (data_channel()) {
2114 transport_names.insert(data_channel()->transport_name());
2115 }
2116 for (const auto& name : transport_names) {
2117 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002118 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002119 ReportBestConnectionState(stats);
2120 ReportNegotiatedCiphers(stats);
2121 }
2122 }
2123}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002124// Walk through the ConnectionInfos to gather best connection usage
2125// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002126void WebRtcSession::ReportBestConnectionState(
2127 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002128 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002129 for (cricket::TransportChannelStatsList::const_iterator it =
2130 stats.channel_stats.begin();
2131 it != stats.channel_stats.end(); ++it) {
2132 for (cricket::ConnectionInfos::const_iterator it_info =
2133 it->connection_infos.begin();
2134 it_info != it->connection_infos.end(); ++it_info) {
2135 if (!it_info->best_connection) {
2136 continue;
2137 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002138
2139 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2140 const cricket::Candidate& local = it_info->local_candidate;
2141 const cricket::Candidate& remote = it_info->remote_candidate;
2142
2143 // Increment the counter for IceCandidatePairType.
2144 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2145 (local.type() == RELAY_PORT_TYPE &&
2146 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2147 type = kEnumCounterIceCandidatePairTypeTcp;
2148 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2149 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002150 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002151 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002152 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002153 metrics_observer_->IncrementEnumCounter(
2154 type, GetIceCandidatePairCounter(local, remote),
2155 kIceCandidatePairMax);
2156
2157 // Increment the counter for IP type.
2158 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002159 metrics_observer_->IncrementEnumCounter(
2160 kEnumCounterAddressFamily, kBestConnections_IPv4,
2161 kPeerConnectionAddressFamilyCounter_Max);
2162
2163 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002164 metrics_observer_->IncrementEnumCounter(
2165 kEnumCounterAddressFamily, kBestConnections_IPv6,
2166 kPeerConnectionAddressFamilyCounter_Max);
2167 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002168 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002169 }
2170
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002171 return;
2172 }
2173 }
2174}
2175
jbauchac8869e2015-07-03 01:36:14 -07002176void WebRtcSession::ReportNegotiatedCiphers(
2177 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002178 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002179 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2180 return;
2181 }
2182
2183 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07002184 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002185 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002186 return;
2187 }
2188
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002189 PeerConnectionEnumCounterType srtp_counter_type;
2190 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002191 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002192 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2193 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002194 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002195 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2196 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002197 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002198 srtp_counter_type = kEnumCounterDataSrtpCipher;
2199 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002200 } else {
2201 RTC_NOTREACHED();
2202 return;
2203 }
2204
2205 if (!srtp_cipher.empty()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002206 metrics_observer_->IncrementSparseEnumCounter(
2207 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002208 }
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002209 if (ssl_cipher) {
2210 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002211 }
2212}
2213
stefanc1aeaf02015-10-15 07:26:07 -07002214void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2215 const rtc::SentPacket& sent_packet) {
2216 RTC_DCHECK(worker_thread()->IsCurrent());
2217 media_controller_->call_w()->OnSentPacket(sent_packet);
2218}
2219
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220} // namespace webrtc