blob: 95abeab77a9c9e6dc6e1a1033b083d9f41535e7a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
deadbeefcbecd352015-09-23 11:50:27 -070034#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/app/webrtc/mediaconstraintsinterface.h"
39#include "talk/app/webrtc/mediastreamsignaling.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070041#include "talk/app/webrtc/sctputils.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000042#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "talk/media/base/constants.h"
44#include "talk/media/base/videocapturer.h"
45#include "talk/session/media/channel.h"
46#include "talk/session/media/channelmanager.h"
47#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070049#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070054#include "webrtc/call.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000055#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070056#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58using cricket::ContentInfo;
59using cricket::ContentInfos;
60using cricket::MediaContentDescription;
61using cricket::SessionDescription;
62using cricket::TransportInfo;
63
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070064using cricket::LOCAL_PORT_TYPE;
65using cricket::STUN_PORT_TYPE;
66using cricket::RELAY_PORT_TYPE;
67using cricket::PRFLX_PORT_TYPE;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069namespace webrtc {
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
73 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000074const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075const char kInvalidCandidates[] = "Description contains invalid candidates.";
76const char kInvalidSdp[] = "Invalid session description.";
77const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
79const char kPushDownTDFailed[] =
80 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000081const char kSdpWithoutDtlsFingerprint[] =
82 "Called with SDP without DTLS fingerprint.";
83const char kSdpWithoutSdesCrypto[] =
84 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000085const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000086 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000088const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000089const char kDtlsSetupFailureRtp[] =
90 "Couldn't set up DTLS-SRTP on RTP channel.";
91const char kDtlsSetupFailureRtcp[] =
92 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070093const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000094const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070096IceCandidatePairType GetIceCandidatePairCounter(
97 const cricket::Candidate& local,
98 const cricket::Candidate& remote) {
99 const auto& l = local.type();
100 const auto& r = remote.type();
101 const auto& host = LOCAL_PORT_TYPE;
102 const auto& srflx = STUN_PORT_TYPE;
103 const auto& relay = RELAY_PORT_TYPE;
104 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700105 if (l == host && r == host) {
106 bool local_private = IPIsPrivate(local.address().ipaddr());
107 bool remote_private = IPIsPrivate(remote.address().ipaddr());
108 if (local_private) {
109 if (remote_private) {
110 return kIceCandidatePairHostPrivateHostPrivate;
111 } else {
112 return kIceCandidatePairHostPrivateHostPublic;
113 }
114 } else {
115 if (remote_private) {
116 return kIceCandidatePairHostPublicHostPrivate;
117 } else {
118 return kIceCandidatePairHostPublicHostPublic;
119 }
120 }
121 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700122 if (l == host && r == srflx)
123 return kIceCandidatePairHostSrflx;
124 if (l == host && r == relay)
125 return kIceCandidatePairHostRelay;
126 if (l == host && r == prflx)
127 return kIceCandidatePairHostPrflx;
128 if (l == srflx && r == host)
129 return kIceCandidatePairSrflxHost;
130 if (l == srflx && r == srflx)
131 return kIceCandidatePairSrflxSrflx;
132 if (l == srflx && r == relay)
133 return kIceCandidatePairSrflxRelay;
134 if (l == srflx && r == prflx)
135 return kIceCandidatePairSrflxPrflx;
136 if (l == relay && r == host)
137 return kIceCandidatePairRelayHost;
138 if (l == relay && r == srflx)
139 return kIceCandidatePairRelaySrflx;
140 if (l == relay && r == relay)
141 return kIceCandidatePairRelayRelay;
142 if (l == relay && r == prflx)
143 return kIceCandidatePairRelayPrflx;
144 if (l == prflx && r == host)
145 return kIceCandidatePairPrflxHost;
146 if (l == prflx && r == srflx)
147 return kIceCandidatePairPrflxSrflx;
148 if (l == prflx && r == relay)
149 return kIceCandidatePairPrflxRelay;
150 return kIceCandidatePairMax;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153// Compares |answer| against |offer|. Comparision is done
154// for number of m-lines in answer against offer. If matches true will be
155// returned otherwise false.
156static bool VerifyMediaDescriptions(
157 const SessionDescription* answer, const SessionDescription* offer) {
158 if (offer->contents().size() != answer->contents().size())
159 return false;
160
161 for (size_t i = 0; i < offer->contents().size(); ++i) {
162 if ((offer->contents()[i].name) != answer->contents()[i].name) {
163 return false;
164 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000165 const MediaContentDescription* offer_mdesc =
166 static_cast<const MediaContentDescription*>(
167 offer->contents()[i].description);
168 const MediaContentDescription* answer_mdesc =
169 static_cast<const MediaContentDescription*>(
170 answer->contents()[i].description);
171 if (offer_mdesc->type() != answer_mdesc->type()) {
172 return false;
173 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 }
175 return true;
176}
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178// Checks that each non-rejected content has SDES crypto keys or a DTLS
179// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
180// keys, will be caught in Transport negotiation, and backstopped by Channel's
181// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000182static bool VerifyCrypto(const SessionDescription* desc,
183 bool dtls_enabled,
184 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 const ContentInfos& contents = desc->contents();
186 for (size_t index = 0; index < contents.size(); ++index) {
187 const ContentInfo* cinfo = &contents[index];
188 if (cinfo->rejected) {
189 continue;
190 }
191
192 // If the content isn't rejected, crypto must be present.
193 const MediaContentDescription* media =
194 static_cast<const MediaContentDescription*>(cinfo->description);
195 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
196 if (!media || !tinfo) {
197 // Something is not right.
198 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000199 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 return false;
201 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000202 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000203 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000204 LOG(LS_WARNING) <<
205 "Session description must have DTLS fingerprint if DTLS enabled.";
206 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000207 return false;
208 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000209 } else {
210 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000211 LOG(LS_WARNING) <<
212 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000213 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000214 return false;
215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 }
217 }
218
219 return true;
220}
221
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000222// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
223static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
224 const ContentInfos& contents = desc->contents();
225 for (size_t index = 0; index < contents.size(); ++index) {
226 const ContentInfo* cinfo = &contents[index];
227 if (cinfo->rejected) {
228 continue;
229 }
230
231 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
232 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
233 if (!tinfo) {
234 // Something is not right.
235 LOG(LS_ERROR) << kInvalidSdp;
236 return false;
237 }
238 if (tinfo->description.ice_ufrag.empty() ||
239 tinfo->description.ice_pwd.empty()) {
240 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
241 return false;
242 }
243 }
244 return true;
245}
246
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247// Forces |sdesc->crypto_required| to the appropriate state based on the
248// current security policy, to ensure a failure occurs if there is an error
249// in crypto negotiation.
250// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000251static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
252 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000253 if (!sdesc) {
254 return;
255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 // Updating the |crypto_required_| in MediaContentDescription to the
258 // appropriate state based on the current security policy.
259 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
260 iter != sdesc->contents().end(); ++iter) {
261 if (cricket::IsMediaContent(&*iter)) {
262 MediaContentDescription* mdesc =
263 static_cast<MediaContentDescription*> (iter->description);
264 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000265 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000266 }
267 }
268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
272 const std::string& track_id,
273 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 const cricket::ContentInfo* audio_info =
275 cricket::GetFirstAudioContent(session_description);
276 if (!audio_info) {
277 LOG(LS_ERROR) << "Audio not used in this call";
278 return false;
279 }
280
281 const cricket::MediaContentDescription* audio_content =
282 static_cast<const cricket::MediaContentDescription*>(
283 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 const cricket::StreamParams* stream =
285 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
286 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 return false;
288 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000289
290 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return true;
292}
293
294static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200295 uint32_t ssrc,
296 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 ASSERT(track_id != NULL);
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const cricket::ContentInfo* audio_info =
300 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000301 if (audio_info) {
302 const cricket::MediaContentDescription* audio_content =
303 static_cast<const cricket::MediaContentDescription*>(
304 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000306 const auto* found =
307 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
308 if (found) {
309 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000310 return true;
311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
314 const cricket::ContentInfo* video_info =
315 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000316 if (video_info) {
317 const cricket::MediaContentDescription* video_content =
318 static_cast<const cricket::MediaContentDescription*>(
319 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000321 const auto* found =
322 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
323 if (found) {
324 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000325 return true;
326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328 return false;
329}
330
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000331static bool BadSdp(const std::string& source,
332 const std::string& type,
333 const std::string& reason,
334 std::string* err_desc) {
335 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700336 desc << "Failed to set " << source;
337 if (!type.empty()) {
338 desc << " " << type;
339 }
340 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000343 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000345 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 return false;
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350 const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000354 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000356 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358}
359
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360static bool BadLocalSdp(const std::string& type,
361 const std::string& reason,
362 std::string* err_desc) {
363 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
364}
365
366static bool BadRemoteSdp(const std::string& type,
367 const std::string& reason,
368 std::string* err_desc) {
369 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
370}
371
372static bool BadOfferSdp(cricket::ContentSource source,
373 const std::string& reason,
374 std::string* err_desc) {
375 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
376}
377
378static bool BadPranswerSdp(cricket::ContentSource source,
379 const std::string& reason,
380 std::string* err_desc) {
381 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
382 reason, err_desc);
383}
384
385static bool BadAnswerSdp(cricket::ContentSource source,
386 const std::string& reason,
387 std::string* err_desc) {
388 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389}
390
deadbeefd59daf82015-10-14 15:02:44 -0700391#define GET_STRING_OF_STATE(state) \
392 case webrtc::WebRtcSession::state: \
393 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 break;
395
deadbeefd59daf82015-10-14 15:02:44 -0700396static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 std::string result;
398 switch (state) {
399 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700400 GET_STRING_OF_STATE(STATE_SENTOFFER)
401 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
402 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
403 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700405 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 default:
407 ASSERT(false);
408 break;
409 }
410 return result;
411}
412
deadbeefd59daf82015-10-14 15:02:44 -0700413#define GET_STRING_OF_ERROR_CODE(err) \
414 case webrtc::WebRtcSession::err: \
415 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 break;
417
deadbeefd59daf82015-10-14 15:02:44 -0700418static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 std::string result;
420 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000421 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
423 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 default:
deadbeefd59daf82015-10-14 15:02:44 -0700425 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 break;
427 }
428 return result;
429}
430
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431static std::string MakeErrorString(const std::string& error,
432 const std::string& desc) {
433 std::ostringstream ret;
434 ret << error << " " << desc;
435 return ret.str();
436}
437
438static std::string MakeTdErrorString(const std::string& desc) {
439 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000442// Set |option| to the highest-priority value of |key| in the optional
443// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000444template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000445static void SetOptionFromOptionalConstraint(
446 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000447 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000448 if (!constraints) {
449 return;
450 }
451 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000452 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000453 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000455 option->Set(value);
456 }
457 }
458}
459
Peter Boström0c4e06b2015-10-07 12:23:21 +0200460uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000461 PeerConnectionInterface::IceTransportsType type) {
462 switch (type) {
463 case PeerConnectionInterface::kNone:
464 return cricket::CF_NONE;
465 case PeerConnectionInterface::kRelay:
466 return cricket::CF_RELAY;
467 case PeerConnectionInterface::kNoHost:
468 return (cricket::CF_ALL & ~cricket::CF_HOST);
469 case PeerConnectionInterface::kAll:
470 return cricket::CF_ALL;
471 default: ASSERT(false);
472 }
473 return cricket::CF_NONE;
474}
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476// Help class used to remember if a a remote peer has requested ice restart by
477// by sending a description with new ice ufrag and password.
478class IceRestartAnswerLatch {
479 public:
480 IceRestartAnswerLatch() : ice_restart_(false) { }
481
wu@webrtc.org91053e72013-08-10 07:18:04 +0000482 // Returns true if CheckForRemoteIceRestart has been called with a new session
483 // description where ice password and ufrag has changed since last time
484 // Reset() was called.
485 bool Get() const {
486 return ice_restart_;
487 }
488
489 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 if (ice_restart_) {
491 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 }
494
honghaiz503726c2015-07-31 12:37:38 -0700495 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
496 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700498 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500 const SessionDescription* new_sd = new_desc->description();
501 const SessionDescription* old_sd = old_desc->description();
502 const ContentInfos& contents = new_sd->contents();
503 for (size_t index = 0; index < contents.size(); ++index) {
504 const ContentInfo* cinfo = &contents[index];
505 if (cinfo->rejected) {
506 continue;
507 }
508 // If the content isn't rejected, check if ufrag and password has
509 // changed.
510 const cricket::TransportDescription* new_transport_desc =
511 new_sd->GetTransportDescriptionByName(cinfo->name);
512 const cricket::TransportDescription* old_transport_desc =
513 old_sd->GetTransportDescriptionByName(cinfo->name);
514 if (!new_transport_desc || !old_transport_desc) {
515 // No transport description exist. This is not an ice restart.
516 continue;
517 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000518 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
519 old_transport_desc->ice_pwd,
520 new_transport_desc->ice_ufrag,
521 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 LOG(LS_INFO) << "Remote peer request ice restart.";
523 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700524 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
526 }
honghaiz503726c2015-07-31 12:37:38 -0700527 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 }
529
530 private:
531 bool ice_restart_;
532};
533
stefanc1aeaf02015-10-15 07:26:07 -0700534WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700535 rtc::Thread* signaling_thread,
536 rtc::Thread* worker_thread,
537 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700538 : signaling_thread_(signaling_thread),
539 worker_thread_(worker_thread),
540 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // RFC 3264: The numeric value of the session id and version in the
542 // o line MUST be representable with a "64 bit signed integer".
543 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700544 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
545 transport_controller_(new cricket::TransportController(signaling_thread,
546 worker_thread,
547 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700548 media_controller_(media_controller),
549 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 ice_observer_(NULL),
551 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700552 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000554 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000556 ice_restart_latch_(new IceRestartAnswerLatch),
557 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700558 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
559 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700560 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700561 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700562 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700563 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700564 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700565 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700566 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567}
568
569WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700570 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000571 // Destroy video_channel_ first since it may have a pointer to the
572 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000573 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SignalVideoChannelDestroyed();
575 channel_manager_->DestroyVideoChannel(video_channel_.release());
576 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000577 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000578 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200579 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000580 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000581 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 SignalDataChannelDestroyed();
583 channel_manager_->DestroyDataChannel(data_channel_.release());
584 }
deadbeefd59daf82015-10-14 15:02:44 -0700585
586 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587}
588
wu@webrtc.org91053e72013-08-10 07:18:04 +0000589bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000590 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700591 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200592 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200593 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
594 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700595 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
deadbeefd59daf82015-10-14 15:02:44 -0700596 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000597
Henrik Boström87713d02015-08-25 09:53:21 +0200598 // Obtain a certificate from RTCConfiguration if any were provided (optional).
599 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
600 if (!rtc_configuration.certificates.empty()) {
601 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
602 // just picking the first one. The decision should be made based on the DTLS
603 // handshake. The DTLS negotiations need to know about all certificates.
604 certificate = rtc_configuration.certificates[0];
605 }
606
honghaiz1f429e32015-09-28 07:57:34 -0700607 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // TODO(perkj): Take |constraints| into consideration. Return false if not all
610 // mandatory constraints can be fulfilled. Note that |constraints|
611 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000613
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000614 if (options.disable_encryption) {
615 dtls_enabled_ = false;
616 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200617 // Enable DTLS by default if we have an identity store or a certificate.
618 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000619 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700620 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
621 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000622 dtls_enabled_ = value;
623 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000627 // It takes precendence over the disable_sctp_data_channels
628 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 if (FindConstraint(
630 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
631 &value, NULL) && value) {
632 LOG(LS_INFO) << "Allowing RTP data engine.";
633 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000634 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000636 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000637 LOG(LS_INFO) << "Allowing SCTP data engine.";
638 data_channel_type_ = cricket::DCT_SCTP;
639 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641
wu@webrtc.orgde305012013-10-31 15:40:38 +0000642 // Find DSCP constraint.
643 if (FindConstraint(
644 constraints,
645 MediaConstraintsInterface::kEnableDscp,
646 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000647 audio_options_.dscp.Set(value);
648 video_options_.dscp.Set(value);
649 }
650
651 // Find Suspend Below Min Bitrate constraint.
652 if (FindConstraint(
653 constraints,
654 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
655 &value,
656 NULL)) {
657 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000658 }
659
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000660 SetOptionFromOptionalConstraint(constraints,
661 MediaConstraintsInterface::kScreencastMinBitrate,
662 &video_options_.screencast_min_bitrate);
663
664 // Find constraints for cpu overuse detection.
665 SetOptionFromOptionalConstraint(constraints,
666 MediaConstraintsInterface::kCpuUnderuseThreshold,
667 &video_options_.cpu_underuse_threshold);
668 SetOptionFromOptionalConstraint(constraints,
669 MediaConstraintsInterface::kCpuOveruseThreshold,
670 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000671 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000672 MediaConstraintsInterface::kCpuOveruseDetection,
673 &video_options_.cpu_overuse_detection);
674 SetOptionFromOptionalConstraint(constraints,
675 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
676 &video_options_.cpu_overuse_encode_usage);
677 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000678 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
679 &video_options_.cpu_underuse_encode_rsd_threshold);
680 SetOptionFromOptionalConstraint(constraints,
681 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
682 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000683
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000684 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000685 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
686 &video_options_.unsignalled_recv_stream_limit);
687 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
688 int stream_limit;
689 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000690 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
691 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000692 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
693 }
694
695 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000696 MediaConstraintsInterface::kHighStartBitrate,
697 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000698
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000699 SetOptionFromOptionalConstraint(constraints,
700 MediaConstraintsInterface::kCombinedAudioVideoBwe,
701 &audio_options_.combined_audio_video_bwe);
702
Henrik Lundin64dad832015-05-11 12:44:23 +0200703 audio_options_.audio_jitter_buffer_max_packets.Set(
704 rtc_configuration.audio_jitter_buffer_max_packets);
705
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200706 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
707 rtc_configuration.audio_jitter_buffer_fast_accelerate);
708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 const cricket::VideoCodec default_codec(
710 JsepSessionDescription::kDefaultVideoCodecId,
711 JsepSessionDescription::kDefaultVideoCodecName,
712 JsepSessionDescription::kMaxVideoCodecWidth,
713 JsepSessionDescription::kMaxVideoCodecHeight,
714 JsepSessionDescription::kDefaultVideoCodecFramerate,
715 JsepSessionDescription::kDefaultVideoCodecPreference);
716 channel_manager_->SetDefaultVideoEncoderConfig(
717 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000718
Henrik Boström87713d02015-08-25 09:53:21 +0200719 if (!dtls_enabled_) {
720 // Construct with DTLS disabled.
721 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700722 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200723 } else {
724 // Construct with DTLS enabled.
725 if (!certificate) {
726 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700727 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200728 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700729 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
730 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200731 } else {
732 // Use the already generated certificate.
733 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700734 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200735 }
736 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000737
Henrik Boströmd8281982015-08-27 10:12:24 +0200738 webrtc_session_desc_factory_->SignalCertificateReady.connect(
739 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000740
wu@webrtc.org97077a32013-10-25 21:18:33 +0000741 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000742 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000743 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000744 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200745 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700746
747 if (rtc_configuration.enable_localhost_ice_candidate) {
748 port_allocator()->set_flags(
749 port_allocator()->flags() |
750 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
751 }
752
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return true;
754}
755
deadbeefd59daf82015-10-14 15:02:44 -0700756void WebRtcSession::Close() {
757 SetState(STATE_CLOSED);
758 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000759 ASSERT(!voice_channel_);
760 ASSERT(!video_channel_);
761 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762}
763
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000764void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
765 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766}
767
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000768cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
769 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770}
771
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700773 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000774 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
775 << "SSL Role of the session.";
776 return false;
777 }
778
deadbeefd59daf82015-10-14 15:02:44 -0700779 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000780}
781
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000782void WebRtcSession::CreateOffer(
783 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700784 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
785 const cricket::MediaSessionOptions& session_options) {
786 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000787}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788
deadbeefab9b2d12015-10-14 11:33:11 -0700789void WebRtcSession::CreateAnswer(
790 CreateSessionDescriptionObserver* observer,
791 const MediaConstraintsInterface* constraints,
792 const cricket::MediaSessionOptions& session_options) {
793 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
794 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795}
796
797bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
798 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700799 ASSERT(signaling_thread()->IsCurrent());
800
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000801 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000802 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000803
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000804 // Validate SDP.
805 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
806 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 }
808
deadbeefd59daf82015-10-14 15:02:44 -0700809 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000810 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700812 initial_offerer_ = true;
813 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 }
815
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000816 cricket::SecurePolicy sdes_policy =
817 webrtc_session_desc_factory_->SdesPolicy();
818 cricket::CryptoType crypto_required = dtls_enabled_ ?
819 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
820 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000822 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000824 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825
826 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000827 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 // TODO(mallinath) - Handle CreateChannel failure, as new local description
829 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000830 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
832
deadbeefcbecd352015-09-23 11:50:27 -0700833 // Remove unused channels if MediaContentDescription is rejected.
834 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000836 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 return false;
838 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000839
deadbeefd59daf82015-10-14 15:02:44 -0700840 if (remote_desc_) {
841 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700842 UseCandidatesInSessionDescription(remote_desc_.get());
843 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844
deadbeefd59daf82015-10-14 15:02:44 -0700845 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000846 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 }
848 return true;
849}
850
851bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
852 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700853 ASSERT(signaling_thread()->IsCurrent());
854
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000855 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000857
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000858 // Validate SDP.
859 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
860 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 }
862
deadbeefd59daf82015-10-14 15:02:44 -0700863 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
864 remote_desc_.release());
865 remote_desc_.reset(desc_temp.release());
866
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000868 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 if (action == kOffer && !CreateChannels(desc->description())) {
870 // TODO(mallinath) - Handle CreateChannel failure, as new local description
871 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000872 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 }
874
deadbeefcbecd352015-09-23 11:50:27 -0700875 // Remove unused channels if MediaContentDescription is rejected.
876 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877
878 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
879 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000880 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 return false;
882 }
883
deadbeefd59daf82015-10-14 15:02:44 -0700884 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000885 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
887
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 // Check if this new SessionDescription contains new ice ufrag and password
889 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700890 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700891 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700892 // We retain all received candidates only if ICE is not restarted.
893 // When ICE is restarted, all previous candidates belong to an old generation
894 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700895 // TODO(deadbeef): This goes against the W3C spec which says the remote
896 // description should only contain candidates from the last set remote
897 // description plus any candidates added since then. We should remove this
898 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700899 if (!ice_restart) {
900 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700901 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700902 }
903
deadbeefd59daf82015-10-14 15:02:44 -0700904 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000905 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000907
908 // Set the the ICE connection state to connecting since the connection may
909 // become writable with peer reflexive candidates before any remote candidate
910 // is signaled.
911 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
912 // is to have a new signal the indicates a change in checking state from the
913 // transport and expose a new checking() member from transport that can be
914 // read to determine the current checking state. The existing SignalConnecting
915 // actually means "gathering candidates", so cannot be be used here.
916 if (desc->type() != SessionDescriptionInterface::kOffer &&
917 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
918 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
919 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 return true;
921}
922
deadbeefd59daf82015-10-14 15:02:44 -0700923void WebRtcSession::LogState(State old_state, State new_state) {
924 LOG(LS_INFO) << "Session:" << id()
925 << " Old state:" << GetStateString(old_state)
926 << " New state:" << GetStateString(new_state);
927}
928
929void WebRtcSession::SetState(State state) {
930 ASSERT(signaling_thread_->IsCurrent());
931 if (state != state_) {
932 LogState(state_, state);
933 state_ = state;
934 SignalState(this, state_);
935 }
936}
937
938void WebRtcSession::SetError(Error error, const std::string& error_desc) {
939 ASSERT(signaling_thread_->IsCurrent());
940 if (error != error_) {
941 error_ = error;
942 error_desc_ = error_desc;
943 }
944}
945
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946bool WebRtcSession::UpdateSessionState(
947 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700949 ASSERT(signaling_thread()->IsCurrent());
950
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 // If there's already a pending error then no state transition should happen.
952 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700953 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000954 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000956 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
957 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 }
deadbeefd59daf82015-10-14 15:02:44 -0700959 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
960 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000961 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700962 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000963 }
deadbeefd59daf82015-10-14 15:02:44 -0700964 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000965 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 }
967 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000968 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
969 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 }
971 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700972 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
973 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000974 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700975 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000976 }
deadbeefd59daf82015-10-14 15:02:44 -0700977 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 }
980 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
982 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 }
deadbeefcbecd352015-09-23 11:50:27 -0700984 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700985 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700986 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700987 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700988 if (local_bundle && remote_bundle) {
989 // The answerer decides the transport to bundle on
990 const cricket::ContentGroup* answer_bundle =
991 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
992 if (!EnableBundle(*answer_bundle)) {
993 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
994 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
995 }
996 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700998 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000999 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -07001000 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001001 }
deadbeefd59daf82015-10-14 15:02:44 -07001002 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001003 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 }
1005 }
1006 return true;
1007}
1008
1009WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1010 if (type == SessionDescriptionInterface::kOffer) {
1011 return WebRtcSession::kOffer;
1012 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1013 return WebRtcSession::kPrAnswer;
1014 } else if (type == SessionDescriptionInterface::kAnswer) {
1015 return WebRtcSession::kAnswer;
1016 }
1017 ASSERT(false && "unknown action type");
1018 return WebRtcSession::kOffer;
1019}
1020
deadbeefd59daf82015-10-14 15:02:44 -07001021bool WebRtcSession::PushdownMediaDescription(
1022 cricket::ContentAction action,
1023 cricket::ContentSource source,
1024 std::string* err) {
1025 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1026 if (!ch) {
1027 return true;
1028 } else if (source == cricket::CS_LOCAL) {
1029 return ch->PushdownLocalDescription(local_desc_->description(), action,
1030 err);
1031 } else {
1032 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1033 err);
1034 }
1035 };
1036
1037 return (set_content(voice_channel()) &&
1038 set_content(video_channel()) &&
1039 set_content(data_channel()));
1040}
1041
1042bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1043 cricket::ContentAction action,
1044 std::string* error_desc) {
1045 RTC_DCHECK(signaling_thread()->IsCurrent());
1046
1047 if (source == cricket::CS_LOCAL) {
1048 return PushdownLocalTransportDescription(local_desc_->description(), action,
1049 error_desc);
1050 }
1051 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1052 error_desc);
1053}
1054
1055bool WebRtcSession::PushdownLocalTransportDescription(
1056 const SessionDescription* sdesc,
1057 cricket::ContentAction action,
1058 std::string* err) {
1059 RTC_DCHECK(signaling_thread()->IsCurrent());
1060
1061 if (!sdesc) {
1062 return false;
1063 }
1064
1065 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1066 if (!transport_controller_->SetLocalTransportDescription(
1067 tinfo.content_name, tinfo.description, action, err)) {
1068 return false;
1069 }
1070 }
1071
1072 return true;
1073}
1074
1075bool WebRtcSession::PushdownRemoteTransportDescription(
1076 const SessionDescription* sdesc,
1077 cricket::ContentAction action,
1078 std::string* err) {
1079 RTC_DCHECK(signaling_thread()->IsCurrent());
1080
1081 if (!sdesc) {
1082 return false;
1083 }
1084
1085 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1086 if (!transport_controller_->SetRemoteTransportDescription(
1087 tinfo.content_name, tinfo.description, action, err)) {
1088 return false;
1089 }
1090 }
1091
1092 return true;
1093}
1094
1095bool WebRtcSession::GetTransportDescription(
1096 const SessionDescription* description,
1097 const std::string& content_name,
1098 cricket::TransportDescription* tdesc) {
1099 if (!description || !tdesc) {
1100 return false;
1101 }
1102 const TransportInfo* transport_info =
1103 description->GetTransportInfoByName(content_name);
1104 if (!transport_info) {
1105 return false;
1106 }
1107 *tdesc = transport_info->description;
1108 return true;
1109}
1110
1111bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001112 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001113 return (GetChannelTransportStats(voice_channel(), stats) &&
1114 GetChannelTransportStats(video_channel(), stats) &&
1115 GetChannelTransportStats(data_channel(), stats));
1116}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001117
deadbeefcbecd352015-09-23 11:50:27 -07001118bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001119 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001120 ASSERT(signaling_thread()->IsCurrent());
1121 if (!ch) {
1122 // Not using this channel.
1123 return true;
1124 }
1125
1126 const std::string& content_name = ch->content_name();
1127 const std::string& transport_name = ch->transport_name();
1128 stats->proxy_to_transport[content_name] = transport_name;
1129 if (stats->transport_stats.find(transport_name) !=
1130 stats->transport_stats.end()) {
1131 // Transport stats already done for this transport.
1132 return true;
1133 }
1134
1135 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001136 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001137 return false;
1138 }
1139
1140 stats->transport_stats[transport_name] = tstats;
1141 return true;
1142}
1143
1144bool WebRtcSession::GetLocalCertificate(
1145 const std::string& transport_name,
1146 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1147 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001148 return transport_controller_->GetLocalCertificate(transport_name,
1149 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001150}
1151
1152bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1153 rtc::SSLCertificate** cert) {
1154 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001155 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001156}
1157
1158cricket::BaseChannel* WebRtcSession::GetChannel(
1159 const std::string& content_name) {
1160 if (voice_channel() && voice_channel()->content_name() == content_name) {
1161 return voice_channel();
1162 }
1163 if (video_channel() && video_channel()->content_name() == content_name) {
1164 return video_channel();
1165 }
1166 if (data_channel() && data_channel()->content_name() == content_name) {
1167 return data_channel();
1168 }
1169 return nullptr;
1170}
1171
1172bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1173 const std::string* first_content_name = bundle.FirstContentName();
1174 if (!first_content_name) {
1175 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1176 return false;
1177 }
1178 const std::string& transport_name = *first_content_name;
1179 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1180
1181 auto maybe_set_transport = [this, bundle, transport_name,
1182 first_channel](cricket::BaseChannel* ch) {
1183 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001184 return true;
1185 }
1186
deadbeefcbecd352015-09-23 11:50:27 -07001187 if (ch->transport_name() == transport_name) {
1188 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1189 << " on " << transport_name << ".";
1190 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001191 }
torbjornga81a42f2015-09-23 02:16:58 -07001192
deadbeefcbecd352015-09-23 11:50:27 -07001193 if (!ch->SetTransport(transport_name)) {
1194 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1195 return false;
1196 }
1197 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1198 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001199 return true;
1200 };
1201
deadbeefcbecd352015-09-23 11:50:27 -07001202 if (!maybe_set_transport(voice_channel()) ||
1203 !maybe_set_transport(video_channel()) ||
1204 !maybe_set_transport(data_channel())) {
1205 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001206 }
deadbeefcbecd352015-09-23 11:50:27 -07001207
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001208 return true;
1209}
1210
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001212 if (!remote_desc_) {
1213 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1214 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 return false;
1216 }
1217
1218 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001219 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 return false;
1221 }
1222
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001223 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001224 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1225 if (!valid) {
1226 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 }
1228
1229 // Add this candidate to the remote session description.
1230 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001231 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 return false;
1233 }
1234
deadbeefd59daf82015-10-14 15:02:44 -07001235 if (ready) {
1236 return UseCandidate(candidate);
1237 } else {
1238 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1239 return true;
1240 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241}
1242
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001243bool WebRtcSession::SetIceTransports(
1244 PeerConnectionInterface::IceTransportsType type) {
1245 return port_allocator()->set_candidate_filter(
1246 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001247}
1248
deadbeefd59daf82015-10-14 15:02:44 -07001249cricket::IceConfig WebRtcSession::ParseIceConfig(
1250 const PeerConnectionInterface::RTCConfiguration& config) const {
1251 cricket::IceConfig ice_config;
1252 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
1253 ice_config.gather_continually = (config.continual_gathering_policy ==
1254 PeerConnectionInterface::GATHER_CONTINUALLY);
1255 return ice_config;
1256}
1257
1258void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1259 transport_controller_->SetIceConfig(config);
1260}
1261
1262void WebRtcSession::MaybeStartGathering() {
1263 transport_controller_->MaybeStartGathering();
1264}
1265
Peter Boström0c4e06b2015-10-07 12:23:21 +02001266bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1267 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001268 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001270 }
1271 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272}
1273
Peter Boström0c4e06b2015-10-07 12:23:21 +02001274bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1275 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001276 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001277 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001278 }
1279 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280}
1281
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001282std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001284 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 return desc.str();
1286}
1287
solenbergd4cec0d2015-10-09 08:55:48 -07001288void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 ASSERT(signaling_thread()->IsCurrent());
1290 if (!voice_channel_) {
1291 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1292 return;
1293 }
solenberg4bac9c52015-10-09 02:32:53 -07001294 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1295 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 // otherwise. This in the normal case when the underlying media channel has
1297 // already been deleted.
1298 ASSERT(enable == false);
1299 }
1300}
1301
Peter Boström0c4e06b2015-10-07 12:23:21 +02001302void WebRtcSession::SetAudioSend(uint32_t ssrc,
1303 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001304 const cricket::AudioOptions& options,
1305 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 ASSERT(signaling_thread()->IsCurrent());
1307 if (!voice_channel_) {
1308 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1309 return;
1310 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001311 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001312 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314}
1315
Peter Boström0c4e06b2015-10-07 12:23:21 +02001316void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001317 ASSERT(signaling_thread()->IsCurrent());
1318 ASSERT(volume >= 0 && volume <= 10);
1319 if (!voice_channel_) {
1320 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1321 return;
1322 }
1323
solenberg4bac9c52015-10-09 02:32:53 -07001324 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001325 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001326 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001327}
1328
Peter Boström0c4e06b2015-10-07 12:23:21 +02001329bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 cricket::VideoCapturer* camera) {
1331 ASSERT(signaling_thread()->IsCurrent());
1332
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001333 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1335 // support video.
1336 LOG(LS_WARNING) << "Video not used in this call.";
1337 return false;
1338 }
1339 if (!video_channel_->SetCapturer(ssrc, camera)) {
1340 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1341 // This in the normal case when the underlying media channel has already
1342 // been deleted.
1343 ASSERT(camera == NULL);
1344 return false;
1345 }
1346 return true;
1347}
1348
Peter Boström0c4e06b2015-10-07 12:23:21 +02001349void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 bool enable,
1351 cricket::VideoRenderer* renderer) {
1352 ASSERT(signaling_thread()->IsCurrent());
1353 if (!video_channel_) {
1354 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1355 return;
1356 }
1357 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1358 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1359 // This in the normal case when the underlying media channel has already
1360 // been deleted.
1361 ASSERT(renderer == NULL);
1362 }
1363}
1364
Peter Boström0c4e06b2015-10-07 12:23:21 +02001365void WebRtcSession::SetVideoSend(uint32_t ssrc,
1366 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 const cricket::VideoOptions* options) {
1368 ASSERT(signaling_thread()->IsCurrent());
1369 if (!video_channel_) {
1370 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1371 return;
1372 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001373 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1375 // This in the normal case when the underlying media channel has already
1376 // been deleted.
1377 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379}
1380
1381bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1382 ASSERT(signaling_thread()->IsCurrent());
1383 if (!voice_channel_) {
1384 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1385 return false;
1386 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001387 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1389 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001390 if (!local_desc_ ||
1391 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 &send_ssrc)) {
1393 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1394 return false;
1395 }
1396 return voice_channel_->CanInsertDtmf();
1397}
1398
1399bool WebRtcSession::InsertDtmf(const std::string& track_id,
1400 int code, int duration) {
1401 ASSERT(signaling_thread()->IsCurrent());
1402 if (!voice_channel_) {
1403 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1404 return false;
1405 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001406 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001407 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1408 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1410 return false;
1411 }
1412 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1413 cricket::DF_SEND)) {
1414 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1415 return false;
1416 }
1417 return true;
1418}
1419
1420sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1421 return &SignalVoiceChannelDestroyed;
1422}
1423
wu@webrtc.org78187522013-10-07 23:32:02 +00001424bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001425 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001426 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001427 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001428 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1429 return false;
1430 }
1431 return data_channel_->SendData(params, payload, result);
1432}
1433
1434bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001435 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001436 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1437 return false;
1438 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001439 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1440 &DataChannel::OnChannelReady);
1441 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1442 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001443 data_channel_->SignalStreamClosedRemotely.connect(
1444 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001445 return true;
1446}
1447
1448void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001449 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001450 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1451 return;
1452 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001453 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1454 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001455 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001456}
1457
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001458void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001459 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001460 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1461 return;
1462 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001463 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1464 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001465}
1466
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001467void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001468 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001469 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1470 << "NULL.";
1471 return;
1472 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001473 data_channel_->RemoveRecvStream(sid);
1474 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001475}
1476
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001477bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001478 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001479}
1480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481cricket::DataChannelType WebRtcSession::data_channel_type() const {
1482 return data_channel_type_;
1483}
1484
wu@webrtc.org91053e72013-08-10 07:18:04 +00001485bool WebRtcSession::IceRestartPending() const {
1486 return ice_restart_latch_->Get();
1487}
1488
1489void WebRtcSession::ResetIceRestartLatch() {
1490 ice_restart_latch_->Reset();
1491}
1492
Henrik Boströmd8281982015-08-27 10:12:24 +02001493void WebRtcSession::OnCertificateReady(
1494 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001495 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001496}
1497
Henrik Boströmd8281982015-08-27 10:12:24 +02001498bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001499 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001500}
1501
deadbeefcbecd352015-09-23 11:50:27 -07001502const rtc::scoped_refptr<rtc::RTCCertificate>&
1503WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001504 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001505}
1506
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507void WebRtcSession::SetIceConnectionState(
1508 PeerConnectionInterface::IceConnectionState state) {
1509 if (ice_connection_state_ == state) {
1510 return;
1511 }
1512
1513 // ASSERT that the requested transition is allowed. Note that
1514 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1515 // within PeerConnection). This switch statement should compile away when
1516 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001517 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1518 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 switch (ice_connection_state_) {
1520 case PeerConnectionInterface::kIceConnectionNew:
1521 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1522 break;
1523 case PeerConnectionInterface::kIceConnectionChecking:
1524 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1525 state == PeerConnectionInterface::kIceConnectionConnected);
1526 break;
1527 case PeerConnectionInterface::kIceConnectionConnected:
1528 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1529 state == PeerConnectionInterface::kIceConnectionChecking ||
1530 state == PeerConnectionInterface::kIceConnectionCompleted);
1531 break;
1532 case PeerConnectionInterface::kIceConnectionCompleted:
1533 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1534 state == PeerConnectionInterface::kIceConnectionDisconnected);
1535 break;
1536 case PeerConnectionInterface::kIceConnectionFailed:
1537 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1538 break;
1539 case PeerConnectionInterface::kIceConnectionDisconnected:
1540 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1541 state == PeerConnectionInterface::kIceConnectionConnected ||
1542 state == PeerConnectionInterface::kIceConnectionCompleted ||
1543 state == PeerConnectionInterface::kIceConnectionFailed);
1544 break;
1545 case PeerConnectionInterface::kIceConnectionClosed:
1546 ASSERT(false);
1547 break;
1548 default:
1549 ASSERT(false);
1550 break;
1551 }
1552
1553 ice_connection_state_ = state;
1554 if (ice_observer_) {
1555 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1556 }
1557}
1558
deadbeefcbecd352015-09-23 11:50:27 -07001559void WebRtcSession::OnTransportControllerConnectionState(
1560 cricket::IceConnectionState state) {
1561 switch (state) {
1562 case cricket::kIceConnectionConnecting:
1563 // If the current state is Connected or Completed, then there were
1564 // writable channels but now there are not, so the next state must
1565 // be Disconnected.
1566 // kIceConnectionConnecting is currently used as the default,
1567 // un-connected state by the TransportController, so its only use is
1568 // detecting disconnections.
1569 if (ice_connection_state_ ==
1570 PeerConnectionInterface::kIceConnectionConnected ||
1571 ice_connection_state_ ==
1572 PeerConnectionInterface::kIceConnectionCompleted) {
1573 SetIceConnectionState(
1574 PeerConnectionInterface::kIceConnectionDisconnected);
1575 }
torbjornga81a42f2015-09-23 02:16:58 -07001576 break;
deadbeefcbecd352015-09-23 11:50:27 -07001577 case cricket::kIceConnectionFailed:
1578 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1579 break;
1580 case cricket::kIceConnectionConnected:
1581 LOG(LS_INFO) << "Changing to ICE connected state because "
1582 << "all transports are writable.";
1583 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1584 break;
1585 case cricket::kIceConnectionCompleted:
1586 LOG(LS_INFO) << "Changing to ICE completed state because "
1587 << "all transports are complete.";
1588 if (ice_connection_state_ !=
1589 PeerConnectionInterface::kIceConnectionConnected) {
1590 // If jumping directly from "checking" to "connected",
1591 // signal "connected" first.
1592 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1593 }
1594 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1595 if (metrics_observer_) {
1596 ReportTransportStats();
1597 }
1598 break;
1599 default:
1600 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001601 }
deadbeefcbecd352015-09-23 11:50:27 -07001602}
1603
1604void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001605 SetIceConnectionReceiving(receiving);
1606}
1607
1608void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1609 if (ice_connection_receiving_ == receiving) {
1610 return;
1611 }
1612 ice_connection_receiving_ = receiving;
1613 if (ice_observer_) {
1614 ice_observer_->OnIceConnectionReceivingChange(receiving);
1615 }
1616}
1617
deadbeefcbecd352015-09-23 11:50:27 -07001618void WebRtcSession::OnTransportControllerCandidatesGathered(
1619 const std::string& transport_name,
1620 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001622 int sdp_mline_index;
1623 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1624 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1625 << transport_name << " not found";
1626 return;
1627 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628
deadbeefcbecd352015-09-23 11:50:27 -07001629 for (cricket::Candidates::const_iterator citer = candidates.begin();
1630 citer != candidates.end(); ++citer) {
1631 // Use transport_name as the candidate media id.
1632 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1633 if (ice_observer_) {
1634 ice_observer_->OnIceCandidate(&candidate);
1635 }
1636 if (local_desc_) {
1637 local_desc_->AddCandidate(&candidate);
1638 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 }
1640}
1641
1642// Enabling voice and video channel.
1643void WebRtcSession::EnableChannels() {
1644 if (voice_channel_ && !voice_channel_->enabled())
1645 voice_channel_->Enable(true);
1646
1647 if (video_channel_ && !video_channel_->enabled())
1648 video_channel_->Enable(true);
1649
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001650 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 data_channel_->Enable(true);
1652}
1653
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654// Returns the media index for a local ice candidate given the content name.
1655bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1656 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001657 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660
1661 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001662 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 for (size_t index = 0; index < contents.size(); ++index) {
1664 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001665 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 content_found = true;
1667 break;
1668 }
1669 }
1670 return content_found;
1671}
1672
1673bool WebRtcSession::UseCandidatesInSessionDescription(
1674 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001675 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001679
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1681 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001682 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001683 const IceCandidateInterface* candidate = candidates->at(n);
1684 bool valid = false;
1685 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1686 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001687 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1688 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001689 }
1690 continue;
1691 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001692 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001693 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 break;
deadbeefd59daf82015-10-14 15:02:44 -07001695 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 }
1697 }
1698 return ret;
1699}
1700
1701bool WebRtcSession::UseCandidate(
1702 const IceCandidateInterface* candidate) {
1703
1704 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001705 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 if (mediacontent_index >= remote_content_size) {
1707 LOG(LS_ERROR)
1708 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1709 return false;
1710 }
1711
1712 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001713 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 std::vector<cricket::Candidate> candidates;
1715 candidates.push_back(candidate->candidate());
1716 // Invoking BaseSession method to handle remote candidates.
1717 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001718 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1719 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 // Candidates successfully submitted for checking.
1721 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1722 ice_connection_state_ ==
1723 PeerConnectionInterface::kIceConnectionDisconnected) {
1724 // If state is New, then the session has just gotten its first remote ICE
1725 // candidates, so go to Checking.
1726 // If state is Disconnected, the session is re-using old candidates or
1727 // receiving additional ones, so go to Checking.
1728 // If state is Connected, stay Connected.
1729 // TODO(bemasc): If state is Connected, and the new candidates are for a
1730 // newly added transport, then the state actually _should_ move to
1731 // checking. Add a way to distinguish that case.
1732 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1733 }
1734 // TODO(bemasc): If state is Completed, go back to Connected.
1735 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001736 if (!error.empty()) {
1737 LOG(LS_WARNING) << error;
1738 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 }
1740 return true;
1741}
1742
deadbeefcbecd352015-09-23 11:50:27 -07001743void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001744 // Destroy video_channel_ first since it may have a pointer to the
1745 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 const cricket::ContentInfo* video_info =
1747 cricket::GetFirstVideoContent(desc);
1748 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 SignalVideoChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001750 const std::string content_name = video_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 }
1753
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001754 const cricket::ContentInfo* voice_info =
1755 cricket::GetFirstAudioContent(desc);
1756 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001757 SignalVoiceChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001758 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001759 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001760 }
1761
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 const cricket::ContentInfo* data_info =
1763 cricket::GetFirstDataContent(desc);
1764 if ((!data_info || data_info->rejected) && data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 SignalDataChannelDestroyed();
deadbeef8f46c632015-10-26 14:11:17 -07001766 const std::string content_name = data_channel_->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 }
1769}
1770
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001771// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001772// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 // Creating the media channels and transport proxies.
1775 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1776 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001777 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 LOG(LS_ERROR) << "Failed to create voice channel.";
1779 return false;
1780 }
1781 }
1782
1783 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1784 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001785 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 LOG(LS_ERROR) << "Failed to create video channel.";
1787 return false;
1788 }
1789 }
1790
1791 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1792 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001793 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001794 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 LOG(LS_ERROR) << "Failed to create data channel.";
1796 return false;
1797 }
1798 }
1799
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001800 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1801 if (voice_channel()) {
1802 voice_channel()->ActivateRtcpMux();
1803 }
1804 if (video_channel()) {
1805 video_channel()->ActivateRtcpMux();
1806 }
1807 if (data_channel()) {
1808 data_channel()->ActivateRtcpMux();
1809 }
1810 }
1811
deadbeefcbecd352015-09-23 11:50:27 -07001812 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001813 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001814 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1815 cricket::GROUP_TYPE_BUNDLE);
1816 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001817 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1818 return false;
1819 }
deadbeefcbecd352015-09-23 11:50:27 -07001820 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001821 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1822 return false;
1823 }
1824 }
1825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 return true;
1827}
1828
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001829bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001831 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001832 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001833 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001834 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001835 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001836
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001837 voice_channel_->SignalDtlsSetupFailure.connect(
1838 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001839
1840 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001841 voice_channel_->transport_channel()->SignalSentPacket.connect(
1842 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001843 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844}
1845
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001846bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001848 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001849 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001850 if (!video_channel_) {
1851 return false;
1852 }
1853
1854 video_channel_->SignalDtlsSetupFailure.connect(
1855 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001856
1857 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001858 video_channel_->transport_channel()->SignalSentPacket.connect(
1859 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001860 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861}
1862
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001863bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001864 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001866 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001867 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001868 return false;
1869 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001870
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001871 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001872 data_channel_->SignalDataReceived.connect(
1873 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001874 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001875
1876 data_channel_->SignalDtlsSetupFailure.connect(
1877 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001878
1879 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001880 data_channel_->transport_channel()->SignalSentPacket.connect(
1881 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001882 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883}
1884
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001885void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001886 SetError(ERROR_TRANSPORT,
1887 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888}
1889
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001890void WebRtcSession::OnDataChannelMessageReceived(
1891 cricket::DataChannel* channel,
1892 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001893 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001894 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1895 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1896 // Received OPEN message; parse and signal that a new data channel should
1897 // be created.
1898 std::string label;
1899 InternalDataChannelInit config;
1900 config.id = params.ssrc;
1901 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1902 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1903 << params.ssrc;
1904 return;
1905 }
1906 config.open_handshake_role = InternalDataChannelInit::kAcker;
1907 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 }
deadbeefab9b2d12015-10-14 11:33:11 -07001909 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910}
1911
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001912// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001914 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1915 if (!bundle_enabled)
1916 return true;
1917
1918 const cricket::ContentGroup* bundle_group =
1919 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1920 ASSERT(bundle_group != NULL);
1921
1922 const cricket::ContentInfos& contents = desc->contents();
1923 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1924 citer != contents.end(); ++citer) {
1925 const cricket::ContentInfo* content = (&*citer);
1926 ASSERT(content != NULL);
1927 if (bundle_group->HasContentName(content->name) &&
1928 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1929 if (!HasRtcpMuxEnabled(content))
1930 return false;
1931 }
1932 }
1933 // RTCP-MUX is enabled in all the contents.
1934 return true;
1935}
1936
1937bool WebRtcSession::HasRtcpMuxEnabled(
1938 const cricket::ContentInfo* content) {
1939 const cricket::MediaContentDescription* description =
1940 static_cast<cricket::MediaContentDescription*>(content->description);
1941 return description->rtcp_mux();
1942}
1943
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001944bool WebRtcSession::ValidateSessionDescription(
1945 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001946 cricket::ContentSource source, std::string* err_desc) {
1947 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001948 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001949 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001950 }
1951
1952 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001953 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001954 }
1955
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001956 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001957 Action action = GetAction(sdesc->type());
1958 if (source == cricket::CS_LOCAL) {
1959 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001960 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001961 } else {
1962 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001963 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001964 }
1965
1966 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001967 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001968 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1969 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001970 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001971 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001972 }
1973
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001974 // Verify ice-ufrag and ice-pwd.
1975 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001976 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001977 }
1978
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001979 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001980 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001981 }
1982
1983 // Verify m-lines in Answer when compared against Offer.
1984 if (action == kAnswer) {
1985 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001986 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1987 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001988 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001989 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001990 }
1991 }
1992
1993 return true;
1994}
1995
1996bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1997 return ((action == kOffer && state() == STATE_INIT) ||
1998 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001999 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002001 (action == kOffer && state() == STATE_INPROGRESS) ||
2002 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002003 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2004 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2005 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2006 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002007}
2008
2009bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2010 return ((action == kOffer && state() == STATE_INIT) ||
2011 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002012 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002013 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002014 (action == kOffer && state() == STATE_INPROGRESS) ||
2015 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002016 (action == kAnswer && state() == STATE_SENTOFFER) ||
2017 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2018 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2019 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002020}
2021
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002022std::string WebRtcSession::GetSessionErrorMsg() {
2023 std::ostringstream desc;
2024 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2025 desc << kSessionErrorDesc << error_desc() << ".";
2026 return desc.str();
2027}
2028
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002029// We need to check the local/remote description for the Transport instead of
2030// the session, because a new Transport added during renegotiation may have
2031// them unset while the session has them set from the previous negotiation.
2032// Not doing so may trigger the auto generation of transport description and
2033// mess up DTLS identity information, ICE credential, etc.
2034bool WebRtcSession::ReadyToUseRemoteCandidate(
2035 const IceCandidateInterface* candidate,
2036 const SessionDescriptionInterface* remote_desc,
2037 bool* valid) {
2038 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002039
2040 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002041 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002042
deadbeefd59daf82015-10-14 15:02:44 -07002043 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002044 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002045 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002046
2047 size_t mediacontent_index =
2048 static_cast<size_t>(candidate->sdp_mline_index());
2049 size_t remote_content_size =
2050 current_remote_desc->description()->contents().size();
2051 if (mediacontent_index >= remote_content_size) {
2052 LOG(LS_ERROR)
2053 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2054
2055 *valid = false;
2056 return false;
2057 }
2058
2059 cricket::ContentInfo content =
2060 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002061 cricket::BaseChannel* channel = GetChannel(content.name);
2062 if (!channel) {
2063 return false;
2064 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002065
deadbeefd59daf82015-10-14 15:02:44 -07002066 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002067 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002068}
2069
deadbeefcbecd352015-09-23 11:50:27 -07002070void WebRtcSession::OnTransportControllerGatheringState(
2071 cricket::IceGatheringState state) {
2072 ASSERT(signaling_thread()->IsCurrent());
2073 if (state == cricket::kIceGatheringGathering) {
2074 if (ice_observer_) {
2075 ice_observer_->OnIceGatheringChange(
2076 PeerConnectionInterface::kIceGatheringGathering);
2077 }
2078 } else if (state == cricket::kIceGatheringComplete) {
2079 if (ice_observer_) {
2080 ice_observer_->OnIceGatheringChange(
2081 PeerConnectionInterface::kIceGatheringComplete);
2082 ice_observer_->OnIceComplete();
2083 }
2084 }
2085}
2086
2087void WebRtcSession::ReportTransportStats() {
2088 // Use a set so we don't report the same stats twice if two channels share
2089 // a transport.
2090 std::set<std::string> transport_names;
2091 if (voice_channel()) {
2092 transport_names.insert(voice_channel()->transport_name());
2093 }
2094 if (video_channel()) {
2095 transport_names.insert(video_channel()->transport_name());
2096 }
2097 if (data_channel()) {
2098 transport_names.insert(data_channel()->transport_name());
2099 }
2100 for (const auto& name : transport_names) {
2101 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002102 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002103 ReportBestConnectionState(stats);
2104 ReportNegotiatedCiphers(stats);
2105 }
2106 }
2107}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002108// Walk through the ConnectionInfos to gather best connection usage
2109// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002110void WebRtcSession::ReportBestConnectionState(
2111 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002112 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002113 for (cricket::TransportChannelStatsList::const_iterator it =
2114 stats.channel_stats.begin();
2115 it != stats.channel_stats.end(); ++it) {
2116 for (cricket::ConnectionInfos::const_iterator it_info =
2117 it->connection_infos.begin();
2118 it_info != it->connection_infos.end(); ++it_info) {
2119 if (!it_info->best_connection) {
2120 continue;
2121 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002122
2123 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2124 const cricket::Candidate& local = it_info->local_candidate;
2125 const cricket::Candidate& remote = it_info->remote_candidate;
2126
2127 // Increment the counter for IceCandidatePairType.
2128 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2129 (local.type() == RELAY_PORT_TYPE &&
2130 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2131 type = kEnumCounterIceCandidatePairTypeTcp;
2132 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2133 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002134 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002135 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002136 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002137 metrics_observer_->IncrementEnumCounter(
2138 type, GetIceCandidatePairCounter(local, remote),
2139 kIceCandidatePairMax);
2140
2141 // Increment the counter for IP type.
2142 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002143 metrics_observer_->IncrementEnumCounter(
2144 kEnumCounterAddressFamily, kBestConnections_IPv4,
2145 kPeerConnectionAddressFamilyCounter_Max);
2146
2147 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002148 metrics_observer_->IncrementEnumCounter(
2149 kEnumCounterAddressFamily, kBestConnections_IPv6,
2150 kPeerConnectionAddressFamilyCounter_Max);
2151 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002152 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002153 }
2154
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002155 return;
2156 }
2157 }
2158}
2159
jbauchac8869e2015-07-03 01:36:14 -07002160void WebRtcSession::ReportNegotiatedCiphers(
2161 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002162 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002163 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2164 return;
2165 }
2166
2167 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07002168 int ssl_cipher = stats.channel_stats[0].ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002169 if (srtp_cipher.empty() && !ssl_cipher) {
jbauchac8869e2015-07-03 01:36:14 -07002170 return;
2171 }
2172
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002173 PeerConnectionEnumCounterType srtp_counter_type;
2174 PeerConnectionEnumCounterType ssl_counter_type;
deadbeefcbecd352015-09-23 11:50:27 -07002175 if (stats.transport_name == cricket::CN_AUDIO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002176 srtp_counter_type = kEnumCounterAudioSrtpCipher;
2177 ssl_counter_type = kEnumCounterAudioSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002178 } else if (stats.transport_name == cricket::CN_VIDEO) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002179 srtp_counter_type = kEnumCounterVideoSrtpCipher;
2180 ssl_counter_type = kEnumCounterVideoSslCipher;
deadbeefcbecd352015-09-23 11:50:27 -07002181 } else if (stats.transport_name == cricket::CN_DATA) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002182 srtp_counter_type = kEnumCounterDataSrtpCipher;
2183 ssl_counter_type = kEnumCounterDataSslCipher;
jbauchac8869e2015-07-03 01:36:14 -07002184 } else {
2185 RTC_NOTREACHED();
2186 return;
2187 }
2188
2189 if (!srtp_cipher.empty()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002190 metrics_observer_->IncrementSparseEnumCounter(
2191 srtp_counter_type, rtc::GetSrtpCryptoSuiteFromName(srtp_cipher));
jbauchac8869e2015-07-03 01:36:14 -07002192 }
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002193 if (ssl_cipher) {
2194 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, ssl_cipher);
jbauchac8869e2015-07-03 01:36:14 -07002195 }
2196}
2197
stefanc1aeaf02015-10-15 07:26:07 -07002198void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2199 const rtc::SentPacket& sent_packet) {
2200 RTC_DCHECK(worker_thread()->IsCurrent());
2201 media_controller_->call_w()->OnSentPacket(sent_packet);
2202}
2203
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204} // namespace webrtc