blob: 1fc2a37902740aeda83a8d3c3e58d90aa9d9117c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070034#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/app/webrtc/mediaconstraintsinterface.h"
39#include "talk/app/webrtc/mediastreamsignaling.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070041#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000042#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/base/constants.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/session/media/channel.h"
46#include "talk/session/media/channelmanager.h"
47#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070049#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070054#include "webrtc/call.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000055#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070056#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58using cricket::ContentInfo;
59using cricket::ContentInfos;
60using cricket::MediaContentDescription;
61using cricket::SessionDescription;
62using cricket::TransportInfo;
63
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070064using cricket::LOCAL_PORT_TYPE;
65using cricket::STUN_PORT_TYPE;
66using cricket::RELAY_PORT_TYPE;
67using cricket::PRFLX_PORT_TYPE;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069namespace webrtc {
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
73 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000074const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kInvalidCandidates[] = "Description contains invalid candidates.";
76const char kInvalidSdp[] = "Invalid session description.";
77const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
79const char kPushDownTDFailed[] =
80 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000081const char kSdpWithoutDtlsFingerprint[] =
82 "Called with SDP without DTLS fingerprint.";
83const char kSdpWithoutSdesCrypto[] =
84 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000085const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000086 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000088const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000089const char kDtlsSetupFailureRtp[] =
90 "Couldn't set up DTLS-SRTP on RTP channel.";
91const char kDtlsSetupFailureRtcp[] =
92 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070093const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000094const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070096IceCandidatePairType GetIceCandidatePairCounter(
97 const cricket::Candidate& local,
98 const cricket::Candidate& remote) {
99 const auto& l = local.type();
100 const auto& r = remote.type();
101 const auto& host = LOCAL_PORT_TYPE;
102 const auto& srflx = STUN_PORT_TYPE;
103 const auto& relay = RELAY_PORT_TYPE;
104 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700105 if (l == host && r == host) {
106 bool local_private = IPIsPrivate(local.address().ipaddr());
107 bool remote_private = IPIsPrivate(remote.address().ipaddr());
108 if (local_private) {
109 if (remote_private) {
110 return kIceCandidatePairHostPrivateHostPrivate;
111 } else {
112 return kIceCandidatePairHostPrivateHostPublic;
113 }
114 } else {
115 if (remote_private) {
116 return kIceCandidatePairHostPublicHostPrivate;
117 } else {
118 return kIceCandidatePairHostPublicHostPublic;
119 }
120 }
121 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700122 if (l == host && r == srflx)
123 return kIceCandidatePairHostSrflx;
124 if (l == host && r == relay)
125 return kIceCandidatePairHostRelay;
126 if (l == host && r == prflx)
127 return kIceCandidatePairHostPrflx;
128 if (l == srflx && r == host)
129 return kIceCandidatePairSrflxHost;
130 if (l == srflx && r == srflx)
131 return kIceCandidatePairSrflxSrflx;
132 if (l == srflx && r == relay)
133 return kIceCandidatePairSrflxRelay;
134 if (l == srflx && r == prflx)
135 return kIceCandidatePairSrflxPrflx;
136 if (l == relay && r == host)
137 return kIceCandidatePairRelayHost;
138 if (l == relay && r == srflx)
139 return kIceCandidatePairRelaySrflx;
140 if (l == relay && r == relay)
141 return kIceCandidatePairRelayRelay;
142 if (l == relay && r == prflx)
143 return kIceCandidatePairRelayPrflx;
144 if (l == prflx && r == host)
145 return kIceCandidatePairPrflxHost;
146 if (l == prflx && r == srflx)
147 return kIceCandidatePairPrflxSrflx;
148 if (l == prflx && r == relay)
149 return kIceCandidatePairPrflxRelay;
150 return kIceCandidatePairMax;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153// Compares |answer| against |offer|. Comparision is done
154// for number of m-lines in answer against offer. If matches true will be
155// returned otherwise false.
156static bool VerifyMediaDescriptions(
157 const SessionDescription* answer, const SessionDescription* offer) {
158 if (offer->contents().size() != answer->contents().size())
159 return false;
160
161 for (size_t i = 0; i < offer->contents().size(); ++i) {
162 if ((offer->contents()[i].name) != answer->contents()[i].name) {
163 return false;
164 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000165 const MediaContentDescription* offer_mdesc =
166 static_cast<const MediaContentDescription*>(
167 offer->contents()[i].description);
168 const MediaContentDescription* answer_mdesc =
169 static_cast<const MediaContentDescription*>(
170 answer->contents()[i].description);
171 if (offer_mdesc->type() != answer_mdesc->type()) {
172 return false;
173 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 }
175 return true;
176}
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178// Checks that each non-rejected content has SDES crypto keys or a DTLS
179// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
180// keys, will be caught in Transport negotiation, and backstopped by Channel's
181// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182static bool VerifyCrypto(const SessionDescription* desc,
183 bool dtls_enabled,
184 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 const ContentInfos& contents = desc->contents();
186 for (size_t index = 0; index < contents.size(); ++index) {
187 const ContentInfo* cinfo = &contents[index];
188 if (cinfo->rejected) {
189 continue;
190 }
191
192 // If the content isn't rejected, crypto must be present.
193 const MediaContentDescription* media =
194 static_cast<const MediaContentDescription*>(cinfo->description);
195 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
196 if (!media || !tinfo) {
197 // Something is not right.
198 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 return false;
201 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000203 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000204 LOG(LS_WARNING) <<
205 "Session description must have DTLS fingerprint if DTLS enabled.";
206 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000207 return false;
208 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000209 } else {
210 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000211 LOG(LS_WARNING) <<
212 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000213 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 return false;
215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217 }
218
219 return true;
220}
221
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000222// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
224 const ContentInfos& contents = desc->contents();
225 for (size_t index = 0; index < contents.size(); ++index) {
226 const ContentInfo* cinfo = &contents[index];
227 if (cinfo->rejected) {
228 continue;
229 }
230
231 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
232 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
233 if (!tinfo) {
234 // Something is not right.
235 LOG(LS_ERROR) << kInvalidSdp;
236 return false;
237 }
238 if (tinfo->description.ice_ufrag.empty() ||
239 tinfo->description.ice_pwd.empty()) {
240 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
241 return false;
242 }
243 }
244 return true;
245}
246
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247// Forces |sdesc->crypto_required| to the appropriate state based on the
248// current security policy, to ensure a failure occurs if there is an error
249// in crypto negotiation.
250// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000251static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
252 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000253 if (!sdesc) {
254 return;
255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 // Updating the |crypto_required_| in MediaContentDescription to the
258 // appropriate state based on the current security policy.
259 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
260 iter != sdesc->contents().end(); ++iter) {
261 if (cricket::IsMediaContent(&*iter)) {
262 MediaContentDescription* mdesc =
263 static_cast<MediaContentDescription*> (iter->description);
264 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000265 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266 }
267 }
268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
272 const std::string& track_id,
273 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 const cricket::ContentInfo* audio_info =
275 cricket::GetFirstAudioContent(session_description);
276 if (!audio_info) {
277 LOG(LS_ERROR) << "Audio not used in this call";
278 return false;
279 }
280
281 const cricket::MediaContentDescription* audio_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const cricket::StreamParams* stream =
285 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
286 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 return false;
288 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289
290 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return true;
292}
293
294static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200295 uint32_t ssrc,
296 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 ASSERT(track_id != NULL);
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const cricket::ContentInfo* audio_info =
300 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000301 if (audio_info) {
302 const cricket::MediaContentDescription* audio_content =
303 static_cast<const cricket::MediaContentDescription*>(
304 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000306 const auto* found =
307 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
308 if (found) {
309 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000310 return true;
311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
314 const cricket::ContentInfo* video_info =
315 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000316 if (video_info) {
317 const cricket::MediaContentDescription* video_content =
318 static_cast<const cricket::MediaContentDescription*>(
319 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000321 const auto* found =
322 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
323 if (found) {
324 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000325 return true;
326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328 return false;
329}
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331static bool BadSdp(const std::string& source,
332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
335 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700336 desc << "Failed to set " << source;
337 if (!type.empty()) {
338 desc << " " << type;
339 }
340 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return false;
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000356 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360static bool BadLocalSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
364}
365
366static bool BadRemoteSdp(const std::string& type,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
370}
371
372static bool BadOfferSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
376}
377
378static bool BadPranswerSdp(cricket::ContentSource source,
379 const std::string& reason,
380 std::string* err_desc) {
381 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
382 reason, err_desc);
383}
384
385static bool BadAnswerSdp(cricket::ContentSource source,
386 const std::string& reason,
387 std::string* err_desc) {
388 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389}
390
deadbeefd59daf82015-10-14 15:02:44 -0700391#define GET_STRING_OF_STATE(state) \
392 case webrtc::WebRtcSession::state: \
393 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 break;
395
deadbeefd59daf82015-10-14 15:02:44 -0700396static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 std::string result;
398 switch (state) {
399 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700400 GET_STRING_OF_STATE(STATE_SENTOFFER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
402 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
403 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700405 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
407 ASSERT(false);
408 break;
409 }
410 return result;
411}
412
deadbeefd59daf82015-10-14 15:02:44 -0700413#define GET_STRING_OF_ERROR_CODE(err) \
414 case webrtc::WebRtcSession::err: \
415 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 break;
417
deadbeefd59daf82015-10-14 15:02:44 -0700418static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 std::string result;
420 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
423 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 default:
deadbeefd59daf82015-10-14 15:02:44 -0700425 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 break;
427 }
428 return result;
429}
430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431static std::string MakeErrorString(const std::string& error,
432 const std::string& desc) {
433 std::ostringstream ret;
434 ret << error << " " << desc;
435 return ret.str();
436}
437
438static std::string MakeTdErrorString(const std::string& desc) {
439 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000442// Set |option| to the highest-priority value of |key| in the optional
443// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700444template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000445static void SetOptionFromOptionalConstraint(
446 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700447 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100448 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000449 if (!constraints) {
450 return;
451 }
452 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000453 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000454 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000455 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100456 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000457 }
458 }
459}
460
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000462 PeerConnectionInterface::IceTransportsType type) {
463 switch (type) {
464 case PeerConnectionInterface::kNone:
465 return cricket::CF_NONE;
466 case PeerConnectionInterface::kRelay:
467 return cricket::CF_RELAY;
468 case PeerConnectionInterface::kNoHost:
469 return (cricket::CF_ALL & ~cricket::CF_HOST);
470 case PeerConnectionInterface::kAll:
471 return cricket::CF_ALL;
472 default: ASSERT(false);
473 }
474 return cricket::CF_NONE;
475}
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477// Help class used to remember if a a remote peer has requested ice restart by
478// by sending a description with new ice ufrag and password.
479class IceRestartAnswerLatch {
480 public:
481 IceRestartAnswerLatch() : ice_restart_(false) { }
482
wu@webrtc.org91053e72013-08-10 07:18:04 +0000483 // Returns true if CheckForRemoteIceRestart has been called with a new session
484 // description where ice password and ufrag has changed since last time
485 // Reset() was called.
486 bool Get() const {
487 return ice_restart_;
488 }
489
490 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 if (ice_restart_) {
492 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
495
honghaiz503726c2015-07-31 12:37:38 -0700496 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
497 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700499 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 }
501 const SessionDescription* new_sd = new_desc->description();
502 const SessionDescription* old_sd = old_desc->description();
503 const ContentInfos& contents = new_sd->contents();
504 for (size_t index = 0; index < contents.size(); ++index) {
505 const ContentInfo* cinfo = &contents[index];
506 if (cinfo->rejected) {
507 continue;
508 }
509 // If the content isn't rejected, check if ufrag and password has
510 // changed.
511 const cricket::TransportDescription* new_transport_desc =
512 new_sd->GetTransportDescriptionByName(cinfo->name);
513 const cricket::TransportDescription* old_transport_desc =
514 old_sd->GetTransportDescriptionByName(cinfo->name);
515 if (!new_transport_desc || !old_transport_desc) {
516 // No transport description exist. This is not an ice restart.
517 continue;
518 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000519 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
520 old_transport_desc->ice_pwd,
521 new_transport_desc->ice_ufrag,
522 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 LOG(LS_INFO) << "Remote peer request ice restart.";
524 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700525 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 }
527 }
honghaiz503726c2015-07-31 12:37:38 -0700528 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 }
530
531 private:
532 bool ice_restart_;
533};
534
stefanc1aeaf02015-10-15 07:26:07 -0700535WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700536 rtc::Thread* signaling_thread,
537 rtc::Thread* worker_thread,
538 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700539 : signaling_thread_(signaling_thread),
540 worker_thread_(worker_thread),
541 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // RFC 3264: The numeric value of the session id and version in the
543 // o line MUST be representable with a "64 bit signed integer".
544 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700545 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
546 transport_controller_(new cricket::TransportController(signaling_thread,
547 worker_thread,
548 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700549 media_controller_(media_controller),
550 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 ice_observer_(NULL),
552 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700553 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000555 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000557 ice_restart_latch_(new IceRestartAnswerLatch),
558 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700559 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
560 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700561 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700562 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700563 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700564 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700565 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700566 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700567 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568}
569
570WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700571 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000572 // Destroy video_channel_ first since it may have a pointer to the
573 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000574 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 SignalVideoChannelDestroyed();
576 channel_manager_->DestroyVideoChannel(video_channel_.release());
577 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000578 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000579 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200580 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000581 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000582 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 SignalDataChannelDestroyed();
584 channel_manager_->DestroyDataChannel(data_channel_.release());
585 }
deadbeefd59daf82015-10-14 15:02:44 -0700586
587 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588}
589
wu@webrtc.org91053e72013-08-10 07:18:04 +0000590bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000591 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700592 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200593 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200594 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
595 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700596 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700597 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000598
Henrik Boström87713d02015-08-25 09:53:21 +0200599 // Obtain a certificate from RTCConfiguration if any were provided (optional).
600 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
601 if (!rtc_configuration.certificates.empty()) {
602 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
603 // just picking the first one. The decision should be made based on the DTLS
604 // handshake. The DTLS negotiations need to know about all certificates.
605 certificate = rtc_configuration.certificates[0];
606 }
607
honghaiz1f429e32015-09-28 07:57:34 -0700608 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700609
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 // TODO(perkj): Take |constraints| into consideration. Return false if not all
611 // mandatory constraints can be fulfilled. Note that |constraints|
612 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000614
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000615 if (options.disable_encryption) {
616 dtls_enabled_ = false;
617 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200618 // Enable DTLS by default if we have an identity store or a certificate.
619 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000620 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700621 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
622 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000623 dtls_enabled_ = value;
624 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000625 }
626
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000628 // It takes precendence over the disable_sctp_data_channels
629 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 if (FindConstraint(
631 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
632 &value, NULL) && value) {
633 LOG(LS_INFO) << "Allowing RTP data engine.";
634 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000636 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000637 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000638 LOG(LS_INFO) << "Allowing SCTP data engine.";
639 data_channel_type_ = cricket::DCT_SCTP;
640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642
wu@webrtc.orgde305012013-10-31 15:40:38 +0000643 // Find DSCP constraint.
644 if (FindConstraint(
645 constraints,
646 MediaConstraintsInterface::kEnableDscp,
647 &value, NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100648 audio_options_.dscp = rtc::Optional<bool>(value);
649 video_options_.dscp = rtc::Optional<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000650 }
651
652 // Find Suspend Below Min Bitrate constraint.
653 if (FindConstraint(
654 constraints,
655 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
656 &value,
657 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100658 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000659 }
660
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000661 SetOptionFromOptionalConstraint(constraints,
662 MediaConstraintsInterface::kScreencastMinBitrate,
663 &video_options_.screencast_min_bitrate);
664
665 // Find constraints for cpu overuse detection.
666 SetOptionFromOptionalConstraint(constraints,
667 MediaConstraintsInterface::kCpuUnderuseThreshold,
668 &video_options_.cpu_underuse_threshold);
669 SetOptionFromOptionalConstraint(constraints,
670 MediaConstraintsInterface::kCpuOveruseThreshold,
671 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000672 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000673 MediaConstraintsInterface::kCpuOveruseDetection,
674 &video_options_.cpu_overuse_detection);
675 SetOptionFromOptionalConstraint(constraints,
676 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
677 &video_options_.cpu_overuse_encode_usage);
678 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000679 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
680 &video_options_.cpu_underuse_encode_rsd_threshold);
681 SetOptionFromOptionalConstraint(constraints,
682 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
683 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000684
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000685 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000686 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
687 &video_options_.unsignalled_recv_stream_limit);
kwiberg102c6a62015-10-30 02:47:38 -0700688 if (video_options_.unsignalled_recv_stream_limit) {
Karl Wibergbe579832015-11-10 22:34:18 +0100689 video_options_.unsignalled_recv_stream_limit = rtc::Optional<int>(
kwiberg102c6a62015-10-30 02:47:38 -0700690 std::max(0, std::min(kMaxUnsignalledRecvStreams,
691 *video_options_.unsignalled_recv_stream_limit)));
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000692 }
693
694 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000695 MediaConstraintsInterface::kHighStartBitrate,
696 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000697
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000698 SetOptionFromOptionalConstraint(constraints,
699 MediaConstraintsInterface::kCombinedAudioVideoBwe,
700 &audio_options_.combined_audio_video_bwe);
701
kwiberg102c6a62015-10-30 02:47:38 -0700702 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100703 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200704
Karl Wibergbe579832015-11-10 22:34:18 +0100705 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
706 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 const cricket::VideoCodec default_codec(
709 JsepSessionDescription::kDefaultVideoCodecId,
710 JsepSessionDescription::kDefaultVideoCodecName,
711 JsepSessionDescription::kMaxVideoCodecWidth,
712 JsepSessionDescription::kMaxVideoCodecHeight,
713 JsepSessionDescription::kDefaultVideoCodecFramerate,
714 JsepSessionDescription::kDefaultVideoCodecPreference);
715 channel_manager_->SetDefaultVideoEncoderConfig(
716 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000717
Henrik Boström87713d02015-08-25 09:53:21 +0200718 if (!dtls_enabled_) {
719 // Construct with DTLS disabled.
720 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700721 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200722 } else {
723 // Construct with DTLS enabled.
724 if (!certificate) {
725 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700726 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200727 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700728 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
729 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200730 } else {
731 // Use the already generated certificate.
732 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700733 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200734 }
735 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000736
Henrik Boströmd8281982015-08-27 10:12:24 +0200737 webrtc_session_desc_factory_->SignalCertificateReady.connect(
738 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000739
wu@webrtc.org97077a32013-10-25 21:18:33 +0000740 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000741 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000742 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000743 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200744 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700745
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 return true;
747}
748
deadbeefd59daf82015-10-14 15:02:44 -0700749void WebRtcSession::Close() {
750 SetState(STATE_CLOSED);
751 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000752 ASSERT(!voice_channel_);
753 ASSERT(!video_channel_);
754 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755}
756
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000757void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
758 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759}
760
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000761cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
762 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763}
764
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000765bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700766 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000767 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
768 << "SSL Role of the session.";
769 return false;
770 }
771
deadbeefd59daf82015-10-14 15:02:44 -0700772 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000773}
774
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000775void WebRtcSession::CreateOffer(
776 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700777 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
778 const cricket::MediaSessionOptions& session_options) {
779 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000780}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781
deadbeefab9b2d12015-10-14 11:33:11 -0700782void WebRtcSession::CreateAnswer(
783 CreateSessionDescriptionObserver* observer,
784 const MediaConstraintsInterface* constraints,
785 const cricket::MediaSessionOptions& session_options) {
786 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
787 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788}
789
790bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
791 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700792 ASSERT(signaling_thread()->IsCurrent());
793
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000794 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000795 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000796
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000797 // Validate SDP.
798 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
799 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 }
801
deadbeefd59daf82015-10-14 15:02:44 -0700802 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000803 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700805 initial_offerer_ = true;
806 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 }
808
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000809 cricket::SecurePolicy sdes_policy =
810 webrtc_session_desc_factory_->SdesPolicy();
811 cricket::CryptoType crypto_required = dtls_enabled_ ?
812 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
813 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000815 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000817 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818
819 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000820 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 // TODO(mallinath) - Handle CreateChannel failure, as new local description
822 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000823 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 }
825
deadbeefcbecd352015-09-23 11:50:27 -0700826 // Remove unused channels if MediaContentDescription is rejected.
827 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000829 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 return false;
831 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000832
deadbeefd59daf82015-10-14 15:02:44 -0700833 if (remote_desc_) {
834 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700835 UseCandidatesInSessionDescription(remote_desc_.get());
836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837
deadbeefd59daf82015-10-14 15:02:44 -0700838 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000839 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
841 return true;
842}
843
844bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
845 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700846 ASSERT(signaling_thread()->IsCurrent());
847
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000848 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000849 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000850
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000851 // Validate SDP.
852 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
853 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
855
deadbeefd59daf82015-10-14 15:02:44 -0700856 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
857 remote_desc_.release());
858 remote_desc_.reset(desc_temp.release());
859
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000861 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 if (action == kOffer && !CreateChannels(desc->description())) {
863 // TODO(mallinath) - Handle CreateChannel failure, as new local description
864 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000865 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 }
867
deadbeefcbecd352015-09-23 11:50:27 -0700868 // Remove unused channels if MediaContentDescription is rejected.
869 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870
871 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
872 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000873 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 return false;
875 }
876
deadbeefd59daf82015-10-14 15:02:44 -0700877 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
880
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 // Check if this new SessionDescription contains new ice ufrag and password
882 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700883 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700884 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700885 // We retain all received candidates only if ICE is not restarted.
886 // When ICE is restarted, all previous candidates belong to an old generation
887 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700888 // TODO(deadbeef): This goes against the W3C spec which says the remote
889 // description should only contain candidates from the last set remote
890 // description plus any candidates added since then. We should remove this
891 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700892 if (!ice_restart) {
893 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700894 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700895 }
896
deadbeefd59daf82015-10-14 15:02:44 -0700897 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000898 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000900
901 // Set the the ICE connection state to connecting since the connection may
902 // become writable with peer reflexive candidates before any remote candidate
903 // is signaled.
904 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
905 // is to have a new signal the indicates a change in checking state from the
906 // transport and expose a new checking() member from transport that can be
907 // read to determine the current checking state. The existing SignalConnecting
908 // actually means "gathering candidates", so cannot be be used here.
909 if (desc->type() != SessionDescriptionInterface::kOffer &&
910 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
911 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 return true;
914}
915
deadbeefd59daf82015-10-14 15:02:44 -0700916void WebRtcSession::LogState(State old_state, State new_state) {
917 LOG(LS_INFO) << "Session:" << id()
918 << " Old state:" << GetStateString(old_state)
919 << " New state:" << GetStateString(new_state);
920}
921
922void WebRtcSession::SetState(State state) {
923 ASSERT(signaling_thread_->IsCurrent());
924 if (state != state_) {
925 LogState(state_, state);
926 state_ = state;
927 SignalState(this, state_);
928 }
929}
930
931void WebRtcSession::SetError(Error error, const std::string& error_desc) {
932 ASSERT(signaling_thread_->IsCurrent());
933 if (error != error_) {
934 error_ = error;
935 error_desc_ = error_desc;
936 }
937}
938
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939bool WebRtcSession::UpdateSessionState(
940 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700942 ASSERT(signaling_thread()->IsCurrent());
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 // If there's already a pending error then no state transition should happen.
945 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700946 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000947 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000949 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
950 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 }
deadbeefd59daf82015-10-14 15:02:44 -0700952 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
953 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000954 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700955 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000956 }
deadbeefd59daf82015-10-14 15:02:44 -0700957 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000958 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 }
960 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000961 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
962 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 }
964 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700965 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
966 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000967 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700968 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000969 }
deadbeefd59daf82015-10-14 15:02:44 -0700970 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000971 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 }
973 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000974 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
975 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 }
deadbeefcbecd352015-09-23 11:50:27 -0700977 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700978 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700979 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700980 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700981 if (local_bundle && remote_bundle) {
982 // The answerer decides the transport to bundle on
983 const cricket::ContentGroup* answer_bundle =
984 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
985 if (!EnableBundle(*answer_bundle)) {
986 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
987 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
988 }
989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700991 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000992 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700993 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000994 }
deadbeefd59daf82015-10-14 15:02:44 -0700995 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000996 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 }
998 }
999 return true;
1000}
1001
1002WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1003 if (type == SessionDescriptionInterface::kOffer) {
1004 return WebRtcSession::kOffer;
1005 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1006 return WebRtcSession::kPrAnswer;
1007 } else if (type == SessionDescriptionInterface::kAnswer) {
1008 return WebRtcSession::kAnswer;
1009 }
1010 ASSERT(false && "unknown action type");
1011 return WebRtcSession::kOffer;
1012}
1013
deadbeefd59daf82015-10-14 15:02:44 -07001014bool WebRtcSession::PushdownMediaDescription(
1015 cricket::ContentAction action,
1016 cricket::ContentSource source,
1017 std::string* err) {
1018 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1019 if (!ch) {
1020 return true;
1021 } else if (source == cricket::CS_LOCAL) {
1022 return ch->PushdownLocalDescription(local_desc_->description(), action,
1023 err);
1024 } else {
1025 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1026 err);
1027 }
1028 };
1029
1030 return (set_content(voice_channel()) &&
1031 set_content(video_channel()) &&
1032 set_content(data_channel()));
1033}
1034
1035bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1036 cricket::ContentAction action,
1037 std::string* error_desc) {
1038 RTC_DCHECK(signaling_thread()->IsCurrent());
1039
1040 if (source == cricket::CS_LOCAL) {
1041 return PushdownLocalTransportDescription(local_desc_->description(), action,
1042 error_desc);
1043 }
1044 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1045 error_desc);
1046}
1047
1048bool WebRtcSession::PushdownLocalTransportDescription(
1049 const SessionDescription* sdesc,
1050 cricket::ContentAction action,
1051 std::string* err) {
1052 RTC_DCHECK(signaling_thread()->IsCurrent());
1053
1054 if (!sdesc) {
1055 return false;
1056 }
1057
1058 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1059 if (!transport_controller_->SetLocalTransportDescription(
1060 tinfo.content_name, tinfo.description, action, err)) {
1061 return false;
1062 }
1063 }
1064
1065 return true;
1066}
1067
1068bool WebRtcSession::PushdownRemoteTransportDescription(
1069 const SessionDescription* sdesc,
1070 cricket::ContentAction action,
1071 std::string* err) {
1072 RTC_DCHECK(signaling_thread()->IsCurrent());
1073
1074 if (!sdesc) {
1075 return false;
1076 }
1077
1078 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1079 if (!transport_controller_->SetRemoteTransportDescription(
1080 tinfo.content_name, tinfo.description, action, err)) {
1081 return false;
1082 }
1083 }
1084
1085 return true;
1086}
1087
1088bool WebRtcSession::GetTransportDescription(
1089 const SessionDescription* description,
1090 const std::string& content_name,
1091 cricket::TransportDescription* tdesc) {
1092 if (!description || !tdesc) {
1093 return false;
1094 }
1095 const TransportInfo* transport_info =
1096 description->GetTransportInfoByName(content_name);
1097 if (!transport_info) {
1098 return false;
1099 }
1100 *tdesc = transport_info->description;
1101 return true;
1102}
1103
1104bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001105 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001106 return (GetChannelTransportStats(voice_channel(), stats) &&
1107 GetChannelTransportStats(video_channel(), stats) &&
1108 GetChannelTransportStats(data_channel(), stats));
1109}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001110
deadbeefcbecd352015-09-23 11:50:27 -07001111bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001112 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001113 ASSERT(signaling_thread()->IsCurrent());
1114 if (!ch) {
1115 // Not using this channel.
1116 return true;
1117 }
1118
1119 const std::string& content_name = ch->content_name();
1120 const std::string& transport_name = ch->transport_name();
1121 stats->proxy_to_transport[content_name] = transport_name;
1122 if (stats->transport_stats.find(transport_name) !=
1123 stats->transport_stats.end()) {
1124 // Transport stats already done for this transport.
1125 return true;
1126 }
1127
1128 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001129 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001130 return false;
1131 }
1132
1133 stats->transport_stats[transport_name] = tstats;
1134 return true;
1135}
1136
1137bool WebRtcSession::GetLocalCertificate(
1138 const std::string& transport_name,
1139 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1140 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001141 return transport_controller_->GetLocalCertificate(transport_name,
1142 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001143}
1144
1145bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1146 rtc::SSLCertificate** cert) {
1147 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001148 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001149}
1150
1151cricket::BaseChannel* WebRtcSession::GetChannel(
1152 const std::string& content_name) {
1153 if (voice_channel() && voice_channel()->content_name() == content_name) {
1154 return voice_channel();
1155 }
1156 if (video_channel() && video_channel()->content_name() == content_name) {
1157 return video_channel();
1158 }
1159 if (data_channel() && data_channel()->content_name() == content_name) {
1160 return data_channel();
1161 }
1162 return nullptr;
1163}
1164
1165bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1166 const std::string* first_content_name = bundle.FirstContentName();
1167 if (!first_content_name) {
1168 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1169 return false;
1170 }
1171 const std::string& transport_name = *first_content_name;
1172 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1173
1174 auto maybe_set_transport = [this, bundle, transport_name,
1175 first_channel](cricket::BaseChannel* ch) {
1176 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001177 return true;
1178 }
1179
deadbeefcbecd352015-09-23 11:50:27 -07001180 if (ch->transport_name() == transport_name) {
1181 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1182 << " on " << transport_name << ".";
1183 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001184 }
torbjornga81a42f2015-09-23 02:16:58 -07001185
deadbeefcbecd352015-09-23 11:50:27 -07001186 if (!ch->SetTransport(transport_name)) {
1187 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1188 return false;
1189 }
1190 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1191 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001192 return true;
1193 };
1194
deadbeefcbecd352015-09-23 11:50:27 -07001195 if (!maybe_set_transport(voice_channel()) ||
1196 !maybe_set_transport(video_channel()) ||
1197 !maybe_set_transport(data_channel())) {
1198 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001199 }
deadbeefcbecd352015-09-23 11:50:27 -07001200
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001201 return true;
1202}
1203
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001205 if (!remote_desc_) {
1206 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1207 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 return false;
1209 }
1210
1211 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001212 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 return false;
1214 }
1215
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001216 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001217 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1218 if (!valid) {
1219 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 }
1221
1222 // Add this candidate to the remote session description.
1223 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001224 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 return false;
1226 }
1227
deadbeefd59daf82015-10-14 15:02:44 -07001228 if (ready) {
1229 return UseCandidate(candidate);
1230 } else {
1231 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1232 return true;
1233 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234}
1235
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001236bool WebRtcSession::SetIceTransports(
1237 PeerConnectionInterface::IceTransportsType type) {
1238 return port_allocator()->set_candidate_filter(
1239 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001240}
1241
deadbeefd59daf82015-10-14 15:02:44 -07001242cricket::IceConfig WebRtcSession::ParseIceConfig(
1243 const PeerConnectionInterface::RTCConfiguration& config) const {
1244 cricket::IceConfig ice_config;
1245 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1246 ice_config.gather_continually = (config.continual_gathering_policy ==
1247 PeerConnectionInterface::GATHER_CONTINUALLY);
1248 return ice_config;
1249}
1250
1251void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1252 transport_controller_->SetIceConfig(config);
1253}
1254
1255void WebRtcSession::MaybeStartGathering() {
1256 transport_controller_->MaybeStartGathering();
1257}
1258
Peter Boström0c4e06b2015-10-07 12:23:21 +02001259bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1260 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001261 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001263 }
1264 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265}
1266
Peter Boström0c4e06b2015-10-07 12:23:21 +02001267bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1268 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001269 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001270 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001271 }
1272 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273}
1274
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001275std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001277 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 return desc.str();
1279}
1280
solenbergd4cec0d2015-10-09 08:55:48 -07001281void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 ASSERT(signaling_thread()->IsCurrent());
1283 if (!voice_channel_) {
1284 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1285 return;
1286 }
solenberg4bac9c52015-10-09 02:32:53 -07001287 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1288 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 // otherwise. This in the normal case when the underlying media channel has
1290 // already been deleted.
1291 ASSERT(enable == false);
1292 }
1293}
1294
Peter Boström0c4e06b2015-10-07 12:23:21 +02001295void WebRtcSession::SetAudioSend(uint32_t ssrc,
1296 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001297 const cricket::AudioOptions& options,
1298 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 ASSERT(signaling_thread()->IsCurrent());
1300 if (!voice_channel_) {
1301 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1302 return;
1303 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001304 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001305 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001306 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307}
1308
Peter Boström0c4e06b2015-10-07 12:23:21 +02001309void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001310 ASSERT(signaling_thread()->IsCurrent());
1311 ASSERT(volume >= 0 && volume <= 10);
1312 if (!voice_channel_) {
1313 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1314 return;
1315 }
1316
solenberg4bac9c52015-10-09 02:32:53 -07001317 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001318 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001319 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001320}
1321
Peter Boström0c4e06b2015-10-07 12:23:21 +02001322bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 cricket::VideoCapturer* camera) {
1324 ASSERT(signaling_thread()->IsCurrent());
1325
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001326 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1328 // support video.
1329 LOG(LS_WARNING) << "Video not used in this call.";
1330 return false;
1331 }
1332 if (!video_channel_->SetCapturer(ssrc, camera)) {
1333 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1334 // This in the normal case when the underlying media channel has already
1335 // been deleted.
1336 ASSERT(camera == NULL);
1337 return false;
1338 }
1339 return true;
1340}
1341
Peter Boström0c4e06b2015-10-07 12:23:21 +02001342void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 bool enable,
1344 cricket::VideoRenderer* renderer) {
1345 ASSERT(signaling_thread()->IsCurrent());
1346 if (!video_channel_) {
1347 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1348 return;
1349 }
1350 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1351 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1352 // This in the normal case when the underlying media channel has already
1353 // been deleted.
1354 ASSERT(renderer == NULL);
1355 }
1356}
1357
Peter Boström0c4e06b2015-10-07 12:23:21 +02001358void WebRtcSession::SetVideoSend(uint32_t ssrc,
1359 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 const cricket::VideoOptions* options) {
1361 ASSERT(signaling_thread()->IsCurrent());
1362 if (!video_channel_) {
1363 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1364 return;
1365 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001366 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1368 // This in the normal case when the underlying media channel has already
1369 // been deleted.
1370 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372}
1373
1374bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1375 ASSERT(signaling_thread()->IsCurrent());
1376 if (!voice_channel_) {
1377 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1378 return false;
1379 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001380 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1382 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001383 if (!local_desc_ ||
1384 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 &send_ssrc)) {
1386 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1387 return false;
1388 }
1389 return voice_channel_->CanInsertDtmf();
1390}
1391
1392bool WebRtcSession::InsertDtmf(const std::string& track_id,
1393 int code, int duration) {
1394 ASSERT(signaling_thread()->IsCurrent());
1395 if (!voice_channel_) {
1396 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1397 return false;
1398 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001399 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001400 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1401 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1403 return false;
1404 }
1405 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1406 cricket::DF_SEND)) {
1407 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1408 return false;
1409 }
1410 return true;
1411}
1412
1413sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1414 return &SignalVoiceChannelDestroyed;
1415}
1416
wu@webrtc.org78187522013-10-07 23:32:02 +00001417bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001418 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001419 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001420 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001421 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1422 return false;
1423 }
1424 return data_channel_->SendData(params, payload, result);
1425}
1426
1427bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001428 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001429 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1430 return false;
1431 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001432 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1433 &DataChannel::OnChannelReady);
1434 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1435 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001436 data_channel_->SignalStreamClosedRemotely.connect(
1437 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001438 return true;
1439}
1440
1441void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001442 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001443 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1444 return;
1445 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001446 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1447 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001448 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001449}
1450
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001451void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001452 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001453 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1454 return;
1455 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001456 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1457 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001458}
1459
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001460void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001461 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001462 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1463 << "NULL.";
1464 return;
1465 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001466 data_channel_->RemoveRecvStream(sid);
1467 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001468}
1469
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001470bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001471 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001472}
1473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474cricket::DataChannelType WebRtcSession::data_channel_type() const {
1475 return data_channel_type_;
1476}
1477
wu@webrtc.org91053e72013-08-10 07:18:04 +00001478bool WebRtcSession::IceRestartPending() const {
1479 return ice_restart_latch_->Get();
1480}
1481
1482void WebRtcSession::ResetIceRestartLatch() {
1483 ice_restart_latch_->Reset();
1484}
1485
Henrik Boströmd8281982015-08-27 10:12:24 +02001486void WebRtcSession::OnCertificateReady(
1487 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001488 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001489}
1490
Henrik Boströmd8281982015-08-27 10:12:24 +02001491bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001492 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001493}
1494
deadbeefcbecd352015-09-23 11:50:27 -07001495const rtc::scoped_refptr<rtc::RTCCertificate>&
1496WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001497 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001498}
1499
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500void WebRtcSession::SetIceConnectionState(
1501 PeerConnectionInterface::IceConnectionState state) {
1502 if (ice_connection_state_ == state) {
1503 return;
1504 }
1505
1506 // ASSERT that the requested transition is allowed. Note that
1507 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1508 // within PeerConnection). This switch statement should compile away when
1509 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001510 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1511 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 switch (ice_connection_state_) {
1513 case PeerConnectionInterface::kIceConnectionNew:
1514 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1515 break;
1516 case PeerConnectionInterface::kIceConnectionChecking:
1517 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1518 state == PeerConnectionInterface::kIceConnectionConnected);
1519 break;
1520 case PeerConnectionInterface::kIceConnectionConnected:
1521 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1522 state == PeerConnectionInterface::kIceConnectionChecking ||
1523 state == PeerConnectionInterface::kIceConnectionCompleted);
1524 break;
1525 case PeerConnectionInterface::kIceConnectionCompleted:
1526 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1527 state == PeerConnectionInterface::kIceConnectionDisconnected);
1528 break;
1529 case PeerConnectionInterface::kIceConnectionFailed:
1530 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1531 break;
1532 case PeerConnectionInterface::kIceConnectionDisconnected:
1533 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1534 state == PeerConnectionInterface::kIceConnectionConnected ||
1535 state == PeerConnectionInterface::kIceConnectionCompleted ||
1536 state == PeerConnectionInterface::kIceConnectionFailed);
1537 break;
1538 case PeerConnectionInterface::kIceConnectionClosed:
1539 ASSERT(false);
1540 break;
1541 default:
1542 ASSERT(false);
1543 break;
1544 }
1545
1546 ice_connection_state_ = state;
1547 if (ice_observer_) {
1548 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1549 }
1550}
1551
deadbeefcbecd352015-09-23 11:50:27 -07001552void WebRtcSession::OnTransportControllerConnectionState(
1553 cricket::IceConnectionState state) {
1554 switch (state) {
1555 case cricket::kIceConnectionConnecting:
1556 // If the current state is Connected or Completed, then there were
1557 // writable channels but now there are not, so the next state must
1558 // be Disconnected.
1559 // kIceConnectionConnecting is currently used as the default,
1560 // un-connected state by the TransportController, so its only use is
1561 // detecting disconnections.
1562 if (ice_connection_state_ ==
1563 PeerConnectionInterface::kIceConnectionConnected ||
1564 ice_connection_state_ ==
1565 PeerConnectionInterface::kIceConnectionCompleted) {
1566 SetIceConnectionState(
1567 PeerConnectionInterface::kIceConnectionDisconnected);
1568 }
torbjornga81a42f2015-09-23 02:16:58 -07001569 break;
deadbeefcbecd352015-09-23 11:50:27 -07001570 case cricket::kIceConnectionFailed:
1571 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1572 break;
1573 case cricket::kIceConnectionConnected:
1574 LOG(LS_INFO) << "Changing to ICE connected state because "
1575 << "all transports are writable.";
1576 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1577 break;
1578 case cricket::kIceConnectionCompleted:
1579 LOG(LS_INFO) << "Changing to ICE completed state because "
1580 << "all transports are complete.";
1581 if (ice_connection_state_ !=
1582 PeerConnectionInterface::kIceConnectionConnected) {
1583 // If jumping directly from "checking" to "connected",
1584 // signal "connected" first.
1585 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1586 }
1587 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1588 if (metrics_observer_) {
1589 ReportTransportStats();
1590 }
1591 break;
1592 default:
1593 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001594 }
deadbeefcbecd352015-09-23 11:50:27 -07001595}
1596
1597void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001598 SetIceConnectionReceiving(receiving);
1599}
1600
1601void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1602 if (ice_connection_receiving_ == receiving) {
1603 return;
1604 }
1605 ice_connection_receiving_ = receiving;
1606 if (ice_observer_) {
1607 ice_observer_->OnIceConnectionReceivingChange(receiving);
1608 }
1609}
1610
deadbeefcbecd352015-09-23 11:50:27 -07001611void WebRtcSession::OnTransportControllerCandidatesGathered(
1612 const std::string& transport_name,
1613 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001615 int sdp_mline_index;
1616 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1617 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1618 << transport_name << " not found";
1619 return;
1620 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621
deadbeefcbecd352015-09-23 11:50:27 -07001622 for (cricket::Candidates::const_iterator citer = candidates.begin();
1623 citer != candidates.end(); ++citer) {
1624 // Use transport_name as the candidate media id.
1625 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1626 if (ice_observer_) {
1627 ice_observer_->OnIceCandidate(&candidate);
1628 }
1629 if (local_desc_) {
1630 local_desc_->AddCandidate(&candidate);
1631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633}
1634
1635// Enabling voice and video channel.
1636void WebRtcSession::EnableChannels() {
1637 if (voice_channel_ && !voice_channel_->enabled())
1638 voice_channel_->Enable(true);
1639
1640 if (video_channel_ && !video_channel_->enabled())
1641 video_channel_->Enable(true);
1642
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001643 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 data_channel_->Enable(true);
1645}
1646
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647// Returns the media index for a local ice candidate given the content name.
1648bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1649 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001650 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001652 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653
1654 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001655 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 for (size_t index = 0; index < contents.size(); ++index) {
1657 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001658 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 content_found = true;
1660 break;
1661 }
1662 }
1663 return content_found;
1664}
1665
1666bool WebRtcSession::UseCandidatesInSessionDescription(
1667 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001668 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001672
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1674 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001675 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001676 const IceCandidateInterface* candidate = candidates->at(n);
1677 bool valid = false;
1678 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1679 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001680 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1681 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001682 }
1683 continue;
1684 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001685 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001686 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 break;
deadbeefd59daf82015-10-14 15:02:44 -07001688 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 }
1690 }
1691 return ret;
1692}
1693
1694bool WebRtcSession::UseCandidate(
1695 const IceCandidateInterface* candidate) {
1696
1697 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001698 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 if (mediacontent_index >= remote_content_size) {
1700 LOG(LS_ERROR)
1701 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1702 return false;
1703 }
1704
1705 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001706 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 std::vector<cricket::Candidate> candidates;
1708 candidates.push_back(candidate->candidate());
1709 // Invoking BaseSession method to handle remote candidates.
1710 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001711 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1712 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 // Candidates successfully submitted for checking.
1714 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1715 ice_connection_state_ ==
1716 PeerConnectionInterface::kIceConnectionDisconnected) {
1717 // If state is New, then the session has just gotten its first remote ICE
1718 // candidates, so go to Checking.
1719 // If state is Disconnected, the session is re-using old candidates or
1720 // receiving additional ones, so go to Checking.
1721 // If state is Connected, stay Connected.
1722 // TODO(bemasc): If state is Connected, and the new candidates are for a
1723 // newly added transport, then the state actually _should_ move to
1724 // checking. Add a way to distinguish that case.
1725 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1726 }
1727 // TODO(bemasc): If state is Completed, go back to Connected.
1728 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001729 if (!error.empty()) {
1730 LOG(LS_WARNING) << error;
1731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 }
1733 return true;
1734}
1735
deadbeefcbecd352015-09-23 11:50:27 -07001736void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001737 // Destroy video_channel_ first since it may have a pointer to the
1738 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 const cricket::ContentInfo* video_info =
1740 cricket::GetFirstVideoContent(desc);
1741 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 SignalVideoChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001743 const std::string content_name = video_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 }
1746
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001747 const cricket::ContentInfo* voice_info =
1748 cricket::GetFirstAudioContent(desc);
1749 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001750 SignalVoiceChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001751 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001752 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001753 }
1754
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 const cricket::ContentInfo* data_info =
1756 cricket::GetFirstDataContent(desc);
1757 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 SignalDataChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001759 const std::string content_name = data_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
1762}
1763
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001764// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001765// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 // Creating the media channels and transport proxies.
1768 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1769 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001770 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 LOG(LS_ERROR) << "Failed to create voice channel.";
1772 return false;
1773 }
1774 }
1775
1776 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1777 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001778 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 LOG(LS_ERROR) << "Failed to create video channel.";
1780 return false;
1781 }
1782 }
1783
1784 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1785 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001786 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001787 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 LOG(LS_ERROR) << "Failed to create data channel.";
1789 return false;
1790 }
1791 }
1792
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001793 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1794 if (voice_channel()) {
1795 voice_channel()->ActivateRtcpMux();
1796 }
1797 if (video_channel()) {
1798 video_channel()->ActivateRtcpMux();
1799 }
1800 if (data_channel()) {
1801 data_channel()->ActivateRtcpMux();
1802 }
1803 }
1804
deadbeefcbecd352015-09-23 11:50:27 -07001805 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001806 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001807 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1808 cricket::GROUP_TYPE_BUNDLE);
1809 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001810 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1811 return false;
1812 }
deadbeefcbecd352015-09-23 11:50:27 -07001813 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001814 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1815 return false;
1816 }
1817 }
1818
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 return true;
1820}
1821
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001822bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001824 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001825 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001826 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001827 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001828 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001829
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001830 voice_channel_->SignalDtlsSetupFailure.connect(
1831 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001832
1833 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001834 voice_channel_->transport_channel()->SignalSentPacket.connect(
1835 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001836 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837}
1838
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001839bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001841 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001842 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001843 if (!video_channel_) {
1844 return false;
1845 }
1846
1847 video_channel_->SignalDtlsSetupFailure.connect(
1848 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001849
1850 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001851 video_channel_->transport_channel()->SignalSentPacket.connect(
1852 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001853 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854}
1855
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001856bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001857 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001859 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001860 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001861 return false;
1862 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001863
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001864 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001865 data_channel_->SignalDataReceived.connect(
1866 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001867 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001868
1869 data_channel_->SignalDtlsSetupFailure.connect(
1870 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001871
1872 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001873 data_channel_->transport_channel()->SignalSentPacket.connect(
1874 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001875 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876}
1877
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001878void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001879 SetError(ERROR_TRANSPORT,
1880 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881}
1882
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001883void WebRtcSession::OnDataChannelMessageReceived(
1884 cricket::DataChannel* channel,
1885 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001886 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001887 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1888 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1889 // Received OPEN message; parse and signal that a new data channel should
1890 // be created.
1891 std::string label;
1892 InternalDataChannelInit config;
1893 config.id = params.ssrc;
1894 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1895 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1896 << params.ssrc;
1897 return;
1898 }
1899 config.open_handshake_role = InternalDataChannelInit::kAcker;
1900 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901 }
deadbeefab9b2d12015-10-14 11:33:11 -07001902 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903}
1904
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001905// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001906bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001907 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1908 if (!bundle_enabled)
1909 return true;
1910
1911 const cricket::ContentGroup* bundle_group =
1912 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1913 ASSERT(bundle_group != NULL);
1914
1915 const cricket::ContentInfos& contents = desc->contents();
1916 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1917 citer != contents.end(); ++citer) {
1918 const cricket::ContentInfo* content = (&*citer);
1919 ASSERT(content != NULL);
1920 if (bundle_group->HasContentName(content->name) &&
1921 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1922 if (!HasRtcpMuxEnabled(content))
1923 return false;
1924 }
1925 }
1926 // RTCP-MUX is enabled in all the contents.
1927 return true;
1928}
1929
1930bool WebRtcSession::HasRtcpMuxEnabled(
1931 const cricket::ContentInfo* content) {
1932 const cricket::MediaContentDescription* description =
1933 static_cast<cricket::MediaContentDescription*>(content->description);
1934 return description->rtcp_mux();
1935}
1936
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001937bool WebRtcSession::ValidateSessionDescription(
1938 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001939 cricket::ContentSource source, std::string* err_desc) {
1940 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001941 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001942 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001943 }
1944
1945 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001946 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001947 }
1948
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001949 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001950 Action action = GetAction(sdesc->type());
1951 if (source == cricket::CS_LOCAL) {
1952 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001953 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954 } else {
1955 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001956 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001957 }
1958
1959 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001960 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001961 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1962 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001963 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001964 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001965 }
1966
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001967 // Verify ice-ufrag and ice-pwd.
1968 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001969 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001970 }
1971
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001972 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001973 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001974 }
1975
1976 // Verify m-lines in Answer when compared against Offer.
1977 if (action == kAnswer) {
1978 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001979 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1980 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001981 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001982 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001983 }
1984 }
1985
1986 return true;
1987}
1988
1989bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1990 return ((action == kOffer && state() == STATE_INIT) ||
1991 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001992 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001993 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001994 (action == kOffer && state() == STATE_INPROGRESS) ||
1995 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07001996 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
1997 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
1998 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
1999 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000}
2001
2002bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2003 return ((action == kOffer && state() == STATE_INIT) ||
2004 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002005 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002006 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002007 (action == kOffer && state() == STATE_INPROGRESS) ||
2008 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002009 (action == kAnswer && state() == STATE_SENTOFFER) ||
2010 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2011 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2012 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002013}
2014
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002015std::string WebRtcSession::GetSessionErrorMsg() {
2016 std::ostringstream desc;
2017 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2018 desc << kSessionErrorDesc << error_desc() << ".";
2019 return desc.str();
2020}
2021
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002022// We need to check the local/remote description for the Transport instead of
2023// the session, because a new Transport added during renegotiation may have
2024// them unset while the session has them set from the previous negotiation.
2025// Not doing so may trigger the auto generation of transport description and
2026// mess up DTLS identity information, ICE credential, etc.
2027bool WebRtcSession::ReadyToUseRemoteCandidate(
2028 const IceCandidateInterface* candidate,
2029 const SessionDescriptionInterface* remote_desc,
2030 bool* valid) {
2031 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002032
2033 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002034 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002035
deadbeefd59daf82015-10-14 15:02:44 -07002036 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002037 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002038 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002039
2040 size_t mediacontent_index =
2041 static_cast<size_t>(candidate->sdp_mline_index());
2042 size_t remote_content_size =
2043 current_remote_desc->description()->contents().size();
2044 if (mediacontent_index >= remote_content_size) {
2045 LOG(LS_ERROR)
2046 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2047
2048 *valid = false;
2049 return false;
2050 }
2051
2052 cricket::ContentInfo content =
2053 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002054 cricket::BaseChannel* channel = GetChannel(content.name);
2055 if (!channel) {
2056 return false;
2057 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002058
deadbeefd59daf82015-10-14 15:02:44 -07002059 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002060 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002061}
2062
deadbeefcbecd352015-09-23 11:50:27 -07002063void WebRtcSession::OnTransportControllerGatheringState(
2064 cricket::IceGatheringState state) {
2065 ASSERT(signaling_thread()->IsCurrent());
2066 if (state == cricket::kIceGatheringGathering) {
2067 if (ice_observer_) {
2068 ice_observer_->OnIceGatheringChange(
2069 PeerConnectionInterface::kIceGatheringGathering);
2070 }
2071 } else if (state == cricket::kIceGatheringComplete) {
2072 if (ice_observer_) {
2073 ice_observer_->OnIceGatheringChange(
2074 PeerConnectionInterface::kIceGatheringComplete);
2075 ice_observer_->OnIceComplete();
2076 }
2077 }
2078}
2079
2080void WebRtcSession::ReportTransportStats() {
2081 // Use a set so we don't report the same stats twice if two channels share
2082 // a transport.
2083 std::set<std::string> transport_names;
2084 if (voice_channel()) {
2085 transport_names.insert(voice_channel()->transport_name());
2086 }
2087 if (video_channel()) {
2088 transport_names.insert(video_channel()->transport_name());
2089 }
2090 if (data_channel()) {
2091 transport_names.insert(data_channel()->transport_name());
2092 }
2093 for (const auto& name : transport_names) {
2094 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002095 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002096 ReportBestConnectionState(stats);
2097 ReportNegotiatedCiphers(stats);
2098 }
2099 }
2100}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002101// Walk through the ConnectionInfos to gather best connection usage
2102// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002103void WebRtcSession::ReportBestConnectionState(
2104 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002105 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002106 for (cricket::TransportChannelStatsList::const_iterator it =
2107 stats.channel_stats.begin();
2108 it != stats.channel_stats.end(); ++it) {
2109 for (cricket::ConnectionInfos::const_iterator it_info =
2110 it->connection_infos.begin();
2111 it_info != it->connection_infos.end(); ++it_info) {
2112 if (!it_info->best_connection) {
2113 continue;
2114 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002115
2116 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2117 const cricket::Candidate& local = it_info->local_candidate;
2118 const cricket::Candidate& remote = it_info->remote_candidate;
2119
2120 // Increment the counter for IceCandidatePairType.
2121 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2122 (local.type() == RELAY_PORT_TYPE &&
2123 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2124 type = kEnumCounterIceCandidatePairTypeTcp;
2125 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2126 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002127 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002128 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002129 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002130 metrics_observer_->IncrementEnumCounter(
2131 type, GetIceCandidatePairCounter(local, remote),
2132 kIceCandidatePairMax);
2133
2134 // Increment the counter for IP type.
2135 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002136 metrics_observer_->IncrementEnumCounter(
2137 kEnumCounterAddressFamily, kBestConnections_IPv4,
2138 kPeerConnectionAddressFamilyCounter_Max);
2139
2140 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002141 metrics_observer_->IncrementEnumCounter(
2142 kEnumCounterAddressFamily, kBestConnections_IPv6,
2143 kPeerConnectionAddressFamilyCounter_Max);
2144 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002145 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002146 }
2147
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002148 return;
2149 }
2150 }
2151}
2152
jbauchac8869e2015-07-03 01:36:14 -07002153void WebRtcSession::ReportNegotiatedCiphers(
2154 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002155 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002156 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2157 return;
2158 }
2159
guoweis318166b2015-11-18 19:03:38 -08002160 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
2161 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
2162 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002163 return;
2164 }
2165
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002166 PeerConnectionEnumCounterType srtp_counter_type;
2167 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002168 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002169 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2170 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002171 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002172 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2173 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002174 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002175 srtp_counter_type = kEnumCounterDataSrtpCipher;
2176 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002177 } else {
2178 RTC_NOTREACHED();
2179 return;
2180 }
2181
guoweis318166b2015-11-18 19:03:38 -08002182 if (!srtp_cipher.empty()) {
2183 metrics_observer_->IncrementSparseEnumCounter(
2184 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002185 }
guoweis318166b2015-11-18 19:03:38 -08002186 if (ssl_cipher) {
2187 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002188 }
2189}
2190
stefanc1aeaf02015-10-15 07:26:07 -07002191void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2192 const rtc::SentPacket& sent_packet) {
2193 RTC_DCHECK(worker_thread()->IsCurrent());
2194 media_controller_->call_w()->OnSentPacket(sent_packet);
2195}
2196
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197} // namespace webrtc