blob: 5e946a291bfcb31546fd5ae9f4a6064db2c955ea [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>
deadbeefcbecd352015-09-23 11:50:27 -070033#include <set>
Tommif888bb52015-12-12 01:37:01 +010034#include <utility>
35#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37#include "talk/app/webrtc/jsepicecandidate.h"
38#include "talk/app/webrtc/jsepsessiondescription.h"
39#include "talk/app/webrtc/mediaconstraintsinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#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"
Tommif888bb52015-12-12 01:37:01 +010048#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070050#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000051#include "webrtc/base/helpers.h"
52#include "webrtc/base/logging.h"
53#include "webrtc/base/stringencode.h"
54#include "webrtc/base/stringutils.h"
stefanc1aeaf02015-10-15 07:26:07 -070055#include "webrtc/call.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000056#include "webrtc/p2p/base/portallocator.h"
stefanc1aeaf02015-10-15 07:26:07 -070057#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59using cricket::ContentInfo;
60using cricket::ContentInfos;
61using cricket::MediaContentDescription;
62using cricket::SessionDescription;
63using cricket::TransportInfo;
64
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070065using cricket::LOCAL_PORT_TYPE;
66using cricket::STUN_PORT_TYPE;
67using cricket::RELAY_PORT_TYPE;
68using cricket::PRFLX_PORT_TYPE;
69
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070namespace webrtc {
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000073const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
74 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076const char kInvalidCandidates[] = "Description contains invalid candidates.";
77const char kInvalidSdp[] = "Invalid session description.";
78const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
80const char kPushDownTDFailed[] =
81 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000082const char kSdpWithoutDtlsFingerprint[] =
83 "Called with SDP without DTLS fingerprint.";
84const char kSdpWithoutSdesCrypto[] =
85 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000086const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000087 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000089const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000090const char kDtlsSetupFailureRtp[] =
91 "Couldn't set up DTLS-SRTP on RTP channel.";
92const char kDtlsSetupFailureRtcp[] =
93 "Couldn't set up DTLS-SRTP on RTCP channel.";
deadbeefcbecd352015-09-23 11:50:27 -070094const char kEnableBundleFailed[] = "Failed to enable BUNDLE.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000095const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070097IceCandidatePairType GetIceCandidatePairCounter(
98 const cricket::Candidate& local,
99 const cricket::Candidate& remote) {
100 const auto& l = local.type();
101 const auto& r = remote.type();
102 const auto& host = LOCAL_PORT_TYPE;
103 const auto& srflx = STUN_PORT_TYPE;
104 const auto& relay = RELAY_PORT_TYPE;
105 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700106 if (l == host && r == host) {
107 bool local_private = IPIsPrivate(local.address().ipaddr());
108 bool remote_private = IPIsPrivate(remote.address().ipaddr());
109 if (local_private) {
110 if (remote_private) {
111 return kIceCandidatePairHostPrivateHostPrivate;
112 } else {
113 return kIceCandidatePairHostPrivateHostPublic;
114 }
115 } else {
116 if (remote_private) {
117 return kIceCandidatePairHostPublicHostPrivate;
118 } else {
119 return kIceCandidatePairHostPublicHostPublic;
120 }
121 }
122 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700123 if (l == host && r == srflx)
124 return kIceCandidatePairHostSrflx;
125 if (l == host && r == relay)
126 return kIceCandidatePairHostRelay;
127 if (l == host && r == prflx)
128 return kIceCandidatePairHostPrflx;
129 if (l == srflx && r == host)
130 return kIceCandidatePairSrflxHost;
131 if (l == srflx && r == srflx)
132 return kIceCandidatePairSrflxSrflx;
133 if (l == srflx && r == relay)
134 return kIceCandidatePairSrflxRelay;
135 if (l == srflx && r == prflx)
136 return kIceCandidatePairSrflxPrflx;
137 if (l == relay && r == host)
138 return kIceCandidatePairRelayHost;
139 if (l == relay && r == srflx)
140 return kIceCandidatePairRelaySrflx;
141 if (l == relay && r == relay)
142 return kIceCandidatePairRelayRelay;
143 if (l == relay && r == prflx)
144 return kIceCandidatePairRelayPrflx;
145 if (l == prflx && r == host)
146 return kIceCandidatePairPrflxHost;
147 if (l == prflx && r == srflx)
148 return kIceCandidatePairPrflxSrflx;
149 if (l == prflx && r == relay)
150 return kIceCandidatePairPrflxRelay;
151 return kIceCandidatePairMax;
152}
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154// Compares |answer| against |offer|. Comparision is done
155// for number of m-lines in answer against offer. If matches true will be
156// returned otherwise false.
157static bool VerifyMediaDescriptions(
158 const SessionDescription* answer, const SessionDescription* offer) {
159 if (offer->contents().size() != answer->contents().size())
160 return false;
161
162 for (size_t i = 0; i < offer->contents().size(); ++i) {
163 if ((offer->contents()[i].name) != answer->contents()[i].name) {
164 return false;
165 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000166 const MediaContentDescription* offer_mdesc =
167 static_cast<const MediaContentDescription*>(
168 offer->contents()[i].description);
169 const MediaContentDescription* answer_mdesc =
170 static_cast<const MediaContentDescription*>(
171 answer->contents()[i].description);
172 if (offer_mdesc->type() != answer_mdesc->type()) {
173 return false;
174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
176 return true;
177}
178
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179// Checks that each non-rejected content has SDES crypto keys or a DTLS
180// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
181// keys, will be caught in Transport negotiation, and backstopped by Channel's
182// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000183static bool VerifyCrypto(const SessionDescription* desc,
184 bool dtls_enabled,
185 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 const ContentInfos& contents = desc->contents();
187 for (size_t index = 0; index < contents.size(); ++index) {
188 const ContentInfo* cinfo = &contents[index];
189 if (cinfo->rejected) {
190 continue;
191 }
192
193 // If the content isn't rejected, crypto must be present.
194 const MediaContentDescription* media =
195 static_cast<const MediaContentDescription*>(cinfo->description);
196 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
197 if (!media || !tinfo) {
198 // Something is not right.
199 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000200 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 return false;
202 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000203 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000204 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000205 LOG(LS_WARNING) <<
206 "Session description must have DTLS fingerprint if DTLS enabled.";
207 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000208 return false;
209 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000210 } else {
211 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000212 LOG(LS_WARNING) <<
213 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000214 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000215 return false;
216 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 }
218 }
219
220 return true;
221}
222
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000223// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
224static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
225 const ContentInfos& contents = desc->contents();
226 for (size_t index = 0; index < contents.size(); ++index) {
227 const ContentInfo* cinfo = &contents[index];
228 if (cinfo->rejected) {
229 continue;
230 }
231
232 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
233 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
234 if (!tinfo) {
235 // Something is not right.
236 LOG(LS_ERROR) << kInvalidSdp;
237 return false;
238 }
239 if (tinfo->description.ice_ufrag.empty() ||
240 tinfo->description.ice_pwd.empty()) {
241 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
242 return false;
243 }
244 }
245 return true;
246}
247
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248// Forces |sdesc->crypto_required| to the appropriate state based on the
249// current security policy, to ensure a failure occurs if there is an error
250// in crypto negotiation.
251// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000252static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
253 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000254 if (!sdesc) {
255 return;
256 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257
wu@webrtc.org91053e72013-08-10 07:18:04 +0000258 // Updating the |crypto_required_| in MediaContentDescription to the
259 // appropriate state based on the current security policy.
260 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
261 iter != sdesc->contents().end(); ++iter) {
262 if (cricket::IsMediaContent(&*iter)) {
263 MediaContentDescription* mdesc =
264 static_cast<MediaContentDescription*> (iter->description);
265 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000266 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000267 }
268 }
269 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270}
271
Peter Boström0c4e06b2015-10-07 12:23:21 +0200272static bool GetAudioSsrcByTrackId(const SessionDescription* session_description,
273 const std::string& track_id,
274 uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 const cricket::ContentInfo* audio_info =
276 cricket::GetFirstAudioContent(session_description);
277 if (!audio_info) {
278 LOG(LS_ERROR) << "Audio not used in this call";
279 return false;
280 }
281
282 const cricket::MediaContentDescription* audio_content =
283 static_cast<const cricket::MediaContentDescription*>(
284 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000285 const cricket::StreamParams* stream =
286 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
287 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 return false;
289 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000290
291 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 return true;
293}
294
295static bool GetTrackIdBySsrc(const SessionDescription* session_description,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200296 uint32_t ssrc,
297 std::string* track_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 ASSERT(track_id != NULL);
299
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 const cricket::ContentInfo* audio_info =
301 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000302 if (audio_info) {
303 const cricket::MediaContentDescription* audio_content =
304 static_cast<const cricket::MediaContentDescription*>(
305 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000307 const auto* found =
308 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
309 if (found) {
310 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000311 return true;
312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 }
314
315 const cricket::ContentInfo* video_info =
316 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000317 if (video_info) {
318 const cricket::MediaContentDescription* video_content =
319 static_cast<const cricket::MediaContentDescription*>(
320 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000322 const auto* found =
323 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
324 if (found) {
325 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000326 return true;
327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 }
329 return false;
330}
331
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000332static bool BadSdp(const std::string& source,
333 const std::string& type,
334 const std::string& reason,
335 std::string* err_desc) {
336 std::ostringstream desc;
deadbeefd59daf82015-10-14 15:02:44 -0700337 desc << "Failed to set " << source;
338 if (!type.empty()) {
339 desc << " " << type;
340 }
341 desc << " sdp: " << reason;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 return false;
348}
349
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 const std::string& type,
352 const std::string& reason,
353 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000355 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000357 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 }
359}
360
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000361static bool BadLocalSdp(const std::string& type,
362 const std::string& reason,
363 std::string* err_desc) {
364 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
365}
366
367static bool BadRemoteSdp(const std::string& type,
368 const std::string& reason,
369 std::string* err_desc) {
370 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
371}
372
373static bool BadOfferSdp(cricket::ContentSource source,
374 const std::string& reason,
375 std::string* err_desc) {
376 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
377}
378
379static bool BadPranswerSdp(cricket::ContentSource source,
380 const std::string& reason,
381 std::string* err_desc) {
382 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
383 reason, err_desc);
384}
385
386static bool BadAnswerSdp(cricket::ContentSource source,
387 const std::string& reason,
388 std::string* err_desc) {
389 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390}
391
deadbeefd59daf82015-10-14 15:02:44 -0700392#define GET_STRING_OF_STATE(state) \
393 case webrtc::WebRtcSession::state: \
394 result = #state; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 break;
396
deadbeefd59daf82015-10-14 15:02:44 -0700397static std::string GetStateString(webrtc::WebRtcSession::State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 std::string result;
399 switch (state) {
400 GET_STRING_OF_STATE(STATE_INIT)
deadbeefd59daf82015-10-14 15:02:44 -0700401 GET_STRING_OF_STATE(STATE_SENTOFFER)
402 GET_STRING_OF_STATE(STATE_RECEIVEDOFFER)
403 GET_STRING_OF_STATE(STATE_SENTPRANSWER)
404 GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 GET_STRING_OF_STATE(STATE_INPROGRESS)
deadbeefd59daf82015-10-14 15:02:44 -0700406 GET_STRING_OF_STATE(STATE_CLOSED)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 default:
408 ASSERT(false);
409 break;
410 }
411 return result;
412}
413
deadbeefd59daf82015-10-14 15:02:44 -0700414#define GET_STRING_OF_ERROR_CODE(err) \
415 case webrtc::WebRtcSession::err: \
416 result = #err; \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 break;
418
deadbeefd59daf82015-10-14 15:02:44 -0700419static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 std::string result;
421 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000422 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000423 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
424 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 default:
deadbeefd59daf82015-10-14 15:02:44 -0700426 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 break;
428 }
429 return result;
430}
431
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000432static std::string MakeErrorString(const std::string& error,
433 const std::string& desc) {
434 std::ostringstream ret;
435 ret << error << " " << desc;
436 return ret.str();
437}
438
439static std::string MakeTdErrorString(const std::string& desc) {
440 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000443// Set |option| to the highest-priority value of |key| in the optional
444// constraints if the key is found and has a valid value.
kwiberg102c6a62015-10-30 02:47:38 -0700445template <typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000446static void SetOptionFromOptionalConstraint(
447 const MediaConstraintsInterface* constraints,
kwiberg102c6a62015-10-30 02:47:38 -0700448 const std::string& key,
Karl Wibergbe579832015-11-10 22:34:18 +0100449 rtc::Optional<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000450 if (!constraints) {
451 return;
452 }
453 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000454 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000455 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 if (rtc::FromString(string_value, &value)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100457 *option = rtc::Optional<T>(value);
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000458 }
459 }
460}
461
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462uint32_t ConvertIceTransportTypeToCandidateFilter(
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000463 PeerConnectionInterface::IceTransportsType type) {
464 switch (type) {
465 case PeerConnectionInterface::kNone:
466 return cricket::CF_NONE;
467 case PeerConnectionInterface::kRelay:
468 return cricket::CF_RELAY;
469 case PeerConnectionInterface::kNoHost:
470 return (cricket::CF_ALL & ~cricket::CF_HOST);
471 case PeerConnectionInterface::kAll:
472 return cricket::CF_ALL;
473 default: ASSERT(false);
474 }
475 return cricket::CF_NONE;
476}
477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478// Help class used to remember if a a remote peer has requested ice restart by
479// by sending a description with new ice ufrag and password.
480class IceRestartAnswerLatch {
481 public:
482 IceRestartAnswerLatch() : ice_restart_(false) { }
483
wu@webrtc.org91053e72013-08-10 07:18:04 +0000484 // Returns true if CheckForRemoteIceRestart has been called with a new session
485 // description where ice password and ufrag has changed since last time
486 // Reset() was called.
487 bool Get() const {
488 return ice_restart_;
489 }
490
491 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 if (ice_restart_) {
493 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 }
496
Honghai Zhang04e91462015-12-11 14:26:22 -0800497 // This method has two purposes: 1. Return whether |new_desc| requests
498 // an ICE restart (i.e., new ufrag/pwd). 2. If it requests an ICE restart
499 // and it is an OFFER, remember this in |ice_restart_| so that the next
500 // Local Answer will be created with new ufrag and pwd.
honghaiz503726c2015-07-31 12:37:38 -0700501 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
502 const SessionDescriptionInterface* new_desc) {
Honghai Zhang04e91462015-12-11 14:26:22 -0800503 if (!old_desc) {
honghaiz503726c2015-07-31 12:37:38 -0700504 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 }
506 const SessionDescription* new_sd = new_desc->description();
507 const SessionDescription* old_sd = old_desc->description();
508 const ContentInfos& contents = new_sd->contents();
509 for (size_t index = 0; index < contents.size(); ++index) {
510 const ContentInfo* cinfo = &contents[index];
511 if (cinfo->rejected) {
512 continue;
513 }
514 // If the content isn't rejected, check if ufrag and password has
515 // changed.
516 const cricket::TransportDescription* new_transport_desc =
517 new_sd->GetTransportDescriptionByName(cinfo->name);
518 const cricket::TransportDescription* old_transport_desc =
519 old_sd->GetTransportDescriptionByName(cinfo->name);
520 if (!new_transport_desc || !old_transport_desc) {
521 // No transport description exist. This is not an ice restart.
522 continue;
523 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000524 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
525 old_transport_desc->ice_pwd,
526 new_transport_desc->ice_ufrag,
527 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 LOG(LS_INFO) << "Remote peer request ice restart.";
Honghai Zhang04e91462015-12-11 14:26:22 -0800529 if (new_desc->type() == SessionDescriptionInterface::kOffer) {
530 ice_restart_ = true;
531 }
honghaiz503726c2015-07-31 12:37:38 -0700532 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 }
534 }
honghaiz503726c2015-07-31 12:37:38 -0700535 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
537
538 private:
539 bool ice_restart_;
540};
541
stefanc1aeaf02015-10-15 07:26:07 -0700542WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
deadbeefab9b2d12015-10-14 11:33:11 -0700543 rtc::Thread* signaling_thread,
544 rtc::Thread* worker_thread,
545 cricket::PortAllocator* port_allocator)
deadbeefd59daf82015-10-14 15:02:44 -0700546 : signaling_thread_(signaling_thread),
547 worker_thread_(worker_thread),
548 port_allocator_(port_allocator),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 // RFC 3264: The numeric value of the session id and version in the
550 // o line MUST be representable with a "64 bit signed integer".
551 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
deadbeefd59daf82015-10-14 15:02:44 -0700552 sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
553 transport_controller_(new cricket::TransportController(signaling_thread,
554 worker_thread,
555 port_allocator)),
stefanc1aeaf02015-10-15 07:26:07 -0700556 media_controller_(media_controller),
557 channel_manager_(media_controller_->channel_manager()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 ice_observer_(NULL),
559 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700560 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000562 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000564 ice_restart_latch_(new IceRestartAnswerLatch),
565 metrics_observer_(NULL) {
deadbeefd59daf82015-10-14 15:02:44 -0700566 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED);
567 transport_controller_->SignalConnectionState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700568 this, &WebRtcSession::OnTransportControllerConnectionState);
deadbeefd59daf82015-10-14 15:02:44 -0700569 transport_controller_->SignalReceiving.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700570 this, &WebRtcSession::OnTransportControllerReceiving);
deadbeefd59daf82015-10-14 15:02:44 -0700571 transport_controller_->SignalGatheringState.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700572 this, &WebRtcSession::OnTransportControllerGatheringState);
deadbeefd59daf82015-10-14 15:02:44 -0700573 transport_controller_->SignalCandidatesGathered.connect(
deadbeefcbecd352015-09-23 11:50:27 -0700574 this, &WebRtcSession::OnTransportControllerCandidatesGathered);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575}
576
577WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700578 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000579 // Destroy video_channel_ first since it may have a pointer to the
580 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000581 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 SignalVideoChannelDestroyed();
583 channel_manager_->DestroyVideoChannel(video_channel_.release());
584 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000585 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000586 SignalVoiceChannelDestroyed();
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200587 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000588 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000589 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 SignalDataChannelDestroyed();
591 channel_manager_->DestroyDataChannel(data_channel_.release());
592 }
deadbeefd59daf82015-10-14 15:02:44 -0700593
594 LOG(LS_INFO) << "Session: " << id() << " is destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595}
596
wu@webrtc.org91053e72013-08-10 07:18:04 +0000597bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000598 const PeerConnectionFactoryInterface::Options& options,
deadbeefcbecd352015-09-23 11:50:27 -0700599 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200600 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200601 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
602 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700603 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
qiangchen444682a2015-11-24 18:07:56 -0800604 video_options_.disable_prerenderer_smoothing =
605 rtc::Optional<bool>(rtc_configuration.disable_prerenderer_smoothing);
deadbeefd59daf82015-10-14 15:02:44 -0700606 transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000607
Henrik Boström87713d02015-08-25 09:53:21 +0200608 // Obtain a certificate from RTCConfiguration if any were provided (optional).
609 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
610 if (!rtc_configuration.certificates.empty()) {
611 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
612 // just picking the first one. The decision should be made based on the DTLS
613 // handshake. The DTLS negotiations need to know about all certificates.
614 certificate = rtc_configuration.certificates[0];
615 }
616
honghaiz1f429e32015-09-28 07:57:34 -0700617 SetIceConfig(ParseIceConfig(rtc_configuration));
honghaiz4edc39c2015-09-01 09:53:56 -0700618
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 // TODO(perkj): Take |constraints| into consideration. Return false if not all
620 // mandatory constraints can be fulfilled. Note that |constraints|
621 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000623
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000624 if (options.disable_encryption) {
625 dtls_enabled_ = false;
626 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200627 // Enable DTLS by default if we have an identity store or a certificate.
628 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000629 // |constraints| can override the default |dtls_enabled_| value.
deadbeefcbecd352015-09-23 11:50:27 -0700630 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableDtlsSrtp,
631 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000632 dtls_enabled_ = value;
633 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000637 // It takes precendence over the disable_sctp_data_channels
638 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 if (FindConstraint(
640 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
641 &value, NULL) && value) {
642 LOG(LS_INFO) << "Allowing RTP data engine.";
643 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000644 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000645 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000646 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000647 LOG(LS_INFO) << "Allowing SCTP data engine.";
648 data_channel_type_ = cricket::DCT_SCTP;
649 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
wu@webrtc.orgde305012013-10-31 15:40:38 +0000652 // Find DSCP constraint.
653 if (FindConstraint(
654 constraints,
655 MediaConstraintsInterface::kEnableDscp,
656 &value, NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100657 audio_options_.dscp = rtc::Optional<bool>(value);
658 video_options_.dscp = rtc::Optional<bool>(value);
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000659 }
660
661 // Find Suspend Below Min Bitrate constraint.
662 if (FindConstraint(
663 constraints,
664 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
665 &value,
666 NULL)) {
Karl Wibergbe579832015-11-10 22:34:18 +0100667 video_options_.suspend_below_min_bitrate = rtc::Optional<bool>(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000668 }
669
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000670 SetOptionFromOptionalConstraint(constraints,
671 MediaConstraintsInterface::kScreencastMinBitrate,
672 &video_options_.screencast_min_bitrate);
673
674 // Find constraints for cpu overuse detection.
675 SetOptionFromOptionalConstraint(constraints,
676 MediaConstraintsInterface::kCpuUnderuseThreshold,
677 &video_options_.cpu_underuse_threshold);
678 SetOptionFromOptionalConstraint(constraints,
679 MediaConstraintsInterface::kCpuOveruseThreshold,
680 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000681 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000682 MediaConstraintsInterface::kCpuOveruseDetection,
683 &video_options_.cpu_overuse_detection);
684 SetOptionFromOptionalConstraint(constraints,
685 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
686 &video_options_.cpu_overuse_encode_usage);
687 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000688 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
689 &video_options_.cpu_underuse_encode_rsd_threshold);
690 SetOptionFromOptionalConstraint(constraints,
691 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
692 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000693
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000694 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000695 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
696 &video_options_.unsignalled_recv_stream_limit);
kwiberg102c6a62015-10-30 02:47:38 -0700697 if (video_options_.unsignalled_recv_stream_limit) {
Karl Wibergbe579832015-11-10 22:34:18 +0100698 video_options_.unsignalled_recv_stream_limit = rtc::Optional<int>(
kwiberg102c6a62015-10-30 02:47:38 -0700699 std::max(0, std::min(kMaxUnsignalledRecvStreams,
700 *video_options_.unsignalled_recv_stream_limit)));
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000701 }
702
703 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000704 MediaConstraintsInterface::kHighStartBitrate,
705 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000706
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000707 SetOptionFromOptionalConstraint(constraints,
708 MediaConstraintsInterface::kCombinedAudioVideoBwe,
709 &audio_options_.combined_audio_video_bwe);
710
kwiberg102c6a62015-10-30 02:47:38 -0700711 audio_options_.audio_jitter_buffer_max_packets =
Karl Wibergbe579832015-11-10 22:34:18 +0100712 rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200713
Karl Wibergbe579832015-11-10 22:34:18 +0100714 audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(
715 rtc_configuration.audio_jitter_buffer_fast_accelerate);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200716
Henrik Boström87713d02015-08-25 09:53:21 +0200717 if (!dtls_enabled_) {
718 // Construct with DTLS disabled.
719 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700720 signaling_thread(), channel_manager_, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200721 } else {
722 // Construct with DTLS enabled.
723 if (!certificate) {
724 // Use the |dtls_identity_store| to generate a certificate.
henrikg91d6ede2015-09-17 00:24:34 -0700725 RTC_DCHECK(dtls_identity_store);
Henrik Boström87713d02015-08-25 09:53:21 +0200726 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700727 signaling_thread(), channel_manager_, dtls_identity_store.Pass(),
728 this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200729 } else {
730 // Use the already generated certificate.
731 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
deadbeefab9b2d12015-10-14 11:33:11 -0700732 signaling_thread(), channel_manager_, certificate, this, id()));
Henrik Boström87713d02015-08-25 09:53:21 +0200733 }
734 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000735
Henrik Boströmd8281982015-08-27 10:12:24 +0200736 webrtc_session_desc_factory_->SignalCertificateReady.connect(
737 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000738
wu@webrtc.org97077a32013-10-25 21:18:33 +0000739 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000740 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000741 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000742 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200743 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 return true;
746}
747
deadbeefd59daf82015-10-14 15:02:44 -0700748void WebRtcSession::Close() {
749 SetState(STATE_CLOSED);
750 RemoveUnusedChannels(nullptr);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000751 ASSERT(!voice_channel_);
752 ASSERT(!video_channel_);
753 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754}
755
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000756void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
757 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758}
759
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000760cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
761 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762}
763
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000764bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
deadbeefd59daf82015-10-14 15:02:44 -0700765 if (!local_desc_ || !remote_desc_) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000766 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
767 << "SSL Role of the session.";
768 return false;
769 }
770
deadbeefd59daf82015-10-14 15:02:44 -0700771 return transport_controller_->GetSslRole(role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000772}
773
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000774void WebRtcSession::CreateOffer(
775 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700776 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
777 const cricket::MediaSessionOptions& session_options) {
778 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000779}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780
deadbeefab9b2d12015-10-14 11:33:11 -0700781void WebRtcSession::CreateAnswer(
782 CreateSessionDescriptionObserver* observer,
783 const MediaConstraintsInterface* constraints,
784 const cricket::MediaSessionOptions& session_options) {
785 webrtc_session_desc_factory_->CreateAnswer(observer, constraints,
786 session_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787}
788
789bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
790 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700791 ASSERT(signaling_thread()->IsCurrent());
792
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000793 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000794 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000795
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000796 // Validate SDP.
797 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
798 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 }
800
deadbeefd59daf82015-10-14 15:02:44 -0700801 // Update the initial_offerer flag if this session is the initial_offerer.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000802 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 if (state() == STATE_INIT && action == kOffer) {
deadbeefd59daf82015-10-14 15:02:44 -0700804 initial_offerer_ = true;
805 transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000808 cricket::SecurePolicy sdes_policy =
809 webrtc_session_desc_factory_->SdesPolicy();
810 cricket::CryptoType crypto_required = dtls_enabled_ ?
811 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
812 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000814 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000816 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817
818 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000819 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 // TODO(mallinath) - Handle CreateChannel failure, as new local description
821 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000822 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
824
deadbeefcbecd352015-09-23 11:50:27 -0700825 // Remove unused channels if MediaContentDescription is rejected.
826 RemoveUnusedChannels(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000828 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 return false;
830 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000831
deadbeefd59daf82015-10-14 15:02:44 -0700832 if (remote_desc_) {
833 // Now that we have a local description, we can push down remote candidates.
deadbeefcbecd352015-09-23 11:50:27 -0700834 UseCandidatesInSessionDescription(remote_desc_.get());
835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836
deadbeefd59daf82015-10-14 15:02:44 -0700837 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000838 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840 return true;
841}
842
843bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
844 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700845 ASSERT(signaling_thread()->IsCurrent());
846
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000847 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000848 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000849
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000850 // Validate SDP.
851 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
852 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
deadbeefd59daf82015-10-14 15:02:44 -0700855 rtc::scoped_ptr<SessionDescriptionInterface> old_remote_desc(
856 remote_desc_.release());
857 remote_desc_.reset(desc_temp.release());
858
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000860 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 if (action == kOffer && !CreateChannels(desc->description())) {
862 // TODO(mallinath) - Handle CreateChannel failure, as new local description
863 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000864 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 }
866
deadbeefcbecd352015-09-23 11:50:27 -0700867 // Remove unused channels if MediaContentDescription is rejected.
868 RemoveUnusedChannels(desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869
870 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
871 // is called.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000872 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return false;
874 }
875
deadbeefd59daf82015-10-14 15:02:44 -0700876 if (local_desc_ && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000877 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 }
879
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 // Check if this new SessionDescription contains new ice ufrag and password
881 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700882 bool ice_restart =
deadbeefd59daf82015-10-14 15:02:44 -0700883 ice_restart_latch_->CheckForRemoteIceRestart(old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700884 // We retain all received candidates only if ICE is not restarted.
885 // When ICE is restarted, all previous candidates belong to an old generation
886 // and should not be kept.
deadbeefd59daf82015-10-14 15:02:44 -0700887 // TODO(deadbeef): This goes against the W3C spec which says the remote
888 // description should only contain candidates from the last set remote
889 // description plus any candidates added since then. We should remove this
890 // once we're sure it won't break anything.
honghaiz503726c2015-07-31 12:37:38 -0700891 if (!ice_restart) {
892 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
deadbeefd59daf82015-10-14 15:02:44 -0700893 old_remote_desc.get(), desc);
honghaiz503726c2015-07-31 12:37:38 -0700894 }
895
deadbeefd59daf82015-10-14 15:02:44 -0700896 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000897 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000899
900 // Set the the ICE connection state to connecting since the connection may
901 // become writable with peer reflexive candidates before any remote candidate
902 // is signaled.
903 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
904 // is to have a new signal the indicates a change in checking state from the
905 // transport and expose a new checking() member from transport that can be
906 // read to determine the current checking state. The existing SignalConnecting
907 // actually means "gathering candidates", so cannot be be used here.
908 if (desc->type() != SessionDescriptionInterface::kOffer &&
909 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
910 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
911 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 return true;
913}
914
deadbeefd59daf82015-10-14 15:02:44 -0700915void WebRtcSession::LogState(State old_state, State new_state) {
916 LOG(LS_INFO) << "Session:" << id()
917 << " Old state:" << GetStateString(old_state)
918 << " New state:" << GetStateString(new_state);
919}
920
921void WebRtcSession::SetState(State state) {
922 ASSERT(signaling_thread_->IsCurrent());
923 if (state != state_) {
924 LogState(state_, state);
925 state_ = state;
926 SignalState(this, state_);
927 }
928}
929
930void WebRtcSession::SetError(Error error, const std::string& error_desc) {
931 ASSERT(signaling_thread_->IsCurrent());
932 if (error != error_) {
933 error_ = error;
934 error_desc_ = error_desc;
935 }
936}
937
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938bool WebRtcSession::UpdateSessionState(
939 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 std::string* err_desc) {
deadbeefcbecd352015-09-23 11:50:27 -0700941 ASSERT(signaling_thread()->IsCurrent());
942
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 // If there's already a pending error then no state transition should happen.
944 // But all call-sites should be verifying this before calling us!
deadbeefd59daf82015-10-14 15:02:44 -0700945 ASSERT(error() == ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000946 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000948 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
949 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 }
deadbeefd59daf82015-10-14 15:02:44 -0700951 SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER
952 : STATE_RECEIVEDOFFER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000953 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700954 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000955 }
deadbeefd59daf82015-10-14 15:02:44 -0700956 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000957 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 }
959 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000960 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
961 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
963 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700964 SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER
965 : STATE_RECEIVEDPRANSWER);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000966 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700967 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000968 }
deadbeefd59daf82015-10-14 15:02:44 -0700969 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000970 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
972 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000973 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
974 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
deadbeefcbecd352015-09-23 11:50:27 -0700976 const cricket::ContentGroup* local_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700977 local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700978 const cricket::ContentGroup* remote_bundle =
deadbeefd59daf82015-10-14 15:02:44 -0700979 remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
deadbeefcbecd352015-09-23 11:50:27 -0700980 if (local_bundle && remote_bundle) {
981 // The answerer decides the transport to bundle on
982 const cricket::ContentGroup* answer_bundle =
983 (source == cricket::CS_LOCAL ? local_bundle : remote_bundle);
984 if (!EnableBundle(*answer_bundle)) {
985 LOG(LS_WARNING) << "Failed to enable BUNDLE.";
986 return BadAnswerSdp(source, kEnableBundleFailed, err_desc);
987 }
988 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 EnableChannels();
deadbeefd59daf82015-10-14 15:02:44 -0700990 SetState(STATE_INPROGRESS);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000991 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
deadbeefd59daf82015-10-14 15:02:44 -0700992 SetError(ERROR_CONTENT, *err_desc);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000993 }
deadbeefd59daf82015-10-14 15:02:44 -0700994 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000995 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 }
997 }
998 return true;
999}
1000
1001WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1002 if (type == SessionDescriptionInterface::kOffer) {
1003 return WebRtcSession::kOffer;
1004 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1005 return WebRtcSession::kPrAnswer;
1006 } else if (type == SessionDescriptionInterface::kAnswer) {
1007 return WebRtcSession::kAnswer;
1008 }
1009 ASSERT(false && "unknown action type");
1010 return WebRtcSession::kOffer;
1011}
1012
deadbeefd59daf82015-10-14 15:02:44 -07001013bool WebRtcSession::PushdownMediaDescription(
1014 cricket::ContentAction action,
1015 cricket::ContentSource source,
1016 std::string* err) {
1017 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1018 if (!ch) {
1019 return true;
1020 } else if (source == cricket::CS_LOCAL) {
1021 return ch->PushdownLocalDescription(local_desc_->description(), action,
1022 err);
1023 } else {
1024 return ch->PushdownRemoteDescription(remote_desc_->description(), action,
1025 err);
1026 }
1027 };
1028
1029 return (set_content(voice_channel()) &&
1030 set_content(video_channel()) &&
1031 set_content(data_channel()));
1032}
1033
1034bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source,
1035 cricket::ContentAction action,
1036 std::string* error_desc) {
1037 RTC_DCHECK(signaling_thread()->IsCurrent());
1038
1039 if (source == cricket::CS_LOCAL) {
1040 return PushdownLocalTransportDescription(local_desc_->description(), action,
1041 error_desc);
1042 }
1043 return PushdownRemoteTransportDescription(remote_desc_->description(), action,
1044 error_desc);
1045}
1046
1047bool WebRtcSession::PushdownLocalTransportDescription(
1048 const SessionDescription* sdesc,
1049 cricket::ContentAction action,
1050 std::string* err) {
1051 RTC_DCHECK(signaling_thread()->IsCurrent());
1052
1053 if (!sdesc) {
1054 return false;
1055 }
1056
1057 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1058 if (!transport_controller_->SetLocalTransportDescription(
1059 tinfo.content_name, tinfo.description, action, err)) {
1060 return false;
1061 }
1062 }
1063
1064 return true;
1065}
1066
1067bool WebRtcSession::PushdownRemoteTransportDescription(
1068 const SessionDescription* sdesc,
1069 cricket::ContentAction action,
1070 std::string* err) {
1071 RTC_DCHECK(signaling_thread()->IsCurrent());
1072
1073 if (!sdesc) {
1074 return false;
1075 }
1076
1077 for (const TransportInfo& tinfo : sdesc->transport_infos()) {
1078 if (!transport_controller_->SetRemoteTransportDescription(
1079 tinfo.content_name, tinfo.description, action, err)) {
1080 return false;
1081 }
1082 }
1083
1084 return true;
1085}
1086
1087bool WebRtcSession::GetTransportDescription(
1088 const SessionDescription* description,
1089 const std::string& content_name,
1090 cricket::TransportDescription* tdesc) {
1091 if (!description || !tdesc) {
1092 return false;
1093 }
1094 const TransportInfo* transport_info =
1095 description->GetTransportInfoByName(content_name);
1096 if (!transport_info) {
1097 return false;
1098 }
1099 *tdesc = transport_info->description;
1100 return true;
1101}
1102
1103bool WebRtcSession::GetTransportStats(SessionStats* stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001104 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001105 return (GetChannelTransportStats(voice_channel(), stats) &&
1106 GetChannelTransportStats(video_channel(), stats) &&
1107 GetChannelTransportStats(data_channel(), stats));
1108}
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001109
deadbeefcbecd352015-09-23 11:50:27 -07001110bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch,
deadbeefd59daf82015-10-14 15:02:44 -07001111 SessionStats* stats) {
deadbeefcbecd352015-09-23 11:50:27 -07001112 ASSERT(signaling_thread()->IsCurrent());
1113 if (!ch) {
1114 // Not using this channel.
1115 return true;
1116 }
1117
1118 const std::string& content_name = ch->content_name();
1119 const std::string& transport_name = ch->transport_name();
1120 stats->proxy_to_transport[content_name] = transport_name;
1121 if (stats->transport_stats.find(transport_name) !=
1122 stats->transport_stats.end()) {
1123 // Transport stats already done for this transport.
1124 return true;
1125 }
1126
1127 cricket::TransportStats tstats;
deadbeefd59daf82015-10-14 15:02:44 -07001128 if (!transport_controller_->GetStats(transport_name, &tstats)) {
deadbeefcbecd352015-09-23 11:50:27 -07001129 return false;
1130 }
1131
1132 stats->transport_stats[transport_name] = tstats;
1133 return true;
1134}
1135
1136bool WebRtcSession::GetLocalCertificate(
1137 const std::string& transport_name,
1138 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1139 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001140 return transport_controller_->GetLocalCertificate(transport_name,
1141 certificate);
deadbeefcbecd352015-09-23 11:50:27 -07001142}
1143
1144bool WebRtcSession::GetRemoteSSLCertificate(const std::string& transport_name,
1145 rtc::SSLCertificate** cert) {
1146 ASSERT(signaling_thread()->IsCurrent());
deadbeefd59daf82015-10-14 15:02:44 -07001147 return transport_controller_->GetRemoteSSLCertificate(transport_name, cert);
deadbeefcbecd352015-09-23 11:50:27 -07001148}
1149
1150cricket::BaseChannel* WebRtcSession::GetChannel(
1151 const std::string& content_name) {
1152 if (voice_channel() && voice_channel()->content_name() == content_name) {
1153 return voice_channel();
1154 }
1155 if (video_channel() && video_channel()->content_name() == content_name) {
1156 return video_channel();
1157 }
1158 if (data_channel() && data_channel()->content_name() == content_name) {
1159 return data_channel();
1160 }
1161 return nullptr;
1162}
1163
1164bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) {
1165 const std::string* first_content_name = bundle.FirstContentName();
1166 if (!first_content_name) {
1167 LOG(LS_WARNING) << "Tried to BUNDLE with no contents.";
1168 return false;
1169 }
1170 const std::string& transport_name = *first_content_name;
1171 cricket::BaseChannel* first_channel = GetChannel(transport_name);
1172
1173 auto maybe_set_transport = [this, bundle, transport_name,
1174 first_channel](cricket::BaseChannel* ch) {
1175 if (!ch || !bundle.HasContentName(ch->content_name())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001176 return true;
1177 }
1178
deadbeefcbecd352015-09-23 11:50:27 -07001179 if (ch->transport_name() == transport_name) {
1180 LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name()
1181 << " on " << transport_name << ".";
1182 return true;
deadbeef47ee2f32015-09-22 15:08:23 -07001183 }
torbjornga81a42f2015-09-23 02:16:58 -07001184
deadbeefcbecd352015-09-23 11:50:27 -07001185 if (!ch->SetTransport(transport_name)) {
1186 LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name();
1187 return false;
1188 }
1189 LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on "
1190 << transport_name << ".";
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001191 return true;
1192 };
1193
deadbeefcbecd352015-09-23 11:50:27 -07001194 if (!maybe_set_transport(voice_channel()) ||
1195 !maybe_set_transport(video_channel()) ||
1196 !maybe_set_transport(data_channel())) {
1197 return false;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001198 }
deadbeefcbecd352015-09-23 11:50:27 -07001199
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001200 return true;
1201}
1202
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001204 if (!remote_desc_) {
1205 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1206 << "without any remote session description.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return false;
1208 }
1209
1210 if (!candidate) {
deadbeefd59daf82015-10-14 15:02:44 -07001211 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 return false;
1213 }
1214
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001215 bool valid = false;
deadbeefd59daf82015-10-14 15:02:44 -07001216 bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid);
1217 if (!valid) {
1218 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 }
1220
1221 // Add this candidate to the remote session description.
1222 if (!remote_desc_->AddCandidate(candidate)) {
deadbeefd59daf82015-10-14 15:02:44 -07001223 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 return false;
1225 }
1226
deadbeefd59daf82015-10-14 15:02:44 -07001227 if (ready) {
1228 return UseCandidate(candidate);
1229 } else {
1230 LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate.";
1231 return true;
1232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001235bool WebRtcSession::SetIceTransports(
1236 PeerConnectionInterface::IceTransportsType type) {
1237 return port_allocator()->set_candidate_filter(
1238 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001239}
1240
deadbeefd59daf82015-10-14 15:02:44 -07001241cricket::IceConfig WebRtcSession::ParseIceConfig(
1242 const PeerConnectionInterface::RTCConfiguration& config) const {
1243 cricket::IceConfig ice_config;
1244 ice_config.receiving_timeout_ms = config.ice_connection_receiving_timeout;
Honghai Zhang381b4212015-12-04 12:24:03 -08001245 ice_config.backup_connection_ping_interval =
1246 config.ice_backup_candidate_pair_ping_interval;
deadbeefd59daf82015-10-14 15:02:44 -07001247 ice_config.gather_continually = (config.continual_gathering_policy ==
1248 PeerConnectionInterface::GATHER_CONTINUALLY);
1249 return ice_config;
1250}
1251
1252void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) {
1253 transport_controller_->SetIceConfig(config);
1254}
1255
1256void WebRtcSession::MaybeStartGathering() {
1257 transport_controller_->MaybeStartGathering();
1258}
1259
Peter Boström0c4e06b2015-10-07 12:23:21 +02001260bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc,
1261 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001262 if (!local_desc_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001264 }
1265 return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266}
1267
Peter Boström0c4e06b2015-10-07 12:23:21 +02001268bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc,
1269 std::string* track_id) {
deadbeefd59daf82015-10-14 15:02:44 -07001270 if (!remote_desc_) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001271 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001272 }
1273 return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274}
1275
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001276std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001278 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 return desc.str();
1280}
1281
solenbergd4cec0d2015-10-09 08:55:48 -07001282void WebRtcSession::SetAudioPlayout(uint32_t ssrc, bool enable) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 ASSERT(signaling_thread()->IsCurrent());
1284 if (!voice_channel_) {
1285 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1286 return;
1287 }
solenberg4bac9c52015-10-09 02:32:53 -07001288 if (!voice_channel_->SetOutputVolume(ssrc, enable ? 1 : 0)) {
1289 // Allow that SetOutputVolume fail if |enable| is false but assert
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 // otherwise. This in the normal case when the underlying media channel has
1291 // already been deleted.
1292 ASSERT(enable == false);
1293 }
1294}
1295
Peter Boström0c4e06b2015-10-07 12:23:21 +02001296void WebRtcSession::SetAudioSend(uint32_t ssrc,
1297 bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001298 const cricket::AudioOptions& options,
1299 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 ASSERT(signaling_thread()->IsCurrent());
1301 if (!voice_channel_) {
1302 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1303 return;
1304 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001305 if (!voice_channel_->SetAudioSend(ssrc, enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001306 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001307 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308}
1309
Peter Boström0c4e06b2015-10-07 12:23:21 +02001310void WebRtcSession::SetAudioPlayoutVolume(uint32_t ssrc, double volume) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001311 ASSERT(signaling_thread()->IsCurrent());
1312 ASSERT(volume >= 0 && volume <= 10);
1313 if (!voice_channel_) {
1314 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1315 return;
1316 }
1317
solenberg4bac9c52015-10-09 02:32:53 -07001318 if (!voice_channel_->SetOutputVolume(ssrc, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001319 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001320 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001321}
1322
Tommif888bb52015-12-12 01:37:01 +01001323void WebRtcSession::SetRawAudioSink(uint32_t ssrc,
1324 rtc::scoped_ptr<AudioSinkInterface> sink) {
1325 ASSERT(signaling_thread()->IsCurrent());
1326 if (!voice_channel_)
1327 return;
1328
1329 voice_channel_->SetRawAudioSink(ssrc, std::move(sink));
1330}
1331
Peter Boström0c4e06b2015-10-07 12:23:21 +02001332bool WebRtcSession::SetCaptureDevice(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 cricket::VideoCapturer* camera) {
1334 ASSERT(signaling_thread()->IsCurrent());
1335
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001336 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1338 // support video.
1339 LOG(LS_WARNING) << "Video not used in this call.";
1340 return false;
1341 }
1342 if (!video_channel_->SetCapturer(ssrc, camera)) {
1343 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1344 // This in the normal case when the underlying media channel has already
1345 // been deleted.
1346 ASSERT(camera == NULL);
1347 return false;
1348 }
1349 return true;
1350}
1351
Peter Boström0c4e06b2015-10-07 12:23:21 +02001352void WebRtcSession::SetVideoPlayout(uint32_t ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 bool enable,
1354 cricket::VideoRenderer* renderer) {
1355 ASSERT(signaling_thread()->IsCurrent());
1356 if (!video_channel_) {
1357 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1358 return;
1359 }
1360 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1361 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1362 // This in the normal case when the underlying media channel has already
1363 // been deleted.
1364 ASSERT(renderer == NULL);
1365 }
1366}
1367
Peter Boström0c4e06b2015-10-07 12:23:21 +02001368void WebRtcSession::SetVideoSend(uint32_t ssrc,
1369 bool enable,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 const cricket::VideoOptions* options) {
1371 ASSERT(signaling_thread()->IsCurrent());
1372 if (!video_channel_) {
1373 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1374 return;
1375 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001376 if (!video_channel_->SetVideoSend(ssrc, enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1378 // This in the normal case when the underlying media channel has already
1379 // been deleted.
1380 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382}
1383
1384bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1385 ASSERT(signaling_thread()->IsCurrent());
1386 if (!voice_channel_) {
1387 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1388 return false;
1389 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001390 uint32_t send_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1392 // exists.
deadbeefd59daf82015-10-14 15:02:44 -07001393 if (!local_desc_ ||
1394 !GetAudioSsrcByTrackId(local_desc_->description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 &send_ssrc)) {
1396 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1397 return false;
1398 }
1399 return voice_channel_->CanInsertDtmf();
1400}
1401
1402bool WebRtcSession::InsertDtmf(const std::string& track_id,
1403 int code, int duration) {
1404 ASSERT(signaling_thread()->IsCurrent());
1405 if (!voice_channel_) {
1406 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1407 return false;
1408 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001409 uint32_t send_ssrc = 0;
deadbeefd59daf82015-10-14 15:02:44 -07001410 if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(),
1411 track_id, &send_ssrc))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1413 return false;
1414 }
solenberg1d63dd02015-12-02 12:35:09 -08001415 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1417 return false;
1418 }
1419 return true;
1420}
1421
1422sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1423 return &SignalVoiceChannelDestroyed;
1424}
1425
wu@webrtc.org78187522013-10-07 23:32:02 +00001426bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001427 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001428 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001429 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001430 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1431 return false;
1432 }
1433 return data_channel_->SendData(params, payload, result);
1434}
1435
1436bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001437 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001438 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1439 return false;
1440 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001441 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1442 &DataChannel::OnChannelReady);
1443 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1444 &DataChannel::OnDataReceived);
deadbeefab9b2d12015-10-14 11:33:11 -07001445 data_channel_->SignalStreamClosedRemotely.connect(
1446 webrtc_data_channel, &DataChannel::OnStreamClosedRemotely);
wu@webrtc.org78187522013-10-07 23:32:02 +00001447 return true;
1448}
1449
1450void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001451 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001452 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1453 return;
1454 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001455 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1456 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
deadbeefab9b2d12015-10-14 11:33:11 -07001457 data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel);
wu@webrtc.org78187522013-10-07 23:32:02 +00001458}
1459
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001460void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001461 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001462 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1463 return;
1464 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001465 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1466 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001467}
1468
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001469void WebRtcSession::RemoveSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001470 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001471 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1472 << "NULL.";
1473 return;
1474 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001475 data_channel_->RemoveRecvStream(sid);
1476 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001477}
1478
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001479bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001480 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001481}
1482
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483cricket::DataChannelType WebRtcSession::data_channel_type() const {
1484 return data_channel_type_;
1485}
1486
wu@webrtc.org91053e72013-08-10 07:18:04 +00001487bool WebRtcSession::IceRestartPending() const {
1488 return ice_restart_latch_->Get();
1489}
1490
1491void WebRtcSession::ResetIceRestartLatch() {
1492 ice_restart_latch_->Reset();
1493}
1494
Henrik Boströmd8281982015-08-27 10:12:24 +02001495void WebRtcSession::OnCertificateReady(
1496 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
deadbeefd59daf82015-10-14 15:02:44 -07001497 transport_controller_->SetLocalCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001498}
1499
Henrik Boströmd8281982015-08-27 10:12:24 +02001500bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001501 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001502}
1503
deadbeefcbecd352015-09-23 11:50:27 -07001504const rtc::scoped_refptr<rtc::RTCCertificate>&
1505WebRtcSession::certificate_for_testing() {
deadbeefd59daf82015-10-14 15:02:44 -07001506 return transport_controller_->certificate_for_testing();
deadbeefcbecd352015-09-23 11:50:27 -07001507}
1508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509void WebRtcSession::SetIceConnectionState(
1510 PeerConnectionInterface::IceConnectionState state) {
1511 if (ice_connection_state_ == state) {
1512 return;
1513 }
1514
1515 // ASSERT that the requested transition is allowed. Note that
1516 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1517 // within PeerConnection). This switch statement should compile away when
1518 // ASSERTs are disabled.
deadbeefcbecd352015-09-23 11:50:27 -07001519 LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_
1520 << " => " << state;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 switch (ice_connection_state_) {
1522 case PeerConnectionInterface::kIceConnectionNew:
1523 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1524 break;
1525 case PeerConnectionInterface::kIceConnectionChecking:
1526 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1527 state == PeerConnectionInterface::kIceConnectionConnected);
1528 break;
1529 case PeerConnectionInterface::kIceConnectionConnected:
1530 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1531 state == PeerConnectionInterface::kIceConnectionChecking ||
1532 state == PeerConnectionInterface::kIceConnectionCompleted);
1533 break;
1534 case PeerConnectionInterface::kIceConnectionCompleted:
1535 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1536 state == PeerConnectionInterface::kIceConnectionDisconnected);
1537 break;
1538 case PeerConnectionInterface::kIceConnectionFailed:
1539 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1540 break;
1541 case PeerConnectionInterface::kIceConnectionDisconnected:
1542 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1543 state == PeerConnectionInterface::kIceConnectionConnected ||
1544 state == PeerConnectionInterface::kIceConnectionCompleted ||
1545 state == PeerConnectionInterface::kIceConnectionFailed);
1546 break;
1547 case PeerConnectionInterface::kIceConnectionClosed:
1548 ASSERT(false);
1549 break;
1550 default:
1551 ASSERT(false);
1552 break;
1553 }
1554
1555 ice_connection_state_ = state;
1556 if (ice_observer_) {
1557 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1558 }
1559}
1560
deadbeefcbecd352015-09-23 11:50:27 -07001561void WebRtcSession::OnTransportControllerConnectionState(
1562 cricket::IceConnectionState state) {
1563 switch (state) {
1564 case cricket::kIceConnectionConnecting:
1565 // If the current state is Connected or Completed, then there were
1566 // writable channels but now there are not, so the next state must
1567 // be Disconnected.
1568 // kIceConnectionConnecting is currently used as the default,
1569 // un-connected state by the TransportController, so its only use is
1570 // detecting disconnections.
1571 if (ice_connection_state_ ==
1572 PeerConnectionInterface::kIceConnectionConnected ||
1573 ice_connection_state_ ==
1574 PeerConnectionInterface::kIceConnectionCompleted) {
1575 SetIceConnectionState(
1576 PeerConnectionInterface::kIceConnectionDisconnected);
1577 }
torbjornga81a42f2015-09-23 02:16:58 -07001578 break;
deadbeefcbecd352015-09-23 11:50:27 -07001579 case cricket::kIceConnectionFailed:
1580 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1581 break;
1582 case cricket::kIceConnectionConnected:
1583 LOG(LS_INFO) << "Changing to ICE connected state because "
1584 << "all transports are writable.";
1585 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1586 break;
1587 case cricket::kIceConnectionCompleted:
1588 LOG(LS_INFO) << "Changing to ICE completed state because "
1589 << "all transports are complete.";
1590 if (ice_connection_state_ !=
1591 PeerConnectionInterface::kIceConnectionConnected) {
1592 // If jumping directly from "checking" to "connected",
1593 // signal "connected" first.
1594 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
1595 }
1596 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
1597 if (metrics_observer_) {
1598 ReportTransportStats();
1599 }
1600 break;
1601 default:
1602 ASSERT(false);
torbjornga81a42f2015-09-23 02:16:58 -07001603 }
deadbeefcbecd352015-09-23 11:50:27 -07001604}
1605
1606void WebRtcSession::OnTransportControllerReceiving(bool receiving) {
Peter Thatcher54360512015-07-08 11:08:35 -07001607 SetIceConnectionReceiving(receiving);
1608}
1609
1610void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1611 if (ice_connection_receiving_ == receiving) {
1612 return;
1613 }
1614 ice_connection_receiving_ = receiving;
1615 if (ice_observer_) {
1616 ice_observer_->OnIceConnectionReceivingChange(receiving);
1617 }
1618}
1619
deadbeefcbecd352015-09-23 11:50:27 -07001620void WebRtcSession::OnTransportControllerCandidatesGathered(
1621 const std::string& transport_name,
1622 const cricket::Candidates& candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 ASSERT(signaling_thread()->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -07001624 int sdp_mline_index;
1625 if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
1626 LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name "
1627 << transport_name << " not found";
1628 return;
1629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630
deadbeefcbecd352015-09-23 11:50:27 -07001631 for (cricket::Candidates::const_iterator citer = candidates.begin();
1632 citer != candidates.end(); ++citer) {
1633 // Use transport_name as the candidate media id.
1634 JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer);
1635 if (ice_observer_) {
1636 ice_observer_->OnIceCandidate(&candidate);
1637 }
1638 if (local_desc_) {
1639 local_desc_->AddCandidate(&candidate);
1640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 }
1642}
1643
1644// Enabling voice and video channel.
1645void WebRtcSession::EnableChannels() {
1646 if (voice_channel_ && !voice_channel_->enabled())
1647 voice_channel_->Enable(true);
1648
1649 if (video_channel_ && !video_channel_->enabled())
1650 video_channel_->Enable(true);
1651
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001652 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 data_channel_->Enable(true);
1654}
1655
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656// Returns the media index for a local ice candidate given the content name.
1657bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1658 int* sdp_mline_index) {
deadbeefd59daf82015-10-14 15:02:44 -07001659 if (!local_desc_ || !sdp_mline_index) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 return false;
deadbeefd59daf82015-10-14 15:02:44 -07001661 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662
1663 bool content_found = false;
deadbeefd59daf82015-10-14 15:02:44 -07001664 const ContentInfos& contents = local_desc_->description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 for (size_t index = 0; index < contents.size(); ++index) {
1666 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001667 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 content_found = true;
1669 break;
1670 }
1671 }
1672 return content_found;
1673}
1674
1675bool WebRtcSession::UseCandidatesInSessionDescription(
1676 const SessionDescriptionInterface* remote_desc) {
deadbeefd59daf82015-10-14 15:02:44 -07001677 if (!remote_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 return true;
deadbeefd59daf82015-10-14 15:02:44 -07001679 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001681
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1683 const IceCandidateCollection* candidates = remote_desc->candidates(m);
deadbeefd59daf82015-10-14 15:02:44 -07001684 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001685 const IceCandidateInterface* candidate = candidates->at(n);
1686 bool valid = false;
1687 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1688 if (valid) {
deadbeefd59daf82015-10-14 15:02:44 -07001689 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use "
1690 << "candidate.";
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001691 }
1692 continue;
1693 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001694 ret = UseCandidate(candidate);
deadbeefd59daf82015-10-14 15:02:44 -07001695 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 break;
deadbeefd59daf82015-10-14 15:02:44 -07001697 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 }
1699 }
1700 return ret;
1701}
1702
1703bool WebRtcSession::UseCandidate(
1704 const IceCandidateInterface* candidate) {
1705
1706 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
deadbeefd59daf82015-10-14 15:02:44 -07001707 size_t remote_content_size = remote_desc_->description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 if (mediacontent_index >= remote_content_size) {
1709 LOG(LS_ERROR)
1710 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1711 return false;
1712 }
1713
1714 cricket::ContentInfo content =
deadbeefd59daf82015-10-14 15:02:44 -07001715 remote_desc_->description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 std::vector<cricket::Candidate> candidates;
1717 candidates.push_back(candidate->candidate());
1718 // Invoking BaseSession method to handle remote candidates.
1719 std::string error;
deadbeefd59daf82015-10-14 15:02:44 -07001720 if (transport_controller_->AddRemoteCandidates(content.name, candidates,
1721 &error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 // Candidates successfully submitted for checking.
1723 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1724 ice_connection_state_ ==
1725 PeerConnectionInterface::kIceConnectionDisconnected) {
1726 // If state is New, then the session has just gotten its first remote ICE
1727 // candidates, so go to Checking.
1728 // If state is Disconnected, the session is re-using old candidates or
1729 // receiving additional ones, so go to Checking.
1730 // If state is Connected, stay Connected.
1731 // TODO(bemasc): If state is Connected, and the new candidates are for a
1732 // newly added transport, then the state actually _should_ move to
1733 // checking. Add a way to distinguish that case.
1734 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1735 }
1736 // TODO(bemasc): If state is Completed, go back to Connected.
1737 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001738 if (!error.empty()) {
1739 LOG(LS_WARNING) << error;
1740 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 }
1742 return true;
1743}
1744
deadbeefcbecd352015-09-23 11:50:27 -07001745void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001746 // Destroy video_channel_ first since it may have a pointer to the
1747 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 const cricket::ContentInfo* video_info =
1749 cricket::GetFirstVideoContent(desc);
1750 if ((!video_info || video_info->rejected) && video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 SignalVideoChannelDestroyed();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 channel_manager_->DestroyVideoChannel(video_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
1754
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001755 const cricket::ContentInfo* voice_info =
1756 cricket::GetFirstAudioContent(desc);
1757 if ((!voice_info || voice_info->rejected) && voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001758 SignalVoiceChannelDestroyed();
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();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 channel_manager_->DestroyDataChannel(data_channel_.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 }
1768}
1769
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001770// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001771// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 // Creating the media channels and transport proxies.
1774 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1775 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001776 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 LOG(LS_ERROR) << "Failed to create voice channel.";
1778 return false;
1779 }
1780 }
1781
1782 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1783 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001784 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_ERROR) << "Failed to create video channel.";
1786 return false;
1787 }
1788 }
1789
1790 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1791 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001792 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_ERROR) << "Failed to create data channel.";
1795 return false;
1796 }
1797 }
1798
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001799 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1800 if (voice_channel()) {
1801 voice_channel()->ActivateRtcpMux();
1802 }
1803 if (video_channel()) {
1804 video_channel()->ActivateRtcpMux();
1805 }
1806 if (data_channel()) {
1807 data_channel()->ActivateRtcpMux();
1808 }
1809 }
1810
deadbeefcbecd352015-09-23 11:50:27 -07001811 // Enable BUNDLE immediately when kBundlePolicyMaxBundle is in effect.
Donald Curtis0e209b02015-03-24 09:29:54 -07001812 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001813 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1814 cricket::GROUP_TYPE_BUNDLE);
1815 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001816 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1817 return false;
1818 }
deadbeefcbecd352015-09-23 11:50:27 -07001819 if (!EnableBundle(*bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001820 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1821 return false;
1822 }
1823 }
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 return true;
1826}
1827
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001828bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001830 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001831 audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001832 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001833 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001834 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001835
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001836 voice_channel_->SignalDtlsSetupFailure.connect(
1837 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001838
1839 SignalVoiceChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001840 voice_channel_->transport_channel()->SignalSentPacket.connect(
1841 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001842 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843}
1844
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001845bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 video_channel_.reset(channel_manager_->CreateVideoChannel(
stefanc1aeaf02015-10-15 07:26:07 -07001847 media_controller_, transport_controller_.get(), content->name, true,
deadbeefcbecd352015-09-23 11:50:27 -07001848 video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001849 if (!video_channel_) {
1850 return false;
1851 }
1852
1853 video_channel_->SignalDtlsSetupFailure.connect(
1854 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001855
1856 SignalVideoChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001857 video_channel_->transport_channel()->SignalSentPacket.connect(
1858 this, &WebRtcSession::OnSentPacket_w);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001859 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860}
1861
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001862bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001863 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 data_channel_.reset(channel_manager_->CreateDataChannel(
deadbeefd59daf82015-10-14 15:02:44 -07001865 transport_controller_.get(), content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001866 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001867 return false;
1868 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001869
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001870 if (sctp) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001871 data_channel_->SignalDataReceived.connect(
1872 this, &WebRtcSession::OnDataChannelMessageReceived);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001873 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001874
1875 data_channel_->SignalDtlsSetupFailure.connect(
1876 this, &WebRtcSession::OnDtlsSetupFailure);
deadbeefab9b2d12015-10-14 11:33:11 -07001877
1878 SignalDataChannelCreated();
stefanc1aeaf02015-10-15 07:26:07 -07001879 data_channel_->transport_channel()->SignalSentPacket.connect(
1880 this, &WebRtcSession::OnSentPacket_w);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001881 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001884void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
deadbeefd59daf82015-10-14 15:02:44 -07001885 SetError(ERROR_TRANSPORT,
1886 rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887}
1888
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001889void WebRtcSession::OnDataChannelMessageReceived(
1890 cricket::DataChannel* channel,
1891 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001892 const rtc::Buffer& payload) {
deadbeefab9b2d12015-10-14 11:33:11 -07001893 RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP);
1894 if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) {
1895 // Received OPEN message; parse and signal that a new data channel should
1896 // be created.
1897 std::string label;
1898 InternalDataChannelInit config;
1899 config.id = params.ssrc;
1900 if (!ParseDataChannelOpenMessage(payload, &label, &config)) {
1901 LOG(LS_WARNING) << "Failed to parse the OPEN message for sid "
1902 << params.ssrc;
1903 return;
1904 }
1905 config.open_handshake_role = InternalDataChannelInit::kAcker;
1906 SignalDataChannelOpenMessage(label, config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 }
deadbeefab9b2d12015-10-14 11:33:11 -07001908 // Otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909}
1910
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001911// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001912bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001913 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1914 if (!bundle_enabled)
1915 return true;
1916
1917 const cricket::ContentGroup* bundle_group =
1918 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1919 ASSERT(bundle_group != NULL);
1920
1921 const cricket::ContentInfos& contents = desc->contents();
1922 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1923 citer != contents.end(); ++citer) {
1924 const cricket::ContentInfo* content = (&*citer);
1925 ASSERT(content != NULL);
1926 if (bundle_group->HasContentName(content->name) &&
1927 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1928 if (!HasRtcpMuxEnabled(content))
1929 return false;
1930 }
1931 }
1932 // RTCP-MUX is enabled in all the contents.
1933 return true;
1934}
1935
1936bool WebRtcSession::HasRtcpMuxEnabled(
1937 const cricket::ContentInfo* content) {
1938 const cricket::MediaContentDescription* description =
1939 static_cast<cricket::MediaContentDescription*>(content->description);
1940 return description->rtcp_mux();
1941}
1942
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001943bool WebRtcSession::ValidateSessionDescription(
1944 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001945 cricket::ContentSource source, std::string* err_desc) {
1946 std::string type;
deadbeefd59daf82015-10-14 15:02:44 -07001947 if (error() != ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001948 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001949 }
1950
1951 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001952 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001953 }
1954
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001955 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001956 Action action = GetAction(sdesc->type());
1957 if (source == cricket::CS_LOCAL) {
1958 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001959 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001960 } else {
1961 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001962 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001963 }
1964
1965 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001966 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001967 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1968 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001969 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001970 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001971 }
1972
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001973 // Verify ice-ufrag and ice-pwd.
1974 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001975 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001976 }
1977
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001978 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001979 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001980 }
1981
1982 // Verify m-lines in Answer when compared against Offer.
1983 if (action == kAnswer) {
1984 const cricket::SessionDescription* offer_desc =
deadbeefd59daf82015-10-14 15:02:44 -07001985 (source == cricket::CS_LOCAL) ? remote_desc_->description()
1986 : local_desc_->description();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001987 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001988 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001989 }
1990 }
1991
1992 return true;
1993}
1994
1995bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1996 return ((action == kOffer && state() == STATE_INIT) ||
1997 // update local offer
deadbeefd59daf82015-10-14 15:02:44 -07001998 (action == kOffer && state() == STATE_SENTOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001999 // update the current ongoing session.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002000 (action == kOffer && state() == STATE_INPROGRESS) ||
2001 // accept remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002002 (action == kAnswer && state() == STATE_RECEIVEDOFFER) ||
2003 (action == kAnswer && state() == STATE_SENTPRANSWER) ||
2004 (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) ||
2005 (action == kPrAnswer && state() == STATE_SENTPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002006}
2007
2008bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
2009 return ((action == kOffer && state() == STATE_INIT) ||
2010 // update remote offer
deadbeefd59daf82015-10-14 15:02:44 -07002011 (action == kOffer && state() == STATE_RECEIVEDOFFER) ||
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002012 // update the current ongoing session
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002013 (action == kOffer && state() == STATE_INPROGRESS) ||
2014 // accept local offer
deadbeefd59daf82015-10-14 15:02:44 -07002015 (action == kAnswer && state() == STATE_SENTOFFER) ||
2016 (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) ||
2017 (action == kPrAnswer && state() == STATE_SENTOFFER) ||
2018 (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002019}
2020
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002021std::string WebRtcSession::GetSessionErrorMsg() {
2022 std::ostringstream desc;
2023 desc << kSessionError << GetErrorCodeString(error()) << ". ";
2024 desc << kSessionErrorDesc << error_desc() << ".";
2025 return desc.str();
2026}
2027
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002028// We need to check the local/remote description for the Transport instead of
2029// the session, because a new Transport added during renegotiation may have
2030// them unset while the session has them set from the previous negotiation.
2031// Not doing so may trigger the auto generation of transport description and
2032// mess up DTLS identity information, ICE credential, etc.
2033bool WebRtcSession::ReadyToUseRemoteCandidate(
2034 const IceCandidateInterface* candidate,
2035 const SessionDescriptionInterface* remote_desc,
2036 bool* valid) {
2037 *valid = true;;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002038
2039 const SessionDescriptionInterface* current_remote_desc =
deadbeefd59daf82015-10-14 15:02:44 -07002040 remote_desc ? remote_desc : remote_desc_.get();
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002041
deadbeefd59daf82015-10-14 15:02:44 -07002042 if (!current_remote_desc) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002043 return false;
deadbeefd59daf82015-10-14 15:02:44 -07002044 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002045
2046 size_t mediacontent_index =
2047 static_cast<size_t>(candidate->sdp_mline_index());
2048 size_t remote_content_size =
2049 current_remote_desc->description()->contents().size();
2050 if (mediacontent_index >= remote_content_size) {
2051 LOG(LS_ERROR)
2052 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2053
2054 *valid = false;
2055 return false;
2056 }
2057
2058 cricket::ContentInfo content =
2059 current_remote_desc->description()->contents()[mediacontent_index];
deadbeefcbecd352015-09-23 11:50:27 -07002060 cricket::BaseChannel* channel = GetChannel(content.name);
2061 if (!channel) {
2062 return false;
2063 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002064
deadbeefd59daf82015-10-14 15:02:44 -07002065 return transport_controller_->ReadyForRemoteCandidates(
deadbeefcbecd352015-09-23 11:50:27 -07002066 channel->transport_name());
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00002067}
2068
deadbeefcbecd352015-09-23 11:50:27 -07002069void WebRtcSession::OnTransportControllerGatheringState(
2070 cricket::IceGatheringState state) {
2071 ASSERT(signaling_thread()->IsCurrent());
2072 if (state == cricket::kIceGatheringGathering) {
2073 if (ice_observer_) {
2074 ice_observer_->OnIceGatheringChange(
2075 PeerConnectionInterface::kIceGatheringGathering);
2076 }
2077 } else if (state == cricket::kIceGatheringComplete) {
2078 if (ice_observer_) {
2079 ice_observer_->OnIceGatheringChange(
2080 PeerConnectionInterface::kIceGatheringComplete);
2081 ice_observer_->OnIceComplete();
2082 }
2083 }
2084}
2085
2086void WebRtcSession::ReportTransportStats() {
2087 // Use a set so we don't report the same stats twice if two channels share
2088 // a transport.
2089 std::set<std::string> transport_names;
2090 if (voice_channel()) {
2091 transport_names.insert(voice_channel()->transport_name());
2092 }
2093 if (video_channel()) {
2094 transport_names.insert(video_channel()->transport_name());
2095 }
2096 if (data_channel()) {
2097 transport_names.insert(data_channel()->transport_name());
2098 }
2099 for (const auto& name : transport_names) {
2100 cricket::TransportStats stats;
deadbeefd59daf82015-10-14 15:02:44 -07002101 if (transport_controller_->GetStats(name, &stats)) {
deadbeefcbecd352015-09-23 11:50:27 -07002102 ReportBestConnectionState(stats);
2103 ReportNegotiatedCiphers(stats);
2104 }
2105 }
2106}
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002107// Walk through the ConnectionInfos to gather best connection usage
2108// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002109void WebRtcSession::ReportBestConnectionState(
2110 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002111 RTC_DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002112 for (cricket::TransportChannelStatsList::const_iterator it =
2113 stats.channel_stats.begin();
2114 it != stats.channel_stats.end(); ++it) {
2115 for (cricket::ConnectionInfos::const_iterator it_info =
2116 it->connection_infos.begin();
2117 it_info != it->connection_infos.end(); ++it_info) {
2118 if (!it_info->best_connection) {
2119 continue;
2120 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002121
2122 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2123 const cricket::Candidate& local = it_info->local_candidate;
2124 const cricket::Candidate& remote = it_info->remote_candidate;
2125
2126 // Increment the counter for IceCandidatePairType.
2127 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2128 (local.type() == RELAY_PORT_TYPE &&
2129 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2130 type = kEnumCounterIceCandidatePairTypeTcp;
2131 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2132 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002133 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002134 RTC_CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002135 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002136 metrics_observer_->IncrementEnumCounter(
2137 type, GetIceCandidatePairCounter(local, remote),
2138 kIceCandidatePairMax);
2139
2140 // Increment the counter for IP type.
2141 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002142 metrics_observer_->IncrementEnumCounter(
2143 kEnumCounterAddressFamily, kBestConnections_IPv4,
2144 kPeerConnectionAddressFamilyCounter_Max);
2145
2146 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002147 metrics_observer_->IncrementEnumCounter(
2148 kEnumCounterAddressFamily, kBestConnections_IPv6,
2149 kPeerConnectionAddressFamilyCounter_Max);
2150 } else {
henrikg91d6ede2015-09-17 00:24:34 -07002151 RTC_CHECK(0);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002152 }
2153
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002154 return;
2155 }
2156 }
2157}
2158
jbauchac8869e2015-07-03 01:36:14 -07002159void WebRtcSession::ReportNegotiatedCiphers(
2160 const cricket::TransportStats& stats) {
henrikg91d6ede2015-09-17 00:24:34 -07002161 RTC_DCHECK(metrics_observer_ != NULL);
jbauchac8869e2015-07-03 01:36:14 -07002162 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2163 return;
2164 }
2165
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002166 int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite;
2167 int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite;
2168 if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE &&
2169 ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) {
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
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002189 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) {
2190 metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type,
2191 srtp_crypto_suite);
jbauchac8869e2015-07-03 01:36:14 -07002192 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002193 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) {
2194 metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type,
2195 ssl_cipher_suite);
jbauchac8869e2015-07-03 01:36:14 -07002196 }
2197}
2198
stefanc1aeaf02015-10-15 07:26:07 -07002199void WebRtcSession::OnSentPacket_w(cricket::TransportChannel* channel,
2200 const rtc::SentPacket& sent_packet) {
2201 RTC_DCHECK(worker_thread()->IsCurrent());
2202 media_controller_->call_w()->OnSentPacket(sent_packet);
2203}
2204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205} // namespace webrtc